vector.hpp 123 KB

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