vector.hpp 137 KB

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