devector.hpp 105 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041
  1. //////////////////////////////////////////////////////////////////////////////
  2. //
  3. // (C) Copyright Benedek Thaler 2015-2016
  4. // (C) Copyright Ion Gaztanaga 2019-2020. Distributed under the Boost
  5. // Software License, Version 1.0. (See accompanying file
  6. // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  7. //
  8. // See http://www.boost.org/libs/container for documentation.
  9. //
  10. //////////////////////////////////////////////////////////////////////////////
  11. #ifndef BOOST_CONTAINER_DEVECTOR_HPP
  12. #define BOOST_CONTAINER_DEVECTOR_HPP
  13. #include <boost/container/detail/config_begin.hpp>
  14. #include <boost/container/detail/workaround.hpp>
  15. #include <cstring> // memcpy
  16. #include <boost/assert.hpp>
  17. #include <boost/container/detail/copy_move_algo.hpp>
  18. #include <boost/container/new_allocator.hpp> //new_allocator
  19. #include <boost/container/allocator_traits.hpp> //allocator_traits
  20. #include <boost/container/detail/algorithm.hpp> //equal()
  21. #include <boost/container/throw_exception.hpp>
  22. #include <boost/container/options.hpp>
  23. #include <boost/container/detail/guards_dended.hpp>
  24. #include <boost/container/detail/iterator.hpp>
  25. #include <boost/container/detail/iterators.hpp>
  26. #include <boost/container/detail/destroyers.hpp>
  27. #include <boost/container/detail/min_max.hpp>
  28. #include <boost/container/detail/next_capacity.hpp>
  29. #include <boost/container/detail/alloc_helpers.hpp>
  30. #include <boost/container/detail/advanced_insert_int.hpp>
  31. // move
  32. #if defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES)
  33. #include <boost/move/detail/fwd_macros.hpp>
  34. #endif
  35. #include <boost/move/detail/move_helpers.hpp>
  36. #include <boost/move/adl_move_swap.hpp>
  37. #include <boost/move/iterator.hpp>
  38. #include <boost/move/traits.hpp>
  39. #include <boost/move/utility_core.hpp>
  40. #include <boost/move/detail/to_raw_pointer.hpp>
  41. #include <boost/move/algo/detail/merge.hpp>
  42. //std
  43. #if !defined(BOOST_NO_CXX11_HDR_INITIALIZER_LIST)
  44. #include <initializer_list> //for std::initializer_list
  45. #endif
  46. namespace boost {
  47. namespace container {
  48. #if !defined(BOOST_CONTAINER_DOXYGEN_INVOKED)
  49. struct growth_factor_60;
  50. template<class Options, class AllocatorSizeType>
  51. struct get_devector_opt
  52. {
  53. typedef devector_opt< typename default_if_void<typename Options::growth_factor_type, growth_factor_60>::type
  54. , typename default_if_void<typename Options::stored_size_type, AllocatorSizeType>::type
  55. , default_if_zero<Options::free_fraction, relocate_on_90::value>::value
  56. > type;
  57. };
  58. template<class AllocatorSizeType>
  59. struct get_devector_opt<void, AllocatorSizeType>
  60. {
  61. typedef devector_opt< growth_factor_60, AllocatorSizeType, relocate_on_90::value> type;
  62. };
  63. #endif //#if defined(BOOST_CONTAINER_DOXYGEN_INVOKED)
  64. struct reserve_only_tag_t {};
  65. struct reserve_uninitialized_t {};
  66. struct review_implementation_t {};
  67. //struct unsafe_uninitialized_tag_t {};
  68. /**
  69. * A vector-like sequence container providing front and back operations
  70. * (e.g: `push_front`/`pop_front`/`push_back`/`pop_back`) with amortized constant complexity.
  71. *
  72. * Models the [SequenceContainer], [ReversibleContainer], and [AllocatorAwareContainer] concepts.
  73. *
  74. * **Requires**:
  75. * - `T` shall be [MoveInsertable] into the devector.
  76. * - `T` shall be [Erasable] from any `devector<T, allocator_type, GP>`.
  77. * - `GrowthFactor`, and `Allocator` must model the concepts with the same names or be void.
  78. *
  79. * **Definition**: `T` is `NothrowConstructible` if it's either nothrow move constructible or
  80. * nothrow copy constructible.
  81. *
  82. * **Definition**: `T` is `NothrowAssignable` if it's either nothrow move assignable or
  83. * nothrow copy assignable.
  84. *
  85. * **Exceptions**: The exception specifications assume `T` is nothrow [Destructible].
  86. *
  87. * Most methods providing the strong exception guarantee assume `T` either has a move
  88. * constructor marked noexcept or is [CopyInsertable] into the devector. If it isn't true,
  89. * and the move constructor throws, the guarantee is waived and the effects are unspecified.
  90. *
  91. * In addition to the exceptions specified in the **Throws** clause, the following operations
  92. * of `T` can throw when any of the specified concept is required:
  93. * - [DefaultInsertable][]: Default constructor
  94. * - [MoveInsertable][]: Move constructor
  95. * - [CopyInsertable][]: Copy constructor
  96. * - [DefaultConstructible][]: Default constructor
  97. * - [EmplaceConstructible][]: Constructor selected by the given arguments
  98. * - [MoveAssignable][]: Move assignment operator
  99. * - [CopyAssignable][]: Copy assignment operator
  100. *
  101. * Furthermore, not `noexcept` methods throws whatever the allocator throws
  102. * if memory allocation fails. Such methods also throw `length_error` if the capacity
  103. * exceeds `max_size()`.
  104. *
  105. * **Remark**: If a method invalidates some iterators, it also invalidates references
  106. * and pointers to the elements pointed by the invalidated iterators.
  107. *
  108. *! \tparam Options A type produced from \c boost::container::devector_options.
  109. *
  110. * [SequenceContainer]: http://en.cppreference.com/w/cpp/concept/SequenceContainer
  111. * [ReversibleContainer]: http://en.cppreference.com/w/cpp/concept/ReversibleContainer
  112. * [AllocatorAwareContainer]: http://en.cppreference.com/w/cpp/concept/AllocatorAwareContainer
  113. * [DefaultInsertable]: http://en.cppreference.com/w/cpp/concept/DefaultInsertable
  114. * [MoveInsertable]: http://en.cppreference.com/w/cpp/concept/MoveInsertable
  115. * [CopyInsertable]: http://en.cppreference.com/w/cpp/concept/CopyInsertable
  116. * [Erasable]: http://en.cppreference.com/w/cpp/concept/Erasable
  117. * [DefaultConstructible]: http://en.cppreference.com/w/cpp/concept/DefaultConstructible
  118. * [Destructible]: http://en.cppreference.com/w/cpp/concept/Destructible
  119. * [EmplaceConstructible]: http://en.cppreference.com/w/cpp/concept/EmplaceConstructible
  120. * [MoveAssignable]: http://en.cppreference.com/w/cpp/concept/MoveAssignable
  121. * [CopyAssignable]: http://en.cppreference.com/w/cpp/concept/CopyAssignable
  122. */
  123. template < typename T, class A BOOST_CONTAINER_DOCONLY(= void), class Options BOOST_CONTAINER_DOCONLY(= void)>
  124. class devector
  125. {
  126. #ifndef BOOST_CONTAINER_DOXYGEN_INVOKED
  127. typedef boost::container::allocator_traits
  128. <typename real_allocator<T, A>::type> allocator_traits_type;
  129. typedef typename allocator_traits_type::size_type alloc_size_type;
  130. typedef typename get_devector_opt<Options, alloc_size_type>::type options_type;
  131. typedef typename options_type::growth_factor_type growth_factor_type;
  132. typedef typename options_type::stored_size_type stored_size_type;
  133. BOOST_STATIC_CONSTEXPR std::size_t devector_min_free_fraction =
  134. options_type::free_fraction;
  135. #endif // ifndef BOOST_CONTAINER_DOXYGEN_INVOKED
  136. public:
  137. // Standard Interface Types:
  138. typedef T value_type;
  139. typedef BOOST_CONTAINER_IMPDEF
  140. (typename real_allocator<T BOOST_MOVE_I A>::type) allocator_type;
  141. typedef allocator_type stored_allocator_type;
  142. typedef typename allocator_traits<allocator_type>::pointer pointer;
  143. typedef typename allocator_traits<allocator_type>::const_pointer const_pointer;
  144. typedef typename allocator_traits<allocator_type>::reference reference;
  145. typedef typename allocator_traits<allocator_type>::const_reference const_reference;
  146. typedef typename allocator_traits<allocator_type>::size_type size_type;
  147. typedef typename allocator_traits<allocator_type>::difference_type difference_type;
  148. typedef pointer iterator;
  149. typedef const_pointer const_iterator;
  150. typedef BOOST_CONTAINER_IMPDEF
  151. (boost::container::reverse_iterator<iterator>) reverse_iterator;
  152. typedef BOOST_CONTAINER_IMPDEF
  153. (boost::container::reverse_iterator<const_iterator>) const_reverse_iterator;
  154. #ifndef BOOST_CONTAINER_DOXYGEN_INVOKED
  155. private:
  156. BOOST_COPYABLE_AND_MOVABLE(devector)
  157. // Guard to deallocate buffer on exception
  158. typedef typename detail::allocation_guard<allocator_type> allocation_guard;
  159. // Random access pseudo iterator always yielding to the same result
  160. typedef constant_iterator<T> cvalue_iterator;
  161. static size_type to_internal_capacity(size_type desired_capacity)
  162. {
  163. const size_type rounder = devector_min_free_fraction - 2u;
  164. const size_type divisor = devector_min_free_fraction - 1u;
  165. size_type const nc = ((desired_capacity + rounder) / divisor) * devector_min_free_fraction;
  166. BOOST_ASSERT(desired_capacity <= (nc - nc / devector_min_free_fraction));
  167. return nc;
  168. }
  169. #endif // ifndef BOOST_CONTAINER_DOXYGEN_INVOKED
  170. // Standard Interface
  171. public:
  172. // construct/copy/destroy
  173. /**
  174. * **Effects**: Constructs an empty devector.
  175. *
  176. * **Postcondition**: `empty() && front_free_capacity() == 0
  177. * && back_free_capacity() == 0`.
  178. *
  179. * **Complexity**: Constant.
  180. */
  181. devector() BOOST_NOEXCEPT
  182. : m_()
  183. {}
  184. /**
  185. * **Effects**: Constructs an empty devector, using the specified allocator.
  186. *
  187. * **Postcondition**: `empty() && front_free_capacity() == 0
  188. * && back_free_capacity() == 0`.
  189. *
  190. * **Complexity**: Constant.
  191. */
  192. explicit devector(const allocator_type& allocator) BOOST_NOEXCEPT
  193. : m_(allocator)
  194. {}
  195. /**
  196. * **Effects**: Constructs an empty devector, using the specified allocator
  197. * and reserves `n` slots as if `reserve(n)` was called.
  198. *
  199. * **Postcondition**: `empty() && capacity() >= n`.
  200. *
  201. * **Exceptions**: Strong exception guarantee.
  202. *
  203. * **Complexity**: Constant.
  204. */
  205. devector(size_type n, reserve_only_tag_t, const allocator_type& allocator = allocator_type())
  206. : m_(reserve_only_tag_t(), allocator, to_internal_capacity(n))
  207. {}
  208. /**
  209. * **Effects**: Constructs an empty devector, using the specified allocator
  210. * and reserves at least `front_free_cap + back_free_cap` slots as if `reserve_front(front_cap)` and
  211. * `reserve_back(back_cap)` was called.
  212. *
  213. * **Postcondition**: `empty() && front_free_capacity() >= front_free_cap
  214. * && back_free_capacity() >= back_free_cap`.
  215. *
  216. * **Exceptions**: Strong exception guarantee.
  217. *
  218. * **Complexity**: Constant.
  219. */
  220. devector(size_type front_free_cap, size_type back_free_cap, reserve_only_tag_t, const allocator_type& allocator = allocator_type())
  221. : m_(reserve_only_tag_t(), allocator, front_free_cap, back_free_cap)
  222. {}
  223. /**
  224. * [DefaultInsertable]: http://en.cppreference.com/w/cpp/concept/DefaultInsertable
  225. *
  226. * **Effects**: Constructs a devector with `n` value_initialized elements using the specified allocator.
  227. *
  228. * **Requires**: `T` shall be [DefaultInsertable] into `*this`.
  229. *
  230. * **Postcondition**: `size() == n`.
  231. *
  232. * **Exceptions**: Strong exception guarantee.
  233. *
  234. * **Complexity**: Linear in `n`.
  235. */
  236. explicit devector(size_type n, const allocator_type& allocator = allocator_type())
  237. : m_(reserve_uninitialized_t(), allocator, n)
  238. {
  239. allocation_guard buffer_guard(m_.buffer, m_.capacity, get_allocator_ref());
  240. boost::container::uninitialized_value_init_alloc_n(get_allocator_ref(), n, this->priv_raw_begin());
  241. buffer_guard.release();
  242. BOOST_ASSERT(invariants_ok());
  243. }
  244. /**
  245. * **Effects**: Constructs a devector with `n` default-initialized elements using the specified allocator.
  246. *
  247. * **Requires**: `T` shall be [DefaultInsertable] into `*this`.
  248. *
  249. * **Postcondition**: `size() == n`.
  250. *
  251. * **Exceptions**: Strong exception guarantee.
  252. *
  253. * **Complexity**: Linear in `n`.
  254. */
  255. explicit devector(size_type n, default_init_t, const allocator_type& allocator = allocator_type())
  256. : m_(reserve_uninitialized_t(), allocator, n)
  257. {
  258. allocation_guard buffer_guard(m_.buffer, m_.capacity, get_allocator_ref());
  259. boost::container::uninitialized_default_init_alloc_n(get_allocator_ref(), n, this->priv_raw_begin());
  260. buffer_guard.release();
  261. BOOST_ASSERT(invariants_ok());
  262. }
  263. /**
  264. * [CopyInsertable]: http://en.cppreference.com/w/cpp/concept/CopyInsertable
  265. *
  266. * **Effects**: Constructs a devector with `n` copies of `value`, using the specified allocator.
  267. *
  268. * **Requires**: `T` shall be [CopyInsertable] into `*this`.
  269. *
  270. * **Postcondition**: `size() == n`.
  271. *
  272. * **Exceptions**: Strong exception guarantee.
  273. *
  274. * **Complexity**: Linear in `n`.
  275. */
  276. devector(size_type n, const T& value, const allocator_type& allocator = allocator_type())
  277. : m_(reserve_uninitialized_t(), allocator, n)
  278. {
  279. construct_from_range(cvalue_iterator(value, n), cvalue_iterator());
  280. BOOST_ASSERT(invariants_ok());
  281. }
  282. /**
  283. * **Effects**: Constructs a devector equal to the range `[first,last)`, using the specified allocator.
  284. *
  285. * **Requires**: `T` shall be [EmplaceConstructible] into `*this` from `*first`. If the specified
  286. * iterator does not meet the forward iterator requirements, `T` shall also be [MoveInsertable]
  287. * into `*this`.
  288. *
  289. * **Postcondition**: `size() == boost::container::iterator_distance(first, last)
  290. *
  291. * **Exceptions**: Strong exception guarantee.
  292. *
  293. * **Complexity**: Makes only `N` calls to the copy constructor of `T` (where `N` is the distance between `first`
  294. * and `last`), at most one allocation and no reallocations if iterators first and last are of forward,
  295. * bidirectional, or random access categories. It makes `O(N)` calls to the copy constructor of `T`
  296. * and `O(log(N)) reallocations if they are just input iterators.
  297. *
  298. * **Remarks**: Each iterator in the range `[first,last)` shall be dereferenced exactly once,
  299. * unless an exception is thrown.
  300. *
  301. * [EmplaceConstructible]: http://en.cppreference.com/w/cpp/concept/EmplaceConstructible
  302. * [MoveInsertable]: http://en.cppreference.com/w/cpp/concept/MoveInsertable
  303. */
  304. template <class InputIterator>
  305. devector(InputIterator first, InputIterator last, const allocator_type& allocator = allocator_type()
  306. //Input iterators
  307. BOOST_CONTAINER_DOCIGN(BOOST_MOVE_I typename dtl::disable_if_or
  308. < void
  309. BOOST_MOVE_I dtl::is_convertible<InputIterator BOOST_MOVE_I size_type>
  310. BOOST_MOVE_I dtl::is_not_input_iterator<InputIterator>
  311. >::type * = 0)
  312. )
  313. : m_(allocator)
  314. {
  315. BOOST_CONTAINER_TRY{
  316. while (first != last) {
  317. this->emplace_back(*first++);
  318. }
  319. BOOST_ASSERT(invariants_ok());
  320. }
  321. BOOST_CONTAINER_CATCH(...){
  322. this->destroy_elements(m_.buffer + m_.front_idx, m_.buffer + m_.back_idx);
  323. this->deallocate_buffer();
  324. }
  325. BOOST_CONTAINER_CATCH_END
  326. }
  327. #ifndef BOOST_CONTAINER_DOXYGEN_INVOKED
  328. template <class ForwardIterator>
  329. devector(ForwardIterator first, ForwardIterator last, const allocator_type& allocator = allocator_type()
  330. //Other iterators
  331. BOOST_CONTAINER_DOCIGN(BOOST_MOVE_I typename dtl::disable_if_or
  332. < void
  333. BOOST_MOVE_I dtl::is_convertible<ForwardIterator BOOST_MOVE_I size_type>
  334. BOOST_MOVE_I dtl::is_input_iterator<ForwardIterator>
  335. >::type * = 0)
  336. )
  337. : m_(reserve_uninitialized_t(), allocator, boost::container::iterator_udistance(first, last))
  338. {
  339. this->construct_from_range(first, last);
  340. BOOST_ASSERT(invariants_ok());
  341. }
  342. #endif // ifndef BOOST_CONTAINER_DOXYGEN_INVOKED
  343. /**
  344. * [CopyInsertable]: http://en.cppreference.com/w/cpp/concept/CopyInsertable
  345. *
  346. * **Effects**: Copy constructs a devector.
  347. *
  348. * **Requires**: `T` shall be [CopyInsertable] into `*this`.
  349. *
  350. * **Postcondition**: `this->size() == x.size()`.
  351. *
  352. * **Exceptions**: Strong exception guarantee.
  353. *
  354. * **Complexity**: Linear in the size of `x`.
  355. */
  356. devector(const devector& x)
  357. : m_(reserve_uninitialized_t(), allocator_traits_type::select_on_container_copy_construction(x.get_allocator_ref()), x.size())
  358. {
  359. this->construct_from_range(x.begin(), x.end());
  360. BOOST_ASSERT(invariants_ok());
  361. }
  362. /**
  363. * [CopyInsertable]: http://en.cppreference.com/w/cpp/concept/CopyInsertable
  364. *
  365. * **Effects**: Copy constructs a devector, using the specified allocator.
  366. *
  367. * **Requires**: `T` shall be [CopyInsertable] into `*this`.
  368. *
  369. * **Postcondition**: `*this == x`.
  370. *
  371. * **Exceptions**: Strong exception guarantee.
  372. *
  373. * **Complexity**: Linear in the size of `x`.
  374. */
  375. devector(const devector& x, const allocator_type& allocator)
  376. : m_(reserve_uninitialized_t(), allocator, x.size())
  377. {
  378. this->construct_from_range(x.begin(), x.end());
  379. BOOST_ASSERT(invariants_ok());
  380. }
  381. /**
  382. * **Effects**: Moves `rhs`'s resources to `*this`.
  383. *
  384. * **Throws**: Nothing.
  385. *
  386. * **Postcondition**: *this has the same value `rhs` had before the operation.
  387. * `rhs` is left in an unspecified but valid state.
  388. *
  389. * **Exceptions**: Strong exception guarantee if not `noexcept`.
  390. *
  391. * **Complexity**: Constant.
  392. */
  393. devector(BOOST_RV_REF(devector) rhs) BOOST_NOEXCEPT_OR_NOTHROW
  394. : m_(::boost::move(rhs.m_))
  395. {
  396. BOOST_ASSERT( invariants_ok());
  397. BOOST_ASSERT(rhs.invariants_ok());
  398. }
  399. /**
  400. * **Effects**: Moves `rhs`'s resources to `*this`, using the specified allocator.
  401. *
  402. * **Throws**: If allocation or T's move constructor throws.
  403. *
  404. * **Postcondition**: *this has the same value `rhs` had before the operation.
  405. * `rhs` is left in an unspecified but valid state.
  406. *
  407. * **Exceptions**: Strong exception guarantee if not `noexcept`.
  408. *
  409. * **Complexity**: Linear if allocator != rhs.get_allocator(), otherwise constant.
  410. */
  411. devector(BOOST_RV_REF(devector) rhs, const allocator_type& allocator)
  412. : m_(review_implementation_t(), allocator, rhs.m_.buffer, rhs.m_.front_idx, rhs.m_.back_idx, rhs.m_.capacity)
  413. {
  414. // TODO should move elems-by-elems if the two allocators differ
  415. // buffer is already acquired, reset rhs
  416. rhs.m_.capacity = 0u;
  417. rhs.m_.buffer = pointer();
  418. rhs.m_.front_idx = 0;
  419. rhs.m_.back_idx = 0;
  420. BOOST_ASSERT( invariants_ok());
  421. BOOST_ASSERT(rhs.invariants_ok());
  422. }
  423. #if !defined(BOOST_NO_CXX11_HDR_INITIALIZER_LIST)
  424. /**
  425. * **Equivalent to**: `devector(il.begin(), il.end(), allocator)`.
  426. */
  427. devector(const std::initializer_list<T>& il, const allocator_type& allocator = allocator_type())
  428. : m_(reserve_uninitialized_t(), allocator, il.size())
  429. {
  430. this->construct_from_range(il.begin(), il.end());
  431. BOOST_ASSERT(invariants_ok());
  432. }
  433. #endif
  434. /**
  435. * **Effects**: Destroys the devector. All stored values are destroyed and
  436. * used memory, if any, deallocated.
  437. *
  438. * **Complexity**: Linear in the size of `*this`.
  439. */
  440. ~devector() BOOST_NOEXCEPT
  441. {
  442. destroy_elements(m_.buffer + m_.front_idx, m_.buffer + m_.back_idx);
  443. deallocate_buffer();
  444. }
  445. /**
  446. * **Effects**: Copies elements of `x` to `*this`. Previously
  447. * held elements get copy assigned to or destroyed.
  448. *
  449. * **Requires**: `T` shall be [CopyInsertable] into `*this`.
  450. *
  451. * **Postcondition**: `this->size() == x.size()`, the elements of
  452. * `*this` are copies of elements in `x` in the same order.
  453. *
  454. * **Returns**: `*this`.
  455. *
  456. * **Exceptions**: Strong exception guarantee if `T` is `NothrowConstructible`
  457. * and the allocator is allowed to be propagated
  458. * ([propagate_on_container_copy_assignment] is true),
  459. * Basic exception guarantee otherwise.
  460. *
  461. * **Complexity**: Linear in the size of `x` and `*this`.
  462. *
  463. * [CopyInsertable]: http://en.cppreference.com/w/cpp/concept/CopyInsertable
  464. * [propagate_on_container_copy_assignment]: http://en.cppreference.com/w/cpp/memory/allocator_traits
  465. */
  466. inline devector& operator=(BOOST_COPY_ASSIGN_REF(devector) rhs)
  467. {
  468. const devector &x = rhs;
  469. if (this == &x) { return *this; } // skip self
  470. const bool do_propagate = allocator_traits_type::propagate_on_container_copy_assignment::value;
  471. BOOST_IF_CONSTEXPR(do_propagate)
  472. {
  473. allocator_type &this_alloc = this->get_allocator_ref();
  474. const allocator_type &other_alloc = x.get_allocator_ref();
  475. if (this_alloc != other_alloc)
  476. {
  477. // new allocator cannot free existing storage
  478. this->clear();
  479. this->deallocate_buffer();
  480. m_.capacity = 0u;
  481. m_.buffer = pointer();
  482. }
  483. dtl::bool_<do_propagate> flag;
  484. dtl::assign_alloc(this_alloc, other_alloc, flag);
  485. }
  486. size_type n = x.size();
  487. if (m_.capacity >= n)
  488. {
  489. this->overwrite_buffer(x.begin(), x.end());
  490. }
  491. else
  492. {
  493. this->allocate_and_copy_range(x.begin(), x.end());
  494. }
  495. BOOST_ASSERT(invariants_ok());
  496. return *this;
  497. }
  498. /**
  499. * [MoveInsertable]: http://en.cppreference.com/w/cpp/concept/MoveInsertable
  500. *
  501. * **Effects**: Moves elements of `x` to `*this`. Previously
  502. * held elements get move/copy assigned to or destroyed.
  503. *
  504. * **Requires**: `T` shall be [MoveInsertable] into `*this`.
  505. *
  506. * **Postcondition**: `x` is left in an unspecified but valid state.
  507. *
  508. * **Returns**: `*this`.
  509. *
  510. * **Exceptions**: Basic exception guarantee if not `noexcept`.
  511. *
  512. * **Complexity**: Constant if allocator_traits_type::
  513. * propagate_on_container_move_assignment is true or
  514. * this->get>allocator() == x.get_allocator(). Linear otherwise.
  515. */
  516. devector& operator=(BOOST_RV_REF(devector) x)
  517. BOOST_NOEXCEPT_IF(allocator_traits_type::propagate_on_container_move_assignment::value
  518. || allocator_traits_type::is_always_equal::value)
  519. {
  520. if (BOOST_LIKELY(this != &x)) {
  521. //We know resources can be transferred at comiple time if both allocators are
  522. //always equal or the allocator is going to be propagated
  523. const bool can_steal_resources_alloc
  524. = allocator_traits_type::propagate_on_container_move_assignment::value
  525. || allocator_traits_type::is_always_equal::value;
  526. dtl::bool_<can_steal_resources_alloc> flag;
  527. this->priv_move_assign(boost::move(x), flag);
  528. }
  529. BOOST_ASSERT(invariants_ok());
  530. return *this;
  531. }
  532. #if !defined(BOOST_NO_CXX11_HDR_INITIALIZER_LIST)
  533. /**
  534. * **Effects**: Copies elements of `il` to `*this`. Previously
  535. * held elements get copy assigned to or destroyed.
  536. *
  537. * **Requires**: `T` shall be [CopyInsertable] into `*this` and [CopyAssignable].
  538. *
  539. * **Postcondition**: `this->size() == il.size()`, the elements of
  540. * `*this` are copies of elements in `il` in the same order.
  541. *
  542. * **Exceptions**: Strong exception guarantee if `T` is nothrow copy assignable
  543. * from `T` and `NothrowConstructible`, Basic exception guarantee otherwise.
  544. *
  545. * **Returns**: `*this`.
  546. *
  547. * **Complexity**: Linear in the size of `il` and `*this`.
  548. *
  549. * [CopyInsertable]: http://en.cppreference.com/w/cpp/concept/CopyInsertable
  550. * [CopyAssignable]: http://en.cppreference.com/w/cpp/concept/CopyAssignable
  551. */
  552. inline devector& operator=(std::initializer_list<T> il)
  553. {
  554. this->assign(il.begin(), il.end());
  555. return *this;
  556. }
  557. #endif
  558. /**
  559. * **Effects**: Replaces elements of `*this` with a copy of `[first,last)`.
  560. * Previously held elements get copy assigned to or destroyed.
  561. *
  562. * **Requires**: `T` shall be [EmplaceConstructible] from `*first`. If the specified iterator
  563. * does not meet the forward iterator requirements, `T` shall be also [MoveInsertable] into `*this`.
  564. *
  565. * **Precondition**: `first` and `last` are not iterators into `*this`.
  566. *
  567. * **Postcondition**: `size() == N`, where `N` is the distance between `first` and `last`.
  568. *
  569. * **Exceptions**: Strong exception guarantee if `T` is nothrow copy assignable
  570. * from `*first` and `NothrowConstructible`, Basic exception guarantee otherwise.
  571. *
  572. * **Complexity**: Linear in the distance between `first` and `last`.
  573. * Makes a single reallocation at most if the iterators `first` and `last`
  574. * are of forward, bidirectional, or random access categories. It makes
  575. * `O(log(N))` reallocations if they are just input iterators.
  576. *
  577. * **Remarks**: Each iterator in the range `[first,last)` shall be dereferenced exactly once,
  578. * unless an exception is thrown.
  579. *
  580. * [EmplaceConstructible]: http://en.cppreference.com/w/cpp/concept/EmplaceConstructible
  581. * [MoveInsertable]: http://en.cppreference.com/w/cpp/concept/MoveInsertable
  582. */
  583. template <class InputIterator>
  584. void assign(InputIterator first, InputIterator last
  585. //Input iterators
  586. BOOST_CONTAINER_DOCIGN(BOOST_MOVE_I typename dtl::disable_if_or
  587. < void
  588. BOOST_MOVE_I dtl::is_convertible<InputIterator BOOST_MOVE_I size_type>
  589. BOOST_MOVE_I dtl::is_not_input_iterator<InputIterator>
  590. >::type * = 0)
  591. )
  592. {
  593. first = overwrite_buffer_impl(first, last, dtl::false_());
  594. while (first != last)
  595. {
  596. this->emplace_back(*first++);
  597. }
  598. }
  599. #ifndef BOOST_CONTAINER_DOXYGEN_INVOKED
  600. template <class ForwardIterator>
  601. void assign(ForwardIterator first, ForwardIterator last
  602. //Other iterators
  603. BOOST_CONTAINER_DOCIGN(BOOST_MOVE_I typename dtl::disable_if_or
  604. < void
  605. BOOST_MOVE_I dtl::is_convertible<ForwardIterator BOOST_MOVE_I size_type>
  606. BOOST_MOVE_I dtl::is_input_iterator<ForwardIterator>
  607. >::type * = 0)
  608. )
  609. {
  610. const size_type n = boost::container::iterator_udistance(first, last);
  611. if (m_.capacity >= n)
  612. {
  613. overwrite_buffer(first, last);
  614. }
  615. else
  616. {
  617. allocate_and_copy_range(first, last);
  618. }
  619. BOOST_ASSERT(invariants_ok());
  620. }
  621. #endif // ifndef BOOST_CONTAINER_DOXYGEN_INVOKED
  622. /**
  623. * **Effects**: Replaces elements of `*this` with `n` copies of `u`.
  624. * Previously held elements get copy assigned to or destroyed.
  625. *
  626. * **Requires**: `T` shall be [CopyInsertable] into `*this` and
  627. * [CopyAssignable].
  628. *
  629. * **Precondition**: `u` is not a reference into `*this`.
  630. *
  631. * **Postcondition**: `size() == n` and the elements of
  632. * `*this` are copies of `u`.
  633. *
  634. * **Exceptions**: Strong exception guarantee if `T` is nothrow copy assignable
  635. * from `u` and `NothrowConstructible`, Basic exception guarantee otherwise.
  636. *
  637. * **Complexity**: Linear in `n` and the size of `*this`.
  638. *
  639. * [CopyInsertable]: http://en.cppreference.com/w/cpp/concept/CopyInsertable
  640. * [CopyAssignable]: http://en.cppreference.com/w/cpp/concept/CopyAssignable
  641. */
  642. inline void assign(size_type n, const T& u)
  643. {
  644. cvalue_iterator first(u, n);
  645. cvalue_iterator last;
  646. this->assign(first, last);
  647. }
  648. #if !defined(BOOST_NO_CXX11_HDR_INITIALIZER_LIST)
  649. /** **Equivalent to**: `assign(il.begin(), il.end())`. */
  650. inline void assign(std::initializer_list<T> il)
  651. {
  652. this->assign(il.begin(), il.end());
  653. }
  654. #endif
  655. /**
  656. * **Returns**: A copy of the allocator associated with the container.
  657. *
  658. * **Complexity**: Constant.
  659. */
  660. BOOST_CONTAINER_ATTRIBUTE_NODISCARD inline
  661. allocator_type get_allocator() const BOOST_NOEXCEPT
  662. {
  663. return static_cast<const allocator_type&>(m_);
  664. }
  665. BOOST_CONTAINER_ATTRIBUTE_NODISCARD inline
  666. const allocator_type &get_stored_allocator() const BOOST_NOEXCEPT
  667. {
  668. return static_cast<const allocator_type&>(m_);
  669. }
  670. BOOST_CONTAINER_ATTRIBUTE_NODISCARD inline
  671. allocator_type &get_stored_allocator() BOOST_NOEXCEPT
  672. {
  673. return static_cast<allocator_type&>(m_);
  674. }
  675. // iterators
  676. /**
  677. * **Returns**: A iterator pointing to the first element in the devector,
  678. * or the past the end iterator if the devector is empty.
  679. *
  680. * **Complexity**: Constant.
  681. */
  682. BOOST_CONTAINER_ATTRIBUTE_NODISCARD inline
  683. iterator begin() BOOST_NOEXCEPT
  684. {
  685. return m_.buffer + m_.front_idx;
  686. }
  687. /**
  688. * **Returns**: A constant iterator pointing to the first element in the devector,
  689. * or the past the end iterator if the devector is empty.
  690. *
  691. * **Complexity**: Constant.
  692. */
  693. BOOST_CONTAINER_ATTRIBUTE_NODISCARD inline
  694. const_iterator begin() const BOOST_NOEXCEPT
  695. {
  696. return m_.buffer + m_.front_idx;
  697. }
  698. /**
  699. * **Returns**: An iterator pointing past the last element of the container.
  700. *
  701. * **Complexity**: Constant.
  702. */
  703. BOOST_CONTAINER_ATTRIBUTE_NODISCARD inline
  704. iterator end() BOOST_NOEXCEPT
  705. {
  706. return m_.buffer + m_.back_idx;
  707. }
  708. /**
  709. * **Returns**: A constant iterator pointing past the last element of the container.
  710. *
  711. * **Complexity**: Constant.
  712. */
  713. BOOST_CONTAINER_ATTRIBUTE_NODISCARD inline
  714. const_iterator end() const BOOST_NOEXCEPT
  715. {
  716. return m_.buffer + m_.back_idx;
  717. }
  718. /**
  719. * **Returns**: A reverse iterator pointing to the first element in the reversed devector,
  720. * or the reverse past the end iterator if the devector is empty.
  721. *
  722. * **Complexity**: Constant.
  723. */
  724. BOOST_CONTAINER_ATTRIBUTE_NODISCARD inline
  725. reverse_iterator rbegin() BOOST_NOEXCEPT
  726. {
  727. return reverse_iterator(m_.buffer + m_.back_idx);
  728. }
  729. /**
  730. * **Returns**: A constant reverse iterator
  731. * pointing to the first element in the reversed devector,
  732. * or the reverse past the end iterator if the devector is empty.
  733. *
  734. * **Complexity**: Constant.
  735. */
  736. BOOST_CONTAINER_ATTRIBUTE_NODISCARD inline
  737. const_reverse_iterator rbegin() const BOOST_NOEXCEPT
  738. {
  739. return const_reverse_iterator(m_.buffer + m_.back_idx);
  740. }
  741. /**
  742. * **Returns**: A reverse iterator pointing past the last element in the
  743. * reversed container, or to the beginning of the reversed container if it's empty.
  744. *
  745. * **Complexity**: Constant.
  746. */
  747. BOOST_CONTAINER_ATTRIBUTE_NODISCARD inline
  748. reverse_iterator rend() BOOST_NOEXCEPT
  749. {
  750. return reverse_iterator(m_.buffer + m_.front_idx);
  751. }
  752. /**
  753. * **Returns**: A constant reverse iterator pointing past the last element in the
  754. * reversed container, or to the beginning of the reversed container if it's empty.
  755. *
  756. * **Complexity**: Constant.
  757. */
  758. BOOST_CONTAINER_ATTRIBUTE_NODISCARD inline
  759. const_reverse_iterator rend() const BOOST_NOEXCEPT
  760. {
  761. return const_reverse_iterator(m_.buffer + m_.front_idx);
  762. }
  763. /**
  764. * **Returns**: A constant iterator pointing to the first element in the devector,
  765. * or the past the end iterator if the devector is empty.
  766. *
  767. * **Complexity**: Constant.
  768. */
  769. BOOST_CONTAINER_ATTRIBUTE_NODISCARD inline
  770. const_iterator cbegin() const BOOST_NOEXCEPT
  771. {
  772. return m_.buffer + m_.front_idx;
  773. }
  774. /**
  775. * **Returns**: A constant iterator pointing past the last element of the container.
  776. *
  777. * **Complexity**: Constant.
  778. */
  779. const_iterator cend() const BOOST_NOEXCEPT
  780. {
  781. return m_.buffer + m_.back_idx;
  782. }
  783. /**
  784. * **Returns**: A constant reverse iterator
  785. * pointing to the first element in the reversed devector,
  786. * or the reverse past the end iterator if the devector is empty.
  787. *
  788. * **Complexity**: Constant.
  789. */
  790. BOOST_CONTAINER_ATTRIBUTE_NODISCARD inline
  791. const_reverse_iterator crbegin() const BOOST_NOEXCEPT
  792. {
  793. return const_reverse_iterator(m_.buffer + m_.back_idx);
  794. }
  795. /**
  796. * **Returns**: A constant reverse iterator pointing past the last element in the
  797. * reversed container, or to the beginning of the reversed container if it's empty.
  798. *
  799. * **Complexity**: Constant.
  800. */
  801. BOOST_CONTAINER_ATTRIBUTE_NODISCARD inline
  802. const_reverse_iterator crend() const BOOST_NOEXCEPT
  803. {
  804. return const_reverse_iterator(m_.buffer + m_.front_idx);
  805. }
  806. // capacity
  807. /**
  808. * **Returns**: True, if `size() == 0`, false otherwise.
  809. *
  810. * **Complexity**: Constant.
  811. */
  812. BOOST_CONTAINER_ATTRIBUTE_NODISCARD inline
  813. bool empty() const BOOST_NOEXCEPT
  814. {
  815. return m_.front_idx == m_.back_idx;
  816. }
  817. /**
  818. * **Returns**: The number of elements the devector contains.
  819. *
  820. * **Complexity**: Constant.
  821. */
  822. BOOST_CONTAINER_ATTRIBUTE_NODISCARD inline
  823. size_type size() const BOOST_NOEXCEPT
  824. {
  825. return size_type(m_.back_idx - m_.front_idx);
  826. }
  827. /**
  828. * **Returns**: The maximum number of elements the devector could possibly hold.
  829. *
  830. * **Complexity**: Constant.
  831. */
  832. BOOST_CONTAINER_ATTRIBUTE_NODISCARD inline
  833. size_type max_size() const BOOST_NOEXCEPT
  834. {
  835. size_type alloc_max = allocator_traits_type::max_size(get_allocator_ref());
  836. size_type size_type_max = (size_type)-1;
  837. return (alloc_max <= size_type_max) ? size_type(alloc_max) : size_type_max;
  838. }
  839. /**
  840. * **Returns**: The *minimum* number of elements that can be inserted into devector using
  841. * position-based insertions without requiring a reallocation. Note that, unlike in
  842. * typical sequence containers like `vector`, `capacity()`, `capacity()` can be smaller than `size()`.
  843. * This can happen if a user inserts elements in a particular way (usually inserting at
  844. * front up to front_free_capacity() and at back up to back_free_capacity()).
  845. *
  846. * **Complexity**: Constant.
  847. */
  848. BOOST_CONTAINER_ATTRIBUTE_NODISCARD inline
  849. size_type capacity() const BOOST_NOEXCEPT
  850. {
  851. size_type const cap_reserve = m_.capacity/devector_min_free_fraction;
  852. return m_.capacity > cap_reserve ? (m_.capacity - cap_reserve) : 0u;
  853. }
  854. /**
  855. * **Returns**: The total number of elements that can be pushed to the front of the
  856. * devector without requiring reallocation.
  857. *
  858. * **Complexity**: Constant.
  859. */
  860. BOOST_CONTAINER_ATTRIBUTE_NODISCARD inline
  861. size_type front_free_capacity() const BOOST_NOEXCEPT
  862. {
  863. return m_.front_idx;
  864. }
  865. /**
  866. * **Returns**: The total number of elements that can be pushed to the back of the
  867. * devector without requiring reallocation.
  868. *
  869. * **Complexity**: Constant.
  870. */
  871. BOOST_CONTAINER_ATTRIBUTE_NODISCARD inline
  872. size_type back_free_capacity() const BOOST_NOEXCEPT
  873. {
  874. return size_type(m_.capacity - m_.back_idx);
  875. }
  876. /**
  877. * **Effects**: If `sz` is greater than the size of `*this`,
  878. * additional value-initialized elements are inserted. Invalidates iterators
  879. * if reallocation is needed. If `sz` is smaller than than the size of `*this`,
  880. * elements are erased from the extremes.
  881. *
  882. * **Requires**: T shall be [MoveInsertable] into *this and [DefaultConstructible].
  883. *
  884. * **Postcondition**: `sz == size()`.
  885. *
  886. * **Exceptions**: Strong exception guarantee.
  887. *
  888. * **Complexity**: Linear in the size of `*this` and `sz`.
  889. *
  890. * [MoveInsertable]: http://en.cppreference.com/w/cpp/concept/MoveInsertable
  891. * [DefaultConstructible]: http://en.cppreference.com/w/cpp/concept/DefaultConstructible
  892. */
  893. inline void resize(size_type sz)
  894. {
  895. this->resize_back(sz);
  896. }
  897. /**
  898. * **Effects**: Same as resize(sz) but creates default-initialized
  899. * value-initialized.
  900. */
  901. inline void resize(size_type sz, default_init_t)
  902. {
  903. this->resize_back(sz, default_init);
  904. }
  905. /**
  906. * [CopyInsertable]: http://en.cppreference.com/w/cpp/concept/CopyInsertable
  907. *
  908. * **Effects**: If `sz` is greater than the size of `*this`,
  909. * copies of `c` are inserted at extremes.
  910. * If `sz` is smaller than than the size of `*this`,
  911. * elements are popped from the extremes.
  912. *
  913. * **Postcondition**: `sz == size()`.
  914. *
  915. * **Requires**: `T` shall be [CopyInsertable] into `*this`.
  916. *
  917. * **Exceptions**: Strong exception guarantee.
  918. *
  919. * **Complexity**: Linear in the size of `*this` and `sz`.
  920. */
  921. inline void resize(size_type sz, const T& c)
  922. {
  923. this->resize_back(sz, c);
  924. }
  925. /**
  926. * **Effects**: If `sz` is greater than the size of `*this`,
  927. * additional value-initialized elements are inserted
  928. * to the front. Invalidates iterators if reallocation is needed.
  929. * If `sz` is smaller than than the size of `*this`,
  930. * elements are popped from the front.
  931. *
  932. * **Requires**: T shall be [MoveInsertable] into *this and [DefaultConstructible].
  933. *
  934. * **Postcondition**: `sz == size()`.
  935. *
  936. * **Exceptions**: Strong exception guarantee.
  937. *
  938. * **Complexity**: Linear in the size of `*this` and `sz`.
  939. *
  940. * [MoveInsertable]: http://en.cppreference.com/w/cpp/concept/MoveInsertable
  941. * [DefaultConstructible]: http://en.cppreference.com/w/cpp/concept/DefaultConstructible
  942. */
  943. inline void resize_front(size_type sz)
  944. {
  945. resize_front_impl(sz);
  946. BOOST_ASSERT(invariants_ok());
  947. }
  948. /**
  949. * **Effects**: If `sz` is greater than the size of `*this`,
  950. * additional value-initialized elements are inserted
  951. * to the front. Invalidates iterators if reallocation is needed.
  952. * If `sz` is smaller than than the size of `*this`,
  953. * elements are popped from the front.
  954. *
  955. * **Requires**: T shall be [MoveInsertable] into *this and default_initializable.
  956. *
  957. * **Postcondition**: `sz == size()`.
  958. *
  959. * **Exceptions**: Strong exception guarantee.
  960. *
  961. * **Complexity**: Linear in the size of `*this` and `sz`.
  962. *
  963. * [MoveInsertable]: http://en.cppreference.com/w/cpp/concept/MoveInsertable
  964. */
  965. inline void resize_front(size_type sz, default_init_t)
  966. {
  967. resize_front_impl(sz, default_init);
  968. BOOST_ASSERT(invariants_ok());
  969. }
  970. /**
  971. * [CopyInsertable]: http://en.cppreference.com/w/cpp/concept/CopyInsertable
  972. *
  973. * **Effects**: If `sz` is greater than the size of `*this`,
  974. * copies of `c` are inserted to the front.
  975. * Invalidates iterators if reallocation is needed.
  976. * If `sz` is smaller than than the size of `*this`,
  977. * elements are popped from the front.
  978. *
  979. * **Postcondition**: `sz == size()`.
  980. *
  981. * **Requires**: `T` shall be [CopyInsertable] into `*this`.
  982. *
  983. * **Exceptions**: Strong exception guarantee.
  984. *
  985. * **Complexity**: Linear in the size of `*this` and `sz`.
  986. */
  987. inline void resize_front(size_type sz, const T& c)
  988. {
  989. resize_front_impl(sz, c);
  990. BOOST_ASSERT(invariants_ok());
  991. }
  992. /**
  993. * **Effects**: If `sz` is greater than the size of `*this`,
  994. * additional value-initialized elements are inserted
  995. * to the back. Invalidates iterators if reallocation is needed.
  996. * If `sz` is smaller than than the size of `*this`,
  997. * elements are popped from the back.
  998. *
  999. * **Requires**: T shall be [MoveInsertable] into *this and [DefaultConstructible].
  1000. *
  1001. * **Postcondition**: `sz == size()`.
  1002. *
  1003. * **Exceptions**: Strong exception guarantee.
  1004. *
  1005. * **Complexity**: Linear in the size of `*this` and `sz`.
  1006. *
  1007. * [MoveInsertable]: http://en.cppreference.com/w/cpp/concept/MoveInsertable
  1008. * [DefaultConstructible]: http://en.cppreference.com/w/cpp/concept/DefaultConstructible
  1009. */
  1010. inline void resize_back(size_type sz)
  1011. {
  1012. resize_back_impl(sz);
  1013. BOOST_ASSERT(invariants_ok());
  1014. }
  1015. /**
  1016. * **Effects**: If `sz` is greater than the size of `*this`,
  1017. * additional value-initialized elements are inserted
  1018. * to the back. Invalidates iterators if reallocation is needed.
  1019. * If `sz` is smaller than than the size of `*this`,
  1020. * elements are popped from the back.
  1021. *
  1022. * **Requires**: T shall be [MoveInsertable] into *this and default initializable.
  1023. *
  1024. * **Postcondition**: `sz == size()`.
  1025. *
  1026. * **Exceptions**: Strong exception guarantee.
  1027. *
  1028. * **Complexity**: Linear in the size of `*this` and `sz`.
  1029. *
  1030. * [MoveInsertable]: http://en.cppreference.com/w/cpp/concept/MoveInsertable
  1031. */
  1032. inline void resize_back(size_type sz, default_init_t)
  1033. {
  1034. resize_back_impl(sz, default_init);
  1035. BOOST_ASSERT(invariants_ok());
  1036. }
  1037. /**
  1038. * [CopyInsertable]: http://en.cppreference.com/w/cpp/concept/CopyInsertable
  1039. *
  1040. * **Effects**: If `sz` is greater than the size of `*this`,
  1041. * copies of `c` are inserted to the back.
  1042. * If `sz` is smaller than than the size of `*this`,
  1043. * elements are popped from the back.
  1044. *
  1045. * **Postcondition**: `sz == size()`.
  1046. *
  1047. * **Requires**: `T` shall be [CopyInsertable] into `*this`.
  1048. *
  1049. * **Exceptions**: Strong exception guarantee.
  1050. *
  1051. * **Complexity**: Linear in the size of `*this` and `sz`.
  1052. */
  1053. inline void resize_back(size_type sz, const T& c)
  1054. {
  1055. resize_back_impl(sz, c);
  1056. BOOST_ASSERT(invariants_ok());
  1057. }
  1058. /**
  1059. * [MoveInsertable]: http://en.cppreference.com/w/cpp/concept/MoveInsertable
  1060. *
  1061. * **Effects**: Ensures that at least `n` elements can be inserted
  1062. * without requiring reallocation, where `n` is `new_capacity - size()`,
  1063. * if `n` is positive. Otherwise, there are no effects.
  1064. * Invalidates iterators if reallocation is needed.
  1065. *
  1066. * **Requires**: `T` shall be [MoveInsertable] into `*this`.
  1067. *
  1068. * **Complexity**: Linear in the size of *this.
  1069. *
  1070. * **Exceptions**: Strong exception guarantee.
  1071. *
  1072. * **Throws**: length_error if `new_capacity > max_size()`.
  1073. */
  1074. inline void reserve(size_type new_capacity)
  1075. {
  1076. if (this->capacity() < new_capacity) {
  1077. const size_type rounder = devector_min_free_fraction - 2u;
  1078. const size_type divisor = devector_min_free_fraction - 1u;
  1079. size_type const nc = ((new_capacity + rounder)/divisor)*devector_min_free_fraction;
  1080. BOOST_ASSERT(new_capacity <= (nc - nc / devector_min_free_fraction));
  1081. size_type const sz = this->size();
  1082. reallocate_at(nc, (nc-sz)/2u);
  1083. }
  1084. BOOST_ASSERT(invariants_ok());
  1085. }
  1086. /**
  1087. * [MoveInsertable]: http://en.cppreference.com/w/cpp/concept/MoveInsertable
  1088. *
  1089. * **Effects**: Ensures that `n` elements can be pushed to the front
  1090. * without requiring reallocation, where `n` is `new_capacity - size()`,
  1091. * if `n` is positive. Otherwise, there are no effects.
  1092. * Invalidates iterators if reallocation is needed.
  1093. *
  1094. * **Requires**: `T` shall be [MoveInsertable] into `*this`.
  1095. *
  1096. * **Complexity**: Linear in the size of *this.
  1097. *
  1098. * **Exceptions**: Strong exception guarantee.
  1099. *
  1100. * **Throws**: `length_error` if `new_capacity > max_size()`.
  1101. */
  1102. inline void reserve_front(size_type new_capacity)
  1103. {
  1104. if (front_capacity() >= new_capacity) { return; }
  1105. reallocate_at(new_capacity + back_free_capacity(), new_capacity - size());
  1106. BOOST_ASSERT(invariants_ok());
  1107. }
  1108. /**
  1109. * [MoveInsertable]: http://en.cppreference.com/w/cpp/concept/MoveInsertable
  1110. *
  1111. * **Effects**: Ensures that `n` elements can be pushed to the back
  1112. * without requiring reallocation, where `n` is `new_capacity - size()`,
  1113. * if `n` is positive. Otherwise, there are no effects.
  1114. * Invalidates iterators if reallocation is needed.
  1115. *
  1116. * **Requires**: `T` shall be [MoveInsertable] into `*this`.
  1117. *
  1118. * **Complexity**: Linear in the size of *this.
  1119. *
  1120. * **Exceptions**: Strong exception guarantee.
  1121. *
  1122. * **Throws**: length_error if `new_capacity > max_size()`.
  1123. */
  1124. inline void reserve_back(size_type new_capacity)
  1125. {
  1126. if (back_capacity() >= new_capacity) { return; }
  1127. reallocate_at(new_capacity + front_free_capacity(), m_.front_idx);
  1128. BOOST_ASSERT(invariants_ok());
  1129. }
  1130. /**
  1131. * [MoveInsertable]: http://en.cppreference.com/w/cpp/concept/MoveInsertable
  1132. *
  1133. * **Effects**: Reduces `capacity()` to `size()`. Invalidates iterators.
  1134. *
  1135. * **Requires**: `T` shall be [MoveInsertable] into `*this`.
  1136. *
  1137. * **Exceptions**: Strong exception guarantee.
  1138. *
  1139. * **Complexity**: Linear in the size of *this.
  1140. */
  1141. inline void shrink_to_fit()
  1142. {
  1143. if(this->front_capacity() || this->back_capacity())
  1144. this->reallocate_at(size(), 0);
  1145. }
  1146. // element access:
  1147. /**
  1148. * **Returns**: A reference to the `n`th element in the devector.
  1149. *
  1150. * **Precondition**: `n < size()`.
  1151. *
  1152. * **Complexity**: Constant.
  1153. */
  1154. BOOST_CONTAINER_ATTRIBUTE_NODISCARD inline
  1155. reference operator[](size_type n) BOOST_NOEXCEPT
  1156. {
  1157. BOOST_ASSERT(n < size());
  1158. return m_.buffer[m_.front_idx + n];
  1159. }
  1160. /**
  1161. * **Returns**: A constant reference to the `n`th element in the devector.
  1162. *
  1163. * **Precondition**: `n < size()`.
  1164. *
  1165. * **Complexity**: Constant.
  1166. */
  1167. BOOST_CONTAINER_ATTRIBUTE_NODISCARD inline
  1168. const_reference operator[](size_type n) const BOOST_NOEXCEPT
  1169. {
  1170. BOOST_ASSERT(n < size());
  1171. return m_.buffer[m_.front_idx + n];
  1172. }
  1173. //! <b>Requires</b>: size() >= n.
  1174. //!
  1175. //! <b>Effects</b>: Returns an iterator to the nth element
  1176. //! from the beginning of the container. Returns end()
  1177. //! if n == size().
  1178. //!
  1179. //! <b>Throws</b>: Nothing.
  1180. //!
  1181. //! <b>Complexity</b>: Constant.
  1182. //!
  1183. //! <b>Note</b>: Non-standard extension
  1184. BOOST_CONTAINER_ATTRIBUTE_NODISCARD inline
  1185. iterator nth(size_type n) BOOST_NOEXCEPT_OR_NOTHROW
  1186. {
  1187. BOOST_ASSERT(n <= size());
  1188. return iterator(m_.buffer + (m_.front_idx + n));
  1189. }
  1190. //! <b>Requires</b>: size() >= n.
  1191. //!
  1192. //! <b>Effects</b>: Returns a const_iterator to the nth element
  1193. //! from the beginning of the container. Returns end()
  1194. //! if n == size().
  1195. //!
  1196. //! <b>Throws</b>: Nothing.
  1197. //!
  1198. //! <b>Complexity</b>: Constant.
  1199. //!
  1200. //! <b>Note</b>: Non-standard extension
  1201. BOOST_CONTAINER_ATTRIBUTE_NODISCARD inline
  1202. const_iterator nth(size_type n) const BOOST_NOEXCEPT_OR_NOTHROW
  1203. {
  1204. BOOST_ASSERT(n <= size());
  1205. return const_iterator(m_.buffer + (m_.front_idx + n));
  1206. }
  1207. //! <b>Requires</b>: begin() <= p <= end().
  1208. //!
  1209. //! <b>Effects</b>: Returns the index of the element pointed by p
  1210. //! and size() if p == end().
  1211. //!
  1212. //! <b>Throws</b>: Nothing.
  1213. //!
  1214. //! <b>Complexity</b>: Constant.
  1215. //!
  1216. //! <b>Note</b>: Non-standard extension
  1217. BOOST_CONTAINER_ATTRIBUTE_NODISCARD inline
  1218. size_type index_of(iterator p) BOOST_NOEXCEPT_OR_NOTHROW
  1219. {
  1220. BOOST_ASSERT(p >= begin());
  1221. BOOST_ASSERT(p <= end());
  1222. return static_cast<size_type>(p - this->begin());
  1223. }
  1224. //! <b>Requires</b>: begin() <= p <= end().
  1225. //!
  1226. //! <b>Effects</b>: Returns the index of the element pointed by p
  1227. //! and size() if p == end().
  1228. //!
  1229. //! <b>Throws</b>: Nothing.
  1230. //!
  1231. //! <b>Complexity</b>: Constant.
  1232. //!
  1233. //! <b>Note</b>: Non-standard extension
  1234. BOOST_CONTAINER_ATTRIBUTE_NODISCARD inline
  1235. size_type index_of(const_iterator p) const BOOST_NOEXCEPT_OR_NOTHROW
  1236. {
  1237. BOOST_ASSERT(p >= cbegin());
  1238. BOOST_ASSERT(p <= cend());
  1239. return static_cast<size_type>(p - this->cbegin());
  1240. }
  1241. /**
  1242. * **Returns**: A reference to the `n`th element in the devector.
  1243. *
  1244. * **Throws**: `out_of_range`, if `n >= size()`.
  1245. *
  1246. * **Complexity**: Constant.
  1247. */
  1248. BOOST_CONTAINER_ATTRIBUTE_NODISCARD inline
  1249. reference at(size_type n)
  1250. {
  1251. if (size() <= n)
  1252. throw_out_of_range("devector::at out of range");
  1253. return (*this)[n];
  1254. }
  1255. /**
  1256. * **Returns**: A constant reference to the `n`th element in the devector.
  1257. *
  1258. * **Throws**: `out_of_range`, if `n >= size()`.
  1259. *
  1260. * **Complexity**: Constant.
  1261. */
  1262. BOOST_CONTAINER_ATTRIBUTE_NODISCARD inline
  1263. const_reference at(size_type n) const
  1264. {
  1265. if (size() <= n)
  1266. throw_out_of_range("devector::at out of range");
  1267. return (*this)[n];
  1268. }
  1269. /**
  1270. * **Returns**: A reference to the first element in the devector.
  1271. *
  1272. * **Precondition**: `!empty()`.
  1273. *
  1274. * **Complexity**: Constant.
  1275. */
  1276. BOOST_CONTAINER_ATTRIBUTE_NODISCARD inline
  1277. reference front() BOOST_NOEXCEPT
  1278. {
  1279. BOOST_ASSERT(!empty());
  1280. return m_.buffer[m_.front_idx];
  1281. }
  1282. /**
  1283. * **Returns**: A constant reference to the first element in the devector.
  1284. *
  1285. * **Precondition**: `!empty()`.
  1286. *
  1287. * **Complexity**: Constant.
  1288. */
  1289. BOOST_CONTAINER_ATTRIBUTE_NODISCARD inline
  1290. const_reference front() const BOOST_NOEXCEPT
  1291. {
  1292. BOOST_ASSERT(!empty());
  1293. return m_.buffer[m_.front_idx];
  1294. }
  1295. /**
  1296. * **Returns**: A reference to the last element in the devector.
  1297. *
  1298. * **Precondition**: `!empty()`.
  1299. *
  1300. * **Complexity**: Constant.
  1301. */
  1302. BOOST_CONTAINER_ATTRIBUTE_NODISCARD inline
  1303. reference back() BOOST_NOEXCEPT
  1304. {
  1305. BOOST_ASSERT(!empty());
  1306. return m_.buffer[m_.back_idx - 1u];
  1307. }
  1308. /**
  1309. * **Returns**: A constant reference to the last element in the devector.
  1310. *
  1311. * **Precondition**: `!empty()`.
  1312. *
  1313. * **Complexity**: Constant.
  1314. */
  1315. BOOST_CONTAINER_ATTRIBUTE_NODISCARD inline
  1316. const_reference back() const BOOST_NOEXCEPT
  1317. {
  1318. BOOST_ASSERT(!empty());
  1319. return m_.buffer[m_.back_idx - 1u];
  1320. }
  1321. /**
  1322. * **Returns**: A pointer to the underlying array serving as element storage.
  1323. * The range `[data(); data() + size())` is always valid. For a non-empty devector,
  1324. * `data() == &front()`.
  1325. *
  1326. * **Complexity**: Constant.
  1327. */
  1328. BOOST_CONTAINER_ATTRIBUTE_NODISCARD inline
  1329. T* data() BOOST_NOEXCEPT
  1330. {
  1331. return boost::movelib::to_raw_pointer(m_.buffer) + m_.front_idx;
  1332. }
  1333. /**
  1334. * **Returns**: A constant pointer to the underlying array serving as element storage.
  1335. * The range `[data(); data() + size())` is always valid. For a non-empty devector,
  1336. * `data() == &front()`.
  1337. *
  1338. * **Complexity**: Constant.
  1339. */
  1340. BOOST_CONTAINER_ATTRIBUTE_NODISCARD inline
  1341. const T* data() const BOOST_NOEXCEPT
  1342. {
  1343. return boost::movelib::to_raw_pointer(m_.buffer) + m_.front_idx;
  1344. }
  1345. // modifiers:
  1346. /**
  1347. * **Effects**: Pushes a new element to the front of the devector.
  1348. * The element is constructed in-place, using the perfect forwarded `args`
  1349. * as constructor arguments. Invalidates iterators if reallocation is needed.
  1350. * (`front_free_capacity() == 0`)
  1351. *
  1352. * **Requires**: `T` shall be [EmplaceConstructible] from `args` and [MoveInsertable] into `*this`.
  1353. *
  1354. * **Exceptions**: Strong exception guarantee.
  1355. *
  1356. * **Complexity**: Amortized constant in the size of `*this`.
  1357. * (Constant, if `front_free_capacity() > 0`)
  1358. *
  1359. * [EmplaceConstructible]: http://en.cppreference.com/w/cpp/concept/EmplaceConstructible
  1360. * [MoveInsertable]: http://en.cppreference.com/w/cpp/concept/MoveInsertable
  1361. */
  1362. #if !defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES) || defined(BOOST_CONTAINER_DOXYGEN_INVOKED)
  1363. template <class... Args>
  1364. reference emplace_front(Args&&... args)
  1365. {
  1366. if (BOOST_LIKELY(front_free_capacity() != 0)) // fast path
  1367. {
  1368. pointer const p = m_.buffer + (m_.front_idx - 1u);
  1369. this->alloc_construct(p, boost::forward<Args>(args)...);
  1370. --m_.front_idx;
  1371. BOOST_ASSERT(invariants_ok());
  1372. return *p;
  1373. }
  1374. else
  1375. {
  1376. typedef dtl::insert_emplace_proxy<allocator_type, Args...> proxy_t;
  1377. return *this->insert_range_slow_path(this->begin(), 1, proxy_t(::boost::forward<Args>(args)...));
  1378. }
  1379. }
  1380. #else //!defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES) || defined(BOOST_CONTAINER_DOXYGEN_INVOKED)
  1381. #define BOOST_CONTAINER_DEVECTOR_EMPLACE_FRONT(N) \
  1382. BOOST_MOVE_TMPL_LT##N BOOST_MOVE_CLASS##N BOOST_MOVE_GT##N \
  1383. inline reference emplace_front(BOOST_MOVE_UREF##N)\
  1384. {\
  1385. if (front_free_capacity())\
  1386. {\
  1387. pointer const p = m_.buffer + (m_.front_idx - 1u);\
  1388. this->alloc_construct(p BOOST_MOVE_I##N BOOST_MOVE_FWD##N);\
  1389. --m_.front_idx;\
  1390. return *p;\
  1391. }\
  1392. else\
  1393. {\
  1394. typedef dtl::insert_emplace_proxy_arg##N<allocator_type BOOST_MOVE_I##N BOOST_MOVE_TARG##N> proxy_t;\
  1395. return *this->insert_range_slow_path(this->begin(), 1, proxy_t(BOOST_MOVE_FWD##N));\
  1396. }\
  1397. }\
  1398. //
  1399. BOOST_MOVE_ITERATE_0TO9(BOOST_CONTAINER_DEVECTOR_EMPLACE_FRONT)
  1400. #undef BOOST_CONTAINER_DEVECTOR_EMPLACE_FRONT
  1401. #endif
  1402. #if defined(BOOST_CONTAINER_DOXYGEN_INVOKED)
  1403. /**
  1404. * [CopyInsertable]: http://en.cppreference.com/w/cpp/concept/CopyInsertable
  1405. *
  1406. * **Effects**: Pushes the copy of `x` to the front of the devector.
  1407. * Invalidates iterators if reallocation is needed.
  1408. * (`front_free_capacity() == 0`)
  1409. *
  1410. * **Requires**: `T` shall be [CopyInsertable] into `*this`.
  1411. *
  1412. * **Exceptions**: Strong exception guarantee.
  1413. *
  1414. * **Complexity**: Amortized constant in the size of `*this`.
  1415. * (Constant, if `front_free_capacity() > 0`)
  1416. */
  1417. void push_front(const T& x);
  1418. /**
  1419. * [MoveInsertable]: http://en.cppreference.com/w/cpp/concept/MoveInsertable
  1420. *
  1421. * **Effects**: Move constructs a new element at the front of the devector using `x`.
  1422. * Invalidates iterators if reallocation is needed.
  1423. * (`front_free_capacity() == 0`)
  1424. *
  1425. * **Requires**: `T` shall be [MoveInsertable] into `*this`.
  1426. *
  1427. * **Exceptions**: Strong exception guarantee, not regarding the state of `x`.
  1428. *
  1429. * **Complexity**: Amortized constant in the size of `*this`.
  1430. * (Constant, if `front_free_capacity() > 0`)
  1431. */
  1432. void push_front(T&& x);
  1433. #else
  1434. BOOST_MOVE_CONVERSION_AWARE_CATCH(push_front, T, void, priv_push_front)
  1435. #endif
  1436. /**
  1437. * **Effects**: Removes the first element of `*this`.
  1438. *
  1439. * **Precondition**: `!empty()`.
  1440. *
  1441. * **Postcondition**: `front_free_capacity()` is incremented by 1.
  1442. *
  1443. * **Complexity**: Constant.
  1444. */
  1445. void pop_front() BOOST_NOEXCEPT
  1446. {
  1447. BOOST_ASSERT(!empty());
  1448. allocator_traits_type::destroy(get_allocator_ref(), boost::movelib::to_raw_pointer(m_.buffer + m_.front_idx));
  1449. ++m_.front_idx;
  1450. BOOST_ASSERT(invariants_ok());
  1451. }
  1452. /**
  1453. * **Effects**: Pushes a new element to the back of the devector.
  1454. * The element is constructed in-place, using the perfect forwarded `args`
  1455. * as constructor arguments. Invalidates iterators if reallocation is needed.
  1456. * (`back_free_capacity() == 0`)
  1457. *
  1458. * **Requires**: `T` shall be [EmplaceConstructible] from `args` and [MoveInsertable] into `*this`,
  1459. * and [MoveAssignable].
  1460. *
  1461. * **Exceptions**: Strong exception guarantee.
  1462. *
  1463. * **Complexity**: Amortized constant in the size of `*this`.
  1464. * (Constant, if `back_free_capacity() > 0`)
  1465. *
  1466. * [EmplaceConstructible]: http://en.cppreference.com/w/cpp/concept/EmplaceConstructible
  1467. * [MoveInsertable]: http://en.cppreference.com/w/cpp/concept/MoveInsertable
  1468. * [MoveAssignable]: http://en.cppreference.com/w/cpp/concept/MoveAssignable
  1469. */
  1470. #if !defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES) || defined(BOOST_CONTAINER_DOXYGEN_INVOKED)
  1471. template <class... Args>
  1472. inline reference emplace_back(Args&&... args)
  1473. {
  1474. if (BOOST_LIKELY(this->back_free_capacity() != 0)){
  1475. pointer const p = m_.buffer + m_.back_idx;
  1476. this->alloc_construct(p, boost::forward<Args>(args)...);
  1477. ++m_.back_idx;
  1478. BOOST_ASSERT(invariants_ok());
  1479. return *p;
  1480. }
  1481. else {
  1482. typedef dtl::insert_emplace_proxy<allocator_type, Args...> proxy_t;
  1483. return *this->insert_range_slow_path(this->end(), 1, proxy_t(::boost::forward<Args>(args)...));
  1484. }
  1485. }
  1486. #else //!defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES) || defined(BOOST_CONTAINER_DOXYGEN_INVOKED)
  1487. #define BOOST_CONTAINER_DEVECTOR_EMPLACE_BACK(N) \
  1488. BOOST_MOVE_TMPL_LT##N BOOST_MOVE_CLASS##N BOOST_MOVE_GT##N \
  1489. inline reference emplace_back(BOOST_MOVE_UREF##N)\
  1490. {\
  1491. if (this->back_free_capacity()){\
  1492. pointer const p = m_.buffer + m_.back_idx;\
  1493. this->alloc_construct(p BOOST_MOVE_I##N BOOST_MOVE_FWD##N);\
  1494. ++m_.back_idx;\
  1495. return *p;\
  1496. }\
  1497. else {\
  1498. typedef dtl::insert_emplace_proxy_arg##N<allocator_type BOOST_MOVE_I##N BOOST_MOVE_TARG##N> proxy_t;\
  1499. return *this->insert_range_slow_path(this->end(), 1, proxy_t(BOOST_MOVE_FWD##N));\
  1500. }\
  1501. }\
  1502. //
  1503. BOOST_MOVE_ITERATE_0TO9(BOOST_CONTAINER_DEVECTOR_EMPLACE_BACK)
  1504. #undef BOOST_CONTAINER_DEVECTOR_EMPLACE_BACK
  1505. #endif //!defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES) || defined(BOOST_CONTAINER_DOXYGEN_INVOKED)
  1506. #if defined(BOOST_CONTAINER_DOXYGEN_INVOKED)
  1507. /**
  1508. * [CopyInsertable]: http://en.cppreference.com/w/cpp/concept/CopyInsertable
  1509. *
  1510. * **Effects**: Pushes the copy of `x` to the back of the devector.
  1511. * Invalidates iterators if reallocation is needed.
  1512. * (`back_free_capacity() == 0`)
  1513. *
  1514. * **Requires**: `T` shall be [CopyInsertable] into `*this`.
  1515. *
  1516. * **Exceptions**: Strong exception guarantee.
  1517. *
  1518. * **Complexity**: Amortized constant in the size of `*this`.
  1519. * (Constant, if `back_free_capacity() > 0`)
  1520. */
  1521. void push_back(const T& x);
  1522. /**
  1523. * [MoveInsertable]: http://en.cppreference.com/w/cpp/concept/MoveInsertable
  1524. *
  1525. * **Effects**: Move constructs a new element at the back of the devector using `x`.
  1526. * Invalidates iterators if reallocation is needed.
  1527. * (`back_free_capacity() == 0`)
  1528. *
  1529. * **Requires**: `T` shall be [MoveInsertable] into `*this`.
  1530. *
  1531. * **Exceptions**: Strong exception guarantee, not regarding the state of `x`.
  1532. *
  1533. * **Complexity**: Amortized constant in the size of `*this`.
  1534. * (Constant, if `back_free_capacity() > 0`)
  1535. */
  1536. void push_back(T&& x);
  1537. #else
  1538. BOOST_MOVE_CONVERSION_AWARE_CATCH(push_back, T, void, priv_push_back)
  1539. #endif
  1540. /**
  1541. * **Effects**: Removes the last element of `*this`.
  1542. *
  1543. * **Precondition**: `!empty()`.
  1544. *
  1545. * **Postcondition**: `back_free_capacity()` is incremented by 1.
  1546. *
  1547. * **Complexity**: Constant.
  1548. */
  1549. void pop_back() BOOST_NOEXCEPT
  1550. {
  1551. BOOST_ASSERT(! empty());
  1552. --m_.back_idx;
  1553. allocator_traits_type::destroy(get_allocator_ref(), boost::movelib::to_raw_pointer(m_.buffer + m_.back_idx));
  1554. BOOST_ASSERT(invariants_ok());
  1555. }
  1556. /**
  1557. * **Effects**: Constructs a new element before the element pointed by `position`.
  1558. * The element is constructed in-place, using the perfect forwarded `args`
  1559. * as constructor arguments. Invalidates iterators if reallocation is needed.
  1560. *
  1561. * **Requires**: `T` shall be [EmplaceConstructible], and [MoveInsertable] into `*this`,
  1562. * and [MoveAssignable].
  1563. *
  1564. * **Returns**: Iterator pointing to the newly constructed element.
  1565. *
  1566. * **Exceptions**: Strong exception guarantee if `T` is `NothrowConstructible`
  1567. * and `NothrowAssignable`, Basic exception guarantee otherwise.
  1568. *
  1569. * **Complexity**: Linear in the size of `*this`.
  1570. *
  1571. * [EmplaceConstructible]: http://en.cppreference.com/w/cpp/concept/EmplaceConstructible
  1572. * [MoveInsertable]: http://en.cppreference.com/w/cpp/concept/MoveInsertable
  1573. * [MoveAssignable]: http://en.cppreference.com/w/cpp/concept/MoveAssignable
  1574. */
  1575. #if !defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES) || defined(BOOST_CONTAINER_DOXYGEN_INVOKED)
  1576. template <class... Args>
  1577. iterator emplace(const_iterator position, Args&&... args)
  1578. {
  1579. BOOST_ASSERT(position >= begin());
  1580. BOOST_ASSERT(position <= end());
  1581. typedef dtl::insert_emplace_proxy<allocator_type, Args...> proxy_t;
  1582. bool prefer_move_back;
  1583. if (position == end()){
  1584. if(back_free_capacity()) // fast path
  1585. {
  1586. pointer const p = m_.buffer + m_.back_idx;
  1587. this->alloc_construct(p, boost::forward<Args>(args)...);
  1588. ++m_.back_idx;
  1589. return iterator(p);
  1590. }
  1591. prefer_move_back = true;
  1592. }
  1593. else if (position == begin()){
  1594. if(front_free_capacity()) // secondary fast path
  1595. {
  1596. pointer const p = m_.buffer + (m_.front_idx - 1);
  1597. this->alloc_construct(p, boost::forward<Args>(args)...);
  1598. --m_.front_idx;
  1599. return iterator(p);
  1600. }
  1601. prefer_move_back = false;
  1602. }
  1603. else{
  1604. iterator nonconst_pos = unconst_iterator(position);
  1605. prefer_move_back = should_move_back(position);
  1606. if(prefer_move_back){
  1607. if(back_free_capacity()){
  1608. boost::container::expand_forward_and_insert_nonempty_middle_alloc
  1609. ( get_allocator_ref()
  1610. , boost::movelib::to_raw_pointer(nonconst_pos)
  1611. , this->priv_raw_end()
  1612. , 1, proxy_t(::boost::forward<Args>(args)...));
  1613. ++m_.back_idx;
  1614. return nonconst_pos;
  1615. }
  1616. }
  1617. else{
  1618. if (front_free_capacity()){
  1619. boost::container::expand_backward_and_insert_nonempty_middle_alloc
  1620. (get_allocator_ref()
  1621. , this->priv_raw_begin()
  1622. , boost::movelib::to_raw_pointer(nonconst_pos)
  1623. , 1, proxy_t(::boost::forward<Args>(args)...));
  1624. --m_.front_idx;
  1625. return --nonconst_pos;
  1626. }
  1627. }
  1628. }
  1629. return this->insert_range_slow_path(position, 1, proxy_t(::boost::forward<Args>(args)...));
  1630. }
  1631. #else //!defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES) || defined(BOOST_CONTAINER_DOXYGEN_INVOKED)
  1632. #define BOOST_CONTAINER_DEVECTOR_EMPLACE(N) \
  1633. BOOST_MOVE_TMPL_LT##N BOOST_MOVE_CLASS##N BOOST_MOVE_GT##N \
  1634. iterator emplace(const_iterator position BOOST_MOVE_I##N BOOST_MOVE_UREF##N)\
  1635. {\
  1636. BOOST_ASSERT(position >= begin());\
  1637. BOOST_ASSERT(position <= end());\
  1638. typedef dtl::insert_emplace_proxy_arg##N<allocator_type BOOST_MOVE_I##N BOOST_MOVE_TARG##N> proxy_t;\
  1639. bool prefer_move_back;\
  1640. if (position == end()){\
  1641. if(back_free_capacity())\
  1642. {\
  1643. pointer const p = m_.buffer + m_.back_idx;\
  1644. this->alloc_construct(p BOOST_MOVE_I##N BOOST_MOVE_FWD##N);\
  1645. ++m_.back_idx;\
  1646. return iterator(p);\
  1647. }\
  1648. prefer_move_back = true;\
  1649. }\
  1650. else if (position == begin()){\
  1651. if(front_free_capacity())\
  1652. {\
  1653. pointer const p = m_.buffer + (m_.front_idx - 1);\
  1654. this->alloc_construct(p BOOST_MOVE_I##N BOOST_MOVE_FWD##N);\
  1655. --m_.front_idx;\
  1656. return iterator(p);\
  1657. }\
  1658. prefer_move_back = false;\
  1659. }\
  1660. else{\
  1661. iterator nonconst_pos = unconst_iterator(position);\
  1662. prefer_move_back = should_move_back(position);\
  1663. \
  1664. if(prefer_move_back){\
  1665. if(back_free_capacity()){\
  1666. boost::container::expand_forward_and_insert_nonempty_middle_alloc\
  1667. ( get_allocator_ref()\
  1668. , boost::movelib::to_raw_pointer(nonconst_pos)\
  1669. , this->priv_raw_end()\
  1670. , 1, proxy_t(BOOST_MOVE_FWD##N));\
  1671. ++m_.back_idx;\
  1672. return nonconst_pos;\
  1673. }\
  1674. }\
  1675. else{\
  1676. if (front_free_capacity()){\
  1677. boost::container::expand_backward_and_insert_nonempty_middle_alloc\
  1678. (get_allocator_ref()\
  1679. , this->priv_raw_begin()\
  1680. , boost::movelib::to_raw_pointer(nonconst_pos)\
  1681. , 1, proxy_t(BOOST_MOVE_FWD##N));\
  1682. --m_.front_idx;\
  1683. return --nonconst_pos;\
  1684. }\
  1685. }\
  1686. }\
  1687. return this->insert_range_slow_path(position, 1, proxy_t(BOOST_MOVE_FWD##N));\
  1688. }\
  1689. //
  1690. BOOST_MOVE_ITERATE_0TO9(BOOST_CONTAINER_DEVECTOR_EMPLACE)
  1691. #undef BOOST_CONTAINER_DEVECTOR_EMPLACE
  1692. #endif //!defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES) || defined(BOOST_CONTAINER_DOXYGEN_INVOKED)
  1693. #if defined(BOOST_CONTAINER_DOXYGEN_INVOKED)
  1694. /**
  1695. * **Effects**: Copy constructs a new element before the element pointed by `position`,
  1696. * using `x` as constructor argument. Invalidates iterators if reallocation is needed.
  1697. *
  1698. * **Requires**: `T` shall be [CopyInsertable] into `*this` and and [CopyAssignable].
  1699. *
  1700. * **Returns**: Iterator pointing to the newly constructed element.
  1701. *
  1702. * **Exceptions**: Strong exception guarantee if `T` is `NothrowConstructible`
  1703. * and `NothrowAssignable`, Basic exception guarantee otherwise.
  1704. *
  1705. * **Complexity**: Linear in the size of `*this`.
  1706. *
  1707. * [CopyInsertable]: http://en.cppreference.com/w/cpp/concept/CopyInsertable
  1708. * [CopyAssignable]: http://en.cppreference.com/w/cpp/concept/CopyAssignable
  1709. */
  1710. iterator insert(const_iterator position, const T &x);
  1711. /**
  1712. * **Effects**: Move constructs a new element before the element pointed by `position`,
  1713. * using `x` as constructor argument. Invalidates iterators if reallocation is needed.
  1714. *
  1715. * **Requires**: `T` shall be [MoveInsertable] into `*this` and and [CopyAssignable].
  1716. *
  1717. * **Returns**: Iterator pointing to the newly constructed element.
  1718. *
  1719. * **Exceptions**: Strong exception guarantee if `T` is `NothrowConstructible`
  1720. * and `NothrowAssignable` (not regarding the state of `x`),
  1721. * Basic exception guarantee otherwise.
  1722. *
  1723. * **Complexity**: Linear in the size of `*this`.
  1724. *
  1725. * [MoveInsertable]: http://en.cppreference.com/w/cpp/concept/MoveInsertable
  1726. * [CopyAssignable]: http://en.cppreference.com/w/cpp/concept/CopyAssignable
  1727. */
  1728. iterator insert(const_iterator position, T &&x);
  1729. #else
  1730. BOOST_MOVE_CONVERSION_AWARE_CATCH_1ARG(insert, T, iterator, priv_insert, const_iterator, const_iterator)
  1731. #endif
  1732. /**
  1733. * **Effects**: Copy constructs `n` elements before the element pointed by `position`,
  1734. * using `x` as constructor argument. Invalidates iterators if reallocation is needed.
  1735. *
  1736. * **Requires**: `T` shall be [CopyInsertable] into `*this` and and [CopyAssignable].
  1737. *
  1738. * **Returns**: Iterator pointing to the first inserted element, or `position`, if `n` is zero.
  1739. *
  1740. * **Exceptions**: Strong exception guarantee if `T` is `NothrowConstructible`
  1741. * and `NothrowAssignable`, Basic exception guarantee otherwise.
  1742. *
  1743. * **Complexity**: Linear in the size of `*this` and `n`.
  1744. *
  1745. * [CopyInsertable]: http://en.cppreference.com/w/cpp/concept/CopyInsertable
  1746. * [CopyAssignable]: http://en.cppreference.com/w/cpp/concept/CopyAssignable
  1747. */
  1748. inline iterator insert(const_iterator position, size_type n, const T& x)
  1749. {
  1750. cvalue_iterator first(x, n);
  1751. cvalue_iterator last = first + n;
  1752. return this->insert_range(position, first, last);
  1753. }
  1754. /**
  1755. * **Effects**: Copy constructs elements before the element pointed by position
  1756. * using each element in the range pointed by `first` and `last` as constructor arguments.
  1757. * Invalidates iterators if reallocation is needed.
  1758. *
  1759. * **Requires**: `T` shall be [EmplaceConstructible] into `*this` from `*first`. If the specified iterator
  1760. * does not meet the forward iterator requirements, `T` shall also be [MoveInsertable] into `*this`
  1761. * and [MoveAssignable].
  1762. *
  1763. * **Precondition**: `first` and `last` are not iterators into `*this`.
  1764. *
  1765. * **Returns**: Iterator pointing to the first inserted element, or `position`, if `first == last`.
  1766. *
  1767. * **Complexity**: Linear in the size of `*this` and `N` (where `N` is the distance between `first` and `last`).
  1768. * Makes only `N` calls to the constructor of `T` and no reallocations if iterators `first` and `last`
  1769. * are of forward, bidirectional, or random access categories. It makes 2N calls to the copy constructor of `T`
  1770. * and `O(log(N)) reallocations if they are just input iterators.
  1771. *
  1772. * **Exceptions**: Strong exception guarantee if `T` is `NothrowConstructible`
  1773. * and `NothrowAssignable`, Basic exception guarantee otherwise.
  1774. *
  1775. * **Remarks**: Each iterator in the range `[first,last)` shall be dereferenced exactly once,
  1776. * unless an exception is thrown.
  1777. *
  1778. * [EmplaceConstructible]: http://en.cppreference.com/w/cpp/concept/EmplaceConstructible
  1779. * [MoveInsertable]: http://en.cppreference.com/w/cpp/concept/MoveInsertable
  1780. * [MoveAssignable]: http://en.cppreference.com/w/cpp/concept/MoveAssignable
  1781. */
  1782. template <class InputIterator>
  1783. iterator insert(const_iterator position, InputIterator first, InputIterator last
  1784. //Input iterators
  1785. BOOST_CONTAINER_DOCIGN(BOOST_MOVE_I typename dtl::disable_if_or
  1786. < void
  1787. BOOST_MOVE_I dtl::is_convertible<InputIterator BOOST_MOVE_I size_type>
  1788. BOOST_MOVE_I dtl::is_not_input_iterator<InputIterator>
  1789. >::type * = 0)
  1790. )
  1791. {
  1792. if (position == end())
  1793. {
  1794. size_type insert_index = size();
  1795. for (; first != last; ++first)
  1796. {
  1797. this->emplace_back(*first);
  1798. }
  1799. return begin() + insert_index;
  1800. }
  1801. else
  1802. {
  1803. const size_type insert_index = static_cast<size_type>(position - this->cbegin());
  1804. const size_type old_size = static_cast<size_type>(this->size());
  1805. for (; first != last; ++first) {
  1806. this->emplace_back(*first);
  1807. }
  1808. iterator rit (this->begin() + insert_index);
  1809. boost::movelib::rotate_gcd(rit, this->begin() + old_size, this->begin() + this->size());
  1810. return rit;
  1811. }
  1812. }
  1813. #ifndef BOOST_CONTAINER_DOXYGEN_INVOKED
  1814. template <class ForwardIterator>
  1815. inline iterator insert(const_iterator position, ForwardIterator first, ForwardIterator last
  1816. //Other iterators
  1817. BOOST_CONTAINER_DOCIGN(BOOST_MOVE_I typename dtl::disable_if_or
  1818. < void
  1819. BOOST_MOVE_I dtl::is_convertible<ForwardIterator BOOST_MOVE_I size_type>
  1820. BOOST_MOVE_I dtl::is_input_iterator<ForwardIterator>
  1821. >::type * = 0)
  1822. )
  1823. {
  1824. return insert_range(position, first, last);
  1825. }
  1826. #endif // ifndef BOOST_CONTAINER_DOXYGEN_INVOKED
  1827. #if !defined(BOOST_NO_CXX11_HDR_INITIALIZER_LIST)
  1828. /** **Equivalent to**: `insert(position, il.begin(), il.end())` */
  1829. inline iterator insert(const_iterator position, std::initializer_list<T> il)
  1830. {
  1831. return this->insert(position, il.begin(), il.end());
  1832. }
  1833. #endif
  1834. /**
  1835. * [MoveAssignable]: http://en.cppreference.com/w/cpp/concept/MoveAssignable
  1836. *
  1837. * **Effects**: Destroys the element pointed by `position` and removes it from the devector.
  1838. * Invalidates iterators.
  1839. *
  1840. * **Requires**: `T` shall be [MoveAssignable].
  1841. *
  1842. * **Precondition**: `position` must be in the range of `[begin(), end())`.
  1843. *
  1844. * **Returns**: Iterator pointing to the element immediately following the erased element
  1845. * prior to its erasure. If no such element exists, `end()` is returned.
  1846. *
  1847. * **Exceptions**: Strong exception guarantee if `T` is `NothrowAssignable`,
  1848. * Basic exception guarantee otherwise.
  1849. *
  1850. * **Complexity**: Linear in half the size of `*this`.
  1851. */
  1852. iterator erase(const_iterator position)
  1853. {
  1854. return erase(position, position + 1);
  1855. }
  1856. /**
  1857. * [MoveAssignable]: http://en.cppreference.com/w/cpp/concept/MoveAssignable
  1858. *
  1859. * **Effects**: Destroys the range `[first,last)` and removes it from the devector.
  1860. * Invalidates iterators.
  1861. *
  1862. * **Requires**: `T` shall be [MoveAssignable].
  1863. *
  1864. * **Precondition**: `[first,last)` must be in the range of `[begin(), end())`.
  1865. *
  1866. * **Returns**: Iterator pointing to the element pointed to by `last` prior to any elements
  1867. * being erased. If no such element exists, `end()` is returned.
  1868. *
  1869. * **Exceptions**: Strong exception guarantee if `T` is `NothrowAssignable`,
  1870. * Basic exception guarantee otherwise.
  1871. *
  1872. * **Complexity**: Linear in half the size of `*this`
  1873. * plus the distance between `first` and `last`.
  1874. */
  1875. iterator erase(const_iterator first, const_iterator last)
  1876. {
  1877. iterator nc_first = unconst_iterator(first);
  1878. iterator nc_last = unconst_iterator(last);
  1879. return erase(nc_first, nc_last);
  1880. }
  1881. /**
  1882. * [MoveAssignable]: http://en.cppreference.com/w/cpp/concept/MoveAssignable
  1883. *
  1884. * **Effects**: Destroys the range `[first,last)` and removes it from the devector.
  1885. * Invalidates iterators.
  1886. *
  1887. * **Requires**: `T` shall be [MoveAssignable].
  1888. *
  1889. * **Precondition**: `[first,last)` must be in the range of `[begin(), end())`.
  1890. *
  1891. * **Returns**: Iterator pointing to the element pointed to by `last` prior to any elements
  1892. * being erased. If no such element exists, `end()` is returned.
  1893. *
  1894. * **Exceptions**: Strong exception guarantee if `T` is `NothrowAssignable`,
  1895. * Basic exception guarantee otherwise.
  1896. *
  1897. * **Complexity**: Linear in half the size of `*this`.
  1898. */
  1899. iterator erase(iterator first, iterator last)
  1900. {
  1901. size_type front_distance = pos_to_index(last);
  1902. size_type back_distance = size_type(end() - first);
  1903. size_type n = boost::container::iterator_udistance(first, last);
  1904. if (front_distance < back_distance)
  1905. {
  1906. // move n to the right
  1907. boost::container::move_backward(begin(), first, last);
  1908. for (iterator i = begin(); i != begin() + n; ++i)
  1909. {
  1910. allocator_traits_type::destroy(get_allocator_ref(), boost::movelib::to_raw_pointer(i));
  1911. }
  1912. //n is always less than max stored_size_type
  1913. m_.set_front_idx(m_.front_idx + n);
  1914. BOOST_ASSERT(invariants_ok());
  1915. return last;
  1916. }
  1917. else {
  1918. // move n to the left
  1919. boost::container::move(last, end(), first);
  1920. for (iterator i = end() - n; i != end(); ++i)
  1921. {
  1922. allocator_traits_type::destroy(get_allocator_ref(), boost::movelib::to_raw_pointer(i));
  1923. }
  1924. //n is always less than max stored_size_type
  1925. m_.set_back_idx(m_.back_idx - n);
  1926. BOOST_ASSERT(invariants_ok());
  1927. return first;
  1928. }
  1929. }
  1930. /**
  1931. * [MoveInsertable]: http://en.cppreference.com/w/cpp/concept/MoveInsertable
  1932. *
  1933. * **Effects**: exchanges the contents of `*this` and `b`.
  1934. *
  1935. * **Requires**: instances of `T` must be swappable by unqualified call of `swap`
  1936. * and `T` must be [MoveInsertable] into `*this`.
  1937. *
  1938. * **Precondition**: The allocators should allow propagation or should compare equal.
  1939. *
  1940. * **Exceptions**: Basic exceptions guarantee if not `noexcept`.
  1941. *
  1942. * **Complexity**: Constant.
  1943. */
  1944. void swap(devector& b)
  1945. BOOST_NOEXCEPT_IF( allocator_traits_type::propagate_on_container_swap::value
  1946. || allocator_traits_type::is_always_equal::value)
  1947. {
  1948. BOOST_CONSTEXPR_OR_CONST bool propagate_alloc = allocator_traits_type::propagate_on_container_swap::value;
  1949. BOOST_ASSERT(propagate_alloc || get_allocator_ref() == b.get_allocator_ref()); // else it's undefined behavior
  1950. swap_big_big(*this, b);
  1951. // swap indices
  1952. boost::adl_move_swap(m_.front_idx, b.m_.front_idx);
  1953. boost::adl_move_swap(m_.back_idx, b.m_.back_idx);
  1954. //And now swap the allocator
  1955. dtl::swap_alloc(this->get_allocator_ref(), b.get_allocator_ref(), dtl::bool_<propagate_alloc>());
  1956. BOOST_ASSERT( invariants_ok());
  1957. BOOST_ASSERT(b.invariants_ok());
  1958. }
  1959. /**
  1960. * **Effects**: Destroys all elements in the devector.
  1961. * Invalidates all references, pointers and iterators to the
  1962. * elements of the devector.
  1963. *
  1964. * **Postcondition**: `empty() && front_free_capacity() == 0
  1965. * && back_free_capacity() == old capacity`.
  1966. *
  1967. * **Complexity**: Linear in the size of `*this`.
  1968. *
  1969. * **Remarks**: Does not free memory.
  1970. */
  1971. void clear() BOOST_NOEXCEPT
  1972. {
  1973. destroy_elements(begin(), end());
  1974. m_.front_idx = m_.back_idx = 0;
  1975. }
  1976. BOOST_CONTAINER_ATTRIBUTE_NODISCARD inline
  1977. friend bool operator==(const devector& x, const devector& y)
  1978. { return x.size() == y.size() && ::boost::container::algo_equal(x.begin(), x.end(), y.begin()); }
  1979. BOOST_CONTAINER_ATTRIBUTE_NODISCARD inline
  1980. friend bool operator!=(const devector& x, const devector& y)
  1981. { return !(x == y); }
  1982. BOOST_CONTAINER_ATTRIBUTE_NODISCARD inline
  1983. friend bool operator< (const devector& x, const devector& y)
  1984. { return boost::container::algo_lexicographical_compare(x.begin(), x.end(), y.begin(), y.end()); }
  1985. BOOST_CONTAINER_ATTRIBUTE_NODISCARD inline
  1986. friend bool operator>(const devector& x, const devector& y)
  1987. { return y < x; }
  1988. BOOST_CONTAINER_ATTRIBUTE_NODISCARD inline
  1989. friend bool operator<=(const devector& x, const devector& y)
  1990. { return !(y < x); }
  1991. BOOST_CONTAINER_ATTRIBUTE_NODISCARD inline
  1992. friend bool operator>=(const devector& x, const devector& y)
  1993. { return !(x < y); }
  1994. inline friend void swap(devector& x, devector& y)
  1995. BOOST_NOEXCEPT_IF( allocator_traits_type::propagate_on_container_swap::value
  1996. || allocator_traits_type::is_always_equal::value)
  1997. { x.swap(y); }
  1998. private:
  1999. void priv_move_assign(BOOST_RV_REF(devector) x, dtl::bool_<true> /*steal_resources*/)
  2000. {
  2001. this->clear();
  2002. this->deallocate_buffer();
  2003. //Move allocator if needed
  2004. dtl::bool_<allocator_traits_type::
  2005. propagate_on_container_move_assignment::value> flag;
  2006. dtl::move_alloc(this->get_allocator_ref(), x.get_allocator_ref(), flag);
  2007. m_.capacity = x.m_.capacity;
  2008. m_.buffer = x.m_.buffer;
  2009. m_.front_idx = x.m_.front_idx;
  2010. m_.back_idx = x.m_.back_idx;
  2011. // leave x in valid state
  2012. x.m_.capacity = 0u;
  2013. x.m_.buffer = pointer();
  2014. x.m_.back_idx = x.m_.front_idx = 0;
  2015. }
  2016. void priv_move_assign(BOOST_RV_REF(devector) x, dtl::bool_<false> /*steal_resources*/)
  2017. {
  2018. //We can't guarantee a compile-time equal allocator or propagation so fallback to runtime
  2019. //Resources can be transferred if both allocators are equal
  2020. if (get_allocator_ref() == x.get_allocator_ref()) {
  2021. this->priv_move_assign(boost::move(x), dtl::true_());
  2022. }
  2023. else {
  2024. // We can't steal memory.
  2025. move_iterator<iterator> xbegin = boost::make_move_iterator(x.begin());
  2026. move_iterator<iterator> xend = boost::make_move_iterator(x.end());
  2027. //Move allocator if needed
  2028. dtl::bool_<allocator_traits_type::
  2029. propagate_on_container_move_assignment::value> flag;
  2030. dtl::move_alloc(this->get_allocator_ref(), x.get_allocator_ref(), flag);
  2031. if (m_.capacity >= x.size()) {
  2032. overwrite_buffer(xbegin, xend);
  2033. }
  2034. else {
  2035. allocate_and_copy_range(xbegin, xend);
  2036. }
  2037. }
  2038. }
  2039. BOOST_CONTAINER_ATTRIBUTE_NODISCARD inline
  2040. size_type pos_to_index(const_iterator i) const
  2041. {
  2042. return static_cast<size_type>(i - cbegin());
  2043. }
  2044. BOOST_CONTAINER_ATTRIBUTE_NODISCARD inline
  2045. bool should_move_back(const_iterator i) const
  2046. {
  2047. return static_cast<size_type>(this->pos_to_index(i)) >= this->size()/2u;
  2048. }
  2049. inline static iterator unconst_iterator(const_iterator i)
  2050. {
  2051. return boost::intrusive::pointer_traits<pointer>::const_cast_from(i);
  2052. }
  2053. inline size_type front_capacity() const
  2054. {
  2055. return m_.back_idx;
  2056. }
  2057. inline size_type back_capacity() const
  2058. {
  2059. return size_type(m_.capacity - m_.front_idx);
  2060. }
  2061. #ifndef BOOST_CONTAINER_DOXYGEN_INVOKED
  2062. inline T* priv_raw_begin() BOOST_NOEXCEPT
  2063. { return boost::movelib::to_raw_pointer(m_.buffer) + m_.front_idx; }
  2064. inline T* priv_raw_end() BOOST_NOEXCEPT
  2065. { return boost::movelib::to_raw_pointer(m_.buffer) + m_.back_idx; }
  2066. template <class U>
  2067. inline void priv_push_front(BOOST_FWD_REF(U) u)
  2068. {
  2069. this->emplace_front(boost::forward<U>(u));
  2070. }
  2071. template <class U>
  2072. inline void priv_push_back(BOOST_FWD_REF(U) u)
  2073. {
  2074. this->emplace_back(boost::forward<U>(u));
  2075. }
  2076. template <class U>
  2077. inline iterator priv_insert(const_iterator pos, BOOST_FWD_REF(U) u)
  2078. {
  2079. return this->emplace(pos, boost::forward<U>(u));
  2080. }
  2081. // allocator_type wrappers
  2082. inline allocator_type& get_allocator_ref() BOOST_NOEXCEPT
  2083. {
  2084. return static_cast<allocator_type&>(m_);
  2085. }
  2086. inline const allocator_type& get_allocator_ref() const BOOST_NOEXCEPT
  2087. {
  2088. return static_cast<const allocator_type&>(m_);
  2089. }
  2090. pointer allocate(size_type capacity)
  2091. {
  2092. pointer const p = impl::do_allocate(get_allocator_ref(), capacity);
  2093. #ifdef BOOST_CONTAINER_DEVECTOR_ALLOC_STATS
  2094. ++m_.capacity_alloc_count;
  2095. #endif // BOOST_CONTAINER_DEVECTOR_ALLOC_STATS
  2096. return p;
  2097. }
  2098. void destroy_elements(pointer begin, pointer end)
  2099. {
  2100. for (; begin != end; ++begin)
  2101. {
  2102. allocator_traits_type::destroy(get_allocator_ref(), boost::movelib::to_raw_pointer(begin));
  2103. }
  2104. }
  2105. void deallocate_buffer()
  2106. {
  2107. if (m_.buffer)
  2108. {
  2109. allocator_traits_type::deallocate(get_allocator_ref(), m_.buffer, m_.capacity);
  2110. }
  2111. }
  2112. #if !defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES) || defined(BOOST_CONTAINER_DOXYGEN_INVOKED)
  2113. template <typename... Args>
  2114. inline void alloc_construct(pointer dst, Args&&... args)
  2115. {
  2116. allocator_traits_type::construct(
  2117. get_allocator_ref(),
  2118. boost::movelib::to_raw_pointer(dst),
  2119. boost::forward<Args>(args)...
  2120. );
  2121. }
  2122. template <typename... Args>
  2123. void construct_n(pointer buffer, size_type n, Args&&... args)
  2124. {
  2125. detail::construction_guard<allocator_type> ctr_guard(buffer, get_allocator_ref());
  2126. guarded_construct_n(buffer, n, ctr_guard, boost::forward<Args>(args)...);
  2127. ctr_guard.release();
  2128. }
  2129. template <typename... Args>
  2130. void guarded_construct_n(pointer buffer, size_type n, detail::construction_guard<allocator_type>& ctr_guard, Args&&... args)
  2131. {
  2132. for (size_type i = 0; i < n; ++i) {
  2133. this->alloc_construct(buffer + i, boost::forward<Args>(args)...);
  2134. ctr_guard.extend();
  2135. }
  2136. }
  2137. #else //!defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES) || defined(BOOST_CONTAINER_DOXYGEN_INVOKED)
  2138. #define BOOST_CONTAINER_DEVECTOR_ALLOC_CONSTRUCT(N) \
  2139. BOOST_MOVE_TMPL_LT##N BOOST_MOVE_CLASS##N BOOST_MOVE_GT##N \
  2140. inline void alloc_construct(pointer dst BOOST_MOVE_I##N BOOST_MOVE_UREF##N)\
  2141. {\
  2142. allocator_traits_type::construct(\
  2143. get_allocator_ref(), boost::movelib::to_raw_pointer(dst) BOOST_MOVE_I##N BOOST_MOVE_FWD##N );\
  2144. }\
  2145. \
  2146. BOOST_MOVE_TMPL_LT##N BOOST_MOVE_CLASS##N BOOST_MOVE_GT##N \
  2147. void construct_n(pointer buffer, size_type n BOOST_MOVE_I##N BOOST_MOVE_UREF##N)\
  2148. {\
  2149. detail::construction_guard<allocator_type> ctr_guard(buffer, get_allocator_ref());\
  2150. guarded_construct_n(buffer, n, ctr_guard BOOST_MOVE_I##N BOOST_MOVE_FWD##N);\
  2151. ctr_guard.release();\
  2152. }\
  2153. \
  2154. BOOST_MOVE_TMPL_LT##N BOOST_MOVE_CLASS##N BOOST_MOVE_GT##N \
  2155. void guarded_construct_n(pointer buffer, size_type n, detail::construction_guard<allocator_type>& ctr_guard BOOST_MOVE_I##N BOOST_MOVE_UREF##N)\
  2156. {\
  2157. for (size_type i = 0; i < n; ++i) {\
  2158. this->alloc_construct(buffer + i BOOST_MOVE_I##N BOOST_MOVE_FWD##N);\
  2159. ctr_guard.extend();\
  2160. }\
  2161. }
  2162. //
  2163. BOOST_MOVE_ITERATE_0TO9(BOOST_CONTAINER_DEVECTOR_ALLOC_CONSTRUCT)
  2164. #undef BOOST_CONTAINER_DEVECTOR_ALLOC_CONSTRUCT
  2165. #endif //!defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES) || defined(BOOST_CONTAINER_DOXYGEN_INVOKED)
  2166. size_type calculate_new_capacity(size_type requested_capacity)
  2167. {
  2168. size_type max = allocator_traits_type::max_size(this->get_allocator_ref());
  2169. (clamp_by_stored_size_type)(max, stored_size_type());
  2170. const size_type remaining_additional_cap = max - size_type(m_.capacity);
  2171. const size_type min_additional_cap = requested_capacity - size_type(m_.capacity);
  2172. if ( remaining_additional_cap < min_additional_cap )
  2173. boost::container::throw_length_error("devector: get_next_capacity, max size exceeded");
  2174. return growth_factor_type()( size_type(m_.capacity), min_additional_cap, max);
  2175. }
  2176. void buffer_move_or_copy(pointer dst)
  2177. {
  2178. detail::construction_guard<allocator_type> guard(dst, get_allocator_ref());
  2179. buffer_move_or_copy(dst, guard);
  2180. guard.release();
  2181. }
  2182. void buffer_move_or_copy(pointer dst, detail::construction_guard<allocator_type>& guard)
  2183. {
  2184. opt_move_or_copy(begin(), end(), dst, guard);
  2185. destroy_elements(data(), data() + size());
  2186. deallocate_buffer();
  2187. }
  2188. template <typename Guard>
  2189. void opt_move_or_copy(pointer begin, pointer end, pointer dst, Guard& guard)
  2190. {
  2191. // if trivial copy and default allocator, memcpy
  2192. boost::container::uninitialized_move_alloc(get_allocator_ref(), begin, end, dst);
  2193. guard.extend();
  2194. }
  2195. #if !defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES) || defined(BOOST_CONTAINER_DOXYGEN_INVOKED)
  2196. template <typename... Args>
  2197. void resize_impl(size_type sz, Args&&... args)
  2198. {
  2199. const size_type old_sz = this->size();
  2200. if (sz > old_sz)
  2201. {
  2202. const size_type n = sz - old_sz;
  2203. if (sz <= m_.capacity)
  2204. {
  2205. //Construct at back
  2206. const size_type bfc = this->back_free_capacity();
  2207. const size_type b = n < bfc ? n : bfc;
  2208. construct_n(m_.buffer + m_.back_idx, b, boost::forward<Args>(args)...);
  2209. m_.set_back_idx(m_.back_idx + b);
  2210. //Construct remaining at front
  2211. const size_type f = n - b;
  2212. construct_n(m_.buffer + m_.front_idx - f, f, boost::forward<Args>(args)...);
  2213. m_.set_front_idx(m_.front_idx - f);
  2214. }
  2215. else
  2216. {
  2217. resize_back_slow_path(sz, n, boost::forward<Args>(args)...);
  2218. }
  2219. }
  2220. else
  2221. {
  2222. const size_type n = old_sz - sz;
  2223. const size_type new_bidx = m_.back_idx - n;
  2224. destroy_elements(m_.buffer + new_bidx, m_.buffer + m_.back_idx);
  2225. m_.set_back_idx(new_bidx);
  2226. }
  2227. }
  2228. template <typename... Args>
  2229. void resize_front_impl(size_type sz , Args&&... args)
  2230. {
  2231. const size_type old_sz = this->size();
  2232. if (sz > old_sz)
  2233. {
  2234. const size_type n = sz - old_sz;
  2235. if (sz <= this->front_capacity())
  2236. {
  2237. construct_n(m_.buffer + m_.front_idx - n, n, boost::forward<Args>(args)...);
  2238. m_.set_front_idx(m_.front_idx - n);
  2239. }
  2240. else
  2241. {
  2242. resize_front_slow_path(sz, n, boost::forward<Args>(args)...);
  2243. }
  2244. }
  2245. else {
  2246. const size_type n = old_sz - sz;
  2247. const size_type new_fidx = m_.front_idx + n;
  2248. destroy_elements(m_.buffer + m_.front_idx, m_.buffer + new_fidx);
  2249. m_.set_front_idx(new_fidx);
  2250. }
  2251. }
  2252. template <typename... Args>
  2253. void resize_front_slow_path(size_type sz, size_type n, Args&&... args)
  2254. {
  2255. const size_type new_capacity = calculate_new_capacity(sz + back_free_capacity());
  2256. pointer new_buffer = allocate(new_capacity);
  2257. allocation_guard new_buffer_guard(new_buffer, new_capacity, get_allocator_ref());
  2258. const size_type old_sz = this->size();
  2259. const size_type new_old_elem_index = new_capacity - old_sz;
  2260. const size_type new_elem_index = new_old_elem_index - n;
  2261. detail::construction_guard<allocator_type> guard(new_buffer + new_elem_index, get_allocator_ref());
  2262. guarded_construct_n(new_buffer + new_elem_index, n, guard, boost::forward<Args>(args)...);
  2263. buffer_move_or_copy(new_buffer + new_old_elem_index, guard);
  2264. guard.release();
  2265. new_buffer_guard.release();
  2266. m_.buffer = new_buffer;
  2267. m_.set_capacity(new_capacity);
  2268. m_.set_front_idx(new_elem_index);
  2269. m_.set_back_idx(new_elem_index + old_sz + n);
  2270. }
  2271. template <typename... Args>
  2272. void resize_back_impl(size_type sz, Args&&... args)
  2273. {
  2274. const size_type old_sz = this->size();
  2275. if (sz > old_sz)
  2276. {
  2277. const size_type n = sz - old_sz;
  2278. if (sz <= this->back_capacity())
  2279. {
  2280. construct_n(m_.buffer + m_.back_idx, n, boost::forward<Args>(args)...);
  2281. m_.set_back_idx(m_.back_idx + n);
  2282. }
  2283. else
  2284. {
  2285. resize_back_slow_path(sz, n, boost::forward<Args>(args)...);
  2286. }
  2287. }
  2288. else
  2289. {
  2290. const size_type n = old_sz - sz;
  2291. const size_type new_bidx = m_.back_idx - n;
  2292. destroy_elements(m_.buffer + new_bidx, m_.buffer + m_.back_idx);
  2293. m_.set_back_idx(new_bidx);
  2294. }
  2295. }
  2296. template <typename... Args>
  2297. void resize_back_slow_path(size_type sz, size_type n, Args&&... args)
  2298. {
  2299. const size_type new_capacity = calculate_new_capacity(sz + front_free_capacity());
  2300. pointer new_buffer = allocate(new_capacity);
  2301. allocation_guard new_buffer_guard(new_buffer, new_capacity, get_allocator_ref());
  2302. detail::construction_guard<allocator_type> guard(new_buffer + m_.back_idx, get_allocator_ref());
  2303. guarded_construct_n(new_buffer + m_.back_idx, n, guard, boost::forward<Args>(args)...);
  2304. buffer_move_or_copy(new_buffer + m_.front_idx);
  2305. guard.release();
  2306. new_buffer_guard.release();
  2307. m_.buffer = new_buffer;
  2308. m_.set_capacity(new_capacity);
  2309. m_.set_back_idx(m_.back_idx + n);
  2310. }
  2311. #else //!defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES) || defined(BOOST_CONTAINER_DOXYGEN_INVOKED)
  2312. #define BOOST_CONTAINER_DEVECTOR_SLOW_PATH(N) \
  2313. BOOST_MOVE_TMPL_LT##N BOOST_MOVE_CLASS##N BOOST_MOVE_GT##N \
  2314. void resize_front_impl(size_type sz BOOST_MOVE_I##N BOOST_MOVE_UREF##N)\
  2315. {\
  2316. if (sz > size())\
  2317. {\
  2318. const size_type n = sz - size();\
  2319. if (sz <= front_capacity()){\
  2320. construct_n(m_.buffer + m_.front_idx - n, n BOOST_MOVE_I##N BOOST_MOVE_FWD##N);\
  2321. m_.set_front_idx(m_.front_idx - n);\
  2322. }\
  2323. else\
  2324. {\
  2325. resize_front_slow_path(sz, n BOOST_MOVE_I##N BOOST_MOVE_FWD##N);\
  2326. }\
  2327. }\
  2328. else {\
  2329. while (this->size() > sz)\
  2330. {\
  2331. this->pop_front();\
  2332. }\
  2333. }\
  2334. }\
  2335. \
  2336. BOOST_MOVE_TMPL_LT##N BOOST_MOVE_CLASS##N BOOST_MOVE_GT##N \
  2337. void resize_front_slow_path(size_type sz, size_type n BOOST_MOVE_I##N BOOST_MOVE_UREF##N)\
  2338. {\
  2339. const size_type new_capacity = calculate_new_capacity(sz + back_free_capacity());\
  2340. pointer new_buffer = allocate(new_capacity);\
  2341. allocation_guard new_buffer_guard(new_buffer, new_capacity, get_allocator_ref());\
  2342. \
  2343. const size_type new_old_elem_index = new_capacity - size();\
  2344. const size_type new_elem_index = new_old_elem_index - n;\
  2345. \
  2346. detail::construction_guard<allocator_type> guard(new_buffer + new_elem_index, get_allocator_ref());\
  2347. guarded_construct_n(new_buffer + new_elem_index, n, guard BOOST_MOVE_I##N BOOST_MOVE_FWD##N);\
  2348. \
  2349. buffer_move_or_copy(new_buffer + new_old_elem_index, guard);\
  2350. \
  2351. guard.release();\
  2352. new_buffer_guard.release();\
  2353. m_.buffer = new_buffer;\
  2354. m_.set_capacity(new_capacity);\
  2355. m_.set_back_idx(new_old_elem_index + m_.back_idx - m_.front_idx);\
  2356. m_.set_front_idx(new_elem_index);\
  2357. }\
  2358. \
  2359. BOOST_MOVE_TMPL_LT##N BOOST_MOVE_CLASS##N BOOST_MOVE_GT##N \
  2360. void resize_back_impl(size_type sz BOOST_MOVE_I##N BOOST_MOVE_UREF##N)\
  2361. {\
  2362. if (sz > size())\
  2363. {\
  2364. const size_type n = sz - size();\
  2365. \
  2366. if (sz <= back_capacity())\
  2367. {\
  2368. construct_n(m_.buffer + m_.back_idx, n BOOST_MOVE_I##N BOOST_MOVE_FWD##N);\
  2369. m_.set_back_idx(m_.back_idx + n);\
  2370. }\
  2371. else\
  2372. {\
  2373. resize_back_slow_path(sz, n BOOST_MOVE_I##N BOOST_MOVE_FWD##N);\
  2374. }\
  2375. }\
  2376. else\
  2377. {\
  2378. while (size() > sz)\
  2379. {\
  2380. pop_back();\
  2381. }\
  2382. }\
  2383. }\
  2384. \
  2385. BOOST_MOVE_TMPL_LT##N BOOST_MOVE_CLASS##N BOOST_MOVE_GT##N \
  2386. void resize_back_slow_path(size_type sz, size_type n BOOST_MOVE_I##N BOOST_MOVE_UREF##N)\
  2387. {\
  2388. const size_type new_capacity = calculate_new_capacity(sz + front_free_capacity());\
  2389. pointer new_buffer = allocate(new_capacity);\
  2390. allocation_guard new_buffer_guard(new_buffer, new_capacity, get_allocator_ref());\
  2391. \
  2392. detail::construction_guard<allocator_type> guard(new_buffer + m_.back_idx, get_allocator_ref());\
  2393. guarded_construct_n(new_buffer + m_.back_idx, n, guard BOOST_MOVE_I##N BOOST_MOVE_FWD##N);\
  2394. \
  2395. buffer_move_or_copy(new_buffer + m_.front_idx);\
  2396. \
  2397. guard.release();\
  2398. new_buffer_guard.release();\
  2399. \
  2400. m_.buffer = new_buffer;\
  2401. m_.set_capacity(new_capacity);\
  2402. m_.set_back_idx(m_.back_idx + n);\
  2403. }\
  2404. \
  2405. //
  2406. BOOST_MOVE_ITERATE_0TO9(BOOST_CONTAINER_DEVECTOR_SLOW_PATH)
  2407. #undef BOOST_CONTAINER_DEVECTOR_SLOW_PATH
  2408. #endif //!defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES) || defined(BOOST_CONTAINER_DOXYGEN_INVOKED)
  2409. void reallocate_at(size_type new_capacity, size_type buffer_offset)
  2410. {
  2411. pointer new_buffer = allocate(new_capacity);
  2412. {
  2413. allocation_guard new_buffer_guard(new_buffer, new_capacity, get_allocator_ref());
  2414. boost::container::uninitialized_move_alloc(get_allocator_ref(), this->begin(), this->end(), new_buffer + buffer_offset);
  2415. new_buffer_guard.release();
  2416. }
  2417. destroy_elements(m_.buffer + m_.front_idx, m_.buffer + m_.back_idx);
  2418. deallocate_buffer();
  2419. m_.buffer = new_buffer;
  2420. //Safe cast, allocate() will handle stored_size_type overflow
  2421. m_.set_capacity(new_capacity);
  2422. m_.set_back_idx(size_type(m_.back_idx - m_.front_idx) + buffer_offset);
  2423. m_.set_front_idx(buffer_offset);
  2424. BOOST_ASSERT(invariants_ok());
  2425. }
  2426. template <typename ForwardIterator>
  2427. iterator insert_range(const_iterator position, ForwardIterator first, ForwardIterator last)
  2428. {
  2429. BOOST_ASSERT(position >= begin());
  2430. BOOST_ASSERT(position <= end());
  2431. typedef dtl::insert_range_proxy<allocator_type, ForwardIterator> proxy_t;
  2432. size_type const n = boost::container::iterator_udistance(first, last);
  2433. bool prefer_move_back;
  2434. if (BOOST_UNLIKELY(!n)) {
  2435. return begin() + size_type(position - cbegin());
  2436. }
  2437. else if (position == end()) {
  2438. if(back_free_capacity() >= n) // fast path
  2439. {
  2440. iterator r(this->end());
  2441. boost::container::uninitialized_copy_alloc(get_allocator_ref(), first, last, this->priv_raw_end());
  2442. m_.set_back_idx(m_.back_idx + n);
  2443. return r;
  2444. }
  2445. prefer_move_back = true;
  2446. }
  2447. else if (position == begin()) {
  2448. if(front_free_capacity() >= n) {// secondary fast path
  2449. boost::container::uninitialized_copy_alloc(get_allocator_ref(), first, last, this->priv_raw_begin() - n);
  2450. m_.set_front_idx(m_.front_idx - n);
  2451. return begin();
  2452. }
  2453. prefer_move_back = false;
  2454. }
  2455. else{
  2456. iterator nonconst_pos = unconst_iterator(position);
  2457. prefer_move_back = should_move_back(position);
  2458. if(prefer_move_back){
  2459. if(back_free_capacity() >= n){
  2460. boost::container::expand_forward_and_insert_nonempty_middle_alloc
  2461. ( get_allocator_ref()
  2462. , boost::movelib::to_raw_pointer(nonconst_pos)
  2463. , this->priv_raw_end()
  2464. , n, proxy_t(first));
  2465. m_.set_back_idx(m_.back_idx + n);
  2466. return nonconst_pos;
  2467. }
  2468. }
  2469. else{
  2470. if (front_free_capacity() >= n){
  2471. boost::container::expand_backward_and_insert_nonempty_middle_alloc
  2472. ( get_allocator_ref()
  2473. , this->priv_raw_begin()
  2474. , boost::movelib::to_raw_pointer(nonconst_pos)
  2475. , n, proxy_t(first));
  2476. m_.set_front_idx(m_.front_idx - n);
  2477. return (nonconst_pos -= n);
  2478. }
  2479. }
  2480. }
  2481. return this->insert_range_slow_path(position, n, proxy_t(first));
  2482. }
  2483. template <class InsertionProxy>
  2484. BOOST_CONTAINER_NOINLINE iterator insert_range_slow_path
  2485. (const_iterator p, const size_type n, const InsertionProxy proxy)
  2486. {
  2487. size_type const back_free_cap = back_free_capacity();
  2488. size_type const front_free_cap = front_free_capacity();
  2489. size_type const free_cap = front_free_cap + back_free_cap;
  2490. size_type const index = size_type(p - cbegin());
  2491. size_type const cap = m_.capacity;
  2492. //Test if enough free memory would be left
  2493. if (free_cap >= n && (free_cap - n) >= cap/devector_min_free_fraction) {
  2494. size_type const old_size = this->size();
  2495. T* const raw_pos = const_cast<T*>(boost::movelib::to_raw_pointer(p));
  2496. size_type const new_size = old_size + n;
  2497. size_type const new_front_idx = (cap - new_size) / 2u;
  2498. T* const raw_beg = this->priv_raw_begin();
  2499. T* const new_raw_beg = raw_beg - std::ptrdiff_t(m_.front_idx - new_front_idx);
  2500. m_.back_idx = 0u;
  2501. m_.front_idx = 0u;
  2502. boost::container::expand_backward_forward_and_insert_alloc
  2503. (raw_beg, old_size, new_raw_beg, raw_pos, n, proxy, get_allocator_ref());
  2504. m_.set_front_idx(new_front_idx);
  2505. m_.set_back_idx(new_front_idx + new_size);
  2506. }
  2507. else {
  2508. // reallocate
  2509. const size_type new_capacity = calculate_new_capacity(m_.capacity + n);
  2510. pointer new_buffer = allocate(new_capacity);
  2511. // guard allocation
  2512. allocation_guard new_buffer_guard(new_buffer, new_capacity, get_allocator_ref());
  2513. size_type const old_size = this->size();
  2514. const size_type new_front_index = (new_capacity - old_size - n) / 2u;
  2515. T* const raw_pos = const_cast<T*>(boost::movelib::to_raw_pointer(p));
  2516. T* const raw_new_start = const_cast<T*>(boost::movelib::to_raw_pointer(new_buffer)) + new_front_index;
  2517. boost::container::uninitialized_move_and_insert_alloc
  2518. (get_allocator_ref(), this->priv_raw_begin(), raw_pos, this->priv_raw_end(), raw_new_start, n, proxy);
  2519. new_buffer_guard.release();
  2520. // cleanup
  2521. destroy_elements(begin(), end());
  2522. deallocate_buffer();
  2523. // rebind members
  2524. m_.set_capacity(new_capacity);
  2525. m_.buffer = new_buffer;
  2526. m_.set_back_idx(new_front_index + old_size + n);
  2527. m_.set_front_idx(new_front_index);
  2528. }
  2529. return begin() + index;
  2530. }
  2531. template <typename Iterator>
  2532. void construct_from_range(Iterator begin, Iterator end)
  2533. {
  2534. allocation_guard buffer_guard(m_.buffer, m_.capacity, get_allocator_ref());
  2535. boost::container::uninitialized_copy_alloc(get_allocator_ref(), begin, end, m_.buffer);
  2536. buffer_guard.release();
  2537. }
  2538. template <typename ForwardIterator>
  2539. void allocate_and_copy_range(ForwardIterator first, ForwardIterator last)
  2540. {
  2541. size_type n = boost::container::iterator_udistance(first, last);
  2542. pointer new_buffer = n ? allocate(n) : pointer();
  2543. allocation_guard new_buffer_guard(new_buffer, n, get_allocator_ref());
  2544. boost::container::uninitialized_copy_alloc(get_allocator_ref(), first, last, new_buffer);
  2545. destroy_elements(begin(), end());
  2546. deallocate_buffer();
  2547. m_.set_capacity(n);
  2548. m_.buffer = new_buffer;
  2549. m_.front_idx = 0;
  2550. m_.set_back_idx(n);
  2551. new_buffer_guard.release();
  2552. }
  2553. static void swap_big_big(devector& a, devector& b) BOOST_NOEXCEPT
  2554. {
  2555. boost::adl_move_swap(a.m_.capacity, b.m_.capacity);
  2556. boost::adl_move_swap(a.m_.buffer, b.m_.buffer);
  2557. }
  2558. template <typename ForwardIterator>
  2559. void overwrite_buffer_impl(ForwardIterator first, ForwardIterator last, dtl::true_)
  2560. {
  2561. const size_type n = boost::container::iterator_udistance(first, last);
  2562. BOOST_ASSERT(m_.capacity >= n);
  2563. boost::container::uninitialized_copy_alloc_n
  2564. ( get_allocator_ref(), first
  2565. , n, boost::movelib::to_raw_pointer(m_.buffer));
  2566. m_.front_idx = 0;
  2567. m_.set_back_idx(n);
  2568. }
  2569. template <typename InputIterator>
  2570. InputIterator overwrite_buffer_impl(InputIterator first, InputIterator last, dtl::false_)
  2571. {
  2572. pointer pos = m_.buffer;
  2573. detail::construction_guard<allocator_type> front_guard(pos, get_allocator_ref());
  2574. while (first != last && pos != begin()) {
  2575. this->alloc_construct(pos++, *first++);
  2576. front_guard.extend();
  2577. }
  2578. while (first != last && pos != end()) {
  2579. *pos++ = *first++;
  2580. }
  2581. detail::construction_guard<allocator_type> back_guard(pos, get_allocator_ref());
  2582. iterator capacity_end = m_.buffer + m_.capacity;
  2583. while (first != last && pos != capacity_end) {
  2584. this->alloc_construct(pos++, *first++);
  2585. back_guard.extend();
  2586. }
  2587. pointer destroy_after = dtl::min_value(dtl::max_value(begin(), pos), end());
  2588. destroy_elements(destroy_after, end());
  2589. front_guard.release();
  2590. back_guard.release();
  2591. m_.front_idx = 0;
  2592. m_.set_back_idx(pos_to_index(pos));
  2593. return first;
  2594. }
  2595. template <typename ForwardIterator>
  2596. inline void overwrite_buffer(ForwardIterator first, ForwardIterator last)
  2597. {
  2598. this->overwrite_buffer_impl(first, last,
  2599. dtl::bool_<dtl::is_trivially_destructible<T>::value>());
  2600. }
  2601. bool invariants_ok()
  2602. {
  2603. return (! m_.capacity || m_.buffer )
  2604. && m_.front_idx <= m_.back_idx
  2605. && m_.back_idx <= m_.capacity;
  2606. }
  2607. struct impl : allocator_type
  2608. {
  2609. BOOST_MOVABLE_BUT_NOT_COPYABLE(impl)
  2610. public:
  2611. allocator_type &get_al()
  2612. { return *this; }
  2613. static pointer do_allocate(allocator_type &a, size_type cap)
  2614. {
  2615. if (cap) {
  2616. //First detect overflow on smaller stored_size_types
  2617. if (cap > stored_size_type(-1)){
  2618. boost::container::throw_length_error("get_next_capacity, allocator's max size reached");
  2619. }
  2620. return allocator_traits_type::allocate(a, cap);
  2621. }
  2622. else {
  2623. return pointer();
  2624. }
  2625. }
  2626. impl()
  2627. : allocator_type(), buffer(), front_idx(), back_idx(), capacity()
  2628. #ifdef BOOST_CONTAINER_DEVECTOR_ALLOC_STATS
  2629. , capacity_alloc_count(0)
  2630. #endif
  2631. {}
  2632. explicit impl(const allocator_type &a)
  2633. : allocator_type(a), buffer(), front_idx(), back_idx(), capacity()
  2634. #ifdef BOOST_CONTAINER_DEVECTOR_ALLOC_STATS
  2635. , capacity_alloc_count(0)
  2636. #endif
  2637. {}
  2638. impl(reserve_uninitialized_t, const allocator_type& a, size_type c)
  2639. : allocator_type(a), buffer(do_allocate(get_al(), c) )
  2640. //static cast sizes, as the allocation function will take care of overflows
  2641. , front_idx(static_cast<stored_size_type>(0u))
  2642. , back_idx(static_cast<stored_size_type>(c))
  2643. , capacity(static_cast<stored_size_type>(c))
  2644. #ifdef BOOST_CONTAINER_DEVECTOR_ALLOC_STATS
  2645. , capacity_alloc_count(size_type(buffer != pointer()))
  2646. #endif
  2647. {}
  2648. impl(reserve_only_tag_t, const allocator_type &a, size_type const ffc, size_type const bfc)
  2649. : allocator_type(a), buffer(do_allocate(get_al(), ffc+bfc) )
  2650. //static cast sizes, as the allocation function will take care of overflows
  2651. , front_idx(static_cast<stored_size_type>(ffc))
  2652. , back_idx(static_cast<stored_size_type>(ffc))
  2653. , capacity(static_cast<stored_size_type>(ffc + bfc))
  2654. #ifdef BOOST_CONTAINER_DEVECTOR_ALLOC_STATS
  2655. , capacity_alloc_count(size_type(buffer != pointer()))
  2656. #endif
  2657. {}
  2658. impl(reserve_only_tag_t, const allocator_type &a, size_type const c)
  2659. : allocator_type(a), buffer(do_allocate(get_al(), c) )
  2660. //static cast sizes, as the allocation function will take care of overflows
  2661. , front_idx(static_cast<stored_size_type>(c/2u))
  2662. , back_idx(static_cast<stored_size_type>(c/2u))
  2663. , capacity(static_cast<stored_size_type>(c))
  2664. #ifdef BOOST_CONTAINER_DEVECTOR_ALLOC_STATS
  2665. , capacity_alloc_count(size_type(buffer != pointer()))
  2666. #endif
  2667. {}
  2668. impl(review_implementation_t, const allocator_type &a, pointer p, size_type fi, size_type bi, size_type c)
  2669. : allocator_type(a), buffer(p)
  2670. //static cast sizes, as the allocation function will take care of overflows
  2671. , front_idx(static_cast<stored_size_type>(fi))
  2672. , back_idx(static_cast<stored_size_type>(bi))
  2673. , capacity(static_cast<stored_size_type>(c))
  2674. #ifdef BOOST_CONTAINER_DEVECTOR_ALLOC_STATS
  2675. , capacity_alloc_count(0)
  2676. #endif
  2677. {}
  2678. impl(BOOST_RV_REF(impl) m)
  2679. : allocator_type(BOOST_MOVE_BASE(allocator_type, m))
  2680. , buffer(static_cast<impl&>(m).buffer)
  2681. , front_idx(static_cast<impl&>(m).front_idx)
  2682. , back_idx(static_cast<impl&>(m).back_idx)
  2683. , capacity(static_cast<impl&>(m).capacity)
  2684. #ifdef BOOST_CONTAINER_DEVECTOR_ALLOC_STATS
  2685. , capacity_alloc_count(0)
  2686. #endif
  2687. {
  2688. impl &i = static_cast<impl&>(m);
  2689. // buffer is already acquired, reset rhs
  2690. i.capacity = 0u;
  2691. i.buffer = pointer();
  2692. i.front_idx = 0;
  2693. i.back_idx = 0;
  2694. }
  2695. inline void set_back_idx(size_type bi)
  2696. {
  2697. back_idx = static_cast<stored_size_type>(bi);
  2698. }
  2699. inline void set_front_idx(size_type fi)
  2700. {
  2701. front_idx = static_cast<stored_size_type>(fi);
  2702. }
  2703. inline void set_capacity(size_type c)
  2704. {
  2705. capacity = static_cast<stored_size_type>(c);
  2706. }
  2707. pointer buffer;
  2708. stored_size_type front_idx;
  2709. stored_size_type back_idx;
  2710. stored_size_type capacity;
  2711. #ifdef BOOST_CONTAINER_DEVECTOR_ALLOC_STATS
  2712. size_type capacity_alloc_count;
  2713. #endif
  2714. } m_;
  2715. #ifdef BOOST_CONTAINER_DEVECTOR_ALLOC_STATS
  2716. public:
  2717. void reset_alloc_stats()
  2718. {
  2719. m_.capacity_alloc_count = 0;
  2720. }
  2721. size_type get_alloc_count() const
  2722. {
  2723. return m_.capacity_alloc_count;
  2724. }
  2725. #endif // ifdef BOOST_CONTAINER_DEVECTOR_ALLOC_STATS
  2726. #endif // ifndef BOOST_CONTAINER_DOXYGEN_INVOKED
  2727. };
  2728. }} // namespace boost::container
  2729. #ifndef BOOST_CONTAINER_DOXYGEN_INVOKED
  2730. namespace boost {
  2731. //!has_trivial_destructor_after_move<> == true_type
  2732. //!specialization for optimizations
  2733. template <class T, class Allocator, class Options>
  2734. struct has_trivial_destructor_after_move<boost::container::devector<T, Allocator, Options> >
  2735. {
  2736. typedef typename boost::container::devector<T, Allocator, Options>::allocator_type allocator_type;
  2737. typedef typename ::boost::container::allocator_traits<allocator_type>::pointer pointer;
  2738. BOOST_STATIC_CONSTEXPR bool value =
  2739. ::boost::has_trivial_destructor_after_move<allocator_type>::value &&
  2740. ::boost::has_trivial_destructor_after_move<pointer>::value;
  2741. };
  2742. }
  2743. #endif //#ifndef BOOST_CONTAINER_DOXYGEN_INVOKED
  2744. #include <boost/container/detail/config_end.hpp>
  2745. #endif // BOOST_CONTAINER_DEVECTOR_HPP