vector.hpp 135 KB

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