vector.hpp 137 KB

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