vector.hpp 137 KB

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