You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

324 lines
9.7 KiB

5 years ago
5 years ago
  1. /*
  2. * This program source code file is part of KiCad, a free EDA CAD application.
  3. *
  4. * Copyright 2017 CERN
  5. * Copyright (C) 2020-2023 KiCad Developers, see AUTHORS.txt for contributors.
  6. *
  7. * @author Maciej Suminski <maciej.suminski@cern.ch>
  8. * @author Bernhard Stegmaier <stegmaier@sw-systems.de>
  9. *
  10. * This program is free software; you can redistribute it and/or
  11. * modify it under the terms of the GNU General Public License
  12. * as published by the Free Software Foundation; either version 2
  13. * of the License, or (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with this program; if not, you may find one here:
  22. * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
  23. * or you may search the http://www.gnu.org website for the version 2 license,
  24. * or you may write to the Free Software Foundation, Inc.,
  25. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
  26. */
  27. #ifndef MULTIVECTOR_H
  28. #define MULTIVECTOR_H
  29. #include <wx/debug.h>
  30. #include <boost/ptr_container/ptr_vector.hpp>
  31. #include <stdexcept>
  32. /**
  33. * Multivector container type.
  34. *
  35. * Keeps items segregated by their type in multiple ptr_vectors. Provides both
  36. * access as a flat list as well as access by type of item.
  37. *
  38. * T is the stored type, needs to provide Type() method used to segregate items.
  39. * FIRST_TYPE_VAL is the lower boundary value of the types stored in the container.
  40. * LAST_TYPE_VAL is the upper boundary value of the types stored in the container.
  41. */
  42. template<typename T, int FIRST_TYPE_VAL, int LAST_TYPE_VAL>
  43. class MULTIVECTOR
  44. {
  45. public:
  46. /**
  47. * Type value to indicate no specific type. Mostly used to access the container as a flat list
  48. * or to return data for the whole container.
  49. */
  50. static constexpr int UNDEFINED_TYPE = 0;
  51. static_assert( FIRST_TYPE_VAL > UNDEFINED_TYPE,
  52. "FIRST_TYPE_VAL must be greater than UNDEFINED_TYPE" );
  53. static_assert( FIRST_TYPE_VAL < LAST_TYPE_VAL,
  54. "FIRST_TYPE_VAL must be greater than LAST_TYPE_VAL" );
  55. /**
  56. * Helper for defining a list of library draw object pointers.
  57. *
  58. * The Boost pointer containers are responsible for deleting object pointers placed
  59. * in them. If you access a object pointer from the list, do not delete it directly.
  60. */
  61. typedef boost::ptr_vector<T> ITEM_PTR_VECTOR;
  62. /**
  63. * Generic implementation of a flat const/non-const iterator over contained items.
  64. */
  65. template<typename ITEM_TYPE, typename ITEM_CONTAINER, typename ITEM_CONTAINER_IT>
  66. class ITERATOR_BASE
  67. {
  68. public:
  69. ITEM_TYPE& operator*()
  70. {
  71. return *m_it;
  72. }
  73. ITEM_TYPE* operator->()
  74. {
  75. return &( *m_it );
  76. }
  77. ITERATOR_BASE& operator++()
  78. {
  79. if( m_it != (*m_parent)[ m_curType ].end() )
  80. ++m_it;
  81. validate();
  82. return *this;
  83. }
  84. bool operator!=( const ITERATOR_BASE& aOther ) const
  85. {
  86. if( aOther.m_parent != m_parent )
  87. return true;
  88. if( aOther.m_filter != m_filter )
  89. return true;
  90. if( aOther.m_curType != m_curType )
  91. return true;
  92. return aOther.m_it != m_it;
  93. }
  94. protected:
  95. /**
  96. * @param aItems is the container to wrap.
  97. * @param aIt is the iterator to initialize this iterator (usually some begin() or end()
  98. * iterator).
  99. * @param aBucket is the type ID of the given iterator.
  100. * @param aType enables item type filtering. When aType is UNDEFINED_TYPE, there is no
  101. * filtering and all item types are accessible by the iterator.
  102. */
  103. ITERATOR_BASE( ITEM_CONTAINER* aItems, ITEM_CONTAINER_IT aIt,
  104. int aBucket, int aType = UNDEFINED_TYPE )
  105. : m_parent( aItems ), m_it( aIt ), m_curType( aBucket )
  106. {
  107. m_filter = ( aType != UNDEFINED_TYPE );
  108. }
  109. ///< Assures the iterator is in a valid state.
  110. void validate()
  111. {
  112. // for all-items iterators (unfiltered): check if this is the end of the
  113. // current type container, if so switch to the next non-empty container
  114. if( !m_filter && m_it == (*m_parent)[ m_curType ].end() )
  115. {
  116. // switch to the next type (look for a not empty container)
  117. int nextType = m_curType;
  118. do
  119. ++nextType;
  120. while( ( nextType <= LAST_TYPE ) && (*m_parent)[ nextType ].empty() );
  121. // there is another not empty container, so make the iterator point to it,
  122. // otherwise it means the iterator points to the last item
  123. if( nextType <= LAST_TYPE )
  124. {
  125. m_curType = nextType;
  126. m_it = (*m_parent)[ m_curType ].begin();
  127. }
  128. }
  129. }
  130. ///< Wrapped container
  131. ITEM_CONTAINER* m_parent;
  132. ///< Iterator for one of the ptr_vector containers stored in the array
  133. ITEM_CONTAINER_IT m_it;
  134. ///< Flag indicating whether type filtering is enabled
  135. bool m_filter;
  136. ///< Type of the currently iterated items
  137. int m_curType;
  138. friend class MULTIVECTOR;
  139. };
  140. ///< The non-const iterator
  141. typedef ITERATOR_BASE<T, MULTIVECTOR<T, FIRST_TYPE_VAL, LAST_TYPE_VAL>,
  142. typename ITEM_PTR_VECTOR::iterator> ITERATOR;
  143. ///< The const iterator
  144. typedef ITERATOR_BASE<const T, const MULTIVECTOR<T, FIRST_TYPE_VAL, LAST_TYPE_VAL>,
  145. typename ITEM_PTR_VECTOR::const_iterator> CONST_ITERATOR;
  146. MULTIVECTOR()
  147. {
  148. }
  149. void push_back( T* aItem )
  150. {
  151. operator[]( aItem->Type() ).push_back( aItem );
  152. }
  153. ITERATOR erase( const ITERATOR& aIterator )
  154. {
  155. ITERATOR it( aIterator );
  156. it.m_it = (*aIterator.m_parent)[ aIterator.m_curType ].erase( aIterator.m_it );
  157. it.validate();
  158. return it;
  159. }
  160. ITERATOR begin( int aType = UNDEFINED_TYPE )
  161. {
  162. int bucket = ( aType != UNDEFINED_TYPE ) ? aType : first();
  163. return ITERATOR( this, operator[]( bucket ).begin(), bucket, aType );
  164. }
  165. ITERATOR end( int aType = UNDEFINED_TYPE )
  166. {
  167. int bucket = ( aType != UNDEFINED_TYPE ) ? aType : last();
  168. return ITERATOR( this, operator[]( bucket ).end(), bucket, aType );
  169. }
  170. CONST_ITERATOR begin( int aType = UNDEFINED_TYPE ) const
  171. {
  172. int bucket = ( aType != UNDEFINED_TYPE ) ? aType : first();
  173. return CONST_ITERATOR( this, operator[]( bucket ).begin(), bucket, aType );
  174. }
  175. CONST_ITERATOR end( int aType = UNDEFINED_TYPE ) const
  176. {
  177. int bucket = ( aType != UNDEFINED_TYPE ) ? aType : last();
  178. return CONST_ITERATOR( this, operator[]( bucket ).end(), bucket, aType );
  179. }
  180. void clear( int aType = UNDEFINED_TYPE )
  181. {
  182. if( aType != UNDEFINED_TYPE )
  183. {
  184. operator[]( aType ).clear();
  185. }
  186. else
  187. {
  188. for( int i = 0; i < TYPES_COUNT; ++i)
  189. m_data[ i ].clear();
  190. }
  191. }
  192. size_t size( int aType = UNDEFINED_TYPE ) const
  193. {
  194. if( aType != UNDEFINED_TYPE )
  195. {
  196. return operator[]( aType ).size();
  197. }
  198. else
  199. {
  200. size_t cnt = 0;
  201. for( int i = 0; i < TYPES_COUNT; ++i)
  202. cnt += m_data[ i ].size();
  203. return cnt;
  204. }
  205. }
  206. bool empty( int aType = UNDEFINED_TYPE ) const
  207. {
  208. return ( size( aType ) == 0 );
  209. }
  210. void sort()
  211. {
  212. for( int i = 0; i < TYPES_COUNT; ++i )
  213. m_data[ i ].sort();
  214. }
  215. /**
  216. * Remove duplicate elements in list
  217. */
  218. void unique()
  219. {
  220. for( int i = 0; i < TYPES_COUNT; ++i )
  221. {
  222. if( m_data[ i ].size() > 1 )
  223. m_data[ i ].unique();
  224. }
  225. }
  226. ITEM_PTR_VECTOR& operator[]( int aType )
  227. {
  228. if( ( aType < FIRST_TYPE ) || ( aType > LAST_TYPE ) )
  229. {
  230. wxFAIL_MSG( wxT( "Attempted access to type not within MULTIVECTOR" ) );
  231. // return type is a reference so we have to return something...
  232. aType = FIRST_TYPE;
  233. }
  234. return m_data[ aType - FIRST_TYPE ];
  235. }
  236. const ITEM_PTR_VECTOR& operator[]( int aType ) const
  237. {
  238. if( ( aType < FIRST_TYPE ) || ( aType > LAST_TYPE ) )
  239. {
  240. wxFAIL_MSG( wxT( "Attempted access to type not within MULTIVECTOR" ) );
  241. // return type is a reference so we have to return something...
  242. aType = FIRST_TYPE;
  243. }
  244. return m_data[ aType - FIRST_TYPE ];
  245. }
  246. // Range of valid types handled by the iterator
  247. static constexpr int FIRST_TYPE = FIRST_TYPE_VAL;
  248. static constexpr int LAST_TYPE = LAST_TYPE_VAL;
  249. static constexpr int TYPES_COUNT = LAST_TYPE - FIRST_TYPE + 1;
  250. private:
  251. ///< Get first non-empty type or first type if all are empty.
  252. int first() const
  253. {
  254. int i = 0;
  255. while( ( i < TYPES_COUNT ) && ( m_data[ i ].empty() ) )
  256. ++i;
  257. return ( i == TYPES_COUNT ) ? FIRST_TYPE : FIRST_TYPE + i;
  258. }
  259. ///< Get last non-empty type or first type if all are empty.
  260. int last() const
  261. {
  262. int i = TYPES_COUNT - 1;
  263. while( ( i >= 0 ) && ( m_data[ i ].empty() ) )
  264. --i;
  265. return ( i < 0 ) ? FIRST_TYPE : FIRST_TYPE + i;
  266. }
  267. ///< Contained items by type
  268. ITEM_PTR_VECTOR m_data[TYPES_COUNT];
  269. };
  270. #endif /* MULTIVECTOR_H */