vector.hpp 137 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429
  1. //////////////////////////////////////////////////////////////////////////////
  2. //
  3. // (C) Copyright Ion Gaztanaga 2005-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. #ifndef BOOST_CONTAINER_CONTAINER_VECTOR_HPP
  11. #define BOOST_CONTAINER_CONTAINER_VECTOR_HPP
  12. #ifndef BOOST_CONFIG_HPP
  13. # include <boost/config.hpp>
  14. #endif
  15. #if defined(BOOST_HAS_PRAGMA_ONCE)
  16. # pragma once
  17. #endif
  18. #include <boost/container/detail/config_begin.hpp>
  19. #include <boost/container/detail/workaround.hpp>
  20. // container
  21. #include <boost/container/container_fwd.hpp>
  22. #include <boost/container/allocator_traits.hpp>
  23. #include <boost/container/new_allocator.hpp> //new_allocator
  24. #include <boost/container/throw_exception.hpp>
  25. #include <boost/container/options.hpp>
  26. // container detail
  27. #include <boost/container/detail/advanced_insert_int.hpp>
  28. #include <boost/container/detail/algorithm.hpp> //equal()
  29. #include <boost/container/detail/alloc_helpers.hpp>
  30. #include <boost/container/detail/allocation_type.hpp>
  31. #include <boost/container/detail/copy_move_algo.hpp>
  32. #include <boost/container/detail/destroyers.hpp>
  33. #include <boost/container/detail/iterator.hpp>
  34. #include <boost/container/detail/iterators.hpp>
  35. #include <boost/move/detail/iterator_to_raw_pointer.hpp>
  36. #include <boost/container/detail/mpl.hpp>
  37. #include <boost/container/detail/next_capacity.hpp>
  38. #include <boost/container/detail/value_functors.hpp>
  39. #include <boost/move/detail/to_raw_pointer.hpp>
  40. #include <boost/container/detail/type_traits.hpp>
  41. #include <boost/container/detail/version_type.hpp>
  42. // intrusive
  43. #include <boost/intrusive/pointer_traits.hpp>
  44. // move
  45. #include <boost/move/adl_move_swap.hpp>
  46. #include <boost/move/iterator.hpp>
  47. #include <boost/move/traits.hpp>
  48. #include <boost/move/utility_core.hpp>
  49. // move/detail
  50. #if defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES)
  51. #include <boost/move/detail/fwd_macros.hpp>
  52. #endif
  53. #include <boost/move/detail/move_helpers.hpp>
  54. // move/algo
  55. #include <boost/move/algo/adaptive_merge.hpp>
  56. #include <boost/move/algo/unique.hpp>
  57. #include <boost/move/algo/predicate.hpp>
  58. #include <boost/move/algo/detail/set_difference.hpp>
  59. // other
  60. #include <boost/core/no_exceptions_support.hpp>
  61. #include <boost/assert.hpp>
  62. #include <boost/cstdint.hpp>
  63. //std
  64. #if !defined(BOOST_NO_CXX11_HDR_INITIALIZER_LIST)
  65. #include <initializer_list> //for std::initializer_list
  66. #endif
  67. namespace boost {
  68. namespace container {
  69. #ifndef BOOST_CONTAINER_DOXYGEN_INVOKED
  70. template <class Pointer, bool IsConst>
  71. class vec_iterator
  72. {
  73. public:
  74. typedef std::random_access_iterator_tag iterator_category;
  75. typedef typename boost::intrusive::pointer_traits<Pointer>::element_type value_type;
  76. typedef typename boost::intrusive::pointer_traits<Pointer>::difference_type difference_type;
  77. typedef typename dtl::if_c
  78. < IsConst
  79. , typename boost::intrusive::pointer_traits<Pointer>::template
  80. rebind_pointer<const value_type>::type
  81. , Pointer
  82. >::type pointer;
  83. typedef typename boost::intrusive::pointer_traits<pointer> ptr_traits;
  84. typedef typename ptr_traits::reference reference;
  85. #ifndef BOOST_CONTAINER_DOXYGEN_INVOKED
  86. private:
  87. Pointer m_ptr;
  88. class nat
  89. {
  90. public:
  91. Pointer get_ptr() const
  92. { return Pointer(); }
  93. };
  94. typedef typename dtl::if_c< IsConst
  95. , vec_iterator<Pointer, false>
  96. , nat>::type nonconst_iterator;
  97. public:
  98. BOOST_CONTAINER_FORCEINLINE const Pointer &get_ptr() const BOOST_NOEXCEPT_OR_NOTHROW
  99. { return m_ptr; }
  100. BOOST_CONTAINER_FORCEINLINE Pointer &get_ptr() BOOST_NOEXCEPT_OR_NOTHROW
  101. { return m_ptr; }
  102. BOOST_CONTAINER_FORCEINLINE explicit vec_iterator(Pointer ptr) BOOST_NOEXCEPT_OR_NOTHROW
  103. : m_ptr(ptr)
  104. {}
  105. #endif //#ifndef BOOST_CONTAINER_DOXYGEN_INVOKED
  106. public:
  107. //Constructors
  108. BOOST_CONTAINER_FORCEINLINE vec_iterator() BOOST_NOEXCEPT_OR_NOTHROW
  109. : m_ptr() //Value initialization to achieve "null iterators" (N3644)
  110. {}
  111. BOOST_CONTAINER_FORCEINLINE vec_iterator(const vec_iterator& other) BOOST_NOEXCEPT_OR_NOTHROW
  112. : m_ptr(other.get_ptr())
  113. {}
  114. BOOST_CONTAINER_FORCEINLINE vec_iterator(const nonconst_iterator &other) BOOST_NOEXCEPT_OR_NOTHROW
  115. : m_ptr(other.get_ptr())
  116. {}
  117. BOOST_CONTAINER_FORCEINLINE vec_iterator & operator=(const vec_iterator& other) BOOST_NOEXCEPT_OR_NOTHROW
  118. { m_ptr = other.get_ptr(); return *this; }
  119. //Pointer like operators
  120. BOOST_CONTAINER_FORCEINLINE reference operator*() const BOOST_NOEXCEPT_OR_NOTHROW
  121. { BOOST_ASSERT(!!m_ptr); return *m_ptr; }
  122. BOOST_CONTAINER_FORCEINLINE pointer operator->() const BOOST_NOEXCEPT_OR_NOTHROW
  123. { return m_ptr; }
  124. BOOST_CONTAINER_FORCEINLINE reference operator[](difference_type off) const BOOST_NOEXCEPT_OR_NOTHROW
  125. { BOOST_ASSERT(!!m_ptr); return m_ptr[off]; }
  126. //Increment / Decrement
  127. BOOST_CONTAINER_FORCEINLINE vec_iterator& operator++() BOOST_NOEXCEPT_OR_NOTHROW
  128. { BOOST_ASSERT(!!m_ptr); ++m_ptr; return *this; }
  129. BOOST_CONTAINER_FORCEINLINE vec_iterator operator++(int) BOOST_NOEXCEPT_OR_NOTHROW
  130. { BOOST_ASSERT(!!m_ptr); return vec_iterator(m_ptr++); }
  131. BOOST_CONTAINER_FORCEINLINE vec_iterator& operator--() BOOST_NOEXCEPT_OR_NOTHROW
  132. { BOOST_ASSERT(!!m_ptr); --m_ptr; return *this; }
  133. BOOST_CONTAINER_FORCEINLINE vec_iterator operator--(int) BOOST_NOEXCEPT_OR_NOTHROW
  134. { BOOST_ASSERT(!!m_ptr); return vec_iterator(m_ptr--); }
  135. //Arithmetic
  136. BOOST_CONTAINER_FORCEINLINE vec_iterator& operator+=(difference_type off) BOOST_NOEXCEPT_OR_NOTHROW
  137. { BOOST_ASSERT(m_ptr || !off); m_ptr += off; return *this; }
  138. BOOST_CONTAINER_FORCEINLINE vec_iterator& operator-=(difference_type off) BOOST_NOEXCEPT_OR_NOTHROW
  139. { BOOST_ASSERT(m_ptr || !off); m_ptr -= off; return *this; }
  140. BOOST_CONTAINER_FORCEINLINE friend vec_iterator operator+(const vec_iterator &x, difference_type off) BOOST_NOEXCEPT_OR_NOTHROW
  141. { BOOST_ASSERT(x.m_ptr || !off); return vec_iterator(x.m_ptr+off); }
  142. BOOST_CONTAINER_FORCEINLINE friend vec_iterator operator+(difference_type off, vec_iterator right) BOOST_NOEXCEPT_OR_NOTHROW
  143. { BOOST_ASSERT(right.m_ptr || !off); right.m_ptr += off; return right; }
  144. BOOST_CONTAINER_FORCEINLINE friend vec_iterator operator-(vec_iterator left, difference_type off) BOOST_NOEXCEPT_OR_NOTHROW
  145. { BOOST_ASSERT(left.m_ptr || !off); left.m_ptr -= off; return left; }
  146. BOOST_CONTAINER_FORCEINLINE friend difference_type operator-(const vec_iterator &left, const vec_iterator& right) BOOST_NOEXCEPT_OR_NOTHROW
  147. { return left.m_ptr - right.m_ptr; }
  148. //Comparison operators
  149. BOOST_CONTAINER_FORCEINLINE friend bool operator== (const vec_iterator& l, const vec_iterator& r) BOOST_NOEXCEPT_OR_NOTHROW
  150. { return l.m_ptr == r.m_ptr; }
  151. BOOST_CONTAINER_FORCEINLINE friend bool operator!= (const vec_iterator& l, const vec_iterator& r) BOOST_NOEXCEPT_OR_NOTHROW
  152. { return l.m_ptr != r.m_ptr; }
  153. BOOST_CONTAINER_FORCEINLINE friend bool operator< (const vec_iterator& l, const vec_iterator& r) BOOST_NOEXCEPT_OR_NOTHROW
  154. { return l.m_ptr < r.m_ptr; }
  155. BOOST_CONTAINER_FORCEINLINE friend bool operator<= (const vec_iterator& l, const vec_iterator& r) BOOST_NOEXCEPT_OR_NOTHROW
  156. { return l.m_ptr <= r.m_ptr; }
  157. BOOST_CONTAINER_FORCEINLINE friend bool operator> (const vec_iterator& l, const vec_iterator& r) BOOST_NOEXCEPT_OR_NOTHROW
  158. { return l.m_ptr > r.m_ptr; }
  159. BOOST_CONTAINER_FORCEINLINE friend bool operator>= (const vec_iterator& l, const vec_iterator& r) BOOST_NOEXCEPT_OR_NOTHROW
  160. { return l.m_ptr >= r.m_ptr; }
  161. };
  162. template<class BiDirPosConstIt, class BiDirValueIt>
  163. struct vector_insert_ordered_cursor
  164. {
  165. typedef typename iterator_traits<BiDirPosConstIt>::value_type size_type;
  166. typedef typename iterator_traits<BiDirValueIt>::reference reference;
  167. BOOST_CONTAINER_FORCEINLINE vector_insert_ordered_cursor(BiDirPosConstIt posit, BiDirValueIt valueit)
  168. : last_position_it(posit), last_value_it(valueit)
  169. {}
  170. void operator --()
  171. {
  172. --last_value_it;
  173. --last_position_it;
  174. while(this->get_pos() == size_type(-1)){
  175. --last_value_it;
  176. --last_position_it;
  177. }
  178. }
  179. BOOST_CONTAINER_FORCEINLINE size_type get_pos() const
  180. { return *last_position_it; }
  181. BOOST_CONTAINER_FORCEINLINE reference get_val()
  182. { return *last_value_it; }
  183. BiDirPosConstIt last_position_it;
  184. BiDirValueIt last_value_it;
  185. };
  186. struct initial_capacity_t{};
  187. template<class Pointer, bool IsConst>
  188. BOOST_CONTAINER_FORCEINLINE const Pointer &vector_iterator_get_ptr(const vec_iterator<Pointer, IsConst> &it) BOOST_NOEXCEPT_OR_NOTHROW
  189. { return it.get_ptr(); }
  190. template<class Pointer, bool IsConst>
  191. BOOST_CONTAINER_FORCEINLINE Pointer &get_ptr(vec_iterator<Pointer, IsConst> &it) BOOST_NOEXCEPT_OR_NOTHROW
  192. { return it.get_ptr(); }
  193. struct vector_uninitialized_size_t {};
  194. static const vector_uninitialized_size_t vector_uninitialized_size = vector_uninitialized_size_t();
  195. template <class T>
  196. struct vector_value_traits_base
  197. {
  198. static const bool trivial_dctr = dtl::is_trivially_destructible<T>::value;
  199. static const bool trivial_dctr_after_move = has_trivial_destructor_after_move<T>::value;
  200. static const bool trivial_copy = dtl::is_trivially_copy_constructible<T>::value;
  201. static const bool nothrow_copy = dtl::is_nothrow_copy_constructible<T>::value || trivial_copy;
  202. static const bool trivial_assign = dtl::is_trivially_copy_assignable<T>::value;
  203. static const bool nothrow_assign = dtl::is_nothrow_copy_assignable<T>::value || trivial_assign;
  204. };
  205. template <class Allocator>
  206. struct vector_value_traits
  207. : public vector_value_traits_base<typename Allocator::value_type>
  208. {
  209. typedef vector_value_traits_base<typename Allocator::value_type> base_t;
  210. //This is the anti-exception array destructor
  211. //to deallocate values already constructed
  212. typedef typename dtl::if_c
  213. <base_t::trivial_dctr
  214. ,dtl::null_scoped_destructor_n<Allocator>
  215. ,dtl::scoped_destructor_n<Allocator>
  216. >::type ArrayDestructor;
  217. //This is the anti-exception array deallocator
  218. typedef dtl::scoped_array_deallocator<Allocator> ArrayDeallocator;
  219. };
  220. //!This struct deallocates and allocated memory
  221. template < class Allocator
  222. , class StoredSizeType
  223. , class AllocatorVersion = typename dtl::version<Allocator>::type
  224. >
  225. struct vector_alloc_holder
  226. : public Allocator
  227. {
  228. private:
  229. BOOST_MOVABLE_BUT_NOT_COPYABLE(vector_alloc_holder)
  230. public:
  231. typedef Allocator allocator_type;
  232. typedef StoredSizeType stored_size_type;
  233. typedef boost::container::allocator_traits<allocator_type> allocator_traits_type;
  234. typedef typename allocator_traits_type::pointer pointer;
  235. typedef typename allocator_traits_type::size_type size_type;
  236. typedef typename allocator_traits_type::value_type value_type;
  237. static bool is_propagable_from(const allocator_type &from_alloc, pointer p, const allocator_type &to_alloc, bool const propagate_allocator)
  238. {
  239. (void)propagate_allocator; (void)p; (void)to_alloc; (void)from_alloc;
  240. const bool all_storage_propagable = !allocator_traits_type::is_partially_propagable::value ||
  241. !allocator_traits_type::storage_is_unpropagable(from_alloc, p);
  242. return all_storage_propagable && (propagate_allocator || allocator_traits_type::equal(from_alloc, to_alloc));
  243. }
  244. static bool are_swap_propagable(const allocator_type &l_a, pointer l_p, const allocator_type &r_a, pointer r_p, bool const propagate_allocator)
  245. {
  246. (void)propagate_allocator; (void)l_p; (void)r_p; (void)l_a; (void)r_a;
  247. const bool all_storage_propagable = !allocator_traits_type::is_partially_propagable::value ||
  248. !(allocator_traits_type::storage_is_unpropagable(l_a, l_p) || allocator_traits_type::storage_is_unpropagable(r_a, r_p));
  249. return all_storage_propagable && (propagate_allocator || allocator_traits_type::equal(l_a, r_a));
  250. }
  251. //Constructor, does not throw
  252. vector_alloc_holder()
  253. BOOST_NOEXCEPT_IF(dtl::is_nothrow_default_constructible<allocator_type>::value)
  254. : allocator_type(), m_start(), m_size(), m_capacity()
  255. {}
  256. //Constructor, does not throw
  257. template<class AllocConvertible>
  258. explicit vector_alloc_holder(BOOST_FWD_REF(AllocConvertible) a) BOOST_NOEXCEPT_OR_NOTHROW
  259. : allocator_type(boost::forward<AllocConvertible>(a)), m_start(), m_size(), m_capacity()
  260. {}
  261. //Constructor, does not throw
  262. template<class AllocConvertible>
  263. vector_alloc_holder(vector_uninitialized_size_t, BOOST_FWD_REF(AllocConvertible) a, size_type initial_size)
  264. : allocator_type(boost::forward<AllocConvertible>(a))
  265. , m_start()
  266. //Size is initialized here so vector should only call uninitialized_xxx after this
  267. , m_size(static_cast<stored_size_type>(initial_size))
  268. , m_capacity()
  269. {
  270. if(initial_size){
  271. pointer reuse = pointer();
  272. size_type final_cap = initial_size;
  273. m_start = this->allocation_command(allocate_new, initial_size, final_cap, reuse);
  274. m_capacity = static_cast<stored_size_type>(final_cap);
  275. }
  276. }
  277. //Constructor, does not throw
  278. vector_alloc_holder(vector_uninitialized_size_t, size_type initial_size)
  279. : allocator_type()
  280. , m_start()
  281. //Size is initialized here so vector should only call uninitialized_xxx after this
  282. , m_size(static_cast<stored_size_type>(initial_size))
  283. , m_capacity()
  284. {
  285. if(initial_size){
  286. pointer reuse = pointer();
  287. size_type final_cap = initial_size;
  288. m_start = this->allocation_command(allocate_new, initial_size, final_cap, reuse);
  289. m_capacity = static_cast<stored_size_type>(final_cap);
  290. }
  291. }
  292. vector_alloc_holder(BOOST_RV_REF(vector_alloc_holder) holder) BOOST_NOEXCEPT_OR_NOTHROW
  293. : allocator_type(BOOST_MOVE_BASE(allocator_type, holder))
  294. , m_start(holder.m_start)
  295. , m_size(holder.m_size)
  296. , m_capacity(holder.m_capacity)
  297. {
  298. holder.m_start = pointer();
  299. holder.m_size = holder.m_capacity = 0;
  300. }
  301. vector_alloc_holder(initial_capacity_t, pointer p, size_type capacity, BOOST_RV_REF(vector_alloc_holder) holder)
  302. : allocator_type(BOOST_MOVE_BASE(allocator_type, holder))
  303. , m_start(p)
  304. , m_size(holder.m_size)
  305. , m_capacity(static_cast<stored_size_type>(capacity))
  306. {
  307. allocator_type &this_alloc = this->alloc();
  308. allocator_type &x_alloc = holder.alloc();
  309. if(this->is_propagable_from(x_alloc, holder.start(), this_alloc, true)){
  310. if(this->m_capacity){
  311. this->deallocate(this->m_start, this->m_capacity);
  312. }
  313. m_start = holder.m_start;
  314. m_capacity = holder.m_capacity;
  315. holder.m_start = pointer();
  316. holder.m_capacity = holder.m_size = 0;
  317. }
  318. else if(this->m_capacity < holder.m_size){
  319. size_type const n = holder.m_size;
  320. pointer reuse = pointer();
  321. size_type final_cap = n;
  322. m_start = this->allocation_command(allocate_new, n, final_cap, reuse);
  323. m_capacity = static_cast<stored_size_type>(final_cap);
  324. #ifdef BOOST_CONTAINER_VECTOR_ALLOC_STATS
  325. this->num_alloc += n != 0;
  326. #endif
  327. }
  328. }
  329. vector_alloc_holder(initial_capacity_t, pointer p, size_type n)
  330. BOOST_NOEXCEPT_IF(dtl::is_nothrow_default_constructible<allocator_type>::value)
  331. : allocator_type()
  332. , m_start(p)
  333. , m_size()
  334. //n is guaranteed to fit into stored_size_type
  335. , m_capacity(static_cast<stored_size_type>(n))
  336. {}
  337. template<class AllocFwd>
  338. vector_alloc_holder(initial_capacity_t, pointer p, size_type n, BOOST_FWD_REF(AllocFwd) a)
  339. : allocator_type(::boost::forward<AllocFwd>(a))
  340. , m_start(p)
  341. , m_size()
  342. , m_capacity(n)
  343. {}
  344. BOOST_CONTAINER_FORCEINLINE ~vector_alloc_holder() BOOST_NOEXCEPT_OR_NOTHROW
  345. {
  346. if(this->m_capacity){
  347. this->deallocate(this->m_start, this->m_capacity);
  348. }
  349. }
  350. BOOST_CONTAINER_FORCEINLINE pointer allocation_command(boost::container::allocation_type command,
  351. size_type limit_size, size_type &prefer_in_recvd_out_size, pointer &reuse)
  352. {
  353. typedef typename dtl::version<allocator_type>::type alloc_version;
  354. return this->priv_allocation_command(alloc_version(), command, limit_size, prefer_in_recvd_out_size, reuse);
  355. }
  356. BOOST_CONTAINER_FORCEINLINE pointer allocate(size_type n)
  357. {
  358. const size_type max_alloc = allocator_traits_type::max_size(this->alloc());
  359. const size_type max = max_alloc <= stored_size_type(-1) ? max_alloc : stored_size_type(-1);
  360. if ( max < n )
  361. boost::container::throw_length_error("get_next_capacity, allocator's max size reached");
  362. return allocator_traits_type::allocate(this->alloc(), n);
  363. }
  364. BOOST_CONTAINER_FORCEINLINE void deallocate(const pointer &p, size_type n)
  365. {
  366. allocator_traits_type::deallocate(this->alloc(), p, n);
  367. }
  368. bool try_expand_fwd(size_type at_least)
  369. {
  370. //There is not enough memory, try to expand the old one
  371. const size_type new_cap = this->capacity() + at_least;
  372. size_type real_cap = new_cap;
  373. pointer reuse = this->start();
  374. bool const success = !!this->allocation_command(expand_fwd, new_cap, real_cap, reuse);
  375. //Check for forward expansion
  376. if(success){
  377. #ifdef BOOST_CONTAINER_VECTOR_ALLOC_STATS
  378. ++this->num_expand_fwd;
  379. #endif
  380. this->capacity(real_cap);
  381. }
  382. return success;
  383. }
  384. template<class GrowthFactorType>
  385. size_type next_capacity(size_type additional_objects) const
  386. {
  387. BOOST_ASSERT(additional_objects > size_type(this->m_capacity - this->m_size));
  388. size_type max = allocator_traits_type::max_size(this->alloc());
  389. (clamp_by_stored_size_type)(max, stored_size_type());
  390. const size_type remaining_cap = max - size_type(this->m_capacity);
  391. const size_type min_additional_cap = additional_objects - size_type(this->m_capacity - this->m_size);
  392. if ( remaining_cap < min_additional_cap )
  393. boost::container::throw_length_error("get_next_capacity, allocator's max size reached");
  394. return GrowthFactorType()( size_type(this->m_capacity), min_additional_cap, max);
  395. }
  396. pointer m_start;
  397. stored_size_type m_size;
  398. stored_size_type m_capacity;
  399. void swap_resources(vector_alloc_holder &x) BOOST_NOEXCEPT_OR_NOTHROW
  400. {
  401. boost::adl_move_swap(this->m_start, x.m_start);
  402. boost::adl_move_swap(this->m_size, x.m_size);
  403. boost::adl_move_swap(this->m_capacity, x.m_capacity);
  404. }
  405. void steal_resources(vector_alloc_holder &x) BOOST_NOEXCEPT_OR_NOTHROW
  406. {
  407. this->m_start = x.m_start;
  408. this->m_size = x.m_size;
  409. this->m_capacity = x.m_capacity;
  410. x.m_start = pointer();
  411. x.m_size = x.m_capacity = 0;
  412. }
  413. BOOST_CONTAINER_FORCEINLINE allocator_type &alloc() BOOST_NOEXCEPT_OR_NOTHROW
  414. { return *this; }
  415. BOOST_CONTAINER_FORCEINLINE const allocator_type &alloc() const BOOST_NOEXCEPT_OR_NOTHROW
  416. { return *this; }
  417. BOOST_CONTAINER_FORCEINLINE const pointer &start() const BOOST_NOEXCEPT_OR_NOTHROW
  418. { return m_start; }
  419. BOOST_CONTAINER_FORCEINLINE size_type capacity() const BOOST_NOEXCEPT_OR_NOTHROW
  420. { return m_capacity; }
  421. BOOST_CONTAINER_FORCEINLINE void start(const pointer &p) BOOST_NOEXCEPT_OR_NOTHROW
  422. { m_start = p; }
  423. BOOST_CONTAINER_FORCEINLINE void capacity(const size_type &c) BOOST_NOEXCEPT_OR_NOTHROW
  424. { BOOST_ASSERT( c <= stored_size_type(-1)); m_capacity = c; }
  425. static BOOST_CONTAINER_FORCEINLINE void on_capacity_overflow()
  426. { }
  427. private:
  428. void priv_first_allocation(size_type cap)
  429. {
  430. if(cap){
  431. pointer reuse = pointer();
  432. m_start = this->allocation_command(allocate_new, cap, cap, reuse);
  433. m_capacity = cap;
  434. #ifdef BOOST_CONTAINER_VECTOR_ALLOC_STATS
  435. ++this->num_alloc;
  436. #endif
  437. }
  438. }
  439. BOOST_CONTAINER_FORCEINLINE static void clamp_by_stored_size_type(size_type &, size_type)
  440. {}
  441. template<class SomeStoredSizeType>
  442. BOOST_CONTAINER_FORCEINLINE static void clamp_by_stored_size_type(size_type &s, SomeStoredSizeType)
  443. {
  444. if (s >= SomeStoredSizeType(-1) )
  445. s = SomeStoredSizeType(-1);
  446. }
  447. BOOST_CONTAINER_FORCEINLINE pointer priv_allocation_command(version_1, boost::container::allocation_type command,
  448. size_type limit_size,
  449. size_type &prefer_in_recvd_out_size,
  450. pointer &reuse)
  451. {
  452. (void)command;
  453. BOOST_ASSERT( (command & allocate_new));
  454. BOOST_ASSERT(!(command & nothrow_allocation));
  455. //First detect overflow on smaller stored_size_types
  456. if (limit_size > stored_size_type(-1)){
  457. boost::container::throw_length_error("get_next_capacity, allocator's max size reached");
  458. }
  459. (clamp_by_stored_size_type)(prefer_in_recvd_out_size, stored_size_type());
  460. pointer const p = this->allocate(prefer_in_recvd_out_size);
  461. reuse = pointer();
  462. return p;
  463. }
  464. pointer priv_allocation_command(version_2, boost::container::allocation_type command,
  465. size_type limit_size,
  466. size_type &prefer_in_recvd_out_size,
  467. pointer &reuse)
  468. {
  469. //First detect overflow on smaller stored_size_types
  470. if (limit_size > stored_size_type(-1)){
  471. boost::container::throw_length_error("get_next_capacity, allocator's max size reached");
  472. }
  473. (clamp_by_stored_size_type)(prefer_in_recvd_out_size, stored_size_type());
  474. //Allocate memory
  475. pointer p = this->alloc().allocation_command(command, limit_size, prefer_in_recvd_out_size, reuse);
  476. //If after allocation prefer_in_recvd_out_size is not representable by stored_size_type, truncate it.
  477. (clamp_by_stored_size_type)(prefer_in_recvd_out_size, stored_size_type());
  478. return p;
  479. }
  480. };
  481. //!This struct deallocates and allocated memory
  482. template <class Allocator, class StoredSizeType>
  483. struct vector_alloc_holder<Allocator, StoredSizeType, version_0>
  484. : public Allocator
  485. {
  486. private:
  487. BOOST_MOVABLE_BUT_NOT_COPYABLE(vector_alloc_holder)
  488. public:
  489. typedef Allocator allocator_type;
  490. typedef boost::container::
  491. allocator_traits<allocator_type> allocator_traits_type;
  492. typedef typename allocator_traits_type::pointer pointer;
  493. typedef typename allocator_traits_type::size_type size_type;
  494. typedef typename allocator_traits_type::value_type value_type;
  495. typedef StoredSizeType stored_size_type;
  496. template <class OtherAllocator, class OtherStoredSizeType, class OtherAllocatorVersion>
  497. friend struct vector_alloc_holder;
  498. //Constructor, does not throw
  499. vector_alloc_holder()
  500. BOOST_NOEXCEPT_IF(dtl::is_nothrow_default_constructible<allocator_type>::value)
  501. : allocator_type(), m_size()
  502. {}
  503. //Constructor, does not throw
  504. template<class AllocConvertible>
  505. explicit vector_alloc_holder(BOOST_FWD_REF(AllocConvertible) a) BOOST_NOEXCEPT_OR_NOTHROW
  506. : allocator_type(boost::forward<AllocConvertible>(a)), m_size()
  507. {}
  508. //Constructor, does not throw
  509. template<class AllocConvertible>
  510. vector_alloc_holder(vector_uninitialized_size_t, BOOST_FWD_REF(AllocConvertible) a, size_type initial_size)
  511. : allocator_type(boost::forward<AllocConvertible>(a))
  512. , m_size(initial_size) //Size is initialized here...
  513. {
  514. //... and capacity here, so vector, must call uninitialized_xxx in the derived constructor
  515. this->priv_first_allocation(initial_size);
  516. }
  517. //Constructor, does not throw
  518. vector_alloc_holder(vector_uninitialized_size_t, size_type initial_size)
  519. : allocator_type()
  520. , m_size(initial_size) //Size is initialized here...
  521. {
  522. //... and capacity here, so vector, must call uninitialized_xxx in the derived constructor
  523. this->priv_first_allocation(initial_size);
  524. }
  525. vector_alloc_holder(BOOST_RV_REF(vector_alloc_holder) holder)
  526. : allocator_type(BOOST_MOVE_BASE(allocator_type, holder))
  527. , m_size(holder.m_size) //Size is initialized here so vector should only call uninitialized_xxx after this
  528. {
  529. ::boost::container::uninitialized_move_alloc_n
  530. (this->alloc(), boost::movelib::to_raw_pointer(holder.start()), m_size, boost::movelib::to_raw_pointer(this->start()));
  531. }
  532. template<class OtherAllocator, class OtherStoredSizeType, class OtherAllocatorVersion>
  533. vector_alloc_holder(BOOST_RV_REF_BEG vector_alloc_holder<OtherAllocator, OtherStoredSizeType, OtherAllocatorVersion> BOOST_RV_REF_END holder)
  534. : allocator_type()
  535. , m_size(holder.m_size) //Initialize it to m_size as first_allocation can only succeed or abort
  536. {
  537. //Different allocator type so we must check we have enough storage
  538. const size_type n = holder.m_size;
  539. this->priv_first_allocation(n);
  540. ::boost::container::uninitialized_move_alloc_n
  541. (this->alloc(), boost::movelib::to_raw_pointer(holder.start()), n, boost::movelib::to_raw_pointer(this->start()));
  542. }
  543. static BOOST_CONTAINER_FORCEINLINE void on_capacity_overflow()
  544. { allocator_type::on_capacity_overflow(); }
  545. BOOST_CONTAINER_FORCEINLINE void priv_first_allocation(size_type cap)
  546. {
  547. if(cap > allocator_type::internal_capacity){
  548. on_capacity_overflow();
  549. }
  550. }
  551. BOOST_CONTAINER_FORCEINLINE void deep_swap(vector_alloc_holder &x)
  552. {
  553. this->priv_deep_swap(x);
  554. }
  555. template<class OtherAllocator, class OtherStoredSizeType, class OtherAllocatorVersion>
  556. void deep_swap(vector_alloc_holder<OtherAllocator, OtherStoredSizeType, OtherAllocatorVersion> &x)
  557. {
  558. typedef typename real_allocator<value_type, OtherAllocator>::type other_allocator_type;
  559. if(this->m_size > other_allocator_type::internal_capacity || x.m_size > allocator_type::internal_capacity){
  560. on_capacity_overflow();
  561. }
  562. this->priv_deep_swap(x);
  563. }
  564. BOOST_CONTAINER_FORCEINLINE void swap_resources(vector_alloc_holder &) BOOST_NOEXCEPT_OR_NOTHROW
  565. { //Containers with version 0 allocators can't be moved without moving elements one by one
  566. on_capacity_overflow();
  567. }
  568. BOOST_CONTAINER_FORCEINLINE void steal_resources(vector_alloc_holder &)
  569. { //Containers with version 0 allocators can't be moved without moving elements one by one
  570. on_capacity_overflow();
  571. }
  572. BOOST_CONTAINER_FORCEINLINE allocator_type &alloc() BOOST_NOEXCEPT_OR_NOTHROW
  573. { return *this; }
  574. BOOST_CONTAINER_FORCEINLINE const allocator_type &alloc() const BOOST_NOEXCEPT_OR_NOTHROW
  575. { return *this; }
  576. BOOST_CONTAINER_FORCEINLINE bool try_expand_fwd(size_type at_least)
  577. { return !at_least; }
  578. BOOST_CONTAINER_FORCEINLINE pointer start() const BOOST_NOEXCEPT_OR_NOTHROW
  579. { return allocator_type::internal_storage(); }
  580. BOOST_CONTAINER_FORCEINLINE size_type capacity() const BOOST_NOEXCEPT_OR_NOTHROW
  581. { return allocator_type::internal_capacity; }
  582. stored_size_type m_size;
  583. private:
  584. template<class OtherAllocator, class OtherStoredSizeType, class OtherAllocatorVersion>
  585. void priv_deep_swap(vector_alloc_holder<OtherAllocator, OtherStoredSizeType, OtherAllocatorVersion> &x)
  586. {
  587. const size_type MaxTmpStorage = sizeof(value_type)*allocator_type::internal_capacity;
  588. value_type *const first_this = boost::movelib::to_raw_pointer(this->start());
  589. value_type *const first_x = boost::movelib::to_raw_pointer(x.start());
  590. if(this->m_size < x.m_size){
  591. boost::container::deep_swap_alloc_n<MaxTmpStorage>(this->alloc(), first_this, this->m_size, first_x, x.m_size);
  592. }
  593. else{
  594. boost::container::deep_swap_alloc_n<MaxTmpStorage>(this->alloc(), first_x, x.m_size, first_this, this->m_size);
  595. }
  596. boost::adl_move_swap(this->m_size, x.m_size);
  597. }
  598. };
  599. struct growth_factor_60;
  600. template<class Options, class AllocatorSizeType>
  601. struct get_vector_opt
  602. {
  603. typedef vector_opt< typename default_if_void<typename Options::growth_factor_type, growth_factor_60>::type
  604. , typename default_if_void<typename Options::stored_size_type, AllocatorSizeType>::type
  605. > type;
  606. };
  607. template<class AllocatorSizeType>
  608. struct get_vector_opt<void, AllocatorSizeType>
  609. {
  610. typedef vector_opt<growth_factor_60, AllocatorSizeType> type;
  611. };
  612. #endif //#ifndef BOOST_CONTAINER_DOXYGEN_INVOKED
  613. //! A vector is a sequence that supports random access to elements, constant
  614. //! time insertion and removal of elements at the end, and linear time insertion
  615. //! and removal of elements at the beginning or in the middle. The number of
  616. //! elements in a vector may vary dynamically; memory management is automatic.
  617. //!
  618. //! \tparam T The type of object that is stored in the vector
  619. //! \tparam A The allocator used for all internal memory management, use void
  620. //! for the default allocator
  621. //! \tparam Options A type produced from \c boost::container::vector_options.
  622. template <class T, class A BOOST_CONTAINER_DOCONLY(= void), class Options BOOST_CONTAINER_DOCONLY(= void) >
  623. class vector
  624. {
  625. public:
  626. //////////////////////////////////////////////
  627. //
  628. // types
  629. //
  630. //////////////////////////////////////////////
  631. typedef T value_type;
  632. typedef BOOST_CONTAINER_IMPDEF
  633. (typename real_allocator<T BOOST_MOVE_I A>::type) allocator_type;
  634. typedef ::boost::container::allocator_traits<allocator_type> allocator_traits_t;
  635. typedef typename allocator_traits<allocator_type>::pointer pointer;
  636. typedef typename allocator_traits<allocator_type>::const_pointer const_pointer;
  637. typedef typename allocator_traits<allocator_type>::reference reference;
  638. typedef typename allocator_traits<allocator_type>::const_reference const_reference;
  639. typedef typename allocator_traits<allocator_type>::size_type size_type;
  640. typedef typename allocator_traits<allocator_type>::difference_type difference_type;
  641. typedef allocator_type stored_allocator_type;
  642. typedef BOOST_CONTAINER_IMPDEF(vec_iterator<pointer BOOST_MOVE_I false>) iterator;
  643. typedef BOOST_CONTAINER_IMPDEF(vec_iterator<pointer BOOST_MOVE_I true >) const_iterator;
  644. typedef BOOST_CONTAINER_IMPDEF(boost::container::reverse_iterator<iterator>) reverse_iterator;
  645. typedef BOOST_CONTAINER_IMPDEF(boost::container::reverse_iterator<const_iterator>) const_reverse_iterator;
  646. private:
  647. #ifndef BOOST_CONTAINER_DOXYGEN_INVOKED
  648. typedef typename boost::container::
  649. allocator_traits<allocator_type>::size_type alloc_size_type;
  650. typedef typename get_vector_opt<Options, alloc_size_type>::type options_type;
  651. typedef typename options_type::growth_factor_type growth_factor_type;
  652. typedef typename options_type::stored_size_type stored_size_type;
  653. typedef value_less<T> value_less_t;
  654. //If provided the stored_size option must specify a type that is equal or a type that is smaller.
  655. BOOST_STATIC_ASSERT( (sizeof(stored_size_type) < sizeof(alloc_size_type) ||
  656. dtl::is_same<stored_size_type, alloc_size_type>::value) );
  657. typedef typename dtl::version<allocator_type>::type alloc_version;
  658. typedef boost::container::vector_alloc_holder
  659. <allocator_type, stored_size_type> alloc_holder_t;
  660. alloc_holder_t m_holder;
  661. typedef allocator_traits<allocator_type> allocator_traits_type;
  662. template <class U, class UA, class UOptions>
  663. friend class vector;
  664. protected:
  665. BOOST_CONTAINER_FORCEINLINE
  666. static bool is_propagable_from(const allocator_type &from_alloc, pointer p, const allocator_type &to_alloc, bool const propagate_allocator)
  667. { return alloc_holder_t::is_propagable_from(from_alloc, p, to_alloc, propagate_allocator); }
  668. BOOST_CONTAINER_FORCEINLINE
  669. static bool are_swap_propagable( const allocator_type &l_a, pointer l_p
  670. , const allocator_type &r_a, pointer r_p, bool const propagate_allocator)
  671. { return alloc_holder_t::are_swap_propagable(l_a, l_p, r_a, r_p, propagate_allocator); }
  672. #endif //#ifndef BOOST_CONTAINER_DOXYGEN_INVOKED
  673. #ifndef BOOST_CONTAINER_DOXYGEN_INVOKED
  674. private:
  675. BOOST_COPYABLE_AND_MOVABLE(vector)
  676. typedef vector_value_traits<allocator_type> value_traits;
  677. typedef constant_iterator<T, difference_type> cvalue_iterator;
  678. protected:
  679. BOOST_CONTAINER_FORCEINLINE void steal_resources(vector &x)
  680. { return this->m_holder.steal_resources(x.m_holder); }
  681. template<class AllocFwd>
  682. BOOST_CONTAINER_FORCEINLINE vector(initial_capacity_t, pointer initial_memory, size_type capacity, BOOST_FWD_REF(AllocFwd) a)
  683. : m_holder(initial_capacity_t(), initial_memory, capacity, ::boost::forward<AllocFwd>(a))
  684. {}
  685. BOOST_CONTAINER_FORCEINLINE vector(initial_capacity_t, pointer initial_memory, size_type capacity)
  686. : m_holder(initial_capacity_t(), initial_memory, capacity)
  687. {}
  688. #endif //#ifndef BOOST_CONTAINER_DOXYGEN_INVOKED
  689. public:
  690. //////////////////////////////////////////////
  691. //
  692. // construct/copy/destroy
  693. //
  694. //////////////////////////////////////////////
  695. //! <b>Effects</b>: Constructs a vector taking the allocator as parameter.
  696. //!
  697. //! <b>Throws</b>: Nothing.
  698. //!
  699. //! <b>Complexity</b>: Constant.
  700. vector() BOOST_NOEXCEPT_IF(dtl::is_nothrow_default_constructible<allocator_type>::value)
  701. : m_holder()
  702. {}
  703. //! <b>Effects</b>: Constructs a vector taking the allocator as parameter.
  704. //!
  705. //! <b>Throws</b>: Nothing
  706. //!
  707. //! <b>Complexity</b>: Constant.
  708. explicit vector(const allocator_type& a) BOOST_NOEXCEPT_OR_NOTHROW
  709. : m_holder(a)
  710. {}
  711. //! <b>Effects</b>: Constructs a vector and inserts n value initialized values.
  712. //!
  713. //! <b>Throws</b>: If allocator_type's allocation
  714. //! throws or T's value initialization throws.
  715. //!
  716. //! <b>Complexity</b>: Linear to n.
  717. explicit vector(size_type n)
  718. : m_holder(vector_uninitialized_size, n)
  719. {
  720. #ifdef BOOST_CONTAINER_VECTOR_ALLOC_STATS
  721. this->num_alloc += n != 0;
  722. #endif
  723. boost::container::uninitialized_value_init_alloc_n
  724. (this->m_holder.alloc(), n, this->priv_raw_begin());
  725. }
  726. //! <b>Effects</b>: Constructs a vector that will use a copy of allocator a
  727. //! and inserts n value initialized values.
  728. //!
  729. //! <b>Throws</b>: If allocator_type's allocation
  730. //! throws or T's value initialization throws.
  731. //!
  732. //! <b>Complexity</b>: Linear to n.
  733. explicit vector(size_type n, const allocator_type &a)
  734. : m_holder(vector_uninitialized_size, a, n)
  735. {
  736. #ifdef BOOST_CONTAINER_VECTOR_ALLOC_STATS
  737. this->num_alloc += n != 0;
  738. #endif
  739. boost::container::uninitialized_value_init_alloc_n
  740. (this->m_holder.alloc(), n, this->priv_raw_begin());
  741. }
  742. //! <b>Effects</b>: Constructs a vector that will use a copy of allocator a
  743. //! and inserts n default initialized values.
  744. //!
  745. //! <b>Throws</b>: If allocator_type's allocation
  746. //! throws or T's default initialization throws.
  747. //!
  748. //! <b>Complexity</b>: Linear to n.
  749. //!
  750. //! <b>Note</b>: Non-standard extension
  751. vector(size_type n, default_init_t)
  752. : m_holder(vector_uninitialized_size, n)
  753. {
  754. #ifdef BOOST_CONTAINER_VECTOR_ALLOC_STATS
  755. this->num_alloc += n != 0;
  756. #endif
  757. boost::container::uninitialized_default_init_alloc_n
  758. (this->m_holder.alloc(), n, this->priv_raw_begin());
  759. }
  760. //! <b>Effects</b>: Constructs a vector that will use a copy of allocator a
  761. //! and inserts n default initialized values.
  762. //!
  763. //! <b>Throws</b>: If allocator_type's allocation
  764. //! throws or T's default initialization throws.
  765. //!
  766. //! <b>Complexity</b>: Linear to n.
  767. //!
  768. //! <b>Note</b>: Non-standard extension
  769. vector(size_type n, default_init_t, const allocator_type &a)
  770. : m_holder(vector_uninitialized_size, a, n)
  771. {
  772. #ifdef BOOST_CONTAINER_VECTOR_ALLOC_STATS
  773. this->num_alloc += n != 0;
  774. #endif
  775. boost::container::uninitialized_default_init_alloc_n
  776. (this->m_holder.alloc(), n, this->priv_raw_begin());
  777. }
  778. //! <b>Effects</b>: Constructs a vector
  779. //! and inserts n copies of value.
  780. //!
  781. //! <b>Throws</b>: If allocator_type's allocation
  782. //! throws or T's copy constructor throws.
  783. //!
  784. //! <b>Complexity</b>: Linear to n.
  785. vector(size_type n, const T& value)
  786. : m_holder(vector_uninitialized_size, n)
  787. {
  788. #ifdef BOOST_CONTAINER_VECTOR_ALLOC_STATS
  789. this->num_alloc += n != 0;
  790. #endif
  791. boost::container::uninitialized_fill_alloc_n
  792. (this->m_holder.alloc(), value, n, this->priv_raw_begin());
  793. }
  794. //! <b>Effects</b>: Constructs a vector that will use a copy of allocator a
  795. //! and inserts n copies of value.
  796. //!
  797. //! <b>Throws</b>: If allocation
  798. //! throws or T's copy constructor throws.
  799. //!
  800. //! <b>Complexity</b>: Linear to n.
  801. vector(size_type n, const T& value, const allocator_type& a)
  802. : m_holder(vector_uninitialized_size, a, n)
  803. {
  804. #ifdef BOOST_CONTAINER_VECTOR_ALLOC_STATS
  805. this->num_alloc += n != 0;
  806. #endif
  807. boost::container::uninitialized_fill_alloc_n
  808. (this->m_holder.alloc(), value, n, this->priv_raw_begin());
  809. }
  810. //! <b>Effects</b>: Constructs a vector
  811. //! and inserts a copy of the range [first, last) in the vector.
  812. //!
  813. //! <b>Throws</b>: If allocator_type's allocation
  814. //! throws or T's constructor taking a dereferenced InIt throws.
  815. //!
  816. //! <b>Complexity</b>: Linear to the range [first, last).
  817. // template <class InIt>
  818. // vector(InIt first, InIt last
  819. // BOOST_CONTAINER_DOCIGN(BOOST_MOVE_I typename dtl::disable_if_c
  820. // < dtl::is_convertible<InIt BOOST_MOVE_I size_type>::value
  821. // BOOST_MOVE_I dtl::nat >::type * = 0)
  822. // ) -> vector<typename iterator_traits<InIt>::value_type, new_allocator<typename iterator_traits<InIt>::value_type>>;
  823. template <class InIt>
  824. vector(InIt first, InIt last
  825. BOOST_CONTAINER_DOCIGN(BOOST_MOVE_I typename dtl::disable_if_c
  826. < dtl::is_convertible<InIt BOOST_MOVE_I size_type>::value
  827. BOOST_MOVE_I dtl::nat >::type * = 0)
  828. )
  829. : m_holder()
  830. { this->assign(first, last); }
  831. //! <b>Effects</b>: Constructs a vector that will use a copy of allocator a
  832. //! and inserts a copy of the range [first, last) in the vector.
  833. //!
  834. //! <b>Throws</b>: If allocator_type's allocation
  835. //! throws or T's constructor taking a dereferenced InIt throws.
  836. //!
  837. //! <b>Complexity</b>: Linear to the range [first, last).
  838. // template <class InIt>
  839. // vector(InIt first, InIt last, const allocator_type& a
  840. // BOOST_CONTAINER_DOCIGN(BOOST_MOVE_I typename dtl::disable_if_c
  841. // < dtl::is_convertible<InIt BOOST_MOVE_I size_type>::value
  842. // BOOST_MOVE_I dtl::nat >::type * = 0)
  843. // ) -> vector<typename iterator_traits<InIt>::value_type, new_allocator<typename iterator_traits<InIt>::value_type>>;
  844. template <class InIt>
  845. vector(InIt first, InIt last, const allocator_type& a
  846. BOOST_CONTAINER_DOCIGN(BOOST_MOVE_I typename dtl::disable_if_c
  847. < dtl::is_convertible<InIt BOOST_MOVE_I size_type>::value
  848. BOOST_MOVE_I dtl::nat >::type * = 0)
  849. )
  850. : m_holder(a)
  851. { this->assign(first, last); }
  852. //! <b>Effects</b>: Copy constructs a vector.
  853. //!
  854. //! <b>Postcondition</b>: x == *this.
  855. //!
  856. //! <b>Throws</b>: If allocator_type's allocation
  857. //! throws or T's copy constructor throws.
  858. //!
  859. //! <b>Complexity</b>: Linear to the elements x contains.
  860. vector(const vector &x)
  861. : m_holder( vector_uninitialized_size
  862. , allocator_traits_type::select_on_container_copy_construction(x.m_holder.alloc())
  863. , x.size())
  864. {
  865. #ifdef BOOST_CONTAINER_VECTOR_ALLOC_STATS
  866. this->num_alloc += x.size() != 0;
  867. #endif
  868. ::boost::container::uninitialized_copy_alloc_n
  869. ( this->m_holder.alloc(), x.priv_raw_begin()
  870. , x.size(), this->priv_raw_begin());
  871. }
  872. //! <b>Effects</b>: Move constructor. Moves x's resources to *this.
  873. //!
  874. //! <b>Throws</b>: Nothing
  875. //!
  876. //! <b>Complexity</b>: Constant.
  877. vector(BOOST_RV_REF(vector) x) BOOST_NOEXCEPT_OR_NOTHROW
  878. : m_holder(boost::move(x.m_holder))
  879. { BOOST_STATIC_ASSERT((!allocator_traits_type::is_partially_propagable::value)); }
  880. #if !defined(BOOST_NO_CXX11_HDR_INITIALIZER_LIST)
  881. //! <b>Effects</b>: Constructs a vector that will use a copy of allocator a
  882. //! and inserts a copy of the range [il.begin(), il.last()) in the vector
  883. //!
  884. //! <b>Throws</b>: If T's constructor taking a dereferenced initializer_list iterator throws.
  885. //!
  886. //! <b>Complexity</b>: Linear to the range [il.begin(), il.end()).
  887. vector(std::initializer_list<value_type> il, const allocator_type& a = allocator_type())
  888. : m_holder(a)
  889. {
  890. this->assign(il.begin(), il.end());
  891. }
  892. #endif
  893. #if !defined(BOOST_CONTAINER_DOXYGEN_INVOKED)
  894. //! <b>Effects</b>: Move constructor. Moves x's resources to *this.
  895. //!
  896. //! <b>Throws</b>: If T's move constructor or allocation throws
  897. //!
  898. //! <b>Complexity</b>: Linear.
  899. //!
  900. //! <b>Note</b>: Non-standard extension to support static_vector
  901. template<class OtherA>
  902. vector(BOOST_RV_REF_BEG vector<T, OtherA, Options> BOOST_RV_REF_END x
  903. , typename dtl::enable_if_c
  904. < dtl::is_version<typename real_allocator<T, OtherA>::type, 0>::value>::type * = 0
  905. )
  906. : m_holder(boost::move(x.m_holder))
  907. {}
  908. #endif //!defined(BOOST_CONTAINER_DOXYGEN_INVOKED)
  909. //! <b>Effects</b>: Copy constructs a vector using the specified allocator.
  910. //!
  911. //! <b>Postcondition</b>: x == *this.
  912. //!
  913. //! <b>Throws</b>: If allocation
  914. //! throws or T's copy constructor throws.
  915. //!
  916. //! <b>Complexity</b>: Linear to the elements x contains.
  917. vector(const vector &x, const allocator_type &a)
  918. : m_holder(vector_uninitialized_size, a, x.size())
  919. {
  920. #ifdef BOOST_CONTAINER_VECTOR_ALLOC_STATS
  921. this->num_alloc += x.size() != 0;
  922. #endif
  923. ::boost::container::uninitialized_copy_alloc_n_source
  924. ( this->m_holder.alloc(), x.priv_raw_begin()
  925. , x.size(), this->priv_raw_begin());
  926. }
  927. //! <b>Effects</b>: Move constructor using the specified allocator.
  928. //! Moves x's resources to *this if a == allocator_type().
  929. //! Otherwise copies values from x to *this.
  930. //!
  931. //! <b>Throws</b>: If allocation or T's copy constructor throws.
  932. //!
  933. //! <b>Complexity</b>: Constant if a == x.get_allocator(), linear otherwise.
  934. vector(BOOST_RV_REF(vector) x, const allocator_type &a)
  935. : m_holder( vector_uninitialized_size, a
  936. , is_propagable_from(x.get_stored_allocator(), x.m_holder.start(), a, true) ? 0 : x.size()
  937. )
  938. {
  939. if(is_propagable_from(x.get_stored_allocator(), x.m_holder.start(), a, true)){
  940. this->m_holder.steal_resources(x.m_holder);
  941. }
  942. else{
  943. const size_type n = x.size();
  944. #ifdef BOOST_CONTAINER_VECTOR_ALLOC_STATS
  945. this->num_alloc += n != 0;
  946. #endif
  947. ::boost::container::uninitialized_move_alloc_n_source
  948. ( this->m_holder.alloc(), x.priv_raw_begin()
  949. , n, this->priv_raw_begin());
  950. }
  951. }
  952. //! <b>Effects</b>: Destroys the vector. All stored values are destroyed
  953. //! and used memory is deallocated.
  954. //!
  955. //! <b>Throws</b>: Nothing.
  956. //!
  957. //! <b>Complexity</b>: Linear to the number of elements.
  958. ~vector() BOOST_NOEXCEPT_OR_NOTHROW
  959. {
  960. boost::container::destroy_alloc_n
  961. (this->get_stored_allocator(), this->priv_raw_begin(), this->m_holder.m_size);
  962. //vector_alloc_holder deallocates the data
  963. }
  964. //! <b>Effects</b>: Makes *this contain the same elements as x.
  965. //!
  966. //! <b>Postcondition</b>: this->size() == x.size(). *this contains a copy
  967. //! of each of x's elements.
  968. //!
  969. //! <b>Throws</b>: If memory allocation throws or T's copy/move constructor/assignment throws.
  970. //!
  971. //! <b>Complexity</b>: Linear to the number of elements in x.
  972. BOOST_CONTAINER_FORCEINLINE vector& operator=(BOOST_COPY_ASSIGN_REF(vector) x)
  973. {
  974. if (BOOST_LIKELY(&x != this)){
  975. this->priv_copy_assign(x);
  976. }
  977. return *this;
  978. }
  979. #if !defined(BOOST_NO_CXX11_HDR_INITIALIZER_LIST)
  980. //! <b>Effects</b>: Make *this container contains elements from il.
  981. //!
  982. //! <b>Complexity</b>: Linear to the range [il.begin(), il.end()).
  983. BOOST_CONTAINER_FORCEINLINE vector& operator=(std::initializer_list<value_type> il)
  984. {
  985. this->assign(il.begin(), il.end());
  986. return *this;
  987. }
  988. #endif
  989. //! <b>Effects</b>: Move assignment. All x's values are transferred to *this.
  990. //!
  991. //! <b>Postcondition</b>: x.empty(). *this contains a the elements x had
  992. //! before the function.
  993. //!
  994. //! <b>Throws</b>: If allocator_traits_type::propagate_on_container_move_assignment
  995. //! is false and (allocation throws or value_type's move constructor throws)
  996. //!
  997. //! <b>Complexity</b>: Constant if allocator_traits_type::
  998. //! propagate_on_container_move_assignment is true or
  999. //! this->get>allocator() == x.get_allocator(). Linear otherwise.
  1000. BOOST_CONTAINER_FORCEINLINE vector& operator=(BOOST_RV_REF(vector) x)
  1001. BOOST_NOEXCEPT_IF(allocator_traits_type::propagate_on_container_move_assignment::value
  1002. || allocator_traits_type::is_always_equal::value)
  1003. {
  1004. if (BOOST_LIKELY(&x != this)){
  1005. this->priv_move_assign(boost::move(x));
  1006. }
  1007. return *this;
  1008. }
  1009. #if !defined(BOOST_CONTAINER_DOXYGEN_INVOKED)
  1010. //! <b>Effects</b>: Move assignment. All x's values are transferred to *this.
  1011. //!
  1012. //! <b>Postcondition</b>: x.empty(). *this contains a the elements x had
  1013. //! before the function.
  1014. //!
  1015. //! <b>Throws</b>: If move constructor/assignment of T throws or allocation throws
  1016. //!
  1017. //! <b>Complexity</b>: Linear.
  1018. //!
  1019. //! <b>Note</b>: Non-standard extension to support static_vector
  1020. template<class OtherA>
  1021. BOOST_CONTAINER_FORCEINLINE typename dtl::enable_if_and
  1022. < vector&
  1023. , dtl::is_version<typename real_allocator<T, OtherA>::type, 0>
  1024. , dtl::is_different<typename real_allocator<T, OtherA>::type, allocator_type>
  1025. >::type
  1026. operator=(BOOST_RV_REF_BEG vector<value_type, OtherA, Options> BOOST_RV_REF_END x)
  1027. {
  1028. this->priv_move_assign(boost::move(x));
  1029. return *this;
  1030. }
  1031. //! <b>Effects</b>: Copy assignment. All x's values are copied to *this.
  1032. //!
  1033. //! <b>Postcondition</b>: x.empty(). *this contains a the elements x had
  1034. //! before the function.
  1035. //!
  1036. //! <b>Throws</b>: If move constructor/assignment of T throws or allocation throws
  1037. //!
  1038. //! <b>Complexity</b>: Linear.
  1039. //!
  1040. //! <b>Note</b>: Non-standard extension to support static_vector
  1041. template<class OtherA>
  1042. BOOST_CONTAINER_FORCEINLINE typename dtl::enable_if_and
  1043. < vector&
  1044. , dtl::is_version<typename real_allocator<T, OtherA>::type, 0>
  1045. , dtl::is_different<typename real_allocator<T, OtherA>::type, allocator_type>
  1046. >::type
  1047. operator=(const vector<value_type, OtherA, Options> &x)
  1048. {
  1049. this->priv_copy_assign(x);
  1050. return *this;
  1051. }
  1052. #endif
  1053. //! <b>Effects</b>: Assigns the the range [first, last) to *this.
  1054. //!
  1055. //! <b>Throws</b>: If memory allocation throws or T's copy/move constructor/assignment or
  1056. //! T's constructor/assignment from dereferencing InpIt throws.
  1057. //!
  1058. //! <b>Complexity</b>: Linear to n.
  1059. template <class InIt>
  1060. void assign(InIt first, InIt last
  1061. //Input iterators or version 0 allocator
  1062. BOOST_CONTAINER_DOCIGN(BOOST_MOVE_I typename dtl::disable_if_or
  1063. < void
  1064. BOOST_MOVE_I dtl::is_convertible<InIt BOOST_MOVE_I size_type>
  1065. BOOST_MOVE_I dtl::and_
  1066. < dtl::is_different<alloc_version BOOST_MOVE_I version_0>
  1067. BOOST_MOVE_I dtl::is_not_input_iterator<InIt>
  1068. >
  1069. >::type * = 0)
  1070. )
  1071. {
  1072. //Overwrite all elements we can from [first, last)
  1073. iterator cur = this->begin();
  1074. const iterator end_it = this->end();
  1075. for ( ; first != last && cur != end_it; ++cur, ++first){
  1076. *cur = *first;
  1077. }
  1078. if (first == last){
  1079. //There are no more elements in the sequence, erase remaining
  1080. T* const end_pos = this->priv_raw_end();
  1081. const size_type n = static_cast<size_type>(end_pos - boost::movelib::iterator_to_raw_pointer(cur));
  1082. this->priv_destroy_last_n(n);
  1083. }
  1084. else{
  1085. //There are more elements in the range, insert the remaining ones
  1086. this->insert(this->cend(), first, last);
  1087. }
  1088. }
  1089. #if !defined(BOOST_NO_CXX11_HDR_INITIALIZER_LIST)
  1090. //! <b>Effects</b>: Assigns the the range [il.begin(), il.end()) to *this.
  1091. //!
  1092. //! <b>Throws</b>: If memory allocation throws or
  1093. //! T's constructor from dereferencing iniializer_list iterator throws.
  1094. //!
  1095. BOOST_CONTAINER_FORCEINLINE void assign(std::initializer_list<T> il)
  1096. {
  1097. this->assign(il.begin(), il.end());
  1098. }
  1099. #endif
  1100. //! <b>Effects</b>: Assigns the the range [first, last) to *this.
  1101. //!
  1102. //! <b>Throws</b>: If memory allocation throws or T's copy/move constructor/assignment or
  1103. //! T's constructor/assignment from dereferencing InpIt throws.
  1104. //!
  1105. //! <b>Complexity</b>: Linear to n.
  1106. template <class FwdIt>
  1107. void assign(FwdIt first, FwdIt last
  1108. //Forward iterators and version > 0 allocator
  1109. BOOST_CONTAINER_DOCIGN(BOOST_MOVE_I typename dtl::disable_if_or
  1110. < void
  1111. BOOST_MOVE_I dtl::is_same<alloc_version BOOST_MOVE_I version_0>
  1112. BOOST_MOVE_I dtl::is_convertible<FwdIt BOOST_MOVE_I size_type>
  1113. BOOST_MOVE_I dtl::is_input_iterator<FwdIt>
  1114. >::type * = 0)
  1115. )
  1116. {
  1117. //For Fwd iterators the standard only requires EmplaceConstructible and assignable from *first
  1118. //so we can't do any backwards allocation
  1119. const size_type input_sz = static_cast<size_type>(boost::container::iterator_distance(first, last));
  1120. const size_type old_capacity = this->capacity();
  1121. if(input_sz > old_capacity){ //If input range is too big, we need to reallocate
  1122. size_type real_cap = 0;
  1123. pointer reuse(this->m_holder.start());
  1124. pointer const ret(this->m_holder.allocation_command(allocate_new|expand_fwd, input_sz, real_cap = input_sz, reuse));
  1125. if(!reuse){ //New allocation, just emplace new values
  1126. #ifdef BOOST_CONTAINER_VECTOR_ALLOC_STATS
  1127. ++this->num_alloc;
  1128. #endif
  1129. pointer const old_p = this->m_holder.start();
  1130. if(old_p){
  1131. this->priv_destroy_all();
  1132. this->m_holder.deallocate(old_p, old_capacity);
  1133. }
  1134. this->m_holder.start(ret);
  1135. this->m_holder.capacity(real_cap);
  1136. this->m_holder.m_size = 0;
  1137. this->priv_uninitialized_construct_at_end(first, last);
  1138. return;
  1139. }
  1140. else{
  1141. #ifdef BOOST_CONTAINER_VECTOR_ALLOC_STATS
  1142. ++this->num_expand_fwd;
  1143. #endif
  1144. this->m_holder.capacity(real_cap);
  1145. //Forward expansion, use assignment + back deletion/construction that comes later
  1146. }
  1147. }
  1148. boost::container::copy_assign_range_alloc_n(this->m_holder.alloc(), first, input_sz, this->priv_raw_begin(), this->size());
  1149. this->m_holder.m_size = input_sz;
  1150. }
  1151. //! <b>Effects</b>: Assigns the n copies of val to *this.
  1152. //!
  1153. //! <b>Throws</b>: If memory allocation throws or
  1154. //! T's copy/move constructor/assignment throws.
  1155. //!
  1156. //! <b>Complexity</b>: Linear to n.
  1157. BOOST_CONTAINER_FORCEINLINE void assign(size_type n, const value_type& val)
  1158. { this->assign(cvalue_iterator(val, n), cvalue_iterator()); }
  1159. //! <b>Effects</b>: Returns a copy of the internal allocator.
  1160. //!
  1161. //! <b>Throws</b>: If allocator's copy constructor throws.
  1162. //!
  1163. //! <b>Complexity</b>: Constant.
  1164. allocator_type get_allocator() const BOOST_NOEXCEPT_OR_NOTHROW
  1165. { return this->m_holder.alloc(); }
  1166. //! <b>Effects</b>: Returns a reference to the internal allocator.
  1167. //!
  1168. //! <b>Throws</b>: Nothing
  1169. //!
  1170. //! <b>Complexity</b>: Constant.
  1171. //!
  1172. //! <b>Note</b>: Non-standard extension.
  1173. BOOST_CONTAINER_FORCEINLINE stored_allocator_type &get_stored_allocator() BOOST_NOEXCEPT_OR_NOTHROW
  1174. { return this->m_holder.alloc(); }
  1175. //! <b>Effects</b>: Returns a reference to the internal allocator.
  1176. //!
  1177. //! <b>Throws</b>: Nothing
  1178. //!
  1179. //! <b>Complexity</b>: Constant.
  1180. //!
  1181. //! <b>Note</b>: Non-standard extension.
  1182. BOOST_CONTAINER_FORCEINLINE const stored_allocator_type &get_stored_allocator() const BOOST_NOEXCEPT_OR_NOTHROW
  1183. { return this->m_holder.alloc(); }
  1184. //////////////////////////////////////////////
  1185. //
  1186. // iterators
  1187. //
  1188. //////////////////////////////////////////////
  1189. //! <b>Effects</b>: Returns an iterator to the first element contained in the vector.
  1190. //!
  1191. //! <b>Throws</b>: Nothing.
  1192. //!
  1193. //! <b>Complexity</b>: Constant.
  1194. BOOST_CONTAINER_FORCEINLINE iterator begin() BOOST_NOEXCEPT_OR_NOTHROW
  1195. { return iterator(this->m_holder.start()); }
  1196. //! <b>Effects</b>: Returns a const_iterator to the first element contained in the vector.
  1197. //!
  1198. //! <b>Throws</b>: Nothing.
  1199. //!
  1200. //! <b>Complexity</b>: Constant.
  1201. BOOST_CONTAINER_FORCEINLINE const_iterator begin() const BOOST_NOEXCEPT_OR_NOTHROW
  1202. { return const_iterator(this->m_holder.start()); }
  1203. //! <b>Effects</b>: Returns an iterator to the end of the vector.
  1204. //!
  1205. //! <b>Throws</b>: Nothing.
  1206. //!
  1207. //! <b>Complexity</b>: Constant.
  1208. BOOST_CONTAINER_FORCEINLINE iterator end() BOOST_NOEXCEPT_OR_NOTHROW
  1209. {
  1210. pointer const bg = this->m_holder.start();
  1211. size_type const sz = this->m_holder.m_size;
  1212. return iterator(BOOST_LIKELY(sz) ? bg + sz : bg); //Avoid UB on null-pointer arithmetic
  1213. }
  1214. //! <b>Effects</b>: Returns a const_iterator to the end of the vector.
  1215. //!
  1216. //! <b>Throws</b>: Nothing.
  1217. //!
  1218. //! <b>Complexity</b>: Constant.
  1219. BOOST_CONTAINER_FORCEINLINE const_iterator end() const BOOST_NOEXCEPT_OR_NOTHROW
  1220. { return this->cend(); }
  1221. //! <b>Effects</b>: Returns a reverse_iterator pointing to the beginning
  1222. //! of the reversed vector.
  1223. //!
  1224. //! <b>Throws</b>: Nothing.
  1225. //!
  1226. //! <b>Complexity</b>: Constant.
  1227. BOOST_CONTAINER_FORCEINLINE reverse_iterator rbegin() BOOST_NOEXCEPT_OR_NOTHROW
  1228. { return reverse_iterator(this->end()); }
  1229. //! <b>Effects</b>: Returns a const_reverse_iterator pointing to the beginning
  1230. //! of the reversed vector.
  1231. //!
  1232. //! <b>Throws</b>: Nothing.
  1233. //!
  1234. //! <b>Complexity</b>: Constant.
  1235. BOOST_CONTAINER_FORCEINLINE const_reverse_iterator rbegin() const BOOST_NOEXCEPT_OR_NOTHROW
  1236. { return this->crbegin(); }
  1237. //! <b>Effects</b>: Returns a reverse_iterator pointing to the end
  1238. //! of the reversed vector.
  1239. //!
  1240. //! <b>Throws</b>: Nothing.
  1241. //!
  1242. //! <b>Complexity</b>: Constant.
  1243. BOOST_CONTAINER_FORCEINLINE reverse_iterator rend() BOOST_NOEXCEPT_OR_NOTHROW
  1244. { return reverse_iterator(this->begin()); }
  1245. //! <b>Effects</b>: Returns a const_reverse_iterator pointing to the end
  1246. //! of the reversed vector.
  1247. //!
  1248. //! <b>Throws</b>: Nothing.
  1249. //!
  1250. //! <b>Complexity</b>: Constant.
  1251. BOOST_CONTAINER_FORCEINLINE const_reverse_iterator rend() const BOOST_NOEXCEPT_OR_NOTHROW
  1252. { return this->crend(); }
  1253. //! <b>Effects</b>: Returns a const_iterator to the first element contained in the vector.
  1254. //!
  1255. //! <b>Throws</b>: Nothing.
  1256. //!
  1257. //! <b>Complexity</b>: Constant.
  1258. BOOST_CONTAINER_FORCEINLINE const_iterator cbegin() const BOOST_NOEXCEPT_OR_NOTHROW
  1259. { return const_iterator(this->m_holder.start()); }
  1260. //! <b>Effects</b>: Returns a const_iterator to the end of the vector.
  1261. //!
  1262. //! <b>Throws</b>: Nothing.
  1263. //!
  1264. //! <b>Complexity</b>: Constant.
  1265. BOOST_CONTAINER_FORCEINLINE const_iterator cend() const BOOST_NOEXCEPT_OR_NOTHROW
  1266. {
  1267. pointer const bg = this->m_holder.start();
  1268. size_type const sz = this->m_holder.m_size;
  1269. return const_iterator(BOOST_LIKELY(sz) ? bg + sz : bg); //Avoid UB on null-pointer arithmetic
  1270. }
  1271. //{ return const_iterator(this->m_holder.start() + this->m_holder.m_size); }
  1272. //! <b>Effects</b>: Returns a const_reverse_iterator pointing to the beginning
  1273. //! of the reversed vector.
  1274. //!
  1275. //! <b>Throws</b>: Nothing.
  1276. //!
  1277. //! <b>Complexity</b>: Constant.
  1278. BOOST_CONTAINER_FORCEINLINE const_reverse_iterator crbegin() const BOOST_NOEXCEPT_OR_NOTHROW
  1279. { return const_reverse_iterator(this->end());}
  1280. //! <b>Effects</b>: Returns a const_reverse_iterator pointing to the end
  1281. //! of the reversed vector.
  1282. //!
  1283. //! <b>Throws</b>: Nothing.
  1284. //!
  1285. //! <b>Complexity</b>: Constant.
  1286. BOOST_CONTAINER_FORCEINLINE const_reverse_iterator crend() const BOOST_NOEXCEPT_OR_NOTHROW
  1287. { return const_reverse_iterator(this->begin()); }
  1288. //////////////////////////////////////////////
  1289. //
  1290. // capacity
  1291. //
  1292. //////////////////////////////////////////////
  1293. //! <b>Effects</b>: Returns true if the vector contains no elements.
  1294. //!
  1295. //! <b>Throws</b>: Nothing.
  1296. //!
  1297. //! <b>Complexity</b>: Constant.
  1298. BOOST_CONTAINER_FORCEINLINE bool empty() const BOOST_NOEXCEPT_OR_NOTHROW
  1299. { return !this->m_holder.m_size; }
  1300. //! <b>Effects</b>: Returns the number of the elements contained in the vector.
  1301. //!
  1302. //! <b>Throws</b>: Nothing.
  1303. //!
  1304. //! <b>Complexity</b>: Constant.
  1305. BOOST_CONTAINER_FORCEINLINE size_type size() const BOOST_NOEXCEPT_OR_NOTHROW
  1306. { return this->m_holder.m_size; }
  1307. //! <b>Effects</b>: Returns the largest possible size of the vector.
  1308. //!
  1309. //! <b>Throws</b>: Nothing.
  1310. //!
  1311. //! <b>Complexity</b>: Constant.
  1312. BOOST_CONTAINER_FORCEINLINE size_type max_size() const BOOST_NOEXCEPT_OR_NOTHROW
  1313. { return allocator_traits_type::max_size(this->m_holder.alloc()); }
  1314. //! <b>Effects</b>: Inserts or erases elements at the end such that
  1315. //! the size becomes n. New elements are value initialized.
  1316. //!
  1317. //! <b>Throws</b>: If memory allocation throws, or T's copy/move or value initialization throws.
  1318. //!
  1319. //! <b>Complexity</b>: Linear to the difference between size() and new_size.
  1320. void resize(size_type new_size)
  1321. { this->priv_resize(new_size, value_init); }
  1322. //! <b>Effects</b>: Inserts or erases elements at the end such that
  1323. //! the size becomes n. New elements are default initialized.
  1324. //!
  1325. //! <b>Throws</b>: If memory allocation throws, or T's copy/move or default initialization throws.
  1326. //!
  1327. //! <b>Complexity</b>: Linear to the difference between size() and new_size.
  1328. //!
  1329. //! <b>Note</b>: Non-standard extension
  1330. void resize(size_type new_size, default_init_t)
  1331. { this->priv_resize(new_size, default_init); }
  1332. //! <b>Effects</b>: Inserts or erases elements at the end such that
  1333. //! the size becomes n. New elements are copy constructed from x.
  1334. //!
  1335. //! <b>Throws</b>: If memory allocation throws, or T's copy/move constructor throws.
  1336. //!
  1337. //! <b>Complexity</b>: Linear to the difference between size() and new_size.
  1338. void resize(size_type new_size, const T& x)
  1339. { this->priv_resize(new_size, x); }
  1340. //! <b>Effects</b>: Number of elements for which memory has been allocated.
  1341. //! capacity() is always greater than or equal to size().
  1342. //!
  1343. //! <b>Throws</b>: Nothing.
  1344. //!
  1345. //! <b>Complexity</b>: Constant.
  1346. BOOST_CONTAINER_FORCEINLINE size_type capacity() const BOOST_NOEXCEPT_OR_NOTHROW
  1347. { return this->m_holder.capacity(); }
  1348. //! <b>Effects</b>: If n is less than or equal to capacity(), this call has no
  1349. //! effect. Otherwise, it is a request for allocation of additional memory.
  1350. //! If the request is successful, then capacity() is greater than or equal to
  1351. //! n; otherwise, capacity() is unchanged. In either case, size() is unchanged.
  1352. //!
  1353. //! <b>Throws</b>: If memory allocation allocation throws or T's copy/move constructor throws.
  1354. BOOST_CONTAINER_FORCEINLINE void reserve(size_type new_cap)
  1355. {
  1356. if (this->capacity() < new_cap){
  1357. this->priv_reserve_no_capacity(new_cap, alloc_version());
  1358. }
  1359. }
  1360. //! <b>Effects</b>: Tries to deallocate the excess of memory created
  1361. //! with previous allocations. The size of the vector is unchanged
  1362. //!
  1363. //! <b>Throws</b>: If memory allocation throws, or T's copy/move constructor throws.
  1364. //!
  1365. //! <b>Complexity</b>: Linear to size().
  1366. BOOST_CONTAINER_FORCEINLINE void shrink_to_fit()
  1367. { this->priv_shrink_to_fit(alloc_version()); }
  1368. //////////////////////////////////////////////
  1369. //
  1370. // element access
  1371. //
  1372. //////////////////////////////////////////////
  1373. //! <b>Requires</b>: !empty()
  1374. //!
  1375. //! <b>Effects</b>: Returns a reference to the first
  1376. //! element of the container.
  1377. //!
  1378. //! <b>Throws</b>: Nothing.
  1379. //!
  1380. //! <b>Complexity</b>: Constant.
  1381. reference front() BOOST_NOEXCEPT_OR_NOTHROW
  1382. {
  1383. BOOST_ASSERT(!this->empty());
  1384. return *this->m_holder.start();
  1385. }
  1386. //! <b>Requires</b>: !empty()
  1387. //!
  1388. //! <b>Effects</b>: Returns a const reference to the first
  1389. //! element of the container.
  1390. //!
  1391. //! <b>Throws</b>: Nothing.
  1392. //!
  1393. //! <b>Complexity</b>: Constant.
  1394. const_reference front() const BOOST_NOEXCEPT_OR_NOTHROW
  1395. {
  1396. BOOST_ASSERT(!this->empty());
  1397. return *this->m_holder.start();
  1398. }
  1399. //! <b>Requires</b>: !empty()
  1400. //!
  1401. //! <b>Effects</b>: Returns a reference to the last
  1402. //! element of the container.
  1403. //!
  1404. //! <b>Throws</b>: Nothing.
  1405. //!
  1406. //! <b>Complexity</b>: Constant.
  1407. reference back() BOOST_NOEXCEPT_OR_NOTHROW
  1408. {
  1409. BOOST_ASSERT(!this->empty());
  1410. return this->m_holder.start()[this->m_holder.m_size - 1];
  1411. }
  1412. //! <b>Requires</b>: !empty()
  1413. //!
  1414. //! <b>Effects</b>: Returns a const reference to the last
  1415. //! element of the container.
  1416. //!
  1417. //! <b>Throws</b>: Nothing.
  1418. //!
  1419. //! <b>Complexity</b>: Constant.
  1420. const_reference back() const BOOST_NOEXCEPT_OR_NOTHROW
  1421. {
  1422. BOOST_ASSERT(!this->empty());
  1423. return this->m_holder.start()[this->m_holder.m_size - 1];
  1424. }
  1425. //! <b>Requires</b>: size() > n.
  1426. //!
  1427. //! <b>Effects</b>: Returns a reference to the nth element
  1428. //! from the beginning of the container.
  1429. //!
  1430. //! <b>Throws</b>: Nothing.
  1431. //!
  1432. //! <b>Complexity</b>: Constant.
  1433. reference operator[](size_type n) BOOST_NOEXCEPT_OR_NOTHROW
  1434. {
  1435. BOOST_ASSERT(this->m_holder.m_size > n);
  1436. return this->m_holder.start()[n];
  1437. }
  1438. //! <b>Requires</b>: size() > n.
  1439. //!
  1440. //! <b>Effects</b>: Returns a const reference to the nth element
  1441. //! from the beginning of the container.
  1442. //!
  1443. //! <b>Throws</b>: Nothing.
  1444. //!
  1445. //! <b>Complexity</b>: Constant.
  1446. const_reference operator[](size_type n) const BOOST_NOEXCEPT_OR_NOTHROW
  1447. {
  1448. BOOST_ASSERT(this->m_holder.m_size > n);
  1449. return this->m_holder.start()[n];
  1450. }
  1451. //! <b>Requires</b>: size() >= n.
  1452. //!
  1453. //! <b>Effects</b>: Returns an iterator to the nth element
  1454. //! from the beginning of the container. Returns end()
  1455. //! if n == size().
  1456. //!
  1457. //! <b>Throws</b>: Nothing.
  1458. //!
  1459. //! <b>Complexity</b>: Constant.
  1460. //!
  1461. //! <b>Note</b>: Non-standard extension
  1462. iterator nth(size_type n) BOOST_NOEXCEPT_OR_NOTHROW
  1463. {
  1464. BOOST_ASSERT(this->m_holder.m_size >= n);
  1465. return iterator(this->m_holder.start()+n);
  1466. }
  1467. //! <b>Requires</b>: size() >= n.
  1468. //!
  1469. //! <b>Effects</b>: Returns a const_iterator to the nth element
  1470. //! from the beginning of the container. Returns end()
  1471. //! if n == size().
  1472. //!
  1473. //! <b>Throws</b>: Nothing.
  1474. //!
  1475. //! <b>Complexity</b>: Constant.
  1476. //!
  1477. //! <b>Note</b>: Non-standard extension
  1478. const_iterator nth(size_type n) const BOOST_NOEXCEPT_OR_NOTHROW
  1479. {
  1480. BOOST_ASSERT(this->m_holder.m_size >= n);
  1481. return const_iterator(this->m_holder.start()+n);
  1482. }
  1483. //! <b>Requires</b>: begin() <= p <= end().
  1484. //!
  1485. //! <b>Effects</b>: Returns the index of the element pointed by p
  1486. //! and size() if p == end().
  1487. //!
  1488. //! <b>Throws</b>: Nothing.
  1489. //!
  1490. //! <b>Complexity</b>: Constant.
  1491. //!
  1492. //! <b>Note</b>: Non-standard extension
  1493. size_type index_of(iterator p) BOOST_NOEXCEPT_OR_NOTHROW
  1494. {
  1495. //Range check assert done in priv_index_of
  1496. return this->priv_index_of(vector_iterator_get_ptr(p));
  1497. }
  1498. //! <b>Requires</b>: begin() <= p <= end().
  1499. //!
  1500. //! <b>Effects</b>: Returns the index of the element pointed by p
  1501. //! and size() if p == end().
  1502. //!
  1503. //! <b>Throws</b>: Nothing.
  1504. //!
  1505. //! <b>Complexity</b>: Constant.
  1506. //!
  1507. //! <b>Note</b>: Non-standard extension
  1508. size_type index_of(const_iterator p) const BOOST_NOEXCEPT_OR_NOTHROW
  1509. {
  1510. //Range check assert done in priv_index_of
  1511. return this->priv_index_of(vector_iterator_get_ptr(p));
  1512. }
  1513. //! <b>Requires</b>: size() > n.
  1514. //!
  1515. //! <b>Effects</b>: Returns a reference to the nth element
  1516. //! from the beginning of the container.
  1517. //!
  1518. //! <b>Throws</b>: std::range_error if n >= size()
  1519. //!
  1520. //! <b>Complexity</b>: Constant.
  1521. reference at(size_type n)
  1522. {
  1523. this->priv_throw_if_out_of_range(n);
  1524. return this->m_holder.start()[n];
  1525. }
  1526. //! <b>Requires</b>: size() > n.
  1527. //!
  1528. //! <b>Effects</b>: Returns a const reference to the nth element
  1529. //! from the beginning of the container.
  1530. //!
  1531. //! <b>Throws</b>: std::range_error if n >= size()
  1532. //!
  1533. //! <b>Complexity</b>: Constant.
  1534. const_reference at(size_type n) const
  1535. {
  1536. this->priv_throw_if_out_of_range(n);
  1537. return this->m_holder.start()[n];
  1538. }
  1539. //////////////////////////////////////////////
  1540. //
  1541. // data access
  1542. //
  1543. //////////////////////////////////////////////
  1544. //! <b>Returns</b>: A pointer such that [data(),data() + size()) is a valid range.
  1545. //! For a non-empty vector, data() == &front().
  1546. //!
  1547. //! <b>Throws</b>: Nothing.
  1548. //!
  1549. //! <b>Complexity</b>: Constant.
  1550. T* data() BOOST_NOEXCEPT_OR_NOTHROW
  1551. { return this->priv_raw_begin(); }
  1552. //! <b>Returns</b>: A pointer such that [data(),data() + size()) is a valid range.
  1553. //! For a non-empty vector, data() == &front().
  1554. //!
  1555. //! <b>Throws</b>: Nothing.
  1556. //!
  1557. //! <b>Complexity</b>: Constant.
  1558. const T * data() const BOOST_NOEXCEPT_OR_NOTHROW
  1559. { return this->priv_raw_begin(); }
  1560. //////////////////////////////////////////////
  1561. //
  1562. // modifiers
  1563. //
  1564. //////////////////////////////////////////////
  1565. #if !defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES) || defined(BOOST_CONTAINER_DOXYGEN_INVOKED)
  1566. //! <b>Effects</b>: Inserts an object of type T constructed with
  1567. //! std::forward<Args>(args)... in the end of the vector.
  1568. //!
  1569. //! <b>Returns</b>: A reference to the created object.
  1570. //!
  1571. //! <b>Throws</b>: If memory allocation throws or the in-place constructor throws or
  1572. //! T's copy/move constructor throws.
  1573. //!
  1574. //! <b>Complexity</b>: Amortized constant time.
  1575. template<class ...Args>
  1576. BOOST_CONTAINER_FORCEINLINE reference emplace_back(BOOST_FWD_REF(Args)...args)
  1577. {
  1578. if (BOOST_LIKELY(this->room_enough())){
  1579. //There is more memory, just construct a new object at the end
  1580. T* const p = this->priv_raw_end();
  1581. allocator_traits_type::construct(this->m_holder.alloc(), p, ::boost::forward<Args>(args)...);
  1582. ++this->m_holder.m_size;
  1583. return *p;
  1584. }
  1585. else{
  1586. typedef dtl::insert_emplace_proxy<allocator_type, T*, Args...> type;
  1587. return *this->priv_forward_range_insert_no_capacity
  1588. (this->back_ptr(), 1, type(::boost::forward<Args>(args)...), alloc_version());
  1589. }
  1590. }
  1591. //! <b>Effects</b>: Inserts an object of type T constructed with
  1592. //! std::forward<Args>(args)... in the end of the vector.
  1593. //!
  1594. //! <b>Throws</b>: If the in-place constructor throws.
  1595. //!
  1596. //! <b>Complexity</b>: Constant time.
  1597. //!
  1598. //! <b>Note</b>: Non-standard extension.
  1599. template<class ...Args>
  1600. BOOST_CONTAINER_FORCEINLINE bool stable_emplace_back(BOOST_FWD_REF(Args)...args)
  1601. {
  1602. const bool is_room_enough = this->room_enough() || (alloc_version::value == 2 && this->m_holder.try_expand_fwd(1u));
  1603. if (BOOST_LIKELY(is_room_enough)){
  1604. //There is more memory, just construct a new object at the end
  1605. allocator_traits_type::construct(this->m_holder.alloc(), this->priv_raw_end(), ::boost::forward<Args>(args)...);
  1606. ++this->m_holder.m_size;
  1607. }
  1608. return is_room_enough;
  1609. }
  1610. //! <b>Requires</b>: position must be a valid iterator of *this.
  1611. //!
  1612. //! <b>Effects</b>: Inserts an object of type T constructed with
  1613. //! std::forward<Args>(args)... before position
  1614. //!
  1615. //! <b>Throws</b>: If memory allocation throws or the in-place constructor throws or
  1616. //! T's copy/move constructor/assignment throws.
  1617. //!
  1618. //! <b>Complexity</b>: If position is end(), amortized constant time
  1619. //! Linear time otherwise.
  1620. template<class ...Args>
  1621. iterator emplace(const_iterator position, BOOST_FWD_REF(Args) ...args)
  1622. {
  1623. BOOST_ASSERT(this->priv_in_range_or_end(position));
  1624. //Just call more general insert(pos, size, value) and return iterator
  1625. typedef dtl::insert_emplace_proxy<allocator_type, T*, Args...> type;
  1626. return this->priv_forward_range_insert( vector_iterator_get_ptr(position), 1
  1627. , type(::boost::forward<Args>(args)...));
  1628. }
  1629. #else // !defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES)
  1630. #define BOOST_CONTAINER_VECTOR_EMPLACE_CODE(N) \
  1631. BOOST_MOVE_TMPL_LT##N BOOST_MOVE_CLASS##N BOOST_MOVE_GT##N \
  1632. BOOST_CONTAINER_FORCEINLINE reference emplace_back(BOOST_MOVE_UREF##N)\
  1633. {\
  1634. if (BOOST_LIKELY(this->room_enough())){\
  1635. T* const p = this->priv_raw_end();\
  1636. allocator_traits_type::construct (this->m_holder.alloc()\
  1637. , this->priv_raw_end() BOOST_MOVE_I##N BOOST_MOVE_FWD##N);\
  1638. ++this->m_holder.m_size;\
  1639. return *p;\
  1640. }\
  1641. else{\
  1642. typedef dtl::insert_emplace_proxy_arg##N<allocator_type, T* BOOST_MOVE_I##N BOOST_MOVE_TARG##N> type;\
  1643. return *this->priv_forward_range_insert_no_capacity\
  1644. ( this->back_ptr(), 1, type(BOOST_MOVE_FWD##N), alloc_version());\
  1645. }\
  1646. }\
  1647. \
  1648. BOOST_MOVE_TMPL_LT##N BOOST_MOVE_CLASS##N BOOST_MOVE_GT##N \
  1649. BOOST_CONTAINER_FORCEINLINE bool stable_emplace_back(BOOST_MOVE_UREF##N)\
  1650. {\
  1651. const bool is_room_enough = this->room_enough() || (alloc_version::value == 2 && this->m_holder.try_expand_fwd(1u));\
  1652. if (BOOST_LIKELY(is_room_enough)){\
  1653. allocator_traits_type::construct (this->m_holder.alloc()\
  1654. , this->priv_raw_end() BOOST_MOVE_I##N BOOST_MOVE_FWD##N);\
  1655. ++this->m_holder.m_size;\
  1656. }\
  1657. return is_room_enough;\
  1658. }\
  1659. \
  1660. BOOST_MOVE_TMPL_LT##N BOOST_MOVE_CLASS##N BOOST_MOVE_GT##N \
  1661. iterator emplace(const_iterator pos BOOST_MOVE_I##N BOOST_MOVE_UREF##N)\
  1662. {\
  1663. BOOST_ASSERT(this->priv_in_range_or_end(pos));\
  1664. typedef dtl::insert_emplace_proxy_arg##N<allocator_type, T* BOOST_MOVE_I##N BOOST_MOVE_TARG##N> type;\
  1665. return this->priv_forward_range_insert(vector_iterator_get_ptr(pos), 1, type(BOOST_MOVE_FWD##N));\
  1666. }\
  1667. //
  1668. BOOST_MOVE_ITERATE_0TO9(BOOST_CONTAINER_VECTOR_EMPLACE_CODE)
  1669. #undef BOOST_CONTAINER_VECTOR_EMPLACE_CODE
  1670. #endif
  1671. #if defined(BOOST_CONTAINER_DOXYGEN_INVOKED)
  1672. //! <b>Effects</b>: Inserts a copy of x at the end of the vector.
  1673. //!
  1674. //! <b>Throws</b>: If memory allocation throws or
  1675. //! T's copy/move constructor throws.
  1676. //!
  1677. //! <b>Complexity</b>: Amortized constant time.
  1678. void push_back(const T &x);
  1679. //! <b>Effects</b>: Constructs a new element in the end of the vector
  1680. //! and moves the resources of x to this new element.
  1681. //!
  1682. //! <b>Throws</b>: If memory allocation throws or
  1683. //! T's copy/move constructor throws.
  1684. //!
  1685. //! <b>Complexity</b>: Amortized constant time.
  1686. void push_back(T &&x);
  1687. #else
  1688. BOOST_MOVE_CONVERSION_AWARE_CATCH(push_back, T, void, priv_push_back)
  1689. #endif
  1690. #if defined(BOOST_CONTAINER_DOXYGEN_INVOKED)
  1691. //! <b>Requires</b>: position must be a valid iterator of *this.
  1692. //!
  1693. //! <b>Effects</b>: Insert a copy of x before position.
  1694. //!
  1695. //! <b>Throws</b>: If memory allocation throws or T's copy/move constructor/assignment throws.
  1696. //!
  1697. //! <b>Complexity</b>: If position is end(), amortized constant time
  1698. //! Linear time otherwise.
  1699. iterator insert(const_iterator position, const T &x);
  1700. //! <b>Requires</b>: position must be a valid iterator of *this.
  1701. //!
  1702. //! <b>Effects</b>: Insert a new element before position with x's resources.
  1703. //!
  1704. //! <b>Throws</b>: If memory allocation throws.
  1705. //!
  1706. //! <b>Complexity</b>: If position is end(), amortized constant time
  1707. //! Linear time otherwise.
  1708. iterator insert(const_iterator position, T &&x);
  1709. #else
  1710. BOOST_MOVE_CONVERSION_AWARE_CATCH_1ARG(insert, T, iterator, priv_insert, const_iterator, const_iterator)
  1711. #endif
  1712. //! <b>Requires</b>: p must be a valid iterator of *this.
  1713. //!
  1714. //! <b>Effects</b>: Insert n copies of x before pos.
  1715. //!
  1716. //! <b>Returns</b>: an iterator to the first inserted element or p if n is 0.
  1717. //!
  1718. //! <b>Throws</b>: If memory allocation throws or T's copy/move constructor throws.
  1719. //!
  1720. //! <b>Complexity</b>: Linear to n.
  1721. iterator insert(const_iterator p, size_type n, const T& x)
  1722. {
  1723. BOOST_ASSERT(this->priv_in_range_or_end(p));
  1724. dtl::insert_n_copies_proxy<allocator_type, T*> proxy(x);
  1725. return this->priv_forward_range_insert(vector_iterator_get_ptr(p), n, proxy);
  1726. }
  1727. //! <b>Requires</b>: p must be a valid iterator of *this.
  1728. //!
  1729. //! <b>Effects</b>: Insert a copy of the [first, last) range before pos.
  1730. //!
  1731. //! <b>Returns</b>: an iterator to the first inserted element or pos if first == last.
  1732. //!
  1733. //! <b>Throws</b>: If memory allocation throws, T's constructor from a
  1734. //! dereferenced InpIt throws or T's copy/move constructor/assignment throws.
  1735. //!
  1736. //! <b>Complexity</b>: Linear to boost::container::iterator_distance [first, last).
  1737. template <class InIt>
  1738. iterator insert(const_iterator pos, InIt first, InIt last
  1739. #if !defined(BOOST_CONTAINER_DOXYGEN_INVOKED)
  1740. , typename dtl::disable_if_or
  1741. < void
  1742. , dtl::is_convertible<InIt, size_type>
  1743. , dtl::is_not_input_iterator<InIt>
  1744. >::type * = 0
  1745. #endif
  1746. )
  1747. {
  1748. BOOST_ASSERT(this->priv_in_range_or_end(pos));
  1749. const size_type n_pos = pos - this->cbegin();
  1750. iterator it(vector_iterator_get_ptr(pos));
  1751. for(;first != last; ++first){
  1752. it = this->emplace(it, *first);
  1753. ++it;
  1754. }
  1755. return iterator(this->m_holder.start() + n_pos);
  1756. }
  1757. #if !defined(BOOST_CONTAINER_DOXYGEN_INVOKED)
  1758. template <class FwdIt>
  1759. iterator insert(const_iterator pos, FwdIt first, FwdIt last
  1760. , typename dtl::disable_if_or
  1761. < void
  1762. , dtl::is_convertible<FwdIt, size_type>
  1763. , dtl::is_input_iterator<FwdIt>
  1764. >::type * = 0
  1765. )
  1766. {
  1767. BOOST_ASSERT(this->priv_in_range_or_end(pos));
  1768. dtl::insert_range_proxy<allocator_type, FwdIt, T*> proxy(first);
  1769. return this->priv_forward_range_insert(vector_iterator_get_ptr(pos), boost::container::iterator_distance(first, last), proxy);
  1770. }
  1771. #endif
  1772. //! <b>Requires</b>: p must be a valid iterator of *this. num, must
  1773. //! be equal to boost::container::iterator_distance(first, last)
  1774. //!
  1775. //! <b>Effects</b>: Insert a copy of the [first, last) range before pos.
  1776. //!
  1777. //! <b>Returns</b>: an iterator to the first inserted element or pos if first == last.
  1778. //!
  1779. //! <b>Throws</b>: If memory allocation throws, T's constructor from a
  1780. //! dereferenced InpIt throws or T's copy/move constructor/assignment throws.
  1781. //!
  1782. //! <b>Complexity</b>: Linear to boost::container::iterator_distance [first, last).
  1783. //!
  1784. //! <b>Note</b>: This function avoids a linear operation to calculate boost::container::iterator_distance[first, last)
  1785. //! for forward and bidirectional iterators, and a one by one insertion for input iterators. This is a
  1786. //! a non-standard extension.
  1787. #if !defined(BOOST_CONTAINER_DOXYGEN_INVOKED)
  1788. template <class InIt>
  1789. iterator insert(const_iterator pos, size_type num, InIt first, InIt last)
  1790. {
  1791. BOOST_ASSERT(this->priv_in_range_or_end(pos));
  1792. BOOST_ASSERT(dtl::is_input_iterator<InIt>::value ||
  1793. num == static_cast<size_type>(boost::container::iterator_distance(first, last)));
  1794. (void)last;
  1795. dtl::insert_range_proxy<allocator_type, InIt, T*> proxy(first);
  1796. return this->priv_forward_range_insert(vector_iterator_get_ptr(pos), num, proxy);
  1797. }
  1798. #endif
  1799. #if !defined(BOOST_NO_CXX11_HDR_INITIALIZER_LIST)
  1800. //! <b>Requires</b>: position must be a valid iterator of *this.
  1801. //!
  1802. //! <b>Effects</b>: Insert a copy of the [il.begin(), il.end()) range before position.
  1803. //!
  1804. //! <b>Returns</b>: an iterator to the first inserted element or position if first == last.
  1805. //!
  1806. //! <b>Complexity</b>: Linear to the range [il.begin(), il.end()).
  1807. iterator insert(const_iterator position, std::initializer_list<value_type> il)
  1808. {
  1809. //Assertion done in insert()
  1810. return this->insert(position, il.begin(), il.end());
  1811. }
  1812. #endif
  1813. //! <b>Effects</b>: Removes the last element from the container.
  1814. //!
  1815. //! <b>Throws</b>: Nothing.
  1816. //!
  1817. //! <b>Complexity</b>: Constant time.
  1818. void pop_back() BOOST_NOEXCEPT_OR_NOTHROW
  1819. {
  1820. BOOST_ASSERT(!this->empty());
  1821. //Destroy last element
  1822. this->priv_destroy_last();
  1823. }
  1824. //! <b>Effects</b>: Erases the element at position pos.
  1825. //!
  1826. //! <b>Throws</b>: Nothing.
  1827. //!
  1828. //! <b>Complexity</b>: Linear to the elements between pos and the
  1829. //! last element. Constant if pos is the last element.
  1830. iterator erase(const_iterator position)
  1831. {
  1832. BOOST_ASSERT(this->priv_in_range(position));
  1833. const pointer p = vector_iterator_get_ptr(position);
  1834. T *const pos_ptr = boost::movelib::to_raw_pointer(p);
  1835. T *const beg_ptr = this->priv_raw_begin();
  1836. T *const new_end_ptr = ::boost::container::move(pos_ptr + 1, beg_ptr + this->m_holder.m_size, pos_ptr);
  1837. //Move elements forward and destroy last
  1838. this->priv_destroy_last(pos_ptr != new_end_ptr);
  1839. return iterator(p);
  1840. }
  1841. //! <b>Effects</b>: Erases the elements pointed by [first, last).
  1842. //!
  1843. //! <b>Throws</b>: Nothing.
  1844. //!
  1845. //! <b>Complexity</b>: Linear to the distance between first and last
  1846. //! plus linear to the elements between pos and the last element.
  1847. iterator erase(const_iterator first, const_iterator last)
  1848. {
  1849. if (first != last){
  1850. BOOST_ASSERT(this->priv_in_range(first));
  1851. BOOST_ASSERT(this->priv_in_range_or_end(last));
  1852. BOOST_ASSERT(first < last);
  1853. T* const old_end_ptr = this->priv_raw_end();
  1854. T* const first_ptr = boost::movelib::to_raw_pointer(vector_iterator_get_ptr(first));
  1855. T* const last_ptr = boost::movelib::to_raw_pointer(vector_iterator_get_ptr(last));
  1856. T* const ptr = boost::movelib::to_raw_pointer(boost::container::move(last_ptr, old_end_ptr, first_ptr));
  1857. this->priv_destroy_last_n(old_end_ptr - ptr);
  1858. }
  1859. return iterator(vector_iterator_get_ptr(first));
  1860. }
  1861. //! <b>Effects</b>: Swaps the contents of *this and x.
  1862. //!
  1863. //! <b>Throws</b>: Nothing.
  1864. //!
  1865. //! <b>Complexity</b>: Constant.
  1866. BOOST_CONTAINER_FORCEINLINE void swap(vector& x)
  1867. BOOST_NOEXCEPT_IF( ((allocator_traits_type::propagate_on_container_swap::value
  1868. || allocator_traits_type::is_always_equal::value) &&
  1869. !dtl::is_version<allocator_type, 0>::value))
  1870. {
  1871. this->priv_swap(x, dtl::bool_<dtl::is_version<allocator_type, 0>::value>());
  1872. }
  1873. #ifndef BOOST_CONTAINER_DOXYGEN_INVOKED
  1874. //! <b>Effects</b>: Swaps the contents of *this and x.
  1875. //!
  1876. //! <b>Throws</b>: Nothing.
  1877. //!
  1878. //! <b>Complexity</b>: Linear
  1879. //!
  1880. //! <b>Note</b>: Non-standard extension to support static_vector
  1881. template<class OtherA>
  1882. BOOST_CONTAINER_FORCEINLINE void swap(vector<T, OtherA, Options> & x
  1883. , typename dtl::enable_if_and
  1884. < void
  1885. , dtl::is_version<typename real_allocator<T, OtherA>::type, 0>
  1886. , dtl::is_different<typename real_allocator<T, OtherA>::type, allocator_type>
  1887. >::type * = 0
  1888. )
  1889. { this->m_holder.deep_swap(x.m_holder); }
  1890. #endif //#ifndef BOOST_CONTAINER_DOXYGEN_INVOKED
  1891. //! <b>Effects</b>: Erases all the elements of the vector.
  1892. //!
  1893. //! <b>Throws</b>: Nothing.
  1894. //!
  1895. //! <b>Complexity</b>: Linear to the number of elements in the container.
  1896. BOOST_CONTAINER_FORCEINLINE void clear() BOOST_NOEXCEPT_OR_NOTHROW
  1897. { this->priv_destroy_all(); }
  1898. //! <b>Effects</b>: Returns true if x and y are equal
  1899. //!
  1900. //! <b>Complexity</b>: Linear to the number of elements in the container.
  1901. BOOST_CONTAINER_FORCEINLINE friend bool operator==(const vector& x, const vector& y)
  1902. { return x.size() == y.size() && ::boost::container::algo_equal(x.begin(), x.end(), y.begin()); }
  1903. //! <b>Effects</b>: Returns true if x and y are unequal
  1904. //!
  1905. //! <b>Complexity</b>: Linear to the number of elements in the container.
  1906. BOOST_CONTAINER_FORCEINLINE friend bool operator!=(const vector& x, const vector& y)
  1907. { return !(x == y); }
  1908. //! <b>Effects</b>: Returns true if x is less than y
  1909. //!
  1910. //! <b>Complexity</b>: Linear to the number of elements in the container.
  1911. friend bool operator<(const vector& x, const vector& y)
  1912. {
  1913. const_iterator first1(x.cbegin()), first2(y.cbegin());
  1914. const const_iterator last1(x.cend()), last2(y.cend());
  1915. for ( ; (first1 != last1) && (first2 != last2); ++first1, ++first2 ) {
  1916. if (*first1 < *first2) return true;
  1917. if (*first2 < *first1) return false;
  1918. }
  1919. return (first1 == last1) && (first2 != last2);
  1920. }
  1921. //! <b>Effects</b>: Returns true if x is greater than y
  1922. //!
  1923. //! <b>Complexity</b>: Linear to the number of elements in the container.
  1924. BOOST_CONTAINER_FORCEINLINE friend bool operator>(const vector& x, const vector& y)
  1925. { return y < x; }
  1926. //! <b>Effects</b>: Returns true if x is equal or less than y
  1927. //!
  1928. //! <b>Complexity</b>: Linear to the number of elements in the container.
  1929. BOOST_CONTAINER_FORCEINLINE friend bool operator<=(const vector& x, const vector& y)
  1930. { return !(y < x); }
  1931. //! <b>Effects</b>: Returns true if x is equal or greater than y
  1932. //!
  1933. //! <b>Complexity</b>: Linear to the number of elements in the container.
  1934. BOOST_CONTAINER_FORCEINLINE friend bool operator>=(const vector& x, const vector& y)
  1935. { return !(x < y); }
  1936. //! <b>Effects</b>: x.swap(y)
  1937. //!
  1938. //! <b>Complexity</b>: Constant.
  1939. BOOST_CONTAINER_FORCEINLINE friend void swap(vector& x, vector& y)
  1940. { x.swap(y); }
  1941. #ifndef BOOST_CONTAINER_DOXYGEN_INVOKED
  1942. //! <b>Effects</b>: If n is less than or equal to capacity(), this call has no
  1943. //! effect. Otherwise, it is a request for allocation of additional memory
  1944. //! (memory expansion) that will not invalidate iterators.
  1945. //! If the request is successful, then capacity() is greater than or equal to
  1946. //! n; otherwise, capacity() is unchanged. In either case, size() is unchanged.
  1947. //!
  1948. //! <b>Throws</b>: If memory allocation allocation throws or T's copy/move constructor throws.
  1949. //!
  1950. //! <b>Note</b>: Non-standard extension.
  1951. bool stable_reserve(size_type new_cap)
  1952. {
  1953. const size_type cp = this->capacity();
  1954. return cp >= new_cap || (alloc_version::value == 2 && this->m_holder.try_expand_fwd(new_cap - cp));
  1955. }
  1956. //Absolutely experimental. This function might change, disappear or simply crash!
  1957. template<class BiDirPosConstIt, class BiDirValueIt>
  1958. BOOST_CONTAINER_FORCEINLINE void insert_ordered_at(const size_type element_count, BiDirPosConstIt last_position_it, BiDirValueIt last_value_it)
  1959. {
  1960. typedef vector_insert_ordered_cursor<BiDirPosConstIt, BiDirValueIt> inserter_t;
  1961. return this->priv_insert_ordered_at(element_count, inserter_t(last_position_it, last_value_it));
  1962. }
  1963. template<class InputIt>
  1964. BOOST_CONTAINER_FORCEINLINE void merge(InputIt first, InputIt last)
  1965. { this->merge(first, last, value_less_t()); }
  1966. template<class InputIt, class Compare>
  1967. BOOST_CONTAINER_FORCEINLINE void merge(InputIt first, InputIt last, Compare comp)
  1968. {
  1969. size_type const s = this->size();
  1970. size_type const c = this->capacity();
  1971. size_type n = 0;
  1972. size_type const free_cap = c - s;
  1973. //If not input iterator and new elements don't fit in the remaining capacity, merge in new buffer
  1974. if(!dtl::is_input_iterator<InputIt>::value &&
  1975. free_cap < (n = static_cast<size_type>(boost::container::iterator_distance(first, last)))){
  1976. this->priv_merge_in_new_buffer(first, n, comp, alloc_version());
  1977. }
  1978. else{
  1979. this->insert(this->cend(), first, last);
  1980. T *const raw_beg = this->priv_raw_begin();
  1981. T *const raw_end = this->priv_raw_end();
  1982. T *const raw_pos = raw_beg + s;
  1983. boost::movelib::adaptive_merge(raw_beg, raw_pos, raw_end, comp, raw_end, free_cap - n);
  1984. }
  1985. }
  1986. template<class InputIt>
  1987. BOOST_CONTAINER_FORCEINLINE void merge_unique(InputIt first, InputIt last)
  1988. { this->merge_unique(first, last, value_less_t()); }
  1989. template<class InputIt, class Compare>
  1990. BOOST_CONTAINER_FORCEINLINE void merge_unique(InputIt first, InputIt last, Compare comp)
  1991. {
  1992. size_type const old_size = this->size();
  1993. this->priv_set_difference_back(first, last, comp);
  1994. T *const raw_beg = this->priv_raw_begin();
  1995. T *const raw_end = this->priv_raw_end();
  1996. T *raw_pos = raw_beg + old_size;
  1997. boost::movelib::adaptive_merge(raw_beg, raw_pos, raw_end, comp, raw_end, this->capacity() - this->size());
  1998. }
  1999. private:
  2000. template<class PositionValue>
  2001. void priv_insert_ordered_at(const size_type element_count, PositionValue position_value)
  2002. {
  2003. const size_type old_size_pos = this->size();
  2004. this->reserve(old_size_pos + element_count);
  2005. T* const begin_ptr = this->priv_raw_begin();
  2006. size_type insertions_left = element_count;
  2007. size_type prev_pos = old_size_pos;
  2008. size_type old_hole_size = element_count;
  2009. //Exception rollback. If any copy throws before the hole is filled, values
  2010. //already inserted/copied at the end of the buffer will be destroyed.
  2011. typename value_traits::ArrayDestructor past_hole_values_destroyer
  2012. (begin_ptr + old_size_pos + element_count, this->m_holder.alloc(), size_type(0u));
  2013. //Loop for each insertion backwards, first moving the elements after the insertion point,
  2014. //then inserting the element.
  2015. while(insertions_left){
  2016. --position_value;
  2017. size_type const pos = position_value.get_pos();
  2018. BOOST_ASSERT(pos != size_type(-1) && pos <= old_size_pos && pos <= prev_pos);
  2019. //If needed shift the range after the insertion point and the previous insertion point.
  2020. //Function will take care if the shift crosses the size() boundary, using copy/move
  2021. //or uninitialized copy/move if necessary.
  2022. size_type new_hole_size = (pos != prev_pos)
  2023. ? priv_insert_ordered_at_shift_range(pos, prev_pos, this->size(), insertions_left)
  2024. : old_hole_size
  2025. ;
  2026. if(new_hole_size){
  2027. //The hole was reduced by priv_insert_ordered_at_shift_range so expand exception rollback range backwards
  2028. past_hole_values_destroyer.increment_size_backwards(prev_pos - pos);
  2029. //Insert the new value in the hole
  2030. allocator_traits_type::construct(this->m_holder.alloc(), begin_ptr + pos + insertions_left - 1, position_value.get_val());
  2031. if(--new_hole_size){
  2032. //The hole was reduced by the new insertion by one
  2033. past_hole_values_destroyer.increment_size_backwards(size_type(1u));
  2034. }
  2035. else{
  2036. //Hole was just filled, disable exception rollback and change vector size
  2037. past_hole_values_destroyer.release();
  2038. this->m_holder.m_size += element_count;
  2039. }
  2040. }
  2041. else{
  2042. if(old_hole_size){
  2043. //Hole was just filled by priv_insert_ordered_at_shift_range, disable exception rollback and change vector size
  2044. past_hole_values_destroyer.release();
  2045. this->m_holder.m_size += element_count;
  2046. }
  2047. //Insert the new value in the already constructed range
  2048. begin_ptr[pos + insertions_left - 1] = position_value.get_val();
  2049. }
  2050. --insertions_left;
  2051. old_hole_size = new_hole_size;
  2052. prev_pos = pos;
  2053. }
  2054. }
  2055. template<class InputIt, class Compare>
  2056. void priv_set_difference_back(InputIt first1, InputIt last1, Compare comp)
  2057. {
  2058. T * old_first2 = this->priv_raw_begin();
  2059. T * first2 = old_first2;
  2060. T * last2 = this->priv_raw_end();
  2061. while (first1 != last1) {
  2062. if (first2 == last2){
  2063. this->insert(this->cend(), first1, last1);
  2064. return;
  2065. }
  2066. if (comp(*first1, *first2)) {
  2067. this->emplace_back(*first1);
  2068. T * const raw_begin = this->priv_raw_begin();
  2069. if(old_first2 != raw_begin)
  2070. {
  2071. //Reallocation happened, update range
  2072. first2 = raw_begin + (first2 - old_first2);
  2073. last2 = raw_begin + (last2 - old_first2);
  2074. old_first2 = raw_begin;
  2075. }
  2076. ++first1;
  2077. }
  2078. else {
  2079. if (!comp(*first2, *first1)) {
  2080. ++first1;
  2081. }
  2082. ++first2;
  2083. }
  2084. }
  2085. }
  2086. template<class FwdIt, class Compare>
  2087. BOOST_CONTAINER_FORCEINLINE void priv_merge_in_new_buffer(FwdIt, size_type, Compare, version_0)
  2088. {
  2089. alloc_holder_t::on_capacity_overflow();
  2090. }
  2091. template<class FwdIt, class Compare, class Version>
  2092. void priv_merge_in_new_buffer(FwdIt first, size_type n, Compare comp, Version)
  2093. {
  2094. size_type const new_size = this->size() + n;
  2095. size_type new_cap = new_size;
  2096. pointer p = pointer();
  2097. pointer const new_storage = this->m_holder.allocation_command(allocate_new, new_size, new_cap, p);
  2098. BOOST_ASSERT((new_cap >= this->size() ) && (new_cap - this->size()) >= n);
  2099. allocator_type &a = this->m_holder.alloc();
  2100. typename value_traits::ArrayDeallocator new_buffer_deallocator(new_storage, a, new_cap);
  2101. typename value_traits::ArrayDestructor new_values_destroyer(new_storage, a, 0u);
  2102. T* pbeg = this->priv_raw_begin();
  2103. size_type const old_size = this->size();
  2104. T* const pend = pbeg + old_size;
  2105. T* d_first = boost::movelib::to_raw_pointer(new_storage);
  2106. size_type added = n;
  2107. //Merge in new buffer loop
  2108. while(1){
  2109. if(!n) {
  2110. ::boost::container::uninitialized_move_alloc(this->m_holder.alloc(), pbeg, pend, d_first);
  2111. break;
  2112. }
  2113. else if(pbeg == pend) {
  2114. ::boost::container::uninitialized_move_alloc_n(this->m_holder.alloc(), first, n, d_first);
  2115. break;
  2116. }
  2117. //maintain stability moving external values only if they are strictly less
  2118. else if(comp(*first, *pbeg)) {
  2119. allocator_traits_type::construct( this->m_holder.alloc(), d_first, *first );
  2120. new_values_destroyer.increment_size(1u);
  2121. ++first;
  2122. --n;
  2123. ++d_first;
  2124. }
  2125. else{
  2126. allocator_traits_type::construct( this->m_holder.alloc(), d_first, boost::move(*pbeg) );
  2127. new_values_destroyer.increment_size(1u);
  2128. ++pbeg;
  2129. ++d_first;
  2130. }
  2131. }
  2132. //Nothrow operations
  2133. pointer const old_p = this->m_holder.start();
  2134. size_type const old_cap = this->m_holder.capacity();
  2135. boost::container::destroy_alloc_n(a, boost::movelib::to_raw_pointer(old_p), old_size);
  2136. if (old_cap > 0) {
  2137. this->m_holder.deallocate(old_p, old_cap);
  2138. }
  2139. this->m_holder.m_size = old_size + added;
  2140. this->m_holder.start(new_storage);
  2141. this->m_holder.capacity(new_cap);
  2142. new_buffer_deallocator.release();
  2143. new_values_destroyer.release();
  2144. }
  2145. BOOST_CONTAINER_FORCEINLINE bool room_enough() const
  2146. { return this->m_holder.m_size < this->m_holder.capacity(); }
  2147. BOOST_CONTAINER_FORCEINLINE pointer back_ptr() const
  2148. { return this->m_holder.start() + this->m_holder.m_size; }
  2149. size_type priv_index_of(pointer p) const
  2150. {
  2151. BOOST_ASSERT(this->m_holder.start() <= p);
  2152. BOOST_ASSERT(p <= (this->m_holder.start()+this->size()));
  2153. return static_cast<size_type>(p - this->m_holder.start());
  2154. }
  2155. template<class OtherA>
  2156. void priv_move_assign(BOOST_RV_REF_BEG vector<T, OtherA, Options> BOOST_RV_REF_END x
  2157. , typename dtl::enable_if_c
  2158. < dtl::is_version<typename real_allocator<T, OtherA>::type, 0>::value >::type * = 0)
  2159. {
  2160. if(!dtl::is_same<typename real_allocator<T, OtherA>::type, allocator_type>::value &&
  2161. this->capacity() < x.size()){
  2162. alloc_holder_t::on_capacity_overflow();
  2163. }
  2164. T* const this_start = this->priv_raw_begin();
  2165. T* const other_start = x.priv_raw_begin();
  2166. const size_type this_sz = m_holder.m_size;
  2167. const size_type other_sz = static_cast<size_type>(x.m_holder.m_size);
  2168. boost::container::move_assign_range_alloc_n(this->m_holder.alloc(), other_start, other_sz, this_start, this_sz);
  2169. this->m_holder.m_size = other_sz;
  2170. }
  2171. template<class OtherA>
  2172. void priv_move_assign(BOOST_RV_REF_BEG vector<T, OtherA, Options> BOOST_RV_REF_END x
  2173. , typename dtl::disable_if_or
  2174. < void
  2175. , dtl::is_version<typename real_allocator<T, OtherA>::type, 0>
  2176. , dtl::is_different<typename real_allocator<T, OtherA>::type, allocator_type>
  2177. >::type * = 0)
  2178. {
  2179. //for move assignment, no aliasing (&x != this) is assumed.
  2180. //x.size() == 0 is allowed for buggy std libraries.
  2181. BOOST_ASSERT(this != &x || x.size() == 0);
  2182. allocator_type &this_alloc = this->m_holder.alloc();
  2183. allocator_type &x_alloc = x.m_holder.alloc();
  2184. const bool propagate_alloc = allocator_traits_type::propagate_on_container_move_assignment::value;
  2185. const bool is_propagable_from_x = is_propagable_from(x_alloc, x.m_holder.start(), this_alloc, propagate_alloc);
  2186. //Resources can be transferred if both allocators are
  2187. //going to be equal after this function (either propagated or already equal)
  2188. if(is_propagable_from_x){
  2189. this->clear();
  2190. if(BOOST_LIKELY(!!this->m_holder.m_start))
  2191. this->m_holder.deallocate(this->m_holder.m_start, this->m_holder.m_capacity);
  2192. this->m_holder.steal_resources(x.m_holder);
  2193. }
  2194. //Else do a one by one move
  2195. else{
  2196. this->assign( boost::make_move_iterator(boost::movelib::iterator_to_raw_pointer(x.begin()))
  2197. , boost::make_move_iterator(boost::movelib::iterator_to_raw_pointer(x.end() ))
  2198. );
  2199. }
  2200. //Move allocator if needed
  2201. dtl::move_alloc(this_alloc, x_alloc, dtl::bool_<propagate_alloc>());
  2202. }
  2203. template<class OtherA>
  2204. void priv_copy_assign(const vector<T, OtherA, Options> &x
  2205. , typename dtl::enable_if_c
  2206. < dtl::is_version<typename real_allocator<T, OtherA>::type, 0>::value >::type * = 0)
  2207. {
  2208. if(!dtl::is_same<typename real_allocator<T, OtherA>::type, allocator_type>::value &&
  2209. this->capacity() < x.size()){
  2210. alloc_holder_t::on_capacity_overflow();
  2211. }
  2212. T* const this_start = this->priv_raw_begin();
  2213. T* const other_start = x.priv_raw_begin();
  2214. const size_type this_sz = m_holder.m_size;
  2215. const size_type other_sz = static_cast<size_type>(x.m_holder.m_size);
  2216. boost::container::copy_assign_range_alloc_n(this->m_holder.alloc(), other_start, other_sz, this_start, this_sz);
  2217. this->m_holder.m_size = other_sz;
  2218. }
  2219. template<class OtherA>
  2220. typename dtl::disable_if_or
  2221. < void
  2222. , dtl::is_version<typename real_allocator<T, OtherA>::type, 0>
  2223. , dtl::is_different<typename real_allocator<T, OtherA>::type, allocator_type>
  2224. >::type
  2225. priv_copy_assign(const vector<T, OtherA, Options> &x)
  2226. {
  2227. allocator_type &this_alloc = this->m_holder.alloc();
  2228. const allocator_type &x_alloc = x.m_holder.alloc();
  2229. dtl::bool_<allocator_traits_type::
  2230. propagate_on_container_copy_assignment::value> flag;
  2231. if(flag && this_alloc != x_alloc){
  2232. this->clear();
  2233. this->shrink_to_fit();
  2234. }
  2235. dtl::assign_alloc(this_alloc, x_alloc, flag);
  2236. this->assign( x.priv_raw_begin(), x.priv_raw_end() );
  2237. }
  2238. template<class Vector> //Template it to avoid it in explicit instantiations
  2239. void priv_swap(Vector &x, dtl::true_type) //version_0
  2240. { this->m_holder.deep_swap(x.m_holder); }
  2241. template<class Vector> //Template it to avoid it in explicit instantiations
  2242. void priv_swap(Vector &x, dtl::false_type) //version_N
  2243. {
  2244. const bool propagate_alloc = allocator_traits_type::propagate_on_container_swap::value;
  2245. if(are_swap_propagable( this->get_stored_allocator(), this->m_holder.start()
  2246. , x.get_stored_allocator(), x.m_holder.start(), propagate_alloc)){
  2247. //Just swap internals
  2248. this->m_holder.swap_resources(x.m_holder);
  2249. }
  2250. else{
  2251. if (BOOST_UNLIKELY(&x == this))
  2252. return;
  2253. //Else swap element by element...
  2254. bool const t_smaller = this->size() < x.size();
  2255. vector &sml = t_smaller ? *this : x;
  2256. vector &big = t_smaller ? x : *this;
  2257. size_type const common_elements = sml.size();
  2258. for(size_type i = 0; i != common_elements; ++i){
  2259. boost::adl_move_swap(sml[i], big[i]);
  2260. }
  2261. //... and move-insert the remaining range
  2262. sml.insert( sml.cend()
  2263. , boost::make_move_iterator(boost::movelib::iterator_to_raw_pointer(big.nth(common_elements)))
  2264. , boost::make_move_iterator(boost::movelib::iterator_to_raw_pointer(big.end()))
  2265. );
  2266. //Destroy remaining elements
  2267. big.erase(big.nth(common_elements), big.cend());
  2268. }
  2269. //And now swap the allocator
  2270. dtl::swap_alloc(this->m_holder.alloc(), x.m_holder.alloc(), dtl::bool_<propagate_alloc>());
  2271. }
  2272. void priv_reserve_no_capacity(size_type, version_0)
  2273. { alloc_holder_t::on_capacity_overflow(); }
  2274. dtl::insert_range_proxy<allocator_type, boost::move_iterator<T*>, T*> priv_dummy_empty_proxy()
  2275. {
  2276. return dtl::insert_range_proxy<allocator_type, boost::move_iterator<T*>, T*>
  2277. (::boost::make_move_iterator((T *)0));
  2278. }
  2279. void priv_reserve_no_capacity(size_type new_cap, version_1)
  2280. {
  2281. //There is not enough memory, allocate a new buffer
  2282. //Pass the hint so that allocators can take advantage of this.
  2283. pointer const p = this->m_holder.allocate(new_cap);
  2284. //We will reuse insert code, so create a dummy input iterator
  2285. this->priv_forward_range_insert_new_allocation
  2286. ( boost::movelib::to_raw_pointer(p), new_cap, this->priv_raw_end(), 0, this->priv_dummy_empty_proxy());
  2287. }
  2288. void priv_reserve_no_capacity(size_type new_cap, version_2)
  2289. {
  2290. //There is not enough memory, allocate a new
  2291. //buffer or expand the old one.
  2292. bool same_buffer_start;
  2293. size_type real_cap = 0;
  2294. pointer reuse(this->m_holder.start());
  2295. pointer const ret(this->m_holder.allocation_command(allocate_new | expand_fwd | expand_bwd, new_cap, real_cap = new_cap, reuse));
  2296. //Check for forward expansion
  2297. same_buffer_start = reuse && this->m_holder.start() == ret;
  2298. if(same_buffer_start){
  2299. #ifdef BOOST_CONTAINER_VECTOR_ALLOC_STATS
  2300. ++this->num_expand_fwd;
  2301. #endif
  2302. this->m_holder.capacity(real_cap);
  2303. }
  2304. else{ //If there is no forward expansion, move objects, we will reuse insertion code
  2305. T * const new_mem = boost::movelib::to_raw_pointer(ret);
  2306. T * const ins_pos = this->priv_raw_end();
  2307. if(reuse){ //Backwards (and possibly forward) expansion
  2308. #ifdef BOOST_CONTAINER_VECTOR_ALLOC_STATS
  2309. ++this->num_expand_bwd;
  2310. #endif
  2311. this->priv_forward_range_insert_expand_backwards
  2312. ( new_mem , real_cap, ins_pos, 0, this->priv_dummy_empty_proxy());
  2313. }
  2314. else{ //New buffer
  2315. #ifdef BOOST_CONTAINER_VECTOR_ALLOC_STATS
  2316. ++this->num_alloc;
  2317. #endif
  2318. this->priv_forward_range_insert_new_allocation
  2319. ( new_mem, real_cap, ins_pos, 0, this->priv_dummy_empty_proxy());
  2320. }
  2321. }
  2322. }
  2323. void priv_destroy_last(const bool moved = false) BOOST_NOEXCEPT_OR_NOTHROW
  2324. {
  2325. (void)moved;
  2326. const bool skip_destructor = value_traits::trivial_dctr || (value_traits::trivial_dctr_after_move && moved);
  2327. if(!skip_destructor){
  2328. value_type* const p = this->priv_raw_end() - 1;
  2329. allocator_traits_type::destroy(this->get_stored_allocator(), p);
  2330. }
  2331. --this->m_holder.m_size;
  2332. }
  2333. void priv_destroy_last_n(const size_type n) BOOST_NOEXCEPT_OR_NOTHROW
  2334. {
  2335. BOOST_ASSERT(n <= this->m_holder.m_size);
  2336. if(!value_traits::trivial_dctr){
  2337. T* const destroy_pos = this->priv_raw_begin() + (this->m_holder.m_size-n);
  2338. boost::container::destroy_alloc_n(this->get_stored_allocator(), destroy_pos, n);
  2339. }
  2340. this->m_holder.m_size -= n;
  2341. }
  2342. template<class InpIt>
  2343. void priv_uninitialized_construct_at_end(InpIt first, InpIt last)
  2344. {
  2345. T* const old_end_pos = this->priv_raw_end();
  2346. T* const new_end_pos = boost::container::uninitialized_copy_alloc(this->m_holder.alloc(), first, last, old_end_pos);
  2347. this->m_holder.m_size += new_end_pos - old_end_pos;
  2348. }
  2349. void priv_destroy_all() BOOST_NOEXCEPT_OR_NOTHROW
  2350. {
  2351. boost::container::destroy_alloc_n
  2352. (this->get_stored_allocator(), this->priv_raw_begin(), this->m_holder.m_size);
  2353. this->m_holder.m_size = 0;
  2354. }
  2355. template<class U>
  2356. iterator priv_insert(const const_iterator &p, BOOST_FWD_REF(U) x)
  2357. {
  2358. BOOST_ASSERT(this->priv_in_range_or_end(p));
  2359. return this->priv_forward_range_insert
  2360. ( vector_iterator_get_ptr(p), 1, dtl::get_insert_value_proxy<T*, allocator_type>(::boost::forward<U>(x)));
  2361. }
  2362. BOOST_CONTAINER_FORCEINLINE dtl::insert_copy_proxy<allocator_type, T*> priv_single_insert_proxy(const T &x)
  2363. { return dtl::insert_copy_proxy<allocator_type, T*> (x); }
  2364. BOOST_CONTAINER_FORCEINLINE dtl::insert_move_proxy<allocator_type, T*> priv_single_insert_proxy(BOOST_RV_REF(T) x)
  2365. { return dtl::insert_move_proxy<allocator_type, T*> (x); }
  2366. template <class U>
  2367. void priv_push_back(BOOST_FWD_REF(U) u)
  2368. {
  2369. if (BOOST_LIKELY(this->room_enough())){
  2370. //There is more memory, just construct a new object at the end
  2371. allocator_traits_type::construct
  2372. ( this->m_holder.alloc(), this->priv_raw_end(), ::boost::forward<U>(u) );
  2373. ++this->m_holder.m_size;
  2374. }
  2375. else{
  2376. this->priv_forward_range_insert_no_capacity
  2377. ( this->back_ptr(), 1
  2378. , this->priv_single_insert_proxy(::boost::forward<U>(u)), alloc_version());
  2379. }
  2380. }
  2381. BOOST_CONTAINER_FORCEINLINE dtl::insert_n_copies_proxy<allocator_type, T*> priv_resize_proxy(const T &x)
  2382. { return dtl::insert_n_copies_proxy<allocator_type, T*>(x); }
  2383. BOOST_CONTAINER_FORCEINLINE dtl::insert_default_initialized_n_proxy<allocator_type, T*> priv_resize_proxy(default_init_t)
  2384. { return dtl::insert_default_initialized_n_proxy<allocator_type, T*>(); }
  2385. BOOST_CONTAINER_FORCEINLINE dtl::insert_value_initialized_n_proxy<allocator_type, T*> priv_resize_proxy(value_init_t)
  2386. { return dtl::insert_value_initialized_n_proxy<allocator_type, T*>(); }
  2387. template <class U>
  2388. void priv_resize(size_type new_size, const U& u)
  2389. {
  2390. const size_type sz = this->size();
  2391. if (new_size < sz){
  2392. //Destroy last elements
  2393. this->priv_destroy_last_n(sz - new_size);
  2394. }
  2395. else{
  2396. const size_type n = new_size - this->size();
  2397. this->priv_forward_range_insert_at_end(n, this->priv_resize_proxy(u), alloc_version());
  2398. }
  2399. }
  2400. BOOST_CONTAINER_FORCEINLINE void priv_shrink_to_fit(version_0) BOOST_NOEXCEPT_OR_NOTHROW
  2401. {}
  2402. void priv_shrink_to_fit(version_1)
  2403. {
  2404. const size_type cp = this->m_holder.capacity();
  2405. if(cp){
  2406. const size_type sz = this->size();
  2407. if(!sz){
  2408. if(BOOST_LIKELY(!!this->m_holder.m_start))
  2409. this->m_holder.deallocate(this->m_holder.m_start, cp);
  2410. this->m_holder.m_start = pointer();
  2411. this->m_holder.m_capacity = 0;
  2412. }
  2413. else if(sz < cp){
  2414. //Allocate a new buffer.
  2415. //Pass the hint so that allocators can take advantage of this.
  2416. pointer const p = this->m_holder.allocate(sz);
  2417. //We will reuse insert code, so create a dummy input iterator
  2418. #ifdef BOOST_CONTAINER_VECTOR_ALLOC_STATS
  2419. ++this->num_alloc;
  2420. #endif
  2421. this->priv_forward_range_insert_new_allocation
  2422. ( boost::movelib::to_raw_pointer(p), sz
  2423. , this->priv_raw_begin(), 0, this->priv_dummy_empty_proxy());
  2424. }
  2425. }
  2426. }
  2427. void priv_shrink_to_fit(version_2) BOOST_NOEXCEPT_OR_NOTHROW
  2428. {
  2429. const size_type cp = this->m_holder.capacity();
  2430. if(cp){
  2431. const size_type sz = this->size();
  2432. if(!sz){
  2433. if(BOOST_LIKELY(!!this->m_holder.m_start))
  2434. this->m_holder.deallocate(this->m_holder.m_start, cp);
  2435. this->m_holder.m_start = pointer();
  2436. this->m_holder.m_capacity = 0;
  2437. }
  2438. else{
  2439. size_type received_size = sz;
  2440. pointer reuse(this->m_holder.start());
  2441. if(this->m_holder.allocation_command
  2442. (shrink_in_place | nothrow_allocation, cp, received_size, reuse)){
  2443. this->m_holder.capacity(received_size);
  2444. #ifdef BOOST_CONTAINER_VECTOR_ALLOC_STATS
  2445. ++this->num_shrink;
  2446. #endif
  2447. }
  2448. }
  2449. }
  2450. }
  2451. template <class InsertionProxy>
  2452. iterator priv_forward_range_insert_no_capacity
  2453. (const pointer &pos, const size_type, const InsertionProxy , version_0)
  2454. {
  2455. alloc_holder_t::on_capacity_overflow();
  2456. return iterator(pos);
  2457. }
  2458. template <class InsertionProxy>
  2459. iterator priv_forward_range_insert_no_capacity
  2460. (const pointer &pos, const size_type n, const InsertionProxy insert_range_proxy, version_1)
  2461. {
  2462. //Check if we have enough memory or try to expand current memory
  2463. const size_type n_pos = pos - this->m_holder.start();
  2464. T *const raw_pos = boost::movelib::to_raw_pointer(pos);
  2465. const size_type new_cap = this->m_holder.template next_capacity<growth_factor_type>(n);
  2466. //Pass the hint so that allocators can take advantage of this.
  2467. T * const new_buf = boost::movelib::to_raw_pointer(this->m_holder.allocate(new_cap));
  2468. #ifdef BOOST_CONTAINER_VECTOR_ALLOC_STATS
  2469. ++this->num_alloc;
  2470. #endif
  2471. this->priv_forward_range_insert_new_allocation
  2472. ( new_buf, new_cap, raw_pos, n, insert_range_proxy);
  2473. return iterator(this->m_holder.start() + n_pos);
  2474. }
  2475. template <class InsertionProxy>
  2476. iterator priv_forward_range_insert_no_capacity
  2477. (const pointer &pos, const size_type n, const InsertionProxy insert_range_proxy, version_2)
  2478. {
  2479. //Check if we have enough memory or try to expand current memory
  2480. T *const raw_pos = boost::movelib::to_raw_pointer(pos);
  2481. const size_type n_pos = raw_pos - this->priv_raw_begin();
  2482. //There is not enough memory, allocate a new
  2483. //buffer or expand the old one.
  2484. size_type real_cap = this->m_holder.template next_capacity<growth_factor_type>(n);
  2485. pointer reuse(this->m_holder.start());
  2486. pointer const ret (this->m_holder.allocation_command
  2487. (allocate_new | expand_fwd | expand_bwd, this->m_holder.m_size + n, real_cap, reuse));
  2488. //Buffer reallocated
  2489. if(reuse){
  2490. //Forward expansion, delay insertion
  2491. if(this->m_holder.start() == ret){
  2492. #ifdef BOOST_CONTAINER_VECTOR_ALLOC_STATS
  2493. ++this->num_expand_fwd;
  2494. #endif
  2495. this->m_holder.capacity(real_cap);
  2496. //Expand forward
  2497. this->priv_forward_range_insert_expand_forward(raw_pos, n, insert_range_proxy);
  2498. }
  2499. //Backwards (and possibly forward) expansion
  2500. else{
  2501. #ifdef BOOST_CONTAINER_VECTOR_ALLOC_STATS
  2502. ++this->num_expand_bwd;
  2503. #endif
  2504. this->priv_forward_range_insert_expand_backwards
  2505. (boost::movelib::to_raw_pointer(ret), real_cap, raw_pos, n, insert_range_proxy);
  2506. }
  2507. }
  2508. //New buffer
  2509. else{
  2510. #ifdef BOOST_CONTAINER_VECTOR_ALLOC_STATS
  2511. ++this->num_alloc;
  2512. #endif
  2513. this->priv_forward_range_insert_new_allocation
  2514. ( boost::movelib::to_raw_pointer(ret), real_cap, raw_pos, n, insert_range_proxy);
  2515. }
  2516. return iterator(this->m_holder.start() + n_pos);
  2517. }
  2518. template <class InsertionProxy>
  2519. iterator priv_forward_range_insert
  2520. (const pointer &pos, const size_type n, const InsertionProxy insert_range_proxy)
  2521. {
  2522. BOOST_ASSERT(this->m_holder.capacity() >= this->m_holder.m_size);
  2523. //Check if we have enough memory or try to expand current memory
  2524. const size_type remaining = this->m_holder.capacity() - this->m_holder.m_size;
  2525. bool same_buffer_start = n <= remaining;
  2526. if (!same_buffer_start){
  2527. return priv_forward_range_insert_no_capacity(pos, n, insert_range_proxy, alloc_version());
  2528. }
  2529. else{
  2530. //Expand forward
  2531. T *const raw_pos = boost::movelib::to_raw_pointer(pos);
  2532. const size_type n_pos = raw_pos - this->priv_raw_begin();
  2533. this->priv_forward_range_insert_expand_forward(raw_pos, n, insert_range_proxy);
  2534. return iterator(this->m_holder.start() + n_pos);
  2535. }
  2536. }
  2537. template <class InsertionProxy>
  2538. iterator priv_forward_range_insert_at_end
  2539. (const size_type n, const InsertionProxy insert_range_proxy, version_0)
  2540. {
  2541. //Check if we have enough memory or try to expand current memory
  2542. const size_type remaining = this->m_holder.capacity() - this->m_holder.m_size;
  2543. if (n > remaining){
  2544. //This will trigger an error
  2545. alloc_holder_t::on_capacity_overflow();
  2546. }
  2547. this->priv_forward_range_insert_at_end_expand_forward(n, insert_range_proxy);
  2548. return this->end();
  2549. }
  2550. template <class InsertionProxy, class AllocVersion>
  2551. BOOST_CONTAINER_FORCEINLINE iterator priv_forward_range_insert_at_end
  2552. (const size_type n, const InsertionProxy insert_range_proxy, AllocVersion)
  2553. {
  2554. return this->priv_forward_range_insert(this->back_ptr(), n, insert_range_proxy);
  2555. }
  2556. //Takes the range pointed by [first_pos, last_pos) and shifts it to the right
  2557. //by 'shift_count'. 'limit_pos' marks the end of constructed elements.
  2558. //
  2559. //Precondition: first_pos <= last_pos <= limit_pos
  2560. //
  2561. //The shift operation might cross limit_pos so elements to moved beyond limit_pos
  2562. //are uninitialized_moved with an allocator. Other elements are moved.
  2563. //
  2564. //The shift operation might left uninitialized elements after limit_pos
  2565. //and the number of uninitialized elements is returned by the function.
  2566. //
  2567. //Old situation:
  2568. // first_pos last_pos old_limit
  2569. // | | |
  2570. // ____________V_______V__________________V_____________
  2571. //| prefix | range | suffix |raw_mem ~
  2572. //|____________|_______|__________________|_____________~
  2573. //
  2574. //New situation in Case A (hole_size == 0):
  2575. // range is moved through move assignments
  2576. //
  2577. // first_pos last_pos limit_pos
  2578. // | | |
  2579. // ____________V_______V__________________V_____________
  2580. //| prefix' | | | range |suffix'|raw_mem ~
  2581. //|________________+______|___^___|_______|_____________~
  2582. // | |
  2583. // |_>_>_>_>_>^
  2584. //
  2585. //
  2586. //New situation in Case B (hole_size >= 0):
  2587. // range is moved through uninitialized moves
  2588. //
  2589. // first_pos last_pos limit_pos
  2590. // | | |
  2591. // ____________V_______V__________________V________________
  2592. //| prefix' | | | [hole] | range |
  2593. //|_______________________________________|________|___^___|
  2594. // | |
  2595. // |_>_>_>_>_>_>_>_>_>_>_>_>_>_>_>_>_>_^
  2596. //
  2597. //New situation in Case C (hole_size == 0):
  2598. // range is moved through move assignments and uninitialized moves
  2599. //
  2600. // first_pos last_pos limit_pos
  2601. // | | |
  2602. // ____________V_______V__________________V___
  2603. //| prefix' | | | range |
  2604. //|___________________________________|___^___|
  2605. // | |
  2606. // |_>_>_>_>_>_>_>_>_>_>_>^
  2607. size_type priv_insert_ordered_at_shift_range
  2608. (size_type first_pos, size_type last_pos, size_type limit_pos, size_type shift_count)
  2609. {
  2610. BOOST_ASSERT(first_pos <= last_pos);
  2611. BOOST_ASSERT(last_pos <= limit_pos);
  2612. //
  2613. T* const begin_ptr = this->priv_raw_begin();
  2614. T* const first_ptr = begin_ptr + first_pos;
  2615. T* const last_ptr = begin_ptr + last_pos;
  2616. size_type hole_size = 0;
  2617. //Case A:
  2618. if((last_pos + shift_count) <= limit_pos){
  2619. //All move assigned
  2620. boost::container::move_backward(first_ptr, last_ptr, last_ptr + shift_count);
  2621. }
  2622. //Case B:
  2623. else if((first_pos + shift_count) >= limit_pos){
  2624. //All uninitialized_moved
  2625. ::boost::container::uninitialized_move_alloc
  2626. (this->m_holder.alloc(), first_ptr, last_ptr, first_ptr + shift_count);
  2627. hole_size = first_pos + shift_count - limit_pos;
  2628. }
  2629. //Case C:
  2630. else{
  2631. //Some uninitialized_moved
  2632. T* const limit_ptr = begin_ptr + limit_pos;
  2633. T* const boundary_ptr = limit_ptr - shift_count;
  2634. ::boost::container::uninitialized_move_alloc(this->m_holder.alloc(), boundary_ptr, last_ptr, limit_ptr);
  2635. //The rest is move assigned
  2636. boost::container::move_backward(first_ptr, boundary_ptr, limit_ptr);
  2637. }
  2638. return hole_size;
  2639. }
  2640. private:
  2641. BOOST_CONTAINER_FORCEINLINE T *priv_raw_begin() const
  2642. { return boost::movelib::to_raw_pointer(m_holder.start()); }
  2643. BOOST_CONTAINER_FORCEINLINE T* priv_raw_end() const
  2644. { return this->priv_raw_begin() + this->m_holder.m_size; }
  2645. template <class InsertionProxy>
  2646. void priv_forward_range_insert_at_end_expand_forward(const size_type n, InsertionProxy insert_range_proxy)
  2647. {
  2648. T* const old_finish = this->priv_raw_end();
  2649. insert_range_proxy.uninitialized_copy_n_and_update(this->m_holder.alloc(), old_finish, n);
  2650. this->m_holder.m_size += n;
  2651. }
  2652. template <class InsertionProxy>
  2653. void priv_forward_range_insert_expand_forward(T* const pos, const size_type n, InsertionProxy insert_range_proxy)
  2654. {
  2655. //n can't be 0, because there is nothing to do in that case
  2656. if(BOOST_UNLIKELY(!n)) return;
  2657. //There is enough memory
  2658. T* const old_finish = this->priv_raw_end();
  2659. const size_type elems_after = old_finish - pos;
  2660. if (!elems_after){
  2661. insert_range_proxy.uninitialized_copy_n_and_update(this->m_holder.alloc(), old_finish, n);
  2662. this->m_holder.m_size += n;
  2663. }
  2664. else if (elems_after >= n){
  2665. //New elements can be just copied.
  2666. //Move to uninitialized memory last objects
  2667. ::boost::container::uninitialized_move_alloc
  2668. (this->m_holder.alloc(), old_finish - n, old_finish, old_finish);
  2669. this->m_holder.m_size += n;
  2670. //Copy previous to last objects to the initialized end
  2671. boost::container::move_backward(pos, old_finish - n, old_finish);
  2672. //Insert new objects in the pos
  2673. insert_range_proxy.copy_n_and_update(this->m_holder.alloc(), pos, n);
  2674. }
  2675. else {
  2676. //The new elements don't fit in the [pos, end()) range.
  2677. //Copy old [pos, end()) elements to the uninitialized memory (a gap is created)
  2678. ::boost::container::uninitialized_move_alloc(this->m_holder.alloc(), pos, old_finish, pos + n);
  2679. BOOST_TRY{
  2680. //Copy first new elements in pos (gap is still there)
  2681. insert_range_proxy.copy_n_and_update(this->m_holder.alloc(), pos, elems_after);
  2682. //Copy to the beginning of the unallocated zone the last new elements (the gap is closed).
  2683. insert_range_proxy.uninitialized_copy_n_and_update(this->m_holder.alloc(), old_finish, n - elems_after);
  2684. this->m_holder.m_size += n;
  2685. }
  2686. BOOST_CATCH(...){
  2687. boost::container::destroy_alloc_n(this->get_stored_allocator(), pos + n, elems_after);
  2688. BOOST_RETHROW
  2689. }
  2690. BOOST_CATCH_END
  2691. }
  2692. }
  2693. template <class InsertionProxy>
  2694. void priv_forward_range_insert_new_allocation
  2695. (T* const new_start, size_type new_cap, T* const pos, const size_type n, InsertionProxy insert_range_proxy)
  2696. {
  2697. //n can be zero, if we want to reallocate!
  2698. T *new_finish = new_start;
  2699. T *old_finish;
  2700. //Anti-exception rollbacks
  2701. typename value_traits::ArrayDeallocator new_buffer_deallocator(new_start, this->m_holder.alloc(), new_cap);
  2702. typename value_traits::ArrayDestructor new_values_destroyer(new_start, this->m_holder.alloc(), 0u);
  2703. //Initialize with [begin(), pos) old buffer
  2704. //the start of the new buffer
  2705. T * const old_buffer = this->priv_raw_begin();
  2706. if(old_buffer){
  2707. new_finish = ::boost::container::uninitialized_move_alloc
  2708. (this->m_holder.alloc(), this->priv_raw_begin(), pos, old_finish = new_finish);
  2709. new_values_destroyer.increment_size(new_finish - old_finish);
  2710. }
  2711. //Initialize new objects, starting from previous point
  2712. old_finish = new_finish;
  2713. insert_range_proxy.uninitialized_copy_n_and_update(this->m_holder.alloc(), old_finish, n);
  2714. new_finish += n;
  2715. new_values_destroyer.increment_size(new_finish - old_finish);
  2716. //Initialize from the rest of the old buffer,
  2717. //starting from previous point
  2718. if(old_buffer){
  2719. new_finish = ::boost::container::uninitialized_move_alloc
  2720. (this->m_holder.alloc(), pos, old_buffer + this->m_holder.m_size, new_finish);
  2721. //Destroy and deallocate old elements
  2722. //If there is allocated memory, destroy and deallocate
  2723. if(!value_traits::trivial_dctr_after_move)
  2724. boost::container::destroy_alloc_n(this->get_stored_allocator(), old_buffer, this->m_holder.m_size);
  2725. this->m_holder.deallocate(this->m_holder.start(), this->m_holder.capacity());
  2726. }
  2727. this->m_holder.start(new_start);
  2728. this->m_holder.m_size = size_type(new_finish - new_start);
  2729. this->m_holder.capacity(new_cap);
  2730. //All construction successful, disable rollbacks
  2731. new_values_destroyer.release();
  2732. new_buffer_deallocator.release();
  2733. }
  2734. template <class InsertionProxy>
  2735. void priv_forward_range_insert_expand_backwards
  2736. (T* const new_start, const size_type new_capacity,
  2737. T* const pos, const size_type n, InsertionProxy insert_range_proxy)
  2738. {
  2739. //n can be zero to just expand capacity
  2740. //Backup old data
  2741. T* const old_start = this->priv_raw_begin();
  2742. const size_type old_size = this->m_holder.m_size;
  2743. T* const old_finish = old_start + old_size;
  2744. //We can have 8 possibilities:
  2745. const size_type elemsbefore = static_cast<size_type>(pos - old_start);
  2746. const size_type s_before = static_cast<size_type>(old_start - new_start);
  2747. const size_type before_plus_new = elemsbefore + n;
  2748. //Update the vector buffer information to a safe state
  2749. this->m_holder.start(new_start);
  2750. this->m_holder.capacity(new_capacity);
  2751. this->m_holder.m_size = 0;
  2752. //If anything goes wrong, this object will destroy
  2753. //all the old objects to fulfill previous vector state
  2754. typename value_traits::ArrayDestructor old_values_destroyer(old_start, this->m_holder.alloc(), old_size);
  2755. //Check if s_before is big enough to hold the beginning of old data + new data
  2756. if(s_before >= before_plus_new){
  2757. //Copy first old values before pos, after that the new objects
  2758. T *const new_elem_pos =
  2759. ::boost::container::uninitialized_move_alloc(this->m_holder.alloc(), old_start, pos, new_start);
  2760. this->m_holder.m_size = elemsbefore;
  2761. insert_range_proxy.uninitialized_copy_n_and_update(this->m_holder.alloc(), new_elem_pos, n);
  2762. this->m_holder.m_size = before_plus_new;
  2763. const size_type new_size = old_size + n;
  2764. //Check if s_before is so big that even copying the old data + new data
  2765. //there is a gap between the new data and the old data
  2766. if(s_before >= new_size){
  2767. //Old situation:
  2768. // _________________________________________________________
  2769. //| raw_mem | old_begin | old_end |
  2770. //| __________________________________|___________|_________|
  2771. //
  2772. //New situation:
  2773. // _________________________________________________________
  2774. //| old_begin | new | old_end | raw_mem |
  2775. //|___________|__________|_________|________________________|
  2776. //
  2777. //Now initialize the rest of memory with the last old values
  2778. if(before_plus_new != new_size){ //Special case to avoid operations in back insertion
  2779. ::boost::container::uninitialized_move_alloc
  2780. (this->m_holder.alloc(), pos, old_finish, new_start + before_plus_new);
  2781. //All new elements correctly constructed, avoid new element destruction
  2782. this->m_holder.m_size = new_size;
  2783. }
  2784. //Old values destroyed automatically with "old_values_destroyer"
  2785. //when "old_values_destroyer" goes out of scope unless the have trivial
  2786. //destructor after move.
  2787. if(value_traits::trivial_dctr_after_move)
  2788. old_values_destroyer.release();
  2789. }
  2790. //s_before is so big that divides old_end
  2791. else{
  2792. //Old situation:
  2793. // __________________________________________________
  2794. //| raw_mem | old_begin | old_end |
  2795. //| ___________________________|___________|_________|
  2796. //
  2797. //New situation:
  2798. // __________________________________________________
  2799. //| old_begin | new | old_end | raw_mem |
  2800. //|___________|__________|_________|_________________|
  2801. //
  2802. //Now initialize the rest of memory with the last old values
  2803. //All new elements correctly constructed, avoid new element destruction
  2804. const size_type raw_gap = s_before - before_plus_new;
  2805. if(!value_traits::trivial_dctr){
  2806. //Now initialize the rest of s_before memory with the
  2807. //first of elements after new values
  2808. ::boost::container::uninitialized_move_alloc_n
  2809. (this->m_holder.alloc(), pos, raw_gap, new_start + before_plus_new);
  2810. //Now we have a contiguous buffer so program trailing element destruction
  2811. //and update size to the final size.
  2812. old_values_destroyer.shrink_forward(new_size-s_before);
  2813. this->m_holder.m_size = new_size;
  2814. //Now move remaining last objects in the old buffer begin
  2815. T * const remaining_pos = pos + raw_gap;
  2816. if(remaining_pos != old_start){ //Make sure data has to be moved
  2817. ::boost::container::move(remaining_pos, old_finish, old_start);
  2818. }
  2819. //Once moved, avoid calling the destructors if trivial after move
  2820. if(value_traits::trivial_dctr_after_move){
  2821. old_values_destroyer.release();
  2822. }
  2823. }
  2824. else{ //If trivial destructor, we can uninitialized copy + copy in a single uninitialized copy
  2825. ::boost::container::uninitialized_move_alloc_n
  2826. (this->m_holder.alloc(), pos, static_cast<size_type>(old_finish - pos), new_start + before_plus_new);
  2827. this->m_holder.m_size = new_size;
  2828. old_values_destroyer.release();
  2829. }
  2830. }
  2831. }
  2832. else{
  2833. //Check if we have to do the insertion in two phases
  2834. //since maybe s_before is not big enough and
  2835. //the buffer was expanded both sides
  2836. //
  2837. //Old situation:
  2838. // _________________________________________________
  2839. //| raw_mem | old_begin + old_end | raw_mem |
  2840. //|_________|_____________________|_________________|
  2841. //
  2842. //New situation with do_after:
  2843. // _________________________________________________
  2844. //| old_begin + new + old_end | raw_mem |
  2845. //|___________________________________|_____________|
  2846. //
  2847. //New without do_after:
  2848. // _________________________________________________
  2849. //| old_begin + new + old_end | raw_mem |
  2850. //|____________________________|____________________|
  2851. //
  2852. const bool do_after = n > s_before;
  2853. //Now we can have two situations: the raw_mem of the
  2854. //beginning divides the old_begin, or the new elements:
  2855. if (s_before <= elemsbefore) {
  2856. //The raw memory divides the old_begin group:
  2857. //
  2858. //If we need two phase construction (do_after)
  2859. //new group is divided in new = new_beg + new_end groups
  2860. //In this phase only new_beg will be inserted
  2861. //
  2862. //Old situation:
  2863. // _________________________________________________
  2864. //| raw_mem | old_begin | old_end | raw_mem |
  2865. //|_________|___________|_________|_________________|
  2866. //
  2867. //New situation with do_after(1):
  2868. //This is not definitive situation, the second phase
  2869. //will include
  2870. // _________________________________________________
  2871. //| old_begin | new_beg | old_end | raw_mem |
  2872. //|___________|_________|_________|_________________|
  2873. //
  2874. //New situation without do_after:
  2875. // _________________________________________________
  2876. //| old_begin | new | old_end | raw_mem |
  2877. //|___________|_____|_________|_____________________|
  2878. //
  2879. //Copy the first part of old_begin to raw_mem
  2880. ::boost::container::uninitialized_move_alloc_n
  2881. (this->m_holder.alloc(), old_start, s_before, new_start);
  2882. //The buffer is all constructed until old_end,
  2883. //so program trailing destruction and assign final size
  2884. //if !do_after, s_before+n otherwise.
  2885. size_type new_1st_range;
  2886. if(do_after){
  2887. new_1st_range = s_before;
  2888. //release destroyer and update size
  2889. old_values_destroyer.release();
  2890. }
  2891. else{
  2892. new_1st_range = n;
  2893. if(value_traits::trivial_dctr_after_move)
  2894. old_values_destroyer.release();
  2895. else{
  2896. old_values_destroyer.shrink_forward(old_size - (s_before - n));
  2897. }
  2898. }
  2899. this->m_holder.m_size = old_size + new_1st_range;
  2900. //Now copy the second part of old_begin overwriting itself
  2901. T *const next = ::boost::container::move(old_start + s_before, pos, old_start);
  2902. //Now copy the new_beg elements
  2903. insert_range_proxy.copy_n_and_update(this->m_holder.alloc(), next, new_1st_range);
  2904. //If there is no after work and the last old part needs to be moved to front, do it
  2905. if(!do_after && (n != s_before)){
  2906. //Now displace old_end elements
  2907. ::boost::container::move(pos, old_finish, next + new_1st_range);
  2908. }
  2909. }
  2910. else {
  2911. //If we have to expand both sides,
  2912. //we will play if the first new values so
  2913. //calculate the upper bound of new values
  2914. //The raw memory divides the new elements
  2915. //
  2916. //If we need two phase construction (do_after)
  2917. //new group is divided in new = new_beg + new_end groups
  2918. //In this phase only new_beg will be inserted
  2919. //
  2920. //Old situation:
  2921. // _______________________________________________________
  2922. //| raw_mem | old_begin | old_end | raw_mem |
  2923. //|_______________|___________|_________|_________________|
  2924. //
  2925. //New situation with do_after():
  2926. // ____________________________________________________
  2927. //| old_begin | new_beg | old_end | raw_mem |
  2928. //|___________|_______________|_________|______________|
  2929. //
  2930. //New situation without do_after:
  2931. // ______________________________________________________
  2932. //| old_begin | new | old_end | raw_mem |
  2933. //|___________|_____|_________|__________________________|
  2934. //
  2935. //First copy whole old_begin and part of new to raw_mem
  2936. T * const new_pos = ::boost::container::uninitialized_move_alloc
  2937. (this->m_holder.alloc(), old_start, pos, new_start);
  2938. this->m_holder.m_size = elemsbefore;
  2939. const size_type mid_n = s_before - elemsbefore;
  2940. insert_range_proxy.uninitialized_copy_n_and_update(this->m_holder.alloc(), new_pos, mid_n);
  2941. //The buffer is all constructed until old_end,
  2942. //release destroyer
  2943. this->m_holder.m_size = old_size + s_before;
  2944. old_values_destroyer.release();
  2945. if(do_after){
  2946. //Copy new_beg part
  2947. insert_range_proxy.copy_n_and_update(this->m_holder.alloc(), old_start, elemsbefore);
  2948. }
  2949. else{
  2950. //Copy all new elements
  2951. const size_type rest_new = n - mid_n;
  2952. insert_range_proxy.copy_n_and_update(this->m_holder.alloc(), old_start, rest_new);
  2953. T* const move_start = old_start + rest_new;
  2954. //Displace old_end, but make sure data has to be moved
  2955. T* const move_end = move_start != pos ? ::boost::container::move(pos, old_finish, move_start)
  2956. : old_finish;
  2957. //Destroy remaining moved elements from old_end except if they
  2958. //have trivial destructor after being moved
  2959. size_type n_destroy = s_before - n;
  2960. if(!value_traits::trivial_dctr_after_move)
  2961. boost::container::destroy_alloc_n(this->get_stored_allocator(), move_end, n_destroy);
  2962. this->m_holder.m_size -= n_destroy;
  2963. }
  2964. }
  2965. //This is only executed if two phase construction is needed
  2966. if(do_after){
  2967. //The raw memory divides the new elements
  2968. //
  2969. //Old situation:
  2970. // ______________________________________________________
  2971. //| raw_mem | old_begin | old_end | raw_mem |
  2972. //|______________|___________|____________|______________|
  2973. //
  2974. //New situation with do_after(1):
  2975. // _______________________________________________________
  2976. //| old_begin + new_beg | new_end |old_end | raw_mem |
  2977. //|__________________________|_________|________|_________|
  2978. //
  2979. //New situation with do_after(2):
  2980. // ______________________________________________________
  2981. //| old_begin + new | old_end |raw |
  2982. //|_______________________________________|_________|____|
  2983. //
  2984. const size_type n_after = n - s_before;
  2985. const size_type elemsafter = old_size - elemsbefore;
  2986. //We can have two situations:
  2987. if (elemsafter >= n_after){
  2988. //The raw_mem from end will divide displaced old_end
  2989. //
  2990. //Old situation:
  2991. // ______________________________________________________
  2992. //| raw_mem | old_begin | old_end | raw_mem |
  2993. //|______________|___________|____________|______________|
  2994. //
  2995. //New situation with do_after(1):
  2996. // _______________________________________________________
  2997. //| old_begin + new_beg | new_end |old_end | raw_mem |
  2998. //|__________________________|_________|________|_________|
  2999. //
  3000. //First copy the part of old_end raw_mem
  3001. T* finish_n = old_finish - n_after;
  3002. ::boost::container::uninitialized_move_alloc
  3003. (this->m_holder.alloc(), finish_n, old_finish, old_finish);
  3004. this->m_holder.m_size += n_after;
  3005. //Displace the rest of old_end to the new position
  3006. boost::container::move_backward(pos, finish_n, old_finish);
  3007. //Now overwrite with new_end
  3008. //The new_end part is [first + (n - n_after), last)
  3009. insert_range_proxy.copy_n_and_update(this->m_holder.alloc(), pos, n_after);
  3010. }
  3011. else {
  3012. //The raw_mem from end will divide new_end part
  3013. //
  3014. //Old situation:
  3015. // _____________________________________________________________
  3016. //| raw_mem | old_begin | old_end | raw_mem |
  3017. //|______________|___________|____________|_____________________|
  3018. //
  3019. //New situation with do_after(2):
  3020. // _____________________________________________________________
  3021. //| old_begin + new_beg | new_end |old_end | raw_mem |
  3022. //|__________________________|_______________|________|_________|
  3023. //
  3024. const size_type mid_last_dist = n_after - elemsafter;
  3025. //First initialize data in raw memory
  3026. //Copy to the old_end part to the uninitialized zone leaving a gap.
  3027. ::boost::container::uninitialized_move_alloc
  3028. (this->m_holder.alloc(), pos, old_finish, old_finish + mid_last_dist);
  3029. typename value_traits::ArrayDestructor old_end_destroyer
  3030. (old_finish + mid_last_dist, this->m_holder.alloc(), old_finish - pos);
  3031. //Copy the first part to the already constructed old_end zone
  3032. insert_range_proxy.copy_n_and_update(this->m_holder.alloc(), pos, elemsafter);
  3033. //Copy the rest to the uninitialized zone filling the gap
  3034. insert_range_proxy.uninitialized_copy_n_and_update(this->m_holder.alloc(), old_finish, mid_last_dist);
  3035. this->m_holder.m_size += n_after;
  3036. old_end_destroyer.release();
  3037. }
  3038. }
  3039. }
  3040. }
  3041. void priv_throw_if_out_of_range(size_type n) const
  3042. {
  3043. //If n is out of range, throw an out_of_range exception
  3044. if (n >= this->size()){
  3045. throw_out_of_range("vector::at out of range");
  3046. }
  3047. }
  3048. BOOST_CONTAINER_FORCEINLINE bool priv_in_range(const_iterator pos) const
  3049. {
  3050. return (this->begin() <= pos) && (pos < this->end());
  3051. }
  3052. BOOST_CONTAINER_FORCEINLINE bool priv_in_range_or_end(const_iterator pos) const
  3053. {
  3054. return (this->begin() <= pos) && (pos <= this->end());
  3055. }
  3056. #ifdef BOOST_CONTAINER_VECTOR_ALLOC_STATS
  3057. public:
  3058. unsigned int num_expand_fwd;
  3059. unsigned int num_expand_bwd;
  3060. unsigned int num_shrink;
  3061. unsigned int num_alloc;
  3062. void reset_alloc_stats()
  3063. { num_expand_fwd = num_expand_bwd = num_alloc = 0, num_shrink = 0; }
  3064. #endif
  3065. #endif //#ifndef BOOST_CONTAINER_DOXYGEN_INVOKED
  3066. };
  3067. #ifndef BOOST_CONTAINER_NO_CXX17_CTAD
  3068. template <typename InputIterator>
  3069. vector(InputIterator, InputIterator) ->
  3070. vector<typename iterator_traits<InputIterator>::value_type>;
  3071. template <typename InputIterator, typename Allocator>
  3072. vector(InputIterator, InputIterator, Allocator const&) ->
  3073. vector<typename iterator_traits<InputIterator>::value_type, Allocator>;
  3074. #endif
  3075. }} //namespace boost::container
  3076. #ifndef BOOST_CONTAINER_DOXYGEN_INVOKED
  3077. namespace boost {
  3078. //!has_trivial_destructor_after_move<> == true_type
  3079. //!specialization for optimizations
  3080. template <class T, class Allocator, class Options>
  3081. struct has_trivial_destructor_after_move<boost::container::vector<T, Allocator, Options> >
  3082. {
  3083. typedef typename boost::container::vector<T, Allocator, Options>::allocator_type allocator_type;
  3084. typedef typename ::boost::container::allocator_traits<allocator_type>::pointer pointer;
  3085. static const bool value = ::boost::has_trivial_destructor_after_move<allocator_type>::value &&
  3086. ::boost::has_trivial_destructor_after_move<pointer>::value;
  3087. };
  3088. }
  3089. #endif //#ifndef BOOST_CONTAINER_DOXYGEN_INVOKED
  3090. #include <boost/container/detail/config_end.hpp>
  3091. #endif // #ifndef BOOST_CONTAINER_CONTAINER_VECTOR_HPP