vector.hpp 133 KB

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