stable_vector.hpp 82 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199
  1. //////////////////////////////////////////////////////////////////////////////
  2. //
  3. // (C) Copyright Ion Gaztanaga 2008-2015. Distributed under the Boost
  4. // Software License, Version 1.0. (See accompanying file
  5. // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  6. //
  7. // See http://www.boost.org/libs/container for documentation.
  8. //
  9. //////////////////////////////////////////////////////////////////////////////
  10. // Stable vector.
  11. //
  12. // Copyright 2008 Joaquin M Lopez Munoz.
  13. // Distributed under the Boost Software License, Version 1.0.
  14. // (See accompanying file LICENSE_1_0.txt or copy at
  15. // http://www.boost.org/LICENSE_1_0.txt)
  16. //
  17. //////////////////////////////////////////////////////////////////////////////
  18. #ifndef BOOST_CONTAINER_STABLE_VECTOR_HPP
  19. #define BOOST_CONTAINER_STABLE_VECTOR_HPP
  20. #ifndef BOOST_CONFIG_HPP
  21. # include <boost/config.hpp>
  22. #endif
  23. #if defined(BOOST_HAS_PRAGMA_ONCE)
  24. # pragma once
  25. #endif
  26. #include <boost/container/detail/config_begin.hpp>
  27. #include <boost/container/detail/workaround.hpp>
  28. // container
  29. #include <boost/container/allocator_traits.hpp>
  30. #include <boost/container/container_fwd.hpp>
  31. #include <boost/container/new_allocator.hpp> //new_allocator
  32. #include <boost/container/throw_exception.hpp>
  33. // container/detail
  34. #include <boost/container/detail/addressof.hpp>
  35. #include <boost/container/detail/algorithm.hpp> //algo_equal(), algo_lexicographical_compare
  36. #include <boost/container/detail/alloc_helpers.hpp>
  37. #include <boost/container/detail/allocator_version_traits.hpp>
  38. #include <boost/container/detail/construct_in_place.hpp>
  39. #include <boost/container/detail/iterator.hpp>
  40. #include <boost/container/detail/iterators.hpp>
  41. #include <boost/container/detail/placement_new.hpp>
  42. #include <boost/move/detail/to_raw_pointer.hpp>
  43. #include <boost/container/detail/type_traits.hpp>
  44. // intrusive
  45. #include <boost/intrusive/pointer_traits.hpp>
  46. // intrusive/detail
  47. #include <boost/intrusive/detail/minimal_pair_header.hpp> //pair
  48. // move
  49. #include <boost/move/utility_core.hpp>
  50. #include <boost/move/iterator.hpp>
  51. #include <boost/move/adl_move_swap.hpp>
  52. // move/detail
  53. #include <boost/move/detail/move_helpers.hpp>
  54. // other
  55. #include <boost/assert.hpp>
  56. #include <boost/core/no_exceptions_support.hpp>
  57. // std
  58. #if !defined(BOOST_NO_CXX11_HDR_INITIALIZER_LIST)
  59. #include <initializer_list>
  60. #endif
  61. #ifndef BOOST_CONTAINER_DOXYGEN_INVOKED
  62. #include <boost/container/vector.hpp>
  63. //#define STABLE_VECTOR_ENABLE_INVARIANT_CHECKING
  64. #endif //#ifndef BOOST_CONTAINER_DOXYGEN_INVOKED
  65. namespace boost {
  66. namespace container {
  67. #ifndef BOOST_CONTAINER_DOXYGEN_INVOKED
  68. namespace stable_vector_detail{
  69. template <class C>
  70. class clear_on_destroy
  71. {
  72. public:
  73. BOOST_CONTAINER_FORCEINLINE clear_on_destroy(C &c)
  74. : c_(c), do_clear_(true)
  75. {}
  76. BOOST_CONTAINER_FORCEINLINE void release()
  77. { do_clear_ = false; }
  78. BOOST_CONTAINER_FORCEINLINE ~clear_on_destroy()
  79. {
  80. if(do_clear_){
  81. c_.clear();
  82. c_.priv_clear_pool();
  83. }
  84. }
  85. private:
  86. clear_on_destroy(const clear_on_destroy &);
  87. clear_on_destroy &operator=(const clear_on_destroy &);
  88. C &c_;
  89. bool do_clear_;
  90. };
  91. template<typename Pointer>
  92. struct node;
  93. template<class VoidPtr>
  94. struct node_base
  95. {
  96. private:
  97. typedef typename boost::intrusive::
  98. pointer_traits<VoidPtr> void_ptr_traits;
  99. typedef typename void_ptr_traits::
  100. template rebind_pointer
  101. <node_base>::type node_base_ptr;
  102. public:
  103. typedef typename void_ptr_traits::
  104. template rebind_pointer
  105. <node_base_ptr>::type node_base_ptr_ptr;
  106. public:
  107. BOOST_CONTAINER_FORCEINLINE explicit node_base(const node_base_ptr_ptr &n)
  108. : up(n)
  109. {}
  110. BOOST_CONTAINER_FORCEINLINE node_base()
  111. : up()
  112. {}
  113. node_base_ptr_ptr up;
  114. };
  115. template<typename Pointer>
  116. struct node
  117. : public node_base
  118. <typename ::boost::intrusive::pointer_traits<Pointer>::template
  119. rebind_pointer<void>::type
  120. >
  121. {
  122. public:
  123. typedef typename ::boost::intrusive::pointer_traits<Pointer>::element_type T;
  124. typedef node_base
  125. <typename ::boost::intrusive::pointer_traits<Pointer>::template
  126. rebind_pointer<void>::type
  127. > hook_type;
  128. typedef typename boost::container::dtl::aligned_storage
  129. <sizeof(T), boost::container::dtl::alignment_of<T>::value>::type storage_t;
  130. storage_t m_storage;
  131. BOOST_CONTAINER_FORCEINLINE explicit node(const typename hook_type::node_base_ptr_ptr &n)
  132. : hook_type(n)
  133. {}
  134. BOOST_CONTAINER_FORCEINLINE node()
  135. {}
  136. #if defined(BOOST_GCC) && (BOOST_GCC >= 40600) && (BOOST_GCC < 80000)
  137. #pragma GCC diagnostic push
  138. #pragma GCC diagnostic ignored "-Wstrict-aliasing"
  139. #define BOOST_CONTAINER_DISABLE_ALIASING_WARNING
  140. # endif
  141. BOOST_CONTAINER_FORCEINLINE T &get_data()
  142. { return *reinterpret_cast<T*>(this->m_storage.data); }
  143. BOOST_CONTAINER_FORCEINLINE const T &get_data() const
  144. { return *reinterpret_cast<const T*>(this->m_storage.data); }
  145. BOOST_CONTAINER_FORCEINLINE T *get_data_ptr()
  146. { return reinterpret_cast<T*>(this->m_storage.data); }
  147. BOOST_CONTAINER_FORCEINLINE const T *get_data_ptr() const
  148. { return reinterpret_cast<T*>(this->m_storage.data); }
  149. BOOST_CONTAINER_FORCEINLINE ~node()
  150. { reinterpret_cast<T*>(this->m_storage.data)->~T(); }
  151. #if defined(BOOST_CONTAINER_DISABLE_ALIASING_WARNING)
  152. #pragma GCC diagnostic pop
  153. #undef BOOST_CONTAINER_DISABLE_ALIASING_WARNING
  154. # endif
  155. BOOST_CONTAINER_FORCEINLINE void destroy_header()
  156. { static_cast<hook_type*>(this)->~hook_type(); }
  157. };
  158. template<class VoidPtr, class VoidAllocator>
  159. struct index_traits
  160. {
  161. typedef boost::intrusive::
  162. pointer_traits
  163. <VoidPtr> void_ptr_traits;
  164. typedef stable_vector_detail::
  165. node_base<VoidPtr> node_base_type;
  166. typedef typename void_ptr_traits::template
  167. rebind_pointer<node_base_type>::type node_base_ptr;
  168. typedef typename void_ptr_traits::template
  169. rebind_pointer<node_base_ptr>::type node_base_ptr_ptr;
  170. typedef boost::intrusive::
  171. pointer_traits<node_base_ptr> node_base_ptr_traits;
  172. typedef boost::intrusive::
  173. pointer_traits<node_base_ptr_ptr> node_base_ptr_ptr_traits;
  174. typedef typename allocator_traits<VoidAllocator>::
  175. template portable_rebind_alloc
  176. <node_base_ptr>::type node_base_ptr_allocator;
  177. typedef ::boost::container::vector
  178. <node_base_ptr, node_base_ptr_allocator> index_type;
  179. typedef typename index_type::iterator index_iterator;
  180. typedef typename index_type::const_iterator const_index_iterator;
  181. typedef typename index_type::size_type size_type;
  182. static const size_type ExtraPointers = 3;
  183. //Stable vector stores metadata at the end of the index (node_base_ptr vector) with additional 3 pointers:
  184. // back() is this->index.back() - ExtraPointers;
  185. // end node index is *(this->index.end() - 3)
  186. // Node cache first is *(this->index.end() - 2);
  187. // Node cache last is this->index.back();
  188. BOOST_CONTAINER_FORCEINLINE static node_base_ptr_ptr ptr_to_node_base_ptr(node_base_ptr &n)
  189. { return node_base_ptr_ptr_traits::pointer_to(n); }
  190. static void fix_up_pointers(index_iterator first, index_iterator last)
  191. {
  192. while(first != last){
  193. typedef typename index_type::reference node_base_ptr_ref;
  194. node_base_ptr_ref nbp = *first;
  195. nbp->up = index_traits::ptr_to_node_base_ptr(nbp);
  196. ++first;
  197. }
  198. }
  199. BOOST_CONTAINER_FORCEINLINE static index_iterator get_fix_up_end(index_type &index)
  200. { return index.end() - (ExtraPointers - 1); }
  201. BOOST_CONTAINER_FORCEINLINE static void fix_up_pointers_from(index_type & index, index_iterator first)
  202. { index_traits::fix_up_pointers(first, index_traits::get_fix_up_end(index)); }
  203. static void readjust_end_node(index_type &index, node_base_type &end_node)
  204. {
  205. if(!index.empty()){
  206. index_iterator end_node_it(index_traits::get_fix_up_end(index));
  207. node_base_ptr &end_node_idx_ref = *(--end_node_it);
  208. end_node_idx_ref = node_base_ptr_traits::pointer_to(end_node);
  209. end_node.up = node_base_ptr_ptr_traits::pointer_to(end_node_idx_ref);
  210. }
  211. else{
  212. end_node.up = node_base_ptr_ptr();
  213. }
  214. }
  215. static void initialize_end_node(index_type &index, node_base_type &end_node, const size_type index_capacity_if_empty)
  216. {
  217. if(index.empty()){
  218. index.reserve(index_capacity_if_empty + ExtraPointers);
  219. index.resize(ExtraPointers);
  220. node_base_ptr &end_node_ref = *index.data();
  221. end_node_ref = node_base_ptr_traits::pointer_to(end_node);
  222. end_node.up = index_traits::ptr_to_node_base_ptr(end_node_ref);
  223. }
  224. }
  225. #ifdef STABLE_VECTOR_ENABLE_INVARIANT_CHECKING
  226. static bool invariants(index_type &index)
  227. {
  228. for( index_iterator it = index.begin()
  229. , it_end = index_traits::get_fix_up_end(index)
  230. ; it != it_end
  231. ; ++it){
  232. if((*it)->up != index_traits::ptr_to_node_base_ptr(*it)){
  233. return false;
  234. }
  235. }
  236. return true;
  237. }
  238. #endif //STABLE_VECTOR_ENABLE_INVARIANT_CHECKING
  239. };
  240. } //namespace stable_vector_detail
  241. template<typename Pointer, bool IsConst>
  242. class stable_vector_iterator
  243. {
  244. typedef boost::intrusive::pointer_traits<Pointer> non_const_ptr_traits;
  245. public:
  246. typedef std::random_access_iterator_tag iterator_category;
  247. typedef typename non_const_ptr_traits::element_type value_type;
  248. typedef typename non_const_ptr_traits::difference_type difference_type;
  249. typedef typename ::boost::container::dtl::if_c
  250. < IsConst
  251. , typename non_const_ptr_traits::template
  252. rebind_pointer<const value_type>::type
  253. , Pointer
  254. >::type pointer;
  255. typedef boost::intrusive::pointer_traits<pointer> ptr_traits;
  256. typedef typename ptr_traits::reference reference;
  257. typedef typename non_const_ptr_traits::template
  258. rebind_pointer<void>::type void_ptr;
  259. typedef stable_vector_detail::node<Pointer> node_type;
  260. typedef stable_vector_detail::node_base<void_ptr> node_base_type;
  261. typedef typename non_const_ptr_traits::template
  262. rebind_pointer<node_type>::type node_ptr;
  263. typedef boost::intrusive::
  264. pointer_traits<node_ptr> node_ptr_traits;
  265. typedef typename non_const_ptr_traits::template
  266. rebind_pointer<node_base_type>::type node_base_ptr;
  267. typedef typename non_const_ptr_traits::template
  268. rebind_pointer<node_base_ptr>::type node_base_ptr_ptr;
  269. class nat
  270. {
  271. public:
  272. node_base_ptr node_pointer() const
  273. { return node_base_ptr(); }
  274. };
  275. typedef typename dtl::if_c< IsConst
  276. , stable_vector_iterator<Pointer, false>
  277. , nat>::type nonconst_iterator;
  278. node_base_ptr m_pn;
  279. public:
  280. BOOST_CONTAINER_FORCEINLINE explicit stable_vector_iterator(node_base_ptr p) BOOST_NOEXCEPT_OR_NOTHROW
  281. : m_pn(p)
  282. {}
  283. BOOST_CONTAINER_FORCEINLINE stable_vector_iterator() BOOST_NOEXCEPT_OR_NOTHROW
  284. : m_pn() //Value initialization to achieve "null iterators" (N3644)
  285. {}
  286. BOOST_CONTAINER_FORCEINLINE stable_vector_iterator(const stable_vector_iterator& other) BOOST_NOEXCEPT_OR_NOTHROW
  287. : m_pn(other.node_pointer())
  288. {}
  289. stable_vector_iterator(const nonconst_iterator& other) BOOST_NOEXCEPT_OR_NOTHROW
  290. : m_pn(other.node_pointer())
  291. {}
  292. BOOST_CONTAINER_FORCEINLINE stable_vector_iterator & operator=(const stable_vector_iterator& other) BOOST_NOEXCEPT_OR_NOTHROW
  293. { m_pn = other.node_pointer(); return *this; }
  294. BOOST_CONTAINER_FORCEINLINE node_ptr node_pointer() const BOOST_NOEXCEPT_OR_NOTHROW
  295. { return node_ptr_traits::static_cast_from(m_pn); }
  296. public:
  297. //Pointer like operators
  298. BOOST_CONTAINER_FORCEINLINE reference operator*() const BOOST_NOEXCEPT_OR_NOTHROW
  299. { return node_pointer()->get_data(); }
  300. BOOST_CONTAINER_FORCEINLINE pointer operator->() const BOOST_NOEXCEPT_OR_NOTHROW
  301. { return ptr_traits::pointer_to(this->operator*()); }
  302. //Increment / Decrement
  303. BOOST_CONTAINER_FORCEINLINE stable_vector_iterator& operator++() BOOST_NOEXCEPT_OR_NOTHROW
  304. {
  305. node_base_ptr_ptr p(this->m_pn->up);
  306. this->m_pn = *(++p);
  307. return *this;
  308. }
  309. BOOST_CONTAINER_FORCEINLINE stable_vector_iterator operator++(int) BOOST_NOEXCEPT_OR_NOTHROW
  310. { stable_vector_iterator tmp(*this); ++*this; return stable_vector_iterator(tmp); }
  311. BOOST_CONTAINER_FORCEINLINE stable_vector_iterator& operator--() BOOST_NOEXCEPT_OR_NOTHROW
  312. {
  313. node_base_ptr_ptr p(this->m_pn->up);
  314. this->m_pn = *(--p);
  315. return *this;
  316. }
  317. BOOST_CONTAINER_FORCEINLINE stable_vector_iterator operator--(int) BOOST_NOEXCEPT_OR_NOTHROW
  318. { stable_vector_iterator tmp(*this); --*this; return stable_vector_iterator(tmp); }
  319. BOOST_CONTAINER_FORCEINLINE reference operator[](difference_type off) const BOOST_NOEXCEPT_OR_NOTHROW
  320. { return node_ptr_traits::static_cast_from(this->m_pn->up[off])->get_data(); }
  321. BOOST_CONTAINER_FORCEINLINE stable_vector_iterator& operator+=(difference_type off) BOOST_NOEXCEPT_OR_NOTHROW
  322. {
  323. if(off) this->m_pn = this->m_pn->up[off];
  324. return *this;
  325. }
  326. BOOST_CONTAINER_FORCEINLINE friend stable_vector_iterator operator+(const stable_vector_iterator &left, difference_type off) BOOST_NOEXCEPT_OR_NOTHROW
  327. {
  328. stable_vector_iterator tmp(left);
  329. tmp += off;
  330. return tmp;
  331. }
  332. BOOST_CONTAINER_FORCEINLINE friend stable_vector_iterator operator+(difference_type off, const stable_vector_iterator& right) BOOST_NOEXCEPT_OR_NOTHROW
  333. {
  334. stable_vector_iterator tmp(right);
  335. tmp += off;
  336. return tmp;
  337. }
  338. BOOST_CONTAINER_FORCEINLINE stable_vector_iterator& operator-=(difference_type off) BOOST_NOEXCEPT_OR_NOTHROW
  339. { *this += -off; return *this; }
  340. BOOST_CONTAINER_FORCEINLINE friend stable_vector_iterator operator-(const stable_vector_iterator &left, difference_type off) BOOST_NOEXCEPT_OR_NOTHROW
  341. {
  342. stable_vector_iterator tmp(left);
  343. tmp -= off;
  344. return tmp;
  345. }
  346. BOOST_CONTAINER_FORCEINLINE friend difference_type operator-(const stable_vector_iterator &left, const stable_vector_iterator &right) BOOST_NOEXCEPT_OR_NOTHROW
  347. { return left.m_pn->up - right.m_pn->up; }
  348. //Comparison operators
  349. BOOST_CONTAINER_FORCEINLINE friend bool operator== (const stable_vector_iterator& l, const stable_vector_iterator& r) BOOST_NOEXCEPT_OR_NOTHROW
  350. { return l.m_pn == r.m_pn; }
  351. BOOST_CONTAINER_FORCEINLINE friend bool operator!= (const stable_vector_iterator& l, const stable_vector_iterator& r) BOOST_NOEXCEPT_OR_NOTHROW
  352. { return l.m_pn != r.m_pn; }
  353. BOOST_CONTAINER_FORCEINLINE friend bool operator< (const stable_vector_iterator& l, const stable_vector_iterator& r) BOOST_NOEXCEPT_OR_NOTHROW
  354. { return l.m_pn->up < r.m_pn->up; }
  355. BOOST_CONTAINER_FORCEINLINE friend bool operator<= (const stable_vector_iterator& l, const stable_vector_iterator& r) BOOST_NOEXCEPT_OR_NOTHROW
  356. { return l.m_pn->up <= r.m_pn->up; }
  357. BOOST_CONTAINER_FORCEINLINE friend bool operator> (const stable_vector_iterator& l, const stable_vector_iterator& r) BOOST_NOEXCEPT_OR_NOTHROW
  358. { return l.m_pn->up > r.m_pn->up; }
  359. BOOST_CONTAINER_FORCEINLINE friend bool operator>= (const stable_vector_iterator& l, const stable_vector_iterator& r) BOOST_NOEXCEPT_OR_NOTHROW
  360. { return l.m_pn->up >= r.m_pn->up; }
  361. };
  362. #if defined(STABLE_VECTOR_ENABLE_INVARIANT_CHECKING)
  363. #define STABLE_VECTOR_CHECK_INVARIANT \
  364. invariant_checker BOOST_JOIN(check_invariant_,__LINE__)(*this); \
  365. BOOST_JOIN(check_invariant_,__LINE__).touch();
  366. #else //STABLE_VECTOR_ENABLE_INVARIANT_CHECKING
  367. #define STABLE_VECTOR_CHECK_INVARIANT
  368. #endif //#if defined(STABLE_VECTOR_ENABLE_INVARIANT_CHECKING)
  369. #endif //#ifndef BOOST_CONTAINER_DOXYGEN_INVOKED
  370. //! Originally developed by Joaquin M. Lopez Munoz, stable_vector is a std::vector
  371. //! drop-in replacement implemented as a node container, offering iterator and reference
  372. //! stability.
  373. //!
  374. //! Here are the details taken from the author's blog
  375. //! (<a href="http://bannalia.blogspot.com/2008/09/introducing-stablevector.html" >
  376. //! Introducing stable_vector</a>):
  377. //!
  378. //! We present stable_vector, a fully STL-compliant stable container that provides
  379. //! most of the features of std::vector except element contiguity.
  380. //!
  381. //! General properties: stable_vector satisfies all the requirements of a container,
  382. //! a reversible container and a sequence and provides all the optional operations
  383. //! present in std::vector. Like std::vector, iterators are random access.
  384. //! stable_vector does not provide element contiguity; in exchange for this absence,
  385. //! the container is stable, i.e. references and iterators to an element of a stable_vector
  386. //! remain valid as long as the element is not erased, and an iterator that has been
  387. //! assigned the return value of end() always remain valid until the destruction of
  388. //! the associated stable_vector.
  389. //!
  390. //! Operation complexity: The big-O complexities of stable_vector operations match
  391. //! exactly those of std::vector. In general, insertion/deletion is constant time at
  392. //! the end of the sequence and linear elsewhere. Unlike std::vector, stable_vector
  393. //! does not internally perform any value_type destruction, copy or assignment
  394. //! operations other than those exactly corresponding to the insertion of new
  395. //! elements or deletion of stored elements, which can sometimes compensate in terms
  396. //! of performance for the extra burden of doing more pointer manipulation and an
  397. //! additional allocation per element.
  398. //!
  399. //! Exception safety: As stable_vector does not internally copy elements around, some
  400. //! operations provide stronger exception safety guarantees than in std::vector.
  401. //!
  402. //! \tparam T The type of object that is stored in the stable_vector
  403. //! \tparam Allocator The allocator used for all internal memory management
  404. #ifdef BOOST_CONTAINER_DOXYGEN_INVOKED
  405. template <class T, class Allocator = void >
  406. #else
  407. template <class T, class Allocator>
  408. #endif
  409. class stable_vector
  410. {
  411. #ifndef BOOST_CONTAINER_DOXYGEN_INVOKED
  412. typedef typename real_allocator<T, Allocator>::type ValueAllocator;
  413. typedef allocator_traits<ValueAllocator> allocator_traits_type;
  414. typedef boost::intrusive::
  415. pointer_traits
  416. <typename allocator_traits_type::pointer> ptr_traits;
  417. typedef typename ptr_traits::
  418. template rebind_pointer<void>::type void_ptr;
  419. typedef typename allocator_traits_type::
  420. template portable_rebind_alloc
  421. <void>::type void_allocator_type;
  422. typedef stable_vector_detail::index_traits
  423. <void_ptr, void_allocator_type> index_traits_type;
  424. typedef typename index_traits_type::node_base_type node_base_type;
  425. typedef typename index_traits_type::node_base_ptr node_base_ptr;
  426. typedef typename index_traits_type::
  427. node_base_ptr_ptr node_base_ptr_ptr;
  428. typedef typename index_traits_type::
  429. node_base_ptr_traits node_base_ptr_traits;
  430. typedef typename index_traits_type::
  431. node_base_ptr_ptr_traits node_base_ptr_ptr_traits;
  432. typedef typename index_traits_type::index_type index_type;
  433. typedef typename index_traits_type::index_iterator index_iterator;
  434. typedef typename index_traits_type::
  435. const_index_iterator const_index_iterator;
  436. typedef stable_vector_detail::node
  437. <typename ptr_traits::pointer> node_type;
  438. typedef typename ptr_traits::template
  439. rebind_pointer<node_type>::type node_ptr;
  440. typedef boost::intrusive::
  441. pointer_traits<node_ptr> node_ptr_traits;
  442. typedef typename ptr_traits::template
  443. rebind_pointer<const node_type>::type const_node_ptr;
  444. typedef boost::intrusive::
  445. pointer_traits<const_node_ptr> const_node_ptr_traits;
  446. typedef typename node_ptr_traits::reference node_reference;
  447. typedef typename const_node_ptr_traits::reference const_node_reference;
  448. typedef ::boost::container::dtl::integral_constant
  449. <unsigned, boost::container::dtl::
  450. version<ValueAllocator>::value> alloc_version;
  451. typedef typename allocator_traits_type::
  452. template portable_rebind_alloc
  453. <node_type>::type node_allocator_type;
  454. typedef ::boost::container::dtl::
  455. allocator_version_traits<node_allocator_type> allocator_version_traits_t;
  456. typedef typename allocator_version_traits_t::multiallocation_chain multiallocation_chain;
  457. BOOST_CONTAINER_FORCEINLINE node_ptr allocate_one()
  458. { return allocator_version_traits_t::allocate_one(this->priv_node_alloc()); }
  459. BOOST_CONTAINER_FORCEINLINE void deallocate_one(const node_ptr &p)
  460. { allocator_version_traits_t::deallocate_one(this->priv_node_alloc(), p); }
  461. BOOST_CONTAINER_FORCEINLINE void allocate_individual(typename allocator_traits_type::size_type n, multiallocation_chain &m)
  462. { allocator_version_traits_t::allocate_individual(this->priv_node_alloc(), n, m); }
  463. BOOST_CONTAINER_FORCEINLINE void deallocate_individual(multiallocation_chain &holder)
  464. { allocator_version_traits_t::deallocate_individual(this->priv_node_alloc(), holder); }
  465. friend class stable_vector_detail::clear_on_destroy<stable_vector>;
  466. typedef stable_vector_iterator
  467. < typename allocator_traits<ValueAllocator>::pointer
  468. , false> iterator_impl;
  469. typedef stable_vector_iterator
  470. < typename allocator_traits<ValueAllocator>::pointer
  471. , true> const_iterator_impl;
  472. #endif //#ifndef BOOST_CONTAINER_DOXYGEN_INVOKED
  473. public:
  474. //////////////////////////////////////////////
  475. //
  476. // types
  477. //
  478. //////////////////////////////////////////////
  479. typedef T value_type;
  480. typedef typename ::boost::container::allocator_traits<ValueAllocator>::pointer pointer;
  481. typedef typename ::boost::container::allocator_traits<ValueAllocator>::const_pointer const_pointer;
  482. typedef typename ::boost::container::allocator_traits<ValueAllocator>::reference reference;
  483. typedef typename ::boost::container::allocator_traits<ValueAllocator>::const_reference const_reference;
  484. typedef typename ::boost::container::allocator_traits<ValueAllocator>::size_type size_type;
  485. typedef typename ::boost::container::allocator_traits<ValueAllocator>::difference_type difference_type;
  486. typedef ValueAllocator allocator_type;
  487. typedef node_allocator_type stored_allocator_type;
  488. typedef BOOST_CONTAINER_IMPDEF(iterator_impl) iterator;
  489. typedef BOOST_CONTAINER_IMPDEF(const_iterator_impl) const_iterator;
  490. typedef BOOST_CONTAINER_IMPDEF(boost::container::reverse_iterator<iterator>) reverse_iterator;
  491. typedef BOOST_CONTAINER_IMPDEF(boost::container::reverse_iterator<const_iterator>) const_reverse_iterator;
  492. #ifndef BOOST_CONTAINER_DOXYGEN_INVOKED
  493. private:
  494. BOOST_COPYABLE_AND_MOVABLE(stable_vector)
  495. static const size_type ExtraPointers = index_traits_type::ExtraPointers;
  496. class insert_rollback;
  497. friend class insert_rollback;
  498. class push_back_rollback;
  499. friend class push_back_rollback;
  500. #endif //#ifndef BOOST_CONTAINER_DOXYGEN_INVOKED
  501. public:
  502. //////////////////////////////////////////////
  503. //
  504. // construct/copy/destroy
  505. //
  506. //////////////////////////////////////////////
  507. //! <b>Effects</b>: Default constructs a stable_vector.
  508. //!
  509. //! <b>Throws</b>: If allocator_type's default constructor throws.
  510. //!
  511. //! <b>Complexity</b>: Constant.
  512. BOOST_CONTAINER_FORCEINLINE stable_vector() BOOST_NOEXCEPT_IF(dtl::is_nothrow_default_constructible<ValueAllocator>::value)
  513. : internal_data(), index()
  514. {
  515. STABLE_VECTOR_CHECK_INVARIANT;
  516. }
  517. //! <b>Effects</b>: Constructs a stable_vector taking the allocator as parameter.
  518. //!
  519. //! <b>Throws</b>: Nothing
  520. //!
  521. //! <b>Complexity</b>: Constant.
  522. BOOST_CONTAINER_FORCEINLINE explicit stable_vector(const allocator_type& al) BOOST_NOEXCEPT_OR_NOTHROW
  523. : internal_data(al), index(al)
  524. {
  525. STABLE_VECTOR_CHECK_INVARIANT;
  526. }
  527. //! <b>Effects</b>: Constructs a stable_vector
  528. //! and inserts n value initialized values.
  529. //!
  530. //! <b>Throws</b>: If allocator_type's default constructor
  531. //! throws or T's default or copy constructor throws.
  532. //!
  533. //! <b>Complexity</b>: Linear to n.
  534. explicit stable_vector(size_type n)
  535. : internal_data(), index()
  536. {
  537. stable_vector_detail::clear_on_destroy<stable_vector> cod(*this);
  538. this->resize(n);
  539. STABLE_VECTOR_CHECK_INVARIANT;
  540. cod.release();
  541. }
  542. //! <b>Effects</b>: Constructs a stable_vector
  543. //! and inserts n default initialized values.
  544. //!
  545. //! <b>Throws</b>: If allocator_type's default constructor
  546. //! throws or T's default or copy constructor throws.
  547. //!
  548. //! <b>Complexity</b>: Linear to n.
  549. //!
  550. //! <b>Note</b>: Non-standard extension
  551. stable_vector(size_type n, default_init_t)
  552. : internal_data(), index()
  553. {
  554. stable_vector_detail::clear_on_destroy<stable_vector> cod(*this);
  555. this->resize(n, default_init);
  556. STABLE_VECTOR_CHECK_INVARIANT;
  557. cod.release();
  558. }
  559. //! <b>Effects</b>: Constructs a stable_vector that will use a copy of allocator a
  560. //! and inserts n value initialized values.
  561. //!
  562. //! <b>Throws</b>: If allocator_type's default constructor
  563. //! throws or T's default or copy constructor throws.
  564. //!
  565. //! <b>Complexity</b>: Linear to n.
  566. explicit stable_vector(size_type n, const allocator_type &a)
  567. : internal_data(), index(a)
  568. {
  569. stable_vector_detail::clear_on_destroy<stable_vector> cod(*this);
  570. this->resize(n);
  571. STABLE_VECTOR_CHECK_INVARIANT;
  572. cod.release();
  573. }
  574. //! <b>Effects</b>: Constructs a stable_vector that will use a copy of allocator a
  575. //! and inserts n default initialized values.
  576. //!
  577. //! <b>Throws</b>: If allocator_type's default constructor
  578. //! throws or T's default or copy constructor throws.
  579. //!
  580. //! <b>Complexity</b>: Linear to n.
  581. //!
  582. //! <b>Note</b>: Non-standard extension
  583. stable_vector(size_type n, default_init_t, const allocator_type &a)
  584. : internal_data(), index(a)
  585. {
  586. stable_vector_detail::clear_on_destroy<stable_vector> cod(*this);
  587. this->resize(n, default_init);
  588. STABLE_VECTOR_CHECK_INVARIANT;
  589. cod.release();
  590. }
  591. //! <b>Effects</b>: Constructs a stable_vector that will use a copy of allocator a
  592. //! and inserts n copies of value.
  593. //!
  594. //! <b>Throws</b>: If allocator_type's default constructor
  595. //! throws or T's default or copy constructor throws.
  596. //!
  597. //! <b>Complexity</b>: Linear to n.
  598. stable_vector(size_type n, const T& t, const allocator_type& al = allocator_type())
  599. : internal_data(al), index(al)
  600. {
  601. stable_vector_detail::clear_on_destroy<stable_vector> cod(*this);
  602. this->insert(this->cend(), n, t);
  603. STABLE_VECTOR_CHECK_INVARIANT;
  604. cod.release();
  605. }
  606. //! <b>Effects</b>: Constructs a stable_vector that will use a copy of allocator a
  607. //! and inserts a copy of the range [first, last) in the stable_vector.
  608. //!
  609. //! <b>Throws</b>: If allocator_type's default constructor
  610. //! throws or T's constructor taking a dereferenced InIt throws.
  611. //!
  612. //! <b>Complexity</b>: Linear to the range [first, last).
  613. template <class InputIterator>
  614. stable_vector(InputIterator first,InputIterator last, const allocator_type& al = allocator_type())
  615. : internal_data(al), index(al)
  616. {
  617. stable_vector_detail::clear_on_destroy<stable_vector> cod(*this);
  618. this->insert(this->cend(), first, last);
  619. STABLE_VECTOR_CHECK_INVARIANT;
  620. cod.release();
  621. }
  622. //! <b>Effects</b>: Copy constructs a stable_vector.
  623. //!
  624. //! <b>Postcondition</b>: x == *this.
  625. //!
  626. //! <b>Complexity</b>: Linear to the elements x contains.
  627. stable_vector(const stable_vector& x)
  628. : internal_data(allocator_traits<node_allocator_type>::
  629. select_on_container_copy_construction(x.priv_node_alloc()))
  630. , index(allocator_traits<allocator_type>::
  631. select_on_container_copy_construction(x.index.get_stored_allocator()))
  632. {
  633. stable_vector_detail::clear_on_destroy<stable_vector> cod(*this);
  634. this->insert(this->cend(), x.begin(), x.end());
  635. STABLE_VECTOR_CHECK_INVARIANT;
  636. cod.release();
  637. }
  638. #if !defined(BOOST_NO_CXX11_HDR_INITIALIZER_LIST)
  639. //! <b>Effects</b>: Constructs a stable_vector that will use a copy of allocator a
  640. //! and inserts a copy of the range [il.begin(), il.last()) in the stable_vector
  641. //!
  642. //! <b>Throws</b>: If allocator_type's default constructor
  643. //! throws or T's constructor taking a dereferenced initializer_list iterator throws.
  644. //!
  645. //! <b>Complexity</b>: Linear to the range [il.begin(), il.end()).
  646. stable_vector(std::initializer_list<value_type> il, const allocator_type& l = allocator_type())
  647. : internal_data(l), index(l)
  648. {
  649. stable_vector_detail::clear_on_destroy<stable_vector> cod(*this);
  650. insert(cend(), il.begin(), il.end());
  651. STABLE_VECTOR_CHECK_INVARIANT;
  652. cod.release();
  653. }
  654. #endif
  655. //! <b>Effects</b>: Move constructor. Moves x's resources to *this.
  656. //!
  657. //! <b>Throws</b>: If allocator_type's copy constructor throws.
  658. //!
  659. //! <b>Complexity</b>: Constant.
  660. BOOST_CONTAINER_FORCEINLINE stable_vector(BOOST_RV_REF(stable_vector) x) BOOST_NOEXCEPT_OR_NOTHROW
  661. : internal_data(boost::move(x.priv_node_alloc())), index(boost::move(x.index))
  662. {
  663. this->priv_swap_members(x);
  664. }
  665. //! <b>Effects</b>: Copy constructs a stable_vector using the specified allocator.
  666. //!
  667. //! <b>Postcondition</b>: x == *this.
  668. //!
  669. //! <b>Complexity</b>: Linear to the elements x contains.
  670. stable_vector(const stable_vector& x, const allocator_type &a)
  671. : internal_data(a), index(a)
  672. {
  673. stable_vector_detail::clear_on_destroy<stable_vector> cod(*this);
  674. this->insert(this->cend(), x.begin(), x.end());
  675. STABLE_VECTOR_CHECK_INVARIANT;
  676. cod.release();
  677. }
  678. //! <b>Effects</b>: Move constructor using the specified allocator.
  679. //! Moves x's resources to *this.
  680. //!
  681. //! <b>Throws</b>: If allocator_type's copy constructor throws.
  682. //!
  683. //! <b>Complexity</b>: Constant if a == x.get_allocator(), linear otherwise
  684. stable_vector(BOOST_RV_REF(stable_vector) x, const allocator_type &a)
  685. : internal_data(a), index(a)
  686. {
  687. if(this->priv_node_alloc() == x.priv_node_alloc()){
  688. this->index.swap(x.index);
  689. this->priv_swap_members(x);
  690. }
  691. else{
  692. stable_vector_detail::clear_on_destroy<stable_vector> cod(*this);
  693. this->insert(this->cend(), boost::make_move_iterator(x.begin()), boost::make_move_iterator(x.end()));
  694. STABLE_VECTOR_CHECK_INVARIANT;
  695. cod.release();
  696. }
  697. }
  698. //! <b>Effects</b>: Destroys the stable_vector. All stored values are destroyed
  699. //! and used memory is deallocated.
  700. //!
  701. //! <b>Throws</b>: Nothing.
  702. //!
  703. //! <b>Complexity</b>: Linear to the number of elements.
  704. ~stable_vector()
  705. {
  706. this->clear();
  707. this->priv_clear_pool();
  708. }
  709. //! <b>Effects</b>: Makes *this contain the same elements as x.
  710. //!
  711. //! <b>Postcondition</b>: this->size() == x.size(). *this contains a copy
  712. //! of each of x's elements.
  713. //!
  714. //! <b>Throws</b>: If memory allocation throws or T's copy constructor throws.
  715. //!
  716. //! <b>Complexity</b>: Linear to the number of elements in x.
  717. stable_vector& operator=(BOOST_COPY_ASSIGN_REF(stable_vector) x)
  718. {
  719. STABLE_VECTOR_CHECK_INVARIANT;
  720. if (&x != this){
  721. node_allocator_type &this_alloc = this->priv_node_alloc();
  722. const node_allocator_type &x_alloc = x.priv_node_alloc();
  723. dtl::bool_<allocator_traits_type::
  724. propagate_on_container_copy_assignment::value> flag;
  725. if(flag && this_alloc != x_alloc){
  726. this->clear();
  727. this->shrink_to_fit();
  728. }
  729. dtl::assign_alloc(this->priv_node_alloc(), x.priv_node_alloc(), flag);
  730. dtl::assign_alloc(this->index.get_stored_allocator(), x.index.get_stored_allocator(), flag);
  731. this->assign(x.begin(), x.end());
  732. }
  733. return *this;
  734. }
  735. //! <b>Effects</b>: Move assignment. All x's values are transferred to *this.
  736. //!
  737. //! <b>Postcondition</b>: x.empty(). *this contains a the elements x had
  738. //! before the function.
  739. //!
  740. //! <b>Throws</b>: If allocator_traits_type::propagate_on_container_move_assignment
  741. //! is false and (allocation throws or T's move constructor throws)
  742. //!
  743. //! <b>Complexity</b>: Constant if allocator_traits_type::
  744. //! propagate_on_container_move_assignment is true or
  745. //! this->get>allocator() == x.get_allocator(). Linear otherwise.
  746. stable_vector& operator=(BOOST_RV_REF(stable_vector) x)
  747. BOOST_NOEXCEPT_IF(allocator_traits_type::propagate_on_container_move_assignment::value
  748. || allocator_traits_type::is_always_equal::value)
  749. {
  750. //for move constructor, no aliasing (&x != this) is assumed.
  751. BOOST_ASSERT(this != &x);
  752. node_allocator_type &this_alloc = this->priv_node_alloc();
  753. node_allocator_type &x_alloc = x.priv_node_alloc();
  754. const bool propagate_alloc = allocator_traits_type::
  755. propagate_on_container_move_assignment::value;
  756. dtl::bool_<propagate_alloc> flag;
  757. const bool allocators_equal = this_alloc == x_alloc; (void)allocators_equal;
  758. //Resources can be transferred if both allocators are
  759. //going to be equal after this function (either propagated or already equal)
  760. if(propagate_alloc || allocators_equal){
  761. STABLE_VECTOR_CHECK_INVARIANT
  762. //Destroy objects but retain memory in case x reuses it in the future
  763. this->clear();
  764. //Move allocator if needed
  765. dtl::move_alloc(this_alloc, x_alloc, flag);
  766. //Take resources
  767. this->index.swap(x.index);
  768. this->priv_swap_members(x);
  769. }
  770. //Else do a one by one move
  771. else{
  772. this->assign( boost::make_move_iterator(x.begin())
  773. , boost::make_move_iterator(x.end()));
  774. }
  775. return *this;
  776. }
  777. #if !defined(BOOST_NO_CXX11_HDR_INITIALIZER_LIST)
  778. //! <b>Effects</b>: Make *this container contains elements from il.
  779. //!
  780. //! <b>Complexity</b>: Linear to the range [il.begin(), il.end()).
  781. stable_vector& operator=(std::initializer_list<value_type> il)
  782. {
  783. STABLE_VECTOR_CHECK_INVARIANT;
  784. assign(il.begin(), il.end());
  785. return *this;
  786. }
  787. #endif
  788. //! <b>Effects</b>: Assigns the n copies of val to *this.
  789. //!
  790. //! <b>Throws</b>: If memory allocation throws or T's copy constructor throws.
  791. //!
  792. //! <b>Complexity</b>: Linear to n.
  793. BOOST_CONTAINER_FORCEINLINE void assign(size_type n, const T& t)
  794. {
  795. typedef constant_iterator<value_type, difference_type> cvalue_iterator;
  796. this->assign(cvalue_iterator(t, n), cvalue_iterator());
  797. }
  798. //! <b>Effects</b>: Assigns the the range [first, last) to *this.
  799. //!
  800. //! <b>Throws</b>: If memory allocation throws or
  801. //! T's constructor from dereferencing InpIt throws.
  802. //!
  803. //! <b>Complexity</b>: Linear to n.
  804. template<typename InputIterator>
  805. #if !defined(BOOST_CONTAINER_DOXYGEN_INVOKED)
  806. typename dtl::disable_if_convertible<InputIterator, size_type>::type
  807. #else
  808. void
  809. #endif
  810. assign(InputIterator first,InputIterator last)
  811. {
  812. STABLE_VECTOR_CHECK_INVARIANT;
  813. iterator first1 = this->begin();
  814. iterator last1 = this->end();
  815. for ( ; first1 != last1 && first != last; ++first1, ++first)
  816. *first1 = *first;
  817. if (first == last){
  818. this->erase(first1, last1);
  819. }
  820. else{
  821. this->insert(last1, first, last);
  822. }
  823. }
  824. #if !defined(BOOST_NO_CXX11_HDR_INITIALIZER_LIST)
  825. //! <b>Effects</b>: Assigns the the range [il.begin(), il.end()) to *this.
  826. //!
  827. //! <b>Throws</b>: If memory allocation throws or
  828. //! T's constructor from dereferencing initializer_list iterator throws.
  829. //!
  830. BOOST_CONTAINER_FORCEINLINE void assign(std::initializer_list<value_type> il)
  831. {
  832. STABLE_VECTOR_CHECK_INVARIANT;
  833. assign(il.begin(), il.end());
  834. }
  835. #endif
  836. //! <b>Effects</b>: Returns a copy of the internal allocator.
  837. //!
  838. //! <b>Throws</b>: If allocator's copy constructor throws.
  839. //!
  840. //! <b>Complexity</b>: Constant.
  841. BOOST_CONTAINER_FORCEINLINE allocator_type get_allocator() const
  842. { return this->priv_node_alloc(); }
  843. //! <b>Effects</b>: Returns a reference to the internal allocator.
  844. //!
  845. //! <b>Throws</b>: Nothing
  846. //!
  847. //! <b>Complexity</b>: Constant.
  848. //!
  849. //! <b>Note</b>: Non-standard extension.
  850. BOOST_CONTAINER_FORCEINLINE const stored_allocator_type &get_stored_allocator() const BOOST_NOEXCEPT_OR_NOTHROW
  851. { return this->priv_node_alloc(); }
  852. //! <b>Effects</b>: Returns a reference to the internal allocator.
  853. //!
  854. //! <b>Throws</b>: Nothing
  855. //!
  856. //! <b>Complexity</b>: Constant.
  857. //!
  858. //! <b>Note</b>: Non-standard extension.
  859. BOOST_CONTAINER_FORCEINLINE stored_allocator_type &get_stored_allocator() BOOST_NOEXCEPT_OR_NOTHROW
  860. { return this->priv_node_alloc(); }
  861. //////////////////////////////////////////////
  862. //
  863. // iterators
  864. //
  865. //////////////////////////////////////////////
  866. //! <b>Effects</b>: Returns an iterator to the first element contained in the stable_vector.
  867. //!
  868. //! <b>Throws</b>: Nothing.
  869. //!
  870. //! <b>Complexity</b>: Constant.
  871. BOOST_CONTAINER_FORCEINLINE iterator begin() BOOST_NOEXCEPT_OR_NOTHROW
  872. { return (this->index.empty()) ? this->end(): iterator(node_ptr_traits::static_cast_from(this->index.front())); }
  873. //! <b>Effects</b>: Returns a const_iterator to the first element contained in the stable_vector.
  874. //!
  875. //! <b>Throws</b>: Nothing.
  876. //!
  877. //! <b>Complexity</b>: Constant.
  878. BOOST_CONTAINER_FORCEINLINE const_iterator begin() const BOOST_NOEXCEPT_OR_NOTHROW
  879. { return (this->index.empty()) ? this->cend() : const_iterator(node_ptr_traits::static_cast_from(this->index.front())) ; }
  880. //! <b>Effects</b>: Returns an iterator to the end of the stable_vector.
  881. //!
  882. //! <b>Throws</b>: Nothing.
  883. //!
  884. //! <b>Complexity</b>: Constant.
  885. BOOST_CONTAINER_FORCEINLINE iterator end() BOOST_NOEXCEPT_OR_NOTHROW
  886. { return iterator(this->priv_get_end_node()); }
  887. //! <b>Effects</b>: Returns a const_iterator to the end of the stable_vector.
  888. //!
  889. //! <b>Throws</b>: Nothing.
  890. //!
  891. //! <b>Complexity</b>: Constant.
  892. BOOST_CONTAINER_FORCEINLINE const_iterator end() const BOOST_NOEXCEPT_OR_NOTHROW
  893. { return const_iterator(this->priv_get_end_node()); }
  894. //! <b>Effects</b>: Returns a reverse_iterator pointing to the beginning
  895. //! of the reversed stable_vector.
  896. //!
  897. //! <b>Throws</b>: Nothing.
  898. //!
  899. //! <b>Complexity</b>: Constant.
  900. BOOST_CONTAINER_FORCEINLINE reverse_iterator rbegin() BOOST_NOEXCEPT_OR_NOTHROW
  901. { return reverse_iterator(this->end()); }
  902. //! <b>Effects</b>: Returns a const_reverse_iterator pointing to the beginning
  903. //! of the reversed stable_vector.
  904. //!
  905. //! <b>Throws</b>: Nothing.
  906. //!
  907. //! <b>Complexity</b>: Constant.
  908. BOOST_CONTAINER_FORCEINLINE const_reverse_iterator rbegin() const BOOST_NOEXCEPT_OR_NOTHROW
  909. { return const_reverse_iterator(this->end()); }
  910. //! <b>Effects</b>: Returns a reverse_iterator pointing to the end
  911. //! of the reversed stable_vector.
  912. //!
  913. //! <b>Throws</b>: Nothing.
  914. //!
  915. //! <b>Complexity</b>: Constant.
  916. BOOST_CONTAINER_FORCEINLINE reverse_iterator rend() BOOST_NOEXCEPT_OR_NOTHROW
  917. { return reverse_iterator(this->begin()); }
  918. //! <b>Effects</b>: Returns a const_reverse_iterator pointing to the end
  919. //! of the reversed stable_vector.
  920. //!
  921. //! <b>Throws</b>: Nothing.
  922. //!
  923. //! <b>Complexity</b>: Constant.
  924. BOOST_CONTAINER_FORCEINLINE const_reverse_iterator rend() const BOOST_NOEXCEPT_OR_NOTHROW
  925. { return const_reverse_iterator(this->begin()); }
  926. //! <b>Effects</b>: Returns a const_iterator to the first element contained in the stable_vector.
  927. //!
  928. //! <b>Throws</b>: Nothing.
  929. //!
  930. //! <b>Complexity</b>: Constant.
  931. BOOST_CONTAINER_FORCEINLINE const_iterator cbegin() const BOOST_NOEXCEPT_OR_NOTHROW
  932. { return this->begin(); }
  933. //! <b>Effects</b>: Returns a const_iterator to the end of the stable_vector.
  934. //!
  935. //! <b>Throws</b>: Nothing.
  936. //!
  937. //! <b>Complexity</b>: Constant.
  938. BOOST_CONTAINER_FORCEINLINE const_iterator cend() const BOOST_NOEXCEPT_OR_NOTHROW
  939. { return this->end(); }
  940. //! <b>Effects</b>: Returns a const_reverse_iterator pointing to the beginning
  941. //! of the reversed stable_vector.
  942. //!
  943. //! <b>Throws</b>: Nothing.
  944. //!
  945. //! <b>Complexity</b>: Constant.
  946. BOOST_CONTAINER_FORCEINLINE const_reverse_iterator crbegin() const BOOST_NOEXCEPT_OR_NOTHROW
  947. { return this->rbegin(); }
  948. //! <b>Effects</b>: Returns a const_reverse_iterator pointing to the end
  949. //! of the reversed stable_vector.
  950. //!
  951. //! <b>Throws</b>: Nothing.
  952. //!
  953. //! <b>Complexity</b>: Constant.
  954. BOOST_CONTAINER_FORCEINLINE const_reverse_iterator crend()const BOOST_NOEXCEPT_OR_NOTHROW
  955. { return this->rend(); }
  956. //////////////////////////////////////////////
  957. //
  958. // capacity
  959. //
  960. //////////////////////////////////////////////
  961. //! <b>Effects</b>: Returns true if the stable_vector contains no elements.
  962. //!
  963. //! <b>Throws</b>: Nothing.
  964. //!
  965. //! <b>Complexity</b>: Constant.
  966. BOOST_CONTAINER_FORCEINLINE bool empty() const BOOST_NOEXCEPT_OR_NOTHROW
  967. { return this->index.size() <= ExtraPointers; }
  968. //! <b>Effects</b>: Returns the number of the elements contained in the stable_vector.
  969. //!
  970. //! <b>Throws</b>: Nothing.
  971. //!
  972. //! <b>Complexity</b>: Constant.
  973. BOOST_CONTAINER_FORCEINLINE size_type size() const BOOST_NOEXCEPT_OR_NOTHROW
  974. {
  975. const size_type index_size = this->index.size();
  976. return (index_size - ExtraPointers) & (size_type(0u) -size_type(index_size != 0));
  977. }
  978. //! <b>Effects</b>: Returns the largest possible size of the stable_vector.
  979. //!
  980. //! <b>Throws</b>: Nothing.
  981. //!
  982. //! <b>Complexity</b>: Constant.
  983. BOOST_CONTAINER_FORCEINLINE size_type max_size() const BOOST_NOEXCEPT_OR_NOTHROW
  984. { return this->index.max_size() - ExtraPointers; }
  985. //! <b>Effects</b>: Inserts or erases elements at the end such that
  986. //! the size becomes n. New elements are value initialized.
  987. //!
  988. //! <b>Throws</b>: If memory allocation throws, or T's value initialization throws.
  989. //!
  990. //! <b>Complexity</b>: Linear to the difference between size() and new_size.
  991. void resize(size_type n)
  992. {
  993. typedef value_init_construct_iterator<value_type, difference_type> value_init_iterator;
  994. STABLE_VECTOR_CHECK_INVARIANT;
  995. if(n > this->size())
  996. this->insert(this->cend(), value_init_iterator(n - this->size()), value_init_iterator());
  997. else if(n < this->size())
  998. this->erase(this->cbegin() + n, this->cend());
  999. }
  1000. //! <b>Effects</b>: Inserts or erases elements at the end such that
  1001. //! the size becomes n. New elements are default initialized.
  1002. //!
  1003. //! <b>Throws</b>: If memory allocation throws, or T's default initialization throws.
  1004. //!
  1005. //! <b>Complexity</b>: Linear to the difference between size() and new_size.
  1006. //!
  1007. //! <b>Note</b>: Non-standard extension
  1008. void resize(size_type n, default_init_t)
  1009. {
  1010. typedef default_init_construct_iterator<value_type, difference_type> default_init_iterator;
  1011. STABLE_VECTOR_CHECK_INVARIANT;
  1012. if(n > this->size())
  1013. this->insert(this->cend(), default_init_iterator(n - this->size()), default_init_iterator());
  1014. else if(n < this->size())
  1015. this->erase(this->cbegin() + n, this->cend());
  1016. }
  1017. //! <b>Effects</b>: Inserts or erases elements at the end such that
  1018. //! the size becomes n. New elements are copy constructed from x.
  1019. //!
  1020. //! <b>Throws</b>: If memory allocation throws, or T's copy constructor throws.
  1021. //!
  1022. //! <b>Complexity</b>: Linear to the difference between size() and new_size.
  1023. void resize(size_type n, const T& t)
  1024. {
  1025. STABLE_VECTOR_CHECK_INVARIANT;
  1026. if(n > this->size())
  1027. this->insert(this->cend(), n - this->size(), t);
  1028. else if(n < this->size())
  1029. this->erase(this->cbegin() + n, this->cend());
  1030. }
  1031. //! <b>Effects</b>: Number of elements for which memory has been allocated.
  1032. //! capacity() is always greater than or equal to size().
  1033. //!
  1034. //! <b>Throws</b>: Nothing.
  1035. //!
  1036. //! <b>Complexity</b>: Constant.
  1037. size_type capacity() const BOOST_NOEXCEPT_OR_NOTHROW
  1038. {
  1039. const size_type index_size = this->index.size();
  1040. BOOST_ASSERT(!index_size || index_size >= ExtraPointers);
  1041. const size_type node_extra_capacity = this->internal_data.pool_size;
  1042. //Pool count must be less than index capacity, as index is a vector
  1043. BOOST_ASSERT(node_extra_capacity <= (this->index.capacity()- index_size));
  1044. const size_type index_offset =
  1045. (node_extra_capacity - ExtraPointers) & (size_type(0u) - size_type(index_size != 0));
  1046. return index_size + index_offset;
  1047. }
  1048. //! <b>Effects</b>: If n is less than or equal to capacity(), this call has no
  1049. //! effect. Otherwise, it is a request for allocation of additional memory.
  1050. //! If the request is successful, then capacity() is greater than or equal to
  1051. //! n; otherwise, capacity() is unchanged. In either case, size() is unchanged.
  1052. //!
  1053. //! <b>Throws</b>: If memory allocation allocation throws.
  1054. void reserve(size_type n)
  1055. {
  1056. STABLE_VECTOR_CHECK_INVARIANT;
  1057. if(n > this->max_size()){
  1058. throw_length_error("stable_vector::reserve max_size() exceeded");
  1059. }
  1060. size_type sz = this->size();
  1061. size_type old_capacity = this->capacity();
  1062. if(n > old_capacity){
  1063. index_traits_type::initialize_end_node(this->index, this->internal_data.end_node, n);
  1064. const void * old_ptr = &index[0];
  1065. this->index.reserve(n + ExtraPointers);
  1066. bool realloced = &index[0] != old_ptr;
  1067. //Fix the pointers for the newly allocated buffer
  1068. if(realloced){
  1069. index_traits_type::fix_up_pointers_from(this->index, this->index.begin());
  1070. }
  1071. //Now fill pool if data is not enough
  1072. if((n - sz) > this->internal_data.pool_size){
  1073. this->priv_increase_pool((n - sz) - this->internal_data.pool_size);
  1074. }
  1075. }
  1076. }
  1077. //! <b>Effects</b>: Tries to deallocate the excess of memory created
  1078. //! with previous allocations. The size of the stable_vector is unchanged
  1079. //!
  1080. //! <b>Throws</b>: If memory allocation throws.
  1081. //!
  1082. //! <b>Complexity</b>: Linear to size().
  1083. void shrink_to_fit()
  1084. {
  1085. if(this->capacity()){
  1086. //First empty allocated node pool
  1087. this->priv_clear_pool();
  1088. //If empty completely destroy the index, let's recover default-constructed state
  1089. if(this->empty()){
  1090. this->index.clear();
  1091. this->index.shrink_to_fit();
  1092. this->internal_data.end_node.up = node_base_ptr_ptr();
  1093. }
  1094. //Otherwise, try to shrink-to-fit the index and readjust pointers if necessary
  1095. else{
  1096. const void* old_ptr = &index[0];
  1097. this->index.shrink_to_fit();
  1098. bool realloced = &index[0] != old_ptr;
  1099. //Fix the pointers for the newly allocated buffer
  1100. if(realloced){
  1101. index_traits_type::fix_up_pointers_from(this->index, this->index.begin());
  1102. }
  1103. }
  1104. }
  1105. }
  1106. //////////////////////////////////////////////
  1107. //
  1108. // element access
  1109. //
  1110. //////////////////////////////////////////////
  1111. //! <b>Requires</b>: !empty()
  1112. //!
  1113. //! <b>Effects</b>: Returns a reference to the first
  1114. //! element of the container.
  1115. //!
  1116. //! <b>Throws</b>: Nothing.
  1117. //!
  1118. //! <b>Complexity</b>: Constant.
  1119. BOOST_CONTAINER_FORCEINLINE reference front() BOOST_NOEXCEPT_OR_NOTHROW
  1120. {
  1121. BOOST_ASSERT(!this->empty());
  1122. return static_cast<node_reference>(*this->index.front()).get_data();
  1123. }
  1124. //! <b>Requires</b>: !empty()
  1125. //!
  1126. //! <b>Effects</b>: Returns a const reference to the first
  1127. //! element of the container.
  1128. //!
  1129. //! <b>Throws</b>: Nothing.
  1130. //!
  1131. //! <b>Complexity</b>: Constant.
  1132. BOOST_CONTAINER_FORCEINLINE const_reference front() const BOOST_NOEXCEPT_OR_NOTHROW
  1133. {
  1134. BOOST_ASSERT(!this->empty());
  1135. return static_cast<const_node_reference>(*this->index.front()).get_data();
  1136. }
  1137. //! <b>Requires</b>: !empty()
  1138. //!
  1139. //! <b>Effects</b>: Returns a reference to the last
  1140. //! element of the container.
  1141. //!
  1142. //! <b>Throws</b>: Nothing.
  1143. //!
  1144. //! <b>Complexity</b>: Constant.
  1145. BOOST_CONTAINER_FORCEINLINE reference back() BOOST_NOEXCEPT_OR_NOTHROW
  1146. {
  1147. BOOST_ASSERT(!this->empty());
  1148. return static_cast<node_reference>(*this->index[this->size()-1u]).get_data();
  1149. }
  1150. //! <b>Requires</b>: !empty()
  1151. //!
  1152. //! <b>Effects</b>: Returns a const reference to the last
  1153. //! element of the container.
  1154. //!
  1155. //! <b>Throws</b>: Nothing.
  1156. //!
  1157. //! <b>Complexity</b>: Constant.
  1158. const_reference back() const BOOST_NOEXCEPT_OR_NOTHROW
  1159. {
  1160. BOOST_ASSERT(!this->empty());
  1161. return static_cast<const_node_reference>(*this->index[this->size()-1u]).get_data();
  1162. }
  1163. //! <b>Requires</b>: size() > n.
  1164. //!
  1165. //! <b>Effects</b>: Returns a reference to the nth element
  1166. //! from the beginning of the container.
  1167. //!
  1168. //! <b>Throws</b>: Nothing.
  1169. //!
  1170. //! <b>Complexity</b>: Constant.
  1171. BOOST_CONTAINER_FORCEINLINE reference operator[](size_type n) BOOST_NOEXCEPT_OR_NOTHROW
  1172. {
  1173. BOOST_ASSERT(this->size() > n);
  1174. return static_cast<node_reference>(*this->index[n]).get_data();
  1175. }
  1176. //! <b>Requires</b>: size() > n.
  1177. //!
  1178. //! <b>Effects</b>: Returns a const reference to the nth element
  1179. //! from the beginning of the container.
  1180. //!
  1181. //! <b>Throws</b>: Nothing.
  1182. //!
  1183. //! <b>Complexity</b>: Constant.
  1184. BOOST_CONTAINER_FORCEINLINE const_reference operator[](size_type n) const BOOST_NOEXCEPT_OR_NOTHROW
  1185. {
  1186. BOOST_ASSERT(this->size() > n);
  1187. return static_cast<const_node_reference>(*this->index[n]).get_data();
  1188. }
  1189. //! <b>Requires</b>: size() >= n.
  1190. //!
  1191. //! <b>Effects</b>: Returns an iterator to the nth element
  1192. //! from the beginning of the container. Returns end()
  1193. //! if n == size().
  1194. //!
  1195. //! <b>Throws</b>: Nothing.
  1196. //!
  1197. //! <b>Complexity</b>: Constant.
  1198. //!
  1199. //! <b>Note</b>: Non-standard extension
  1200. BOOST_CONTAINER_FORCEINLINE iterator nth(size_type n) BOOST_NOEXCEPT_OR_NOTHROW
  1201. {
  1202. BOOST_ASSERT(this->size() >= n);
  1203. return (this->index.empty()) ? this->end() : iterator(node_ptr_traits::static_cast_from(this->index[n]));
  1204. }
  1205. //! <b>Requires</b>: size() >= n.
  1206. //!
  1207. //! <b>Effects</b>: Returns a const_iterator to the nth element
  1208. //! from the beginning of the container. Returns end()
  1209. //! if n == size().
  1210. //!
  1211. //! <b>Throws</b>: Nothing.
  1212. //!
  1213. //! <b>Complexity</b>: Constant.
  1214. //!
  1215. //! <b>Note</b>: Non-standard extension
  1216. BOOST_CONTAINER_FORCEINLINE const_iterator nth(size_type n) const BOOST_NOEXCEPT_OR_NOTHROW
  1217. {
  1218. BOOST_ASSERT(this->size() >= n);
  1219. return (this->index.empty()) ? this->cend() : iterator(node_ptr_traits::static_cast_from(this->index[n]));
  1220. }
  1221. //! <b>Requires</b>: begin() <= p <= end().
  1222. //!
  1223. //! <b>Effects</b>: Returns the index of the element pointed by p
  1224. //! and size() if p == end().
  1225. //!
  1226. //! <b>Throws</b>: Nothing.
  1227. //!
  1228. //! <b>Complexity</b>: Constant.
  1229. //!
  1230. //! <b>Note</b>: Non-standard extension
  1231. BOOST_CONTAINER_FORCEINLINE size_type index_of(iterator p) BOOST_NOEXCEPT_OR_NOTHROW
  1232. { return this->priv_index_of(p.node_pointer()); }
  1233. //! <b>Requires</b>: begin() <= p <= end().
  1234. //!
  1235. //! <b>Effects</b>: Returns the index of the element pointed by p
  1236. //! and size() if p == end().
  1237. //!
  1238. //! <b>Throws</b>: Nothing.
  1239. //!
  1240. //! <b>Complexity</b>: Constant.
  1241. //!
  1242. //! <b>Note</b>: Non-standard extension
  1243. BOOST_CONTAINER_FORCEINLINE size_type index_of(const_iterator p) const BOOST_NOEXCEPT_OR_NOTHROW
  1244. { return this->priv_index_of(p.node_pointer()); }
  1245. //! <b>Requires</b>: size() > n.
  1246. //!
  1247. //! <b>Effects</b>: Returns a reference to the nth element
  1248. //! from the beginning of the container.
  1249. //!
  1250. //! <b>Throws</b>: std::range_error if n >= size()
  1251. //!
  1252. //! <b>Complexity</b>: Constant.
  1253. reference at(size_type n)
  1254. {
  1255. if(n >= this->size()){
  1256. throw_out_of_range("vector::at invalid subscript");
  1257. }
  1258. return operator[](n);
  1259. }
  1260. //! <b>Requires</b>: size() > n.
  1261. //!
  1262. //! <b>Effects</b>: Returns a const reference to the nth element
  1263. //! from the beginning of the container.
  1264. //!
  1265. //! <b>Throws</b>: std::range_error if n >= size()
  1266. //!
  1267. //! <b>Complexity</b>: Constant.
  1268. const_reference at(size_type n)const
  1269. {
  1270. if(n >= this->size()){
  1271. throw_out_of_range("vector::at invalid subscript");
  1272. }
  1273. return operator[](n);
  1274. }
  1275. //////////////////////////////////////////////
  1276. //
  1277. // modifiers
  1278. //
  1279. //////////////////////////////////////////////
  1280. #if !defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES) || defined(BOOST_CONTAINER_DOXYGEN_INVOKED)
  1281. //! <b>Effects</b>: Inserts an object of type T constructed with
  1282. //! std::forward<Args>(args)... in the end of the stable_vector.
  1283. //!
  1284. //! <b>Returns</b>: A reference to the created object.
  1285. //!
  1286. //! <b>Throws</b>: If memory allocation throws or the in-place constructor throws.
  1287. //!
  1288. //! <b>Complexity</b>: Amortized constant time.
  1289. template<class ...Args>
  1290. reference emplace_back(Args &&...args)
  1291. {
  1292. typedef emplace_functor<Args...> EmplaceFunctor;
  1293. typedef emplace_iterator<value_type, EmplaceFunctor, difference_type> EmplaceIterator;
  1294. EmplaceFunctor &&ef = EmplaceFunctor(boost::forward<Args>(args)...);
  1295. return *this->insert(this->cend(), EmplaceIterator(ef), EmplaceIterator());
  1296. }
  1297. //! <b>Requires</b>: p must be a valid iterator of *this.
  1298. //!
  1299. //! <b>Effects</b>: Inserts an object of type T constructed with
  1300. //! std::forward<Args>(args)... before p
  1301. //!
  1302. //! <b>Throws</b>: If memory allocation throws or the in-place constructor throws.
  1303. //!
  1304. //! <b>Complexity</b>: If p is end(), amortized constant time
  1305. //! Linear time otherwise.
  1306. template<class ...Args>
  1307. iterator emplace(const_iterator p, Args && ...args)
  1308. {
  1309. BOOST_ASSERT(this->priv_in_range_or_end(p));
  1310. size_type pos_n = p - cbegin();
  1311. typedef emplace_functor<Args...> EmplaceFunctor;
  1312. typedef emplace_iterator<value_type, EmplaceFunctor, difference_type> EmplaceIterator;
  1313. EmplaceFunctor &&ef = EmplaceFunctor(boost::forward<Args>(args)...);
  1314. this->insert(p, EmplaceIterator(ef), EmplaceIterator());
  1315. return iterator(this->begin() + pos_n);
  1316. }
  1317. #else
  1318. #define BOOST_CONTAINER_STABLE_VECTOR_EMPLACE_CODE(N) \
  1319. BOOST_MOVE_TMPL_LT##N BOOST_MOVE_CLASS##N BOOST_MOVE_GT##N \
  1320. reference emplace_back(BOOST_MOVE_UREF##N)\
  1321. {\
  1322. typedef emplace_functor##N\
  1323. BOOST_MOVE_LT##N BOOST_MOVE_TARG##N BOOST_MOVE_GT##N EmplaceFunctor;\
  1324. typedef emplace_iterator<value_type, EmplaceFunctor, difference_type> EmplaceIterator;\
  1325. EmplaceFunctor ef BOOST_MOVE_LP##N BOOST_MOVE_FWD##N BOOST_MOVE_RP##N;\
  1326. return *this->insert(this->cend() , EmplaceIterator(ef), EmplaceIterator());\
  1327. }\
  1328. \
  1329. BOOST_MOVE_TMPL_LT##N BOOST_MOVE_CLASS##N BOOST_MOVE_GT##N \
  1330. iterator emplace(const_iterator p BOOST_MOVE_I##N BOOST_MOVE_UREF##N)\
  1331. {\
  1332. BOOST_ASSERT(this->priv_in_range_or_end(p));\
  1333. typedef emplace_functor##N\
  1334. BOOST_MOVE_LT##N BOOST_MOVE_TARG##N BOOST_MOVE_GT##N EmplaceFunctor;\
  1335. typedef emplace_iterator<value_type, EmplaceFunctor, difference_type> EmplaceIterator;\
  1336. EmplaceFunctor ef BOOST_MOVE_LP##N BOOST_MOVE_FWD##N BOOST_MOVE_RP##N;\
  1337. const size_type pos_n = p - this->cbegin();\
  1338. this->insert(p, EmplaceIterator(ef), EmplaceIterator());\
  1339. return this->begin() += pos_n;\
  1340. }\
  1341. //
  1342. BOOST_MOVE_ITERATE_0TO9(BOOST_CONTAINER_STABLE_VECTOR_EMPLACE_CODE)
  1343. #undef BOOST_CONTAINER_STABLE_VECTOR_EMPLACE_CODE
  1344. #endif // !defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES)
  1345. #if defined(BOOST_CONTAINER_DOXYGEN_INVOKED)
  1346. //! <b>Effects</b>: Inserts a copy of x at the end of the stable_vector.
  1347. //!
  1348. //! <b>Throws</b>: If memory allocation throws or
  1349. //! T's copy constructor throws.
  1350. //!
  1351. //! <b>Complexity</b>: Amortized constant time.
  1352. void push_back(const T &x);
  1353. //! <b>Effects</b>: Constructs a new element in the end of the stable_vector
  1354. //! and moves the resources of x to this new element.
  1355. //!
  1356. //! <b>Throws</b>: If memory allocation throws.
  1357. //!
  1358. //! <b>Complexity</b>: Amortized constant time.
  1359. void push_back(T &&x);
  1360. #else
  1361. BOOST_MOVE_CONVERSION_AWARE_CATCH(push_back, T, void, priv_push_back)
  1362. #endif
  1363. #if defined(BOOST_CONTAINER_DOXYGEN_INVOKED)
  1364. //! <b>Requires</b>: p must be a valid iterator of *this.
  1365. //!
  1366. //! <b>Effects</b>: Insert a copy of x before p.
  1367. //!
  1368. //! <b>Returns</b>: An iterator to the inserted element.
  1369. //!
  1370. //! <b>Throws</b>: If memory allocation throws or x's copy constructor throws.
  1371. //!
  1372. //! <b>Complexity</b>: If p is end(), amortized constant time
  1373. //! Linear time otherwise.
  1374. iterator insert(const_iterator p, const T &x);
  1375. //! <b>Requires</b>: p must be a valid iterator of *this.
  1376. //!
  1377. //! <b>Effects</b>: Insert a new element before p with x's resources.
  1378. //!
  1379. //! <b>Returns</b>: an iterator to the inserted element.
  1380. //!
  1381. //! <b>Throws</b>: If memory allocation throws.
  1382. //!
  1383. //! <b>Complexity</b>: If p is end(), amortized constant time
  1384. //! Linear time otherwise.
  1385. iterator insert(const_iterator p, T &&x);
  1386. #else
  1387. BOOST_MOVE_CONVERSION_AWARE_CATCH_1ARG(insert, T, iterator, priv_insert, const_iterator, const_iterator)
  1388. #endif
  1389. //! <b>Requires</b>: p must be a valid iterator of *this.
  1390. //!
  1391. //! <b>Effects</b>: Insert n copies of x before p.
  1392. //!
  1393. //! <b>Returns</b>: an iterator to the first inserted element or p if n is 0.
  1394. //!
  1395. //! <b>Throws</b>: If memory allocation throws or T's copy constructor throws.
  1396. //!
  1397. //! <b>Complexity</b>: Linear to n.
  1398. iterator insert(const_iterator p, size_type n, const T& t)
  1399. {
  1400. BOOST_ASSERT(this->priv_in_range_or_end(p));
  1401. STABLE_VECTOR_CHECK_INVARIANT;
  1402. typedef constant_iterator<value_type, difference_type> cvalue_iterator;
  1403. return this->insert(p, cvalue_iterator(t, n), cvalue_iterator());
  1404. }
  1405. //! <b>Requires</b>: p must be a valid iterator of *this.
  1406. #if !defined(BOOST_NO_CXX11_HDR_INITIALIZER_LIST)
  1407. //! <b>Requires</b>: p must be a valid iterator of *this.
  1408. //!
  1409. //! <b>Effects</b>: Insert a copy of the [il.begin(), il.end()) range before p.
  1410. //!
  1411. //! <b>Returns</b>: an iterator to the first inserted element or p if first == last.
  1412. //!
  1413. //! <b>Complexity</b>: Linear to distance [il.begin(), il.end()).
  1414. BOOST_CONTAINER_FORCEINLINE iterator insert(const_iterator p, std::initializer_list<value_type> il)
  1415. {
  1416. //Position checks done by insert()
  1417. STABLE_VECTOR_CHECK_INVARIANT;
  1418. return insert(p, il.begin(), il.end());
  1419. }
  1420. #endif
  1421. //! <b>Requires</b>: pos must be a valid iterator of *this.
  1422. //!
  1423. //! <b>Effects</b>: Insert a copy of the [first, last) range before p.
  1424. //!
  1425. //! <b>Returns</b>: an iterator to the first inserted element or p if first == last.
  1426. //!
  1427. //! <b>Throws</b>: If memory allocation throws, T's constructor from a
  1428. //! dereferenced InpIt throws or T's copy constructor throws.
  1429. //!
  1430. //! <b>Complexity</b>: Linear to distance [first, last).
  1431. template <class InputIterator>
  1432. iterator insert(const_iterator p, InputIterator first, InputIterator last
  1433. #if !defined(BOOST_CONTAINER_DOXYGEN_INVOKED)
  1434. //Put this as argument instead of the return type as old GCC's like 3.4
  1435. //detect this and the next disable_if_or as overloads
  1436. , typename dtl::disable_if_or
  1437. < void
  1438. , dtl::is_convertible<InputIterator, size_type>
  1439. , dtl::is_not_input_iterator<InputIterator>
  1440. >::type* = 0
  1441. #endif
  1442. )
  1443. {
  1444. BOOST_ASSERT(this->priv_in_range_or_end(p));
  1445. STABLE_VECTOR_CHECK_INVARIANT;
  1446. const size_type pos_n = p - this->cbegin();
  1447. for(; first != last; ++first){
  1448. this->emplace(p, *first);
  1449. }
  1450. return this->begin() + pos_n;
  1451. }
  1452. #if !defined(BOOST_CONTAINER_DOXYGEN_INVOKED)
  1453. template <class FwdIt>
  1454. typename dtl::disable_if_or
  1455. < iterator
  1456. , dtl::is_convertible<FwdIt, size_type>
  1457. , dtl::is_input_iterator<FwdIt>
  1458. >::type
  1459. insert(const_iterator p, FwdIt first, FwdIt last)
  1460. {
  1461. BOOST_ASSERT(this->priv_in_range_or_end(p));
  1462. const size_type num_new = static_cast<size_type>(boost::container::iterator_distance(first, last));
  1463. const size_type idx = static_cast<size_type>(p - this->cbegin());
  1464. if(num_new){
  1465. //Fills the node pool and inserts num_new null pointers in idx.
  1466. //If a new buffer was needed fixes up pointers up to idx so
  1467. //past-new nodes are not aligned until the end of this function
  1468. //or in a rollback in case of exception
  1469. index_iterator it_past_newly_constructed(this->priv_insert_forward_non_templated(idx, num_new));
  1470. const index_iterator it_past_new(it_past_newly_constructed + num_new);
  1471. {
  1472. //Prepare rollback
  1473. insert_rollback rollback(*this, it_past_newly_constructed, it_past_new);
  1474. while(first != last){
  1475. const node_ptr n = this->priv_get_from_pool();
  1476. BOOST_ASSERT(!!n);
  1477. //Put it in the index so rollback can return it in pool if construct_in_place throws
  1478. *it_past_newly_constructed = n;
  1479. //Constructs and fixes up pointers This can throw
  1480. this->priv_build_node_from_it(n, it_past_newly_constructed, first);
  1481. ++first;
  1482. ++it_past_newly_constructed;
  1483. }
  1484. //rollback.~insert_rollback() called in case of exception
  1485. }
  1486. //Fix up pointers for past-new nodes (new nodes were fixed during construction) and
  1487. //nodes before insertion p in priv_insert_forward_non_templated(...)
  1488. index_traits_type::fix_up_pointers_from(this->index, it_past_newly_constructed);
  1489. }
  1490. return this->begin() + idx;
  1491. }
  1492. #endif
  1493. //! <b>Effects</b>: Removes the last element from the stable_vector.
  1494. //!
  1495. //! <b>Throws</b>: Nothing.
  1496. //!
  1497. //! <b>Complexity</b>: Constant time.
  1498. BOOST_CONTAINER_FORCEINLINE void pop_back() BOOST_NOEXCEPT_OR_NOTHROW
  1499. {
  1500. BOOST_ASSERT(!this->empty());
  1501. this->erase(--this->cend());
  1502. }
  1503. //! <b>Effects</b>: Erases the element at p.
  1504. //!
  1505. //! <b>Throws</b>: Nothing.
  1506. //!
  1507. //! <b>Complexity</b>: Linear to the elements between p and the
  1508. //! last element. Constant if p is the last element.
  1509. BOOST_CONTAINER_FORCEINLINE iterator erase(const_iterator p) BOOST_NOEXCEPT_OR_NOTHROW
  1510. {
  1511. BOOST_ASSERT(this->priv_in_range(p));
  1512. STABLE_VECTOR_CHECK_INVARIANT;
  1513. const size_type d = p - this->cbegin();
  1514. index_iterator it = this->index.begin() + d;
  1515. this->priv_delete_node(p.node_pointer());
  1516. it = this->index.erase(it);
  1517. index_traits_type::fix_up_pointers_from(this->index, it);
  1518. return iterator(node_ptr_traits::static_cast_from(*it));
  1519. }
  1520. //! <b>Effects</b>: Erases the elements pointed by [first, last).
  1521. //!
  1522. //! <b>Throws</b>: Nothing.
  1523. //!
  1524. //! <b>Complexity</b>: Linear to the distance between first and last
  1525. //! plus linear to the elements between p and the last element.
  1526. iterator erase(const_iterator first, const_iterator last) BOOST_NOEXCEPT_OR_NOTHROW
  1527. {
  1528. BOOST_ASSERT(first == last ||
  1529. (first < last && this->priv_in_range(first) && this->priv_in_range_or_end(last)));
  1530. STABLE_VECTOR_CHECK_INVARIANT;
  1531. const const_iterator cbeg(this->cbegin());
  1532. const size_type d1 = static_cast<size_type>(first - cbeg),
  1533. d2 = static_cast<size_type>(last - cbeg);
  1534. size_type d_dif = d2 - d1;
  1535. if(d_dif){
  1536. multiallocation_chain holder;
  1537. const index_iterator it1(this->index.begin() + d1);
  1538. const index_iterator it2(it1 + d_dif);
  1539. index_iterator it(it1);
  1540. while(d_dif--){
  1541. node_base_ptr &nb = *it;
  1542. ++it;
  1543. node_type &n = *node_ptr_traits::static_cast_from(nb);
  1544. this->priv_destroy_node(n);
  1545. holder.push_back(node_ptr_traits::pointer_to(n));
  1546. }
  1547. this->priv_put_in_pool(holder);
  1548. const index_iterator e = this->index.erase(it1, it2);
  1549. index_traits_type::fix_up_pointers_from(this->index, e);
  1550. }
  1551. return iterator(last.node_pointer());
  1552. }
  1553. //! <b>Effects</b>: Swaps the contents of *this and x.
  1554. //!
  1555. //! <b>Throws</b>: Nothing.
  1556. //!
  1557. //! <b>Complexity</b>: Constant.
  1558. void swap(stable_vector & x)
  1559. BOOST_NOEXCEPT_IF( allocator_traits_type::propagate_on_container_swap::value
  1560. || allocator_traits_type::is_always_equal::value)
  1561. {
  1562. BOOST_ASSERT(allocator_traits_type::propagate_on_container_swap::value ||
  1563. allocator_traits_type::is_always_equal::value ||
  1564. this->get_stored_allocator() == x.get_stored_allocator());
  1565. STABLE_VECTOR_CHECK_INVARIANT;
  1566. dtl::bool_<allocator_traits_type::propagate_on_container_swap::value> flag;
  1567. dtl::swap_alloc(this->priv_node_alloc(), x.priv_node_alloc(), flag);
  1568. //vector's allocator is swapped here
  1569. this->index.swap(x.index);
  1570. this->priv_swap_members(x);
  1571. }
  1572. //! <b>Effects</b>: Erases all the elements of the stable_vector.
  1573. //!
  1574. //! <b>Throws</b>: Nothing.
  1575. //!
  1576. //! <b>Complexity</b>: Linear to the number of elements in the stable_vector.
  1577. BOOST_CONTAINER_FORCEINLINE void clear() BOOST_NOEXCEPT_OR_NOTHROW
  1578. { this->erase(this->cbegin(),this->cend()); }
  1579. //! <b>Effects</b>: Returns true if x and y are equal
  1580. //!
  1581. //! <b>Complexity</b>: Linear to the number of elements in the container.
  1582. BOOST_CONTAINER_FORCEINLINE friend bool operator==(const stable_vector& x, const stable_vector& y)
  1583. { return x.size() == y.size() && ::boost::container::algo_equal(x.begin(), x.end(), y.begin()); }
  1584. //! <b>Effects</b>: Returns true if x and y are unequal
  1585. //!
  1586. //! <b>Complexity</b>: Linear to the number of elements in the container.
  1587. BOOST_CONTAINER_FORCEINLINE friend bool operator!=(const stable_vector& x, const stable_vector& y)
  1588. { return !(x == y); }
  1589. //! <b>Effects</b>: Returns true if x is less than y
  1590. //!
  1591. //! <b>Complexity</b>: Linear to the number of elements in the container.
  1592. BOOST_CONTAINER_FORCEINLINE friend bool operator<(const stable_vector& x, const stable_vector& y)
  1593. { return ::boost::container::algo_lexicographical_compare(x.begin(), x.end(), y.begin(), y.end()); }
  1594. //! <b>Effects</b>: Returns true if x is greater than y
  1595. //!
  1596. //! <b>Complexity</b>: Linear to the number of elements in the container.
  1597. BOOST_CONTAINER_FORCEINLINE friend bool operator>(const stable_vector& x, const stable_vector& y)
  1598. { return y < x; }
  1599. //! <b>Effects</b>: Returns true if x is equal or less than y
  1600. //!
  1601. //! <b>Complexity</b>: Linear to the number of elements in the container.
  1602. BOOST_CONTAINER_FORCEINLINE friend bool operator<=(const stable_vector& x, const stable_vector& y)
  1603. { return !(y < x); }
  1604. //! <b>Effects</b>: Returns true if x is equal or greater than y
  1605. //!
  1606. //! <b>Complexity</b>: Linear to the number of elements in the container.
  1607. BOOST_CONTAINER_FORCEINLINE friend bool operator>=(const stable_vector& x, const stable_vector& y)
  1608. { return !(x < y); }
  1609. //! <b>Effects</b>: x.swap(y)
  1610. //!
  1611. //! <b>Complexity</b>: Constant.
  1612. BOOST_CONTAINER_FORCEINLINE friend void swap(stable_vector& x, stable_vector& y)
  1613. { x.swap(y); }
  1614. #ifndef BOOST_CONTAINER_DOXYGEN_INVOKED
  1615. private:
  1616. bool priv_in_range(const_iterator pos) const
  1617. {
  1618. return (this->begin() <= pos) && (pos < this->end());
  1619. }
  1620. BOOST_CONTAINER_FORCEINLINE bool priv_in_range_or_end(const_iterator pos) const
  1621. {
  1622. return (this->begin() <= pos) && (pos <= this->end());
  1623. }
  1624. BOOST_CONTAINER_FORCEINLINE size_type priv_index_of(node_ptr p) const
  1625. {
  1626. //Check range
  1627. BOOST_ASSERT(this->index.empty() || (this->index.data() <= p->up));
  1628. BOOST_ASSERT(this->index.empty() || p->up <= (this->index.data() + this->index.size()));
  1629. return this->index.empty() ? 0 : p->up - this->index.data();
  1630. }
  1631. class insert_rollback
  1632. {
  1633. public:
  1634. insert_rollback(stable_vector &sv, index_iterator &it_past_constructed, const index_iterator &it_past_new)
  1635. : m_sv(sv), m_it_past_constructed(it_past_constructed), m_it_past_new(it_past_new)
  1636. {}
  1637. ~insert_rollback()
  1638. {
  1639. if(m_it_past_constructed != m_it_past_new){
  1640. m_sv.priv_put_in_pool(node_ptr_traits::static_cast_from(*m_it_past_constructed));
  1641. index_iterator e = m_sv.index.erase(m_it_past_constructed, m_it_past_new);
  1642. index_traits_type::fix_up_pointers_from(m_sv.index, e);
  1643. }
  1644. }
  1645. private:
  1646. stable_vector &m_sv;
  1647. index_iterator &m_it_past_constructed;
  1648. const index_iterator &m_it_past_new;
  1649. };
  1650. class push_back_rollback
  1651. {
  1652. public:
  1653. BOOST_CONTAINER_FORCEINLINE push_back_rollback(stable_vector &sv, const node_ptr &p)
  1654. : m_sv(sv), m_p(p)
  1655. {}
  1656. BOOST_CONTAINER_FORCEINLINE ~push_back_rollback()
  1657. {
  1658. if(m_p){
  1659. m_sv.priv_put_in_pool(m_p);
  1660. }
  1661. }
  1662. BOOST_CONTAINER_FORCEINLINE void release()
  1663. { m_p = node_ptr(); }
  1664. private:
  1665. stable_vector &m_sv;
  1666. node_ptr m_p;
  1667. };
  1668. index_iterator priv_insert_forward_non_templated(size_type idx, size_type num_new)
  1669. {
  1670. index_traits_type::initialize_end_node(this->index, this->internal_data.end_node, num_new);
  1671. //Now try to fill the pool with new data
  1672. if(this->internal_data.pool_size < num_new){
  1673. this->priv_increase_pool(num_new - this->internal_data.pool_size);
  1674. }
  1675. //Now try to make room in the vector
  1676. const node_base_ptr_ptr old_buffer = this->index.data();
  1677. this->index.insert(this->index.begin() + idx, num_new, node_ptr());
  1678. bool new_buffer = this->index.data() != old_buffer;
  1679. //Fix the pointers for the newly allocated buffer
  1680. const index_iterator index_beg = this->index.begin();
  1681. if(new_buffer){
  1682. index_traits_type::fix_up_pointers(index_beg, index_beg + idx);
  1683. }
  1684. return index_beg + idx;
  1685. }
  1686. BOOST_CONTAINER_FORCEINLINE bool priv_capacity_bigger_than_size() const
  1687. {
  1688. return this->index.capacity() > this->index.size() &&
  1689. this->internal_data.pool_size > 0;
  1690. }
  1691. template <class U>
  1692. void priv_push_back(BOOST_MOVE_CATCH_FWD(U) x)
  1693. {
  1694. if(BOOST_LIKELY(this->priv_capacity_bigger_than_size())){
  1695. //Enough memory in the pool and in the index
  1696. const node_ptr p = this->priv_get_from_pool();
  1697. BOOST_ASSERT(!!p);
  1698. {
  1699. push_back_rollback rollback(*this, p);
  1700. //This might throw
  1701. this->priv_build_node_from_convertible(p, ::boost::forward<U>(x));
  1702. rollback.release();
  1703. }
  1704. //This can't throw as there is room for a new elements in the index
  1705. index_iterator new_index = this->index.insert(this->index.end() - ExtraPointers, p);
  1706. index_traits_type::fix_up_pointers_from(this->index, new_index);
  1707. }
  1708. else{
  1709. this->insert(this->cend(), ::boost::forward<U>(x));
  1710. }
  1711. }
  1712. iterator priv_insert(const_iterator p, const value_type &t)
  1713. {
  1714. BOOST_ASSERT(this->priv_in_range_or_end(p));
  1715. typedef constant_iterator<value_type, difference_type> cvalue_iterator;
  1716. return this->insert(p, cvalue_iterator(t, 1), cvalue_iterator());
  1717. }
  1718. iterator priv_insert(const_iterator p, BOOST_RV_REF(T) x)
  1719. {
  1720. BOOST_ASSERT(this->priv_in_range_or_end(p));
  1721. typedef repeat_iterator<T, difference_type> repeat_it;
  1722. typedef boost::move_iterator<repeat_it> repeat_move_it;
  1723. //Just call more general insert(p, size, value) and return iterator
  1724. return this->insert(p, repeat_move_it(repeat_it(x, 1)), repeat_move_it(repeat_it()));
  1725. }
  1726. void priv_clear_pool()
  1727. {
  1728. if(!this->index.empty() && this->index.back()){
  1729. node_base_ptr &pool_first_ref = *(this->index.end() - 2);
  1730. node_base_ptr &pool_last_ref = this->index.back();
  1731. multiallocation_chain holder;
  1732. holder.incorporate_after( holder.before_begin()
  1733. , node_ptr_traits::static_cast_from(pool_first_ref)
  1734. , node_ptr_traits::static_cast_from(pool_last_ref)
  1735. , internal_data.pool_size);
  1736. this->deallocate_individual(holder);
  1737. pool_first_ref = pool_last_ref = 0;
  1738. this->internal_data.pool_size = 0;
  1739. }
  1740. }
  1741. void priv_increase_pool(size_type n)
  1742. {
  1743. node_base_ptr &pool_first_ref = *(this->index.end() - 2);
  1744. node_base_ptr &pool_last_ref = this->index.back();
  1745. multiallocation_chain holder;
  1746. holder.incorporate_after( holder.before_begin()
  1747. , node_ptr_traits::static_cast_from(pool_first_ref)
  1748. , node_ptr_traits::static_cast_from(pool_last_ref)
  1749. , internal_data.pool_size);
  1750. multiallocation_chain m;
  1751. this->allocate_individual(n, m);
  1752. holder.splice_after(holder.before_begin(), m, m.before_begin(), m.last(), n);
  1753. this->internal_data.pool_size += n;
  1754. std::pair<node_ptr, node_ptr> data(holder.extract_data());
  1755. pool_first_ref = data.first;
  1756. pool_last_ref = data.second;
  1757. }
  1758. void priv_put_in_pool(const node_ptr &p)
  1759. {
  1760. node_base_ptr &pool_first_ref = *(this->index.end()-2);
  1761. node_base_ptr &pool_last_ref = this->index.back();
  1762. multiallocation_chain holder;
  1763. holder.incorporate_after( holder.before_begin()
  1764. , node_ptr_traits::static_cast_from(pool_first_ref)
  1765. , node_ptr_traits::static_cast_from(pool_last_ref)
  1766. , internal_data.pool_size);
  1767. holder.push_front(p);
  1768. ++this->internal_data.pool_size;
  1769. std::pair<node_ptr, node_ptr> ret(holder.extract_data());
  1770. pool_first_ref = ret.first;
  1771. pool_last_ref = ret.second;
  1772. }
  1773. void priv_put_in_pool(multiallocation_chain &ch)
  1774. {
  1775. node_base_ptr &pool_first_ref = *(this->index.end()-(ExtraPointers-1));
  1776. node_base_ptr &pool_last_ref = this->index.back();
  1777. ch.incorporate_after( ch.before_begin()
  1778. , node_ptr_traits::static_cast_from(pool_first_ref)
  1779. , node_ptr_traits::static_cast_from(pool_last_ref)
  1780. , internal_data.pool_size);
  1781. this->internal_data.pool_size = ch.size();
  1782. const std::pair<node_ptr, node_ptr> ret(ch.extract_data());
  1783. pool_first_ref = ret.first;
  1784. pool_last_ref = ret.second;
  1785. }
  1786. node_ptr priv_get_from_pool()
  1787. {
  1788. //Precondition: index is not empty
  1789. BOOST_ASSERT(!this->index.empty());
  1790. node_base_ptr &pool_first_ref = *(this->index.end() - (ExtraPointers-1));
  1791. node_base_ptr &pool_last_ref = this->index.back();
  1792. multiallocation_chain holder;
  1793. holder.incorporate_after( holder.before_begin()
  1794. , node_ptr_traits::static_cast_from(pool_first_ref)
  1795. , node_ptr_traits::static_cast_from(pool_last_ref)
  1796. , internal_data.pool_size);
  1797. node_ptr ret = holder.pop_front();
  1798. --this->internal_data.pool_size;
  1799. if(!internal_data.pool_size){
  1800. pool_first_ref = pool_last_ref = node_ptr();
  1801. }
  1802. else{
  1803. const std::pair<node_ptr, node_ptr> data(holder.extract_data());
  1804. pool_first_ref = data.first;
  1805. pool_last_ref = data.second;
  1806. }
  1807. return ret;
  1808. }
  1809. BOOST_CONTAINER_FORCEINLINE node_base_ptr priv_get_end_node() const
  1810. { return node_base_ptr_traits::pointer_to(const_cast<node_base_type&>(this->internal_data.end_node)); }
  1811. BOOST_CONTAINER_FORCEINLINE void priv_destroy_node(const node_type &n)
  1812. {
  1813. allocator_traits<node_allocator_type>::
  1814. destroy(this->priv_node_alloc(), &n);
  1815. }
  1816. BOOST_CONTAINER_FORCEINLINE void priv_delete_node(const node_ptr &n)
  1817. {
  1818. this->priv_destroy_node(*n);
  1819. this->priv_put_in_pool(n);
  1820. }
  1821. template<class Iterator>
  1822. void priv_build_node_from_it(const node_ptr &p, const index_iterator &up_index, const Iterator &it)
  1823. {
  1824. node_type *praw = ::new(boost::movelib::iterator_to_raw_pointer(p), boost_container_new_t())
  1825. node_type(index_traits_type::ptr_to_node_base_ptr(*up_index));
  1826. BOOST_TRY{
  1827. //This can throw
  1828. boost::container::construct_in_place
  1829. ( this->priv_node_alloc()
  1830. , praw->get_data_ptr()
  1831. , it);
  1832. }
  1833. BOOST_CATCH(...) {
  1834. praw->destroy_header();
  1835. this->priv_node_alloc().deallocate(p, 1);
  1836. BOOST_RETHROW
  1837. }
  1838. BOOST_CATCH_END
  1839. }
  1840. template<class ValueConvertible>
  1841. void priv_build_node_from_convertible(const node_ptr &p, BOOST_FWD_REF(ValueConvertible) value_convertible)
  1842. {
  1843. node_type *praw = ::new(boost::movelib::iterator_to_raw_pointer(p), boost_container_new_t()) node_type;
  1844. BOOST_TRY{
  1845. //This can throw
  1846. boost::container::allocator_traits<node_allocator_type>::construct
  1847. ( this->priv_node_alloc()
  1848. , p->get_data_ptr()
  1849. , ::boost::forward<ValueConvertible>(value_convertible));
  1850. }
  1851. BOOST_CATCH(...) {
  1852. praw->destroy_header();
  1853. this->priv_node_alloc().deallocate(p, 1);
  1854. BOOST_RETHROW
  1855. }
  1856. BOOST_CATCH_END
  1857. }
  1858. void priv_swap_members(stable_vector &x)
  1859. {
  1860. boost::adl_move_swap(this->internal_data.pool_size, x.internal_data.pool_size);
  1861. index_traits_type::readjust_end_node(this->index, this->internal_data.end_node);
  1862. index_traits_type::readjust_end_node(x.index, x.internal_data.end_node);
  1863. }
  1864. #if defined(STABLE_VECTOR_ENABLE_INVARIANT_CHECKING)
  1865. bool priv_invariant()const
  1866. {
  1867. index_type & index_ref = const_cast<index_type&>(this->index);
  1868. const size_type index_size = this->index.size();
  1869. if(!index_size)
  1870. return !this->capacity() && !this->size();
  1871. if(index_size < ExtraPointers)
  1872. return false;
  1873. const size_type bucket_extra_capacity = this->index.capacity()- index_size;
  1874. const size_type node_extra_capacity = this->internal_data.pool_size;
  1875. if(bucket_extra_capacity < node_extra_capacity){
  1876. return false;
  1877. }
  1878. if(this->priv_get_end_node() != *(index.end() - ExtraPointers)){
  1879. return false;
  1880. }
  1881. if(!index_traits_type::invariants(index_ref)){
  1882. return false;
  1883. }
  1884. size_type n = this->capacity() - this->size();
  1885. node_base_ptr &pool_first_ref = *(index_ref.end() - (ExtraPointers-1));
  1886. node_base_ptr &pool_last_ref = index_ref.back();
  1887. multiallocation_chain holder;
  1888. holder.incorporate_after( holder.before_begin()
  1889. , node_ptr_traits::static_cast_from(pool_first_ref)
  1890. , node_ptr_traits::static_cast_from(pool_last_ref)
  1891. , internal_data.pool_size);
  1892. typename multiallocation_chain::iterator beg(holder.begin()), end(holder.end());
  1893. size_type num_pool = 0;
  1894. while(beg != end){
  1895. ++num_pool;
  1896. ++beg;
  1897. }
  1898. return n >= num_pool && num_pool == internal_data.pool_size;
  1899. }
  1900. class invariant_checker
  1901. {
  1902. invariant_checker(const invariant_checker &);
  1903. invariant_checker & operator=(const invariant_checker &);
  1904. const stable_vector* p;
  1905. public:
  1906. invariant_checker(const stable_vector& v):p(&v){}
  1907. ~invariant_checker(){BOOST_ASSERT(p->priv_invariant());}
  1908. void touch(){}
  1909. };
  1910. #endif
  1911. class ebo_holder
  1912. : public node_allocator_type
  1913. {
  1914. private:
  1915. BOOST_MOVABLE_BUT_NOT_COPYABLE(ebo_holder)
  1916. public:
  1917. template<class AllocatorRLValue>
  1918. explicit ebo_holder(BOOST_FWD_REF(AllocatorRLValue) a)
  1919. : node_allocator_type(boost::forward<AllocatorRLValue>(a))
  1920. , pool_size(0)
  1921. , end_node()
  1922. {}
  1923. ebo_holder()
  1924. : node_allocator_type()
  1925. , pool_size(0)
  1926. , end_node()
  1927. {}
  1928. size_type pool_size;
  1929. node_base_type end_node;
  1930. } internal_data;
  1931. node_allocator_type &priv_node_alloc() { return internal_data; }
  1932. const node_allocator_type &priv_node_alloc() const { return internal_data; }
  1933. index_type index;
  1934. #endif //#ifndef BOOST_CONTAINER_DOXYGEN_INVOKED
  1935. };
  1936. #ifndef BOOST_CONTAINER_NO_CXX17_CTAD
  1937. template <typename InputIterator>
  1938. stable_vector(InputIterator, InputIterator) ->
  1939. stable_vector<typename iterator_traits<InputIterator>::value_type>;
  1940. template <typename InputIterator, typename Allocator>
  1941. stable_vector(InputIterator, InputIterator, Allocator const&) ->
  1942. stable_vector<typename iterator_traits<InputIterator>::value_type, Allocator>;
  1943. #endif
  1944. #ifndef BOOST_CONTAINER_DOXYGEN_INVOKED
  1945. #undef STABLE_VECTOR_CHECK_INVARIANT
  1946. } //namespace container {
  1947. //!has_trivial_destructor_after_move<> == true_type
  1948. //!specialization for optimizations
  1949. template <class T, class Allocator>
  1950. struct has_trivial_destructor_after_move<boost::container::stable_vector<T, Allocator> >
  1951. {
  1952. typedef typename ::boost::container::allocator_traits<Allocator>::pointer pointer;
  1953. static const bool value = ::boost::has_trivial_destructor_after_move<Allocator>::value &&
  1954. ::boost::has_trivial_destructor_after_move<pointer>::value;
  1955. };
  1956. namespace container {
  1957. #endif //#ifndef BOOST_CONTAINER_DOXYGEN_INVOKED
  1958. }} //namespace boost{ namespace container {
  1959. #include <boost/container/detail/config_end.hpp>
  1960. #endif //BOOST_CONTAINER_STABLE_VECTOR_HPP