devector.hpp 101 KB

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