devector.hpp 107 KB

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