devector.hpp 102 KB

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