associative_ptr_container.hpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438
  1. //
  2. // Boost.Pointer Container
  3. //
  4. // Copyright Thorsten Ottosen 2003-2005. Use, modification and
  5. // distribution is subject to the Boost Software License, Version
  6. // 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  7. // http://www.boost.org/LICENSE_1_0.txt)
  8. //
  9. // For more information, see http://www.boost.org/libs/ptr_container/
  10. //
  11. #ifndef BOOST_PTR_CONTAINER_DETAIL_ASSOCIATIVE_PTR_CONTAINER_HPP
  12. #define BOOST_PTR_CONTAINER_DETAIL_ASSOCIATIVE_PTR_CONTAINER_HPP
  13. #if defined(_MSC_VER) && (_MSC_VER >= 1200)
  14. # pragma once
  15. #endif
  16. #include <boost/ptr_container/detail/reversible_ptr_container.hpp>
  17. #include <boost/ptr_container/detail/ptr_container_disable_deprecated.hpp>
  18. #if defined(BOOST_PTR_CONTAINER_DISABLE_DEPRECATED)
  19. #pragma GCC diagnostic push
  20. #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
  21. #endif
  22. namespace boost
  23. {
  24. namespace ptr_container_detail
  25. {
  26. template
  27. <
  28. class Config,
  29. class CloneAllocator
  30. >
  31. class associative_ptr_container :
  32. public reversible_ptr_container<Config,CloneAllocator>
  33. {
  34. typedef reversible_ptr_container<Config,CloneAllocator>
  35. base_type;
  36. typedef BOOST_DEDUCED_TYPENAME base_type::scoped_deleter
  37. scoped_deleter;
  38. typedef BOOST_DEDUCED_TYPENAME Config::container_type
  39. container_type;
  40. public: // typedefs
  41. typedef BOOST_DEDUCED_TYPENAME Config::key_type
  42. key_type;
  43. typedef BOOST_DEDUCED_TYPENAME Config::key_compare
  44. key_compare;
  45. typedef BOOST_DEDUCED_TYPENAME Config::value_compare
  46. value_compare;
  47. typedef BOOST_DEDUCED_TYPENAME Config::hasher
  48. hasher;
  49. typedef BOOST_DEDUCED_TYPENAME Config::key_equal
  50. key_equal;
  51. typedef BOOST_DEDUCED_TYPENAME Config::iterator
  52. iterator;
  53. typedef BOOST_DEDUCED_TYPENAME Config::const_iterator
  54. const_iterator;
  55. typedef BOOST_DEDUCED_TYPENAME Config::local_iterator
  56. local_iterator;
  57. typedef BOOST_DEDUCED_TYPENAME Config::const_local_iterator
  58. const_local_iterator;
  59. typedef BOOST_DEDUCED_TYPENAME base_type::size_type
  60. size_type;
  61. typedef BOOST_DEDUCED_TYPENAME base_type::reference
  62. reference;
  63. typedef BOOST_DEDUCED_TYPENAME base_type::const_reference
  64. const_reference;
  65. public: // foundation
  66. associative_ptr_container()
  67. { }
  68. template< class SizeType >
  69. associative_ptr_container( SizeType n, unordered_associative_container_tag tag )
  70. : base_type( n, tag )
  71. { }
  72. template< class Compare, class Allocator >
  73. associative_ptr_container( const Compare& comp,
  74. const Allocator& a )
  75. : base_type( comp, a, container_type() )
  76. { }
  77. template< class Hash, class Pred, class Allocator >
  78. associative_ptr_container( const Hash& hash,
  79. const Pred& pred,
  80. const Allocator& a )
  81. : base_type( hash, pred, a )
  82. { }
  83. template< class InputIterator, class Compare, class Allocator >
  84. associative_ptr_container( InputIterator first, InputIterator last,
  85. const Compare& comp,
  86. const Allocator& a )
  87. : base_type( first, last, comp, a, container_type() )
  88. { }
  89. template< class InputIterator, class Hash, class Pred, class Allocator >
  90. associative_ptr_container( InputIterator first, InputIterator last,
  91. const Hash& hash,
  92. const Pred& pred,
  93. const Allocator& a )
  94. : base_type( first, last, hash, pred, a )
  95. { }
  96. #ifndef BOOST_NO_AUTO_PTR
  97. template< class PtrContainer >
  98. explicit associative_ptr_container( std::auto_ptr<PtrContainer> r )
  99. : base_type( r )
  100. { }
  101. #endif
  102. #ifndef BOOST_NO_CXX11_SMART_PTR
  103. template< class PtrContainer >
  104. explicit associative_ptr_container( std::unique_ptr<PtrContainer> r )
  105. : base_type( std::move( r ) )
  106. { }
  107. #endif
  108. associative_ptr_container( const associative_ptr_container& r )
  109. : base_type( r.begin(), r.end(), container_type() )
  110. { }
  111. template< class C, class V >
  112. associative_ptr_container( const associative_ptr_container<C,V>& r )
  113. : base_type( r.begin(), r.end(), container_type() )
  114. { }
  115. #ifndef BOOST_NO_AUTO_PTR
  116. template< class PtrContainer >
  117. associative_ptr_container& operator=( std::auto_ptr<PtrContainer> r ) // nothrow
  118. {
  119. base_type::operator=( r );
  120. return *this;
  121. }
  122. #endif
  123. #ifndef BOOST_NO_CXX11_SMART_PTR
  124. template< class PtrContainer >
  125. associative_ptr_container& operator=( std::unique_ptr<PtrContainer> r ) // nothrow
  126. {
  127. base_type::operator=( std::move( r ) );
  128. return *this;
  129. }
  130. #endif
  131. associative_ptr_container& operator=( associative_ptr_container r ) // strong
  132. {
  133. this->swap( r );
  134. return *this;
  135. }
  136. public: // associative container interface
  137. key_compare key_comp() const
  138. {
  139. return this->base().key_comp();
  140. }
  141. value_compare value_comp() const
  142. {
  143. return this->base().value_comp();
  144. }
  145. iterator erase( iterator before ) // nothrow
  146. {
  147. BOOST_ASSERT( !this->empty() );
  148. BOOST_ASSERT( before != this->end() );
  149. this->remove( before ); // nothrow
  150. iterator res( before ); // nothrow
  151. ++res; // nothrow
  152. this->base().erase( before.base() ); // nothrow
  153. return res; // nothrow
  154. }
  155. size_type erase( const key_type& x ) // nothrow
  156. {
  157. iterator i( this->base().find( x ) );
  158. // nothrow
  159. if( i == this->end() ) // nothrow
  160. return 0u; // nothrow
  161. this->remove( i ); // nothrow
  162. return this->base().erase( x ); // nothrow
  163. }
  164. iterator erase( iterator first,
  165. iterator last ) // nothrow
  166. {
  167. iterator res( last ); // nothrow
  168. if( res != this->end() )
  169. ++res; // nothrow
  170. this->remove( first, last ); // nothrow
  171. this->base().erase( first.base(), last.base() ); // nothrow
  172. return res; // nothrow
  173. }
  174. #if defined(BOOST_NO_SFINAE) || defined(BOOST_NO_FUNCTION_TEMPLATE_ORDERING)
  175. #else
  176. template< class Range >
  177. BOOST_DEDUCED_TYPENAME boost::disable_if< boost::is_convertible<Range&,key_type&>,
  178. iterator >::type
  179. erase( const Range& r )
  180. {
  181. return erase( boost::begin(r), boost::end(r) );
  182. }
  183. #endif
  184. protected:
  185. template< class AssociatePtrCont >
  186. void multi_transfer( BOOST_DEDUCED_TYPENAME AssociatePtrCont::iterator object,
  187. AssociatePtrCont& from ) // strong
  188. {
  189. BOOST_ASSERT( (void*)&from != (void*)this );
  190. BOOST_ASSERT( !from.empty() && "Cannot transfer from empty container" );
  191. this->base().insert( *object.base() ); // strong
  192. from.base().erase( object.base() ); // nothrow
  193. }
  194. template< class AssociatePtrCont >
  195. size_type multi_transfer( BOOST_DEDUCED_TYPENAME AssociatePtrCont::iterator first,
  196. BOOST_DEDUCED_TYPENAME AssociatePtrCont::iterator last,
  197. AssociatePtrCont& from ) // basic
  198. {
  199. BOOST_ASSERT( (void*)&from != (void*)this );
  200. size_type res = 0;
  201. for( ; first != last; )
  202. {
  203. BOOST_ASSERT( first != from.end() );
  204. this->base().insert( *first.base() ); // strong
  205. BOOST_DEDUCED_TYPENAME AssociatePtrCont::iterator
  206. to_delete( first );
  207. ++first;
  208. from.base().erase( to_delete.base() ); // nothrow
  209. ++res;
  210. }
  211. return res;
  212. }
  213. template< class AssociatePtrCont >
  214. bool single_transfer( BOOST_DEDUCED_TYPENAME AssociatePtrCont::iterator object,
  215. AssociatePtrCont& from ) // strong
  216. {
  217. BOOST_ASSERT( (void*)&from != (void*)this );
  218. BOOST_ASSERT( !from.empty() && "Cannot transfer from empty container" );
  219. std::pair<BOOST_DEDUCED_TYPENAME base_type::ptr_iterator,bool> p =
  220. this->base().insert( *object.base() ); // strong
  221. if( p.second )
  222. from.base().erase( object.base() ); // nothrow
  223. return p.second;
  224. }
  225. template< class AssociatePtrCont >
  226. size_type single_transfer( BOOST_DEDUCED_TYPENAME AssociatePtrCont::iterator first,
  227. BOOST_DEDUCED_TYPENAME AssociatePtrCont::iterator last,
  228. AssociatePtrCont& from ) // basic
  229. {
  230. BOOST_ASSERT( (void*)&from != (void*)this );
  231. size_type res = 0;
  232. for( ; first != last; )
  233. {
  234. BOOST_ASSERT( first != from.end() );
  235. std::pair<BOOST_DEDUCED_TYPENAME base_type::ptr_iterator,bool> p =
  236. this->base().insert( *first.base() ); // strong
  237. BOOST_DEDUCED_TYPENAME AssociatePtrCont::iterator
  238. to_delete( first );
  239. ++first;
  240. if( p.second )
  241. {
  242. from.base().erase( to_delete.base() ); // nothrow
  243. ++res;
  244. }
  245. }
  246. return res;
  247. }
  248. reference front()
  249. {
  250. BOOST_ASSERT( !this->empty() );
  251. BOOST_ASSERT( *this->begin().base() != 0 );
  252. return *this->begin();
  253. }
  254. const_reference front() const
  255. {
  256. return const_cast<associative_ptr_container*>(this)->front();
  257. }
  258. reference back()
  259. {
  260. BOOST_ASSERT( !this->empty() );
  261. BOOST_ASSERT( *(--this->end()).base() != 0 );
  262. return *--this->end();
  263. }
  264. const_reference back() const
  265. {
  266. return const_cast<associative_ptr_container*>(this)->back();
  267. }
  268. protected: // unordered interface
  269. hasher hash_function() const
  270. {
  271. return this->base().hash_function();
  272. }
  273. key_equal key_eq() const
  274. {
  275. return this->base().key_eq();
  276. }
  277. size_type bucket_count() const
  278. {
  279. return this->base().bucket_count();
  280. }
  281. size_type max_bucket_count() const
  282. {
  283. return this->base().max_bucket_count();
  284. }
  285. size_type bucket_size( size_type n ) const
  286. {
  287. return this->base().bucket_size( n );
  288. }
  289. float load_factor() const
  290. {
  291. return this->base().load_factor();
  292. }
  293. float max_load_factor() const
  294. {
  295. return this->base().max_load_factor();
  296. }
  297. void max_load_factor( float factor )
  298. {
  299. return this->base().max_load_factor( factor );
  300. }
  301. void rehash( size_type n )
  302. {
  303. this->base().rehash( n );
  304. }
  305. public:
  306. #if BOOST_WORKAROUND(__DECCXX_VER, BOOST_TESTED_AT(70190006))
  307. iterator begin()
  308. {
  309. return base_type::begin();
  310. }
  311. const_iterator begin() const
  312. {
  313. return base_type::begin();
  314. }
  315. iterator end()
  316. {
  317. return base_type::end();
  318. }
  319. const_iterator end() const
  320. {
  321. return base_type::end();
  322. }
  323. const_iterator cbegin() const
  324. {
  325. return base_type::cbegin();
  326. }
  327. const_iterator cend() const
  328. {
  329. return base_type::cend();
  330. }
  331. #else
  332. using base_type::begin;
  333. using base_type::end;
  334. using base_type::cbegin;
  335. using base_type::cend;
  336. #endif
  337. protected:
  338. local_iterator begin( size_type n )
  339. {
  340. return local_iterator( this->base().begin( n ) );
  341. }
  342. const_local_iterator begin( size_type n ) const
  343. {
  344. return const_local_iterator( this->base().begin( n ) );
  345. }
  346. local_iterator end( size_type n )
  347. {
  348. return local_iterator( this->base().end( n ) );
  349. }
  350. const_local_iterator end( size_type n ) const
  351. {
  352. return const_local_iterator( this->base().end( n ) );
  353. }
  354. const_local_iterator cbegin( size_type n ) const
  355. {
  356. return const_local_iterator( this->base().cbegin( n ) );
  357. }
  358. const_local_iterator cend( size_type n )
  359. {
  360. return const_local_iterator( this->base().cend( n ) );
  361. }
  362. }; // class 'associative_ptr_container'
  363. } // namespace 'ptr_container_detail'
  364. } // namespace 'boost'
  365. #if defined(BOOST_PTR_CONTAINER_DISABLE_DEPRECATED)
  366. #pragma GCC diagnostic pop
  367. #endif
  368. #endif