vector.hpp 130 KB

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