deque.hpp 116 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115
  1. //////////////////////////////////////////////////////////////////////////////
  2. //
  3. // (C) Copyright Ion Gaztanaga 2025-2025. Distributed under the Boost
  4. // Software License, Version 1.0. (See accompanying file
  5. // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  6. //
  7. // See http://www.boost.org/libs/container for documentation.
  8. //
  9. //////////////////////////////////////////////////////////////////////////////
  10. #ifndef BOOST_CONTAINER_DEQUE_HPP
  11. #define BOOST_CONTAINER_DEQUE_HPP
  12. #ifndef BOOST_CONFIG_HPP
  13. # include <boost/config.hpp>
  14. #endif
  15. #if defined(BOOST_HAS_PRAGMA_ONCE)
  16. # pragma once
  17. #endif
  18. #include <boost/container/detail/config_begin.hpp>
  19. #include <boost/container/detail/workaround.hpp>
  20. // container
  21. #include <boost/container/allocator_traits.hpp>
  22. #include <boost/container/container_fwd.hpp>
  23. #include <boost/container/new_allocator.hpp> //new_allocator
  24. #include <boost/container/throw_exception.hpp>
  25. #include <boost/container/options.hpp>
  26. // container/detail
  27. #include <boost/container/detail/advanced_insert_int.hpp>
  28. #include <boost/container/detail/algorithm.hpp> //algo_equal(), algo_lexicographical_compare
  29. #include <boost/container/detail/alloc_helpers.hpp>
  30. #include <boost/container/detail/copy_move_algo.hpp>
  31. #include <boost/container/detail/iterator.hpp>
  32. #include <boost/move/detail/iterator_to_raw_pointer.hpp>
  33. #include <boost/container/detail/iterators.hpp>
  34. #include <boost/container/detail/min_max.hpp>
  35. #include <boost/container/detail/mpl.hpp>
  36. #include <boost/move/detail/to_raw_pointer.hpp>
  37. #include <boost/container/detail/type_traits.hpp>
  38. #include <boost/container/detail/math_functions.hpp>
  39. // move
  40. #include <boost/move/adl_move_swap.hpp>
  41. #include <boost/move/algo/detail/merge.hpp>
  42. #include <boost/move/iterator.hpp>
  43. #include <boost/move/traits.hpp>
  44. #include <boost/move/utility_core.hpp>
  45. // move/detail
  46. #if defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES)
  47. #include <boost/move/detail/fwd_macros.hpp>
  48. #endif
  49. #include <boost/move/detail/move_helpers.hpp>
  50. // other
  51. #include <boost/assert.hpp>
  52. // std
  53. #include <cstddef>
  54. #if !defined(BOOST_NO_CXX11_HDR_INITIALIZER_LIST)
  55. #include <initializer_list>
  56. #endif
  57. namespace boost {
  58. namespace container {
  59. #ifndef BOOST_CONTAINER_DOXYGEN_INVOKED
  60. template <class T>
  61. struct deque_value_traits
  62. {
  63. typedef T value_type;
  64. BOOST_STATIC_CONSTEXPR bool trivial_dctr = dtl::is_trivially_destructible<value_type>::value;
  65. BOOST_STATIC_CONSTEXPR bool trivial_dctr_after_move = ::boost::has_trivial_destructor_after_move<value_type>::value;
  66. };
  67. template<class T, std::size_t BlockBytes, std::size_t BlockSize, class StoredSizeType>
  68. struct deque_block_traits
  69. {
  70. BOOST_CONTAINER_STATIC_ASSERT_MSG(!(BlockBytes && BlockSize), "BlockBytes and BlockSize can't be specified at the same time");
  71. //Calculate default block size
  72. BOOST_STATIC_CONSTEXPR std::size_t default_block_bytes = sizeof(void*)*128u;
  73. BOOST_STATIC_CONSTEXPR std::size_t default_block_start = (default_block_bytes - 1u)/sizeof(T) + 1u;
  74. //Round to the next power of two
  75. BOOST_STATIC_CONSTEXPR std::size_t default_block_size_upp_pow2 = dtl::upper_power_of_2_ct<std::size_t, default_block_start>::value;
  76. //Check minimal size
  77. BOOST_STATIC_CONSTEXPR std::size_t default_min_block_size_pow = 3u;
  78. BOOST_STATIC_CONSTEXPR std::size_t default_min_block_size = 8u;
  79. BOOST_STATIC_CONSTEXPR std::size_t default_block_size_initial = default_block_size_upp_pow2 < default_min_block_size
  80. ? default_min_block_size
  81. : default_block_size_upp_pow2;
  82. //Limit by stored size max value
  83. BOOST_STATIC_CONSTEXPR std::size_t max_stored_size_block_size = std::size_t(1u) << (sizeof(StoredSizeType)*CHAR_BIT - default_min_block_size_pow);
  84. BOOST_STATIC_CONSTEXPR std::size_t default_block_size = default_block_size_initial > max_stored_size_block_size
  85. ? max_stored_size_block_size
  86. : default_block_size_initial;
  87. //Now select between the default or the specified by the user
  88. BOOST_STATIC_CONSTEXPR std::size_t value = BlockSize ? BlockSize
  89. : BlockBytes ? (BlockBytes-1u)/sizeof(T) + 1u : default_block_size
  90. ;
  91. BOOST_CONTAINER_STATIC_ASSERT_MSG(value <= max_stored_size_block_size, "BlockSize or BlockBytes is too big for the stored_size_type");
  92. };
  93. // Class invariants:
  94. // For any nonsingular iterator i:
  95. // i.node is the address of an element in the map array. The
  96. // contents of i.node is a pointer to the beginning of a node.
  97. // i.cur is a pointer in the range [i.first, i.last). NOTE:
  98. // the implication of this is that i.cur is always a dereferenceable
  99. // pointer, even if i is a past-the-end iterator.
  100. // For every node other than start.node and finish.node, every element
  101. // in the node is an initialized object. If start.node == finish.node,
  102. // then [start.cur, finish.cur) are initialized objects, and
  103. // the elements outside that range are uninitialized storage. Otherwise,
  104. // [start.cur, start.last) and [finish.first, finish.cur) are initialized
  105. // objects, and [start.first, start.cur) and [finish.cur, finish.last)
  106. // are uninitialized storage.
  107. // [map, map + map_size) is a valid range.
  108. // [start.node, finish.node] is a valid range contained within
  109. // [map, map + map_size).
  110. // A pointer in the range [map, map + map_size) points to an allocated node
  111. // if and only if the pointer is in the range [start.node, finish.node].
  112. template<class Pointer, bool IsConst, unsigned BlockBytes, unsigned BlockSize, class StoredSizeType>
  113. class deque_iterator
  114. {
  115. public:
  116. typedef std::random_access_iterator_tag iterator_category;
  117. typedef typename boost::intrusive::pointer_traits<Pointer>::element_type value_type;
  118. typedef typename boost::intrusive::pointer_traits<Pointer>::difference_type difference_type;
  119. typedef typename boost::intrusive::pointer_traits<Pointer>::size_type size_type;
  120. typedef typename dtl::if_c
  121. < IsConst
  122. , typename boost::intrusive::pointer_traits<Pointer>::template
  123. rebind_pointer<const value_type>::type
  124. , Pointer
  125. >::type pointer;
  126. typedef typename dtl::if_c
  127. < IsConst
  128. , const value_type&
  129. , value_type&
  130. >::type reference;
  131. BOOST_CONSTEXPR inline static size_type get_block_size() BOOST_NOEXCEPT_OR_NOTHROW
  132. {
  133. return deque_block_traits<value_type, BlockBytes, BlockSize, StoredSizeType>::value;
  134. }
  135. BOOST_CONSTEXPR inline static difference_type get_block_ssize() BOOST_NOEXCEPT_OR_NOTHROW
  136. { return difference_type((get_block_size())); }
  137. typedef Pointer val_alloc_ptr;
  138. typedef typename boost::intrusive::pointer_traits<Pointer>::
  139. template rebind_pointer<Pointer>::type index_pointer;
  140. class nat
  141. {
  142. public:
  143. inline Pointer get_cur() const { return Pointer(); }
  144. inline index_pointer get_node() const { return index_pointer(); }
  145. };
  146. typedef typename dtl::if_c< IsConst
  147. , deque_iterator<Pointer, false, BlockBytes, BlockSize, StoredSizeType>
  148. , nat>::type nonconst_iterator_arg;
  149. typedef deque_iterator<Pointer, false, BlockBytes, BlockSize, StoredSizeType> nonconst_iterator;
  150. Pointer m_cur;
  151. index_pointer m_node;
  152. public:
  153. BOOST_CONTAINER_ATTRIBUTE_NODISCARD inline Pointer get_cur() const { return m_cur; }
  154. BOOST_CONTAINER_ATTRIBUTE_NODISCARD inline index_pointer get_node() const { return m_node; }
  155. BOOST_CONTAINER_ATTRIBUTE_NODISCARD inline Pointer get_first() const { return *m_node; }
  156. BOOST_CONTAINER_ATTRIBUTE_NODISCARD inline Pointer get_last() const { return *m_node + get_block_ssize(); }
  157. inline deque_iterator(val_alloc_ptr x, index_pointer y) BOOST_NOEXCEPT_OR_NOTHROW
  158. : m_cur(x), m_node(y)
  159. {}
  160. inline deque_iterator() BOOST_NOEXCEPT_OR_NOTHROW
  161. : m_cur(), m_node() //Value initialization to achieve "null iterators" (N3644)
  162. {}
  163. inline deque_iterator(const deque_iterator& x) BOOST_NOEXCEPT_OR_NOTHROW
  164. : m_cur(x.get_cur()), m_node(x.get_node())
  165. {}
  166. inline deque_iterator(const nonconst_iterator_arg& x) BOOST_NOEXCEPT_OR_NOTHROW
  167. : m_cur(x.get_cur()), m_node(x.get_node())
  168. {}
  169. inline deque_iterator& operator=(const deque_iterator& x) BOOST_NOEXCEPT_OR_NOTHROW
  170. { m_cur = x.get_cur(); m_node = x.get_node(); return *this; }
  171. inline deque_iterator& operator=(const nonconst_iterator_arg& x) BOOST_NOEXCEPT_OR_NOTHROW
  172. { m_cur = x.get_cur(); m_node = x.get_node(); return *this; }
  173. inline nonconst_iterator unconst() const BOOST_NOEXCEPT_OR_NOTHROW
  174. {
  175. return nonconst_iterator(this->get_cur(), this->get_node());
  176. }
  177. inline reference operator*() const BOOST_NOEXCEPT_OR_NOTHROW
  178. { return *this->m_cur; }
  179. inline pointer operator->() const BOOST_NOEXCEPT_OR_NOTHROW
  180. { return this->m_cur; }
  181. BOOST_CONTAINER_ATTRIBUTE_NODISCARD difference_type operator-(const deque_iterator& x) const BOOST_NOEXCEPT_OR_NOTHROW
  182. {
  183. if(this->m_cur == x.m_cur){ //Includes when both are null
  184. return 0;
  185. }
  186. BOOST_CONSTEXPR_OR_CONST difference_type block_size = get_block_ssize();
  187. return block_size * (this->m_node - x.m_node) +
  188. (this->m_cur - this->get_first()) - (x.m_cur - x.get_first());
  189. }
  190. deque_iterator& operator++() BOOST_NOEXCEPT_OR_NOTHROW
  191. {
  192. BOOST_ASSERT(!!m_cur);
  193. ++this->m_cur;
  194. const Pointer last = *m_node + get_block_ssize();
  195. if (BOOST_UNLIKELY(this->m_cur == last)) {
  196. ++this->m_node;
  197. this->m_cur = *this->m_node;
  198. }
  199. return *this;
  200. }
  201. inline deque_iterator operator++(int) BOOST_NOEXCEPT_OR_NOTHROW
  202. {
  203. deque_iterator tmp(*this);
  204. ++*this;
  205. return tmp;
  206. }
  207. //Some GCC versions issue bogus warnings about array bounds here
  208. #if defined(BOOST_GCC) && (BOOST_GCC >= 40600)
  209. # pragma GCC diagnostic ignored "-Warray-bounds"
  210. #endif
  211. deque_iterator& operator--() BOOST_NOEXCEPT_OR_NOTHROW
  212. {
  213. BOOST_ASSERT(!!m_cur);
  214. const Pointer first = *m_node;
  215. if (BOOST_UNLIKELY(this->m_cur == first)) {
  216. --this->m_node;
  217. this->m_cur = *this->m_node;
  218. this->m_cur += get_block_ssize() - 1;
  219. }
  220. else {
  221. --this->m_cur;
  222. }
  223. return *this;
  224. }
  225. #if defined(BOOST_GCC) && (BOOST_GCC >= 40600)
  226. #pragma GCC diagnostic pop
  227. #endif
  228. inline deque_iterator operator--(int) BOOST_NOEXCEPT_OR_NOTHROW
  229. {
  230. deque_iterator tmp(*this);
  231. --*this;
  232. return tmp;
  233. }
  234. deque_iterator& operator+=(difference_type n) BOOST_NOEXCEPT_OR_NOTHROW
  235. {
  236. if (n){
  237. BOOST_CONSTEXPR_OR_CONST difference_type block_size = get_block_ssize();
  238. const difference_type offset = n + (this->m_cur - *this->m_node);
  239. if (offset >= 0 && offset < block_size)
  240. this->m_cur += difference_type(n);
  241. else {
  242. const difference_type node_offset =
  243. offset > 0 ? (offset / block_size)
  244. : (-difference_type((-offset - 1) / block_size) - 1);
  245. this->m_node += node_offset;
  246. this->m_cur = *this->m_node + difference_type(offset - node_offset * block_size);
  247. }
  248. }
  249. return *this;
  250. }
  251. BOOST_CONTAINER_ATTRIBUTE_NODISCARD inline
  252. deque_iterator operator+(difference_type n) const BOOST_NOEXCEPT_OR_NOTHROW
  253. { deque_iterator tmp(*this); return tmp += n; }
  254. inline
  255. deque_iterator& operator-=(difference_type n) BOOST_NOEXCEPT_OR_NOTHROW
  256. { return *this += -n; }
  257. BOOST_CONTAINER_ATTRIBUTE_NODISCARD inline
  258. deque_iterator operator-(difference_type n) const BOOST_NOEXCEPT_OR_NOTHROW
  259. { deque_iterator tmp(*this); return tmp -= n; }
  260. BOOST_CONTAINER_ATTRIBUTE_NODISCARD inline
  261. reference operator[](difference_type n) const BOOST_NOEXCEPT_OR_NOTHROW
  262. {
  263. BOOST_ASSERT(!!m_cur);
  264. BOOST_CONSTEXPR_OR_CONST difference_type block_size = get_block_ssize();
  265. const difference_type offset = n + (this->m_cur - *this->m_node);
  266. if (offset >= 0 && offset < block_size)
  267. return this->m_cur[difference_type(n)];
  268. else {
  269. const difference_type node_offset = offset > 0
  270. ? (offset / block_size)
  271. : (-difference_type((-offset - 1) / block_size) - 1);
  272. return (this->m_node[node_offset]) [offset - node_offset * block_size];
  273. }
  274. }
  275. //Comparisons
  276. BOOST_CONTAINER_ATTRIBUTE_NODISCARD inline
  277. friend bool operator==(const deque_iterator& l, const deque_iterator& r) BOOST_NOEXCEPT_OR_NOTHROW
  278. { return l.m_cur == r.m_cur; }
  279. BOOST_CONTAINER_ATTRIBUTE_NODISCARD inline
  280. friend bool operator!=(const deque_iterator& l, const deque_iterator& r) BOOST_NOEXCEPT_OR_NOTHROW
  281. { return l.m_cur != r.m_cur; }
  282. BOOST_CONTAINER_ATTRIBUTE_NODISCARD inline
  283. friend bool operator<(const deque_iterator& l, const deque_iterator& r) BOOST_NOEXCEPT_OR_NOTHROW
  284. { return (l.m_node == r.m_node) ? (l.m_cur < r.m_cur) : (l.m_node < r.m_node); }
  285. BOOST_CONTAINER_ATTRIBUTE_NODISCARD inline
  286. friend bool operator>(const deque_iterator& l, const deque_iterator& r) BOOST_NOEXCEPT_OR_NOTHROW
  287. { return r < l; }
  288. BOOST_CONTAINER_ATTRIBUTE_NODISCARD inline
  289. friend bool operator<=(const deque_iterator& l, const deque_iterator& r) BOOST_NOEXCEPT_OR_NOTHROW
  290. { return !(r < l); }
  291. BOOST_CONTAINER_ATTRIBUTE_NODISCARD inline
  292. friend bool operator>=(const deque_iterator& l, const deque_iterator& r) BOOST_NOEXCEPT_OR_NOTHROW
  293. { return !(l < r); }
  294. BOOST_CONTAINER_ATTRIBUTE_NODISCARD inline
  295. friend deque_iterator operator+(difference_type n, deque_iterator x) BOOST_NOEXCEPT_OR_NOTHROW
  296. { return x += n; }
  297. inline void priv_set_node(index_pointer new_node) BOOST_NOEXCEPT_OR_NOTHROW
  298. { this->m_node = new_node; }
  299. };
  300. template<class Options, class AllocatorSizeType>
  301. struct get_deque_opt
  302. {
  303. typedef deque_opt< Options::block_bytes, Options::block_size
  304. , typename default_if_void<typename Options::stored_size_type, AllocatorSizeType>::type
  305. , Options::reservable> type;
  306. };
  307. template<class AllocatorSizeType>
  308. struct get_deque_opt<void, AllocatorSizeType>
  309. {
  310. typedef deque_opt<deque_null_opt::block_bytes, deque_null_opt::block_size, AllocatorSizeType, deque_null_opt::reservable> type;
  311. };
  312. // Deque base class. It has two purposes. First, its constructor
  313. // and destructor allocate (but don't initialize) storage. This makes
  314. // exception safety easier.
  315. template <class Allocator, class Options>
  316. class deque_base
  317. {
  318. BOOST_COPYABLE_AND_MOVABLE(deque_base)
  319. public:
  320. typedef allocator_traits<Allocator> val_alloc_traits_type;
  321. typedef typename val_alloc_traits_type::value_type val_alloc_val;
  322. typedef typename val_alloc_traits_type::pointer val_alloc_ptr;
  323. typedef typename val_alloc_traits_type::const_pointer val_alloc_cptr;
  324. typedef typename val_alloc_traits_type::reference val_alloc_ref;
  325. typedef typename val_alloc_traits_type::const_reference val_alloc_cref;
  326. typedef typename val_alloc_traits_type::difference_type val_alloc_diff;
  327. typedef typename val_alloc_traits_type::size_type val_alloc_size;
  328. typedef typename val_alloc_traits_type::template
  329. portable_rebind_alloc<val_alloc_ptr>::type ptr_alloc_t;
  330. typedef allocator_traits<ptr_alloc_t> ptr_alloc_traits_type;
  331. typedef typename ptr_alloc_traits_type::value_type ptr_alloc_val;
  332. typedef typename ptr_alloc_traits_type::pointer ptr_alloc_ptr;
  333. typedef typename ptr_alloc_traits_type::const_pointer ptr_alloc_cptr;
  334. typedef typename ptr_alloc_traits_type::reference ptr_alloc_ref;
  335. typedef typename ptr_alloc_traits_type::const_reference ptr_alloc_cref;
  336. typedef Allocator allocator_type;
  337. typedef allocator_type stored_allocator_type;
  338. typedef val_alloc_size size_type;
  339. typedef val_alloc_diff difference_type;
  340. private:
  341. typedef typename get_deque_opt<Options, val_alloc_size>::type options_type;
  342. protected:
  343. typedef typename options_type::stored_size_type stored_size_type;
  344. typedef deque_iterator< val_alloc_ptr, false
  345. , options_type::block_bytes
  346. , options_type::block_size
  347. , stored_size_type> iterator;
  348. typedef deque_iterator< val_alloc_ptr, true
  349. , options_type::block_bytes
  350. , options_type::block_size
  351. , stored_size_type> const_iterator;
  352. static const bool is_reservable = options_type::reservable;
  353. BOOST_CONSTEXPR inline static val_alloc_diff get_block_ssize() BOOST_NOEXCEPT_OR_NOTHROW
  354. { return val_alloc_diff((get_block_size())); }
  355. BOOST_CONSTEXPR inline static size_type get_block_size() BOOST_NOEXCEPT_OR_NOTHROW
  356. { return deque_block_traits<val_alloc_val, options_type::block_bytes, options_type::block_size, stored_size_type>::value; }
  357. typedef deque_value_traits<val_alloc_val> traits_t;
  358. typedef ptr_alloc_t map_allocator_type;
  359. inline val_alloc_ptr prot_allocate_node()
  360. {
  361. return this->alloc().allocate(get_block_size());
  362. }
  363. inline void prot_deallocate_node(val_alloc_ptr p) BOOST_NOEXCEPT_OR_NOTHROW
  364. {
  365. this->alloc().deallocate(p, get_block_size());
  366. }
  367. inline ptr_alloc_ptr prot_allocate_map(size_type n)
  368. {
  369. return this->ptr_alloc().allocate(n);
  370. }
  371. inline void prot_deallocate_map(ptr_alloc_ptr p, size_type n) BOOST_NOEXCEPT_OR_NOTHROW
  372. {
  373. this->ptr_alloc().deallocate(p, n);
  374. }
  375. inline deque_base(size_type num_elements, const allocator_type& a)
  376. : members_(a)
  377. { this->prot_initialize_map_and_nodes(num_elements); }
  378. inline explicit deque_base(const allocator_type& a)
  379. : members_(a)
  380. {}
  381. inline deque_base()
  382. : members_()
  383. {}
  384. inline explicit deque_base(BOOST_RV_REF(deque_base) x)
  385. : members_( boost::move(x.ptr_alloc())
  386. , boost::move(x.alloc()) )
  387. {}
  388. ~deque_base()
  389. {
  390. if (this->members_.m_map) {
  391. this->prot_deallocate_all_nodes();
  392. this->prot_deallocate_map(this->members_.m_map, this->members_.m_map_size);
  393. }
  394. }
  395. private:
  396. deque_base(const deque_base&);
  397. protected:
  398. void swap_members(deque_base &x) BOOST_NOEXCEPT_OR_NOTHROW
  399. {
  400. this->members_.swap(x.members_);
  401. }
  402. void test_size_against_max(size_type n)
  403. {
  404. const size_type max_alloc = val_alloc_traits_type::max_size(this->alloc());
  405. const size_type max = max_alloc <= stored_size_type(-1) ? max_alloc : stored_size_type(-1);
  406. if (BOOST_UNLIKELY(max < n) )
  407. boost::container::throw_length_error("get_next_capacity, allocator's max size reached");
  408. }
  409. void test_size_against_n_nodes(size_type n_nodes)
  410. {
  411. test_size_against_max(size_type(size_type(n_nodes * get_block_size()) - 1u));
  412. }
  413. void prot_initialize_map_and_nodes(size_type num_elements)
  414. {
  415. BOOST_CONSTEXPR_OR_CONST size_type block_size = get_block_size();
  416. //Even a zero element initialized map+nodes needs at least 1 node (for sentinel finish position)
  417. size_type num_nodes = size_type(num_elements / block_size + 1u);
  418. //Allocate at least one extra slot on each end to avoid inmediate map reallocation on push/front insertions
  419. const size_type map_size = dtl::max_value(size_type(InitialMapSize), size_type(num_nodes + 2u));
  420. const size_type start_map_pos = size_type(map_size - num_nodes)/2u;
  421. //The end position must be representable in stored_size_type
  422. this->test_size_against_n_nodes(map_size);
  423. this->members_.m_map = this->prot_allocate_map(map_size);
  424. this->members_.m_map_size = static_cast<stored_size_type>(map_size);
  425. BOOST_CONTAINER_TRY {
  426. BOOST_IF_CONSTEXPR(is_reservable){ //reservable implies all slots are allocated with nodes
  427. this->prot_allocate_nodes(this->members_.m_map, map_size);
  428. }
  429. else{ //Otherwise only create the nodes that will hold values
  430. this->prot_allocate_nodes(this->members_.m_map + difference_type(start_map_pos), num_nodes);
  431. }
  432. }
  433. BOOST_CONTAINER_CATCH(...){
  434. this->prot_deallocate_map(this->members_.m_map, this->members_.m_map_size);
  435. this->members_.m_map = ptr_alloc_ptr();
  436. this->members_.m_map_size = 0;
  437. BOOST_CONTAINER_RETHROW
  438. }
  439. BOOST_CONTAINER_CATCH_END
  440. this->prot_set_start_finish_from_node(start_map_pos, num_elements);
  441. }
  442. static size_type priv_new_offset(bool add_at_front, size_type map_size, size_type active_nodes, size_type additional_nodes)
  443. {
  444. return size_type(size_type(map_size - active_nodes) / 2 + (add_at_front ? +additional_nodes : +0));
  445. }
  446. void prot_reallocate_map_and_nodes //is_reservable == true
  447. (dtl::bool_<true>, const size_type new_elems, const bool add_at_front)
  448. {
  449. const ptr_alloc_ptr start_node = this->prot_start_node();
  450. const size_type old_active_nodes = size_type(this->prot_finish_node() + 1u - start_node);
  451. const size_type additional_nodes = size_type((new_elems - 1u)/get_block_size() + 1u);
  452. const size_type new_active_nodes = size_type(old_active_nodes + additional_nodes);
  453. ptr_alloc_ptr new_nstart;
  454. const size_type old_map_size = this->members_.m_map_size;
  455. const ptr_alloc_ptr old_map = this->members_.m_map;
  456. //Check for 1.5 factor
  457. if (old_map_size/3u >= new_active_nodes/2u) {
  458. new_nstart = old_map + difference_type(priv_new_offset(add_at_front, old_map_size, new_active_nodes, additional_nodes));
  459. const ptr_alloc_ptr end_node = start_node + difference_type(old_active_nodes);
  460. if (new_nstart < start_node)
  461. boost::movelib::rotate_gcd(new_nstart, start_node, end_node);
  462. else
  463. boost::movelib::rotate_gcd(start_node, end_node, new_nstart + old_active_nodes);
  464. }
  465. else {
  466. //1.5 increase, but at least one spare slot on each end
  467. const size_type new_map_size = dtl::max_value(size_type(old_map_size + old_map_size/2u), size_type(new_active_nodes + 2u));
  468. //The end position must be representable in stored_size_type
  469. this->test_size_against_n_nodes(new_map_size);
  470. const ptr_alloc_ptr new_map = this->prot_allocate_map(new_map_size);
  471. const size_type new_active_off = priv_new_offset(add_at_front, new_map_size, new_active_nodes, additional_nodes);
  472. new_nstart = new_map + difference_type(new_active_off);
  473. const size_type new_nodes = size_type(new_map_size - old_map_size);
  474. BOOST_CONTAINER_TRY {
  475. this->prot_allocate_nodes(new_map, new_nodes);
  476. }
  477. BOOST_CONTAINER_CATCH(...){
  478. this->prot_deallocate_map(new_map, new_map_size);
  479. BOOST_CONTAINER_RETHROW
  480. }
  481. BOOST_CONTAINER_CATCH_END
  482. const ptr_alloc_ptr new_old_node_limit = new_map + difference_type(new_nodes);
  483. //Move old nodes from the old map, rotate to the right position and deallocate the old map
  484. boost::container::move_n(old_map, old_map_size, new_old_node_limit);
  485. const size_type old_active_off = size_type(start_node - old_map);
  486. const ptr_alloc_ptr active_start = new_old_node_limit + difference_type(old_active_off);
  487. const ptr_alloc_ptr active_end = active_start + difference_type(old_active_nodes);
  488. if (new_nstart < active_start)
  489. boost::movelib::rotate_gcd(new_nstart, active_start, active_end);
  490. else
  491. boost::movelib::rotate_gcd(active_start, active_end, new_nstart + difference_type(old_active_nodes));
  492. this->prot_deallocate_map(old_map, old_map_size);
  493. this->members_.m_map = new_map;
  494. this->members_.m_map_size = static_cast<stored_size_type>(new_map_size);
  495. }
  496. this->prot_start_update_node(new_nstart);
  497. this->prot_finish_update_node(new_nstart + difference_type(old_active_nodes - 1u));
  498. }
  499. void prot_reallocate_map_and_nodes //is_reservable == false
  500. (dtl::bool_<false>, const size_type new_elems, const bool add_at_front)
  501. {
  502. const size_type additional_nodes = size_type((new_elems - 1u)/get_block_size() + 1u);
  503. const ptr_alloc_ptr start_node = this->prot_start_node();
  504. const ptr_alloc_ptr finish_node = this->prot_finish_node();
  505. size_type unused_slots = add_at_front ? size_type(start_node - this->members_.m_map)
  506. : size_type(this->members_.m_map_size - 1u - size_type(finish_node - this->members_.m_map));
  507. if (additional_nodes > unused_slots){
  508. const ptr_alloc_ptr next_finish_node = finish_node + 1u;
  509. const size_type old_active_nodes = size_type(next_finish_node - start_node);
  510. const size_type new_active_nodes = size_type(old_active_nodes + additional_nodes);
  511. ptr_alloc_ptr new_nstart;
  512. const size_type old_map_size = this->members_.m_map_size;
  513. const ptr_alloc_ptr old_map = this->members_.m_map;
  514. if (old_map_size/2u >= new_active_nodes) {
  515. new_nstart = old_map + difference_type(priv_new_offset(add_at_front, old_map_size, new_active_nodes, additional_nodes));
  516. if (new_nstart < start_node)
  517. boost::container::move_n(start_node, old_active_nodes, new_nstart);
  518. else
  519. boost::container::move_backward_n(next_finish_node, old_active_nodes, new_nstart + difference_type(old_active_nodes));
  520. }
  521. else {
  522. //Doubling size, but at least one spare slot on each end
  523. const size_type new_map_size = dtl::max_value(size_type(old_map_size*2), size_type(new_active_nodes + 2u));
  524. //The end position must be representable in stored_size_type
  525. this->test_size_against_n_nodes(new_map_size);
  526. const ptr_alloc_ptr new_map = this->prot_allocate_map(new_map_size);
  527. new_nstart = new_map + difference_type(priv_new_offset(add_at_front, new_map_size, new_active_nodes, additional_nodes));
  528. boost::container::move_n(start_node, old_active_nodes, new_nstart);
  529. this->prot_deallocate_map(old_map, old_map_size);
  530. this->members_.m_map = new_map;
  531. this->members_.m_map_size = static_cast<stored_size_type>(new_map_size);
  532. }
  533. this->prot_start_update_node(new_nstart);
  534. this->prot_finish_update_node(new_nstart + difference_type(old_active_nodes - 1u));
  535. }
  536. if(add_at_front)
  537. this->prot_allocate_nodes(this->prot_start_node() - difference_type(additional_nodes), additional_nodes);
  538. else
  539. this->prot_allocate_nodes(this->prot_finish_node() + 1, additional_nodes);
  540. }
  541. void prot_allocate_nodes(ptr_alloc_ptr start, size_type n)
  542. {
  543. size_type i = 0;
  544. BOOST_CONTAINER_TRY {
  545. for (; i < n; ++i)
  546. start[difference_type(i)] = this->prot_allocate_node();
  547. }
  548. BOOST_CONTAINER_CATCH(...) {
  549. for (size_type j = 0; j < i; ++j)
  550. this->prot_deallocate_node(start[difference_type(j)]);
  551. BOOST_CONTAINER_RETHROW
  552. }
  553. BOOST_CONTAINER_CATCH_END
  554. }
  555. void prot_deallocate_nodes(ptr_alloc_ptr nstart, ptr_alloc_ptr nfinish) BOOST_NOEXCEPT_OR_NOTHROW
  556. {
  557. for (ptr_alloc_ptr n = nstart; n < nfinish; ++n){
  558. this->prot_deallocate_node(*n);
  559. }
  560. }
  561. void prot_deallocate_nodes_if_not_reservable(ptr_alloc_ptr nstart, ptr_alloc_ptr nfinish) BOOST_NOEXCEPT_OR_NOTHROW
  562. {
  563. (void)nstart; (void)nfinish;
  564. BOOST_IF_CONSTEXPR(!is_reservable)
  565. this->prot_deallocate_nodes(nstart, nfinish);
  566. }
  567. void prot_deallocate_node_if_not_reservable(val_alloc_ptr p) BOOST_NOEXCEPT_OR_NOTHROW
  568. {
  569. (void)p;
  570. BOOST_IF_CONSTEXPR(!is_reservable)
  571. this->prot_deallocate_node(p);
  572. }
  573. void prot_deallocate_all_nodes()
  574. {
  575. BOOST_IF_CONSTEXPR(is_reservable)
  576. this->prot_deallocate_nodes(this->members_.m_map, this->members_.m_map + difference_type(this->members_.m_map_size));
  577. else
  578. this->prot_deallocate_nodes(this->prot_start_node(), this->prot_finish_node() + 1);
  579. }
  580. void prot_clear_map() BOOST_NOEXCEPT_OR_NOTHROW
  581. {
  582. if (this->members_.m_map) {
  583. this->prot_deallocate_all_nodes();
  584. this->prot_deallocate_map(this->members_.m_map, this->members_.m_map_size);
  585. this->members_.m_map = ptr_alloc_ptr();
  586. this->members_.m_map_size = 0u;
  587. this->members_.m_start_off = 0u;
  588. this->members_.m_finish_off = 0u;
  589. }
  590. }
  591. enum { InitialMapSize = 4 };
  592. protected:
  593. struct members_holder
  594. : public ptr_alloc_t
  595. , public allocator_type
  596. {
  597. friend class deque_base;
  598. members_holder()
  599. : map_allocator_type(), allocator_type()
  600. , m_map(), m_map_size()
  601. , m_start_off(), m_finish_off()
  602. {}
  603. explicit members_holder(const allocator_type &a)
  604. : map_allocator_type(a), allocator_type(a)
  605. , m_map(), m_map_size()
  606. , m_start_off(), m_finish_off()
  607. {}
  608. template<class ValAllocConvertible, class PtrAllocConvertible>
  609. members_holder(BOOST_FWD_REF(PtrAllocConvertible) pa, BOOST_FWD_REF(ValAllocConvertible) va)
  610. : map_allocator_type(boost::forward<PtrAllocConvertible>(pa))
  611. , allocator_type (boost::forward<ValAllocConvertible>(va))
  612. , m_map(), m_map_size()
  613. , m_start_off(), m_finish_off()
  614. {}
  615. void swap(members_holder &x) BOOST_NOEXCEPT_OR_NOTHROW
  616. {
  617. ::boost::adl_move_swap(this->m_map, x.m_map);
  618. ::boost::adl_move_swap(this->m_map_size, x.m_map_size);
  619. ::boost::adl_move_swap(this->m_start_off, x.m_start_off);
  620. ::boost::adl_move_swap(this->m_finish_off, x.m_finish_off);
  621. }
  622. ptr_alloc_ptr m_map;
  623. stored_size_type m_map_size;
  624. private:
  625. stored_size_type m_start_off;
  626. stored_size_type m_finish_off;
  627. } members_;
  628. inline ptr_alloc_t &ptr_alloc() BOOST_NOEXCEPT_OR_NOTHROW
  629. { return members_; }
  630. inline const ptr_alloc_t &ptr_alloc() const BOOST_NOEXCEPT_OR_NOTHROW
  631. { return members_; }
  632. inline allocator_type &alloc() BOOST_NOEXCEPT_OR_NOTHROW
  633. { return members_; }
  634. inline const allocator_type &alloc() const BOOST_NOEXCEPT_OR_NOTHROW
  635. { return members_; }
  636. static BOOST_CONTAINER_FORCEINLINE val_alloc_ptr prot_node_last(ptr_alloc_ptr idx)
  637. {
  638. BOOST_CONSTEXPR_OR_CONST std::size_t block_size = deque_base::get_block_size();
  639. return *idx + difference_type(block_size - 1u);
  640. }
  641. BOOST_CONTAINER_FORCEINLINE size_type prot_front_free_capacity() const
  642. {
  643. BOOST_IF_CONSTEXPR(is_reservable){
  644. return static_cast<size_type>(this->members_.m_start_off);
  645. }
  646. else{
  647. BOOST_CONSTEXPR_OR_CONST std::size_t block_size = deque_base::get_block_size();
  648. return static_cast<size_type>(this->members_.m_start_off % block_size);
  649. }
  650. }
  651. BOOST_CONTAINER_FORCEINLINE size_type prot_back_free_capacity() const
  652. {
  653. BOOST_CONSTEXPR_OR_CONST std::size_t block_size = deque_base::get_block_size();
  654. BOOST_IF_CONSTEXPR(is_reservable){
  655. return static_cast<size_type>
  656. (size_type(this->members_.m_map_size*block_size) - size_type(this->members_.m_map != ptr_alloc_ptr()) - this->members_.m_finish_off);
  657. }
  658. else {
  659. //m_finish_off points to positions [0....block_size-1], and one position is always needed as the sentinel node resulting [block_size-1....0] capacity
  660. return static_cast<size_type>(this->members_.m_map ? (block_size - 1u) - (this->members_.m_finish_off % block_size) : 0u);
  661. }
  662. }
  663. //////////////////////////
  664. // it_to_off / off_to_it
  665. //////////////////////////
  666. BOOST_CONTAINER_FORCEINLINE stored_size_type prot_it_to_off(const_iterator it) const
  667. {
  668. const ptr_alloc_ptr n = it.get_node();
  669. BOOST_ASSERT(!this->members_.m_map == !n); //Both should be null or both non-null
  670. if (n) {
  671. BOOST_CONSTEXPR_OR_CONST std::size_t block_size = deque_base::get_block_size();
  672. return static_cast<stored_size_type>(std::size_t(n - this->members_.m_map)*block_size + std::size_t(it.get_cur() - *n));
  673. }
  674. else{
  675. return 0;
  676. }
  677. }
  678. BOOST_CONTAINER_FORCEINLINE iterator prot_off_to_it(std::size_t off) const
  679. {
  680. BOOST_CONSTEXPR_OR_CONST std::size_t block_size = deque_base::get_block_size();
  681. const ptr_alloc_ptr node = this->members_.m_map + difference_type(off/block_size);
  682. return iterator(node ? *node + difference_type(off%block_size) : val_alloc_ptr(), node);
  683. }
  684. stored_size_type prot_it_to_start_off(const_iterator it) const
  685. {
  686. const size_type off = this->prot_it_to_off(it);
  687. BOOST_ASSERT(off >= this->members_.m_start_off);
  688. return static_cast<stored_size_type>(off - this->members_.m_start_off);
  689. }
  690. /////////////
  691. // xxx_to_node
  692. /////////////
  693. BOOST_CONTAINER_FORCEINLINE ptr_alloc_ptr prot_off_to_node(std::size_t off) const
  694. {
  695. BOOST_CONSTEXPR_OR_CONST std::size_t block_size = deque_base::get_block_size();
  696. return this->members_.m_map + difference_type(off/block_size);
  697. }
  698. BOOST_CONTAINER_FORCEINLINE ptr_alloc_ptr prot_start_node() const
  699. {
  700. return this->prot_off_to_node(this->members_.m_start_off);
  701. }
  702. BOOST_CONTAINER_FORCEINLINE ptr_alloc_ptr prot_finish_node() const
  703. {
  704. return this->prot_off_to_node(this->members_.m_finish_off);
  705. }
  706. //
  707. // xxx_to_cur_unchecked versions, faster but need non-default constructed deque
  708. //
  709. BOOST_CONTAINER_FORCEINLINE val_alloc_ptr prot_off_to_cur_unchecked(std::size_t off) const
  710. {
  711. BOOST_ASSERT(!!this->members_.m_map);
  712. BOOST_CONSTEXPR_OR_CONST std::size_t block_size = deque_base::get_block_size();
  713. const ptr_alloc_ptr node = this->members_.m_map + difference_type(off/block_size);
  714. return *node + difference_type(off%block_size);
  715. }
  716. BOOST_CONTAINER_FORCEINLINE val_alloc_ptr prot_start_cur_unchecked() const
  717. {
  718. return this->prot_off_to_cur_unchecked(this->members_.m_start_off);
  719. }
  720. BOOST_CONTAINER_FORCEINLINE val_alloc_ptr prot_finish_cur_unchecked() const
  721. {
  722. return this->prot_off_to_cur_unchecked(this->members_.m_finish_off);
  723. }
  724. BOOST_CONTAINER_FORCEINLINE val_alloc_ptr prot_last_cur_unchecked() const
  725. {
  726. BOOST_ASSERT(members_.m_start_off != members_.m_finish_off);
  727. return this->prot_off_to_cur_unchecked(this->members_.m_finish_off-1u);
  728. }
  729. //
  730. // functions returning iterators to different positions
  731. //
  732. BOOST_CONTAINER_FORCEINLINE const_iterator prot_start() const
  733. { return this->prot_off_to_it(members_.m_start_off); }
  734. BOOST_CONTAINER_FORCEINLINE iterator prot_start()
  735. { return this->prot_off_to_it(members_.m_start_off); }
  736. BOOST_CONTAINER_FORCEINLINE const_iterator prot_finish() const
  737. { return this->prot_off_to_it(members_.m_finish_off); }
  738. BOOST_CONTAINER_FORCEINLINE iterator prot_finish()
  739. { return this->prot_off_to_it(members_.m_finish_off); }
  740. BOOST_CONTAINER_FORCEINLINE const_iterator prot_nth(size_type n) const
  741. { return this->prot_off_to_it(size_type(members_.m_start_off + n)); }
  742. BOOST_CONTAINER_FORCEINLINE iterator prot_nth(size_type n)
  743. { return this->prot_off_to_it(size_type(members_.m_start_off + n)); }
  744. BOOST_CONTAINER_FORCEINLINE iterator prot_back_it()
  745. {
  746. BOOST_ASSERT(members_.m_start_off != members_.m_finish_off);
  747. return this->prot_off_to_it(size_type(members_.m_finish_off - 1u));
  748. }
  749. //
  750. // size/empty
  751. //
  752. BOOST_CONTAINER_FORCEINLINE size_type prot_size() const
  753. { return size_type(this->members_.m_finish_off - this->members_.m_start_off); }
  754. BOOST_CONTAINER_FORCEINLINE bool prot_empty() const
  755. { return this->members_.m_finish_off == this->members_.m_start_off; }
  756. //
  757. // Functions to move start/finish indexes
  758. //
  759. BOOST_CONTAINER_FORCEINLINE void prot_inc_start()
  760. { ++this->members_.m_start_off; }
  761. BOOST_CONTAINER_FORCEINLINE void prot_dec_start()
  762. { --this->members_.m_start_off; }
  763. BOOST_CONTAINER_FORCEINLINE void prot_inc_finish()
  764. { ++this->members_.m_finish_off; }
  765. BOOST_CONTAINER_FORCEINLINE void prot_dec_finish()
  766. { --this->members_.m_finish_off; }
  767. BOOST_CONTAINER_FORCEINLINE void prot_dec_finish(std::size_t n)
  768. { this->members_.m_finish_off = static_cast<stored_size_type>(this->members_.m_finish_off - n); }
  769. BOOST_CONTAINER_FORCEINLINE void prot_inc_finish(std::size_t n)
  770. { this->members_.m_finish_off = static_cast<stored_size_type>(this->members_.m_finish_off + n); }
  771. BOOST_CONTAINER_FORCEINLINE void prot_dec_start(std::size_t n)
  772. { this->members_.m_start_off = static_cast<stored_size_type>(this->members_.m_start_off - n); }
  773. BOOST_CONTAINER_FORCEINLINE void prot_inc_start(std::size_t n)
  774. { this->members_.m_start_off = static_cast<stored_size_type>(this->members_.m_start_off + n); }
  775. //
  776. // Functions to obtain indexes from nodes
  777. //
  778. BOOST_CONTAINER_FORCEINLINE stored_size_type prot_non_null_node_to_off(ptr_alloc_ptr n) const
  779. {
  780. BOOST_CONSTEXPR_OR_CONST std::size_t block_size = deque_base::get_block_size();
  781. return static_cast<stored_size_type>(std::size_t(n - this->members_.m_map)*block_size);
  782. }
  783. void prot_set_start_finish_from_node(size_type node_idx, size_type n_elements)
  784. {
  785. BOOST_CONSTEXPR_OR_CONST std::size_t block_size = deque_base::get_block_size();
  786. this->members_.m_start_off = static_cast<stored_size_type>(node_idx*block_size);
  787. this->members_.m_finish_off = static_cast<stored_size_type>(this->members_.m_start_off + n_elements);
  788. }
  789. inline void prot_start_update_node(ptr_alloc_ptr new_start)
  790. {
  791. //iG: to-do: optimizable avoiding some division/remainder
  792. std::size_t new_block_off = prot_non_null_node_to_off(new_start);
  793. this->members_.m_start_off = static_cast<stored_size_type>(new_block_off + (this->members_.m_start_off % get_block_size()));
  794. }
  795. inline void prot_finish_update_node(ptr_alloc_ptr new_finish)
  796. {
  797. //iG: to-do: optimizable avoiding some division/remainder
  798. std::size_t new_block_off = prot_non_null_node_to_off(new_finish);
  799. this->members_.m_finish_off = static_cast<stored_size_type>(new_block_off + (this->members_.m_finish_off % get_block_size()));
  800. }
  801. inline void prot_reset_finish_to_start()
  802. { this->members_.m_finish_off = this->members_.m_start_off; }
  803. inline void prot_reset_start_to_finish()
  804. { this->members_.m_start_off = this->members_.m_finish_off; }
  805. inline val_alloc_val *prot_push_back_simple_pos() const
  806. {
  807. BOOST_CONSTEXPR_OR_CONST std::size_t block_size = deque_base::get_block_size();
  808. const std::size_t last_in_block = block_size - 1u;
  809. const ptr_alloc_val *const map = boost::movelib::to_raw_pointer(this->members_.m_map);
  810. if(BOOST_LIKELY(map != 0)) {
  811. const std::size_t off = this->members_.m_finish_off;
  812. const std::size_t rem = off % block_size;
  813. if(BOOST_LIKELY(rem != last_in_block)){
  814. return boost::movelib::to_raw_pointer(map[difference_type(off/block_size)]) + difference_type(rem);
  815. }
  816. }
  817. return 0;
  818. }
  819. inline val_alloc_val *prot_push_front_simple_pos() const
  820. {
  821. BOOST_CONSTEXPR_OR_CONST std::size_t block_size = deque_base::get_block_size();
  822. //No need to check !m_map, as m_start_off is zero in that case
  823. const std::size_t off = this->members_.m_start_off;
  824. const std::size_t rem = off % block_size;
  825. if(BOOST_LIKELY(rem != 0u)){
  826. return boost::movelib::to_raw_pointer(this->members_.m_map[difference_type(off/block_size)]) + difference_type(rem-1u);
  827. }
  828. return 0;
  829. }
  830. BOOST_CONTAINER_FORCEINLINE bool prot_pop_back_simple_available() const
  831. {
  832. return (this->members_.m_finish_off % get_block_size()) != 0u;
  833. }
  834. BOOST_CONTAINER_FORCEINLINE bool prot_pop_front_simple_available() const
  835. {
  836. return size_type(this->members_.m_start_off % get_block_size()) != size_type(get_block_size() - 1u);
  837. }
  838. };
  839. #endif //#ifndef BOOST_CONTAINER_DOXYGEN_INVOKED
  840. #ifdef BOOST_CONTAINER_DOXYGEN_INVOKED
  841. //! A double-ended queue is a sequence that supports random access to elements, constant time insertion
  842. //! and removal of elements at the end of the sequence, and linear time insertion and removal of elements in the middle.
  843. //!
  844. //! \tparam T The type of object that is stored in the deque
  845. //! \tparam A The allocator used for all internal memory management, use void
  846. //! for the default allocator
  847. //! \tparam Options A type produced from \c boost::container::deque_options.
  848. template <class T, class Allocator = void, class Options = void>
  849. #else
  850. template <class T, class Allocator, class Options>
  851. #endif
  852. class deque : protected deque_base<typename real_allocator<T, Allocator>::type, Options>
  853. {
  854. #ifndef BOOST_CONTAINER_DOXYGEN_INVOKED
  855. private:
  856. typedef deque_base<typename real_allocator<T, Allocator>::type, Options> Base;
  857. #endif //#ifndef BOOST_CONTAINER_DOXYGEN_INVOKED
  858. typedef typename real_allocator<T, Allocator>::type ValAllocator;
  859. typedef constant_iterator<T> c_it;
  860. public:
  861. //////////////////////////////////////////////
  862. //
  863. // types
  864. //
  865. //////////////////////////////////////////////
  866. typedef T value_type;
  867. typedef ValAllocator allocator_type;
  868. typedef typename ::boost::container::allocator_traits<ValAllocator>::pointer pointer;
  869. typedef typename ::boost::container::allocator_traits<ValAllocator>::const_pointer const_pointer;
  870. typedef typename ::boost::container::allocator_traits<ValAllocator>::reference reference;
  871. typedef typename ::boost::container::allocator_traits<ValAllocator>::const_reference const_reference;
  872. typedef typename ::boost::container::allocator_traits<ValAllocator>::size_type size_type;
  873. typedef typename ::boost::container::allocator_traits<ValAllocator>::difference_type difference_type;
  874. typedef BOOST_CONTAINER_IMPDEF(allocator_type) stored_allocator_type;
  875. typedef BOOST_CONTAINER_IMPDEF(typename Base::iterator) iterator;
  876. typedef BOOST_CONTAINER_IMPDEF(typename Base::const_iterator) const_iterator;
  877. typedef BOOST_CONTAINER_IMPDEF(boost::container::reverse_iterator<iterator>) reverse_iterator;
  878. typedef BOOST_CONTAINER_IMPDEF(boost::container::reverse_iterator<const_iterator>) const_reverse_iterator;
  879. #ifndef BOOST_CONTAINER_DOXYGEN_INVOKED
  880. private: // Internal typedefs
  881. //`allocator_type::value_type` must match container's `value type`. If this
  882. //assertion fails, please review your allocator definition.
  883. BOOST_CONTAINER_STATIC_ASSERT((dtl::is_same<value_type, typename allocator_traits<ValAllocator>::value_type>::value));
  884. BOOST_COPYABLE_AND_MOVABLE(deque)
  885. typedef typename Base::ptr_alloc_ptr index_pointer;
  886. typedef allocator_traits<ValAllocator> allocator_traits_type;
  887. typedef typename Base::stored_size_type stored_size_type;
  888. #endif //#ifndef BOOST_CONTAINER_DOXYGEN_INVOKED
  889. using Base::get_block_ssize;
  890. public:
  891. using Base::get_block_size;
  892. static const std::size_t is_reservable = Base::is_reservable;
  893. //////////////////////////////////////////////
  894. //
  895. // construct/copy/destroy
  896. //
  897. //////////////////////////////////////////////
  898. //! <b>Effects</b>: Default constructors a deque.
  899. //!
  900. //! <b>Throws</b>: If allocator_type's default constructor throws.
  901. //!
  902. //! <b>Complexity</b>: Constant.
  903. inline deque()
  904. BOOST_NOEXCEPT_IF(dtl::is_nothrow_default_constructible<ValAllocator>::value)
  905. : Base()
  906. {}
  907. //! <b>Effects</b>: Constructs a deque taking the allocator as parameter.
  908. //!
  909. //! <b>Throws</b>: Nothing
  910. //!
  911. //! <b>Complexity</b>: Constant.
  912. inline explicit deque(const allocator_type& a) BOOST_NOEXCEPT_OR_NOTHROW
  913. : Base(a)
  914. {}
  915. //! <b>Effects</b>: Constructs a deque
  916. //! and inserts n value initialized values.
  917. //!
  918. //! <b>Throws</b>: If allocator_type's default constructor
  919. //! throws or T's value initialization throws.
  920. //!
  921. //! <b>Complexity</b>: Linear to n.
  922. inline explicit deque(size_type n)
  923. : Base(n, allocator_type())
  924. {
  925. dtl::insert_value_initialized_n_proxy<ValAllocator> proxy;
  926. this->priv_segmented_proxy_uninitialized_copy_n_and_update(this->begin(), n, proxy);
  927. //deque_base will deallocate in case of exception...
  928. }
  929. //! <b>Effects</b>: Constructs a deque
  930. //! and inserts n default initialized values.
  931. //!
  932. //! <b>Throws</b>: If allocator_type's default constructor
  933. //! throws or T's default initialization or copy constructor throws.
  934. //!
  935. //! <b>Complexity</b>: Linear to n.
  936. //!
  937. //! <b>Note</b>: Non-standard extension
  938. inline deque(size_type n, default_init_t)
  939. : Base(n, allocator_type())
  940. {
  941. dtl::insert_default_initialized_n_proxy<ValAllocator> proxy;
  942. this->priv_segmented_proxy_uninitialized_copy_n_and_update(this->begin(), n, proxy);
  943. //deque_base will deallocate in case of exception...
  944. }
  945. //! <b>Effects</b>: Constructs a deque that will use a copy of allocator a
  946. //! and inserts n value initialized values.
  947. //!
  948. //! <b>Throws</b>: If allocator_type's default constructor
  949. //! throws or T's value initialization throws.
  950. //!
  951. //! <b>Complexity</b>: Linear to n.
  952. inline explicit deque(size_type n, const allocator_type &a)
  953. : Base(n, a)
  954. {
  955. dtl::insert_value_initialized_n_proxy<ValAllocator> proxy;
  956. this->priv_segmented_proxy_uninitialized_copy_n_and_update(this->begin(), n, proxy);
  957. //deque_base will deallocate in case of exception...
  958. }
  959. //! <b>Effects</b>: Constructs a deque that will use a copy of allocator a
  960. //! and inserts n default initialized values.
  961. //!
  962. //! <b>Throws</b>: If allocator_type's default constructor
  963. //! throws or T's default initialization or copy constructor throws.
  964. //!
  965. //! <b>Complexity</b>: Linear to n.
  966. //!
  967. //! <b>Note</b>: Non-standard extension
  968. inline deque(size_type n, default_init_t, const allocator_type &a)
  969. : Base(n, a)
  970. {
  971. dtl::insert_default_initialized_n_proxy<ValAllocator> proxy;
  972. this->priv_segmented_proxy_uninitialized_copy_n_and_update(this->begin(), n, proxy);
  973. //deque_base will deallocate in case of exception...
  974. }
  975. //! <b>Effects</b>: Constructs a deque that will use a copy of allocator a
  976. //! and inserts n copies of value.
  977. //!
  978. //! <b>Throws</b>: If allocator_type's default constructor
  979. //! throws or T's copy constructor throws.
  980. //!
  981. //! <b>Complexity</b>: Linear to n.
  982. inline deque(size_type n, const value_type& value)
  983. : Base(n, allocator_type())
  984. { this->priv_fill_initialize(n, value); }
  985. //! <b>Effects</b>: Constructs a deque that will use a copy of allocator a
  986. //! and inserts n copies of value.
  987. //!
  988. //! <b>Throws</b>: If allocator_type's default constructor
  989. //! throws or T's copy constructor throws.
  990. //!
  991. //! <b>Complexity</b>: Linear to n.
  992. inline deque(size_type n, const value_type& value, const allocator_type& a)
  993. : Base(n, a)
  994. { this->priv_fill_initialize(n, value); }
  995. //! <b>Effects</b>: Constructs a deque that will use a copy of allocator a
  996. //! and inserts a copy of the range [first, last) in the deque.
  997. //!
  998. //! <b>Throws</b>: If allocator_type's default constructor
  999. //! throws or T's constructor taking a dereferenced InIt throws.
  1000. //!
  1001. //! <b>Complexity</b>: Linear to the range [first, last).
  1002. template <class InIt>
  1003. inline deque(InIt first, InIt last
  1004. #if !defined(BOOST_CONTAINER_DOXYGEN_INVOKED)
  1005. , typename dtl::disable_if_convertible
  1006. <InIt, size_type>::type * = 0
  1007. #endif
  1008. )
  1009. : Base(allocator_type())
  1010. {
  1011. this->priv_range_initialize(first, last);
  1012. }
  1013. //! <b>Effects</b>: Constructs a deque that will use a copy of allocator a
  1014. //! and inserts a copy of the range [first, last) in the deque.
  1015. //!
  1016. //! <b>Throws</b>: If allocator_type's default constructor
  1017. //! throws or T's constructor taking a dereferenced InIt throws.
  1018. //!
  1019. //! <b>Complexity</b>: Linear to the range [first, last).
  1020. template <class InIt>
  1021. inline deque(InIt first, InIt last, const allocator_type& a
  1022. #if !defined(BOOST_CONTAINER_DOXYGEN_INVOKED)
  1023. , typename dtl::disable_if_convertible
  1024. <InIt, size_type>::type * = 0
  1025. #endif
  1026. )
  1027. : Base(a)
  1028. {
  1029. this->priv_range_initialize(first, last);
  1030. }
  1031. #if !defined(BOOST_NO_CXX11_HDR_INITIALIZER_LIST)
  1032. //! <b>Effects</b>: Constructs a deque that will use a copy of allocator a
  1033. //! and inserts a copy of the range [il.begin(), il.end()) in the deque.
  1034. //!
  1035. //! <b>Throws</b>: If allocator_type's default constructor
  1036. //! throws or T's constructor taking a dereferenced std::initializer_list iterator throws.
  1037. //!
  1038. //! <b>Complexity</b>: Linear to the range [il.begin(), il.end()).
  1039. inline deque(std::initializer_list<value_type> il, const allocator_type& a = allocator_type())
  1040. : Base(a)
  1041. {
  1042. this->priv_range_initialize(il.begin(), il.end());
  1043. }
  1044. #endif
  1045. //! <b>Effects</b>: Copy constructs a deque.
  1046. //!
  1047. //! <b>Postcondition</b>: x == *this.
  1048. //!
  1049. //! <b>Complexity</b>: Linear to the elements x contains.
  1050. inline deque(const deque& x)
  1051. : Base(allocator_traits_type::select_on_container_copy_construction(x.alloc()))
  1052. {
  1053. if(x.size()){
  1054. this->prot_initialize_map_and_nodes(x.size());
  1055. this->priv_segmented_uninitialized_copy_alloc_n(x.begin(), x.size(), this->begin());
  1056. }
  1057. }
  1058. //! <b>Effects</b>: Move constructor. Moves x's resources to *this.
  1059. //!
  1060. //! <b>Throws</b>: If allocator_type's copy constructor throws.
  1061. //!
  1062. //! <b>Complexity</b>: Constant.
  1063. inline deque(BOOST_RV_REF(deque) x) BOOST_NOEXCEPT_OR_NOTHROW
  1064. : Base(BOOST_MOVE_BASE(Base, x))
  1065. { this->swap_members(x); }
  1066. //! <b>Effects</b>: Copy constructs a vector using the specified allocator.
  1067. //!
  1068. //! <b>Postcondition</b>: x == *this.
  1069. //!
  1070. //! <b>Throws</b>: If allocation
  1071. //! throws or T's copy constructor throws.
  1072. //!
  1073. //! <b>Complexity</b>: Linear to the elements x contains.
  1074. deque(const deque& x, const allocator_type &a)
  1075. : Base(a)
  1076. {
  1077. if(x.size()){
  1078. this->prot_initialize_map_and_nodes(x.size());
  1079. this->priv_segmented_uninitialized_copy_alloc_n(x.begin(), x.size(), this->begin());
  1080. }
  1081. }
  1082. //! <b>Effects</b>: Move constructor using the specified allocator.
  1083. //! Moves x's resources to *this if a == allocator_type().
  1084. //! Otherwise copies values from x to *this.
  1085. //!
  1086. //! <b>Throws</b>: If allocation or T's copy constructor throws.
  1087. //!
  1088. //! <b>Complexity</b>: Constant if a == x.get_allocator(), linear otherwise.
  1089. deque(BOOST_RV_REF(deque) x, const allocator_type &a)
  1090. : Base(a)
  1091. {
  1092. if(x.alloc() == a){
  1093. this->swap_members(x);
  1094. }
  1095. else{
  1096. if(x.size()){
  1097. this->prot_initialize_map_and_nodes(x.size());
  1098. this->priv_segmented_uninitialized_move_alloc_n(x.begin(), x.size(), this->begin());
  1099. }
  1100. }
  1101. }
  1102. //! <b>Effects</b>: Destroys the deque. All stored values are destroyed
  1103. //! and used memory is deallocated.
  1104. //!
  1105. //! <b>Throws</b>: Nothing.
  1106. //!
  1107. //! <b>Complexity</b>: Linear to the number of elements.
  1108. inline ~deque() BOOST_NOEXCEPT_OR_NOTHROW
  1109. {
  1110. this->prot_destroy_range(this->prot_start(), this->prot_finish());
  1111. }
  1112. //! <b>Effects</b>: Makes *this contain the same elements as x.
  1113. //!
  1114. //! <b>Postcondition</b>: this->size() == x.size(). *this contains a copy
  1115. //! of each of x's elements.
  1116. //!
  1117. //! <b>Throws</b>: If memory allocation throws or T's copy constructor throws.
  1118. //!
  1119. //! <b>Complexity</b>: Linear to the number of elements in x.
  1120. deque& operator= (BOOST_COPY_ASSIGN_REF(deque) x)
  1121. {
  1122. if (BOOST_LIKELY(&x != this)){
  1123. allocator_type &this_alloc = this->alloc();
  1124. const allocator_type &x_alloc = x.alloc();
  1125. dtl::bool_<allocator_traits_type::
  1126. propagate_on_container_copy_assignment::value> flag;
  1127. if(flag && this_alloc != x_alloc){
  1128. this->clear();
  1129. this->shrink_to_fit();
  1130. }
  1131. dtl::assign_alloc(this->alloc(), x.alloc(), flag);
  1132. dtl::assign_alloc(this->ptr_alloc(), x.ptr_alloc(), flag);
  1133. this->assign(x.cbegin(), x.cend());
  1134. }
  1135. return *this;
  1136. }
  1137. //! <b>Effects</b>: Move assignment. All x's values are transferred to *this.
  1138. //!
  1139. //! <b>Throws</b>: If allocator_traits_type::propagate_on_container_move_assignment
  1140. //! is false and (allocation throws or value_type's move constructor throws)
  1141. //!
  1142. //! <b>Complexity</b>: Constant if allocator_traits_type::
  1143. //! propagate_on_container_move_assignment is true or
  1144. //! this->get>allocator() == x.get_allocator(). Linear otherwise.
  1145. deque& operator= (BOOST_RV_REF(deque) x)
  1146. BOOST_NOEXCEPT_IF(allocator_traits_type::propagate_on_container_move_assignment::value
  1147. || allocator_traits_type::is_always_equal::value)
  1148. {
  1149. if (BOOST_LIKELY(this != &x)) {
  1150. //We know resources can be transferred at comiple time if both allocators are
  1151. //always equal or the allocator is going to be propagated
  1152. const bool can_steal_resources_alloc
  1153. = allocator_traits_type::propagate_on_container_move_assignment::value
  1154. || allocator_traits_type::is_always_equal::value;
  1155. dtl::bool_<can_steal_resources_alloc> flag;
  1156. this->priv_move_assign(boost::move(x), flag);
  1157. }
  1158. return *this;
  1159. }
  1160. #if !defined(BOOST_NO_CXX11_HDR_INITIALIZER_LIST)
  1161. //! <b>Effects</b>: Makes *this contain the same elements as il.
  1162. //!
  1163. //! <b>Postcondition</b>: this->size() == il.size(). *this contains a copy
  1164. //! of each of x's elements.
  1165. //!
  1166. //! <b>Throws</b>: If memory allocation throws or T's copy constructor throws.
  1167. //!
  1168. //! <b>Complexity</b>: Linear to the number of elements in il.
  1169. inline deque& operator=(std::initializer_list<value_type> il)
  1170. {
  1171. this->assign(il.begin(), il.end());
  1172. return *this;
  1173. }
  1174. #endif
  1175. //! <b>Effects</b>: Assigns the n copies of val to *this.
  1176. //!
  1177. //! <b>Throws</b>: If memory allocation throws or T's copy constructor throws.
  1178. //!
  1179. //! <b>Complexity</b>: Linear to n.
  1180. inline void assign(size_type n, const T& val)
  1181. {
  1182. dtl::insert_n_copies_proxy<ValAllocator> proxy(val);
  1183. this->priv_assign(n, proxy);
  1184. }
  1185. //! <b>Effects</b>: Assigns the the range [first, last) to *this.
  1186. //!
  1187. //! <b>Throws</b>: If memory allocation throws or
  1188. //! T's constructor from dereferencing InIt throws.
  1189. //!
  1190. //! <b>Complexity</b>: Linear to n.
  1191. template <class InIt>
  1192. void assign(InIt first, InIt last
  1193. #if !defined(BOOST_CONTAINER_DOXYGEN_INVOKED)
  1194. , typename dtl::disable_if_or
  1195. < void
  1196. , dtl::is_convertible<InIt, size_type>
  1197. , dtl::is_not_input_iterator<InIt>
  1198. >::type * = 0
  1199. #endif
  1200. )
  1201. {
  1202. iterator cur = this->begin();
  1203. for ( ; first != last && cur != end(); ++cur, ++first){
  1204. *cur = *first;
  1205. }
  1206. if (first == last){
  1207. this->erase(cur, this->cend());
  1208. }
  1209. else{
  1210. this->insert(this->cend(), first, last);
  1211. }
  1212. }
  1213. #if !defined(BOOST_CONTAINER_DOXYGEN_INVOKED)
  1214. template <class FwdIt>
  1215. void assign(FwdIt first, FwdIt last
  1216. , typename dtl::disable_if_or
  1217. < void
  1218. , dtl::is_convertible<FwdIt, size_type>
  1219. , dtl::is_input_iterator<FwdIt>
  1220. >::type * = 0
  1221. )
  1222. {
  1223. typedef typename iter_size<FwdIt>::type it_size_type;
  1224. const it_size_type sz = boost::container::iterator_udistance(first, last);
  1225. if (BOOST_UNLIKELY(sz > size_type(-1))){
  1226. boost::container::throw_length_error("vector::insert, FwdIt's max length reached");
  1227. }
  1228. const size_type n = static_cast<size_type>(sz);
  1229. dtl::insert_range_proxy<ValAllocator, FwdIt> proxy(first);
  1230. this->priv_assign(n, proxy);
  1231. }
  1232. #endif
  1233. #if !defined(BOOST_NO_CXX11_HDR_INITIALIZER_LIST)
  1234. //! <b>Effects</b>: Assigns the the range [il.begin(), il.end()) to *this.
  1235. //!
  1236. //! <b>Throws</b>: If memory allocation throws or
  1237. //! T's constructor from dereferencing std::initializer_list iterator throws.
  1238. //!
  1239. //! <b>Complexity</b>: Linear to il.size().
  1240. inline void assign(std::initializer_list<value_type> il)
  1241. { this->assign(il.begin(), il.end()); }
  1242. #endif
  1243. //! <b>Effects</b>: Returns a copy of the internal allocator.
  1244. //!
  1245. //! <b>Throws</b>: If allocator's copy constructor throws.
  1246. //!
  1247. //! <b>Complexity</b>: Constant.
  1248. BOOST_CONTAINER_ATTRIBUTE_NODISCARD inline
  1249. allocator_type get_allocator() const BOOST_NOEXCEPT_OR_NOTHROW
  1250. { return Base::alloc(); }
  1251. //! <b>Effects</b>: Returns a reference to the internal allocator.
  1252. //!
  1253. //! <b>Throws</b>: Nothing
  1254. //!
  1255. //! <b>Complexity</b>: Constant.
  1256. //!
  1257. //! <b>Note</b>: Non-standard extension.
  1258. BOOST_CONTAINER_ATTRIBUTE_NODISCARD inline
  1259. const stored_allocator_type &get_stored_allocator() const BOOST_NOEXCEPT_OR_NOTHROW
  1260. { return Base::alloc(); }
  1261. //////////////////////////////////////////////
  1262. //
  1263. // iterators
  1264. //
  1265. //////////////////////////////////////////////
  1266. //! <b>Effects</b>: Returns a reference to the internal allocator.
  1267. //!
  1268. //! <b>Throws</b>: Nothing
  1269. //!
  1270. //! <b>Complexity</b>: Constant.
  1271. //!
  1272. //! <b>Note</b>: Non-standard extension.
  1273. BOOST_CONTAINER_ATTRIBUTE_NODISCARD inline
  1274. stored_allocator_type &get_stored_allocator() BOOST_NOEXCEPT_OR_NOTHROW
  1275. { return Base::alloc(); }
  1276. //! <b>Effects</b>: Returns an iterator to the first element contained in the deque.
  1277. //!
  1278. //! <b>Throws</b>: Nothing.
  1279. //!
  1280. //! <b>Complexity</b>: Constant.
  1281. BOOST_CONTAINER_ATTRIBUTE_NODISCARD inline
  1282. iterator begin() BOOST_NOEXCEPT_OR_NOTHROW
  1283. { return this->prot_start(); }
  1284. //! <b>Effects</b>: Returns a const_iterator to the first element contained in the deque.
  1285. //!
  1286. //! <b>Throws</b>: Nothing.
  1287. //!
  1288. //! <b>Complexity</b>: Constant.
  1289. BOOST_CONTAINER_ATTRIBUTE_NODISCARD inline
  1290. const_iterator begin() const BOOST_NOEXCEPT_OR_NOTHROW
  1291. { return this->prot_start(); }
  1292. //! <b>Effects</b>: Returns an iterator to the end of the deque.
  1293. //!
  1294. //! <b>Throws</b>: Nothing.
  1295. //!
  1296. //! <b>Complexity</b>: Constant.
  1297. BOOST_CONTAINER_ATTRIBUTE_NODISCARD inline
  1298. iterator end() BOOST_NOEXCEPT_OR_NOTHROW
  1299. { return this->prot_finish(); }
  1300. //! <b>Effects</b>: Returns a const_iterator to the end of the deque.
  1301. //!
  1302. //! <b>Throws</b>: Nothing.
  1303. //!
  1304. //! <b>Complexity</b>: Constant.
  1305. BOOST_CONTAINER_ATTRIBUTE_NODISCARD inline
  1306. const_iterator end() const BOOST_NOEXCEPT_OR_NOTHROW
  1307. { return this->prot_finish(); }
  1308. //! <b>Effects</b>: Returns a reverse_iterator pointing to the beginning
  1309. //! of the reversed deque.
  1310. //!
  1311. //! <b>Throws</b>: Nothing.
  1312. //!
  1313. //! <b>Complexity</b>: Constant.
  1314. BOOST_CONTAINER_ATTRIBUTE_NODISCARD inline
  1315. reverse_iterator rbegin() BOOST_NOEXCEPT_OR_NOTHROW
  1316. { return reverse_iterator(this->prot_finish()); }
  1317. //! <b>Effects</b>: Returns a const_reverse_iterator pointing to the beginning
  1318. //! of the reversed deque.
  1319. //!
  1320. //! <b>Throws</b>: Nothing.
  1321. //!
  1322. //! <b>Complexity</b>: Constant.
  1323. BOOST_CONTAINER_ATTRIBUTE_NODISCARD inline
  1324. const_reverse_iterator rbegin() const BOOST_NOEXCEPT_OR_NOTHROW
  1325. { return const_reverse_iterator(this->prot_finish()); }
  1326. //! <b>Effects</b>: Returns a reverse_iterator pointing to the end
  1327. //! of the reversed deque.
  1328. //!
  1329. //! <b>Throws</b>: Nothing.
  1330. //!
  1331. //! <b>Complexity</b>: Constant.
  1332. BOOST_CONTAINER_ATTRIBUTE_NODISCARD inline
  1333. reverse_iterator rend() BOOST_NOEXCEPT_OR_NOTHROW
  1334. { return reverse_iterator(this->prot_start()); }
  1335. //! <b>Effects</b>: Returns a const_reverse_iterator pointing to the end
  1336. //! of the reversed deque.
  1337. //!
  1338. //! <b>Throws</b>: Nothing.
  1339. //!
  1340. //! <b>Complexity</b>: Constant.
  1341. BOOST_CONTAINER_ATTRIBUTE_NODISCARD inline
  1342. const_reverse_iterator rend() const BOOST_NOEXCEPT_OR_NOTHROW
  1343. { return const_reverse_iterator(this->prot_start()); }
  1344. //! <b>Effects</b>: Returns a const_iterator to the first element contained in the deque.
  1345. //!
  1346. //! <b>Throws</b>: Nothing.
  1347. //!
  1348. //! <b>Complexity</b>: Constant.
  1349. BOOST_CONTAINER_ATTRIBUTE_NODISCARD inline
  1350. const_iterator cbegin() const BOOST_NOEXCEPT_OR_NOTHROW
  1351. { return this->prot_start(); }
  1352. //! <b>Effects</b>: Returns a const_iterator to the end of the deque.
  1353. //!
  1354. //! <b>Throws</b>: Nothing.
  1355. //!
  1356. //! <b>Complexity</b>: Constant.
  1357. BOOST_CONTAINER_ATTRIBUTE_NODISCARD inline
  1358. const_iterator cend() const BOOST_NOEXCEPT_OR_NOTHROW
  1359. { return this->prot_finish(); }
  1360. //! <b>Effects</b>: Returns a const_reverse_iterator pointing to the beginning
  1361. //! of the reversed deque.
  1362. //!
  1363. //! <b>Throws</b>: Nothing.
  1364. //!
  1365. //! <b>Complexity</b>: Constant.
  1366. BOOST_CONTAINER_ATTRIBUTE_NODISCARD inline
  1367. const_reverse_iterator crbegin() const BOOST_NOEXCEPT_OR_NOTHROW
  1368. { return const_reverse_iterator(this->prot_finish()); }
  1369. //! <b>Effects</b>: Returns a const_reverse_iterator pointing to the end
  1370. //! of the reversed deque.
  1371. //!
  1372. //! <b>Throws</b>: Nothing.
  1373. //!
  1374. //! <b>Complexity</b>: Constant.
  1375. BOOST_CONTAINER_ATTRIBUTE_NODISCARD inline
  1376. const_reverse_iterator crend() const BOOST_NOEXCEPT_OR_NOTHROW
  1377. { return const_reverse_iterator(this->prot_start()); }
  1378. //////////////////////////////////////////////
  1379. //
  1380. // capacity
  1381. //
  1382. //////////////////////////////////////////////
  1383. //! <b>Effects</b>: Returns true if the deque contains no elements.
  1384. //!
  1385. //! <b>Throws</b>: Nothing.
  1386. //!
  1387. //! <b>Complexity</b>: Constant.
  1388. BOOST_CONTAINER_ATTRIBUTE_NODISCARD inline
  1389. bool empty() const BOOST_NOEXCEPT_OR_NOTHROW
  1390. { return this->prot_empty(); }
  1391. //! <b>Effects</b>: Returns the number of the elements contained in the deque.
  1392. //!
  1393. //! <b>Throws</b>: Nothing.
  1394. //!
  1395. //! <b>Complexity</b>: Constant.
  1396. BOOST_CONTAINER_ATTRIBUTE_NODISCARD inline
  1397. size_type size() const BOOST_NOEXCEPT_OR_NOTHROW
  1398. { return this->prot_size(); }
  1399. //! <b>Effects</b>: Returns the number of the elements that can be inserted
  1400. //! at the back without allocating additional memory.
  1401. //!
  1402. //! <b>Throws</b>: Nothing.
  1403. //!
  1404. //! <b>Complexity</b>: Constant.
  1405. //!
  1406. //! <b>Note</b>: Non-standard extension.
  1407. BOOST_CONTAINER_ATTRIBUTE_NODISCARD inline
  1408. size_type back_capacity() const BOOST_NOEXCEPT_OR_NOTHROW
  1409. { return size_type(this->size() + this->prot_back_free_capacity()); }
  1410. //! <b>Effects</b>: Returns the number of the elements that can be inserted
  1411. //! at the front without allocating additional memory.
  1412. //!
  1413. //! <b>Throws</b>: Nothing.
  1414. //!
  1415. //! <b>Complexity</b>: Constant.
  1416. //!
  1417. //! <b>Note</b>: Non-standard extension.
  1418. BOOST_CONTAINER_ATTRIBUTE_NODISCARD inline
  1419. size_type front_capacity() const BOOST_NOEXCEPT_OR_NOTHROW
  1420. { return size_type(this->size() + this->prot_front_free_capacity()); }
  1421. //! <b>Requires</b>: The container must be "reservable" (is_reservable == true)
  1422. //!
  1423. //! <b>Effects</b>: If n is less than or equal to back_capacity() or the container is not reservable,
  1424. //! this call has no effect. Otherwise, if it is a request for allocation of additional memory.
  1425. //! If the request is successful, then back_capacity() is greater than or equal to
  1426. //! n; otherwise, back_capacity() is unchanged. In either case, size() is unchanged.
  1427. //!
  1428. //! <b>Throws</b>: If memory allocation throws.
  1429. //!
  1430. //! <b>Complexity</b>: Linear to n.
  1431. //!
  1432. //! <b>Note</b>: Non-standard extension.
  1433. void reserve_back(size_type n)
  1434. {
  1435. (void)n;
  1436. BOOST_IF_CONSTEXPR(is_reservable){
  1437. const size_type cur_back_cap = this->back_capacity();
  1438. if (this->back_capacity() < n)
  1439. this->priv_reserve_elements_at_back(size_type(n - cur_back_cap) );
  1440. }
  1441. }
  1442. //! <b>Requires</b>: The container must be "reservable" (is_reservable == true)
  1443. //!
  1444. //! <b>Effects</b>: If n is less than or equal to back_capacity() or the container is not reservable,
  1445. //! this call has no effect. Otherwise, if it is a request for allocation of additional memory.
  1446. //! If the request is successful, then back_capacity() is greater than or equal to
  1447. //! n; otherwise, back_capacity() is unchanged. In either case, size() is unchanged.
  1448. //!
  1449. //! <b>Throws</b>: If memory allocation throws.
  1450. //!
  1451. //! <b>Complexity</b>: Linear to n.
  1452. //!
  1453. //! <b>Note</b>: Non-standard extension.
  1454. void reserve_front(size_type n)
  1455. {
  1456. (void)n;
  1457. BOOST_IF_CONSTEXPR(is_reservable){
  1458. const size_type cur_back_cap = this->front_capacity();
  1459. if (this->front_capacity() < n)
  1460. this->priv_reserve_elements_at_front(size_type(n - cur_back_cap) );
  1461. }
  1462. }
  1463. //! <b>Effects</b>: Returns the largest possible size of the deque.
  1464. //!
  1465. //! <b>Throws</b>: Nothing.
  1466. //!
  1467. //! <b>Complexity</b>: Constant.
  1468. BOOST_CONTAINER_ATTRIBUTE_NODISCARD inline
  1469. size_type max_size() const BOOST_NOEXCEPT_OR_NOTHROW
  1470. { return allocator_traits_type::max_size(this->alloc()); }
  1471. //! <b>Effects</b>: Inserts or erases elements at the end such that
  1472. //! the size becomes n. New elements are value initialized.
  1473. //!
  1474. //! <b>Throws</b>: If memory allocation throws, or T's constructor throws.
  1475. //!
  1476. //! <b>Complexity</b>: Linear to the difference between size() and new_size.
  1477. void resize(size_type new_size)
  1478. {
  1479. const size_type len = this->size();
  1480. if (new_size < len)
  1481. this->priv_erase_last_n(size_type(len - new_size));
  1482. else{
  1483. const size_type n = size_type(new_size - this->size());
  1484. dtl::insert_value_initialized_n_proxy<ValAllocator> proxy;
  1485. this->priv_insert_back_aux_impl(n, proxy);
  1486. }
  1487. }
  1488. //! <b>Effects</b>: Inserts or erases elements at the end such that
  1489. //! the size becomes n. New elements are default initialized.
  1490. //!
  1491. //! <b>Throws</b>: If memory allocation throws, or T's constructor throws.
  1492. //!
  1493. //! <b>Complexity</b>: Linear to the difference between size() and new_size.
  1494. //!
  1495. //! <b>Note</b>: Non-standard extension
  1496. void resize(size_type new_size, default_init_t)
  1497. {
  1498. const size_type len = size();
  1499. if (new_size < len)
  1500. this->priv_erase_last_n(size_type(len - new_size));
  1501. else{
  1502. const size_type n = size_type(new_size - this->size());
  1503. dtl::insert_default_initialized_n_proxy<ValAllocator> proxy;
  1504. this->priv_insert_back_aux_impl(n, proxy);
  1505. }
  1506. }
  1507. //! <b>Effects</b>: Inserts or erases elements at the end such that
  1508. //! the size becomes n. New elements are copy constructed from x.
  1509. //!
  1510. //! <b>Throws</b>: If memory allocation throws, or T's copy constructor throws.
  1511. //!
  1512. //! <b>Complexity</b>: Linear to the difference between size() and new_size.
  1513. void resize(size_type new_size, const value_type& x)
  1514. {
  1515. const size_type sz = this->size();
  1516. if (new_size < sz)
  1517. this->priv_erase_last_n(size_type(sz - new_size));
  1518. else {
  1519. const size_type n = size_type(new_size - sz);
  1520. dtl::insert_n_copies_proxy<ValAllocator> proxy(x);
  1521. this->priv_insert_back_aux_impl(n, proxy);
  1522. }
  1523. }
  1524. //! <b>Effects</b>: Tries to deallocate the excess of memory created
  1525. //! with previous allocations. The size of the deque is unchanged
  1526. //!
  1527. //! <b>Throws</b>: If memory allocation throws.
  1528. //!
  1529. //! <b>Complexity</b>: Constant.
  1530. void shrink_to_fit()
  1531. {
  1532. //This deque implementation already
  1533. //deallocates excess nodes when erasing
  1534. //so there is nothing to do except for
  1535. //empty deque
  1536. if(this->empty()){
  1537. this->prot_clear_map();
  1538. }
  1539. }
  1540. //////////////////////////////////////////////
  1541. //
  1542. // element access
  1543. //
  1544. //////////////////////////////////////////////
  1545. //! <b>Requires</b>: !empty()
  1546. //!
  1547. //! <b>Effects</b>: Returns a reference to the first
  1548. //! element of the container.
  1549. //!
  1550. //! <b>Throws</b>: Nothing.
  1551. //!
  1552. //! <b>Complexity</b>: Constant.
  1553. BOOST_CONTAINER_ATTRIBUTE_NODISCARD inline
  1554. reference front() BOOST_NOEXCEPT_OR_NOTHROW
  1555. {
  1556. BOOST_ASSERT(!this->empty());
  1557. return *this->prot_start_cur_unchecked();
  1558. }
  1559. //! <b>Requires</b>: !empty()
  1560. //!
  1561. //! <b>Effects</b>: Returns a const reference to the first element
  1562. //! from the beginning of the container.
  1563. //!
  1564. //! <b>Throws</b>: Nothing.
  1565. //!
  1566. //! <b>Complexity</b>: Constant.
  1567. BOOST_CONTAINER_ATTRIBUTE_NODISCARD inline
  1568. const_reference front() const BOOST_NOEXCEPT_OR_NOTHROW
  1569. {
  1570. BOOST_ASSERT(!this->empty());
  1571. return *this->prot_start_cur_unchecked();
  1572. }
  1573. //! <b>Requires</b>: !empty()
  1574. //!
  1575. //! <b>Effects</b>: Returns a reference to the last
  1576. //! element of the container.
  1577. //!
  1578. //! <b>Throws</b>: Nothing.
  1579. //!
  1580. //! <b>Complexity</b>: Constant.
  1581. BOOST_CONTAINER_ATTRIBUTE_NODISCARD inline
  1582. reference back() BOOST_NOEXCEPT_OR_NOTHROW
  1583. {
  1584. BOOST_ASSERT(!this->empty());
  1585. return *this->prot_last_cur_unchecked();
  1586. }
  1587. //! <b>Requires</b>: !empty()
  1588. //!
  1589. //! <b>Effects</b>: Returns a const reference to the last
  1590. //! element of the container.
  1591. //!
  1592. //! <b>Throws</b>: Nothing.
  1593. //!
  1594. //! <b>Complexity</b>: Constant.
  1595. BOOST_CONTAINER_ATTRIBUTE_NODISCARD inline
  1596. const_reference back() const BOOST_NOEXCEPT_OR_NOTHROW
  1597. {
  1598. BOOST_ASSERT(!this->empty());
  1599. return *this->prot_last_cur_unchecked();
  1600. }
  1601. //! <b>Requires</b>: size() > n.
  1602. //!
  1603. //! <b>Effects</b>: Returns a reference to the nth element
  1604. //! from the beginning of the container.
  1605. //!
  1606. //! <b>Throws</b>: Nothing.
  1607. //!
  1608. //! <b>Complexity</b>: Constant.
  1609. BOOST_CONTAINER_ATTRIBUTE_NODISCARD inline
  1610. reference operator[](size_type n) BOOST_NOEXCEPT_OR_NOTHROW
  1611. {
  1612. BOOST_ASSERT(this->size() > n);
  1613. return *this->prot_nth(n);
  1614. }
  1615. //! <b>Requires</b>: size() > n.
  1616. //!
  1617. //! <b>Effects</b>: Returns a const reference to the nth element
  1618. //! from the beginning of the container.
  1619. //!
  1620. //! <b>Throws</b>: Nothing.
  1621. //!
  1622. //! <b>Complexity</b>: Constant.
  1623. BOOST_CONTAINER_ATTRIBUTE_NODISCARD inline
  1624. const_reference operator[](size_type n) const BOOST_NOEXCEPT_OR_NOTHROW
  1625. {
  1626. BOOST_ASSERT(this->size() > n);
  1627. return *this->prot_nth(n);
  1628. }
  1629. //! <b>Requires</b>: size() >= n.
  1630. //!
  1631. //! <b>Effects</b>: Returns an iterator to the nth element
  1632. //! from the beginning of the container. Returns end()
  1633. //! if n == size().
  1634. //!
  1635. //! <b>Throws</b>: Nothing.
  1636. //!
  1637. //! <b>Complexity</b>: Constant.
  1638. //!
  1639. //! <b>Note</b>: Non-standard extension
  1640. BOOST_CONTAINER_ATTRIBUTE_NODISCARD inline
  1641. iterator nth(size_type n) BOOST_NOEXCEPT_OR_NOTHROW
  1642. {
  1643. BOOST_ASSERT(this->size() >= n);
  1644. return this->prot_nth(n);
  1645. }
  1646. //! <b>Requires</b>: size() >= n.
  1647. //!
  1648. //! <b>Effects</b>: Returns a const_iterator to the nth element
  1649. //! from the beginning of the container. Returns end()
  1650. //! if n == size().
  1651. //!
  1652. //! <b>Throws</b>: Nothing.
  1653. //!
  1654. //! <b>Complexity</b>: Constant.
  1655. //!
  1656. //! <b>Note</b>: Non-standard extension
  1657. BOOST_CONTAINER_ATTRIBUTE_NODISCARD inline
  1658. const_iterator nth(size_type n) const BOOST_NOEXCEPT_OR_NOTHROW
  1659. {
  1660. BOOST_ASSERT(this->size() >= n);
  1661. return this->prot_nth(n);
  1662. }
  1663. //! <b>Requires</b>: begin() <= p <= end().
  1664. //!
  1665. //! <b>Effects</b>: Returns the index of the element pointed by p
  1666. //! and size() if p == end().
  1667. //!
  1668. //! <b>Throws</b>: Nothing.
  1669. //!
  1670. //! <b>Complexity</b>: Constant.
  1671. //!
  1672. //! <b>Note</b>: Non-standard extension
  1673. BOOST_CONTAINER_ATTRIBUTE_NODISCARD inline
  1674. size_type index_of(iterator p) BOOST_NOEXCEPT_OR_NOTHROW
  1675. {
  1676. //Range checked priv_index_of
  1677. return this->priv_index_of(p);
  1678. }
  1679. //! <b>Requires</b>: begin() <= p <= end().
  1680. //!
  1681. //! <b>Effects</b>: Returns the index of the element pointed by p
  1682. //! and size() if p == end().
  1683. //!
  1684. //! <b>Throws</b>: Nothing.
  1685. //!
  1686. //! <b>Complexity</b>: Constant.
  1687. //!
  1688. //! <b>Note</b>: Non-standard extension
  1689. BOOST_CONTAINER_ATTRIBUTE_NODISCARD inline
  1690. size_type index_of(const_iterator p) const BOOST_NOEXCEPT_OR_NOTHROW
  1691. {
  1692. //Range checked priv_index_of
  1693. return this->priv_index_of(p);
  1694. }
  1695. //! <b>Requires</b>: size() > n.
  1696. //!
  1697. //! <b>Effects</b>: Returns a reference to the nth element
  1698. //! from the beginning of the container.
  1699. //!
  1700. //! <b>Throws</b>: range_error if n >= size()
  1701. //!
  1702. //! <b>Complexity</b>: Constant.
  1703. BOOST_CONTAINER_ATTRIBUTE_NODISCARD inline
  1704. reference at(size_type n)
  1705. {
  1706. this->priv_throw_if_out_of_range(n);
  1707. return (*this)[n];
  1708. }
  1709. //! <b>Requires</b>: size() > n.
  1710. //!
  1711. //! <b>Effects</b>: Returns a const reference to the nth element
  1712. //! from the beginning of the container.
  1713. //!
  1714. //! <b>Throws</b>: range_error if n >= size()
  1715. //!
  1716. //! <b>Complexity</b>: Constant.
  1717. BOOST_CONTAINER_ATTRIBUTE_NODISCARD inline
  1718. const_reference at(size_type n) const
  1719. {
  1720. this->priv_throw_if_out_of_range(n);
  1721. return (*this)[n];
  1722. }
  1723. //////////////////////////////////////////////
  1724. //
  1725. // modifiers
  1726. //
  1727. //////////////////////////////////////////////
  1728. #if !defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES) || defined(BOOST_CONTAINER_DOXYGEN_INVOKED)
  1729. //! <b>Effects</b>: Inserts an object of type T constructed with
  1730. //! std::forward<Args>(args)... in the beginning of the deque.
  1731. //!
  1732. //! <b>Returns</b>: A reference to the created object.
  1733. //!
  1734. //! <b>Throws</b>: If memory allocation throws or the in-place constructor throws.
  1735. //!
  1736. //! <b>Complexity</b>: Amortized constant time
  1737. template <class... Args>
  1738. reference emplace_front(BOOST_FWD_REF(Args)... args)
  1739. {
  1740. value_type *pr = this->prot_push_front_simple_pos();
  1741. if(BOOST_LIKELY(pr != 0)){
  1742. allocator_traits_type::construct
  1743. ( this->alloc()
  1744. , pr
  1745. , boost::forward<Args>(args)...);
  1746. this->prot_dec_start();
  1747. return *pr;
  1748. }
  1749. else{
  1750. typedef dtl::insert_nonmovable_emplace_proxy<ValAllocator, Args...> type;
  1751. return *this->priv_insert_front_aux_impl(1, type(boost::forward<Args>(args)...));
  1752. }
  1753. }
  1754. //! <b>Effects</b>: Inserts an object of type T constructed with
  1755. //! std::forward<Args>(args)... in the end of the deque.
  1756. //!
  1757. //! <b>Returns</b>: A reference to the created object.
  1758. //!
  1759. //! <b>Throws</b>: If memory allocation throws or the in-place constructor throws.
  1760. //!
  1761. //! <b>Complexity</b>: Amortized constant time
  1762. template <class... Args>
  1763. reference emplace_back(BOOST_FWD_REF(Args)... args)
  1764. {
  1765. value_type *pr = this->prot_push_back_simple_pos();
  1766. if(BOOST_LIKELY(pr != 0)){
  1767. allocator_traits_type::construct
  1768. ( this->alloc(), pr, boost::forward<Args>(args)...);
  1769. this->prot_inc_finish();
  1770. return *pr;
  1771. }
  1772. else{
  1773. typedef dtl::insert_nonmovable_emplace_proxy<ValAllocator, Args...> type;
  1774. return *this->priv_insert_back_aux_impl(1, type(boost::forward<Args>(args)...));
  1775. }
  1776. }
  1777. //! <b>Requires</b>: p must be a valid iterator of *this.
  1778. //!
  1779. //! <b>Effects</b>: Inserts an object of type T constructed with
  1780. //! std::forward<Args>(args)... before p
  1781. //!
  1782. //! <b>Throws</b>: If memory allocation throws or the in-place constructor throws.
  1783. //!
  1784. //! <b>Complexity</b>: If p is end(), amortized constant time
  1785. //! Linear time otherwise.
  1786. template <class... Args>
  1787. iterator emplace(const_iterator p, BOOST_FWD_REF(Args)... args)
  1788. {
  1789. const size_type elemsbefore = this->prot_it_to_start_off(p);
  1790. const size_type length = this->prot_size();
  1791. if (!elemsbefore) {
  1792. this->emplace_front(boost::forward<Args>(args)...);
  1793. return this->begin();
  1794. }
  1795. else if (elemsbefore == length) {
  1796. this->emplace_back(boost::forward<Args>(args)...);
  1797. return this->prot_back_it();
  1798. }
  1799. else {
  1800. typedef dtl::insert_emplace_proxy<ValAllocator, Args...> type;
  1801. return this->priv_insert_middle_aux_impl(elemsbefore, 1, type(boost::forward<Args>(args)...));
  1802. }
  1803. }
  1804. #else //!defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES)
  1805. #define BOOST_CONTAINER_DEQUE_EMPLACE_CODE(N) \
  1806. BOOST_MOVE_TMPL_LT##N BOOST_MOVE_CLASS##N BOOST_MOVE_GT##N\
  1807. reference emplace_front(BOOST_MOVE_UREF##N)\
  1808. {\
  1809. value_type *pr = this->prot_push_front_simple_pos();\
  1810. if(BOOST_LIKELY(pr != 0)){\
  1811. allocator_traits_type::construct\
  1812. ( this->alloc(), pr BOOST_MOVE_I##N BOOST_MOVE_FWD##N);\
  1813. this->prot_dec_start();\
  1814. return *pr;\
  1815. }\
  1816. else{\
  1817. typedef dtl::insert_nonmovable_emplace_proxy##N\
  1818. <ValAllocator BOOST_MOVE_I##N BOOST_MOVE_TARG##N> type;\
  1819. return *priv_insert_front_aux_impl(1, type(BOOST_MOVE_FWD##N));\
  1820. }\
  1821. }\
  1822. \
  1823. BOOST_MOVE_TMPL_LT##N BOOST_MOVE_CLASS##N BOOST_MOVE_GT##N\
  1824. reference emplace_back(BOOST_MOVE_UREF##N)\
  1825. {\
  1826. value_type *pr = this->prot_push_back_simple_pos();\
  1827. if(BOOST_LIKELY(pr != 0)){\
  1828. allocator_traits_type::construct\
  1829. ( this->alloc(), pr BOOST_MOVE_I##N BOOST_MOVE_FWD##N);\
  1830. this->prot_inc_finish();\
  1831. return *pr;\
  1832. }\
  1833. else{\
  1834. typedef dtl::insert_nonmovable_emplace_proxy##N\
  1835. <ValAllocator BOOST_MOVE_I##N BOOST_MOVE_TARG##N> type;\
  1836. return *priv_insert_back_aux_impl(1, type(BOOST_MOVE_FWD##N));\
  1837. }\
  1838. }\
  1839. \
  1840. BOOST_MOVE_TMPL_LT##N BOOST_MOVE_CLASS##N BOOST_MOVE_GT##N\
  1841. iterator emplace(const_iterator p BOOST_MOVE_I##N BOOST_MOVE_UREF##N)\
  1842. {\
  1843. const size_type elemsbefore = this->prot_it_to_start_off(p);\
  1844. const size_type length = this->prot_size();\
  1845. \
  1846. if (!elemsbefore) {\
  1847. this->emplace_front(BOOST_MOVE_FWD##N);\
  1848. return this->begin();\
  1849. }\
  1850. else if (elemsbefore == length) {\
  1851. this->emplace_back(BOOST_MOVE_FWD##N);\
  1852. return this->prot_back_it();\
  1853. }\
  1854. else {\
  1855. typedef dtl::insert_emplace_proxy_arg##N\
  1856. <ValAllocator BOOST_MOVE_I##N BOOST_MOVE_TARG##N> type;\
  1857. return this->priv_insert_middle_aux_impl(elemsbefore, 1, type(BOOST_MOVE_FWD##N));\
  1858. }\
  1859. }\
  1860. //
  1861. BOOST_MOVE_ITERATE_0TO9(BOOST_CONTAINER_DEQUE_EMPLACE_CODE)
  1862. #undef BOOST_CONTAINER_DEQUE_EMPLACE_CODE
  1863. #endif // !defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES)
  1864. #if defined(BOOST_CONTAINER_DOXYGEN_INVOKED)
  1865. //! <b>Effects</b>: Inserts a copy of x at the front of the deque.
  1866. //!
  1867. //! <b>Throws</b>: If memory allocation throws or
  1868. //! T's copy constructor throws.
  1869. //!
  1870. //! <b>Complexity</b>: Amortized constant time.
  1871. void push_front(const T &x);
  1872. //! <b>Effects</b>: Constructs a new element in the front of the deque
  1873. //! and moves the resources of x to this new element.
  1874. //!
  1875. //! <b>Throws</b>: If memory allocation throws.
  1876. //!
  1877. //! <b>Complexity</b>: Amortized constant time.
  1878. void push_front(T &&x);
  1879. #else
  1880. BOOST_MOVE_CONVERSION_AWARE_CATCH(push_front, T, void, priv_push_front)
  1881. #endif
  1882. #if defined(BOOST_CONTAINER_DOXYGEN_INVOKED)
  1883. //! <b>Effects</b>: Inserts a copy of x at the end of the deque.
  1884. //!
  1885. //! <b>Throws</b>: If memory allocation throws or
  1886. //! T's copy constructor throws.
  1887. //!
  1888. //! <b>Complexity</b>: Amortized constant time.
  1889. void push_back(const T &x);
  1890. //! <b>Effects</b>: Constructs a new element in the end of the deque
  1891. //! and moves the resources of x to this new element.
  1892. //!
  1893. //! <b>Throws</b>: If memory allocation throws.
  1894. //!
  1895. //! <b>Complexity</b>: Amortized constant time.
  1896. void push_back(T &&x);
  1897. #else
  1898. BOOST_MOVE_CONVERSION_AWARE_CATCH(push_back, T, void, priv_push_back)
  1899. #endif
  1900. #if defined(BOOST_CONTAINER_DOXYGEN_INVOKED)
  1901. //! <b>Requires</b>: p must be a valid iterator of *this.
  1902. //!
  1903. //! <b>Effects</b>: Insert a copy of x before p.
  1904. //!
  1905. //! <b>Returns</b>: an iterator to the inserted element.
  1906. //!
  1907. //! <b>Throws</b>: If memory allocation throws or x's copy constructor throws.
  1908. //!
  1909. //! <b>Complexity</b>: If p is end(), amortized constant time
  1910. //! Linear time otherwise.
  1911. iterator insert(const_iterator p, const T &x);
  1912. //! <b>Requires</b>: p must be a valid iterator of *this.
  1913. //!
  1914. //! <b>Effects</b>: Insert a new element before p with x's resources.
  1915. //!
  1916. //! <b>Returns</b>: an iterator to the inserted element.
  1917. //!
  1918. //! <b>Throws</b>: If memory allocation throws.
  1919. //!
  1920. //! <b>Complexity</b>: If p is end(), amortized constant time
  1921. //! Linear time otherwise.
  1922. iterator insert(const_iterator p, T &&x);
  1923. #else
  1924. BOOST_MOVE_CONVERSION_AWARE_CATCH_1ARG(insert, T, iterator, priv_insert, const_iterator, const_iterator)
  1925. #endif
  1926. //! <b>Requires</b>: pos must be a valid iterator of *this.
  1927. //!
  1928. //! <b>Effects</b>: Insert n copies of x before pos.
  1929. //!
  1930. //! <b>Returns</b>: an iterator to the first inserted element or pos if n is 0.
  1931. //!
  1932. //! <b>Throws</b>: If memory allocation throws or T's copy constructor throws.
  1933. //!
  1934. //! <b>Complexity</b>: Linear to n.
  1935. inline iterator insert(const_iterator pos, size_type n, const value_type& x)
  1936. {
  1937. BOOST_ASSERT(this->priv_in_range_or_end(pos));
  1938. dtl::insert_n_copies_proxy<ValAllocator> proxy(x);
  1939. return this->priv_insert_aux_impl(pos, n, proxy);
  1940. }
  1941. //! <b>Requires</b>: pos must be a valid iterator of *this.
  1942. //!
  1943. //! <b>Effects</b>: Insert a copy of the [first, last) range before pos.
  1944. //!
  1945. //! <b>Returns</b>: an iterator to the first inserted element or pos if first == last.
  1946. //!
  1947. //! <b>Throws</b>: If memory allocation throws, T's constructor from a
  1948. //! dereferenced InIt throws or T's copy constructor throws.
  1949. //!
  1950. //! <b>Complexity</b>: Linear to distance [first, last).
  1951. template <class InIt>
  1952. iterator insert(const_iterator pos, InIt first, InIt last
  1953. #if !defined(BOOST_CONTAINER_DOXYGEN_INVOKED)
  1954. , typename dtl::disable_if_or
  1955. < void
  1956. , dtl::is_convertible<InIt, size_type>
  1957. , dtl::is_not_input_iterator<InIt>
  1958. >::type * = 0
  1959. #endif
  1960. )
  1961. {
  1962. BOOST_ASSERT(this->priv_in_range_or_end(pos));
  1963. size_type n = 0;
  1964. iterator it(pos.unconst());
  1965. for(;first != last; ++first, ++n){
  1966. it = this->emplace(it, *first);
  1967. ++it;
  1968. }
  1969. it -= difference_type(n);
  1970. return it;
  1971. }
  1972. #if !defined(BOOST_NO_CXX11_HDR_INITIALIZER_LIST)
  1973. //! <b>Requires</b>: pos must be a valid iterator of *this.
  1974. //!
  1975. //! <b>Effects</b>: Insert a copy of the [il.begin(), il.end()) range before pos.
  1976. //!
  1977. //! <b>Returns</b>: an iterator to the first inserted element or pos if il.begin() == il.end().
  1978. //!
  1979. //! <b>Throws</b>: If memory allocation throws, T's constructor from a
  1980. //! dereferenced std::initializer_list throws or T's copy constructor throws.
  1981. //!
  1982. //! <b>Complexity</b>: Linear to distance [il.begin(), il.end()).
  1983. inline iterator insert(const_iterator pos, std::initializer_list<value_type> il)
  1984. {
  1985. //Range check os pos is done in insert()
  1986. return insert(pos, il.begin(), il.end());
  1987. }
  1988. #endif
  1989. #if !defined(BOOST_CONTAINER_DOXYGEN_INVOKED)
  1990. template <class FwdIt>
  1991. inline iterator insert(const_iterator p, FwdIt first, FwdIt last
  1992. #if !defined(BOOST_CONTAINER_DOXYGEN_INVOKED)
  1993. , typename dtl::disable_if_or
  1994. < void
  1995. , dtl::is_convertible<FwdIt, size_type>
  1996. , dtl::is_input_iterator<FwdIt>
  1997. >::type * = 0
  1998. #endif
  1999. )
  2000. {
  2001. BOOST_ASSERT(this->priv_in_range_or_end(p));
  2002. typedef typename iter_size<FwdIt>::type it_size_type;
  2003. const it_size_type sz = boost::container::iterator_udistance(first, last);
  2004. if (BOOST_UNLIKELY(sz > size_type(-1))){
  2005. boost::container::throw_length_error("vector::insert, FwdIt's max length reached");
  2006. }
  2007. const size_type n = static_cast<size_type>(sz);
  2008. dtl::insert_range_proxy<ValAllocator, FwdIt> proxy(first);
  2009. return this->priv_insert_aux_impl( p, n, proxy);
  2010. }
  2011. #endif
  2012. //! <b>Effects</b>: Removes the first element from the deque.
  2013. //!
  2014. //! <b>Throws</b>: Nothing.
  2015. //!
  2016. //! <b>Complexity</b>: Constant time.
  2017. void pop_front() BOOST_NOEXCEPT_OR_NOTHROW
  2018. {
  2019. BOOST_ASSERT(!this->empty());
  2020. if (BOOST_LIKELY(this->prot_pop_front_simple_available())) {
  2021. allocator_traits_type::destroy
  2022. ( this->alloc()
  2023. , boost::movelib::to_raw_pointer(this->prot_start_cur_unchecked())
  2024. );
  2025. this->prot_inc_start();
  2026. }
  2027. else
  2028. this->priv_pop_front_aux();
  2029. }
  2030. //! <b>Effects</b>: Removes the last element from the deque.
  2031. //!
  2032. //! <b>Throws</b>: Nothing.
  2033. //!
  2034. //! <b>Complexity</b>: Constant time.
  2035. void pop_back() BOOST_NOEXCEPT_OR_NOTHROW
  2036. {
  2037. BOOST_ASSERT(!this->empty());
  2038. if (BOOST_LIKELY(this->prot_pop_back_simple_available())) {
  2039. this->prot_dec_finish();
  2040. allocator_traits_type::destroy
  2041. ( this->alloc()
  2042. , boost::movelib::to_raw_pointer(this->prot_finish_cur_unchecked())
  2043. );
  2044. }
  2045. else
  2046. this->priv_pop_back_aux();
  2047. }
  2048. //! <b>Effects</b>: Erases the element at p.
  2049. //!
  2050. //! <b>Throws</b>: Nothing.
  2051. //!
  2052. //! <b>Complexity</b>: Linear to the elements between pos and the
  2053. //! last element (if pos is near the end) or the first element
  2054. //! if(pos is near the beginning).
  2055. //! Constant if pos is the first or the last element.
  2056. iterator erase(const_iterator pos) BOOST_NOEXCEPT_OR_NOTHROW
  2057. {
  2058. BOOST_ASSERT(this->priv_in_range(pos));
  2059. iterator next = pos.unconst();
  2060. ++next;
  2061. const size_type index = this->prot_it_to_start_off(pos);
  2062. const size_type sz = this->prot_size();
  2063. if (index < sz/2u) {
  2064. this->priv_segmented_move_backward_n(pos.unconst(), index, next);
  2065. pop_front();
  2066. return next;
  2067. }
  2068. else {
  2069. this->priv_segmented_move_n(next, size_type(sz - size_type(index + 1u)), pos.unconst());
  2070. pop_back();
  2071. return pos.unconst();
  2072. }
  2073. }
  2074. //! <b>Effects</b>: Erases the elements pointed by [first, last).
  2075. //!
  2076. //! <b>Throws</b>: Nothing.
  2077. //!
  2078. //! <b>Complexity</b>: Linear to the distance between first and
  2079. //! last plus the elements between pos and the
  2080. //! last element (if pos is near the end) or the first element
  2081. //! if(pos is near the beginning).
  2082. iterator erase(const_iterator first, const_iterator last) BOOST_NOEXCEPT_OR_NOTHROW
  2083. {
  2084. BOOST_ASSERT(first == last ||
  2085. (first < last && this->priv_in_range(first) && this->priv_in_range_or_end(last)));
  2086. const size_type n = static_cast<size_type>(last - first);
  2087. if (n == this->prot_size()) {
  2088. this->clear();
  2089. return this->end();
  2090. }
  2091. else {
  2092. const size_type elems_before = this->prot_it_to_start_off(first);
  2093. const size_type elems_after = size_type(this->prot_size() - n - elems_before);
  2094. if (elems_before < elems_after) {
  2095. const iterator old_start = this->begin();
  2096. iterator new_start = this->priv_segmented_move_backward_n(first.unconst(), elems_before, last.unconst());
  2097. this->prot_destroy_range(old_start, new_start);
  2098. this->prot_deallocate_nodes_if_not_reservable(old_start.get_node(), new_start.m_node);
  2099. this->prot_inc_start(n);
  2100. }
  2101. else {
  2102. const iterator old_finish = this->end();
  2103. iterator new_finish = this->priv_segmented_move_n(last.unconst(), elems_after, first.unconst());
  2104. this->prot_destroy_range(new_finish, old_finish);
  2105. this->prot_deallocate_nodes_if_not_reservable(new_finish.m_node + 1, old_finish.get_node() + 1);
  2106. this->prot_dec_finish(n);
  2107. }
  2108. return this->nth(elems_before);
  2109. }
  2110. }
  2111. //! <b>Effects</b>: Swaps the contents of *this and x.
  2112. //!
  2113. //! <b>Throws</b>: Nothing.
  2114. //!
  2115. //! <b>Complexity</b>: Constant.
  2116. inline void swap(deque &x)
  2117. BOOST_NOEXCEPT_IF(allocator_traits_type::propagate_on_container_swap::value
  2118. || allocator_traits_type::is_always_equal::value)
  2119. {
  2120. this->swap_members(x);
  2121. dtl::bool_<allocator_traits_type::propagate_on_container_swap::value> flag;
  2122. dtl::swap_alloc(this->alloc(), x.alloc(), flag);
  2123. dtl::swap_alloc(this->ptr_alloc(), x.ptr_alloc(), flag);
  2124. }
  2125. //! <b>Effects</b>: Erases all the elements of the deque.
  2126. //!
  2127. //! <b>Throws</b>: Nothing.
  2128. //!
  2129. //! <b>Complexity</b>: Linear to the number of elements in the deque.
  2130. void clear() BOOST_NOEXCEPT_OR_NOTHROW
  2131. {
  2132. if (!this->empty()) {
  2133. const iterator start = this->prot_start();
  2134. const iterator finish = this->prot_finish();
  2135. const index_pointer start_node = start.get_node();
  2136. const index_pointer finish_node = finish.get_node();
  2137. for (index_pointer node = start_node + 1; node < finish_node; ++node) {
  2138. this->prot_destroy_range(*node, *node + get_block_ssize());
  2139. this->prot_deallocate_node_if_not_reservable(*node);
  2140. }
  2141. if (start_node != finish_node) {
  2142. this->prot_destroy_range(start.get_cur(), start.get_last());
  2143. this->prot_destroy_range(finish.get_first(), finish.get_cur());
  2144. this->prot_deallocate_node_if_not_reservable(finish.get_first());
  2145. }
  2146. else
  2147. this->prot_destroy_range(start.get_cur(), finish.get_cur());
  2148. this->prot_reset_finish_to_start();
  2149. }
  2150. }
  2151. //! <b>Effects</b>: Returns true if x and y are equal
  2152. //!
  2153. //! <b>Complexity</b>: Linear to the number of elements in the container.
  2154. BOOST_CONTAINER_ATTRIBUTE_NODISCARD inline
  2155. friend bool operator==(const deque& x, const deque& y)
  2156. { return x.size() == y.size() && ::boost::container::algo_equal(x.begin(), x.end(), y.begin()); }
  2157. //! <b>Effects</b>: Returns true if x and y are unequal
  2158. //!
  2159. //! <b>Complexity</b>: Linear to the number of elements in the container.
  2160. BOOST_CONTAINER_ATTRIBUTE_NODISCARD inline
  2161. friend bool operator!=(const deque& x, const deque& y)
  2162. { return !(x == y); }
  2163. //! <b>Effects</b>: Returns true if x is less than y
  2164. //!
  2165. //! <b>Complexity</b>: Linear to the number of elements in the container.
  2166. BOOST_CONTAINER_ATTRIBUTE_NODISCARD inline
  2167. friend bool operator<(const deque& x, const deque& y)
  2168. { return ::boost::container::algo_lexicographical_compare(x.begin(), x.end(), y.begin(), y.end()); }
  2169. //! <b>Effects</b>: Returns true if x is greater than y
  2170. //!
  2171. //! <b>Complexity</b>: Linear to the number of elements in the container.
  2172. BOOST_CONTAINER_ATTRIBUTE_NODISCARD inline
  2173. friend bool operator>(const deque& x, const deque& y)
  2174. { return y < x; }
  2175. //! <b>Effects</b>: Returns true if x is equal or less than y
  2176. //!
  2177. //! <b>Complexity</b>: Linear to the number of elements in the container.
  2178. BOOST_CONTAINER_ATTRIBUTE_NODISCARD inline
  2179. friend bool operator<=(const deque& x, const deque& y)
  2180. { return !(y < x); }
  2181. //! <b>Effects</b>: Returns true if x is equal or greater than y
  2182. //!
  2183. //! <b>Complexity</b>: Linear to the number of elements in the container.
  2184. BOOST_CONTAINER_ATTRIBUTE_NODISCARD inline
  2185. friend bool operator>=(const deque& x, const deque& y)
  2186. { return !(x < y); }
  2187. //! <b>Effects</b>: x.swap(y)
  2188. //!
  2189. //! <b>Complexity</b>: Constant.
  2190. inline friend void swap(deque& x, deque& y)
  2191. BOOST_NOEXCEPT_IF(BOOST_NOEXCEPT(x.swap(y)))
  2192. { x.swap(y); }
  2193. #ifndef BOOST_CONTAINER_DOXYGEN_INVOKED
  2194. private:
  2195. template <class InsertProxy>
  2196. void priv_assign(size_type n, InsertProxy proxy)
  2197. {
  2198. const size_type sz = this->prot_size();
  2199. this->priv_segmented_proxy_copy_n_and_update(this->begin(), sz < n ? sz : n, proxy);
  2200. if (n > sz) {
  2201. this->priv_insert_back_aux_impl(size_type(n - sz), proxy);
  2202. }
  2203. else{
  2204. this->priv_erase_last_n(size_type(sz - n));
  2205. }
  2206. }
  2207. void priv_move_assign(BOOST_RV_REF(deque) x, dtl::bool_<true> /*steal_resources*/)
  2208. {
  2209. //Destroy objects but retain memory in case x reuses it in the future
  2210. this->clear();
  2211. //Move allocator if needed
  2212. dtl::bool_<allocator_traits_type::propagate_on_container_move_assignment::value> flag;
  2213. dtl::move_alloc(this->alloc(), x.alloc(), flag);
  2214. dtl::move_alloc(this->ptr_alloc(), x.ptr_alloc(), flag);
  2215. //Nothrow swap
  2216. this->swap_members(x);
  2217. }
  2218. void priv_move_assign(BOOST_RV_REF(deque) x, dtl::bool_<false> /*steal_resources*/)
  2219. {
  2220. //We can't guarantee a compile-time equal allocator or propagation so fallback to runtime
  2221. //Resources can be transferred if both allocators are equal
  2222. if (this->alloc() == x.alloc()) {
  2223. this->priv_move_assign(boost::move(x), dtl::true_());
  2224. }
  2225. else {
  2226. this->assign(boost::make_move_iterator(x.begin()), boost::make_move_iterator(x.end()));
  2227. }
  2228. }
  2229. inline size_type priv_index_of(const_iterator p) const
  2230. {
  2231. BOOST_ASSERT(this->cbegin() <= p);
  2232. BOOST_ASSERT(p <= this->cend());
  2233. return this->prot_it_to_start_off(p);
  2234. }
  2235. void priv_erase_last_n(size_type n)
  2236. {
  2237. const size_type sz = this->prot_size();
  2238. BOOST_ASSERT(n <= sz);
  2239. if(sz) {
  2240. const iterator old_finish = this->prot_finish();
  2241. const iterator new_finish = old_finish - difference_type(n);
  2242. this->prot_destroy_range(new_finish, old_finish);
  2243. this->prot_deallocate_nodes_if_not_reservable(new_finish.get_node() + 1, old_finish.get_node() + 1);
  2244. this->prot_dec_finish(n);
  2245. }
  2246. }
  2247. void priv_throw_if_out_of_range(size_type n) const
  2248. {
  2249. if (n >= this->size())
  2250. throw_out_of_range("deque::at out of range");
  2251. }
  2252. inline bool priv_in_range(const_iterator pos) const
  2253. {
  2254. return (this->begin() <= pos) && (pos < this->end());
  2255. }
  2256. inline bool priv_in_range_or_end(const_iterator pos) const
  2257. {
  2258. return (this->begin() <= pos) && (pos <= this->end());
  2259. }
  2260. template <class U>
  2261. BOOST_CONTAINER_FORCEINLINE iterator priv_insert(const_iterator p, BOOST_FWD_REF(U) x)
  2262. {
  2263. return this->emplace(p, ::boost::forward<U>(x));
  2264. }
  2265. template <class U>
  2266. BOOST_CONTAINER_FORCEINLINE void priv_push_back(BOOST_FWD_REF(U) u)
  2267. {
  2268. this->emplace_back(::boost::forward<U>(u));
  2269. }
  2270. template <class U>
  2271. BOOST_CONTAINER_FORCEINLINE void priv_push_front(BOOST_FWD_REF(U) u)
  2272. {
  2273. this->emplace_front(::boost::forward<U>(u));
  2274. }
  2275. void prot_destroy_range(iterator start, iterator finish)
  2276. {
  2277. (void)start; (void)finish;
  2278. BOOST_IF_CONSTEXPR(!Base::traits_t::trivial_dctr){
  2279. const index_pointer start_node = start.get_node();
  2280. const index_pointer finish_node = finish.get_node();
  2281. //In a sane deque both should be null or non-null
  2282. BOOST_ASSERT(!start_node == !finish_node);
  2283. if(start_node){
  2284. for (index_pointer node = start_node + 1; node < finish_node; ++node) {
  2285. this->prot_destroy_range(*node, *node + get_block_ssize());
  2286. }
  2287. if (start_node != finish_node) {
  2288. this->prot_destroy_range(start.get_cur(), start.get_last());
  2289. this->prot_destroy_range(finish.get_first(), finish.get_cur());
  2290. }
  2291. else
  2292. this->prot_destroy_range(start.get_cur(), finish.get_cur());
  2293. }
  2294. }
  2295. }
  2296. void prot_destroy_range(pointer p, pointer p2)
  2297. {
  2298. (void)p; (void)p2;
  2299. BOOST_IF_CONSTEXPR(!Base::traits_t::trivial_dctr){
  2300. for(;p != p2; ++p){
  2301. allocator_traits_type::destroy(this->alloc(), boost::movelib::iterator_to_raw_pointer(p));
  2302. }
  2303. }
  2304. }
  2305. template<class InsertProxy>
  2306. iterator priv_insert_middle_aux_impl(const size_type elemsbefore, const size_type n, InsertProxy proxy)
  2307. {
  2308. typedef dtl::bool_<dtl::is_single_value_proxy<InsertProxy>::value> single_t;
  2309. BOOST_ASSERT(!single_t::value || n == 1);
  2310. const size_type length = this->prot_size();
  2311. if (elemsbefore < length / 2) {
  2312. this->priv_reserve_elements_at_front(n);
  2313. const iterator old_start = this->prot_start();
  2314. iterator new_start = old_start;
  2315. priv_itsub(new_start, n, single_t());
  2316. BOOST_ASSERT(!single_t::value || elemsbefore >= 1);
  2317. if(single_t::value || elemsbefore >= n) {
  2318. iterator start_n = old_start;
  2319. priv_itadd(start_n, n, single_t());
  2320. BOOST_CONTAINER_TRY {
  2321. this->priv_segmented_uninitialized_move_alloc_n(old_start, n, new_start, single_t());
  2322. }
  2323. BOOST_CONTAINER_CATCH(...) {
  2324. this->prot_deallocate_nodes_if_not_reservable(new_start.m_node, old_start.m_node);
  2325. BOOST_CONTAINER_RETHROW
  2326. }
  2327. BOOST_CONTAINER_CATCH_END
  2328. this->prot_dec_start(n);
  2329. iterator p = this->priv_segmented_move_n(start_n, size_type(elemsbefore - n), old_start);
  2330. this->priv_segmented_proxy_copy_n_and_update(p, n, proxy);
  2331. return p;
  2332. }
  2333. else {
  2334. const size_type mid_count = size_type(n - elemsbefore);
  2335. iterator mid_start = old_start - difference_type(mid_count);
  2336. BOOST_CONTAINER_TRY {
  2337. this->priv_segmented_proxy_uninitialized_copy_n_and_update(mid_start, mid_count, proxy);
  2338. this->prot_dec_start(mid_count);
  2339. this->priv_segmented_uninitialized_move_alloc_n(old_start, elemsbefore, new_start);
  2340. }
  2341. BOOST_CONTAINER_CATCH(...) {
  2342. this->prot_deallocate_nodes_if_not_reservable(new_start.m_node, old_start.get_node());
  2343. BOOST_CONTAINER_RETHROW
  2344. }
  2345. BOOST_CONTAINER_CATCH_END
  2346. this->prot_dec_start(size_type(n - mid_count));
  2347. this->priv_segmented_proxy_copy_n_and_update(old_start, elemsbefore, proxy);
  2348. return mid_start;
  2349. }
  2350. }
  2351. else {
  2352. this->priv_reserve_elements_at_back(n);
  2353. const iterator old_finish = this->prot_finish();
  2354. const size_type elemsafter = size_type(length - elemsbefore);
  2355. BOOST_ASSERT(!single_t::value || elemsafter >= 1);
  2356. if(single_t::value || elemsafter >= n) {
  2357. iterator finish_n = old_finish;
  2358. priv_itsub(finish_n, n, single_t());
  2359. BOOST_CONTAINER_TRY {
  2360. this->priv_segmented_uninitialized_move_alloc_n(finish_n, n, old_finish, single_t());
  2361. }
  2362. BOOST_CONTAINER_CATCH(...) {
  2363. this->prot_deallocate_nodes_if_not_reservable(old_finish.get_node() + 1, (old_finish + difference_type(n)).m_node + 1);
  2364. BOOST_CONTAINER_RETHROW
  2365. }
  2366. BOOST_CONTAINER_CATCH_END
  2367. this->prot_inc_finish(n);
  2368. const size_type move_n = size_type(elemsafter - n);
  2369. this->priv_segmented_move_backward_n(finish_n, move_n, old_finish);
  2370. finish_n -= difference_type(move_n);
  2371. this->priv_segmented_proxy_copy_n_and_update(finish_n, n, proxy);
  2372. return finish_n;
  2373. }
  2374. else {
  2375. const size_type raw_gap = size_type(n - elemsafter);
  2376. iterator pos = old_finish - difference_type(elemsafter);
  2377. BOOST_CONTAINER_TRY{
  2378. this->priv_segmented_uninitialized_move_alloc_n(pos, elemsafter, old_finish + difference_type(raw_gap));
  2379. BOOST_CONTAINER_TRY{
  2380. this->priv_segmented_proxy_copy_n_and_update(pos, elemsafter, proxy);
  2381. this->priv_segmented_proxy_uninitialized_copy_n_and_update(old_finish, raw_gap, proxy);
  2382. }
  2383. BOOST_CONTAINER_CATCH(...) {
  2384. this->prot_destroy_range(old_finish, old_finish + difference_type(elemsafter));
  2385. BOOST_CONTAINER_RETHROW
  2386. }
  2387. BOOST_CONTAINER_CATCH_END
  2388. }
  2389. BOOST_CONTAINER_CATCH(...) {
  2390. this->prot_deallocate_nodes_if_not_reservable(old_finish.get_node() + 1, (old_finish + difference_type(n)).m_node + 1);
  2391. BOOST_CONTAINER_RETHROW
  2392. }
  2393. BOOST_CONTAINER_CATCH_END
  2394. this->prot_inc_finish(n);
  2395. return pos;
  2396. }
  2397. }
  2398. }
  2399. template<class InsertProxy>
  2400. iterator priv_insert_aux_impl(const_iterator p, size_type n, InsertProxy proxy)
  2401. {
  2402. const size_type elemsbefore = this->prot_it_to_start_off(p);
  2403. if (!elemsbefore) {
  2404. return this->priv_insert_front_aux_impl(n, proxy);
  2405. }
  2406. else if (elemsbefore == this->prot_size()) {
  2407. return this->priv_insert_back_aux_impl(n, proxy);
  2408. }
  2409. else {
  2410. return this->priv_insert_middle_aux_impl(elemsbefore, n, proxy);
  2411. }
  2412. }
  2413. template <class InsertProxy>
  2414. void priv_segmented_proxy_uninitialized_copy_n_and_update(const iterator first, size_type n, InsertProxy &proxy)
  2415. {
  2416. BOOST_IF_CONSTEXPR (dtl::is_single_value_proxy<InsertProxy>::value) {
  2417. (void)n;
  2418. proxy.uninitialized_copy_n_and_update(this->alloc(), boost::movelib::to_raw_pointer(first.get_cur()), 1u);
  2419. }
  2420. else if (BOOST_LIKELY(n != 0)){ //We might initialize an empty range and current_node might be null
  2421. BOOST_CONSTEXPR_OR_CONST size_type block_size = get_block_size();
  2422. index_pointer current_node = first.get_node();
  2423. BOOST_CONTAINER_TRY{
  2424. const pointer cur = first.get_cur();
  2425. const size_type block = size_type(block_size - size_type(cur - *current_node));
  2426. size_type cnt = n < block ? n: block;
  2427. proxy.uninitialized_copy_n_and_update(this->alloc(), boost::movelib::to_raw_pointer(cur), cnt);
  2428. n = size_type(n - cnt);
  2429. while (n) {
  2430. ++current_node;
  2431. cnt = n < block_size ? n: block_size;
  2432. proxy.uninitialized_copy_n_and_update(this->alloc(), boost::movelib::to_raw_pointer(*current_node), cnt);
  2433. n = size_type(n - cnt);
  2434. }
  2435. }
  2436. BOOST_CONTAINER_CATCH(...) {
  2437. this->prot_destroy_range(first, iterator(*current_node, current_node));
  2438. BOOST_CONTAINER_RETHROW
  2439. }
  2440. BOOST_CONTAINER_CATCH_END
  2441. }
  2442. }
  2443. template <class InsertProxy>
  2444. void priv_segmented_proxy_copy_n_and_update(const iterator first, size_type n, InsertProxy &proxy)
  2445. {
  2446. BOOST_IF_CONSTEXPR (dtl::is_single_value_proxy<InsertProxy>::value) {
  2447. (void)n;
  2448. proxy.copy_n_and_update(this->alloc(), boost::movelib::to_raw_pointer(first.get_cur()), 1u);
  2449. }
  2450. else if (BOOST_LIKELY(n != 0)){ //We might assign an empty range in a default constructed deque
  2451. BOOST_CONSTEXPR_OR_CONST size_type block_size = get_block_size();
  2452. index_pointer current_node = first.get_node();
  2453. const pointer cur = first.get_cur();
  2454. const size_type block = size_type(block_size - size_type(cur - *current_node));
  2455. size_type cnt = n < block ? n: block;
  2456. proxy.copy_n_and_update(this->alloc(), boost::movelib::to_raw_pointer(cur), cnt);
  2457. n = size_type(n - cnt);
  2458. while (n) {
  2459. ++current_node;
  2460. cnt = n < block_size ? n: block_size;
  2461. proxy.copy_n_and_update(this->alloc(), boost::movelib::to_raw_pointer(*current_node), cnt);
  2462. n = size_type(n - cnt);
  2463. }
  2464. }
  2465. }
  2466. template <class InsertProxy>
  2467. void priv_insert_segmented_uninitialized_copy_n_and_update(const iterator first, size_type n, InsertProxy proxy)
  2468. {
  2469. BOOST_CONTAINER_TRY{
  2470. this->priv_segmented_proxy_uninitialized_copy_n_and_update(first, n, proxy);
  2471. }
  2472. BOOST_CONTAINER_CATCH(...) {
  2473. this->prot_deallocate_nodes_if_not_reservable(first.get_node() + 1, (first+difference_type(n)).get_node() + 1);
  2474. BOOST_CONTAINER_RETHROW
  2475. }
  2476. BOOST_CONTAINER_CATCH_END
  2477. }
  2478. template<class It>
  2479. static BOOST_CONTAINER_FORCEINLINE void priv_itadd(It &it, size_type, dtl::bool_<true> /*single element*/)
  2480. { ++it; }
  2481. template<class It>
  2482. static BOOST_CONTAINER_FORCEINLINE void priv_itadd(It &it, size_type n, dtl::bool_<false> /*!single element*/)
  2483. { it += difference_type(n); }
  2484. template<class It>
  2485. static BOOST_CONTAINER_FORCEINLINE void priv_itsub(It &it, size_type, dtl::bool_<true> /*single element*/)
  2486. { --it; }
  2487. template<class It>
  2488. static BOOST_CONTAINER_FORCEINLINE void priv_itsub(It &it, size_type n, dtl::bool_<false> /*!single element*/)
  2489. { it -= difference_type(n); }
  2490. void priv_segmented_uninitialized_move_alloc_n(iterator first, size_type n, iterator dest, dtl::bool_<true> /*single element*/)
  2491. {
  2492. BOOST_ASSERT(n == 1); (void)n;
  2493. allocator_traits_type::construct
  2494. ( this->alloc()
  2495. , boost::movelib::to_raw_pointer(dest.get_cur())
  2496. , boost::move(*first));
  2497. }
  2498. void priv_segmented_uninitialized_move_alloc_n(iterator first, size_type n, iterator dest, dtl::bool_<false> /*!single element*/)
  2499. {
  2500. if (BOOST_LIKELY(n != 0)) { //Check for empty range, current_node might be null
  2501. BOOST_CONSTEXPR_OR_CONST size_type block_size = get_block_size();
  2502. index_pointer current_node = first.get_node();
  2503. BOOST_CONTAINER_TRY{
  2504. const pointer cur = first.get_cur();
  2505. const size_type block = size_type(block_size - size_type(cur - *current_node));
  2506. size_type cnt = n < block ? n: block;
  2507. dest = ::boost::container::uninitialized_move_alloc_n(this->alloc(), boost::movelib::to_raw_pointer(cur), cnt, dest);
  2508. n = size_type(n - cnt);
  2509. while (n) {
  2510. ++current_node;
  2511. cnt = n < block_size ? n: block_size;
  2512. dest = ::boost::container::uninitialized_move_alloc_n(this->alloc(), boost::movelib::to_raw_pointer(*current_node), cnt, dest);
  2513. n = size_type(n - cnt);
  2514. }
  2515. }
  2516. BOOST_CONTAINER_CATCH(...) {
  2517. this->prot_destroy_range(first, iterator(*current_node, current_node));
  2518. BOOST_CONTAINER_RETHROW
  2519. }
  2520. BOOST_CONTAINER_CATCH_END
  2521. }
  2522. }
  2523. BOOST_CONTAINER_FORCEINLINE void priv_segmented_uninitialized_move_alloc_n(iterator first, size_type n, iterator dest)
  2524. {
  2525. this->priv_segmented_uninitialized_move_alloc_n(first, n, dest, dtl::bool_<false>());
  2526. }
  2527. void priv_segmented_uninitialized_copy_alloc_n(const_iterator first, size_type n, iterator dest)
  2528. {
  2529. if (BOOST_LIKELY(n != 0)) { //We might initialize an empty range and current_node might be null
  2530. BOOST_CONSTEXPR_OR_CONST size_type block_size = get_block_size();
  2531. index_pointer current_node = first.get_node();
  2532. BOOST_CONTAINER_TRY{
  2533. const pointer cur = first.get_cur();
  2534. const size_type block = size_type(block_size - size_type(cur - *current_node));
  2535. size_type cnt = n < block ? n: block;
  2536. dest = ::boost::container::uninitialized_copy_alloc_n(this->alloc(), boost::movelib::to_raw_pointer(cur), cnt, dest);
  2537. n = size_type(n - cnt);
  2538. while (n) {
  2539. ++current_node;
  2540. cnt = n < block_size ? n: block_size;
  2541. dest = ::boost::container::uninitialized_copy_alloc_n(this->alloc(), boost::movelib::to_raw_pointer(*current_node), cnt, dest);
  2542. n = size_type(n - cnt);
  2543. }
  2544. }
  2545. BOOST_CONTAINER_CATCH(...) {
  2546. this->prot_destroy_range(first.unconst(), iterator(*current_node, current_node));
  2547. BOOST_CONTAINER_RETHROW
  2548. }
  2549. BOOST_CONTAINER_CATCH_END
  2550. }
  2551. }
  2552. static iterator priv_segmented_move_n(const_iterator first, size_type n, iterator dest)
  2553. {
  2554. index_pointer current_node = first.get_node();
  2555. BOOST_ASSERT(current_node != index_pointer());
  2556. const pointer cur = first.get_cur();
  2557. BOOST_CONSTEXPR_OR_CONST size_type block_size = get_block_size();
  2558. const size_type block = size_type(block_size - size_type(cur - *current_node));
  2559. size_type cnt = n < block ? n: block;
  2560. dest = ::boost::container::move_n(boost::movelib::to_raw_pointer(cur), cnt, dest);
  2561. n = size_type(n - cnt);
  2562. while (n) {
  2563. ++current_node;
  2564. cnt = n < block_size ? n: block_size;
  2565. dest = ::boost::container::move_n(boost::movelib::to_raw_pointer(*current_node), cnt, dest);
  2566. n = size_type(n - cnt);
  2567. }
  2568. return dest;
  2569. }
  2570. static iterator priv_segmented_move_backward_n(iterator last, size_type n, iterator dest_last)
  2571. {
  2572. index_pointer current_node = last.get_node();
  2573. BOOST_ASSERT(current_node != index_pointer());
  2574. const pointer cur = last.get_cur();
  2575. const size_type block = size_type(cur - *current_node);
  2576. size_type cnt = n < block ? n: block;
  2577. dest_last = ::boost::container::move_backward_n(boost::movelib::to_raw_pointer(cur), cnt, dest_last);
  2578. n = size_type(n - cnt);
  2579. BOOST_CONSTEXPR_OR_CONST size_type block_size = get_block_size();
  2580. while (n) {
  2581. --current_node;
  2582. cnt = n < block_size ? n: block_size;
  2583. dest_last = ::boost::container::move_backward_n(boost::movelib::to_raw_pointer(*current_node + difference_type(block_size)), cnt, dest_last);
  2584. n = size_type(n - cnt);
  2585. }
  2586. return dest_last;
  2587. }
  2588. template <class InsertProxy>
  2589. iterator priv_insert_back_aux_impl(size_type n, InsertProxy proxy)
  2590. {
  2591. this->priv_reserve_elements_at_back(n);
  2592. const iterator old_finish = this->prot_finish();
  2593. this->priv_insert_segmented_uninitialized_copy_n_and_update(old_finish, n, proxy);
  2594. this->prot_inc_finish(n);
  2595. return old_finish;
  2596. }
  2597. template <class InsertProxy>
  2598. iterator priv_insert_front_aux_impl(size_type n, InsertProxy proxy)
  2599. {
  2600. this->priv_reserve_elements_at_front(n);
  2601. const iterator old_start = this->prot_start();
  2602. const iterator new_start = old_start - difference_type(n);
  2603. this->priv_insert_segmented_uninitialized_copy_n_and_update(new_start, n, proxy);
  2604. this->prot_dec_start(n);
  2605. return new_start;
  2606. }
  2607. // Precondition: this->prot_start() and this->prot_finish() have already been initialized,
  2608. // but none of the deque's elements have yet been constructed.
  2609. void priv_fill_initialize(size_type n, const value_type& value)
  2610. {
  2611. dtl::insert_n_copies_proxy<ValAllocator> proxy(value);
  2612. this->priv_segmented_proxy_uninitialized_copy_n_and_update(this->begin(), n, proxy);
  2613. }
  2614. template <class InIt>
  2615. void priv_range_initialize(InIt first, InIt last, typename iterator_enable_if_tag<InIt, std::input_iterator_tag>::type* =0)
  2616. {
  2617. BOOST_CONTAINER_TRY {
  2618. for ( ; first != last; ++first)
  2619. this->emplace_back(*first);
  2620. }
  2621. BOOST_CONTAINER_CATCH(...){
  2622. this->clear();
  2623. BOOST_CONTAINER_RETHROW
  2624. }
  2625. BOOST_CONTAINER_CATCH_END
  2626. }
  2627. template <class FwdIt>
  2628. void priv_range_initialize(FwdIt first, FwdIt last, typename iterator_disable_if_tag<FwdIt, std::input_iterator_tag>::type* =0)
  2629. {
  2630. typedef typename iter_size<FwdIt>::type it_size_type;
  2631. const it_size_type sz = boost::container::iterator_udistance(first, last);
  2632. if (BOOST_UNLIKELY(sz > size_type(-1))){
  2633. boost::container::throw_length_error("vector::insert, FwdIt's max length reached");
  2634. }
  2635. const size_type n = static_cast<size_type>(sz);
  2636. this->prot_initialize_map_and_nodes(n);
  2637. dtl::insert_range_proxy<ValAllocator, FwdIt> proxy(first);
  2638. this->priv_segmented_proxy_uninitialized_copy_n_and_update(this->begin(), n, proxy);
  2639. }
  2640. // Called only if this->prot_finish_cur() == this->prot_finish().get_first().
  2641. void priv_pop_back_aux() BOOST_NOEXCEPT_OR_NOTHROW
  2642. {
  2643. index_pointer ip = this->prot_finish_node();
  2644. this->prot_deallocate_node_if_not_reservable(*ip);
  2645. this->prot_dec_finish();
  2646. --ip;
  2647. allocator_traits_type::destroy
  2648. ( this->alloc()
  2649. , boost::movelib::to_raw_pointer(this->prot_node_last(ip))
  2650. );
  2651. }
  2652. // Called only if this->prot_start_cur() == this->prot_start().get_last() - 1. Note that
  2653. // if the deque has at least one element (a precondition for this member
  2654. // function), and if this->prot_start_cur() == this->prot_start().get_last(), then the deque
  2655. // must have at least two nodes.
  2656. void priv_pop_front_aux() BOOST_NOEXCEPT_OR_NOTHROW
  2657. {
  2658. const index_pointer ip = this->prot_start_node();
  2659. allocator_traits_type::destroy
  2660. ( this->alloc()
  2661. , boost::movelib::to_raw_pointer(this->prot_node_last(ip))
  2662. );
  2663. this->prot_deallocate_node_if_not_reservable(*ip);
  2664. this->prot_inc_start();
  2665. }
  2666. void priv_reserve_elements_at_front(size_type n)
  2667. {
  2668. const size_type vacancies = this->prot_front_free_capacity();
  2669. typedef dtl::bool_<is_reservable> res_t;
  2670. if (n > vacancies){ //n == 0 handled in the else part
  2671. if(this->members_.m_map){
  2672. this->prot_reallocate_map_and_nodes(res_t(), size_type(n - vacancies), true);
  2673. }
  2674. else {
  2675. this->prot_initialize_map_and_nodes(n);
  2676. this->prot_reset_start_to_finish();
  2677. }
  2678. }
  2679. }
  2680. void priv_reserve_elements_at_back(size_type n)
  2681. {
  2682. const size_type vacancies = this->prot_back_free_capacity();
  2683. typedef dtl::bool_<is_reservable> res_t;
  2684. if (n > vacancies){ //n == 0 handled in the else part
  2685. if(this->members_.m_map){
  2686. this->prot_reallocate_map_and_nodes(res_t(), size_type(n - vacancies), false);
  2687. }
  2688. else{
  2689. this->prot_initialize_map_and_nodes(n);
  2690. this->prot_reset_finish_to_start();
  2691. }
  2692. }
  2693. }
  2694. #endif //#ifndef BOOST_CONTAINER_DOXYGEN_INVOKED
  2695. };
  2696. #ifndef BOOST_CONTAINER_NO_CXX17_CTAD
  2697. template <typename InputIterator>
  2698. deque(InputIterator, InputIterator) -> deque<typename iterator_traits<InputIterator>::value_type>;
  2699. template <typename InputIterator, typename Allocator>
  2700. deque(InputIterator, InputIterator, Allocator const&) -> deque<typename iterator_traits<InputIterator>::value_type, Allocator>;
  2701. #endif
  2702. //! <b>Effects</b>: Erases all elements that compare equal to v from the container c.
  2703. //!
  2704. //! <b>Complexity</b>: Linear.
  2705. template <class T, class A, class O, class U>
  2706. inline typename deque<T, A, O>::size_type erase(deque<T, A, O>& c, const U& v)
  2707. {
  2708. typename deque<T, A, O>::size_type old_size = c.size();
  2709. c.erase(boost::container::remove(c.begin(), c.end(), v), c.end());
  2710. return old_size - c.size();
  2711. }
  2712. //! <b>Effects</b>: Erases all elements that satisfy the predicate pred from the container c.
  2713. //!
  2714. //! <b>Complexity</b>: Linear.
  2715. template <class T, class A, class O, class Pred>
  2716. inline typename deque<T, A, O>::size_type erase_if(deque<T, A, O>& c, Pred pred)
  2717. {
  2718. typename deque<T, A, O>::size_type old_size = c.size();
  2719. c.erase(boost::container::remove_if(c.begin(), c.end(), pred), c.end());
  2720. return old_size - c.size();
  2721. }
  2722. } //namespace container
  2723. } //namespace boost
  2724. #ifndef BOOST_CONTAINER_DOXYGEN_INVOKED
  2725. namespace boost {
  2726. //!has_trivial_destructor_after_move<> == true_type
  2727. //!specialization for optimizations
  2728. template <class T, class Allocator, class Options>
  2729. struct has_trivial_destructor_after_move<boost::container::deque<T, Allocator, Options> >
  2730. {
  2731. typedef typename boost::container::deque<T, Allocator, Options>::allocator_type allocator_type;
  2732. typedef typename ::boost::container::allocator_traits<allocator_type>::pointer pointer;
  2733. BOOST_STATIC_CONSTEXPR bool value = ::boost::has_trivial_destructor_after_move<allocator_type>::value &&
  2734. ::boost::has_trivial_destructor_after_move<pointer>::value;
  2735. };
  2736. }
  2737. #endif //#ifndef BOOST_CONTAINER_DOXYGEN_INVOKED
  2738. #include <boost/container/detail/config_end.hpp>
  2739. #endif // #ifndef BOOST_CONTAINER_DEQUE_HPP