vector.hpp 140 KB

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