vector.hpp 107 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684
  1. //////////////////////////////////////////////////////////////////////////////
  2. //
  3. // (C) Copyright Ion Gaztanaga 2005-2012. 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. #if defined(_MSC_VER)
  13. # pragma once
  14. #endif
  15. #include <boost/container/detail/config_begin.hpp>
  16. #include <boost/container/detail/workaround.hpp>
  17. #include <boost/container/container_fwd.hpp>
  18. #include <cstddef>
  19. #include <memory>
  20. #include <algorithm>
  21. #include <iterator>
  22. #include <utility>
  23. #include <boost/detail/no_exceptions_support.hpp>
  24. #include <boost/type_traits/has_trivial_destructor.hpp>
  25. #include <boost/type_traits/has_trivial_copy.hpp>
  26. #include <boost/type_traits/has_trivial_assign.hpp>
  27. #include <boost/type_traits/has_nothrow_copy.hpp>
  28. #include <boost/type_traits/has_nothrow_assign.hpp>
  29. #include <boost/type_traits/has_nothrow_constructor.hpp>
  30. #include <boost/container/container_fwd.hpp>
  31. #include <boost/container/detail/version_type.hpp>
  32. #include <boost/container/detail/allocation_type.hpp>
  33. #include <boost/container/detail/utilities.hpp>
  34. #include <boost/container/detail/iterators.hpp>
  35. #include <boost/container/detail/algorithms.hpp>
  36. #include <boost/container/detail/destroyers.hpp>
  37. #include <boost/container/allocator_traits.hpp>
  38. #include <boost/container/detail/allocator_version_traits.hpp>
  39. #include <boost/container/throw_exception.hpp>
  40. #include <boost/move/utility.hpp>
  41. #include <boost/move/iterator.hpp>
  42. #include <boost/move/detail/move_helpers.hpp>
  43. #include <boost/intrusive/pointer_traits.hpp>
  44. #include <boost/container/detail/mpl.hpp>
  45. #include <boost/container/detail/type_traits.hpp>
  46. #include <boost/container/detail/advanced_insert_int.hpp>
  47. #include <boost/assert.hpp>
  48. namespace boost {
  49. namespace container {
  50. /// @cond
  51. //#define BOOST_CONTAINER_VECTOR_ITERATOR_IS_POINTER
  52. namespace container_detail {
  53. #ifndef BOOST_CONTAINER_VECTOR_ITERATOR_IS_POINTER
  54. template <class Pointer, bool IsConst>
  55. class vec_iterator
  56. {
  57. public:
  58. typedef std::random_access_iterator_tag iterator_category;
  59. typedef typename boost::intrusive::pointer_traits<Pointer>::element_type value_type;
  60. typedef typename boost::intrusive::pointer_traits<Pointer>::difference_type difference_type;
  61. typedef typename if_c
  62. < IsConst
  63. , typename boost::intrusive::pointer_traits<Pointer>::template
  64. rebind_pointer<const value_type>::type
  65. , Pointer
  66. >::type pointer;
  67. typedef typename if_c
  68. < IsConst
  69. , const value_type&
  70. , value_type&
  71. >::type reference;
  72. /// @cond
  73. private:
  74. Pointer m_ptr;
  75. public:
  76. const Pointer &get_ptr() const BOOST_CONTAINER_NOEXCEPT
  77. { return m_ptr; }
  78. Pointer &get_ptr() BOOST_CONTAINER_NOEXCEPT
  79. { return m_ptr; }
  80. explicit vec_iterator(Pointer ptr) BOOST_CONTAINER_NOEXCEPT
  81. : m_ptr(ptr)
  82. {}
  83. /// @endcond
  84. public:
  85. //Constructors
  86. vec_iterator() BOOST_CONTAINER_NOEXCEPT
  87. #ifndef NDEBUG
  88. : m_ptr()
  89. #else
  90. // No value initialization of m_ptr() to speed up things a bit:
  91. #endif
  92. {}
  93. vec_iterator(vec_iterator<Pointer, false> const& other) BOOST_CONTAINER_NOEXCEPT
  94. : m_ptr(other.get_ptr())
  95. {}
  96. //Pointer like operators
  97. reference operator*() const BOOST_CONTAINER_NOEXCEPT
  98. { return *m_ptr; }
  99. pointer operator->() const BOOST_CONTAINER_NOEXCEPT
  100. { return ::boost::intrusive::pointer_traits<pointer>::pointer_to(this->operator*()); }
  101. reference operator[](difference_type off) const BOOST_CONTAINER_NOEXCEPT
  102. { return m_ptr[off]; }
  103. //Increment / Decrement
  104. vec_iterator& operator++() BOOST_CONTAINER_NOEXCEPT
  105. { ++m_ptr; return *this; }
  106. vec_iterator operator++(int) BOOST_CONTAINER_NOEXCEPT
  107. { return vec_iterator(m_ptr++); }
  108. vec_iterator& operator--() BOOST_CONTAINER_NOEXCEPT
  109. { --m_ptr; return *this; }
  110. vec_iterator operator--(int) BOOST_CONTAINER_NOEXCEPT
  111. { return vec_iterator(m_ptr--); }
  112. //Arithmetic
  113. vec_iterator& operator+=(difference_type off) BOOST_CONTAINER_NOEXCEPT
  114. { m_ptr += off; return *this; }
  115. vec_iterator& operator-=(difference_type off) BOOST_CONTAINER_NOEXCEPT
  116. { m_ptr -= off; return *this; }
  117. friend vec_iterator operator+(const vec_iterator &x, difference_type off) BOOST_CONTAINER_NOEXCEPT
  118. { return vec_iterator(x.m_ptr+off); }
  119. friend vec_iterator operator+(difference_type off, vec_iterator right) BOOST_CONTAINER_NOEXCEPT
  120. { right.m_ptr += off; return right; }
  121. friend vec_iterator operator-(vec_iterator left, difference_type off) BOOST_CONTAINER_NOEXCEPT
  122. { left.m_ptr -= off; return left; }
  123. friend difference_type operator-(const vec_iterator &left, const vec_iterator& right) BOOST_CONTAINER_NOEXCEPT
  124. { return left.m_ptr - right.m_ptr; }
  125. //Comparison operators
  126. friend bool operator== (const vec_iterator& l, const vec_iterator& r) BOOST_CONTAINER_NOEXCEPT
  127. { return l.m_ptr == r.m_ptr; }
  128. friend bool operator!= (const vec_iterator& l, const vec_iterator& r) BOOST_CONTAINER_NOEXCEPT
  129. { return l.m_ptr != r.m_ptr; }
  130. friend bool operator< (const vec_iterator& l, const vec_iterator& r) BOOST_CONTAINER_NOEXCEPT
  131. { return l.m_ptr < r.m_ptr; }
  132. friend bool operator<= (const vec_iterator& l, const vec_iterator& r) BOOST_CONTAINER_NOEXCEPT
  133. { return l.m_ptr <= r.m_ptr; }
  134. friend bool operator> (const vec_iterator& l, const vec_iterator& r) BOOST_CONTAINER_NOEXCEPT
  135. { return l.m_ptr > r.m_ptr; }
  136. friend bool operator>= (const vec_iterator& l, const vec_iterator& r) BOOST_CONTAINER_NOEXCEPT
  137. { return l.m_ptr >= r.m_ptr; }
  138. };
  139. } //namespace container_detail {
  140. template<class Pointer, bool IsConst>
  141. const Pointer &vector_iterator_get_ptr(const container_detail::vec_iterator<Pointer, IsConst> &it) BOOST_CONTAINER_NOEXCEPT
  142. { return it.get_ptr(); }
  143. template<class Pointer, bool IsConst>
  144. Pointer &get_ptr(container_detail::vec_iterator<Pointer, IsConst> &it) BOOST_CONTAINER_NOEXCEPT
  145. { return it.get_ptr(); }
  146. namespace container_detail {
  147. #else //ifndef BOOST_CONTAINER_VECTOR_ITERATOR_IS_POINTER
  148. template< class MaybeConstPointer
  149. , bool ElementTypeIsConst
  150. = is_const< typename boost::intrusive::pointer_traits<MaybeConstPointer>::element_type>::value >
  151. struct vector_get_ptr_pointer_to_non_const
  152. {
  153. typedef MaybeConstPointer const_pointer;
  154. typedef boost::intrusive::pointer_traits<const_pointer> pointer_traits_t;
  155. typedef typename pointer_traits_t::element_type element_type;
  156. typedef typename remove_const<element_type>::type non_const_element_type;
  157. typedef typename pointer_traits_t
  158. ::template rebind_pointer<non_const_element_type>::type return_type;
  159. static return_type get_ptr(const const_pointer &ptr) BOOST_CONTAINER_NOEXCEPT
  160. { return boost::intrusive::pointer_traits<return_type>::const_cast_from(ptr); }
  161. };
  162. template<class Pointer>
  163. struct vector_get_ptr_pointer_to_non_const<Pointer, false>
  164. {
  165. typedef const Pointer & return_type;
  166. static return_type get_ptr(const Pointer &ptr) BOOST_CONTAINER_NOEXCEPT
  167. { return ptr; }
  168. };
  169. } //namespace container_detail {
  170. template<class MaybeConstPointer>
  171. typename container_detail::vector_get_ptr_pointer_to_non_const<MaybeConstPointer>::return_type
  172. vector_iterator_get_ptr(const MaybeConstPointer &ptr) BOOST_CONTAINER_NOEXCEPT
  173. {
  174. return container_detail::vector_get_ptr_pointer_to_non_const<MaybeConstPointer>::get_ptr(ptr);
  175. }
  176. namespace container_detail {
  177. #endif //#ifndef BOOST_CONTAINER_VECTOR_ITERATOR_IS_POINTER
  178. template <class T, class Allocator>
  179. struct vector_value_traits
  180. {
  181. typedef T value_type;
  182. typedef Allocator allocator_type;
  183. static const bool trivial_dctr = boost::has_trivial_destructor<value_type>::value;
  184. static const bool trivial_dctr_after_move = ::boost::has_trivial_destructor_after_move<value_type>::value;
  185. static const bool trivial_copy = has_trivial_copy<value_type>::value;
  186. static const bool nothrow_copy = has_nothrow_copy<value_type>::value || trivial_copy;
  187. static const bool trivial_assign = has_trivial_assign<value_type>::value;
  188. static const bool nothrow_assign = has_nothrow_assign<value_type>::value || trivial_assign;
  189. //This is the anti-exception array destructor
  190. //to deallocate values already constructed
  191. typedef typename container_detail::if_c
  192. <trivial_dctr
  193. ,container_detail::null_scoped_destructor_n<Allocator>
  194. ,container_detail::scoped_destructor_n<Allocator>
  195. >::type OldArrayDestructor;
  196. //This is the anti-exception array destructor
  197. //to destroy objects created with copy construction
  198. typedef typename container_detail::if_c
  199. <nothrow_copy
  200. ,container_detail::null_scoped_destructor_n<Allocator>
  201. ,container_detail::scoped_destructor_n<Allocator>
  202. >::type ArrayDestructor;
  203. //This is the anti-exception array deallocator
  204. typedef typename container_detail::if_c
  205. <nothrow_copy
  206. ,container_detail::null_scoped_array_deallocator<Allocator>
  207. ,container_detail::scoped_array_deallocator<Allocator>
  208. >::type ArrayDeallocator;
  209. };
  210. //!This struct deallocates and allocated memory
  211. template < class Allocator
  212. , class AllocatorVersion = container_detail::integral_constant
  213. < unsigned
  214. , boost::container::container_detail::version<Allocator>::value
  215. >
  216. >
  217. struct vector_alloc_holder
  218. : public Allocator
  219. {
  220. private:
  221. BOOST_MOVABLE_BUT_NOT_COPYABLE(vector_alloc_holder)
  222. public:
  223. typedef boost::container::allocator_traits<Allocator> allocator_traits_type;
  224. typedef typename allocator_traits_type::pointer pointer;
  225. typedef typename allocator_traits_type::size_type size_type;
  226. typedef typename allocator_traits_type::value_type value_type;
  227. //Constructor, does not throw
  228. vector_alloc_holder()
  229. BOOST_CONTAINER_NOEXCEPT_IF(::boost::has_nothrow_default_constructor<Allocator>::value)
  230. : Allocator(), m_start(), m_size(), m_capacity()
  231. {}
  232. //Constructor, does not throw
  233. template<class AllocConvertible>
  234. explicit vector_alloc_holder(BOOST_FWD_REF(AllocConvertible) a) BOOST_CONTAINER_NOEXCEPT
  235. : Allocator(boost::forward<AllocConvertible>(a)), m_start(), m_size(), m_capacity()
  236. {}
  237. //Constructor, does not throw
  238. template<class AllocConvertible>
  239. explicit vector_alloc_holder(BOOST_FWD_REF(AllocConvertible) a, size_type initial_size)
  240. : Allocator(boost::forward<AllocConvertible>(a))
  241. , m_start()
  242. , m_size(initial_size) //Size is initialized here so vector should only call uninitialized_xxx after this
  243. , m_capacity()
  244. {
  245. if(initial_size){
  246. m_start = this->allocation_command(allocate_new, initial_size, initial_size, m_capacity, m_start).first;
  247. }
  248. }
  249. //Constructor, does not throw
  250. explicit vector_alloc_holder(size_type initial_size)
  251. : Allocator()
  252. , m_start()
  253. , m_size(initial_size) //Size is initialized here so vector should only call uninitialized_xxx after this
  254. , m_capacity()
  255. {
  256. if(initial_size){
  257. m_start = this->allocation_command
  258. (allocate_new, initial_size, initial_size, m_capacity, m_start).first;
  259. }
  260. }
  261. vector_alloc_holder(BOOST_RV_REF(vector_alloc_holder) holder) BOOST_CONTAINER_NOEXCEPT
  262. : Allocator(boost::move(static_cast<Allocator&>(holder)))
  263. , m_start(holder.m_start)
  264. , m_size(holder.m_size)
  265. , m_capacity(holder.m_capacity)
  266. {
  267. holder.m_start = pointer();
  268. holder.m_size = holder.m_capacity = 0;
  269. }
  270. void first_allocation(size_type cap)
  271. {
  272. if(cap){
  273. m_start = this->allocation_command
  274. (allocate_new, cap, cap, m_capacity, m_start).first;
  275. }
  276. }
  277. void first_allocation_same_allocator_type(size_type cap)
  278. { this->first_allocation(cap); }
  279. ~vector_alloc_holder() BOOST_CONTAINER_NOEXCEPT
  280. {
  281. if(this->m_capacity){
  282. this->alloc().deallocate(this->m_start, this->m_capacity);
  283. }
  284. }
  285. std::pair<pointer, bool>
  286. allocation_command(allocation_type command,
  287. size_type limit_size,
  288. size_type preferred_size,
  289. size_type &received_size, const pointer &reuse = pointer())
  290. {
  291. return allocator_version_traits<Allocator>::allocation_command
  292. (this->alloc(), command, limit_size, preferred_size, received_size, reuse);
  293. }
  294. size_type next_capacity(size_type additional_objects) const
  295. {
  296. std::size_t num_objects = this->m_size + additional_objects;
  297. std::size_t next_cap = this->m_capacity + this->m_capacity/2;
  298. return num_objects > next_cap ? num_objects : next_cap;/*
  299. return get_next_capacity( allocator_traits_type::max_size(this->m_holder.alloc())
  300. , this->m_capacity, additional_objects);*/
  301. }
  302. pointer m_start;
  303. size_type m_size;
  304. size_type m_capacity;
  305. void swap(vector_alloc_holder &x) BOOST_CONTAINER_NOEXCEPT
  306. {
  307. boost::container::swap_dispatch(this->m_start, x.m_start);
  308. boost::container::swap_dispatch(this->m_size, x.m_size);
  309. boost::container::swap_dispatch(this->m_capacity, x.m_capacity);
  310. }
  311. void move_from_empty(vector_alloc_holder &x) BOOST_CONTAINER_NOEXCEPT
  312. {
  313. this->m_start = x.m_start;
  314. this->m_size = x.m_size;
  315. this->m_capacity = x.m_capacity;
  316. x.m_start = pointer();
  317. x.m_size = x.m_capacity = 0;
  318. }
  319. Allocator &alloc() BOOST_CONTAINER_NOEXCEPT
  320. { return *this; }
  321. const Allocator &alloc() const BOOST_CONTAINER_NOEXCEPT
  322. { return *this; }
  323. const pointer &start() const BOOST_CONTAINER_NOEXCEPT { return m_start; }
  324. const size_type &capacity() const BOOST_CONTAINER_NOEXCEPT { return m_capacity; }
  325. void start(const pointer &p) BOOST_CONTAINER_NOEXCEPT { m_start = p; }
  326. void capacity(const size_type &c) BOOST_CONTAINER_NOEXCEPT { m_capacity = c; }
  327. };
  328. //!This struct deallocates and allocated memory
  329. template <class Allocator>
  330. struct vector_alloc_holder<Allocator, container_detail::integral_constant<unsigned, 0> >
  331. : public Allocator
  332. {
  333. private:
  334. BOOST_MOVABLE_BUT_NOT_COPYABLE(vector_alloc_holder)
  335. public:
  336. typedef boost::container::allocator_traits<Allocator> allocator_traits_type;
  337. typedef typename allocator_traits_type::pointer pointer;
  338. typedef typename allocator_traits_type::size_type size_type;
  339. typedef typename allocator_traits_type::value_type value_type;
  340. template <class OtherAllocator, class OtherAllocatorVersion>
  341. friend struct vector_alloc_holder;
  342. //Constructor, does not throw
  343. vector_alloc_holder()
  344. BOOST_CONTAINER_NOEXCEPT_IF(::boost::has_nothrow_default_constructor<Allocator>::value)
  345. : Allocator(), m_size()
  346. {}
  347. //Constructor, does not throw
  348. template<class AllocConvertible>
  349. explicit vector_alloc_holder(BOOST_FWD_REF(AllocConvertible) a) BOOST_CONTAINER_NOEXCEPT
  350. : Allocator(boost::forward<AllocConvertible>(a)), m_size()
  351. {}
  352. //Constructor, does not throw
  353. template<class AllocConvertible>
  354. explicit vector_alloc_holder(BOOST_FWD_REF(AllocConvertible) a, size_type initial_size)
  355. : Allocator(boost::forward<AllocConvertible>(a))
  356. , m_size(initial_size) //Size is initialized here...
  357. {
  358. //... and capacity here, so vector, must call uninitialized_xxx in the derived constructor
  359. this->first_allocation(initial_size);
  360. }
  361. //Constructor, does not throw
  362. explicit vector_alloc_holder(size_type initial_size)
  363. : Allocator()
  364. , m_size(initial_size) //Size is initialized here...
  365. {
  366. //... and capacity here, so vector, must call uninitialized_xxx in the derived constructor
  367. this->first_allocation(initial_size);
  368. }
  369. vector_alloc_holder(BOOST_RV_REF(vector_alloc_holder) holder)
  370. : Allocator(boost::move(static_cast<Allocator&>(holder)))
  371. , m_size(holder.m_size) //Size is initialized here so vector should only call uninitialized_xxx after this
  372. {
  373. ::boost::container::uninitialized_move_alloc_n
  374. (this->alloc(), container_detail::to_raw_pointer(holder.start()), m_size, container_detail::to_raw_pointer(this->start()));
  375. }
  376. template<class OtherAllocator, class OtherAllocatorVersion>
  377. vector_alloc_holder(BOOST_RV_REF_BEG vector_alloc_holder<OtherAllocator, OtherAllocatorVersion> BOOST_RV_REF_END holder)
  378. : Allocator()
  379. , m_size(holder.m_size) //Initialize it to m_size as first_allocation can only succeed or abort
  380. {
  381. //Different allocator type so we must check we have enough storage
  382. const size_type n = holder.m_size;
  383. this->first_allocation(n);
  384. ::boost::container::uninitialized_move_alloc_n
  385. (this->alloc(), container_detail::to_raw_pointer(holder.start()), n, container_detail::to_raw_pointer(this->start()));
  386. }
  387. void first_allocation(size_type cap)
  388. {
  389. if(cap > Allocator::internal_capacity){
  390. throw_bad_alloc();
  391. }
  392. }
  393. void first_allocation_same_allocator_type(size_type) BOOST_CONTAINER_NOEXCEPT
  394. {}
  395. //Destructor
  396. ~vector_alloc_holder() BOOST_CONTAINER_NOEXCEPT
  397. {}
  398. void swap(vector_alloc_holder &x)
  399. {
  400. this->priv_swap_members_impl(x);
  401. }
  402. template<class OtherAllocator, class OtherAllocatorVersion>
  403. void swap(vector_alloc_holder<OtherAllocator, OtherAllocatorVersion> &x)
  404. {
  405. if(this->m_size > OtherAllocator::internal_capacity || x.m_size > Allocator::internal_capacity){
  406. throw_bad_alloc();
  407. }
  408. this->priv_swap_members_impl(x);
  409. }
  410. void move_from_empty(vector_alloc_holder &)
  411. { //Containers with version 0 allocators can't be moved without move elements one by one
  412. throw_bad_alloc();
  413. }
  414. Allocator &alloc() BOOST_CONTAINER_NOEXCEPT
  415. { return *this; }
  416. const Allocator &alloc() const BOOST_CONTAINER_NOEXCEPT
  417. { return *this; }
  418. pointer start() const BOOST_CONTAINER_NOEXCEPT { return Allocator::internal_storage(); }
  419. size_type capacity() const BOOST_CONTAINER_NOEXCEPT { return Allocator::internal_capacity; }
  420. size_type m_size;
  421. private:
  422. template<class OtherAllocator, class OtherAllocatorVersion>
  423. void priv_swap_members_impl(vector_alloc_holder<OtherAllocator, OtherAllocatorVersion> &x)
  424. {
  425. const std::size_t MaxTmpStorage = sizeof(value_type)*Allocator::internal_capacity;
  426. value_type *const first_this = container_detail::to_raw_pointer(this->start());
  427. value_type *const first_x = container_detail::to_raw_pointer(x.start());
  428. if(this->m_size < x.m_size){
  429. boost::container::deep_swap_alloc_n<MaxTmpStorage>(this->alloc(), first_this, this->m_size, first_x, x.m_size);
  430. }
  431. else{
  432. boost::container::deep_swap_alloc_n<MaxTmpStorage>(this->alloc(), first_x, x.m_size, first_this, this->m_size);
  433. }
  434. boost::container::swap_dispatch(this->m_size, x.m_size);
  435. }
  436. };
  437. } //namespace container_detail {
  438. /// @endcond
  439. //! \class vector
  440. //! A vector is a sequence that supports random access to elements, constant
  441. //! time insertion and removal of elements at the end, and linear time insertion
  442. //! and removal of elements at the beginning or in the middle. The number of
  443. //! elements in a vector may vary dynamically; memory management is automatic.
  444. //! boost::container::vector is similar to std::vector but it's compatible
  445. //! with shared memory and memory mapped files.
  446. #ifdef BOOST_CONTAINER_DOXYGEN_INVOKED
  447. template <class T, class Allocator = std::allocator<T> >
  448. #else
  449. template <class T, class Allocator>
  450. #endif
  451. class vector
  452. {
  453. /// @cond
  454. typedef container_detail::integral_constant
  455. <unsigned, boost::container::container_detail::version
  456. <Allocator>::value > alloc_version;
  457. boost::container::container_detail::vector_alloc_holder
  458. <Allocator, alloc_version> m_holder;
  459. typedef allocator_traits<Allocator> allocator_traits_type;
  460. template <class U, class UAllocator>
  461. friend class vector;
  462. typedef typename ::boost::container::allocator_traits
  463. <Allocator>::pointer pointer_impl;
  464. typedef container_detail::vec_iterator<pointer_impl, false> iterator_impl;
  465. typedef container_detail::vec_iterator<pointer_impl, true > const_iterator_impl;
  466. /// @endcond
  467. public:
  468. //////////////////////////////////////////////
  469. //
  470. // types
  471. //
  472. //////////////////////////////////////////////
  473. typedef T value_type;
  474. typedef typename ::boost::container::allocator_traits<Allocator>::pointer pointer;
  475. typedef typename ::boost::container::allocator_traits<Allocator>::const_pointer const_pointer;
  476. typedef typename ::boost::container::allocator_traits<Allocator>::reference reference;
  477. typedef typename ::boost::container::allocator_traits<Allocator>::const_reference const_reference;
  478. typedef typename ::boost::container::allocator_traits<Allocator>::size_type size_type;
  479. typedef typename ::boost::container::allocator_traits<Allocator>::difference_type difference_type;
  480. typedef Allocator allocator_type;
  481. typedef Allocator stored_allocator_type;
  482. #if defined BOOST_CONTAINER_VECTOR_ITERATOR_IS_POINTER && !defined(BOOST_CONTAINER_DOXYGEN_INVOKED)
  483. typedef BOOST_CONTAINER_IMPDEF(pointer) iterator;
  484. typedef BOOST_CONTAINER_IMPDEF(const_pointer) const_iterator;
  485. #else
  486. typedef BOOST_CONTAINER_IMPDEF(iterator_impl) iterator;
  487. typedef BOOST_CONTAINER_IMPDEF(const_iterator_impl) const_iterator;
  488. #endif
  489. typedef BOOST_CONTAINER_IMPDEF(std::reverse_iterator<iterator>) reverse_iterator;
  490. typedef BOOST_CONTAINER_IMPDEF(std::reverse_iterator<const_iterator>) const_reverse_iterator;
  491. /// @cond
  492. private:
  493. BOOST_COPYABLE_AND_MOVABLE(vector)
  494. typedef container_detail::vector_value_traits<value_type, Allocator> value_traits;
  495. typedef container_detail::integral_constant<unsigned, 0> allocator_v0;
  496. typedef container_detail::integral_constant<unsigned, 1> allocator_v1;
  497. typedef container_detail::integral_constant<unsigned, 2> allocator_v2;
  498. typedef constant_iterator<T, difference_type> cvalue_iterator;
  499. /// @endcond
  500. public:
  501. //////////////////////////////////////////////
  502. //
  503. // construct/copy/destroy
  504. //
  505. //////////////////////////////////////////////
  506. //! <b>Effects</b>: Constructs a vector taking the allocator as parameter.
  507. //!
  508. //! <b>Throws</b>: If allocator_type's default constructor throws.
  509. //!
  510. //! <b>Complexity</b>: Constant.
  511. vector()
  512. BOOST_CONTAINER_NOEXCEPT_IF(::boost::has_nothrow_default_constructor<Allocator>::value)
  513. : m_holder()
  514. {}
  515. //! <b>Effects</b>: Constructs a vector taking the allocator as parameter.
  516. //!
  517. //! <b>Throws</b>: Nothing
  518. //!
  519. //! <b>Complexity</b>: Constant.
  520. explicit vector(const Allocator& a) BOOST_CONTAINER_NOEXCEPT
  521. : m_holder(a)
  522. {}
  523. //! <b>Effects</b>: Constructs a vector that will use a copy of allocator a
  524. //! and inserts n value initialized values.
  525. //!
  526. //! <b>Throws</b>: If allocator_type's default constructor or allocation
  527. //! throws or T's default constructor throws.
  528. //!
  529. //! <b>Complexity</b>: Linear to n.
  530. explicit vector(size_type n)
  531. : m_holder(n)
  532. {
  533. boost::container::uninitialized_value_init_alloc_n
  534. (this->m_holder.alloc(), n, container_detail::to_raw_pointer(this->m_holder.start()));
  535. }
  536. //! <b>Effects</b>: Constructs a vector that will use a copy of allocator a
  537. //! and inserts n default initialized values.
  538. //!
  539. //! <b>Throws</b>: If allocator_type's default constructor or allocation
  540. //! throws or T's default constructor throws.
  541. //!
  542. //! <b>Complexity</b>: Linear to n.
  543. //!
  544. //! <b>Note</b>: Non-standard extension
  545. explicit vector(size_type n, default_init_t)
  546. : m_holder(n)
  547. {
  548. boost::container::uninitialized_default_init_alloc_n
  549. (this->m_holder.alloc(), n, container_detail::to_raw_pointer(this->m_holder.start()));
  550. }
  551. //! <b>Effects</b>: Constructs a vector
  552. //! and inserts n copies of value.
  553. //!
  554. //! <b>Throws</b>: If allocator_type's default constructor or allocation
  555. //! throws or T's copy constructor throws.
  556. //!
  557. //! <b>Complexity</b>: Linear to n.
  558. vector(size_type n, const T& value)
  559. : m_holder(n)
  560. {
  561. boost::container::uninitialized_fill_alloc_n
  562. (this->m_holder.alloc(), value, n, container_detail::to_raw_pointer(this->m_holder.start()));
  563. }
  564. //! <b>Effects</b>: Constructs a vector that will use a copy of allocator a
  565. //! and inserts n copies of value.
  566. //!
  567. //! <b>Throws</b>: If allocation
  568. //! throws or T's copy constructor throws.
  569. //!
  570. //! <b>Complexity</b>: Linear to n.
  571. vector(size_type n, const T& value, const allocator_type& a)
  572. : m_holder(a, n)
  573. {
  574. boost::container::uninitialized_fill_alloc_n
  575. (this->m_holder.alloc(), value, n, container_detail::to_raw_pointer(this->m_holder.start()));
  576. }
  577. //! <b>Effects</b>: Constructs a vector
  578. //! and inserts a copy of the range [first, last) in the vector.
  579. //!
  580. //! <b>Throws</b>: If allocator_type's default constructor or allocation
  581. //! throws or T's constructor taking an dereferenced InIt throws.
  582. //!
  583. //! <b>Complexity</b>: Linear to the range [first, last).
  584. template <class InIt>
  585. vector(InIt first, InIt last)
  586. : m_holder()
  587. { this->insert(this->cend(), first, last); }
  588. //! <b>Effects</b>: Constructs a vector that will use a copy of allocator a
  589. //! and inserts a copy of the range [first, last) in the vector.
  590. //!
  591. //! <b>Throws</b>: If allocator_type's default constructor or allocation
  592. //! throws or T's constructor taking an dereferenced InIt throws.
  593. //!
  594. //! <b>Complexity</b>: Linear to the range [first, last).
  595. template <class InIt>
  596. vector(InIt first, InIt last, const allocator_type& a)
  597. : m_holder(a)
  598. { this->insert(this->cend(), first, last); }
  599. //! <b>Effects</b>: Copy constructs a vector.
  600. //!
  601. //! <b>Postcondition</b>: x == *this.
  602. //!
  603. //! <b>Throws</b>: If allocator_type's default constructor or allocation
  604. //! throws or T's copy constructor throws.
  605. //!
  606. //! <b>Complexity</b>: Linear to the elements x contains.
  607. vector(const vector &x)
  608. : m_holder(allocator_traits_type::select_on_container_copy_construction(x.m_holder.alloc()), x.size())
  609. {
  610. ::boost::container::uninitialized_copy_alloc_n
  611. ( this->m_holder.alloc(), container_detail::to_raw_pointer(x.m_holder.start())
  612. , x.size(), container_detail::to_raw_pointer(this->m_holder.start()));
  613. }
  614. //! <b>Effects</b>: Move constructor. Moves mx's resources to *this.
  615. //!
  616. //! <b>Throws</b>: Nothing
  617. //!
  618. //! <b>Complexity</b>: Constant.
  619. vector(BOOST_RV_REF(vector) mx) BOOST_CONTAINER_NOEXCEPT
  620. : m_holder(boost::move(mx.m_holder))
  621. {}
  622. #if !defined(BOOST_CONTAINER_DOXYGEN_INVOKED)
  623. //! <b>Effects</b>: Move constructor. Moves mx's resources to *this.
  624. //!
  625. //! <b>Throws</b>: If T's move constructor or allocation throws
  626. //!
  627. //! <b>Complexity</b>: Linear.
  628. //!
  629. //! <b>Note</b>: Non-standard extension
  630. template<class OtherAllocator>
  631. vector(BOOST_RV_REF_BEG vector<T, OtherAllocator> BOOST_RV_REF_END mx)
  632. : m_holder(boost::move(mx.m_holder))
  633. {}
  634. #endif //!defined(BOOST_CONTAINER_DOXYGEN_INVOKED)
  635. //! <b>Effects</b>: Copy constructs a vector using the specified allocator.
  636. //!
  637. //! <b>Postcondition</b>: x == *this.
  638. //!
  639. //! <b>Throws</b>: If allocation
  640. //! throws or T's copy constructor throws.
  641. //!
  642. //! <b>Complexity</b>: Linear to the elements x contains.
  643. vector(const vector &x, const allocator_type &a)
  644. : m_holder(a, x.size())
  645. {
  646. ::boost::container::uninitialized_copy_alloc_n_source
  647. ( this->m_holder.alloc(), container_detail::to_raw_pointer(x.m_holder.start())
  648. , x.size(), container_detail::to_raw_pointer(this->m_holder.start()));
  649. }
  650. //! <b>Effects</b>: Move constructor using the specified allocator.
  651. //! Moves mx's resources to *this if a == allocator_type().
  652. //! Otherwise copies values from x to *this.
  653. //!
  654. //! <b>Throws</b>: If allocation or T's copy constructor throws.
  655. //!
  656. //! <b>Complexity</b>: Constant if a == mx.get_allocator(), linear otherwise.
  657. vector(BOOST_RV_REF(vector) mx, const allocator_type &a)
  658. : m_holder(a)
  659. {
  660. if(mx.m_holder.alloc() == a){
  661. this->m_holder.move_from_empty(mx.m_holder);
  662. }
  663. else{
  664. const size_type n = mx.size();
  665. this->m_holder.first_allocation_same_allocator_type(n);
  666. ::boost::container::uninitialized_move_alloc_n_source
  667. ( this->m_holder.alloc(), container_detail::to_raw_pointer(mx.m_holder.start())
  668. , n, container_detail::to_raw_pointer(this->m_holder.start()));
  669. this->m_holder.m_size = n;
  670. }
  671. }
  672. //! <b>Effects</b>: Destroys the vector. All stored values are destroyed
  673. //! and used memory is deallocated.
  674. //!
  675. //! <b>Throws</b>: Nothing.
  676. //!
  677. //! <b>Complexity</b>: Linear to the number of elements.
  678. ~vector() BOOST_CONTAINER_NOEXCEPT
  679. {
  680. boost::container::destroy_alloc_n
  681. (this->get_stored_allocator(), container_detail::to_raw_pointer(this->m_holder.start()), this->m_holder.m_size);
  682. //vector_alloc_holder deallocates the data
  683. }
  684. //! <b>Effects</b>: Makes *this contain the same elements as x.
  685. //!
  686. //! <b>Postcondition</b>: this->size() == x.size(). *this contains a copy
  687. //! of each of x's elements.
  688. //!
  689. //! <b>Throws</b>: If memory allocation throws or T's copy/move constructor/assignment throws.
  690. //!
  691. //! <b>Complexity</b>: Linear to the number of elements in x.
  692. vector& operator=(BOOST_COPY_ASSIGN_REF(vector) x)
  693. {
  694. if (&x != this){
  695. this->priv_copy_assign(boost::move(x), alloc_version());
  696. }
  697. return *this;
  698. }
  699. //! <b>Effects</b>: Move assignment. All mx's values are transferred to *this.
  700. //!
  701. //! <b>Postcondition</b>: x.empty(). *this contains a the elements x had
  702. //! before the function.
  703. //!
  704. //! <b>Throws</b>: Nothing
  705. //!
  706. //! <b>Complexity</b>: Linear.
  707. vector& operator=(BOOST_RV_REF(vector) x)
  708. //iG BOOST_CONTAINER_NOEXCEPT_IF(!allocator_type::propagate_on_container_move_assignment::value || is_nothrow_move_assignable<allocator_type>::value);)
  709. BOOST_CONTAINER_NOEXCEPT
  710. {
  711. this->priv_move_assign(boost::move(x), alloc_version());
  712. return *this;
  713. }
  714. #if !defined(BOOST_CONTAINER_DOXYGEN_INVOKED)
  715. //! <b>Effects</b>: Move assignment. All mx's values are transferred to *this.
  716. //!
  717. //! <b>Postcondition</b>: x.empty(). *this contains a the elements x had
  718. //! before the function.
  719. //!
  720. //! <b>Throws</b>: If move constructor/assignment of T throws or allocation throws
  721. //!
  722. //! <b>Complexity</b>: Linear.
  723. template<class OtherAllocator, class OtherAllocatorVersion>
  724. vector& operator=(BOOST_RV_REF_BEG vector<OtherAllocator, OtherAllocatorVersion> BOOST_RV_REF_END x)
  725. {
  726. this->priv_move_assign(boost::move(x), alloc_version());
  727. return *this;
  728. }
  729. #endif
  730. //! <b>Effects</b>: Assigns the the range [first, last) to *this.
  731. //!
  732. //! <b>Throws</b>: If memory allocation throws or T's copy/move constructor/assignment or
  733. //! T's constructor/assignment from dereferencing InpIt throws.
  734. //!
  735. //! <b>Complexity</b>: Linear to n.
  736. template <class InIt>
  737. void assign(InIt first, InIt last
  738. #if !defined(BOOST_CONTAINER_DOXYGEN_INVOKED)
  739. , typename container_detail::enable_if_c
  740. < !container_detail::is_convertible<InIt, size_type>::value
  741. //&& container_detail::is_input_iterator<InIt>::value
  742. >::type * = 0
  743. #endif
  744. )
  745. {
  746. //Overwrite all elements we can from [first, last)
  747. iterator cur = this->begin();
  748. const iterator end_it = this->end();
  749. for ( ; first != last && cur != end_it; ++cur, ++first){
  750. *cur = *first;
  751. }
  752. if (first == last){
  753. //There are no more elements in the sequence, erase remaining
  754. T* const end_pos = container_detail::to_raw_pointer(this->m_holder.start()) + this->m_holder.m_size;
  755. size_type n = static_cast<size_type>(end_pos - container_detail::to_raw_pointer(vector_iterator_get_ptr(cur)));
  756. this->priv_destroy_last_n(n);
  757. }
  758. else{
  759. //There are more elements in the range, insert the remaining ones
  760. this->insert(this->cend(), first, last);
  761. }
  762. }
  763. //! <b>Effects</b>: Assigns the n copies of val to *this.
  764. //!
  765. //! <b>Throws</b>: If memory allocation throws or
  766. //! T's copy/move constructor/assignment throws.
  767. //!
  768. //! <b>Complexity</b>: Linear to n.
  769. void assign(size_type n, const value_type& val)
  770. { this->assign(cvalue_iterator(val, n), cvalue_iterator()); }
  771. //! <b>Effects</b>: Returns a copy of the internal allocator.
  772. //!
  773. //! <b>Throws</b>: If allocator's copy constructor throws.
  774. //!
  775. //! <b>Complexity</b>: Constant.
  776. allocator_type get_allocator() const BOOST_CONTAINER_NOEXCEPT
  777. { return this->m_holder.alloc(); }
  778. //! <b>Effects</b>: Returns a reference to the internal allocator.
  779. //!
  780. //! <b>Throws</b>: Nothing
  781. //!
  782. //! <b>Complexity</b>: Constant.
  783. //!
  784. //! <b>Note</b>: Non-standard extension.
  785. stored_allocator_type &get_stored_allocator() BOOST_CONTAINER_NOEXCEPT
  786. { return this->m_holder.alloc(); }
  787. //! <b>Effects</b>: Returns a reference to the internal allocator.
  788. //!
  789. //! <b>Throws</b>: Nothing
  790. //!
  791. //! <b>Complexity</b>: Constant.
  792. //!
  793. //! <b>Note</b>: Non-standard extension.
  794. const stored_allocator_type &get_stored_allocator() const BOOST_CONTAINER_NOEXCEPT
  795. { return this->m_holder.alloc(); }
  796. //////////////////////////////////////////////
  797. //
  798. // iterators
  799. //
  800. //////////////////////////////////////////////
  801. //! <b>Effects</b>: Returns an iterator to the first element contained in the vector.
  802. //!
  803. //! <b>Throws</b>: Nothing.
  804. //!
  805. //! <b>Complexity</b>: Constant.
  806. iterator begin() BOOST_CONTAINER_NOEXCEPT
  807. { return iterator(this->m_holder.start()); }
  808. //! <b>Effects</b>: Returns a const_iterator to the first element contained in the vector.
  809. //!
  810. //! <b>Throws</b>: Nothing.
  811. //!
  812. //! <b>Complexity</b>: Constant.
  813. const_iterator begin() const BOOST_CONTAINER_NOEXCEPT
  814. { return const_iterator(this->m_holder.start()); }
  815. //! <b>Effects</b>: Returns an iterator to the end of the vector.
  816. //!
  817. //! <b>Throws</b>: Nothing.
  818. //!
  819. //! <b>Complexity</b>: Constant.
  820. iterator end() BOOST_CONTAINER_NOEXCEPT
  821. { return iterator(this->m_holder.start() + this->m_holder.m_size); }
  822. //! <b>Effects</b>: Returns a const_iterator to the end of the vector.
  823. //!
  824. //! <b>Throws</b>: Nothing.
  825. //!
  826. //! <b>Complexity</b>: Constant.
  827. const_iterator end() const BOOST_CONTAINER_NOEXCEPT
  828. { return this->cend(); }
  829. //! <b>Effects</b>: Returns a reverse_iterator pointing to the beginning
  830. //! of the reversed vector.
  831. //!
  832. //! <b>Throws</b>: Nothing.
  833. //!
  834. //! <b>Complexity</b>: Constant.
  835. reverse_iterator rbegin() BOOST_CONTAINER_NOEXCEPT
  836. { return reverse_iterator(this->end()); }
  837. //! <b>Effects</b>: Returns a const_reverse_iterator pointing to the beginning
  838. //! of the reversed vector.
  839. //!
  840. //! <b>Throws</b>: Nothing.
  841. //!
  842. //! <b>Complexity</b>: Constant.
  843. const_reverse_iterator rbegin() const BOOST_CONTAINER_NOEXCEPT
  844. { return this->crbegin(); }
  845. //! <b>Effects</b>: Returns a reverse_iterator pointing to the end
  846. //! of the reversed vector.
  847. //!
  848. //! <b>Throws</b>: Nothing.
  849. //!
  850. //! <b>Complexity</b>: Constant.
  851. reverse_iterator rend() BOOST_CONTAINER_NOEXCEPT
  852. { return reverse_iterator(this->begin()); }
  853. //! <b>Effects</b>: Returns a const_reverse_iterator pointing to the end
  854. //! of the reversed vector.
  855. //!
  856. //! <b>Throws</b>: Nothing.
  857. //!
  858. //! <b>Complexity</b>: Constant.
  859. const_reverse_iterator rend() const BOOST_CONTAINER_NOEXCEPT
  860. { return this->crend(); }
  861. //! <b>Effects</b>: Returns a const_iterator to the first element contained in the vector.
  862. //!
  863. //! <b>Throws</b>: Nothing.
  864. //!
  865. //! <b>Complexity</b>: Constant.
  866. const_iterator cbegin() const BOOST_CONTAINER_NOEXCEPT
  867. { return const_iterator(this->m_holder.start()); }
  868. //! <b>Effects</b>: Returns a const_iterator to the end of the vector.
  869. //!
  870. //! <b>Throws</b>: Nothing.
  871. //!
  872. //! <b>Complexity</b>: Constant.
  873. const_iterator cend() const BOOST_CONTAINER_NOEXCEPT
  874. { return const_iterator(this->m_holder.start() + this->m_holder.m_size); }
  875. //! <b>Effects</b>: Returns a const_reverse_iterator pointing to the beginning
  876. //! of the reversed vector.
  877. //!
  878. //! <b>Throws</b>: Nothing.
  879. //!
  880. //! <b>Complexity</b>: Constant.
  881. const_reverse_iterator crbegin() const BOOST_CONTAINER_NOEXCEPT
  882. { return const_reverse_iterator(this->end());}
  883. //! <b>Effects</b>: Returns a const_reverse_iterator pointing to the end
  884. //! of the reversed vector.
  885. //!
  886. //! <b>Throws</b>: Nothing.
  887. //!
  888. //! <b>Complexity</b>: Constant.
  889. const_reverse_iterator crend() const BOOST_CONTAINER_NOEXCEPT
  890. { return const_reverse_iterator(this->begin()); }
  891. //////////////////////////////////////////////
  892. //
  893. // capacity
  894. //
  895. //////////////////////////////////////////////
  896. //! <b>Effects</b>: Returns true if the vector contains no elements.
  897. //!
  898. //! <b>Throws</b>: Nothing.
  899. //!
  900. //! <b>Complexity</b>: Constant.
  901. bool empty() const BOOST_CONTAINER_NOEXCEPT
  902. { return !this->m_holder.m_size; }
  903. //! <b>Effects</b>: Returns the number of the elements contained in the vector.
  904. //!
  905. //! <b>Throws</b>: Nothing.
  906. //!
  907. //! <b>Complexity</b>: Constant.
  908. size_type size() const BOOST_CONTAINER_NOEXCEPT
  909. { return this->m_holder.m_size; }
  910. //! <b>Effects</b>: Returns the largest possible size of the vector.
  911. //!
  912. //! <b>Throws</b>: Nothing.
  913. //!
  914. //! <b>Complexity</b>: Constant.
  915. size_type max_size() const BOOST_CONTAINER_NOEXCEPT
  916. { return allocator_traits_type::max_size(this->m_holder.alloc()); }
  917. //! <b>Effects</b>: Inserts or erases elements at the end such that
  918. //! the size becomes n. New elements are value initialized.
  919. //!
  920. //! <b>Throws</b>: If memory allocation throws, or T's constructor throws.
  921. //!
  922. //! <b>Complexity</b>: Linear to the difference between size() and new_size.
  923. void resize(size_type new_size)
  924. {
  925. const size_type sz = this->size();
  926. if (new_size < sz){
  927. //Destroy last elements
  928. this->priv_destroy_last_n(sz - new_size);
  929. }
  930. else{
  931. const size_type n = new_size - this->size();
  932. container_detail::insert_value_initialized_n_proxy<Allocator, T*> proxy(this->m_holder.alloc());
  933. this->priv_forward_range_insert_at_end(n, proxy, alloc_version());
  934. }
  935. }
  936. //! <b>Effects</b>: Inserts or erases elements at the end such that
  937. //! the size becomes n. New elements are value initialized.
  938. //!
  939. //! <b>Throws</b>: If memory allocation throws, or T's constructor throws.
  940. //!
  941. //! <b>Complexity</b>: Linear to the difference between size() and new_size.
  942. //!
  943. //! <b>Note</b>: Non-standard extension
  944. void resize(size_type new_size, default_init_t)
  945. {
  946. const size_type sz = this->size();
  947. if (new_size < sz){
  948. //Destroy last elements
  949. this->priv_destroy_last_n(sz - new_size);
  950. }
  951. else{
  952. const size_type n = new_size - this->size();
  953. container_detail::insert_default_initialized_n_proxy<Allocator, T*> proxy(this->m_holder.alloc());
  954. this->priv_forward_range_insert_at_end(n, proxy, alloc_version());
  955. }
  956. }
  957. //! <b>Effects</b>: Inserts or erases elements at the end such that
  958. //! the size becomes n. New elements are copy constructed from x.
  959. //!
  960. //! <b>Throws</b>: If memory allocation throws, or T's copy constructor throws.
  961. //!
  962. //! <b>Complexity</b>: Linear to the difference between size() and new_size.
  963. void resize(size_type new_size, const T& x)
  964. {
  965. const size_type sz = this->size();
  966. if (new_size < sz){
  967. //Destroy last elements
  968. this->priv_destroy_last_n(sz - new_size);
  969. }
  970. else{
  971. const size_type n = new_size - this->size();
  972. container_detail::insert_n_copies_proxy<Allocator, T*> proxy(this->m_holder.alloc(), x);
  973. this->priv_forward_range_insert_at_end(n, proxy, alloc_version());
  974. }
  975. }
  976. //! <b>Effects</b>: Number of elements for which memory has been allocated.
  977. //! capacity() is always greater than or equal to size().
  978. //!
  979. //! <b>Throws</b>: Nothing.
  980. //!
  981. //! <b>Complexity</b>: Constant.
  982. size_type capacity() const BOOST_CONTAINER_NOEXCEPT
  983. { return this->m_holder.capacity(); }
  984. //! <b>Effects</b>: If n is less than or equal to capacity(), this call has no
  985. //! effect. Otherwise, it is a request for allocation of additional memory.
  986. //! If the request is successful, then capacity() is greater than or equal to
  987. //! n; otherwise, capacity() is unchanged. In either case, size() is unchanged.
  988. //!
  989. //! <b>Throws</b>: If memory allocation allocation throws or T's copy/move constructor throws.
  990. void reserve(size_type new_cap)
  991. {
  992. if (this->capacity() < new_cap){
  993. this->priv_reserve(new_cap, alloc_version());
  994. }
  995. }
  996. //! <b>Effects</b>: Tries to deallocate the excess of memory created
  997. //! with previous allocations. The size of the vector is unchanged
  998. //!
  999. //! <b>Throws</b>: If memory allocation throws, or T's copy/move constructor throws.
  1000. //!
  1001. //! <b>Complexity</b>: Linear to size().
  1002. void shrink_to_fit()
  1003. { this->priv_shrink_to_fit(alloc_version()); }
  1004. //////////////////////////////////////////////
  1005. //
  1006. // element access
  1007. //
  1008. //////////////////////////////////////////////
  1009. //! <b>Requires</b>: !empty()
  1010. //!
  1011. //! <b>Effects</b>: Returns a reference to the first
  1012. //! element of the container.
  1013. //!
  1014. //! <b>Throws</b>: Nothing.
  1015. //!
  1016. //! <b>Complexity</b>: Constant.
  1017. reference front() BOOST_CONTAINER_NOEXCEPT
  1018. { return *this->m_holder.start(); }
  1019. //! <b>Requires</b>: !empty()
  1020. //!
  1021. //! <b>Effects</b>: Returns a const reference to the first
  1022. //! element of the container.
  1023. //!
  1024. //! <b>Throws</b>: Nothing.
  1025. //!
  1026. //! <b>Complexity</b>: Constant.
  1027. const_reference front() const BOOST_CONTAINER_NOEXCEPT
  1028. { return *this->m_holder.start(); }
  1029. //! <b>Requires</b>: !empty()
  1030. //!
  1031. //! <b>Effects</b>: Returns a reference to the last
  1032. //! element of the container.
  1033. //!
  1034. //! <b>Throws</b>: Nothing.
  1035. //!
  1036. //! <b>Complexity</b>: Constant.
  1037. reference back() BOOST_CONTAINER_NOEXCEPT
  1038. { return this->m_holder.start()[this->m_holder.m_size - 1]; }
  1039. //! <b>Requires</b>: !empty()
  1040. //!
  1041. //! <b>Effects</b>: Returns a const reference to the last
  1042. //! element of the container.
  1043. //!
  1044. //! <b>Throws</b>: Nothing.
  1045. //!
  1046. //! <b>Complexity</b>: Constant.
  1047. const_reference back() const BOOST_CONTAINER_NOEXCEPT
  1048. { return this->m_holder.start()[this->m_holder.m_size - 1]; }
  1049. //! <b>Requires</b>: size() > n.
  1050. //!
  1051. //! <b>Effects</b>: Returns a reference to the nth element
  1052. //! from the beginning of the container.
  1053. //!
  1054. //! <b>Throws</b>: Nothing.
  1055. //!
  1056. //! <b>Complexity</b>: Constant.
  1057. reference operator[](size_type n) BOOST_CONTAINER_NOEXCEPT
  1058. { return this->m_holder.start()[n]; }
  1059. //! <b>Requires</b>: size() > n.
  1060. //!
  1061. //! <b>Effects</b>: Returns a const reference to the nth element
  1062. //! from the beginning of the container.
  1063. //!
  1064. //! <b>Throws</b>: Nothing.
  1065. //!
  1066. //! <b>Complexity</b>: Constant.
  1067. const_reference operator[](size_type n) const BOOST_CONTAINER_NOEXCEPT
  1068. { return this->m_holder.start()[n]; }
  1069. //! <b>Requires</b>: size() > n.
  1070. //!
  1071. //! <b>Effects</b>: Returns a reference to the nth element
  1072. //! from the beginning of the container.
  1073. //!
  1074. //! <b>Throws</b>: std::range_error if n >= size()
  1075. //!
  1076. //! <b>Complexity</b>: Constant.
  1077. reference at(size_type n)
  1078. { this->priv_check_range(n); return this->m_holder.start()[n]; }
  1079. //! <b>Requires</b>: size() > n.
  1080. //!
  1081. //! <b>Effects</b>: Returns a const reference to the nth element
  1082. //! from the beginning of the container.
  1083. //!
  1084. //! <b>Throws</b>: std::range_error if n >= size()
  1085. //!
  1086. //! <b>Complexity</b>: Constant.
  1087. const_reference at(size_type n) const
  1088. { this->priv_check_range(n); return this->m_holder.start()[n]; }
  1089. //////////////////////////////////////////////
  1090. //
  1091. // data access
  1092. //
  1093. //////////////////////////////////////////////
  1094. //! <b>Returns</b>: Allocator pointer such that [data(),data() + size()) is a valid range.
  1095. //! For a non-empty vector, data() == &front().
  1096. //!
  1097. //! <b>Throws</b>: Nothing.
  1098. //!
  1099. //! <b>Complexity</b>: Constant.
  1100. T* data() BOOST_CONTAINER_NOEXCEPT
  1101. { return container_detail::to_raw_pointer(this->m_holder.start()); }
  1102. //! <b>Returns</b>: Allocator pointer such that [data(),data() + size()) is a valid range.
  1103. //! For a non-empty vector, data() == &front().
  1104. //!
  1105. //! <b>Throws</b>: Nothing.
  1106. //!
  1107. //! <b>Complexity</b>: Constant.
  1108. const T * data() const BOOST_CONTAINER_NOEXCEPT
  1109. { return container_detail::to_raw_pointer(this->m_holder.start()); }
  1110. //////////////////////////////////////////////
  1111. //
  1112. // modifiers
  1113. //
  1114. //////////////////////////////////////////////
  1115. #if defined(BOOST_CONTAINER_PERFECT_FORWARDING) || defined(BOOST_CONTAINER_DOXYGEN_INVOKED)
  1116. //! <b>Effects</b>: Inserts an object of type T constructed with
  1117. //! std::forward<Args>(args)... in the end of the vector.
  1118. //!
  1119. //! <b>Throws</b>: If memory allocation throws or the in-place constructor throws or
  1120. //! T's move constructor throws.
  1121. //!
  1122. //! <b>Complexity</b>: Amortized constant time.
  1123. template<class ...Args>
  1124. void emplace_back(Args &&...args)
  1125. {
  1126. T* back_pos = container_detail::to_raw_pointer(this->m_holder.start()) + this->m_holder.m_size;
  1127. if (this->m_holder.m_size < this->m_holder.capacity()){
  1128. //There is more memory, just construct a new object at the end
  1129. allocator_traits_type::construct(this->m_holder.alloc(), back_pos, ::boost::forward<Args>(args)...);
  1130. ++this->m_holder.m_size;
  1131. }
  1132. else{
  1133. typedef container_detail::insert_emplace_proxy<Allocator, T*, Args...> type;
  1134. this->priv_forward_range_insert_no_capacity
  1135. (vector_iterator_get_ptr(this->cend()), 1, type(this->m_holder.alloc(), ::boost::forward<Args>(args)...), alloc_version());
  1136. }
  1137. }
  1138. //! <b>Requires</b>: position must be a valid iterator of *this.
  1139. //!
  1140. //! <b>Effects</b>: Inserts an object of type T constructed with
  1141. //! std::forward<Args>(args)... before position
  1142. //!
  1143. //! <b>Throws</b>: If memory allocation throws or the in-place constructor throws or
  1144. //! T's move constructor/assignment throws.
  1145. //!
  1146. //! <b>Complexity</b>: If position is end(), amortized constant time
  1147. //! Linear time otherwise.
  1148. template<class ...Args>
  1149. iterator emplace(const_iterator position, Args && ...args)
  1150. {
  1151. //Just call more general insert(pos, size, value) and return iterator
  1152. typedef container_detail::insert_emplace_proxy<Allocator, T*, Args...> type;
  1153. return this->priv_forward_range_insert( vector_iterator_get_ptr(position), 1, type(this->m_holder.alloc()
  1154. , ::boost::forward<Args>(args)...), alloc_version());
  1155. }
  1156. #else
  1157. #define BOOST_PP_LOCAL_MACRO(n) \
  1158. BOOST_PP_EXPR_IF(n, template<) BOOST_PP_ENUM_PARAMS(n, class P) BOOST_PP_EXPR_IF(n, >) \
  1159. void emplace_back(BOOST_PP_ENUM(n, BOOST_CONTAINER_PP_PARAM_LIST, _)) \
  1160. { \
  1161. T* back_pos = container_detail::to_raw_pointer \
  1162. (this->m_holder.start()) + this->m_holder.m_size; \
  1163. if (this->m_holder.m_size < this->m_holder.capacity()){ \
  1164. allocator_traits_type::construct (this->m_holder.alloc() \
  1165. , back_pos BOOST_PP_ENUM_TRAILING(n, BOOST_CONTAINER_PP_PARAM_FORWARD, _) ); \
  1166. ++this->m_holder.m_size; \
  1167. } \
  1168. else{ \
  1169. container_detail::BOOST_PP_CAT(insert_emplace_proxy_arg, n) \
  1170. <Allocator, T* BOOST_PP_ENUM_TRAILING_PARAMS(n, P)> proxy \
  1171. (this->m_holder.alloc() BOOST_PP_ENUM_TRAILING(n, BOOST_CONTAINER_PP_PARAM_FORWARD, _)); \
  1172. this->priv_forward_range_insert_no_capacity \
  1173. (vector_iterator_get_ptr(this->cend()), 1, proxy, alloc_version()); \
  1174. } \
  1175. } \
  1176. \
  1177. BOOST_PP_EXPR_IF(n, template<) BOOST_PP_ENUM_PARAMS(n, class P) BOOST_PP_EXPR_IF(n, >) \
  1178. iterator emplace(const_iterator pos \
  1179. BOOST_PP_ENUM_TRAILING(n, BOOST_CONTAINER_PP_PARAM_LIST, _)) \
  1180. { \
  1181. container_detail::BOOST_PP_CAT(insert_emplace_proxy_arg, n) \
  1182. <Allocator, T* BOOST_PP_ENUM_TRAILING_PARAMS(n, P)> proxy \
  1183. (this->m_holder.alloc() BOOST_PP_ENUM_TRAILING(n, BOOST_CONTAINER_PP_PARAM_FORWARD, _)); \
  1184. return this->priv_forward_range_insert \
  1185. (container_detail::to_raw_pointer(vector_iterator_get_ptr(pos)), 1, proxy, alloc_version()); \
  1186. } \
  1187. //!
  1188. #define BOOST_PP_LOCAL_LIMITS (0, BOOST_CONTAINER_MAX_CONSTRUCTOR_PARAMETERS)
  1189. #include BOOST_PP_LOCAL_ITERATE()
  1190. #endif //#ifdef BOOST_CONTAINER_PERFECT_FORWARDING
  1191. #if defined(BOOST_CONTAINER_DOXYGEN_INVOKED)
  1192. //! <b>Effects</b>: Inserts a copy of x at the end of the vector.
  1193. //!
  1194. //! <b>Throws</b>: If memory allocation throws or
  1195. //! T's copy/move constructor throws.
  1196. //!
  1197. //! <b>Complexity</b>: Amortized constant time.
  1198. void push_back(const T &x);
  1199. //! <b>Effects</b>: Constructs a new element in the end of the vector
  1200. //! and moves the resources of mx to this new element.
  1201. //!
  1202. //! <b>Throws</b>: If memory allocation throws or
  1203. //! T's move constructor throws.
  1204. //!
  1205. //! <b>Complexity</b>: Amortized constant time.
  1206. void push_back(T &&x);
  1207. #else
  1208. BOOST_MOVE_CONVERSION_AWARE_CATCH(push_back, T, void, priv_push_back)
  1209. #endif
  1210. #if defined(BOOST_CONTAINER_DOXYGEN_INVOKED)
  1211. //! <b>Requires</b>: position must be a valid iterator of *this.
  1212. //!
  1213. //! <b>Effects</b>: Insert a copy of x before position.
  1214. //!
  1215. //! <b>Throws</b>: If memory allocation throws or T's copy/move constructor/assignment throws.
  1216. //!
  1217. //! <b>Complexity</b>: If position is end(), amortized constant time
  1218. //! Linear time otherwise.
  1219. iterator insert(const_iterator position, const T &x);
  1220. //! <b>Requires</b>: position must be a valid iterator of *this.
  1221. //!
  1222. //! <b>Effects</b>: Insert a new element before position with mx's resources.
  1223. //!
  1224. //! <b>Throws</b>: If memory allocation throws.
  1225. //!
  1226. //! <b>Complexity</b>: If position is end(), amortized constant time
  1227. //! Linear time otherwise.
  1228. iterator insert(const_iterator position, T &&x);
  1229. #else
  1230. BOOST_MOVE_CONVERSION_AWARE_CATCH_1ARG(insert, T, iterator, priv_insert, const_iterator, const_iterator)
  1231. #endif
  1232. //! <b>Requires</b>: p must be a valid iterator of *this.
  1233. //!
  1234. //! <b>Effects</b>: Insert n copies of x before pos.
  1235. //!
  1236. //! <b>Returns</b>: an iterator to the first inserted element or p if n is 0.
  1237. //!
  1238. //! <b>Throws</b>: If memory allocation throws or T's copy constructor throws.
  1239. //!
  1240. //! <b>Complexity</b>: Linear to n.
  1241. iterator insert(const_iterator p, size_type n, const T& x)
  1242. {
  1243. container_detail::insert_n_copies_proxy<Allocator, T*> proxy(this->m_holder.alloc(), x);
  1244. return this->priv_forward_range_insert(vector_iterator_get_ptr(p), n, proxy, alloc_version());
  1245. }
  1246. //! <b>Requires</b>: p must be a valid iterator of *this.
  1247. //!
  1248. //! <b>Effects</b>: Insert a copy of the [first, last) range before pos.
  1249. //!
  1250. //! <b>Returns</b>: an iterator to the first inserted element or pos if first == last.
  1251. //!
  1252. //! <b>Throws</b>: If memory allocation throws, T's constructor from a
  1253. //! dereferenced InpIt throws or T's copy/move constructor/assignment throws.
  1254. //!
  1255. //! <b>Complexity</b>: Linear to std::distance [first, last).
  1256. template <class InIt>
  1257. iterator insert(const_iterator pos, InIt first, InIt last
  1258. #if !defined(BOOST_CONTAINER_DOXYGEN_INVOKED)
  1259. , typename container_detail::enable_if_c
  1260. < !container_detail::is_convertible<InIt, size_type>::value
  1261. && container_detail::is_input_iterator<InIt>::value
  1262. >::type * = 0
  1263. #endif
  1264. )
  1265. {
  1266. const size_type n_pos = pos - this->cbegin();
  1267. iterator it(vector_iterator_get_ptr(pos));
  1268. for(;first != last; ++first){
  1269. it = this->emplace(it, *first);
  1270. ++it;
  1271. }
  1272. return iterator(this->m_holder.start() + n_pos);
  1273. }
  1274. #if !defined(BOOST_CONTAINER_DOXYGEN_INVOKED)
  1275. template <class FwdIt>
  1276. iterator insert(const_iterator pos, FwdIt first, FwdIt last
  1277. , typename container_detail::enable_if_c
  1278. < !container_detail::is_convertible<FwdIt, size_type>::value
  1279. && !container_detail::is_input_iterator<FwdIt>::value
  1280. >::type * = 0
  1281. )
  1282. {
  1283. container_detail::insert_range_proxy<Allocator, FwdIt, T*> proxy(this->m_holder.alloc(), first);
  1284. return this->priv_forward_range_insert(vector_iterator_get_ptr(pos), std::distance(first, last), proxy, alloc_version());
  1285. }
  1286. #endif
  1287. //! <b>Effects</b>: Removes the last element from the vector.
  1288. //!
  1289. //! <b>Throws</b>: Nothing.
  1290. //!
  1291. //! <b>Complexity</b>: Constant time.
  1292. void pop_back() BOOST_CONTAINER_NOEXCEPT
  1293. {
  1294. //Destroy last element
  1295. --this->m_holder.m_size;
  1296. this->priv_destroy(container_detail::to_raw_pointer(this->m_holder.start()) + this->m_holder.m_size);
  1297. }
  1298. //! <b>Effects</b>: Erases the element at position pos.
  1299. //!
  1300. //! <b>Throws</b>: Nothing.
  1301. //!
  1302. //! <b>Complexity</b>: Linear to the elements between pos and the
  1303. //! last element. Constant if pos is the last element.
  1304. iterator erase(const_iterator position)
  1305. {
  1306. T *const pos = container_detail::to_raw_pointer(vector_iterator_get_ptr(position));
  1307. T *const beg = container_detail::to_raw_pointer(this->m_holder.start());
  1308. //Move elements forward and destroy last
  1309. this->priv_destroy(::boost::move(pos + 1, beg + this->m_holder.m_size, pos));
  1310. --this->m_holder.m_size;
  1311. return iterator(vector_iterator_get_ptr(position));
  1312. }
  1313. //! <b>Effects</b>: Erases the elements pointed by [first, last).
  1314. //!
  1315. //! <b>Throws</b>: Nothing.
  1316. //!
  1317. //! <b>Complexity</b>: Linear to the distance between first and last
  1318. //! plus linear to the elements between pos and the last element.
  1319. iterator erase(const_iterator first, const_iterator last)
  1320. {
  1321. if (first != last){
  1322. T* const end_pos = container_detail::to_raw_pointer(this->m_holder.start()) + this->m_holder.m_size;
  1323. T* const ptr = container_detail::to_raw_pointer(boost::move
  1324. (container_detail::to_raw_pointer(vector_iterator_get_ptr(last))
  1325. ,end_pos
  1326. ,container_detail::to_raw_pointer(vector_iterator_get_ptr(first))
  1327. ));
  1328. const size_type destroyed = (end_pos - ptr);
  1329. boost::container::destroy_alloc_n(this->get_stored_allocator(), ptr, destroyed);
  1330. this->m_holder.m_size -= destroyed;
  1331. }
  1332. return iterator(vector_iterator_get_ptr(first));
  1333. }
  1334. //! <b>Effects</b>: Swaps the contents of *this and x.
  1335. //!
  1336. //! <b>Throws</b>: Nothing.
  1337. //!
  1338. //! <b>Complexity</b>: Constant.
  1339. void swap(vector& x) BOOST_CONTAINER_NOEXCEPT_IF((!container_detail::is_same<alloc_version, allocator_v0>::value))
  1340. {
  1341. //Just swap internals in case of !allocator_v0. Otherwise, deep swap
  1342. this->m_holder.swap(x.m_holder);
  1343. //And now the allocator
  1344. container_detail::bool_<allocator_traits_type::propagate_on_container_swap::value> flag;
  1345. container_detail::swap_alloc(this->m_holder.alloc(), x.m_holder.alloc(), flag);
  1346. }
  1347. #ifndef BOOST_CONTAINER_DOXYGEN_INVOKED
  1348. //! <b>Effects</b>: Swaps the contents of *this and x.
  1349. //!
  1350. //! <b>Throws</b>: If T's move constructor throws.
  1351. //!
  1352. //! <b>Complexity</b>: Linear
  1353. //!
  1354. //! <b>Note</b>: non-standard extension.
  1355. template<class OtherAllocator>
  1356. void swap(vector<T, OtherAllocator> & x)
  1357. {
  1358. this->m_holder.swap(x.m_holder);
  1359. }
  1360. #endif //#ifndef BOOST_CONTAINER_DOXYGEN_INVOKED
  1361. //! <b>Effects</b>: Erases all the elements of the vector.
  1362. //!
  1363. //! <b>Throws</b>: Nothing.
  1364. //!
  1365. //! <b>Complexity</b>: Linear to the number of elements in the vector.
  1366. void clear() BOOST_CONTAINER_NOEXCEPT
  1367. { this->priv_destroy_all(); }
  1368. /// @cond
  1369. //Absolutely experimental. This function might change, disappear or simply crash!
  1370. template<class BiDirPosConstIt, class BiDirValueIt>
  1371. void insert_ordered_at(size_type element_count, BiDirPosConstIt last_position_it, BiDirValueIt last_value_it)
  1372. {
  1373. const size_type *dummy = 0;
  1374. this->priv_insert_ordered_at(element_count, last_position_it, false, &dummy[0], last_value_it);
  1375. }
  1376. //Absolutely experimental. This function might change, disappear or simply crash!
  1377. template<class BiDirPosConstIt, class BiDirSkipConstIt, class BiDirValueIt>
  1378. void insert_ordered_at(size_type element_count, BiDirPosConstIt last_position_it, BiDirSkipConstIt last_skip_it, BiDirValueIt last_value_it)
  1379. {
  1380. this->priv_insert_ordered_at(element_count, last_position_it, true, last_skip_it, last_value_it);
  1381. }
  1382. private:
  1383. template<class OtherAllocator, class AllocVersion>
  1384. void priv_move_assign(BOOST_RV_REF_BEG vector<T, OtherAllocator> BOOST_RV_REF_END x
  1385. , AllocVersion
  1386. , typename container_detail::enable_if_c
  1387. < container_detail::is_same<AllocVersion, allocator_v0>::value &&
  1388. !container_detail::is_same<OtherAllocator, allocator_type>::value
  1389. >::type * = 0)
  1390. {
  1391. if(this->capacity() < x.size()){
  1392. throw_bad_alloc();
  1393. }
  1394. this->priv_move_assign_impl(boost::move(x), AllocVersion());
  1395. }
  1396. template<class OtherAllocator, class AllocVersion>
  1397. void priv_move_assign(BOOST_RV_REF_BEG vector<T, OtherAllocator> BOOST_RV_REF_END x
  1398. , AllocVersion
  1399. , typename container_detail::enable_if_c
  1400. < !container_detail::is_same<AllocVersion, allocator_v0>::value ||
  1401. container_detail::is_same<OtherAllocator, allocator_type>::value
  1402. >::type * = 0)
  1403. {
  1404. this->priv_move_assign_impl(boost::move(x), AllocVersion());
  1405. }
  1406. template<class OtherAllocator, class AllocVersion>
  1407. void priv_move_assign_impl(BOOST_RV_REF_BEG vector<T, OtherAllocator> BOOST_RV_REF_END x
  1408. , AllocVersion
  1409. , typename container_detail::enable_if_c
  1410. < container_detail::is_same<AllocVersion, allocator_v0>::value
  1411. >::type * = 0)
  1412. {
  1413. T* const this_start = container_detail::to_raw_pointer(m_holder.start());
  1414. T* const other_start = container_detail::to_raw_pointer(x.m_holder.start());
  1415. const size_type this_sz = m_holder.m_size;
  1416. const size_type other_sz = static_cast<size_type>(x.m_holder.m_size);
  1417. boost::container::move_assign_range_alloc_n(this->m_holder.alloc(), other_start, other_sz, this_start, this_sz);
  1418. this->m_holder.m_size = other_sz;
  1419. }
  1420. template<class OtherAllocator, class AllocVersion>
  1421. void priv_move_assign_impl(BOOST_RV_REF_BEG vector<T, OtherAllocator> BOOST_RV_REF_END x
  1422. , AllocVersion
  1423. , typename container_detail::enable_if_c
  1424. < !container_detail::is_same<AllocVersion, allocator_v0>::value
  1425. >::type * = 0)
  1426. {
  1427. //for move constructor, no aliasing (&x != this) is assummed.
  1428. allocator_type &this_alloc = this->m_holder.alloc();
  1429. allocator_type &x_alloc = x.m_holder.alloc();
  1430. //If allocators are equal we can just swap pointers
  1431. if(this_alloc == x_alloc){
  1432. //Destroy objects but retain memory in case x reuses it in the future
  1433. this->clear();
  1434. this->m_holder.swap(x.m_holder);
  1435. //Move allocator if needed
  1436. container_detail::bool_<allocator_traits_type::
  1437. propagate_on_container_move_assignment::value> flag;
  1438. container_detail::move_alloc(this_alloc, x_alloc, flag);
  1439. }
  1440. //If unequal allocators, then do a one by one move
  1441. else{
  1442. //TO-DO: optimize this
  1443. this->assign( boost::make_move_iterator(container_detail::to_raw_pointer(x.m_holder.start()))
  1444. , boost::make_move_iterator(container_detail::to_raw_pointer(x.m_holder.start() + x.m_holder.m_size)));
  1445. }
  1446. }
  1447. template<class AllocVersion>
  1448. void priv_copy_assign(const vector &x, AllocVersion
  1449. , typename container_detail::enable_if_c
  1450. < container_detail::is_same<AllocVersion, allocator_v0>::value
  1451. >::type * = 0)
  1452. {
  1453. T* const this_start = container_detail::to_raw_pointer(m_holder.start());
  1454. T* const other_start = container_detail::to_raw_pointer(x.m_holder.start());
  1455. const size_type this_sz = m_holder.m_size;
  1456. const size_type other_sz = static_cast<size_type>(x.m_holder.m_size);
  1457. boost::container::copy_assign_range_alloc_n(this->m_holder.alloc(), other_start, other_sz, this_start, this_sz);
  1458. this->m_holder.m_size = other_sz;
  1459. }
  1460. template<class AllocVersion>
  1461. void priv_copy_assign(const vector &x, AllocVersion
  1462. , typename container_detail::enable_if_c
  1463. < !container_detail::is_same<AllocVersion, allocator_v0>::value
  1464. >::type * = 0)
  1465. {
  1466. allocator_type &this_alloc = this->m_holder.alloc();
  1467. const allocator_type &x_alloc = x.m_holder.alloc();
  1468. container_detail::bool_<allocator_traits_type::
  1469. propagate_on_container_copy_assignment::value> flag;
  1470. if(flag && this_alloc != x_alloc){
  1471. this->clear();
  1472. this->shrink_to_fit();
  1473. }
  1474. container_detail::assign_alloc(this_alloc, x_alloc, flag);
  1475. this->assign( container_detail::to_raw_pointer(x.m_holder.start())
  1476. , container_detail::to_raw_pointer(x.m_holder.start() + x.m_holder.m_size));
  1477. }
  1478. void priv_reserve(size_type, allocator_v0)
  1479. {
  1480. throw_bad_alloc();
  1481. }
  1482. void priv_reserve(size_type new_cap, allocator_v1)
  1483. {
  1484. //There is not enough memory, allocate a new buffer
  1485. pointer p = this->m_holder.allocate(new_cap);
  1486. //Backwards (and possibly forward) expansion
  1487. #ifdef BOOST_CONTAINER_VECTOR_ALLOC_STATS
  1488. ++this->num_alloc;
  1489. #endif
  1490. T * const raw_beg = container_detail::to_raw_pointer(this->m_holder.start());
  1491. const size_type sz = m_holder.m_size;
  1492. ::boost::container::uninitialized_move_alloc_n_source
  1493. ( this->m_holder.alloc(), raw_beg, sz, container_detail::to_raw_pointer(p) );
  1494. if(this->m_holder.capacity()){
  1495. if(!value_traits::trivial_dctr_after_move)
  1496. boost::container::destroy_alloc_n(this->m_holder.alloc(), raw_beg, sz);
  1497. this->m_holder.deallocate(this->m_holder.start(), this->m_holder.capacity());
  1498. }
  1499. this->m_holder.start(p);
  1500. this->m_holder.capacity(new_cap);
  1501. }
  1502. void priv_reserve(size_type new_cap, allocator_v2)
  1503. {
  1504. //There is not enough memory, allocate a new
  1505. //buffer or expand the old one.
  1506. bool same_buffer_start;
  1507. size_type real_cap = 0;
  1508. std::pair<pointer, bool> ret =
  1509. this->m_holder.allocation_command
  1510. (allocate_new | expand_fwd | expand_bwd,
  1511. new_cap, new_cap, real_cap, this->m_holder.start());
  1512. //Check for forward expansion
  1513. same_buffer_start = ret.second && this->m_holder.start() == ret.first;
  1514. if(same_buffer_start){
  1515. #ifdef BOOST_CONTAINER_VECTOR_ALLOC_STATS
  1516. ++this->num_expand_fwd;
  1517. #endif
  1518. this->m_holder.capacity(real_cap);
  1519. }
  1520. //If there is no forward expansion, move objects
  1521. else{
  1522. //Backwards (and possibly forward) expansion
  1523. if(ret.second){
  1524. //We will reuse insert code, so create a dummy input iterator
  1525. container_detail::insert_range_proxy<Allocator, boost::move_iterator<T*>, T*>
  1526. proxy(this->m_holder.alloc(), ::boost::make_move_iterator((T *)0));
  1527. #ifdef BOOST_CONTAINER_VECTOR_ALLOC_STATS
  1528. ++this->num_expand_bwd;
  1529. #endif
  1530. this->priv_forward_range_insert_expand_backwards
  1531. ( container_detail::to_raw_pointer(ret.first)
  1532. , real_cap
  1533. , container_detail::to_raw_pointer(this->m_holder.start())
  1534. , 0
  1535. , proxy);
  1536. }
  1537. //New buffer
  1538. else{
  1539. #ifdef BOOST_CONTAINER_VECTOR_ALLOC_STATS
  1540. ++this->num_alloc;
  1541. #endif
  1542. T * const raw_beg = container_detail::to_raw_pointer(this->m_holder.start());
  1543. const size_type sz = m_holder.m_size;
  1544. ::boost::container::uninitialized_move_alloc_n_source
  1545. ( this->m_holder.alloc(), raw_beg, sz, container_detail::to_raw_pointer(ret.first) );
  1546. if(this->m_holder.capacity()){
  1547. if(!value_traits::trivial_dctr_after_move)
  1548. boost::container::destroy_alloc_n(this->m_holder.alloc(), raw_beg, sz);
  1549. this->m_holder.deallocate(this->m_holder.start(), this->m_holder.capacity());
  1550. }
  1551. this->m_holder.start(ret.first);
  1552. this->m_holder.capacity(real_cap);
  1553. }
  1554. }
  1555. }
  1556. template<class Proxy>
  1557. void priv_uninitialized_fill(Proxy proxy, size_type n) const
  1558. {
  1559. //Copy first new elements in pos
  1560. proxy.uninitialized_copy_n_and_update
  1561. (container_detail::to_raw_pointer(this->m_holder.start()), n);
  1562. //m_holder.size was already initialized to n in vector_alloc_holder's constructor
  1563. }
  1564. void priv_destroy(value_type* p) BOOST_CONTAINER_NOEXCEPT
  1565. {
  1566. if(!value_traits::trivial_dctr)
  1567. allocator_traits_type::destroy(this->get_stored_allocator(), p);
  1568. }
  1569. void priv_destroy_last_n(size_type n) BOOST_CONTAINER_NOEXCEPT
  1570. {
  1571. T* const end_pos = container_detail::to_raw_pointer(this->m_holder.start()) + this->m_holder.m_size;
  1572. boost::container::destroy_alloc_n(this->get_stored_allocator(), end_pos-n, n);
  1573. this->m_holder.m_size -= n;
  1574. }
  1575. void priv_destroy_all() BOOST_CONTAINER_NOEXCEPT
  1576. {
  1577. boost::container::destroy_alloc_n
  1578. (this->get_stored_allocator(), container_detail::to_raw_pointer(this->m_holder.start()), this->m_holder.m_size);
  1579. this->m_holder.m_size = 0;
  1580. }
  1581. template<class U>
  1582. iterator priv_insert(const const_iterator &p, BOOST_FWD_REF(U) x)
  1583. {
  1584. return this->priv_forward_range_insert
  1585. ( vector_iterator_get_ptr(p), 1, container_detail::get_insert_value_proxy<T*>(this->m_holder.alloc()
  1586. , ::boost::forward<U>(x)), alloc_version());
  1587. }
  1588. void priv_push_back(const T &x)
  1589. {
  1590. if (this->m_holder.m_size < this->m_holder.capacity()){
  1591. //There is more memory, just construct a new object at the end
  1592. allocator_traits_type::construct
  1593. ( this->m_holder.alloc()
  1594. , container_detail::to_raw_pointer(this->m_holder.start() + this->m_holder.m_size)
  1595. , x );
  1596. ++this->m_holder.m_size;
  1597. }
  1598. else{
  1599. container_detail::insert_copy_proxy<Allocator, T*> proxy(this->m_holder.alloc(), x);
  1600. this->priv_forward_range_insert_no_capacity(vector_iterator_get_ptr(this->cend()), 1, proxy, alloc_version());
  1601. }
  1602. }
  1603. void priv_push_back(BOOST_RV_REF(T) x)
  1604. {
  1605. if (this->m_holder.m_size < this->m_holder.capacity()){
  1606. //There is more memory, just construct a new object at the end
  1607. allocator_traits_type::construct
  1608. ( this->m_holder.alloc()
  1609. , container_detail::to_raw_pointer(this->m_holder.start() + this->m_holder.m_size)
  1610. , ::boost::move(x) );
  1611. ++this->m_holder.m_size;
  1612. }
  1613. else{
  1614. container_detail::insert_move_proxy<Allocator, T*> proxy(this->m_holder.alloc(), x);
  1615. this->priv_forward_range_insert_no_capacity(vector_iterator_get_ptr(this->cend()), 1, proxy, alloc_version());
  1616. }
  1617. }
  1618. void priv_shrink_to_fit(allocator_v0) BOOST_CONTAINER_NOEXCEPT
  1619. {}
  1620. void priv_shrink_to_fit(allocator_v1)
  1621. {
  1622. const size_type cp = this->m_holder.capacity();
  1623. if(cp){
  1624. const size_type sz = this->size();
  1625. if(!sz){
  1626. this->m_holder.alloc().deallocate(this->m_holder.m_start, cp);
  1627. this->m_holder.m_start = pointer();
  1628. this->m_holder.m_capacity = 0;
  1629. }
  1630. else if(sz < cp){
  1631. //Allocate a new buffer.
  1632. pointer p = this->m_holder.allocate(sz);
  1633. //We will reuse insert code, so create a dummy input iterator
  1634. container_detail::insert_range_proxy<Allocator, boost::move_iterator<T*>, T*>
  1635. proxy(this->m_holder.alloc(), ::boost::make_move_iterator((T *)0));
  1636. #ifdef BOOST_CONTAINER_VECTOR_ALLOC_STATS
  1637. ++this->num_alloc;
  1638. #endif
  1639. this->priv_forward_range_insert_new_allocation
  1640. ( container_detail::to_raw_pointer(p)
  1641. , sz
  1642. , container_detail::to_raw_pointer(this->m_holder.start())
  1643. , 0
  1644. , proxy);
  1645. }
  1646. }
  1647. }
  1648. void priv_shrink_to_fit(allocator_v2) BOOST_CONTAINER_NOEXCEPT
  1649. {
  1650. const size_type cp = this->m_holder.capacity();
  1651. if(cp){
  1652. const size_type sz = this->size();
  1653. if(!sz){
  1654. this->m_holder.alloc().deallocate(this->m_holder.m_start, cp);
  1655. this->m_holder.m_start = pointer();
  1656. this->m_holder.m_capacity = 0;
  1657. }
  1658. else{
  1659. size_type received_size;
  1660. if(this->m_holder.allocation_command
  1661. ( shrink_in_place | nothrow_allocation
  1662. , cp, sz, received_size, this->m_holder.start()).first){
  1663. this->m_holder.capacity(received_size);
  1664. #ifdef BOOST_CONTAINER_VECTOR_ALLOC_STATS
  1665. ++this->num_shrink;
  1666. #endif
  1667. }
  1668. }
  1669. }
  1670. }
  1671. template <class InsertionProxy>
  1672. iterator priv_forward_range_insert_no_capacity
  1673. (const pointer &pos, const size_type, const InsertionProxy , allocator_v0)
  1674. {
  1675. throw_bad_alloc();
  1676. return iterator(pos);
  1677. }
  1678. template <class InsertionProxy>
  1679. iterator priv_forward_range_insert_no_capacity
  1680. (const pointer &pos, const size_type n, const InsertionProxy insert_range_proxy, allocator_v1)
  1681. {
  1682. //Check if we have enough memory or try to expand current memory
  1683. const size_type n_pos = pos - this->m_holder.start();
  1684. T *const raw_pos = container_detail::to_raw_pointer(pos);
  1685. const size_type new_cap = this->m_holder.next_capacity(n);
  1686. T * new_buf = container_detail::to_raw_pointer(this->m_holder.alloc().allocate(new_cap));
  1687. #ifdef BOOST_CONTAINER_VECTOR_ALLOC_STATS
  1688. ++this->num_alloc;
  1689. #endif
  1690. this->priv_forward_range_insert_new_allocation
  1691. ( new_buf, new_cap, raw_pos, n, insert_range_proxy);
  1692. return iterator(this->m_holder.start() + n_pos);
  1693. }
  1694. template <class InsertionProxy>
  1695. iterator priv_forward_range_insert_no_capacity
  1696. (const pointer &pos, const size_type n, const InsertionProxy insert_range_proxy, allocator_v2)
  1697. {
  1698. //Check if we have enough memory or try to expand current memory
  1699. T *const raw_pos = container_detail::to_raw_pointer(pos);
  1700. const size_type n_pos = raw_pos - container_detail::to_raw_pointer(this->m_holder.start());
  1701. size_type real_cap = 0;
  1702. //There is not enough memory, allocate a new
  1703. //buffer or expand the old one.
  1704. std::pair<pointer, bool> ret = (this->m_holder.allocation_command
  1705. (allocate_new | expand_fwd | expand_bwd,
  1706. this->m_holder.m_size + n, this->m_holder.next_capacity(n), real_cap, this->m_holder.start()));
  1707. //Buffer reallocated
  1708. if(ret.second){
  1709. //Forward expansion, delay insertion
  1710. if(this->m_holder.start() == ret.first){
  1711. #ifdef BOOST_CONTAINER_VECTOR_ALLOC_STATS
  1712. ++this->num_expand_fwd;
  1713. #endif
  1714. this->m_holder.capacity(real_cap);
  1715. //Expand forward
  1716. this->priv_forward_range_insert_expand_forward(raw_pos, n, insert_range_proxy);
  1717. }
  1718. //Backwards (and possibly forward) expansion
  1719. else{
  1720. #ifdef BOOST_CONTAINER_VECTOR_ALLOC_STATS
  1721. ++this->num_expand_bwd;
  1722. #endif
  1723. this->priv_forward_range_insert_expand_backwards
  1724. ( container_detail::to_raw_pointer(ret.first)
  1725. , real_cap, raw_pos, n, insert_range_proxy);
  1726. }
  1727. }
  1728. //New buffer
  1729. else{
  1730. #ifdef BOOST_CONTAINER_VECTOR_ALLOC_STATS
  1731. ++this->num_alloc;
  1732. #endif
  1733. this->priv_forward_range_insert_new_allocation
  1734. ( container_detail::to_raw_pointer(ret.first)
  1735. , real_cap, raw_pos, n, insert_range_proxy);
  1736. }
  1737. return iterator(this->m_holder.start() + n_pos);
  1738. }
  1739. template <class InsertionProxy>
  1740. iterator priv_forward_range_insert
  1741. (const pointer &pos, const size_type n, const InsertionProxy insert_range_proxy, allocator_v0)
  1742. {
  1743. //Check if we have enough memory or try to expand current memory
  1744. const size_type remaining = this->m_holder.capacity() - this->m_holder.m_size;
  1745. if (n > remaining){
  1746. //This will trigger an error
  1747. throw_bad_alloc();
  1748. }
  1749. const size_type n_pos = pos - this->m_holder.start();
  1750. T *const raw_pos = container_detail::to_raw_pointer(pos);
  1751. this->priv_forward_range_insert_expand_forward(raw_pos, n, insert_range_proxy);
  1752. return iterator(this->m_holder.start() + n_pos);
  1753. }
  1754. template <class InsertionProxy>
  1755. iterator priv_forward_range_insert
  1756. (const pointer &pos, const size_type n, const InsertionProxy insert_range_proxy, allocator_v1)
  1757. {
  1758. //Check if we have enough memory or try to expand current memory
  1759. const size_type remaining = this->m_holder.capacity() - this->m_holder.m_size;
  1760. T *const raw_pos = container_detail::to_raw_pointer(pos);
  1761. if (n <= remaining){
  1762. const size_type n_pos = raw_pos - container_detail::to_raw_pointer(this->m_holder.start());
  1763. this->priv_forward_range_insert_expand_forward
  1764. (raw_pos, n, insert_range_proxy);
  1765. return iterator(this->m_holder.start() + n_pos);
  1766. }
  1767. else{
  1768. return this->priv_forward_range_insert_no_capacity(pos, n, insert_range_proxy, alloc_version());
  1769. }
  1770. }
  1771. template <class InsertionProxy>
  1772. iterator priv_forward_range_insert
  1773. (const pointer &pos, const size_type n, const InsertionProxy insert_range_proxy, allocator_v2)
  1774. {
  1775. //Check if we have enough memory or try to expand current memory
  1776. const size_type remaining = this->m_holder.capacity() - this->m_holder.m_size;
  1777. bool same_buffer_start = n <= remaining;
  1778. if (!same_buffer_start){
  1779. return priv_forward_range_insert_no_capacity(pos, n, insert_range_proxy, alloc_version());
  1780. }
  1781. else{
  1782. //Expand forward
  1783. T *const raw_pos = container_detail::to_raw_pointer(pos);
  1784. const size_type n_pos = raw_pos - container_detail::to_raw_pointer(this->m_holder.start());
  1785. this->priv_forward_range_insert_expand_forward(raw_pos, n, insert_range_proxy);
  1786. return iterator(this->m_holder.start() + n_pos);
  1787. }
  1788. }
  1789. template <class InsertionProxy>
  1790. iterator priv_forward_range_insert_at_end
  1791. (const size_type n, const InsertionProxy insert_range_proxy, allocator_v0)
  1792. {
  1793. //Check if we have enough memory or try to expand current memory
  1794. const size_type remaining = this->m_holder.capacity() - this->m_holder.m_size;
  1795. if (n > remaining){
  1796. //This will trigger an error
  1797. throw_bad_alloc();
  1798. }
  1799. this->priv_forward_range_insert_at_end_expand_forward(n, insert_range_proxy);
  1800. return this->end();
  1801. }
  1802. template <class InsertionProxy>
  1803. iterator priv_forward_range_insert_at_end
  1804. (const size_type n, const InsertionProxy insert_range_proxy, allocator_v1)
  1805. {
  1806. return this->priv_forward_range_insert(vector_iterator_get_ptr(this->cend()), n, insert_range_proxy, allocator_v1());
  1807. }
  1808. template <class InsertionProxy>
  1809. iterator priv_forward_range_insert_at_end
  1810. (const size_type n, const InsertionProxy insert_range_proxy, allocator_v2)
  1811. {
  1812. return this->priv_forward_range_insert(vector_iterator_get_ptr(this->cend()), n, insert_range_proxy, allocator_v2());
  1813. }
  1814. //Absolutely experimental. This function might change, disappear or simply crash!
  1815. template<class BiDirPosConstIt, class BiDirSkipConstIt, class BiDirValueIt>
  1816. void priv_insert_ordered_at( size_type element_count, BiDirPosConstIt last_position_it
  1817. , bool do_skip, BiDirSkipConstIt last_skip_it, BiDirValueIt last_value_it)
  1818. {
  1819. const size_type old_size_pos = this->size();
  1820. this->reserve(old_size_pos + element_count);
  1821. T* const begin_ptr = container_detail::to_raw_pointer(this->m_holder.start());
  1822. size_type insertions_left = element_count;
  1823. size_type next_pos = old_size_pos;
  1824. size_type hole_size = element_count;
  1825. //Exception rollback. If any copy throws before the hole is filled, values
  1826. //already inserted/copied at the end of the buffer will be destroyed.
  1827. typename value_traits::ArrayDestructor past_hole_values_destroyer
  1828. (begin_ptr + old_size_pos + element_count, this->m_holder.alloc(), size_type(0u));
  1829. //Loop for each insertion backwards, first moving the elements after the insertion point,
  1830. //then inserting the element.
  1831. while(insertions_left){
  1832. if(do_skip){
  1833. size_type n = *(--last_skip_it);
  1834. std::advance(last_value_it, -difference_type(n));
  1835. }
  1836. const size_type pos = static_cast<size_type>(*(--last_position_it));
  1837. BOOST_ASSERT(pos <= old_size_pos);
  1838. //If needed shift the range after the insertion point and the previous insertion point.
  1839. //Function will take care if the shift crosses the size() boundary, using copy/move
  1840. //or uninitialized copy/move if necessary.
  1841. size_type new_hole_size = (pos != next_pos)
  1842. ? priv_insert_ordered_at_shift_range(pos, next_pos, this->size(), insertions_left)
  1843. : hole_size
  1844. ;
  1845. if(new_hole_size > 0){
  1846. //The hole was reduced by priv_insert_ordered_at_shift_range so expand exception rollback range backwards
  1847. past_hole_values_destroyer.increment_size_backwards(next_pos - pos);
  1848. //Insert the new value in the hole
  1849. allocator_traits_type::construct(this->m_holder.alloc(), begin_ptr + pos + insertions_left - 1, *(--last_value_it));
  1850. --new_hole_size;
  1851. if(new_hole_size == 0){
  1852. //Hole was just filled, disable exception rollback and change vector size
  1853. past_hole_values_destroyer.release();
  1854. this->m_holder.m_size += element_count;
  1855. }
  1856. else{
  1857. //The hole was reduced by the new insertion by one
  1858. past_hole_values_destroyer.increment_size_backwards(size_type(1u));
  1859. }
  1860. }
  1861. else{
  1862. if(hole_size){
  1863. //Hole was just filled by priv_insert_ordered_at_shift_range, disable exception rollback and change vector size
  1864. past_hole_values_destroyer.release();
  1865. this->m_holder.m_size += element_count;
  1866. }
  1867. //Insert the new value in the already constructed range
  1868. begin_ptr[pos + insertions_left - 1] = *(--last_value_it);
  1869. }
  1870. --insertions_left;
  1871. hole_size = new_hole_size;
  1872. next_pos = pos;
  1873. }
  1874. }
  1875. //Takes the range pointed by [first_pos, last_pos) and shifts it to the right
  1876. //by 'shift_count'. 'limit_pos' marks the end of constructed elements.
  1877. //
  1878. //Precondition: first_pos <= last_pos <= limit_pos
  1879. //
  1880. //The shift operation might cross limit_pos so elements to moved beyond limit_pos
  1881. //are uninitialized_moved with an allocator. Other elements are moved.
  1882. //
  1883. //The shift operation might left uninitialized elements after limit_pos
  1884. //and the number of uninitialized elements is returned by the function.
  1885. //
  1886. //Old situation:
  1887. // first_pos last_pos old_limit
  1888. // | | |
  1889. // ____________V_______V__________________V_____________
  1890. //| prefix | range | suffix |raw_mem ~
  1891. //|____________|_______|__________________|_____________~
  1892. //
  1893. //New situation in Case Allocator (hole_size == 0):
  1894. // range is moved through move assignments
  1895. //
  1896. // first_pos last_pos limit_pos
  1897. // | | |
  1898. // ____________V_______V__________________V_____________
  1899. //| prefix' | | | range |suffix'|raw_mem ~
  1900. //|________________+______|___^___|_______|_____________~
  1901. // | |
  1902. // |_>_>_>_>_>^
  1903. //
  1904. //
  1905. //New situation in Case B (hole_size > 0):
  1906. // range is moved through uninitialized moves
  1907. //
  1908. // first_pos last_pos limit_pos
  1909. // | | |
  1910. // ____________V_______V__________________V________________
  1911. //| prefix' | | | [hole] | range |
  1912. //|_______________________________________|________|___^___|
  1913. // | |
  1914. // |_>_>_>_>_>_>_>_>_>_>_>_>_>_>_>_>_>_^
  1915. //
  1916. //New situation in Case C (hole_size == 0):
  1917. // range is moved through move assignments and uninitialized moves
  1918. //
  1919. // first_pos last_pos limit_pos
  1920. // | | |
  1921. // ____________V_______V__________________V___
  1922. //| prefix' | | | range |
  1923. //|___________________________________|___^___|
  1924. // | |
  1925. // |_>_>_>_>_>_>_>_>_>_>_>^
  1926. size_type priv_insert_ordered_at_shift_range
  1927. (size_type first_pos, size_type last_pos, size_type limit_pos, size_type shift_count)
  1928. {
  1929. BOOST_ASSERT(first_pos <= last_pos);
  1930. BOOST_ASSERT(last_pos <= limit_pos);
  1931. //
  1932. T* const begin_ptr = container_detail::to_raw_pointer(this->m_holder.start());
  1933. T* const first_ptr = begin_ptr + first_pos;
  1934. T* const last_ptr = begin_ptr + last_pos;
  1935. size_type hole_size = 0;
  1936. //Case Allocator:
  1937. if((last_pos + shift_count) <= limit_pos){
  1938. //All move assigned
  1939. boost::move_backward(first_ptr, last_ptr, last_ptr + shift_count);
  1940. }
  1941. //Case B:
  1942. else if((first_pos + shift_count) >= limit_pos){
  1943. //All uninitialized_moved
  1944. ::boost::container::uninitialized_move_alloc
  1945. (this->m_holder.alloc(), first_ptr, last_ptr, first_ptr + shift_count);
  1946. hole_size = last_pos + shift_count - limit_pos;
  1947. }
  1948. //Case C:
  1949. else{
  1950. //Some uninitialized_moved
  1951. T* const limit_ptr = begin_ptr + limit_pos;
  1952. T* const boundary_ptr = limit_ptr - shift_count;
  1953. ::boost::container::uninitialized_move_alloc(this->m_holder.alloc(), boundary_ptr, last_ptr, limit_ptr);
  1954. //The rest is move assigned
  1955. boost::move_backward(first_ptr, boundary_ptr, limit_ptr);
  1956. }
  1957. return hole_size;
  1958. }
  1959. private:
  1960. template <class InsertionProxy>
  1961. void priv_forward_range_insert_at_end_expand_forward(const size_type n, InsertionProxy insert_range_proxy)
  1962. {
  1963. T* const old_finish = container_detail::to_raw_pointer(this->m_holder.start()) + this->m_holder.m_size;
  1964. insert_range_proxy.uninitialized_copy_n_and_update(old_finish, n);
  1965. this->m_holder.m_size += n;
  1966. }
  1967. template <class InsertionProxy>
  1968. void priv_forward_range_insert_expand_forward(T* const pos, const size_type n, InsertionProxy insert_range_proxy)
  1969. {
  1970. //n can't be 0, because there is nothing to do in that case
  1971. if(!n) return;
  1972. //There is enough memory
  1973. T* const old_finish = container_detail::to_raw_pointer(this->m_holder.start()) + this->m_holder.m_size;
  1974. const size_type elems_after = old_finish - pos;
  1975. if (!elems_after){
  1976. insert_range_proxy.uninitialized_copy_n_and_update(old_finish, n);
  1977. this->m_holder.m_size += n;
  1978. }
  1979. else if (elems_after >= n){
  1980. //New elements can be just copied.
  1981. //Move to uninitialized memory last objects
  1982. ::boost::container::uninitialized_move_alloc
  1983. (this->m_holder.alloc(), old_finish - n, old_finish, old_finish);
  1984. this->m_holder.m_size += n;
  1985. //Copy previous to last objects to the initialized end
  1986. boost::move_backward(pos, old_finish - n, old_finish);
  1987. //Insert new objects in the pos
  1988. insert_range_proxy.copy_n_and_update(pos, n);
  1989. }
  1990. else {
  1991. //The new elements don't fit in the [pos, end()) range.
  1992. //Copy old [pos, end()) elements to the uninitialized memory (a gap is created)
  1993. ::boost::container::uninitialized_move_alloc(this->m_holder.alloc(), pos, old_finish, pos + n);
  1994. BOOST_TRY{
  1995. //Copy first new elements in pos (gap is still there)
  1996. insert_range_proxy.copy_n_and_update(pos, elems_after);
  1997. //Copy to the beginning of the unallocated zone the last new elements (the gap is closed).
  1998. insert_range_proxy.uninitialized_copy_n_and_update(old_finish, n - elems_after);
  1999. this->m_holder.m_size += n;
  2000. }
  2001. BOOST_CATCH(...){
  2002. boost::container::destroy_alloc_n(this->get_stored_allocator(), pos + n, elems_after);
  2003. BOOST_RETHROW
  2004. }
  2005. BOOST_CATCH_END
  2006. }
  2007. }
  2008. template <class InsertionProxy>
  2009. void priv_forward_range_insert_new_allocation
  2010. (T* const new_start, size_type new_cap, T* const pos, const size_type n, InsertionProxy insert_range_proxy)
  2011. {
  2012. //n can be zero, if we want to reallocate!
  2013. T *new_finish = new_start;
  2014. T *old_finish;
  2015. //Anti-exception rollbacks
  2016. typename value_traits::ArrayDeallocator scoped_alloc(new_start, this->m_holder.alloc(), new_cap);
  2017. typename value_traits::ArrayDestructor constructed_values_destroyer(new_start, this->m_holder.alloc(), 0u);
  2018. //Initialize with [begin(), pos) old buffer
  2019. //the start of the new buffer
  2020. T *old_buffer = container_detail::to_raw_pointer(this->m_holder.start());
  2021. if(old_buffer){
  2022. new_finish = ::boost::container::uninitialized_move_alloc
  2023. (this->m_holder.alloc(), container_detail::to_raw_pointer(this->m_holder.start()), pos, old_finish = new_finish);
  2024. constructed_values_destroyer.increment_size(new_finish - old_finish);
  2025. }
  2026. //Initialize new objects, starting from previous point
  2027. insert_range_proxy.uninitialized_copy_n_and_update(old_finish = new_finish, n);
  2028. new_finish += n;
  2029. constructed_values_destroyer.increment_size(new_finish - old_finish);
  2030. //Initialize from the rest of the old buffer,
  2031. //starting from previous point
  2032. if(old_buffer){
  2033. new_finish = ::boost::container::uninitialized_move_alloc
  2034. (this->m_holder.alloc(), pos, old_buffer + this->m_holder.m_size, new_finish);
  2035. //Destroy and deallocate old elements
  2036. //If there is allocated memory, destroy and deallocate
  2037. if(!value_traits::trivial_dctr_after_move)
  2038. boost::container::destroy_alloc_n(this->get_stored_allocator(), old_buffer, this->m_holder.m_size);
  2039. this->m_holder.alloc().deallocate(this->m_holder.start(), this->m_holder.capacity());
  2040. }
  2041. this->m_holder.start(new_start);
  2042. this->m_holder.m_size = new_finish - new_start;
  2043. this->m_holder.capacity(new_cap);
  2044. //All construction successful, disable rollbacks
  2045. constructed_values_destroyer.release();
  2046. scoped_alloc.release();
  2047. }
  2048. template <class InsertionProxy>
  2049. void priv_forward_range_insert_expand_backwards
  2050. (T* const new_start, const size_type new_capacity,
  2051. T* const pos, const size_type n, InsertionProxy insert_range_proxy)
  2052. {
  2053. //n can be zero to just expand capacity
  2054. //Backup old data
  2055. T* const old_start = container_detail::to_raw_pointer(this->m_holder.start());
  2056. T* const old_finish = old_start + this->m_holder.m_size;
  2057. const size_type old_size = this->m_holder.m_size;
  2058. //We can have 8 possibilities:
  2059. const size_type elemsbefore = static_cast<size_type>(pos - old_start);
  2060. const size_type s_before = static_cast<size_type>(old_start - new_start);
  2061. const size_type before_plus_new = elemsbefore + n;
  2062. //Update the vector buffer information to a safe state
  2063. this->m_holder.start(new_start);
  2064. this->m_holder.capacity(new_capacity);
  2065. this->m_holder.m_size = 0;
  2066. //If anything goes wrong, this object will destroy
  2067. //all the old objects to fulfill previous vector state
  2068. typename value_traits::OldArrayDestructor old_values_destroyer(old_start, this->m_holder.alloc(), old_size);
  2069. //Check if s_before is big enough to hold the beginning of old data + new data
  2070. if(s_before >= before_plus_new){
  2071. //Copy first old values before pos, after that the new objects
  2072. T *const new_elem_pos = ::boost::container::uninitialized_move_alloc(this->m_holder.alloc(), old_start, pos, new_start);
  2073. this->m_holder.m_size = elemsbefore;
  2074. insert_range_proxy.uninitialized_copy_n_and_update(new_elem_pos, n);
  2075. this->m_holder.m_size += n;
  2076. //Check if s_before is so big that even copying the old data + new data
  2077. //there is a gap between the new data and the old data
  2078. const size_type new_size = old_size + n;
  2079. if(s_before >= new_size){
  2080. //Old situation:
  2081. // _________________________________________________________
  2082. //| raw_mem | old_begin | old_end |
  2083. //| __________________________________|___________|_________|
  2084. //
  2085. //New situation:
  2086. // _________________________________________________________
  2087. //| old_begin | new | old_end | raw_mem |
  2088. //|___________|__________|_________|________________________|
  2089. //
  2090. //Now initialize the rest of memory with the last old values
  2091. ::boost::container::uninitialized_move_alloc
  2092. (this->m_holder.alloc(), pos, old_finish, new_start + before_plus_new);
  2093. //All new elements correctly constructed, avoid new element destruction
  2094. this->m_holder.m_size = new_size;
  2095. //Old values destroyed automatically with "old_values_destroyer"
  2096. //when "old_values_destroyer" goes out of scope unless the have trivial
  2097. //destructor after move.
  2098. if(value_traits::trivial_dctr_after_move)
  2099. old_values_destroyer.release();
  2100. }
  2101. //s_before is so big that divides old_end
  2102. else{
  2103. //Old situation:
  2104. // __________________________________________________
  2105. //| raw_mem | old_begin | old_end |
  2106. //| ___________________________|___________|_________|
  2107. //
  2108. //New situation:
  2109. // __________________________________________________
  2110. //| old_begin | new | old_end | raw_mem |
  2111. //|___________|__________|_________|_________________|
  2112. //
  2113. //Now initialize the rest of memory with the last old values
  2114. //All new elements correctly constructed, avoid new element destruction
  2115. const size_type raw_gap = s_before - before_plus_new;
  2116. //Now initialize the rest of s_before memory with the
  2117. //first of elements after new values
  2118. ::boost::container::uninitialized_move_alloc_n
  2119. (this->m_holder.alloc(), pos, raw_gap, new_start + before_plus_new);
  2120. //Update size since we have a contiguous buffer
  2121. this->m_holder.m_size = old_size + s_before;
  2122. //All new elements correctly constructed, avoid old element destruction
  2123. old_values_destroyer.release();
  2124. //Now copy remaining last objects in the old buffer begin
  2125. T * const to_destroy = ::boost::move(pos + raw_gap, old_finish, old_start);
  2126. //Now destroy redundant elements except if they were moved and
  2127. //they have trivial destructor after move
  2128. size_type n_destroy = old_finish - to_destroy;
  2129. if(!value_traits::trivial_dctr_after_move)
  2130. boost::container::destroy_alloc_n(this->get_stored_allocator(), to_destroy, n_destroy);
  2131. this->m_holder.m_size -= n_destroy;
  2132. }
  2133. }
  2134. else{
  2135. //Check if we have to do the insertion in two phases
  2136. //since maybe s_before is not big enough and
  2137. //the buffer was expanded both sides
  2138. //
  2139. //Old situation:
  2140. // _________________________________________________
  2141. //| raw_mem | old_begin + old_end | raw_mem |
  2142. //|_________|_____________________|_________________|
  2143. //
  2144. //New situation with do_after:
  2145. // _________________________________________________
  2146. //| old_begin + new + old_end | raw_mem |
  2147. //|___________________________________|_____________|
  2148. //
  2149. //New without do_after:
  2150. // _________________________________________________
  2151. //| old_begin + new + old_end | raw_mem |
  2152. //|____________________________|____________________|
  2153. //
  2154. const bool do_after = n > s_before;
  2155. //Now we can have two situations: the raw_mem of the
  2156. //beginning divides the old_begin, or the new elements:
  2157. if (s_before <= elemsbefore) {
  2158. //The raw memory divides the old_begin group:
  2159. //
  2160. //If we need two phase construction (do_after)
  2161. //new group is divided in new = new_beg + new_end groups
  2162. //In this phase only new_beg will be inserted
  2163. //
  2164. //Old situation:
  2165. // _________________________________________________
  2166. //| raw_mem | old_begin | old_end | raw_mem |
  2167. //|_________|___________|_________|_________________|
  2168. //
  2169. //New situation with do_after(1):
  2170. //This is not definitive situation, the second phase
  2171. //will include
  2172. // _________________________________________________
  2173. //| old_begin | new_beg | old_end | raw_mem |
  2174. //|___________|_________|_________|_________________|
  2175. //
  2176. //New situation without do_after:
  2177. // _________________________________________________
  2178. //| old_begin | new | old_end | raw_mem |
  2179. //|___________|_____|_________|_____________________|
  2180. //
  2181. //Copy the first part of old_begin to raw_mem
  2182. ::boost::container::uninitialized_move_alloc_n
  2183. (this->m_holder.alloc(), old_start, s_before, new_start);
  2184. //The buffer is all constructed until old_end,
  2185. //release destroyer and update size
  2186. old_values_destroyer.release();
  2187. this->m_holder.m_size = old_size + s_before;
  2188. //Now copy the second part of old_begin overwriting itself
  2189. T *const next = ::boost::move(old_start + s_before, pos, old_start);
  2190. if(do_after){
  2191. //Now copy the new_beg elements
  2192. insert_range_proxy.copy_n_and_update(next, s_before);
  2193. }
  2194. else{
  2195. //Now copy the all the new elements
  2196. insert_range_proxy.copy_n_and_update(next, n);
  2197. //Now displace old_end elements
  2198. T* const move_end = ::boost::move(pos, old_finish, next + n);
  2199. //Destroy remaining moved elements from old_end except if
  2200. //they have trivial destructor after being moved
  2201. const size_type n_destroy = s_before - n;
  2202. if(!value_traits::trivial_dctr_after_move)
  2203. boost::container::destroy_alloc_n(this->get_stored_allocator(), move_end, n_destroy);
  2204. this->m_holder.m_size -= n_destroy;
  2205. }
  2206. }
  2207. else {
  2208. //If we have to expand both sides,
  2209. //we will play if the first new values so
  2210. //calculate the upper bound of new values
  2211. //The raw memory divides the new elements
  2212. //
  2213. //If we need two phase construction (do_after)
  2214. //new group is divided in new = new_beg + new_end groups
  2215. //In this phase only new_beg will be inserted
  2216. //
  2217. //Old situation:
  2218. // _______________________________________________________
  2219. //| raw_mem | old_begin | old_end | raw_mem |
  2220. //|_______________|___________|_________|_________________|
  2221. //
  2222. //New situation with do_after():
  2223. // ____________________________________________________
  2224. //| old_begin | new_beg | old_end | raw_mem |
  2225. //|___________|_______________|_________|______________|
  2226. //
  2227. //New situation without do_after:
  2228. // ______________________________________________________
  2229. //| old_begin | new | old_end | raw_mem |
  2230. //|___________|_____|_________|__________________________|
  2231. //
  2232. //First copy whole old_begin and part of new to raw_mem
  2233. T * const new_pos = ::boost::container::uninitialized_move_alloc
  2234. (this->m_holder.alloc(), old_start, pos, new_start);
  2235. this->m_holder.m_size = elemsbefore;
  2236. const size_type mid_n = s_before - elemsbefore;
  2237. insert_range_proxy.uninitialized_copy_n_and_update(new_pos, mid_n);
  2238. //The buffer is all constructed until old_end,
  2239. //release destroyer
  2240. this->m_holder.m_size = old_size + s_before;
  2241. old_values_destroyer.release();
  2242. if(do_after){
  2243. //Copy new_beg part
  2244. insert_range_proxy.copy_n_and_update(old_start, elemsbefore);
  2245. }
  2246. else{
  2247. //Copy all new elements
  2248. const size_type rest_new = n - mid_n;
  2249. insert_range_proxy.copy_n_and_update(old_start, rest_new);
  2250. T* move_start = old_start + rest_new;
  2251. //Displace old_end
  2252. T* move_end = ::boost::move(pos, old_finish, move_start);
  2253. //Destroy remaining moved elements from old_end except if they
  2254. //have trivial destructor after being moved
  2255. size_type n_destroy = s_before - n;
  2256. if(!value_traits::trivial_dctr_after_move)
  2257. boost::container::destroy_alloc_n(this->get_stored_allocator(), move_end, n_destroy);
  2258. this->m_holder.m_size -= n_destroy;
  2259. }
  2260. }
  2261. //This is only executed if two phase construction is needed
  2262. if(do_after){
  2263. //The raw memory divides the new elements
  2264. //
  2265. //Old situation:
  2266. // ______________________________________________________
  2267. //| raw_mem | old_begin | old_end | raw_mem |
  2268. //|______________|___________|____________|______________|
  2269. //
  2270. //New situation with do_after(1):
  2271. // _______________________________________________________
  2272. //| old_begin + new_beg | new_end |old_end | raw_mem |
  2273. //|__________________________|_________|________|_________|
  2274. //
  2275. //New situation with do_after(2):
  2276. // ______________________________________________________
  2277. //| old_begin + new | old_end |raw |
  2278. //|_______________________________________|_________|____|
  2279. //
  2280. const size_type n_after = n - s_before;
  2281. const size_type elemsafter = old_size - elemsbefore;
  2282. //We can have two situations:
  2283. if (elemsafter >= n_after){
  2284. //The raw_mem from end will divide displaced old_end
  2285. //
  2286. //Old situation:
  2287. // ______________________________________________________
  2288. //| raw_mem | old_begin | old_end | raw_mem |
  2289. //|______________|___________|____________|______________|
  2290. //
  2291. //New situation with do_after(1):
  2292. // _______________________________________________________
  2293. //| old_begin + new_beg | new_end |old_end | raw_mem |
  2294. //|__________________________|_________|________|_________|
  2295. //
  2296. //First copy the part of old_end raw_mem
  2297. T* finish_n = old_finish - n_after;
  2298. ::boost::container::uninitialized_move_alloc
  2299. (this->m_holder.alloc(), finish_n, old_finish, old_finish);
  2300. this->m_holder.m_size += n_after;
  2301. //Displace the rest of old_end to the new position
  2302. boost::move_backward(pos, finish_n, old_finish);
  2303. //Now overwrite with new_end
  2304. //The new_end part is [first + (n - n_after), last)
  2305. insert_range_proxy.copy_n_and_update(pos, n_after);
  2306. }
  2307. else {
  2308. //The raw_mem from end will divide new_end part
  2309. //
  2310. //Old situation:
  2311. // _____________________________________________________________
  2312. //| raw_mem | old_begin | old_end | raw_mem |
  2313. //|______________|___________|____________|_____________________|
  2314. //
  2315. //New situation with do_after(2):
  2316. // _____________________________________________________________
  2317. //| old_begin + new_beg | new_end |old_end | raw_mem |
  2318. //|__________________________|_______________|________|_________|
  2319. //
  2320. const size_type mid_last_dist = n_after - elemsafter;
  2321. //First initialize data in raw memory
  2322. //Copy to the old_end part to the uninitialized zone leaving a gap.
  2323. ::boost::container::uninitialized_move_alloc
  2324. (this->m_holder.alloc(), pos, old_finish, old_finish + mid_last_dist);
  2325. BOOST_TRY{
  2326. //Copy the first part to the already constructed old_end zone
  2327. insert_range_proxy.copy_n_and_update(pos, elemsafter);
  2328. //Copy the rest to the uninitialized zone filling the gap
  2329. insert_range_proxy.uninitialized_copy_n_and_update(old_finish, mid_last_dist);
  2330. this->m_holder.m_size += n_after;
  2331. }
  2332. BOOST_CATCH(...){
  2333. boost::container::destroy_alloc_n(this->get_stored_allocator(), pos, mid_last_dist);
  2334. BOOST_RETHROW
  2335. }
  2336. BOOST_CATCH_END
  2337. /*
  2338. size_type mid_last_dist = n_after - elemsafter;
  2339. //First initialize data in raw memory
  2340. //The new_end part is [first + (n - n_after), last)
  2341. insert_range_proxy.uninitialized_copy_last_and_update(old_finish, elemsafter);
  2342. this->m_holder.m_size += mid_last_dist;
  2343. ::boost::container::uninitialized_move_alloc
  2344. (this->m_holder.alloc(), pos, old_finish, old_finish + mid_last_dist);
  2345. this->m_holder.m_size += n_after - mid_last_dist;
  2346. //Now copy the part of new_end over constructed elements
  2347. insert_range_proxy.copy_remaining_to(pos);*/
  2348. }
  2349. }
  2350. }
  2351. }
  2352. void priv_check_range(size_type n) const
  2353. {
  2354. //If n is out of range, throw an out_of_range exception
  2355. if (n >= this->size()){
  2356. throw_out_of_range("vector::at out of range");
  2357. }
  2358. }
  2359. #ifdef BOOST_CONTAINER_VECTOR_ALLOC_STATS
  2360. public:
  2361. unsigned int num_expand_fwd;
  2362. unsigned int num_expand_bwd;
  2363. unsigned int num_shrink;
  2364. unsigned int num_alloc;
  2365. void reset_alloc_stats()
  2366. { num_expand_fwd = num_expand_bwd = num_alloc = 0, num_shrink = 0; }
  2367. #endif
  2368. /// @endcond
  2369. };
  2370. template <class T, class Allocator>
  2371. inline bool
  2372. operator==(const vector<T, Allocator>& x, const vector<T, Allocator>& y)
  2373. {
  2374. //Check first size and each element if needed
  2375. return x.size() == y.size() && std::equal(x.begin(), x.end(), y.begin());
  2376. }
  2377. template <class T, class Allocator>
  2378. inline bool
  2379. operator!=(const vector<T, Allocator>& x, const vector<T, Allocator>& y)
  2380. {
  2381. //Check first size and each element if needed
  2382. return x.size() != y.size() || !std::equal(x.begin(), x.end(), y.begin());
  2383. }
  2384. template <class T, class Allocator>
  2385. inline bool
  2386. operator<(const vector<T, Allocator>& x, const vector<T, Allocator>& y)
  2387. {
  2388. return std::lexicographical_compare(x.begin(), x.end(), y.begin(), y.end());
  2389. }
  2390. template <class T, class Allocator>
  2391. inline void swap(vector<T, Allocator>& x, vector<T, Allocator>& y)
  2392. { x.swap(y); }
  2393. }}
  2394. /// @cond
  2395. namespace boost {
  2396. //!has_trivial_destructor_after_move<> == true_type
  2397. //!specialization for optimizations
  2398. template <class T, class Allocator>
  2399. struct has_trivial_destructor_after_move<boost::container::vector<T, Allocator> >
  2400. : public ::boost::has_trivial_destructor_after_move<Allocator>
  2401. {};
  2402. }
  2403. //#define BOOST_CONTAINER_PUT_SWAP_OVERLOAD_IN_NAMESPACE_STD
  2404. #ifdef BOOST_CONTAINER_PUT_SWAP_OVERLOAD_IN_NAMESPACE_STD
  2405. namespace std {
  2406. template <class T, class Allocator>
  2407. inline void swap(boost::container::vector<T, Allocator>& x, boost::container::vector<T, Allocator>& y)
  2408. { x.swap(y); }
  2409. } //namespace std {
  2410. #endif
  2411. /// @endcond
  2412. #include <boost/container/detail/config_end.hpp>
  2413. #endif // #ifndef BOOST_CONTAINER_CONTAINER_VECTOR_HPP