ord_index_impl.hpp 46 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578
  1. /* Copyright 2003-2019 Joaquin M Lopez Munoz.
  2. * Distributed under the Boost Software License, Version 1.0.
  3. * (See accompanying file LICENSE_1_0.txt or copy at
  4. * http://www.boost.org/LICENSE_1_0.txt)
  5. *
  6. * See http://www.boost.org/libs/multi_index for library home page.
  7. *
  8. * The internal implementation of red-black trees is based on that of SGI STL
  9. * stl_tree.h file:
  10. *
  11. * Copyright (c) 1996,1997
  12. * Silicon Graphics Computer Systems, Inc.
  13. *
  14. * Permission to use, copy, modify, distribute and sell this software
  15. * and its documentation for any purpose is hereby granted without fee,
  16. * provided that the above copyright notice appear in all copies and
  17. * that both that copyright notice and this permission notice appear
  18. * in supporting documentation. Silicon Graphics makes no
  19. * representations about the suitability of this software for any
  20. * purpose. It is provided "as is" without express or implied warranty.
  21. *
  22. *
  23. * Copyright (c) 1994
  24. * Hewlett-Packard Company
  25. *
  26. * Permission to use, copy, modify, distribute and sell this software
  27. * and its documentation for any purpose is hereby granted without fee,
  28. * provided that the above copyright notice appear in all copies and
  29. * that both that copyright notice and this permission notice appear
  30. * in supporting documentation. Hewlett-Packard Company makes no
  31. * representations about the suitability of this software for any
  32. * purpose. It is provided "as is" without express or implied warranty.
  33. *
  34. */
  35. #ifndef BOOST_MULTI_INDEX_DETAIL_ORD_INDEX_IMPL_HPP
  36. #define BOOST_MULTI_INDEX_DETAIL_ORD_INDEX_IMPL_HPP
  37. #if defined(_MSC_VER)
  38. #pragma once
  39. #endif
  40. #include <boost/config.hpp> /* keep it first to prevent nasty warns in MSVC */
  41. #include <algorithm>
  42. #include <boost/call_traits.hpp>
  43. #include <boost/core/addressof.hpp>
  44. #include <boost/detail/no_exceptions_support.hpp>
  45. #include <boost/detail/workaround.hpp>
  46. #include <boost/foreach_fwd.hpp>
  47. #include <boost/iterator/reverse_iterator.hpp>
  48. #include <boost/move/core.hpp>
  49. #include <boost/mpl/bool.hpp>
  50. #include <boost/mpl/if.hpp>
  51. #include <boost/mpl/push_front.hpp>
  52. #include <boost/multi_index/detail/access_specifier.hpp>
  53. #include <boost/multi_index/detail/allocator_traits.hpp>
  54. #include <boost/multi_index/detail/bidir_node_iterator.hpp>
  55. #include <boost/multi_index/detail/do_not_copy_elements_tag.hpp>
  56. #include <boost/multi_index/detail/index_node_base.hpp>
  57. #include <boost/multi_index/detail/modify_key_adaptor.hpp>
  58. #include <boost/multi_index/detail/ord_index_node.hpp>
  59. #include <boost/multi_index/detail/ord_index_ops.hpp>
  60. #include <boost/multi_index/detail/safe_mode.hpp>
  61. #include <boost/multi_index/detail/scope_guard.hpp>
  62. #include <boost/multi_index/detail/unbounded.hpp>
  63. #include <boost/multi_index/detail/value_compare.hpp>
  64. #include <boost/multi_index/detail/vartempl_support.hpp>
  65. #include <boost/multi_index/detail/ord_index_impl_fwd.hpp>
  66. #include <boost/ref.hpp>
  67. #include <boost/tuple/tuple.hpp>
  68. #include <boost/type_traits/is_same.hpp>
  69. #include <utility>
  70. #if !defined(BOOST_NO_CXX11_HDR_INITIALIZER_LIST)
  71. #include <initializer_list>
  72. #endif
  73. #if !defined(BOOST_MULTI_INDEX_DISABLE_SERIALIZATION)
  74. #include <boost/archive/archive_exception.hpp>
  75. #include <boost/bind.hpp>
  76. #include <boost/multi_index/detail/duplicates_iterator.hpp>
  77. #include <boost/throw_exception.hpp>
  78. #endif
  79. #if defined(BOOST_MULTI_INDEX_ENABLE_INVARIANT_CHECKING)
  80. #define BOOST_MULTI_INDEX_ORD_INDEX_CHECK_INVARIANT_OF(x) \
  81. detail::scope_guard BOOST_JOIN(check_invariant_,__LINE__)= \
  82. detail::make_obj_guard(x,&ordered_index_impl::check_invariant_); \
  83. BOOST_JOIN(check_invariant_,__LINE__).touch();
  84. #define BOOST_MULTI_INDEX_ORD_INDEX_CHECK_INVARIANT \
  85. BOOST_MULTI_INDEX_ORD_INDEX_CHECK_INVARIANT_OF(*this)
  86. #else
  87. #define BOOST_MULTI_INDEX_ORD_INDEX_CHECK_INVARIANT_OF(x)
  88. #define BOOST_MULTI_INDEX_ORD_INDEX_CHECK_INVARIANT
  89. #endif
  90. namespace boost{
  91. namespace multi_index{
  92. namespace detail{
  93. /* ordered_index adds a layer of ordered indexing to a given Super and accepts
  94. * an augmenting policy for optional addition of order statistics.
  95. */
  96. /* Most of the implementation of unique and non-unique indices is
  97. * shared. We tell from one another on instantiation time by using
  98. * these tags.
  99. */
  100. struct ordered_unique_tag{};
  101. struct ordered_non_unique_tag{};
  102. template<
  103. typename KeyFromValue,typename Compare,
  104. typename SuperMeta,typename TagList,typename Category,typename AugmentPolicy
  105. >
  106. class ordered_index;
  107. template<
  108. typename KeyFromValue,typename Compare,
  109. typename SuperMeta,typename TagList,typename Category,typename AugmentPolicy
  110. >
  111. class ordered_index_impl:
  112. BOOST_MULTI_INDEX_PROTECTED_IF_MEMBER_TEMPLATE_FRIENDS SuperMeta::type
  113. #if defined(BOOST_MULTI_INDEX_ENABLE_SAFE_MODE)
  114. ,public safe_mode::safe_container<
  115. ordered_index_impl<
  116. KeyFromValue,Compare,SuperMeta,TagList,Category,AugmentPolicy> >
  117. #endif
  118. {
  119. #if defined(BOOST_MULTI_INDEX_ENABLE_INVARIANT_CHECKING)&&\
  120. BOOST_WORKAROUND(__MWERKS__,<=0x3003)
  121. /* The "ISO C++ Template Parser" option in CW8.3 has a problem with the
  122. * lifetime of const references bound to temporaries --precisely what
  123. * scopeguards are.
  124. */
  125. #pragma parse_mfunc_templ off
  126. #endif
  127. typedef typename SuperMeta::type super;
  128. protected:
  129. typedef ordered_index_node<
  130. AugmentPolicy,typename super::node_type> node_type;
  131. protected: /* for the benefit of AugmentPolicy::augmented_interface */
  132. typedef typename node_type::impl_type node_impl_type;
  133. typedef typename node_impl_type::pointer node_impl_pointer;
  134. public:
  135. /* types */
  136. typedef typename KeyFromValue::result_type key_type;
  137. typedef typename node_type::value_type value_type;
  138. typedef KeyFromValue key_from_value;
  139. typedef Compare key_compare;
  140. typedef value_comparison<
  141. value_type,KeyFromValue,Compare> value_compare;
  142. typedef tuple<key_from_value,key_compare> ctor_args;
  143. typedef typename super::final_allocator_type allocator_type;
  144. typedef value_type& reference;
  145. typedef const value_type& const_reference;
  146. #if defined(BOOST_MULTI_INDEX_ENABLE_SAFE_MODE)
  147. typedef safe_mode::safe_iterator<
  148. bidir_node_iterator<node_type>,
  149. ordered_index_impl> iterator;
  150. #else
  151. typedef bidir_node_iterator<node_type> iterator;
  152. #endif
  153. typedef iterator const_iterator;
  154. private:
  155. typedef allocator_traits<allocator_type> alloc_traits;
  156. public:
  157. typedef typename alloc_traits::size_type size_type;
  158. typedef typename alloc_traits::difference_type difference_type;
  159. typedef typename alloc_traits::pointer pointer;
  160. typedef typename alloc_traits::const_pointer const_pointer;
  161. typedef typename
  162. boost::reverse_iterator<iterator> reverse_iterator;
  163. typedef typename
  164. boost::reverse_iterator<const_iterator> const_reverse_iterator;
  165. typedef TagList tag_list;
  166. protected:
  167. typedef typename super::final_node_type final_node_type;
  168. typedef tuples::cons<
  169. ctor_args,
  170. typename super::ctor_args_list> ctor_args_list;
  171. typedef typename mpl::push_front<
  172. typename super::index_type_list,
  173. ordered_index<
  174. KeyFromValue,Compare,
  175. SuperMeta,TagList,Category,AugmentPolicy
  176. > >::type index_type_list;
  177. typedef typename mpl::push_front<
  178. typename super::iterator_type_list,
  179. iterator>::type iterator_type_list;
  180. typedef typename mpl::push_front<
  181. typename super::const_iterator_type_list,
  182. const_iterator>::type const_iterator_type_list;
  183. typedef typename super::copy_map_type copy_map_type;
  184. #if !defined(BOOST_MULTI_INDEX_DISABLE_SERIALIZATION)
  185. typedef typename super::index_saver_type index_saver_type;
  186. typedef typename super::index_loader_type index_loader_type;
  187. #endif
  188. protected:
  189. #if defined(BOOST_MULTI_INDEX_ENABLE_SAFE_MODE)
  190. typedef safe_mode::safe_container<
  191. ordered_index_impl> safe_super;
  192. #endif
  193. typedef typename call_traits<
  194. value_type>::param_type value_param_type;
  195. typedef typename call_traits<
  196. key_type>::param_type key_param_type;
  197. /* Needed to avoid commas in BOOST_MULTI_INDEX_OVERLOADS_TO_VARTEMPL
  198. * expansion.
  199. */
  200. typedef std::pair<iterator,bool> emplace_return_type;
  201. public:
  202. /* construct/copy/destroy
  203. * Default and copy ctors are in the protected section as indices are
  204. * not supposed to be created on their own. No range ctor either.
  205. * Assignment operators defined at ordered_index rather than here.
  206. */
  207. allocator_type get_allocator()const BOOST_NOEXCEPT
  208. {
  209. return this->final().get_allocator();
  210. }
  211. /* iterators */
  212. iterator
  213. begin()BOOST_NOEXCEPT{return make_iterator(leftmost());}
  214. const_iterator
  215. begin()const BOOST_NOEXCEPT{return make_iterator(leftmost());}
  216. iterator
  217. end()BOOST_NOEXCEPT{return make_iterator(header());}
  218. const_iterator
  219. end()const BOOST_NOEXCEPT{return make_iterator(header());}
  220. reverse_iterator
  221. rbegin()BOOST_NOEXCEPT{return boost::make_reverse_iterator(end());}
  222. const_reverse_iterator
  223. rbegin()const BOOST_NOEXCEPT{return boost::make_reverse_iterator(end());}
  224. reverse_iterator
  225. rend()BOOST_NOEXCEPT{return boost::make_reverse_iterator(begin());}
  226. const_reverse_iterator
  227. rend()const BOOST_NOEXCEPT{return boost::make_reverse_iterator(begin());}
  228. const_iterator
  229. cbegin()const BOOST_NOEXCEPT{return begin();}
  230. const_iterator
  231. cend()const BOOST_NOEXCEPT{return end();}
  232. const_reverse_iterator
  233. crbegin()const BOOST_NOEXCEPT{return rbegin();}
  234. const_reverse_iterator
  235. crend()const BOOST_NOEXCEPT{return rend();}
  236. iterator iterator_to(const value_type& x)
  237. {
  238. return make_iterator(node_from_value<node_type>(boost::addressof(x)));
  239. }
  240. const_iterator iterator_to(const value_type& x)const
  241. {
  242. return make_iterator(node_from_value<node_type>(boost::addressof(x)));
  243. }
  244. /* capacity */
  245. bool empty()const BOOST_NOEXCEPT{return this->final_empty_();}
  246. size_type size()const BOOST_NOEXCEPT{return this->final_size_();}
  247. size_type max_size()const BOOST_NOEXCEPT{return this->final_max_size_();}
  248. /* modifiers */
  249. BOOST_MULTI_INDEX_OVERLOADS_TO_VARTEMPL(
  250. emplace_return_type,emplace,emplace_impl)
  251. BOOST_MULTI_INDEX_OVERLOADS_TO_VARTEMPL_EXTRA_ARG(
  252. iterator,emplace_hint,emplace_hint_impl,iterator,position)
  253. std::pair<iterator,bool> insert(const value_type& x)
  254. {
  255. BOOST_MULTI_INDEX_ORD_INDEX_CHECK_INVARIANT;
  256. std::pair<final_node_type*,bool> p=this->final_insert_(x);
  257. return std::pair<iterator,bool>(make_iterator(p.first),p.second);
  258. }
  259. std::pair<iterator,bool> insert(BOOST_RV_REF(value_type) x)
  260. {
  261. BOOST_MULTI_INDEX_ORD_INDEX_CHECK_INVARIANT;
  262. std::pair<final_node_type*,bool> p=this->final_insert_rv_(x);
  263. return std::pair<iterator,bool>(make_iterator(p.first),p.second);
  264. }
  265. iterator insert(iterator position,const value_type& x)
  266. {
  267. BOOST_MULTI_INDEX_CHECK_VALID_ITERATOR(position);
  268. BOOST_MULTI_INDEX_CHECK_IS_OWNER(position,*this);
  269. BOOST_MULTI_INDEX_ORD_INDEX_CHECK_INVARIANT;
  270. std::pair<final_node_type*,bool> p=this->final_insert_(
  271. x,static_cast<final_node_type*>(position.get_node()));
  272. return make_iterator(p.first);
  273. }
  274. iterator insert(iterator position,BOOST_RV_REF(value_type) x)
  275. {
  276. BOOST_MULTI_INDEX_CHECK_VALID_ITERATOR(position);
  277. BOOST_MULTI_INDEX_CHECK_IS_OWNER(position,*this);
  278. BOOST_MULTI_INDEX_ORD_INDEX_CHECK_INVARIANT;
  279. std::pair<final_node_type*,bool> p=this->final_insert_rv_(
  280. x,static_cast<final_node_type*>(position.get_node()));
  281. return make_iterator(p.first);
  282. }
  283. template<typename InputIterator>
  284. void insert(InputIterator first,InputIterator last)
  285. {
  286. BOOST_MULTI_INDEX_ORD_INDEX_CHECK_INVARIANT;
  287. node_type* hint=header(); /* end() */
  288. for(;first!=last;++first){
  289. hint=this->final_insert_ref_(
  290. *first,static_cast<final_node_type*>(hint)).first;
  291. node_type::increment(hint);
  292. }
  293. }
  294. #if !defined(BOOST_NO_CXX11_HDR_INITIALIZER_LIST)
  295. void insert(std::initializer_list<value_type> list)
  296. {
  297. insert(list.begin(),list.end());
  298. }
  299. #endif
  300. iterator erase(iterator position)
  301. {
  302. BOOST_MULTI_INDEX_CHECK_VALID_ITERATOR(position);
  303. BOOST_MULTI_INDEX_CHECK_DEREFERENCEABLE_ITERATOR(position);
  304. BOOST_MULTI_INDEX_CHECK_IS_OWNER(position,*this);
  305. BOOST_MULTI_INDEX_ORD_INDEX_CHECK_INVARIANT;
  306. this->final_erase_(static_cast<final_node_type*>(position++.get_node()));
  307. return position;
  308. }
  309. size_type erase(key_param_type x)
  310. {
  311. BOOST_MULTI_INDEX_ORD_INDEX_CHECK_INVARIANT;
  312. std::pair<iterator,iterator> p=equal_range(x);
  313. size_type s=0;
  314. while(p.first!=p.second){
  315. p.first=erase(p.first);
  316. ++s;
  317. }
  318. return s;
  319. }
  320. iterator erase(iterator first,iterator last)
  321. {
  322. BOOST_MULTI_INDEX_CHECK_VALID_ITERATOR(first);
  323. BOOST_MULTI_INDEX_CHECK_VALID_ITERATOR(last);
  324. BOOST_MULTI_INDEX_CHECK_IS_OWNER(first,*this);
  325. BOOST_MULTI_INDEX_CHECK_IS_OWNER(last,*this);
  326. BOOST_MULTI_INDEX_CHECK_VALID_RANGE(first,last);
  327. BOOST_MULTI_INDEX_ORD_INDEX_CHECK_INVARIANT;
  328. while(first!=last){
  329. first=erase(first);
  330. }
  331. return first;
  332. }
  333. bool replace(iterator position,const value_type& x)
  334. {
  335. BOOST_MULTI_INDEX_CHECK_VALID_ITERATOR(position);
  336. BOOST_MULTI_INDEX_CHECK_DEREFERENCEABLE_ITERATOR(position);
  337. BOOST_MULTI_INDEX_CHECK_IS_OWNER(position,*this);
  338. BOOST_MULTI_INDEX_ORD_INDEX_CHECK_INVARIANT;
  339. return this->final_replace_(
  340. x,static_cast<final_node_type*>(position.get_node()));
  341. }
  342. bool replace(iterator position,BOOST_RV_REF(value_type) x)
  343. {
  344. BOOST_MULTI_INDEX_CHECK_VALID_ITERATOR(position);
  345. BOOST_MULTI_INDEX_CHECK_DEREFERENCEABLE_ITERATOR(position);
  346. BOOST_MULTI_INDEX_CHECK_IS_OWNER(position,*this);
  347. BOOST_MULTI_INDEX_ORD_INDEX_CHECK_INVARIANT;
  348. return this->final_replace_rv_(
  349. x,static_cast<final_node_type*>(position.get_node()));
  350. }
  351. template<typename Modifier>
  352. bool modify(iterator position,Modifier mod)
  353. {
  354. BOOST_MULTI_INDEX_CHECK_VALID_ITERATOR(position);
  355. BOOST_MULTI_INDEX_CHECK_DEREFERENCEABLE_ITERATOR(position);
  356. BOOST_MULTI_INDEX_CHECK_IS_OWNER(position,*this);
  357. BOOST_MULTI_INDEX_ORD_INDEX_CHECK_INVARIANT;
  358. #if defined(BOOST_MULTI_INDEX_ENABLE_SAFE_MODE)
  359. /* MSVC++ 6.0 optimizer on safe mode code chokes if this
  360. * this is not added. Left it for all compilers as it does no
  361. * harm.
  362. */
  363. position.detach();
  364. #endif
  365. return this->final_modify_(
  366. mod,static_cast<final_node_type*>(position.get_node()));
  367. }
  368. template<typename Modifier,typename Rollback>
  369. bool modify(iterator position,Modifier mod,Rollback back_)
  370. {
  371. BOOST_MULTI_INDEX_CHECK_VALID_ITERATOR(position);
  372. BOOST_MULTI_INDEX_CHECK_DEREFERENCEABLE_ITERATOR(position);
  373. BOOST_MULTI_INDEX_CHECK_IS_OWNER(position,*this);
  374. BOOST_MULTI_INDEX_ORD_INDEX_CHECK_INVARIANT;
  375. #if defined(BOOST_MULTI_INDEX_ENABLE_SAFE_MODE)
  376. /* MSVC++ 6.0 optimizer on safe mode code chokes if this
  377. * this is not added. Left it for all compilers as it does no
  378. * harm.
  379. */
  380. position.detach();
  381. #endif
  382. return this->final_modify_(
  383. mod,back_,static_cast<final_node_type*>(position.get_node()));
  384. }
  385. template<typename Modifier>
  386. bool modify_key(iterator position,Modifier mod)
  387. {
  388. BOOST_MULTI_INDEX_CHECK_VALID_ITERATOR(position);
  389. BOOST_MULTI_INDEX_CHECK_DEREFERENCEABLE_ITERATOR(position);
  390. BOOST_MULTI_INDEX_CHECK_IS_OWNER(position,*this);
  391. BOOST_MULTI_INDEX_ORD_INDEX_CHECK_INVARIANT;
  392. return modify(
  393. position,modify_key_adaptor<Modifier,value_type,KeyFromValue>(mod,key));
  394. }
  395. template<typename Modifier,typename Rollback>
  396. bool modify_key(iterator position,Modifier mod,Rollback back_)
  397. {
  398. BOOST_MULTI_INDEX_CHECK_VALID_ITERATOR(position);
  399. BOOST_MULTI_INDEX_CHECK_DEREFERENCEABLE_ITERATOR(position);
  400. BOOST_MULTI_INDEX_CHECK_IS_OWNER(position,*this);
  401. BOOST_MULTI_INDEX_ORD_INDEX_CHECK_INVARIANT;
  402. return modify(
  403. position,
  404. modify_key_adaptor<Modifier,value_type,KeyFromValue>(mod,key),
  405. modify_key_adaptor<Rollback,value_type,KeyFromValue>(back_,key));
  406. }
  407. void swap(
  408. ordered_index<
  409. KeyFromValue,Compare,SuperMeta,TagList,Category,AugmentPolicy>& x)
  410. {
  411. BOOST_MULTI_INDEX_ORD_INDEX_CHECK_INVARIANT;
  412. BOOST_MULTI_INDEX_ORD_INDEX_CHECK_INVARIANT_OF(x);
  413. this->final_swap_(x.final());
  414. }
  415. void clear()BOOST_NOEXCEPT
  416. {
  417. BOOST_MULTI_INDEX_ORD_INDEX_CHECK_INVARIANT;
  418. this->final_clear_();
  419. }
  420. /* observers */
  421. key_from_value key_extractor()const{return key;}
  422. key_compare key_comp()const{return comp_;}
  423. value_compare value_comp()const{return value_compare(key,comp_);}
  424. /* set operations */
  425. /* Internally, these ops rely on const_iterator being the same
  426. * type as iterator.
  427. */
  428. template<typename CompatibleKey>
  429. iterator find(const CompatibleKey& x)const
  430. {
  431. return make_iterator(ordered_index_find(root(),header(),key,x,comp_));
  432. }
  433. template<typename CompatibleKey,typename CompatibleCompare>
  434. iterator find(
  435. const CompatibleKey& x,const CompatibleCompare& comp)const
  436. {
  437. return make_iterator(ordered_index_find(root(),header(),key,x,comp));
  438. }
  439. template<typename CompatibleKey>
  440. size_type count(const CompatibleKey& x)const
  441. {
  442. return count(x,comp_);
  443. }
  444. template<typename CompatibleKey,typename CompatibleCompare>
  445. size_type count(const CompatibleKey& x,const CompatibleCompare& comp)const
  446. {
  447. std::pair<iterator,iterator> p=equal_range(x,comp);
  448. size_type n=static_cast<size_type>(std::distance(p.first,p.second));
  449. return n;
  450. }
  451. template<typename CompatibleKey>
  452. iterator lower_bound(const CompatibleKey& x)const
  453. {
  454. return make_iterator(
  455. ordered_index_lower_bound(root(),header(),key,x,comp_));
  456. }
  457. template<typename CompatibleKey,typename CompatibleCompare>
  458. iterator lower_bound(
  459. const CompatibleKey& x,const CompatibleCompare& comp)const
  460. {
  461. return make_iterator(
  462. ordered_index_lower_bound(root(),header(),key,x,comp));
  463. }
  464. template<typename CompatibleKey>
  465. iterator upper_bound(const CompatibleKey& x)const
  466. {
  467. return make_iterator(
  468. ordered_index_upper_bound(root(),header(),key,x,comp_));
  469. }
  470. template<typename CompatibleKey,typename CompatibleCompare>
  471. iterator upper_bound(
  472. const CompatibleKey& x,const CompatibleCompare& comp)const
  473. {
  474. return make_iterator(
  475. ordered_index_upper_bound(root(),header(),key,x,comp));
  476. }
  477. template<typename CompatibleKey>
  478. std::pair<iterator,iterator> equal_range(
  479. const CompatibleKey& x)const
  480. {
  481. std::pair<node_type*,node_type*> p=
  482. ordered_index_equal_range(root(),header(),key,x,comp_);
  483. return std::pair<iterator,iterator>(
  484. make_iterator(p.first),make_iterator(p.second));
  485. }
  486. template<typename CompatibleKey,typename CompatibleCompare>
  487. std::pair<iterator,iterator> equal_range(
  488. const CompatibleKey& x,const CompatibleCompare& comp)const
  489. {
  490. std::pair<node_type*,node_type*> p=
  491. ordered_index_equal_range(root(),header(),key,x,comp);
  492. return std::pair<iterator,iterator>(
  493. make_iterator(p.first),make_iterator(p.second));
  494. }
  495. /* range */
  496. template<typename LowerBounder,typename UpperBounder>
  497. std::pair<iterator,iterator>
  498. range(LowerBounder lower,UpperBounder upper)const
  499. {
  500. typedef typename mpl::if_<
  501. is_same<LowerBounder,unbounded_type>,
  502. BOOST_DEDUCED_TYPENAME mpl::if_<
  503. is_same<UpperBounder,unbounded_type>,
  504. both_unbounded_tag,
  505. lower_unbounded_tag
  506. >::type,
  507. BOOST_DEDUCED_TYPENAME mpl::if_<
  508. is_same<UpperBounder,unbounded_type>,
  509. upper_unbounded_tag,
  510. none_unbounded_tag
  511. >::type
  512. >::type dispatch;
  513. return range(lower,upper,dispatch());
  514. }
  515. BOOST_MULTI_INDEX_PROTECTED_IF_MEMBER_TEMPLATE_FRIENDS:
  516. ordered_index_impl(const ctor_args_list& args_list,const allocator_type& al):
  517. super(args_list.get_tail(),al),
  518. key(tuples::get<0>(args_list.get_head())),
  519. comp_(tuples::get<1>(args_list.get_head()))
  520. {
  521. empty_initialize();
  522. }
  523. ordered_index_impl(
  524. const ordered_index_impl<
  525. KeyFromValue,Compare,SuperMeta,TagList,Category,AugmentPolicy>& x):
  526. super(x),
  527. #if defined(BOOST_MULTI_INDEX_ENABLE_SAFE_MODE)
  528. safe_super(),
  529. #endif
  530. key(x.key),
  531. comp_(x.comp_)
  532. {
  533. /* Copy ctor just takes the key and compare objects from x. The rest is
  534. * done in a subsequent call to copy_().
  535. */
  536. }
  537. ordered_index_impl(
  538. const ordered_index_impl<
  539. KeyFromValue,Compare,SuperMeta,TagList,Category,AugmentPolicy>& x,
  540. do_not_copy_elements_tag):
  541. super(x,do_not_copy_elements_tag()),
  542. #if defined(BOOST_MULTI_INDEX_ENABLE_SAFE_MODE)
  543. safe_super(),
  544. #endif
  545. key(x.key),
  546. comp_(x.comp_)
  547. {
  548. empty_initialize();
  549. }
  550. ~ordered_index_impl()
  551. {
  552. /* the container is guaranteed to be empty by now */
  553. }
  554. #if defined(BOOST_MULTI_INDEX_ENABLE_SAFE_MODE)
  555. iterator make_iterator(node_type* node){return iterator(node,this);}
  556. const_iterator make_iterator(node_type* node)const
  557. {return const_iterator(node,const_cast<ordered_index_impl*>(this));}
  558. #else
  559. iterator make_iterator(node_type* node){return iterator(node);}
  560. const_iterator make_iterator(node_type* node)const
  561. {return const_iterator(node);}
  562. #endif
  563. void copy_(
  564. const ordered_index_impl<
  565. KeyFromValue,Compare,SuperMeta,TagList,Category,AugmentPolicy>& x,
  566. const copy_map_type& map)
  567. {
  568. if(!x.root()){
  569. empty_initialize();
  570. }
  571. else{
  572. header()->color()=x.header()->color();
  573. AugmentPolicy::copy(x.header()->impl(),header()->impl());
  574. node_type* root_cpy=map.find(static_cast<final_node_type*>(x.root()));
  575. header()->parent()=root_cpy->impl();
  576. node_type* leftmost_cpy=map.find(
  577. static_cast<final_node_type*>(x.leftmost()));
  578. header()->left()=leftmost_cpy->impl();
  579. node_type* rightmost_cpy=map.find(
  580. static_cast<final_node_type*>(x.rightmost()));
  581. header()->right()=rightmost_cpy->impl();
  582. typedef typename copy_map_type::const_iterator copy_map_iterator;
  583. for(copy_map_iterator it=map.begin(),it_end=map.end();it!=it_end;++it){
  584. node_type* org=it->first;
  585. node_type* cpy=it->second;
  586. cpy->color()=org->color();
  587. AugmentPolicy::copy(org->impl(),cpy->impl());
  588. node_impl_pointer parent_org=org->parent();
  589. if(parent_org==node_impl_pointer(0))cpy->parent()=node_impl_pointer(0);
  590. else{
  591. node_type* parent_cpy=map.find(
  592. static_cast<final_node_type*>(node_type::from_impl(parent_org)));
  593. cpy->parent()=parent_cpy->impl();
  594. if(parent_org->left()==org->impl()){
  595. parent_cpy->left()=cpy->impl();
  596. }
  597. else if(parent_org->right()==org->impl()){
  598. /* header() does not satisfy this nor the previous check */
  599. parent_cpy->right()=cpy->impl();
  600. }
  601. }
  602. if(org->left()==node_impl_pointer(0))
  603. cpy->left()=node_impl_pointer(0);
  604. if(org->right()==node_impl_pointer(0))
  605. cpy->right()=node_impl_pointer(0);
  606. }
  607. }
  608. super::copy_(x,map);
  609. }
  610. template<typename Variant>
  611. final_node_type* insert_(
  612. value_param_type v,final_node_type*& x,Variant variant)
  613. {
  614. link_info inf;
  615. if(!link_point(key(v),inf,Category())){
  616. return static_cast<final_node_type*>(node_type::from_impl(inf.pos));
  617. }
  618. final_node_type* res=super::insert_(v,x,variant);
  619. if(res==x){
  620. node_impl_type::link(
  621. static_cast<node_type*>(x)->impl(),inf.side,inf.pos,header()->impl());
  622. }
  623. return res;
  624. }
  625. template<typename Variant>
  626. final_node_type* insert_(
  627. value_param_type v,node_type* position,final_node_type*& x,Variant variant)
  628. {
  629. link_info inf;
  630. if(!hinted_link_point(key(v),position,inf,Category())){
  631. return static_cast<final_node_type*>(node_type::from_impl(inf.pos));
  632. }
  633. final_node_type* res=super::insert_(v,position,x,variant);
  634. if(res==x){
  635. node_impl_type::link(
  636. static_cast<node_type*>(x)->impl(),inf.side,inf.pos,header()->impl());
  637. }
  638. return res;
  639. }
  640. void erase_(node_type* x)
  641. {
  642. node_impl_type::rebalance_for_erase(
  643. x->impl(),header()->parent(),header()->left(),header()->right());
  644. super::erase_(x);
  645. #if defined(BOOST_MULTI_INDEX_ENABLE_SAFE_MODE)
  646. detach_iterators(x);
  647. #endif
  648. }
  649. void delete_all_nodes_()
  650. {
  651. delete_all_nodes(root());
  652. }
  653. void clear_()
  654. {
  655. super::clear_();
  656. empty_initialize();
  657. #if defined(BOOST_MULTI_INDEX_ENABLE_SAFE_MODE)
  658. safe_super::detach_dereferenceable_iterators();
  659. #endif
  660. }
  661. void swap_(
  662. ordered_index_impl<
  663. KeyFromValue,Compare,SuperMeta,TagList,Category,AugmentPolicy>& x)
  664. {
  665. std::swap(key,x.key);
  666. std::swap(comp_,x.comp_);
  667. #if defined(BOOST_MULTI_INDEX_ENABLE_SAFE_MODE)
  668. safe_super::swap(x);
  669. #endif
  670. super::swap_(x);
  671. }
  672. void swap_elements_(
  673. ordered_index_impl<
  674. KeyFromValue,Compare,SuperMeta,TagList,Category,AugmentPolicy>& x)
  675. {
  676. #if defined(BOOST_MULTI_INDEX_ENABLE_SAFE_MODE)
  677. safe_super::swap(x);
  678. #endif
  679. super::swap_elements_(x);
  680. }
  681. template<typename Variant>
  682. bool replace_(value_param_type v,node_type* x,Variant variant)
  683. {
  684. if(in_place(v,x,Category())){
  685. return super::replace_(v,x,variant);
  686. }
  687. node_type* next=x;
  688. node_type::increment(next);
  689. node_impl_type::rebalance_for_erase(
  690. x->impl(),header()->parent(),header()->left(),header()->right());
  691. BOOST_TRY{
  692. link_info inf;
  693. if(link_point(key(v),inf,Category())&&super::replace_(v,x,variant)){
  694. node_impl_type::link(x->impl(),inf.side,inf.pos,header()->impl());
  695. return true;
  696. }
  697. node_impl_type::restore(x->impl(),next->impl(),header()->impl());
  698. return false;
  699. }
  700. BOOST_CATCH(...){
  701. node_impl_type::restore(x->impl(),next->impl(),header()->impl());
  702. BOOST_RETHROW;
  703. }
  704. BOOST_CATCH_END
  705. }
  706. bool modify_(node_type* x)
  707. {
  708. bool b;
  709. BOOST_TRY{
  710. b=in_place(x->value(),x,Category());
  711. }
  712. BOOST_CATCH(...){
  713. erase_(x);
  714. BOOST_RETHROW;
  715. }
  716. BOOST_CATCH_END
  717. if(!b){
  718. node_impl_type::rebalance_for_erase(
  719. x->impl(),header()->parent(),header()->left(),header()->right());
  720. BOOST_TRY{
  721. link_info inf;
  722. if(!link_point(key(x->value()),inf,Category())){
  723. super::erase_(x);
  724. #if defined(BOOST_MULTI_INDEX_ENABLE_SAFE_MODE)
  725. detach_iterators(x);
  726. #endif
  727. return false;
  728. }
  729. node_impl_type::link(x->impl(),inf.side,inf.pos,header()->impl());
  730. }
  731. BOOST_CATCH(...){
  732. super::erase_(x);
  733. #if defined(BOOST_MULTI_INDEX_ENABLE_SAFE_MODE)
  734. detach_iterators(x);
  735. #endif
  736. BOOST_RETHROW;
  737. }
  738. BOOST_CATCH_END
  739. }
  740. BOOST_TRY{
  741. if(!super::modify_(x)){
  742. node_impl_type::rebalance_for_erase(
  743. x->impl(),header()->parent(),header()->left(),header()->right());
  744. #if defined(BOOST_MULTI_INDEX_ENABLE_SAFE_MODE)
  745. detach_iterators(x);
  746. #endif
  747. return false;
  748. }
  749. else return true;
  750. }
  751. BOOST_CATCH(...){
  752. node_impl_type::rebalance_for_erase(
  753. x->impl(),header()->parent(),header()->left(),header()->right());
  754. #if defined(BOOST_MULTI_INDEX_ENABLE_SAFE_MODE)
  755. detach_iterators(x);
  756. #endif
  757. BOOST_RETHROW;
  758. }
  759. BOOST_CATCH_END
  760. }
  761. bool modify_rollback_(node_type* x)
  762. {
  763. if(in_place(x->value(),x,Category())){
  764. return super::modify_rollback_(x);
  765. }
  766. node_type* next=x;
  767. node_type::increment(next);
  768. node_impl_type::rebalance_for_erase(
  769. x->impl(),header()->parent(),header()->left(),header()->right());
  770. BOOST_TRY{
  771. link_info inf;
  772. if(link_point(key(x->value()),inf,Category())&&
  773. super::modify_rollback_(x)){
  774. node_impl_type::link(x->impl(),inf.side,inf.pos,header()->impl());
  775. return true;
  776. }
  777. node_impl_type::restore(x->impl(),next->impl(),header()->impl());
  778. return false;
  779. }
  780. BOOST_CATCH(...){
  781. node_impl_type::restore(x->impl(),next->impl(),header()->impl());
  782. BOOST_RETHROW;
  783. }
  784. BOOST_CATCH_END
  785. }
  786. bool check_rollback_(node_type* x)const
  787. {
  788. return in_place(x->value(),x,Category())&&super::check_rollback_(x);
  789. }
  790. #if !defined(BOOST_MULTI_INDEX_DISABLE_SERIALIZATION)
  791. /* serialization */
  792. template<typename Archive>
  793. void save_(
  794. Archive& ar,const unsigned int version,const index_saver_type& sm)const
  795. {
  796. save_(ar,version,sm,Category());
  797. }
  798. template<typename Archive>
  799. void load_(Archive& ar,const unsigned int version,const index_loader_type& lm)
  800. {
  801. load_(ar,version,lm,Category());
  802. }
  803. #endif
  804. #if defined(BOOST_MULTI_INDEX_ENABLE_INVARIANT_CHECKING)
  805. /* invariant stuff */
  806. bool invariant_()const
  807. {
  808. if(size()==0||begin()==end()){
  809. if(size()!=0||begin()!=end()||
  810. header()->left()!=header()->impl()||
  811. header()->right()!=header()->impl())return false;
  812. }
  813. else{
  814. if((size_type)std::distance(begin(),end())!=size())return false;
  815. std::size_t len=node_impl_type::black_count(
  816. leftmost()->impl(),root()->impl());
  817. for(const_iterator it=begin(),it_end=end();it!=it_end;++it){
  818. node_type* x=it.get_node();
  819. node_type* left_x=node_type::from_impl(x->left());
  820. node_type* right_x=node_type::from_impl(x->right());
  821. if(x->color()==red){
  822. if((left_x&&left_x->color()==red)||
  823. (right_x&&right_x->color()==red))return false;
  824. }
  825. if(left_x&&comp_(key(x->value()),key(left_x->value())))return false;
  826. if(right_x&&comp_(key(right_x->value()),key(x->value())))return false;
  827. if(!left_x&&!right_x&&
  828. node_impl_type::black_count(x->impl(),root()->impl())!=len)
  829. return false;
  830. if(!AugmentPolicy::invariant(x->impl()))return false;
  831. }
  832. if(leftmost()->impl()!=node_impl_type::minimum(root()->impl()))
  833. return false;
  834. if(rightmost()->impl()!=node_impl_type::maximum(root()->impl()))
  835. return false;
  836. }
  837. return super::invariant_();
  838. }
  839. /* This forwarding function eases things for the boost::mem_fn construct
  840. * in BOOST_MULTI_INDEX_ORD_INDEX_CHECK_INVARIANT. Actually,
  841. * final_check_invariant is already an inherited member function of
  842. * ordered_index_impl.
  843. */
  844. void check_invariant_()const{this->final_check_invariant_();}
  845. #endif
  846. protected: /* for the benefit of AugmentPolicy::augmented_interface */
  847. node_type* header()const{return this->final_header();}
  848. node_type* root()const{return node_type::from_impl(header()->parent());}
  849. node_type* leftmost()const{return node_type::from_impl(header()->left());}
  850. node_type* rightmost()const{return node_type::from_impl(header()->right());}
  851. private:
  852. void empty_initialize()
  853. {
  854. header()->color()=red;
  855. /* used to distinguish header() from root, in iterator.operator++ */
  856. header()->parent()=node_impl_pointer(0);
  857. header()->left()=header()->impl();
  858. header()->right()=header()->impl();
  859. }
  860. struct link_info
  861. {
  862. /* coverity[uninit_ctor]: suppress warning */
  863. link_info():side(to_left){}
  864. ordered_index_side side;
  865. node_impl_pointer pos;
  866. };
  867. bool link_point(key_param_type k,link_info& inf,ordered_unique_tag)
  868. {
  869. node_type* y=header();
  870. node_type* x=root();
  871. bool c=true;
  872. while(x){
  873. y=x;
  874. c=comp_(k,key(x->value()));
  875. x=node_type::from_impl(c?x->left():x->right());
  876. }
  877. node_type* yy=y;
  878. if(c){
  879. if(yy==leftmost()){
  880. inf.side=to_left;
  881. inf.pos=y->impl();
  882. return true;
  883. }
  884. else node_type::decrement(yy);
  885. }
  886. if(comp_(key(yy->value()),k)){
  887. inf.side=c?to_left:to_right;
  888. inf.pos=y->impl();
  889. return true;
  890. }
  891. else{
  892. inf.pos=yy->impl();
  893. return false;
  894. }
  895. }
  896. bool link_point(key_param_type k,link_info& inf,ordered_non_unique_tag)
  897. {
  898. node_type* y=header();
  899. node_type* x=root();
  900. bool c=true;
  901. while (x){
  902. y=x;
  903. c=comp_(k,key(x->value()));
  904. x=node_type::from_impl(c?x->left():x->right());
  905. }
  906. inf.side=c?to_left:to_right;
  907. inf.pos=y->impl();
  908. return true;
  909. }
  910. bool lower_link_point(key_param_type k,link_info& inf,ordered_non_unique_tag)
  911. {
  912. node_type* y=header();
  913. node_type* x=root();
  914. bool c=false;
  915. while (x){
  916. y=x;
  917. c=comp_(key(x->value()),k);
  918. x=node_type::from_impl(c?x->right():x->left());
  919. }
  920. inf.side=c?to_right:to_left;
  921. inf.pos=y->impl();
  922. return true;
  923. }
  924. bool hinted_link_point(
  925. key_param_type k,node_type* position,link_info& inf,ordered_unique_tag)
  926. {
  927. if(position->impl()==header()->left()){
  928. if(size()>0&&comp_(k,key(position->value()))){
  929. inf.side=to_left;
  930. inf.pos=position->impl();
  931. return true;
  932. }
  933. else return link_point(k,inf,ordered_unique_tag());
  934. }
  935. else if(position==header()){
  936. if(comp_(key(rightmost()->value()),k)){
  937. inf.side=to_right;
  938. inf.pos=rightmost()->impl();
  939. return true;
  940. }
  941. else return link_point(k,inf,ordered_unique_tag());
  942. }
  943. else{
  944. node_type* before=position;
  945. node_type::decrement(before);
  946. if(comp_(key(before->value()),k)&&comp_(k,key(position->value()))){
  947. if(before->right()==node_impl_pointer(0)){
  948. inf.side=to_right;
  949. inf.pos=before->impl();
  950. return true;
  951. }
  952. else{
  953. inf.side=to_left;
  954. inf.pos=position->impl();
  955. return true;
  956. }
  957. }
  958. else return link_point(k,inf,ordered_unique_tag());
  959. }
  960. }
  961. bool hinted_link_point(
  962. key_param_type k,node_type* position,link_info& inf,ordered_non_unique_tag)
  963. {
  964. if(position->impl()==header()->left()){
  965. if(size()>0&&!comp_(key(position->value()),k)){
  966. inf.side=to_left;
  967. inf.pos=position->impl();
  968. return true;
  969. }
  970. else return lower_link_point(k,inf,ordered_non_unique_tag());
  971. }
  972. else if(position==header()){
  973. if(!comp_(k,key(rightmost()->value()))){
  974. inf.side=to_right;
  975. inf.pos=rightmost()->impl();
  976. return true;
  977. }
  978. else return link_point(k,inf,ordered_non_unique_tag());
  979. }
  980. else{
  981. node_type* before=position;
  982. node_type::decrement(before);
  983. if(!comp_(k,key(before->value()))){
  984. if(!comp_(key(position->value()),k)){
  985. if(before->right()==node_impl_pointer(0)){
  986. inf.side=to_right;
  987. inf.pos=before->impl();
  988. return true;
  989. }
  990. else{
  991. inf.side=to_left;
  992. inf.pos=position->impl();
  993. return true;
  994. }
  995. }
  996. else return lower_link_point(k,inf,ordered_non_unique_tag());
  997. }
  998. else return link_point(k,inf,ordered_non_unique_tag());
  999. }
  1000. }
  1001. void delete_all_nodes(node_type* x)
  1002. {
  1003. if(!x)return;
  1004. delete_all_nodes(node_type::from_impl(x->left()));
  1005. delete_all_nodes(node_type::from_impl(x->right()));
  1006. this->final_delete_node_(static_cast<final_node_type*>(x));
  1007. }
  1008. bool in_place(value_param_type v,node_type* x,ordered_unique_tag)const
  1009. {
  1010. node_type* y;
  1011. if(x!=leftmost()){
  1012. y=x;
  1013. node_type::decrement(y);
  1014. if(!comp_(key(y->value()),key(v)))return false;
  1015. }
  1016. y=x;
  1017. node_type::increment(y);
  1018. return y==header()||comp_(key(v),key(y->value()));
  1019. }
  1020. bool in_place(value_param_type v,node_type* x,ordered_non_unique_tag)const
  1021. {
  1022. node_type* y;
  1023. if(x!=leftmost()){
  1024. y=x;
  1025. node_type::decrement(y);
  1026. if(comp_(key(v),key(y->value())))return false;
  1027. }
  1028. y=x;
  1029. node_type::increment(y);
  1030. return y==header()||!comp_(key(y->value()),key(v));
  1031. }
  1032. #if defined(BOOST_MULTI_INDEX_ENABLE_SAFE_MODE)
  1033. void detach_iterators(node_type* x)
  1034. {
  1035. iterator it=make_iterator(x);
  1036. safe_mode::detach_equivalent_iterators(it);
  1037. }
  1038. #endif
  1039. template<BOOST_MULTI_INDEX_TEMPLATE_PARAM_PACK>
  1040. std::pair<iterator,bool> emplace_impl(BOOST_MULTI_INDEX_FUNCTION_PARAM_PACK)
  1041. {
  1042. BOOST_MULTI_INDEX_ORD_INDEX_CHECK_INVARIANT;
  1043. std::pair<final_node_type*,bool>p=
  1044. this->final_emplace_(BOOST_MULTI_INDEX_FORWARD_PARAM_PACK);
  1045. return std::pair<iterator,bool>(make_iterator(p.first),p.second);
  1046. }
  1047. template<BOOST_MULTI_INDEX_TEMPLATE_PARAM_PACK>
  1048. iterator emplace_hint_impl(
  1049. iterator position,BOOST_MULTI_INDEX_FUNCTION_PARAM_PACK)
  1050. {
  1051. BOOST_MULTI_INDEX_CHECK_VALID_ITERATOR(position);
  1052. BOOST_MULTI_INDEX_CHECK_IS_OWNER(position,*this);
  1053. BOOST_MULTI_INDEX_ORD_INDEX_CHECK_INVARIANT;
  1054. std::pair<final_node_type*,bool>p=
  1055. this->final_emplace_hint_(
  1056. static_cast<final_node_type*>(position.get_node()),
  1057. BOOST_MULTI_INDEX_FORWARD_PARAM_PACK);
  1058. return make_iterator(p.first);
  1059. }
  1060. template<typename LowerBounder,typename UpperBounder>
  1061. std::pair<iterator,iterator>
  1062. range(LowerBounder lower,UpperBounder upper,none_unbounded_tag)const
  1063. {
  1064. node_type* y=header();
  1065. node_type* z=root();
  1066. while(z){
  1067. if(!lower(key(z->value()))){
  1068. z=node_type::from_impl(z->right());
  1069. }
  1070. else if(!upper(key(z->value()))){
  1071. y=z;
  1072. z=node_type::from_impl(z->left());
  1073. }
  1074. else{
  1075. return std::pair<iterator,iterator>(
  1076. make_iterator(
  1077. lower_range(node_type::from_impl(z->left()),z,lower)),
  1078. make_iterator(
  1079. upper_range(node_type::from_impl(z->right()),y,upper)));
  1080. }
  1081. }
  1082. return std::pair<iterator,iterator>(make_iterator(y),make_iterator(y));
  1083. }
  1084. template<typename LowerBounder,typename UpperBounder>
  1085. std::pair<iterator,iterator>
  1086. range(LowerBounder,UpperBounder upper,lower_unbounded_tag)const
  1087. {
  1088. return std::pair<iterator,iterator>(
  1089. begin(),
  1090. make_iterator(upper_range(root(),header(),upper)));
  1091. }
  1092. template<typename LowerBounder,typename UpperBounder>
  1093. std::pair<iterator,iterator>
  1094. range(LowerBounder lower,UpperBounder,upper_unbounded_tag)const
  1095. {
  1096. return std::pair<iterator,iterator>(
  1097. make_iterator(lower_range(root(),header(),lower)),
  1098. end());
  1099. }
  1100. template<typename LowerBounder,typename UpperBounder>
  1101. std::pair<iterator,iterator>
  1102. range(LowerBounder,UpperBounder,both_unbounded_tag)const
  1103. {
  1104. return std::pair<iterator,iterator>(begin(),end());
  1105. }
  1106. template<typename LowerBounder>
  1107. node_type * lower_range(node_type* top,node_type* y,LowerBounder lower)const
  1108. {
  1109. while(top){
  1110. if(lower(key(top->value()))){
  1111. y=top;
  1112. top=node_type::from_impl(top->left());
  1113. }
  1114. else top=node_type::from_impl(top->right());
  1115. }
  1116. return y;
  1117. }
  1118. template<typename UpperBounder>
  1119. node_type * upper_range(node_type* top,node_type* y,UpperBounder upper)const
  1120. {
  1121. while(top){
  1122. if(!upper(key(top->value()))){
  1123. y=top;
  1124. top=node_type::from_impl(top->left());
  1125. }
  1126. else top=node_type::from_impl(top->right());
  1127. }
  1128. return y;
  1129. }
  1130. #if !defined(BOOST_MULTI_INDEX_DISABLE_SERIALIZATION)
  1131. template<typename Archive>
  1132. void save_(
  1133. Archive& ar,const unsigned int version,const index_saver_type& sm,
  1134. ordered_unique_tag)const
  1135. {
  1136. super::save_(ar,version,sm);
  1137. }
  1138. template<typename Archive>
  1139. void load_(
  1140. Archive& ar,const unsigned int version,const index_loader_type& lm,
  1141. ordered_unique_tag)
  1142. {
  1143. super::load_(ar,version,lm);
  1144. }
  1145. template<typename Archive>
  1146. void save_(
  1147. Archive& ar,const unsigned int version,const index_saver_type& sm,
  1148. ordered_non_unique_tag)const
  1149. {
  1150. typedef duplicates_iterator<node_type,value_compare> dup_iterator;
  1151. sm.save(
  1152. dup_iterator(begin().get_node(),end().get_node(),value_comp()),
  1153. dup_iterator(end().get_node(),value_comp()),
  1154. ar,version);
  1155. super::save_(ar,version,sm);
  1156. }
  1157. template<typename Archive>
  1158. void load_(
  1159. Archive& ar,const unsigned int version,const index_loader_type& lm,
  1160. ordered_non_unique_tag)
  1161. {
  1162. lm.load(
  1163. ::boost::bind(
  1164. &ordered_index_impl::rearranger,this,
  1165. ::boost::arg<1>(),::boost::arg<2>()),
  1166. ar,version);
  1167. super::load_(ar,version,lm);
  1168. }
  1169. void rearranger(node_type* position,node_type *x)
  1170. {
  1171. if(!position||comp_(key(position->value()),key(x->value()))){
  1172. position=lower_bound(key(x->value())).get_node();
  1173. }
  1174. else if(comp_(key(x->value()),key(position->value()))){
  1175. /* inconsistent rearrangement */
  1176. throw_exception(
  1177. archive::archive_exception(
  1178. archive::archive_exception::other_exception));
  1179. }
  1180. else node_type::increment(position);
  1181. if(position!=x){
  1182. node_impl_type::rebalance_for_erase(
  1183. x->impl(),header()->parent(),header()->left(),header()->right());
  1184. node_impl_type::restore(
  1185. x->impl(),position->impl(),header()->impl());
  1186. }
  1187. }
  1188. #endif /* serialization */
  1189. protected: /* for the benefit of AugmentPolicy::augmented_interface */
  1190. key_from_value key;
  1191. key_compare comp_;
  1192. #if defined(BOOST_MULTI_INDEX_ENABLE_INVARIANT_CHECKING)&&\
  1193. BOOST_WORKAROUND(__MWERKS__,<=0x3003)
  1194. #pragma parse_mfunc_templ reset
  1195. #endif
  1196. };
  1197. template<
  1198. typename KeyFromValue,typename Compare,
  1199. typename SuperMeta,typename TagList,typename Category,typename AugmentPolicy
  1200. >
  1201. class ordered_index:
  1202. public AugmentPolicy::template augmented_interface<
  1203. ordered_index_impl<
  1204. KeyFromValue,Compare,SuperMeta,TagList,Category,AugmentPolicy
  1205. >
  1206. >::type
  1207. {
  1208. typedef typename AugmentPolicy::template
  1209. augmented_interface<
  1210. ordered_index_impl<
  1211. KeyFromValue,Compare,
  1212. SuperMeta,TagList,Category,AugmentPolicy
  1213. >
  1214. >::type super;
  1215. public:
  1216. typedef typename super::ctor_args_list ctor_args_list;
  1217. typedef typename super::allocator_type allocator_type;
  1218. typedef typename super::iterator iterator;
  1219. /* construct/copy/destroy
  1220. * Default and copy ctors are in the protected section as indices are
  1221. * not supposed to be created on their own. No range ctor either.
  1222. */
  1223. ordered_index& operator=(const ordered_index& x)
  1224. {
  1225. this->final()=x.final();
  1226. return *this;
  1227. }
  1228. #if !defined(BOOST_NO_CXX11_HDR_INITIALIZER_LIST)
  1229. ordered_index& operator=(
  1230. std::initializer_list<BOOST_DEDUCED_TYPENAME super::value_type> list)
  1231. {
  1232. this->final()=list;
  1233. return *this;
  1234. }
  1235. #endif
  1236. protected:
  1237. ordered_index(
  1238. const ctor_args_list& args_list,const allocator_type& al):
  1239. super(args_list,al){}
  1240. ordered_index(const ordered_index& x):super(x){}
  1241. ordered_index(const ordered_index& x,do_not_copy_elements_tag):
  1242. super(x,do_not_copy_elements_tag()){}
  1243. };
  1244. /* comparison */
  1245. template<
  1246. typename KeyFromValue1,typename Compare1,
  1247. typename SuperMeta1,typename TagList1,typename Category1,
  1248. typename AugmentPolicy1,
  1249. typename KeyFromValue2,typename Compare2,
  1250. typename SuperMeta2,typename TagList2,typename Category2,
  1251. typename AugmentPolicy2
  1252. >
  1253. bool operator==(
  1254. const ordered_index<
  1255. KeyFromValue1,Compare1,SuperMeta1,TagList1,Category1,AugmentPolicy1>& x,
  1256. const ordered_index<
  1257. KeyFromValue2,Compare2,SuperMeta2,TagList2,Category2,AugmentPolicy2>& y)
  1258. {
  1259. return x.size()==y.size()&&std::equal(x.begin(),x.end(),y.begin());
  1260. }
  1261. template<
  1262. typename KeyFromValue1,typename Compare1,
  1263. typename SuperMeta1,typename TagList1,typename Category1,
  1264. typename AugmentPolicy1,
  1265. typename KeyFromValue2,typename Compare2,
  1266. typename SuperMeta2,typename TagList2,typename Category2,
  1267. typename AugmentPolicy2
  1268. >
  1269. bool operator<(
  1270. const ordered_index<
  1271. KeyFromValue1,Compare1,SuperMeta1,TagList1,Category1,AugmentPolicy1>& x,
  1272. const ordered_index<
  1273. KeyFromValue2,Compare2,SuperMeta2,TagList2,Category2,AugmentPolicy2>& y)
  1274. {
  1275. return std::lexicographical_compare(x.begin(),x.end(),y.begin(),y.end());
  1276. }
  1277. template<
  1278. typename KeyFromValue1,typename Compare1,
  1279. typename SuperMeta1,typename TagList1,typename Category1,
  1280. typename AugmentPolicy1,
  1281. typename KeyFromValue2,typename Compare2,
  1282. typename SuperMeta2,typename TagList2,typename Category2,
  1283. typename AugmentPolicy2
  1284. >
  1285. bool operator!=(
  1286. const ordered_index<
  1287. KeyFromValue1,Compare1,SuperMeta1,TagList1,Category1,AugmentPolicy1>& x,
  1288. const ordered_index<
  1289. KeyFromValue2,Compare2,SuperMeta2,TagList2,Category2,AugmentPolicy2>& y)
  1290. {
  1291. return !(x==y);
  1292. }
  1293. template<
  1294. typename KeyFromValue1,typename Compare1,
  1295. typename SuperMeta1,typename TagList1,typename Category1,
  1296. typename AugmentPolicy1,
  1297. typename KeyFromValue2,typename Compare2,
  1298. typename SuperMeta2,typename TagList2,typename Category2,
  1299. typename AugmentPolicy2
  1300. >
  1301. bool operator>(
  1302. const ordered_index<
  1303. KeyFromValue1,Compare1,SuperMeta1,TagList1,Category1,AugmentPolicy1>& x,
  1304. const ordered_index<
  1305. KeyFromValue2,Compare2,SuperMeta2,TagList2,Category2,AugmentPolicy2>& y)
  1306. {
  1307. return y<x;
  1308. }
  1309. template<
  1310. typename KeyFromValue1,typename Compare1,
  1311. typename SuperMeta1,typename TagList1,typename Category1,
  1312. typename AugmentPolicy1,
  1313. typename KeyFromValue2,typename Compare2,
  1314. typename SuperMeta2,typename TagList2,typename Category2,
  1315. typename AugmentPolicy2
  1316. >
  1317. bool operator>=(
  1318. const ordered_index<
  1319. KeyFromValue1,Compare1,SuperMeta1,TagList1,Category1,AugmentPolicy1>& x,
  1320. const ordered_index<
  1321. KeyFromValue2,Compare2,SuperMeta2,TagList2,Category2,AugmentPolicy2>& y)
  1322. {
  1323. return !(x<y);
  1324. }
  1325. template<
  1326. typename KeyFromValue1,typename Compare1,
  1327. typename SuperMeta1,typename TagList1,typename Category1,
  1328. typename AugmentPolicy1,
  1329. typename KeyFromValue2,typename Compare2,
  1330. typename SuperMeta2,typename TagList2,typename Category2,
  1331. typename AugmentPolicy2
  1332. >
  1333. bool operator<=(
  1334. const ordered_index<
  1335. KeyFromValue1,Compare1,SuperMeta1,TagList1,Category1,AugmentPolicy1>& x,
  1336. const ordered_index<
  1337. KeyFromValue2,Compare2,SuperMeta2,TagList2,Category2,AugmentPolicy2>& y)
  1338. {
  1339. return !(x>y);
  1340. }
  1341. /* specialized algorithms */
  1342. template<
  1343. typename KeyFromValue,typename Compare,
  1344. typename SuperMeta,typename TagList,typename Category,typename AugmentPolicy
  1345. >
  1346. void swap(
  1347. ordered_index<
  1348. KeyFromValue,Compare,SuperMeta,TagList,Category,AugmentPolicy>& x,
  1349. ordered_index<
  1350. KeyFromValue,Compare,SuperMeta,TagList,Category,AugmentPolicy>& y)
  1351. {
  1352. x.swap(y);
  1353. }
  1354. } /* namespace multi_index::detail */
  1355. } /* namespace multi_index */
  1356. } /* namespace boost */
  1357. /* Boost.Foreach compatibility */
  1358. template<
  1359. typename KeyFromValue,typename Compare,
  1360. typename SuperMeta,typename TagList,typename Category,typename AugmentPolicy
  1361. >
  1362. inline boost::mpl::true_* boost_foreach_is_noncopyable(
  1363. boost::multi_index::detail::ordered_index<
  1364. KeyFromValue,Compare,SuperMeta,TagList,Category,AugmentPolicy>*&,
  1365. boost_foreach_argument_dependent_lookup_hack)
  1366. {
  1367. return 0;
  1368. }
  1369. #undef BOOST_MULTI_INDEX_ORD_INDEX_CHECK_INVARIANT
  1370. #undef BOOST_MULTI_INDEX_ORD_INDEX_CHECK_INVARIANT_OF
  1371. #endif