ordered_index.hpp 45 KB

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