From 1dd22f503546e220c4a24e9dc213955a5965be5b Mon Sep 17 00:00:00 2001 From: Seth Hillbrand Date: Tue, 23 Oct 2018 19:14:11 -0700 Subject: [PATCH] SELECTION: Utilize sorted deque Many operations on the selection involved getting an item by index or dereference. The std::set needed to iterate from the beginning of the set to find the item in its red-black tree. Using a deque that we keep sorted, we get index access in O(1) and item search in O(lg N). --- include/tool/selection.h | 50 +++++++++++++++++++++------------------- 1 file changed, 26 insertions(+), 24 deletions(-) diff --git a/include/tool/selection.h b/include/tool/selection.h index e30a034756..fa433215c9 100644 --- a/include/tool/selection.h +++ b/include/tool/selection.h @@ -26,7 +26,7 @@ #ifndef __SELECTION_H #define __SELECTION_H -#include +#include #include #include @@ -53,8 +53,8 @@ public: return *this; } - using ITER = std::set::iterator; - using CITER = std::set::const_iterator; + using ITER = std::deque::iterator; + using CITER = std::deque::const_iterator; ITER begin() { return m_items.begin(); } ITER end() { return m_items.end(); } @@ -73,12 +73,18 @@ public: virtual void Add( EDA_ITEM* aItem ) { - m_items.insert( aItem ); + CITER i = std::lower_bound( m_items.begin(), m_items.end(), aItem ); + + if( i == m_items.end() || *i > aItem ) + m_items.insert( i, aItem ); } virtual void Remove( EDA_ITEM *aItem ) { - m_items.erase( aItem ); + CITER i = std::lower_bound( m_items.begin(), m_items.end(), aItem ); + + if( !( i == m_items.end() || *i > aItem ) ) + m_items.erase( i ); } virtual void Clear() override @@ -91,18 +97,19 @@ public: return m_items.size(); } - virtual KIGFX::VIEW_ITEM* GetItem( unsigned int idx ) const override + virtual KIGFX::VIEW_ITEM* GetItem( unsigned int aIdx ) const override { - auto iter = m_items.begin(); + if( aIdx < m_items.size() ) + return m_items[ aIdx ]; - std::advance( iter, idx ); - - return *iter; + return nullptr; } bool Contains( EDA_ITEM* aItem ) const { - return m_items.find( aItem ) != m_items.end(); + CITER i = std::lower_bound( m_items.begin(), m_items.end(), aItem ); + + return !( i == m_items.end() || *i > aItem ); } /// Checks if there is anything selected @@ -117,7 +124,7 @@ public: return m_items.size(); } - const std::set GetItems() const + const std::deque GetItems() const { return m_items; } @@ -134,25 +141,20 @@ public: EDA_ITEM* GetTopLeftItem( bool onlyModules = false ) const; EDA_ITEM* GetTopLeftModule() const; - EDA_ITEM* operator[]( const int index ) const + EDA_ITEM* operator[]( const size_t aIdx ) const { - if( index < 0 || (unsigned int) index >= m_items.size() ) - return nullptr; + if( aIdx < m_items.size() ) + return m_items[ aIdx ]; - auto iter = m_items.begin(); - std::advance( iter, index ); - return *iter; + return nullptr; } EDA_ITEM* Front() const { - if ( !m_items.size() ) - return nullptr; - - return *m_items.begin(); + return m_items.front(); } - std::set& Items() + std::deque& Items() { return m_items; } @@ -215,7 +217,7 @@ private: OPT m_referencePoint; /// Set of selected items - std::set m_items; + std::deque m_items; bool m_isHover;