deque.hpp 86 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374
  1. //////////////////////////////////////////////////////////////////////////////
  2. //
  3. // (C) Copyright Ion Gaztanaga 2005-2015. 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. // move
  39. #include <boost/move/adl_move_swap.hpp>
  40. #include <boost/move/iterator.hpp>
  41. #include <boost/move/traits.hpp>
  42. #include <boost/move/utility_core.hpp>
  43. // move/detail
  44. #if defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES)
  45. #include <boost/move/detail/fwd_macros.hpp>
  46. #endif
  47. #include <boost/move/detail/move_helpers.hpp>
  48. // other
  49. #include <boost/assert.hpp>
  50. // std
  51. #include <cstddef>
  52. #if !defined(BOOST_NO_CXX11_HDR_INITIALIZER_LIST)
  53. #include <initializer_list>
  54. #endif
  55. namespace boost {
  56. namespace container {
  57. #ifndef BOOST_CONTAINER_DOXYGEN_INVOKED
  58. template <class T, class Allocator, class Options>
  59. class deque;
  60. template <class T>
  61. struct deque_value_traits
  62. {
  63. typedef T value_type;
  64. static const bool trivial_dctr = dtl::is_trivially_destructible<value_type>::value;
  65. static const 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>
  68. struct deque_block_size
  69. {
  70. BOOST_CONTAINER_STATIC_ASSERT_MSG(!(BlockBytes && BlockSize), "BlockBytes and BlockSize can't be specified at the same time");
  71. static const std::size_t block_bytes = BlockBytes ? BlockBytes : 512u;
  72. static const std::size_t value = BlockSize ? BlockSize : (sizeof(T) < block_bytes ? (block_bytes/sizeof(T)) : std::size_t(1));
  73. };
  74. namespace dtl {
  75. // Class invariants:
  76. // For any nonsingular iterator i:
  77. // i.node is the address of an element in the map array. The
  78. // contents of i.node is a pointer to the beginning of a node.
  79. // i.first == //(i.node)
  80. // i.last == i.first + node_size
  81. // i.cur is a pointer in the range [i.first, i.last). NOTE:
  82. // the implication of this is that i.cur is always a dereferenceable
  83. // pointer, even if i is a past-the-end iterator.
  84. // Start and Finish are always nonsingular iterators. NOTE: this means
  85. // that an empty deque must have one node, and that a deque
  86. // with N elements, where N is the buffer size, must have two nodes.
  87. // For every node other than start.node and finish.node, every element
  88. // in the node is an initialized object. If start.node == finish.node,
  89. // then [start.cur, finish.cur) are initialized objects, and
  90. // the elements outside that range are uninitialized storage. Otherwise,
  91. // [start.cur, start.last) and [finish.first, finish.cur) are initialized
  92. // objects, and [start.first, start.cur) and [finish.cur, finish.last)
  93. // are uninitialized storage.
  94. // [map, map + map_size) is a valid, non-empty range.
  95. // [start.node, finish.node] is a valid range contained within
  96. // [map, map + map_size).
  97. // A pointer in the range [map, map + map_size) points to an allocated node
  98. // if and only if the pointer is in the range [start.node, finish.node].
  99. template<class Pointer, bool IsConst>
  100. class deque_iterator
  101. {
  102. public:
  103. typedef std::random_access_iterator_tag iterator_category;
  104. typedef typename boost::intrusive::pointer_traits<Pointer>::element_type value_type;
  105. typedef typename boost::intrusive::pointer_traits<Pointer>::difference_type difference_type;
  106. typedef typename boost::intrusive::pointer_traits<Pointer>::size_type size_type;
  107. typedef typename if_c
  108. < IsConst
  109. , typename boost::intrusive::pointer_traits<Pointer>::template
  110. rebind_pointer<const value_type>::type
  111. , Pointer
  112. >::type pointer;
  113. typedef typename if_c
  114. < IsConst
  115. , const value_type&
  116. , value_type&
  117. >::type reference;
  118. class nat;
  119. typedef typename dtl::if_c< IsConst
  120. , deque_iterator<Pointer, false>
  121. , nat>::type nonconst_iterator;
  122. typedef Pointer val_alloc_ptr;
  123. typedef typename boost::intrusive::pointer_traits<Pointer>::
  124. template rebind_pointer<Pointer>::type index_pointer;
  125. Pointer m_cur;
  126. Pointer m_first;
  127. Pointer m_last;
  128. index_pointer m_node;
  129. public:
  130. BOOST_CONTAINER_ATTRIBUTE_NODISCARD inline Pointer get_cur() const { return m_cur; }
  131. BOOST_CONTAINER_ATTRIBUTE_NODISCARD inline Pointer get_first() const { return m_first; }
  132. BOOST_CONTAINER_ATTRIBUTE_NODISCARD inline Pointer get_last() const { return m_last; }
  133. BOOST_CONTAINER_ATTRIBUTE_NODISCARD inline index_pointer get_node() const { return m_node; }
  134. inline deque_iterator(val_alloc_ptr x, index_pointer y, difference_type block_size) BOOST_NOEXCEPT_OR_NOTHROW
  135. : m_cur(x), m_first(*y), m_last(*y + block_size), m_node(y)
  136. {}
  137. inline deque_iterator(val_alloc_ptr x, index_pointer y, size_type block_size) BOOST_NOEXCEPT_OR_NOTHROW
  138. : m_cur(x), m_first(*y), m_last(*y + difference_type(block_size)), m_node(y)
  139. {}
  140. inline deque_iterator() BOOST_NOEXCEPT_OR_NOTHROW
  141. : m_cur(), m_first(), m_last(), m_node() //Value initialization to achieve "null iterators" (N3644)
  142. {}
  143. inline deque_iterator(const deque_iterator& x) BOOST_NOEXCEPT_OR_NOTHROW
  144. : m_cur(x.get_cur()), m_first(x.get_first()), m_last(x.get_last()), m_node(x.get_node())
  145. {}
  146. inline deque_iterator(const nonconst_iterator& x) BOOST_NOEXCEPT_OR_NOTHROW
  147. : m_cur(x.get_cur()), m_first(x.get_first()), m_last(x.get_last()), m_node(x.get_node())
  148. {}
  149. inline deque_iterator(Pointer cur, Pointer first, Pointer last, index_pointer node) BOOST_NOEXCEPT_OR_NOTHROW
  150. : m_cur(cur), m_first(first), m_last(last), m_node(node)
  151. {}
  152. inline deque_iterator& operator=(const deque_iterator& x) BOOST_NOEXCEPT_OR_NOTHROW
  153. { m_cur = x.get_cur(); m_first = x.get_first(); m_last = x.get_last(); m_node = x.get_node(); return *this; }
  154. inline deque_iterator<Pointer, false> unconst() const BOOST_NOEXCEPT_OR_NOTHROW
  155. {
  156. return deque_iterator<Pointer, false>(this->get_cur(), this->get_first(), this->get_last(), this->get_node());
  157. }
  158. inline reference operator*() const BOOST_NOEXCEPT_OR_NOTHROW
  159. { return *this->m_cur; }
  160. inline pointer operator->() const BOOST_NOEXCEPT_OR_NOTHROW
  161. { return this->m_cur; }
  162. BOOST_CONTAINER_ATTRIBUTE_NODISCARD difference_type operator-(const deque_iterator& x) const BOOST_NOEXCEPT_OR_NOTHROW
  163. {
  164. if(!this->m_cur && !x.m_cur){
  165. return 0;
  166. }
  167. const difference_type block_size = m_last - m_first;
  168. BOOST_ASSERT(block_size);
  169. return block_size * (this->m_node - x.m_node - 1) +
  170. (this->m_cur - this->m_first) + (x.m_last - x.m_cur);
  171. }
  172. deque_iterator& operator++() BOOST_NOEXCEPT_OR_NOTHROW
  173. {
  174. BOOST_ASSERT(!!m_cur);
  175. ++this->m_cur;
  176. if (this->m_cur == this->m_last) {
  177. const difference_type block_size = m_last - m_first;
  178. BOOST_ASSERT(block_size);
  179. this->priv_set_node(this->m_node + 1, block_size);
  180. this->m_cur = this->m_first;
  181. }
  182. return *this;
  183. }
  184. inline deque_iterator operator++(int) BOOST_NOEXCEPT_OR_NOTHROW
  185. {
  186. deque_iterator tmp(*this);
  187. ++*this;
  188. return tmp;
  189. }
  190. deque_iterator& operator--() BOOST_NOEXCEPT_OR_NOTHROW
  191. {
  192. BOOST_ASSERT(!!m_cur);
  193. if (this->m_cur == this->m_first) {
  194. const difference_type block_size = m_last - m_first;
  195. BOOST_ASSERT(block_size);
  196. this->priv_set_node(this->m_node - 1, block_size);
  197. this->m_cur = this->m_last;
  198. }
  199. --this->m_cur;
  200. return *this;
  201. }
  202. inline deque_iterator operator--(int) BOOST_NOEXCEPT_OR_NOTHROW
  203. {
  204. deque_iterator tmp(*this);
  205. --*this;
  206. return tmp;
  207. }
  208. deque_iterator& operator+=(difference_type n) BOOST_NOEXCEPT_OR_NOTHROW
  209. {
  210. if (!n)
  211. return *this;
  212. BOOST_ASSERT(!!m_cur);
  213. difference_type offset = n + (this->m_cur - this->m_first);
  214. const difference_type block_size = m_last - m_first;
  215. BOOST_ASSERT(block_size);
  216. if (offset >= 0 && offset < block_size)
  217. this->m_cur += difference_type(n);
  218. else {
  219. difference_type node_offset =
  220. offset > 0 ? (offset / block_size)
  221. : (-difference_type((-offset - 1) / block_size) - 1);
  222. this->priv_set_node(this->m_node + node_offset, size_type(block_size));
  223. this->m_cur = this->m_first +
  224. (offset - node_offset * block_size);
  225. }
  226. return *this;
  227. }
  228. BOOST_CONTAINER_ATTRIBUTE_NODISCARD inline
  229. deque_iterator operator+(difference_type n) const BOOST_NOEXCEPT_OR_NOTHROW
  230. { deque_iterator tmp(*this); return tmp += n; }
  231. inline
  232. deque_iterator& operator-=(difference_type n) BOOST_NOEXCEPT_OR_NOTHROW
  233. { return *this += -n; }
  234. BOOST_CONTAINER_ATTRIBUTE_NODISCARD inline
  235. deque_iterator operator-(difference_type n) const BOOST_NOEXCEPT_OR_NOTHROW
  236. { deque_iterator tmp(*this); return tmp -= n; }
  237. BOOST_CONTAINER_ATTRIBUTE_NODISCARD inline
  238. reference operator[](difference_type n) const BOOST_NOEXCEPT_OR_NOTHROW
  239. { return *(*this + n); }
  240. //Comparisons
  241. BOOST_CONTAINER_ATTRIBUTE_NODISCARD inline
  242. friend bool operator==(const deque_iterator& l, const deque_iterator& r) BOOST_NOEXCEPT_OR_NOTHROW
  243. { return l.m_cur == r.m_cur; }
  244. BOOST_CONTAINER_ATTRIBUTE_NODISCARD inline
  245. friend bool operator!=(const deque_iterator& l, const deque_iterator& r) BOOST_NOEXCEPT_OR_NOTHROW
  246. { return l.m_cur != r.m_cur; }
  247. BOOST_CONTAINER_ATTRIBUTE_NODISCARD inline
  248. friend bool operator<(const deque_iterator& l, const deque_iterator& r) BOOST_NOEXCEPT_OR_NOTHROW
  249. { return (l.m_node == r.m_node) ? (l.m_cur < r.m_cur) : (l.m_node < r.m_node); }
  250. BOOST_CONTAINER_ATTRIBUTE_NODISCARD inline
  251. friend bool operator>(const deque_iterator& l, const deque_iterator& r) BOOST_NOEXCEPT_OR_NOTHROW
  252. { return r < l; }
  253. BOOST_CONTAINER_ATTRIBUTE_NODISCARD inline
  254. friend bool operator<=(const deque_iterator& l, const deque_iterator& r) BOOST_NOEXCEPT_OR_NOTHROW
  255. { return !(r < l); }
  256. BOOST_CONTAINER_ATTRIBUTE_NODISCARD inline
  257. friend bool operator>=(const deque_iterator& l, const deque_iterator& r) BOOST_NOEXCEPT_OR_NOTHROW
  258. { return !(l < r); }
  259. BOOST_CONTAINER_ATTRIBUTE_NODISCARD inline
  260. friend deque_iterator operator+(difference_type n, deque_iterator x) BOOST_NOEXCEPT_OR_NOTHROW
  261. { return x += n; }
  262. inline void priv_set_node(index_pointer new_node, size_type block_size) BOOST_NOEXCEPT_OR_NOTHROW
  263. { return this->priv_set_node(new_node, difference_type(block_size)); }
  264. inline void priv_set_node(index_pointer new_node, difference_type block_size) BOOST_NOEXCEPT_OR_NOTHROW
  265. {
  266. this->m_node = new_node;
  267. this->m_first = *new_node;
  268. this->m_last = this->m_first + block_size;
  269. }
  270. };
  271. } //namespace dtl {
  272. template<class Options>
  273. struct get_deque_opt
  274. {
  275. typedef Options type;
  276. };
  277. template<>
  278. struct get_deque_opt<void>
  279. {
  280. typedef deque_null_opt type;
  281. };
  282. // Deque base class. It has two purposes. First, its constructor
  283. // and destructor allocate (but don't initialize) storage. This makes
  284. // exception safety easier.
  285. template <class Allocator, class Options>
  286. class deque_base
  287. {
  288. BOOST_COPYABLE_AND_MOVABLE(deque_base)
  289. public:
  290. typedef allocator_traits<Allocator> val_alloc_traits_type;
  291. typedef typename val_alloc_traits_type::value_type val_alloc_val;
  292. typedef typename val_alloc_traits_type::pointer val_alloc_ptr;
  293. typedef typename val_alloc_traits_type::const_pointer val_alloc_cptr;
  294. typedef typename val_alloc_traits_type::reference val_alloc_ref;
  295. typedef typename val_alloc_traits_type::const_reference val_alloc_cref;
  296. typedef typename val_alloc_traits_type::difference_type val_alloc_diff;
  297. typedef typename val_alloc_traits_type::size_type val_alloc_size;
  298. typedef typename val_alloc_traits_type::template
  299. portable_rebind_alloc<val_alloc_ptr>::type ptr_alloc_t;
  300. typedef allocator_traits<ptr_alloc_t> ptr_alloc_traits_type;
  301. typedef typename ptr_alloc_traits_type::value_type ptr_alloc_val;
  302. typedef typename ptr_alloc_traits_type::pointer ptr_alloc_ptr;
  303. typedef typename ptr_alloc_traits_type::const_pointer ptr_alloc_cptr;
  304. typedef typename ptr_alloc_traits_type::reference ptr_alloc_ref;
  305. typedef typename ptr_alloc_traits_type::const_reference ptr_alloc_cref;
  306. typedef Allocator allocator_type;
  307. typedef allocator_type stored_allocator_type;
  308. typedef val_alloc_size size_type;
  309. typedef val_alloc_diff difference_type;
  310. private:
  311. typedef typename get_deque_opt<Options>::type options_type;
  312. protected:
  313. typedef dtl::deque_iterator<val_alloc_ptr, false> iterator;
  314. typedef dtl::deque_iterator<val_alloc_ptr, true > const_iterator;
  315. BOOST_CONSTEXPR inline static size_type get_block_size() BOOST_NOEXCEPT_OR_NOTHROW
  316. { return deque_block_size<val_alloc_val, options_type::block_bytes, options_type::block_size>::value; }
  317. BOOST_CONSTEXPR inline static val_alloc_diff get_block_ssize() BOOST_NOEXCEPT_OR_NOTHROW
  318. { return val_alloc_diff((get_block_size)()); }
  319. typedef deque_value_traits<val_alloc_val> traits_t;
  320. typedef ptr_alloc_t map_allocator_type;
  321. inline val_alloc_ptr priv_allocate_node()
  322. { return this->alloc().allocate(get_block_size()); }
  323. inline void priv_deallocate_node(val_alloc_ptr p) BOOST_NOEXCEPT_OR_NOTHROW
  324. { this->alloc().deallocate(p, get_block_size()); }
  325. inline ptr_alloc_ptr priv_allocate_map(size_type n)
  326. { return this->ptr_alloc().allocate(n); }
  327. inline void priv_deallocate_map(ptr_alloc_ptr p, size_type n) BOOST_NOEXCEPT_OR_NOTHROW
  328. { this->ptr_alloc().deallocate(p, n); }
  329. inline deque_base(size_type num_elements, const allocator_type& a)
  330. : members_(a)
  331. { this->priv_initialize_map(num_elements); }
  332. inline explicit deque_base(const allocator_type& a)
  333. : members_(a)
  334. {}
  335. inline deque_base()
  336. : members_()
  337. {}
  338. inline explicit deque_base(BOOST_RV_REF(deque_base) x)
  339. : members_( boost::move(x.ptr_alloc())
  340. , boost::move(x.alloc()) )
  341. {}
  342. ~deque_base()
  343. {
  344. if (this->members_.m_map) {
  345. this->priv_destroy_nodes(this->members_.m_start.m_node, this->members_.m_finish.m_node + 1);
  346. this->priv_deallocate_map(this->members_.m_map, this->members_.m_map_size);
  347. }
  348. }
  349. private:
  350. deque_base(const deque_base&);
  351. protected:
  352. void swap_members(deque_base &x) BOOST_NOEXCEPT_OR_NOTHROW
  353. {
  354. ::boost::adl_move_swap(this->members_.m_start, x.members_.m_start);
  355. ::boost::adl_move_swap(this->members_.m_finish, x.members_.m_finish);
  356. ::boost::adl_move_swap(this->members_.m_map, x.members_.m_map);
  357. ::boost::adl_move_swap(this->members_.m_map_size, x.members_.m_map_size);
  358. }
  359. void priv_initialize_map(size_type num_elements)
  360. {
  361. // if(num_elements){
  362. size_type num_nodes = num_elements / get_block_size() + 1;
  363. this->members_.m_map_size = dtl::max_value((size_type) InitialMapSize, num_nodes + 2);
  364. this->members_.m_map = this->priv_allocate_map(this->members_.m_map_size);
  365. ptr_alloc_ptr nstart = this->members_.m_map + difference_type(this->members_.m_map_size - num_nodes) / 2;
  366. ptr_alloc_ptr nfinish = nstart + difference_type(num_nodes);
  367. BOOST_CONTAINER_TRY {
  368. this->priv_create_nodes(nstart, nfinish);
  369. }
  370. BOOST_CONTAINER_CATCH(...){
  371. this->priv_deallocate_map(this->members_.m_map, this->members_.m_map_size);
  372. this->members_.m_map = 0;
  373. this->members_.m_map_size = 0;
  374. BOOST_CONTAINER_RETHROW
  375. }
  376. BOOST_CONTAINER_CATCH_END
  377. this->members_.m_start.priv_set_node(nstart, get_block_size());
  378. this->members_.m_finish.priv_set_node(nfinish - 1, get_block_size());
  379. this->members_.m_start.m_cur = this->members_.m_start.m_first;
  380. this->members_.m_finish.m_cur = this->members_.m_finish.m_first + difference_type(num_elements % get_block_size());
  381. // }
  382. }
  383. void priv_create_nodes(ptr_alloc_ptr nstart, ptr_alloc_ptr nfinish)
  384. {
  385. ptr_alloc_ptr cur = nstart;
  386. BOOST_CONTAINER_TRY {
  387. for (; cur < nfinish; ++cur)
  388. *cur = this->priv_allocate_node();
  389. }
  390. BOOST_CONTAINER_CATCH(...){
  391. this->priv_destroy_nodes(nstart, cur);
  392. BOOST_CONTAINER_RETHROW
  393. }
  394. BOOST_CONTAINER_CATCH_END
  395. }
  396. void priv_destroy_nodes(ptr_alloc_ptr nstart, ptr_alloc_ptr nfinish) BOOST_NOEXCEPT_OR_NOTHROW
  397. {
  398. for (ptr_alloc_ptr n = nstart; n < nfinish; ++n)
  399. this->priv_deallocate_node(*n);
  400. }
  401. void priv_clear_map() BOOST_NOEXCEPT_OR_NOTHROW
  402. {
  403. if (this->members_.m_map) {
  404. this->priv_destroy_nodes(this->members_.m_start.m_node, this->members_.m_finish.m_node + 1);
  405. this->priv_deallocate_map(this->members_.m_map, this->members_.m_map_size);
  406. this->members_.m_map = 0;
  407. this->members_.m_map_size = 0;
  408. this->members_.m_start = iterator();
  409. this->members_.m_finish = this->members_.m_start;
  410. }
  411. }
  412. enum { InitialMapSize = 8 };
  413. protected:
  414. struct members_holder
  415. : public ptr_alloc_t
  416. , public allocator_type
  417. {
  418. members_holder()
  419. : map_allocator_type(), allocator_type()
  420. , m_map(0), m_map_size(0)
  421. , m_start(), m_finish(m_start)
  422. {}
  423. explicit members_holder(const allocator_type &a)
  424. : map_allocator_type(a), allocator_type(a)
  425. , m_map(0), m_map_size(0)
  426. , m_start(), m_finish(m_start)
  427. {}
  428. template<class ValAllocConvertible, class PtrAllocConvertible>
  429. members_holder(BOOST_FWD_REF(PtrAllocConvertible) pa, BOOST_FWD_REF(ValAllocConvertible) va)
  430. : map_allocator_type(boost::forward<PtrAllocConvertible>(pa))
  431. , allocator_type (boost::forward<ValAllocConvertible>(va))
  432. , m_map(0), m_map_size(0)
  433. , m_start(), m_finish(m_start)
  434. {}
  435. ptr_alloc_ptr m_map;
  436. val_alloc_size m_map_size;
  437. iterator m_start;
  438. iterator m_finish;
  439. } members_;
  440. inline ptr_alloc_t &ptr_alloc() BOOST_NOEXCEPT_OR_NOTHROW
  441. { return members_; }
  442. inline const ptr_alloc_t &ptr_alloc() const BOOST_NOEXCEPT_OR_NOTHROW
  443. { return members_; }
  444. inline allocator_type &alloc() BOOST_NOEXCEPT_OR_NOTHROW
  445. { return members_; }
  446. inline const allocator_type &alloc() const BOOST_NOEXCEPT_OR_NOTHROW
  447. { return members_; }
  448. };
  449. #endif //#ifndef BOOST_CONTAINER_DOXYGEN_INVOKED
  450. #ifdef BOOST_CONTAINER_DOXYGEN_INVOKED
  451. //! A double-ended queue is a sequence that supports random access to elements, constant time insertion
  452. //! and removal of elements at the end of the sequence, and linear time insertion and removal of elements in the middle.
  453. //!
  454. //! \tparam T The type of object that is stored in the deque
  455. //! \tparam A The allocator used for all internal memory management, use void
  456. //! for the default allocator
  457. //! \tparam Options A type produced from \c boost::container::deque_options.
  458. template <class T, class Allocator = void, class Options = void>
  459. #else
  460. template <class T, class Allocator, class Options>
  461. #endif
  462. class deque : protected deque_base<typename real_allocator<T, Allocator>::type, Options>
  463. {
  464. #ifndef BOOST_CONTAINER_DOXYGEN_INVOKED
  465. private:
  466. typedef deque_base<typename real_allocator<T, Allocator>::type, Options> Base;
  467. #endif //#ifndef BOOST_CONTAINER_DOXYGEN_INVOKED
  468. typedef typename real_allocator<T, Allocator>::type ValAllocator;
  469. typedef constant_iterator<T> c_it;
  470. public:
  471. //////////////////////////////////////////////
  472. //
  473. // types
  474. //
  475. //////////////////////////////////////////////
  476. typedef T value_type;
  477. typedef ValAllocator allocator_type;
  478. typedef typename ::boost::container::allocator_traits<ValAllocator>::pointer pointer;
  479. typedef typename ::boost::container::allocator_traits<ValAllocator>::const_pointer const_pointer;
  480. typedef typename ::boost::container::allocator_traits<ValAllocator>::reference reference;
  481. typedef typename ::boost::container::allocator_traits<ValAllocator>::const_reference const_reference;
  482. typedef typename ::boost::container::allocator_traits<ValAllocator>::size_type size_type;
  483. typedef typename ::boost::container::allocator_traits<ValAllocator>::difference_type difference_type;
  484. typedef BOOST_CONTAINER_IMPDEF(allocator_type) stored_allocator_type;
  485. typedef BOOST_CONTAINER_IMPDEF(typename Base::iterator) iterator;
  486. typedef BOOST_CONTAINER_IMPDEF(typename Base::const_iterator) const_iterator;
  487. typedef BOOST_CONTAINER_IMPDEF(boost::container::reverse_iterator<iterator>) reverse_iterator;
  488. typedef BOOST_CONTAINER_IMPDEF(boost::container::reverse_iterator<const_iterator>) const_reverse_iterator;
  489. #ifndef BOOST_CONTAINER_DOXYGEN_INVOKED
  490. private: // Internal typedefs
  491. BOOST_COPYABLE_AND_MOVABLE(deque)
  492. typedef typename Base::ptr_alloc_ptr index_pointer;
  493. typedef allocator_traits<ValAllocator> allocator_traits_type;
  494. #endif //#ifndef BOOST_CONTAINER_DOXYGEN_INVOKED
  495. using Base::get_block_ssize;
  496. public:
  497. using Base::get_block_size;
  498. //////////////////////////////////////////////
  499. //
  500. // construct/copy/destroy
  501. //
  502. //////////////////////////////////////////////
  503. //! <b>Effects</b>: Default constructors a deque.
  504. //!
  505. //! <b>Throws</b>: If allocator_type's default constructor throws.
  506. //!
  507. //! <b>Complexity</b>: Constant.
  508. inline deque()
  509. BOOST_NOEXCEPT_IF(dtl::is_nothrow_default_constructible<ValAllocator>::value)
  510. : Base()
  511. {}
  512. //! <b>Effects</b>: Constructs a deque taking the allocator as parameter.
  513. //!
  514. //! <b>Throws</b>: Nothing
  515. //!
  516. //! <b>Complexity</b>: Constant.
  517. inline explicit deque(const allocator_type& a) BOOST_NOEXCEPT_OR_NOTHROW
  518. : Base(a)
  519. {}
  520. //! <b>Effects</b>: Constructs a deque
  521. //! and inserts n value initialized values.
  522. //!
  523. //! <b>Throws</b>: If allocator_type's default constructor
  524. //! throws or T's value initialization throws.
  525. //!
  526. //! <b>Complexity</b>: Linear to n.
  527. inline explicit deque(size_type n)
  528. : Base(n, allocator_type())
  529. {
  530. dtl::insert_value_initialized_n_proxy<ValAllocator> proxy;
  531. proxy.uninitialized_copy_n_and_update(this->alloc(), this->begin(), n);
  532. //deque_base will deallocate in case of exception...
  533. }
  534. //! <b>Effects</b>: Constructs a deque
  535. //! and inserts n default initialized values.
  536. //!
  537. //! <b>Throws</b>: If allocator_type's default constructor
  538. //! throws or T's default initialization or copy constructor throws.
  539. //!
  540. //! <b>Complexity</b>: Linear to n.
  541. //!
  542. //! <b>Note</b>: Non-standard extension
  543. inline deque(size_type n, default_init_t)
  544. : Base(n, allocator_type())
  545. {
  546. dtl::insert_default_initialized_n_proxy<ValAllocator> proxy;
  547. proxy.uninitialized_copy_n_and_update(this->alloc(), this->begin(), n);
  548. //deque_base will deallocate in case of exception...
  549. }
  550. //! <b>Effects</b>: Constructs a deque that will use a copy of allocator a
  551. //! and inserts n value initialized values.
  552. //!
  553. //! <b>Throws</b>: If allocator_type's default constructor
  554. //! throws or T's value initialization throws.
  555. //!
  556. //! <b>Complexity</b>: Linear to n.
  557. inline explicit deque(size_type n, const allocator_type &a)
  558. : Base(n, a)
  559. {
  560. dtl::insert_value_initialized_n_proxy<ValAllocator> proxy;
  561. proxy.uninitialized_copy_n_and_update(this->alloc(), this->begin(), n);
  562. //deque_base will deallocate in case of exception...
  563. }
  564. //! <b>Effects</b>: Constructs a deque that will use a copy of allocator a
  565. //! and inserts n default initialized values.
  566. //!
  567. //! <b>Throws</b>: If allocator_type's default constructor
  568. //! throws or T's default initialization or copy constructor throws.
  569. //!
  570. //! <b>Complexity</b>: Linear to n.
  571. //!
  572. //! <b>Note</b>: Non-standard extension
  573. inline deque(size_type n, default_init_t, const allocator_type &a)
  574. : Base(n, a)
  575. {
  576. dtl::insert_default_initialized_n_proxy<ValAllocator> proxy;
  577. proxy.uninitialized_copy_n_and_update(this->alloc(), this->begin(), n);
  578. //deque_base will deallocate in case of exception...
  579. }
  580. //! <b>Effects</b>: Constructs a deque that will use a copy of allocator a
  581. //! and inserts n copies of value.
  582. //!
  583. //! <b>Throws</b>: If allocator_type's default constructor
  584. //! throws or T's copy constructor throws.
  585. //!
  586. //! <b>Complexity</b>: Linear to n.
  587. inline deque(size_type n, const value_type& value)
  588. : Base(n, allocator_type())
  589. { this->priv_fill_initialize(value); }
  590. //! <b>Effects</b>: Constructs a deque that will use a copy of allocator a
  591. //! and inserts n copies of value.
  592. //!
  593. //! <b>Throws</b>: If allocator_type's default constructor
  594. //! throws or T's copy constructor throws.
  595. //!
  596. //! <b>Complexity</b>: Linear to n.
  597. inline deque(size_type n, const value_type& value, const allocator_type& a)
  598. : Base(n, a)
  599. { this->priv_fill_initialize(value); }
  600. //! <b>Effects</b>: Constructs a deque that will use a copy of allocator a
  601. //! and inserts a copy of the range [first, last) in the deque.
  602. //!
  603. //! <b>Throws</b>: If allocator_type's default constructor
  604. //! throws or T's constructor taking a dereferenced InIt throws.
  605. //!
  606. //! <b>Complexity</b>: Linear to the range [first, last).
  607. template <class InIt>
  608. inline deque(InIt first, InIt last
  609. #if !defined(BOOST_CONTAINER_DOXYGEN_INVOKED)
  610. , typename dtl::disable_if_convertible
  611. <InIt, size_type>::type * = 0
  612. #endif
  613. )
  614. : Base(allocator_type())
  615. {
  616. this->priv_range_initialize(first, last);
  617. }
  618. //! <b>Effects</b>: Constructs a deque that will use a copy of allocator a
  619. //! and inserts a copy of the range [first, last) in the deque.
  620. //!
  621. //! <b>Throws</b>: If allocator_type's default constructor
  622. //! throws or T's constructor taking a dereferenced InIt throws.
  623. //!
  624. //! <b>Complexity</b>: Linear to the range [first, last).
  625. template <class InIt>
  626. inline deque(InIt first, InIt last, const allocator_type& a
  627. #if !defined(BOOST_CONTAINER_DOXYGEN_INVOKED)
  628. , typename dtl::disable_if_convertible
  629. <InIt, size_type>::type * = 0
  630. #endif
  631. )
  632. : Base(a)
  633. {
  634. this->priv_range_initialize(first, last);
  635. }
  636. #if !defined(BOOST_NO_CXX11_HDR_INITIALIZER_LIST)
  637. //! <b>Effects</b>: Constructs a deque that will use a copy of allocator a
  638. //! and inserts a copy of the range [il.begin(), il.end()) in the deque.
  639. //!
  640. //! <b>Throws</b>: If allocator_type's default constructor
  641. //! throws or T's constructor taking a dereferenced std::initializer_list iterator throws.
  642. //!
  643. //! <b>Complexity</b>: Linear to the range [il.begin(), il.end()).
  644. inline deque(std::initializer_list<value_type> il, const allocator_type& a = allocator_type())
  645. : Base(a)
  646. {
  647. this->priv_range_initialize(il.begin(), il.end());
  648. }
  649. #endif
  650. //! <b>Effects</b>: Copy constructs a deque.
  651. //!
  652. //! <b>Postcondition</b>: x == *this.
  653. //!
  654. //! <b>Complexity</b>: Linear to the elements x contains.
  655. inline deque(const deque& x)
  656. : Base(allocator_traits_type::select_on_container_copy_construction(x.alloc()))
  657. {
  658. if(x.size()){
  659. this->priv_initialize_map(x.size());
  660. boost::container::uninitialized_copy_alloc
  661. (this->alloc(), x.begin(), x.end(), this->members_.m_start);
  662. }
  663. }
  664. //! <b>Effects</b>: Move constructor. Moves x's resources to *this.
  665. //!
  666. //! <b>Throws</b>: If allocator_type's copy constructor throws.
  667. //!
  668. //! <b>Complexity</b>: Constant.
  669. inline deque(BOOST_RV_REF(deque) x) BOOST_NOEXCEPT_OR_NOTHROW
  670. : Base(BOOST_MOVE_BASE(Base, x))
  671. { this->swap_members(x); }
  672. //! <b>Effects</b>: Copy constructs a vector using the specified allocator.
  673. //!
  674. //! <b>Postcondition</b>: x == *this.
  675. //!
  676. //! <b>Throws</b>: If allocation
  677. //! throws or T's copy constructor throws.
  678. //!
  679. //! <b>Complexity</b>: Linear to the elements x contains.
  680. deque(const deque& x, const allocator_type &a)
  681. : Base(a)
  682. {
  683. if(x.size()){
  684. this->priv_initialize_map(x.size());
  685. boost::container::uninitialized_copy_alloc
  686. (this->alloc(), x.begin(), x.end(), this->members_.m_start);
  687. }
  688. }
  689. //! <b>Effects</b>: Move constructor using the specified allocator.
  690. //! Moves x's resources to *this if a == allocator_type().
  691. //! Otherwise copies values from x to *this.
  692. //!
  693. //! <b>Throws</b>: If allocation or T's copy constructor throws.
  694. //!
  695. //! <b>Complexity</b>: Constant if a == x.get_allocator(), linear otherwise.
  696. deque(BOOST_RV_REF(deque) x, const allocator_type &a)
  697. : Base(a)
  698. {
  699. if(x.alloc() == a){
  700. this->swap_members(x);
  701. }
  702. else{
  703. if(x.size()){
  704. this->priv_initialize_map(x.size());
  705. boost::container::uninitialized_copy_alloc
  706. ( this->alloc(), boost::make_move_iterator(x.begin())
  707. , boost::make_move_iterator(x.end()), this->members_.m_start);
  708. }
  709. }
  710. }
  711. //! <b>Effects</b>: Destroys the deque. All stored values are destroyed
  712. //! and used memory is deallocated.
  713. //!
  714. //! <b>Throws</b>: Nothing.
  715. //!
  716. //! <b>Complexity</b>: Linear to the number of elements.
  717. inline ~deque() BOOST_NOEXCEPT_OR_NOTHROW
  718. {
  719. this->priv_destroy_range(this->members_.m_start, this->members_.m_finish);
  720. }
  721. //! <b>Effects</b>: Makes *this contain the same elements as x.
  722. //!
  723. //! <b>Postcondition</b>: this->size() == x.size(). *this contains a copy
  724. //! of each of x's elements.
  725. //!
  726. //! <b>Throws</b>: If memory allocation throws or T's copy constructor throws.
  727. //!
  728. //! <b>Complexity</b>: Linear to the number of elements in x.
  729. deque& operator= (BOOST_COPY_ASSIGN_REF(deque) x)
  730. {
  731. if (BOOST_LIKELY(&x != this)){
  732. allocator_type &this_alloc = this->alloc();
  733. const allocator_type &x_alloc = x.alloc();
  734. dtl::bool_<allocator_traits_type::
  735. propagate_on_container_copy_assignment::value> flag;
  736. if(flag && this_alloc != x_alloc){
  737. this->clear();
  738. this->shrink_to_fit();
  739. }
  740. dtl::assign_alloc(this->alloc(), x.alloc(), flag);
  741. dtl::assign_alloc(this->ptr_alloc(), x.ptr_alloc(), flag);
  742. this->assign(x.cbegin(), x.cend());
  743. }
  744. return *this;
  745. }
  746. //! <b>Effects</b>: Move assignment. All x's values are transferred to *this.
  747. //!
  748. //! <b>Throws</b>: If allocator_traits_type::propagate_on_container_move_assignment
  749. //! is false and (allocation throws or value_type's move constructor throws)
  750. //!
  751. //! <b>Complexity</b>: Constant if allocator_traits_type::
  752. //! propagate_on_container_move_assignment is true or
  753. //! this->get>allocator() == x.get_allocator(). Linear otherwise.
  754. deque& operator= (BOOST_RV_REF(deque) x)
  755. BOOST_NOEXCEPT_IF(allocator_traits_type::propagate_on_container_move_assignment::value
  756. || allocator_traits_type::is_always_equal::value)
  757. {
  758. if (BOOST_LIKELY(this != &x)) {
  759. allocator_type &this_alloc = this->alloc();
  760. allocator_type &x_alloc = x.alloc();
  761. const bool propagate_alloc = allocator_traits_type::
  762. propagate_on_container_move_assignment::value;
  763. dtl::bool_<propagate_alloc> flag;
  764. const bool allocators_equal = this_alloc == x_alloc; (void)allocators_equal;
  765. //Resources can be transferred if both allocators are
  766. //going to be equal after this function (either propagated or already equal)
  767. if(propagate_alloc || allocators_equal){
  768. //Destroy objects but retain memory in case x reuses it in the future
  769. this->clear();
  770. //Move allocator if needed
  771. dtl::move_alloc(this_alloc, x_alloc, flag);
  772. dtl::move_alloc(this->ptr_alloc(), x.ptr_alloc(), flag);
  773. //Nothrow swap
  774. this->swap_members(x);
  775. }
  776. //Else do a one by one move
  777. else{
  778. this->assign( boost::make_move_iterator(x.begin())
  779. , boost::make_move_iterator(x.end()));
  780. }
  781. }
  782. return *this;
  783. }
  784. #if !defined(BOOST_NO_CXX11_HDR_INITIALIZER_LIST)
  785. //! <b>Effects</b>: Makes *this contain the same elements as il.
  786. //!
  787. //! <b>Postcondition</b>: this->size() == il.size(). *this contains a copy
  788. //! of each of x's elements.
  789. //!
  790. //! <b>Throws</b>: If memory allocation throws or T's copy constructor throws.
  791. //!
  792. //! <b>Complexity</b>: Linear to the number of elements in il.
  793. inline deque& operator=(std::initializer_list<value_type> il)
  794. {
  795. this->assign(il.begin(), il.end());
  796. return *this;
  797. }
  798. #endif
  799. //! <b>Effects</b>: Assigns the n copies of val to *this.
  800. //!
  801. //! <b>Throws</b>: If memory allocation throws or T's copy constructor throws.
  802. //!
  803. //! <b>Complexity</b>: Linear to n.
  804. inline void assign(size_type n, const T& val)
  805. {
  806. this->assign(c_it(val, n), c_it());
  807. }
  808. //! <b>Effects</b>: Assigns the the range [first, last) to *this.
  809. //!
  810. //! <b>Throws</b>: If memory allocation throws or
  811. //! T's constructor from dereferencing InIt throws.
  812. //!
  813. //! <b>Complexity</b>: Linear to n.
  814. template <class InIt>
  815. void assign(InIt first, InIt last
  816. #if !defined(BOOST_CONTAINER_DOXYGEN_INVOKED)
  817. , typename dtl::disable_if_or
  818. < void
  819. , dtl::is_convertible<InIt, size_type>
  820. , dtl::is_not_input_iterator<InIt>
  821. >::type * = 0
  822. #endif
  823. )
  824. {
  825. iterator cur = this->begin();
  826. for ( ; first != last && cur != end(); ++cur, ++first){
  827. *cur = *first;
  828. }
  829. if (first == last){
  830. this->erase(cur, this->cend());
  831. }
  832. else{
  833. this->insert(this->cend(), first, last);
  834. }
  835. }
  836. #if !defined(BOOST_CONTAINER_DOXYGEN_INVOKED)
  837. template <class FwdIt>
  838. void assign(FwdIt first, FwdIt last
  839. , typename dtl::disable_if_or
  840. < void
  841. , dtl::is_convertible<FwdIt, size_type>
  842. , dtl::is_input_iterator<FwdIt>
  843. >::type * = 0
  844. )
  845. {
  846. const size_type len = boost::container::iterator_udistance(first, last);
  847. if (len > size()) {
  848. FwdIt mid = first;
  849. boost::container::iterator_uadvance(mid, this->size());
  850. boost::container::copy(first, mid, begin());
  851. this->insert(this->cend(), mid, last);
  852. }
  853. else{
  854. this->erase(boost::container::copy(first, last, this->begin()), cend());
  855. }
  856. }
  857. #endif
  858. #if !defined(BOOST_NO_CXX11_HDR_INITIALIZER_LIST)
  859. //! <b>Effects</b>: Assigns the the range [il.begin(), il.end()) to *this.
  860. //!
  861. //! <b>Throws</b>: If memory allocation throws or
  862. //! T's constructor from dereferencing std::initializer_list iterator throws.
  863. //!
  864. //! <b>Complexity</b>: Linear to il.size().
  865. inline void assign(std::initializer_list<value_type> il)
  866. { this->assign(il.begin(), il.end()); }
  867. #endif
  868. //! <b>Effects</b>: Returns a copy of the internal allocator.
  869. //!
  870. //! <b>Throws</b>: If allocator's copy constructor throws.
  871. //!
  872. //! <b>Complexity</b>: Constant.
  873. BOOST_CONTAINER_ATTRIBUTE_NODISCARD inline
  874. allocator_type get_allocator() const BOOST_NOEXCEPT_OR_NOTHROW
  875. { return Base::alloc(); }
  876. //! <b>Effects</b>: Returns a reference to the internal allocator.
  877. //!
  878. //! <b>Throws</b>: Nothing
  879. //!
  880. //! <b>Complexity</b>: Constant.
  881. //!
  882. //! <b>Note</b>: Non-standard extension.
  883. BOOST_CONTAINER_ATTRIBUTE_NODISCARD inline
  884. const stored_allocator_type &get_stored_allocator() const BOOST_NOEXCEPT_OR_NOTHROW
  885. { return Base::alloc(); }
  886. //////////////////////////////////////////////
  887. //
  888. // iterators
  889. //
  890. //////////////////////////////////////////////
  891. //! <b>Effects</b>: Returns a reference to the internal allocator.
  892. //!
  893. //! <b>Throws</b>: Nothing
  894. //!
  895. //! <b>Complexity</b>: Constant.
  896. //!
  897. //! <b>Note</b>: Non-standard extension.
  898. BOOST_CONTAINER_ATTRIBUTE_NODISCARD inline
  899. stored_allocator_type &get_stored_allocator() BOOST_NOEXCEPT_OR_NOTHROW
  900. { return Base::alloc(); }
  901. //! <b>Effects</b>: Returns an iterator to the first element contained in the deque.
  902. //!
  903. //! <b>Throws</b>: Nothing.
  904. //!
  905. //! <b>Complexity</b>: Constant.
  906. BOOST_CONTAINER_ATTRIBUTE_NODISCARD inline
  907. iterator begin() BOOST_NOEXCEPT_OR_NOTHROW
  908. { return this->members_.m_start; }
  909. //! <b>Effects</b>: Returns a const_iterator to the first element contained in the deque.
  910. //!
  911. //! <b>Throws</b>: Nothing.
  912. //!
  913. //! <b>Complexity</b>: Constant.
  914. BOOST_CONTAINER_ATTRIBUTE_NODISCARD inline
  915. const_iterator begin() const BOOST_NOEXCEPT_OR_NOTHROW
  916. { return this->members_.m_start; }
  917. //! <b>Effects</b>: Returns an iterator to the end of the deque.
  918. //!
  919. //! <b>Throws</b>: Nothing.
  920. //!
  921. //! <b>Complexity</b>: Constant.
  922. BOOST_CONTAINER_ATTRIBUTE_NODISCARD inline
  923. iterator end() BOOST_NOEXCEPT_OR_NOTHROW
  924. { return this->members_.m_finish; }
  925. //! <b>Effects</b>: Returns a const_iterator to the end of the deque.
  926. //!
  927. //! <b>Throws</b>: Nothing.
  928. //!
  929. //! <b>Complexity</b>: Constant.
  930. BOOST_CONTAINER_ATTRIBUTE_NODISCARD inline
  931. const_iterator end() const BOOST_NOEXCEPT_OR_NOTHROW
  932. { return this->members_.m_finish; }
  933. //! <b>Effects</b>: Returns a reverse_iterator pointing to the beginning
  934. //! of the reversed deque.
  935. //!
  936. //! <b>Throws</b>: Nothing.
  937. //!
  938. //! <b>Complexity</b>: Constant.
  939. BOOST_CONTAINER_ATTRIBUTE_NODISCARD inline
  940. reverse_iterator rbegin() BOOST_NOEXCEPT_OR_NOTHROW
  941. { return reverse_iterator(this->members_.m_finish); }
  942. //! <b>Effects</b>: Returns a const_reverse_iterator pointing to the beginning
  943. //! of the reversed deque.
  944. //!
  945. //! <b>Throws</b>: Nothing.
  946. //!
  947. //! <b>Complexity</b>: Constant.
  948. BOOST_CONTAINER_ATTRIBUTE_NODISCARD inline
  949. const_reverse_iterator rbegin() const BOOST_NOEXCEPT_OR_NOTHROW
  950. { return const_reverse_iterator(this->members_.m_finish); }
  951. //! <b>Effects</b>: Returns a reverse_iterator pointing to the end
  952. //! of the reversed deque.
  953. //!
  954. //! <b>Throws</b>: Nothing.
  955. //!
  956. //! <b>Complexity</b>: Constant.
  957. BOOST_CONTAINER_ATTRIBUTE_NODISCARD inline
  958. reverse_iterator rend() BOOST_NOEXCEPT_OR_NOTHROW
  959. { return reverse_iterator(this->members_.m_start); }
  960. //! <b>Effects</b>: Returns a const_reverse_iterator pointing to the end
  961. //! of the reversed deque.
  962. //!
  963. //! <b>Throws</b>: Nothing.
  964. //!
  965. //! <b>Complexity</b>: Constant.
  966. BOOST_CONTAINER_ATTRIBUTE_NODISCARD inline
  967. const_reverse_iterator rend() const BOOST_NOEXCEPT_OR_NOTHROW
  968. { return const_reverse_iterator(this->members_.m_start); }
  969. //! <b>Effects</b>: Returns a const_iterator to the first element contained in the deque.
  970. //!
  971. //! <b>Throws</b>: Nothing.
  972. //!
  973. //! <b>Complexity</b>: Constant.
  974. BOOST_CONTAINER_ATTRIBUTE_NODISCARD inline
  975. const_iterator cbegin() const BOOST_NOEXCEPT_OR_NOTHROW
  976. { return this->members_.m_start; }
  977. //! <b>Effects</b>: Returns a const_iterator to the end of the deque.
  978. //!
  979. //! <b>Throws</b>: Nothing.
  980. //!
  981. //! <b>Complexity</b>: Constant.
  982. BOOST_CONTAINER_ATTRIBUTE_NODISCARD inline
  983. const_iterator cend() const BOOST_NOEXCEPT_OR_NOTHROW
  984. { return this->members_.m_finish; }
  985. //! <b>Effects</b>: Returns a const_reverse_iterator pointing to the beginning
  986. //! of the reversed deque.
  987. //!
  988. //! <b>Throws</b>: Nothing.
  989. //!
  990. //! <b>Complexity</b>: Constant.
  991. BOOST_CONTAINER_ATTRIBUTE_NODISCARD inline
  992. const_reverse_iterator crbegin() const BOOST_NOEXCEPT_OR_NOTHROW
  993. { return const_reverse_iterator(this->members_.m_finish); }
  994. //! <b>Effects</b>: Returns a const_reverse_iterator pointing to the end
  995. //! of the reversed deque.
  996. //!
  997. //! <b>Throws</b>: Nothing.
  998. //!
  999. //! <b>Complexity</b>: Constant.
  1000. BOOST_CONTAINER_ATTRIBUTE_NODISCARD inline
  1001. const_reverse_iterator crend() const BOOST_NOEXCEPT_OR_NOTHROW
  1002. { return const_reverse_iterator(this->members_.m_start); }
  1003. //////////////////////////////////////////////
  1004. //
  1005. // capacity
  1006. //
  1007. //////////////////////////////////////////////
  1008. //! <b>Effects</b>: Returns true if the deque contains no elements.
  1009. //!
  1010. //! <b>Throws</b>: Nothing.
  1011. //!
  1012. //! <b>Complexity</b>: Constant.
  1013. BOOST_CONTAINER_ATTRIBUTE_NODISCARD inline
  1014. bool empty() const BOOST_NOEXCEPT_OR_NOTHROW
  1015. { return this->members_.m_finish == this->members_.m_start; }
  1016. //! <b>Effects</b>: Returns the number of the elements contained in the deque.
  1017. //!
  1018. //! <b>Throws</b>: Nothing.
  1019. //!
  1020. //! <b>Complexity</b>: Constant.
  1021. BOOST_CONTAINER_ATTRIBUTE_NODISCARD inline
  1022. size_type size() const BOOST_NOEXCEPT_OR_NOTHROW
  1023. { return size_type(this->members_.m_finish - this->members_.m_start); }
  1024. //! <b>Effects</b>: Returns the largest possible size of the deque.
  1025. //!
  1026. //! <b>Throws</b>: Nothing.
  1027. //!
  1028. //! <b>Complexity</b>: Constant.
  1029. BOOST_CONTAINER_ATTRIBUTE_NODISCARD inline
  1030. size_type max_size() const BOOST_NOEXCEPT_OR_NOTHROW
  1031. { return allocator_traits_type::max_size(this->alloc()); }
  1032. //! <b>Effects</b>: Inserts or erases elements at the end such that
  1033. //! the size becomes n. New elements are value initialized.
  1034. //!
  1035. //! <b>Throws</b>: If memory allocation throws, or T's constructor throws.
  1036. //!
  1037. //! <b>Complexity</b>: Linear to the difference between size() and new_size.
  1038. void resize(size_type new_size)
  1039. {
  1040. const size_type len = size();
  1041. if (new_size < len)
  1042. this->priv_erase_last_n(len - new_size);
  1043. else{
  1044. const size_type n = new_size - this->size();
  1045. dtl::insert_value_initialized_n_proxy<ValAllocator> proxy;
  1046. priv_insert_back_aux_impl(n, proxy);
  1047. }
  1048. }
  1049. //! <b>Effects</b>: Inserts or erases elements at the end such that
  1050. //! the size becomes n. New elements are default initialized.
  1051. //!
  1052. //! <b>Throws</b>: If memory allocation throws, or T's constructor throws.
  1053. //!
  1054. //! <b>Complexity</b>: Linear to the difference between size() and new_size.
  1055. //!
  1056. //! <b>Note</b>: Non-standard extension
  1057. void resize(size_type new_size, default_init_t)
  1058. {
  1059. const size_type len = size();
  1060. if (new_size < len)
  1061. this->priv_erase_last_n(len - new_size);
  1062. else{
  1063. const size_type n = new_size - this->size();
  1064. dtl::insert_default_initialized_n_proxy<ValAllocator> proxy;
  1065. priv_insert_back_aux_impl(n, proxy);
  1066. }
  1067. }
  1068. //! <b>Effects</b>: Inserts or erases elements at the end such that
  1069. //! the size becomes n. New elements are copy constructed from x.
  1070. //!
  1071. //! <b>Throws</b>: If memory allocation throws, or T's copy constructor throws.
  1072. //!
  1073. //! <b>Complexity</b>: Linear to the difference between size() and new_size.
  1074. void resize(size_type new_size, const value_type& x)
  1075. {
  1076. const size_type len = size();
  1077. if (new_size < len)
  1078. this->erase(this->members_.m_start + difference_type(new_size), this->members_.m_finish);
  1079. else
  1080. this->insert(this->members_.m_finish, new_size - len, x);
  1081. }
  1082. //! <b>Effects</b>: Tries to deallocate the excess of memory created
  1083. //! with previous allocations. The size of the deque is unchanged
  1084. //!
  1085. //! <b>Throws</b>: If memory allocation throws.
  1086. //!
  1087. //! <b>Complexity</b>: Constant.
  1088. void shrink_to_fit()
  1089. {
  1090. //This deque implementation already
  1091. //deallocates excess nodes when erasing
  1092. //so there is nothing to do except for
  1093. //empty deque
  1094. if(this->empty()){
  1095. this->priv_clear_map();
  1096. }
  1097. }
  1098. //////////////////////////////////////////////
  1099. //
  1100. // element access
  1101. //
  1102. //////////////////////////////////////////////
  1103. //! <b>Requires</b>: !empty()
  1104. //!
  1105. //! <b>Effects</b>: Returns a reference to the first
  1106. //! element of the container.
  1107. //!
  1108. //! <b>Throws</b>: Nothing.
  1109. //!
  1110. //! <b>Complexity</b>: Constant.
  1111. BOOST_CONTAINER_ATTRIBUTE_NODISCARD inline
  1112. reference front() BOOST_NOEXCEPT_OR_NOTHROW
  1113. {
  1114. BOOST_ASSERT(!this->empty());
  1115. return *this->members_.m_start;
  1116. }
  1117. //! <b>Requires</b>: !empty()
  1118. //!
  1119. //! <b>Effects</b>: Returns a const reference to the first element
  1120. //! from the beginning of the container.
  1121. //!
  1122. //! <b>Throws</b>: Nothing.
  1123. //!
  1124. //! <b>Complexity</b>: Constant.
  1125. BOOST_CONTAINER_ATTRIBUTE_NODISCARD inline
  1126. const_reference front() const BOOST_NOEXCEPT_OR_NOTHROW
  1127. {
  1128. BOOST_ASSERT(!this->empty());
  1129. return *this->members_.m_start;
  1130. }
  1131. //! <b>Requires</b>: !empty()
  1132. //!
  1133. //! <b>Effects</b>: Returns a reference to the last
  1134. //! element of the container.
  1135. //!
  1136. //! <b>Throws</b>: Nothing.
  1137. //!
  1138. //! <b>Complexity</b>: Constant.
  1139. BOOST_CONTAINER_ATTRIBUTE_NODISCARD inline
  1140. reference back() BOOST_NOEXCEPT_OR_NOTHROW
  1141. {
  1142. BOOST_ASSERT(!this->empty());
  1143. return *(end()-1);
  1144. }
  1145. //! <b>Requires</b>: !empty()
  1146. //!
  1147. //! <b>Effects</b>: Returns a const reference to the last
  1148. //! element of the container.
  1149. //!
  1150. //! <b>Throws</b>: Nothing.
  1151. //!
  1152. //! <b>Complexity</b>: Constant.
  1153. BOOST_CONTAINER_ATTRIBUTE_NODISCARD inline
  1154. const_reference back() const BOOST_NOEXCEPT_OR_NOTHROW
  1155. {
  1156. BOOST_ASSERT(!this->empty());
  1157. return *(cend()-1);
  1158. }
  1159. //! <b>Requires</b>: size() > n.
  1160. //!
  1161. //! <b>Effects</b>: Returns a reference to the nth element
  1162. //! from the beginning of the container.
  1163. //!
  1164. //! <b>Throws</b>: Nothing.
  1165. //!
  1166. //! <b>Complexity</b>: Constant.
  1167. BOOST_CONTAINER_ATTRIBUTE_NODISCARD inline
  1168. reference operator[](size_type n) BOOST_NOEXCEPT_OR_NOTHROW
  1169. {
  1170. BOOST_ASSERT(this->size() > n);
  1171. return this->members_.m_start[difference_type(n)];
  1172. }
  1173. //! <b>Requires</b>: size() > n.
  1174. //!
  1175. //! <b>Effects</b>: Returns a const reference to the nth element
  1176. //! from the beginning of the container.
  1177. //!
  1178. //! <b>Throws</b>: Nothing.
  1179. //!
  1180. //! <b>Complexity</b>: Constant.
  1181. BOOST_CONTAINER_ATTRIBUTE_NODISCARD inline
  1182. const_reference operator[](size_type n) const BOOST_NOEXCEPT_OR_NOTHROW
  1183. {
  1184. BOOST_ASSERT(this->size() > n);
  1185. return this->members_.m_start[difference_type(n)];
  1186. }
  1187. //! <b>Requires</b>: size() >= n.
  1188. //!
  1189. //! <b>Effects</b>: Returns an iterator to the nth element
  1190. //! from the beginning of the container. Returns end()
  1191. //! if n == size().
  1192. //!
  1193. //! <b>Throws</b>: Nothing.
  1194. //!
  1195. //! <b>Complexity</b>: Constant.
  1196. //!
  1197. //! <b>Note</b>: Non-standard extension
  1198. BOOST_CONTAINER_ATTRIBUTE_NODISCARD inline
  1199. iterator nth(size_type n) BOOST_NOEXCEPT_OR_NOTHROW
  1200. {
  1201. BOOST_ASSERT(this->size() >= n);
  1202. return iterator(this->begin()+difference_type(n));
  1203. }
  1204. //! <b>Requires</b>: size() >= n.
  1205. //!
  1206. //! <b>Effects</b>: Returns a const_iterator to the nth element
  1207. //! from the beginning of the container. Returns end()
  1208. //! if n == size().
  1209. //!
  1210. //! <b>Throws</b>: Nothing.
  1211. //!
  1212. //! <b>Complexity</b>: Constant.
  1213. //!
  1214. //! <b>Note</b>: Non-standard extension
  1215. BOOST_CONTAINER_ATTRIBUTE_NODISCARD inline
  1216. const_iterator nth(size_type n) const BOOST_NOEXCEPT_OR_NOTHROW
  1217. {
  1218. BOOST_ASSERT(this->size() >= n);
  1219. return const_iterator(this->cbegin()+difference_type(n));
  1220. }
  1221. //! <b>Requires</b>: begin() <= p <= end().
  1222. //!
  1223. //! <b>Effects</b>: Returns the index of the element pointed by p
  1224. //! and size() if p == end().
  1225. //!
  1226. //! <b>Throws</b>: Nothing.
  1227. //!
  1228. //! <b>Complexity</b>: Constant.
  1229. //!
  1230. //! <b>Note</b>: Non-standard extension
  1231. BOOST_CONTAINER_ATTRIBUTE_NODISCARD inline
  1232. size_type index_of(iterator p) BOOST_NOEXCEPT_OR_NOTHROW
  1233. {
  1234. //Range checked priv_index_of
  1235. return this->priv_index_of(p);
  1236. }
  1237. //! <b>Requires</b>: begin() <= p <= end().
  1238. //!
  1239. //! <b>Effects</b>: Returns the index of the element pointed by p
  1240. //! and size() if p == end().
  1241. //!
  1242. //! <b>Throws</b>: Nothing.
  1243. //!
  1244. //! <b>Complexity</b>: Constant.
  1245. //!
  1246. //! <b>Note</b>: Non-standard extension
  1247. BOOST_CONTAINER_ATTRIBUTE_NODISCARD inline
  1248. size_type index_of(const_iterator p) const BOOST_NOEXCEPT_OR_NOTHROW
  1249. {
  1250. //Range checked priv_index_of
  1251. return this->priv_index_of(p);
  1252. }
  1253. //! <b>Requires</b>: size() > n.
  1254. //!
  1255. //! <b>Effects</b>: Returns a reference to the nth element
  1256. //! from the beginning of the container.
  1257. //!
  1258. //! <b>Throws</b>: range_error if n >= size()
  1259. //!
  1260. //! <b>Complexity</b>: Constant.
  1261. BOOST_CONTAINER_ATTRIBUTE_NODISCARD inline
  1262. reference at(size_type n)
  1263. {
  1264. this->priv_throw_if_out_of_range(n);
  1265. return (*this)[n];
  1266. }
  1267. //! <b>Requires</b>: size() > n.
  1268. //!
  1269. //! <b>Effects</b>: Returns a const reference to the nth element
  1270. //! from the beginning of the container.
  1271. //!
  1272. //! <b>Throws</b>: range_error if n >= size()
  1273. //!
  1274. //! <b>Complexity</b>: Constant.
  1275. BOOST_CONTAINER_ATTRIBUTE_NODISCARD inline
  1276. const_reference at(size_type n) const
  1277. {
  1278. this->priv_throw_if_out_of_range(n);
  1279. return (*this)[n];
  1280. }
  1281. //////////////////////////////////////////////
  1282. //
  1283. // modifiers
  1284. //
  1285. //////////////////////////////////////////////
  1286. #if !defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES) || defined(BOOST_CONTAINER_DOXYGEN_INVOKED)
  1287. //! <b>Effects</b>: Inserts an object of type T constructed with
  1288. //! std::forward<Args>(args)... in the beginning of the deque.
  1289. //!
  1290. //! <b>Returns</b>: A reference to the created object.
  1291. //!
  1292. //! <b>Throws</b>: If memory allocation throws or the in-place constructor throws.
  1293. //!
  1294. //! <b>Complexity</b>: Amortized constant time
  1295. template <class... Args>
  1296. reference emplace_front(BOOST_FWD_REF(Args)... args)
  1297. {
  1298. if(this->priv_push_front_simple_available()){
  1299. reference r = *this->priv_push_front_simple_pos();
  1300. allocator_traits_type::construct
  1301. ( this->alloc()
  1302. , this->priv_push_front_simple_pos()
  1303. , boost::forward<Args>(args)...);
  1304. this->priv_push_front_simple_commit();
  1305. return r;
  1306. }
  1307. else{
  1308. typedef dtl::insert_nonmovable_emplace_proxy<ValAllocator, Args...> type;
  1309. return *this->priv_insert_front_aux_impl(1, type(boost::forward<Args>(args)...));
  1310. }
  1311. }
  1312. //! <b>Effects</b>: Inserts an object of type T constructed with
  1313. //! std::forward<Args>(args)... in the end of the deque.
  1314. //!
  1315. //! <b>Returns</b>: A reference to the created object.
  1316. //!
  1317. //! <b>Throws</b>: If memory allocation throws or the in-place constructor throws.
  1318. //!
  1319. //! <b>Complexity</b>: Amortized constant time
  1320. template <class... Args>
  1321. reference emplace_back(BOOST_FWD_REF(Args)... args)
  1322. {
  1323. if(this->priv_push_back_simple_available()){
  1324. reference r = *this->priv_push_back_simple_pos();
  1325. allocator_traits_type::construct
  1326. ( this->alloc()
  1327. , this->priv_push_back_simple_pos()
  1328. , boost::forward<Args>(args)...);
  1329. this->priv_push_back_simple_commit();
  1330. return r;
  1331. }
  1332. else{
  1333. typedef dtl::insert_nonmovable_emplace_proxy<ValAllocator, Args...> type;
  1334. return *this->priv_insert_back_aux_impl(1, type(boost::forward<Args>(args)...));
  1335. }
  1336. }
  1337. //! <b>Requires</b>: p must be a valid iterator of *this.
  1338. //!
  1339. //! <b>Effects</b>: Inserts an object of type T constructed with
  1340. //! std::forward<Args>(args)... before p
  1341. //!
  1342. //! <b>Throws</b>: If memory allocation throws or the in-place constructor throws.
  1343. //!
  1344. //! <b>Complexity</b>: If p is end(), amortized constant time
  1345. //! Linear time otherwise.
  1346. template <class... Args>
  1347. iterator emplace(const_iterator p, BOOST_FWD_REF(Args)... args)
  1348. {
  1349. BOOST_ASSERT(this->priv_in_range_or_end(p));
  1350. if(p == this->cbegin()){
  1351. this->emplace_front(boost::forward<Args>(args)...);
  1352. return this->begin();
  1353. }
  1354. else if(p == this->cend()){
  1355. this->emplace_back(boost::forward<Args>(args)...);
  1356. return (this->end()-1);
  1357. }
  1358. else{
  1359. typedef dtl::insert_emplace_proxy<ValAllocator, Args...> type;
  1360. return this->priv_insert_aux_impl(p, 1, type(boost::forward<Args>(args)...));
  1361. }
  1362. }
  1363. #else //!defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES)
  1364. #define BOOST_CONTAINER_DEQUE_EMPLACE_CODE(N) \
  1365. BOOST_MOVE_TMPL_LT##N BOOST_MOVE_CLASS##N BOOST_MOVE_GT##N\
  1366. reference emplace_front(BOOST_MOVE_UREF##N)\
  1367. {\
  1368. if(priv_push_front_simple_available()){\
  1369. reference r = *this->priv_push_front_simple_pos();\
  1370. allocator_traits_type::construct\
  1371. ( this->alloc(), this->priv_push_front_simple_pos() BOOST_MOVE_I##N BOOST_MOVE_FWD##N);\
  1372. priv_push_front_simple_commit();\
  1373. return r;\
  1374. }\
  1375. else{\
  1376. typedef dtl::insert_nonmovable_emplace_proxy##N\
  1377. <ValAllocator BOOST_MOVE_I##N BOOST_MOVE_TARG##N> type;\
  1378. return *priv_insert_front_aux_impl(1, type(BOOST_MOVE_FWD##N));\
  1379. }\
  1380. }\
  1381. \
  1382. BOOST_MOVE_TMPL_LT##N BOOST_MOVE_CLASS##N BOOST_MOVE_GT##N\
  1383. reference emplace_back(BOOST_MOVE_UREF##N)\
  1384. {\
  1385. if(priv_push_back_simple_available()){\
  1386. reference r = *this->priv_push_back_simple_pos();\
  1387. allocator_traits_type::construct\
  1388. ( this->alloc(), this->priv_push_back_simple_pos() BOOST_MOVE_I##N BOOST_MOVE_FWD##N);\
  1389. priv_push_back_simple_commit();\
  1390. return r;\
  1391. }\
  1392. else{\
  1393. typedef dtl::insert_nonmovable_emplace_proxy##N\
  1394. <ValAllocator BOOST_MOVE_I##N BOOST_MOVE_TARG##N> type;\
  1395. return *priv_insert_back_aux_impl(1, type(BOOST_MOVE_FWD##N));\
  1396. }\
  1397. }\
  1398. \
  1399. BOOST_MOVE_TMPL_LT##N BOOST_MOVE_CLASS##N BOOST_MOVE_GT##N\
  1400. iterator emplace(const_iterator p BOOST_MOVE_I##N BOOST_MOVE_UREF##N)\
  1401. {\
  1402. BOOST_ASSERT(this->priv_in_range_or_end(p));\
  1403. if(p == this->cbegin()){\
  1404. this->emplace_front(BOOST_MOVE_FWD##N);\
  1405. return this->begin();\
  1406. }\
  1407. else if(p == cend()){\
  1408. this->emplace_back(BOOST_MOVE_FWD##N);\
  1409. return (--this->end());\
  1410. }\
  1411. else{\
  1412. typedef dtl::insert_emplace_proxy_arg##N\
  1413. <ValAllocator BOOST_MOVE_I##N BOOST_MOVE_TARG##N> type;\
  1414. return this->priv_insert_aux_impl(p, 1, type(BOOST_MOVE_FWD##N));\
  1415. }\
  1416. }
  1417. //
  1418. BOOST_MOVE_ITERATE_0TO9(BOOST_CONTAINER_DEQUE_EMPLACE_CODE)
  1419. #undef BOOST_CONTAINER_DEQUE_EMPLACE_CODE
  1420. #endif // !defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES)
  1421. #if defined(BOOST_CONTAINER_DOXYGEN_INVOKED)
  1422. //! <b>Effects</b>: Inserts a copy of x at the front of the deque.
  1423. //!
  1424. //! <b>Throws</b>: If memory allocation throws or
  1425. //! T's copy constructor throws.
  1426. //!
  1427. //! <b>Complexity</b>: Amortized constant time.
  1428. void push_front(const T &x);
  1429. //! <b>Effects</b>: Constructs a new element in the front of the deque
  1430. //! and moves the resources of x to this new element.
  1431. //!
  1432. //! <b>Throws</b>: If memory allocation throws.
  1433. //!
  1434. //! <b>Complexity</b>: Amortized constant time.
  1435. void push_front(T &&x);
  1436. #else
  1437. BOOST_MOVE_CONVERSION_AWARE_CATCH(push_front, T, void, priv_push_front)
  1438. #endif
  1439. #if defined(BOOST_CONTAINER_DOXYGEN_INVOKED)
  1440. //! <b>Effects</b>: Inserts a copy of x at the end of the deque.
  1441. //!
  1442. //! <b>Throws</b>: If memory allocation throws or
  1443. //! T's copy constructor throws.
  1444. //!
  1445. //! <b>Complexity</b>: Amortized constant time.
  1446. void push_back(const T &x);
  1447. //! <b>Effects</b>: Constructs a new element in the end of the deque
  1448. //! and moves the resources of x to this new element.
  1449. //!
  1450. //! <b>Throws</b>: If memory allocation throws.
  1451. //!
  1452. //! <b>Complexity</b>: Amortized constant time.
  1453. void push_back(T &&x);
  1454. #else
  1455. BOOST_MOVE_CONVERSION_AWARE_CATCH(push_back, T, void, priv_push_back)
  1456. #endif
  1457. #if defined(BOOST_CONTAINER_DOXYGEN_INVOKED)
  1458. //! <b>Requires</b>: p must be a valid iterator of *this.
  1459. //!
  1460. //! <b>Effects</b>: Insert a copy of x before p.
  1461. //!
  1462. //! <b>Returns</b>: an iterator to the inserted element.
  1463. //!
  1464. //! <b>Throws</b>: If memory allocation throws or x's copy constructor throws.
  1465. //!
  1466. //! <b>Complexity</b>: If p is end(), amortized constant time
  1467. //! Linear time otherwise.
  1468. iterator insert(const_iterator p, const T &x);
  1469. //! <b>Requires</b>: p must be a valid iterator of *this.
  1470. //!
  1471. //! <b>Effects</b>: Insert a new element before p with x's resources.
  1472. //!
  1473. //! <b>Returns</b>: an iterator to the inserted element.
  1474. //!
  1475. //! <b>Throws</b>: If memory allocation throws.
  1476. //!
  1477. //! <b>Complexity</b>: If p is end(), amortized constant time
  1478. //! Linear time otherwise.
  1479. iterator insert(const_iterator p, T &&x);
  1480. #else
  1481. BOOST_MOVE_CONVERSION_AWARE_CATCH_1ARG(insert, T, iterator, priv_insert, const_iterator, const_iterator)
  1482. #endif
  1483. //! <b>Requires</b>: pos must be a valid iterator of *this.
  1484. //!
  1485. //! <b>Effects</b>: Insert n copies of x before pos.
  1486. //!
  1487. //! <b>Returns</b>: an iterator to the first inserted element or pos if n is 0.
  1488. //!
  1489. //! <b>Throws</b>: If memory allocation throws or T's copy constructor throws.
  1490. //!
  1491. //! <b>Complexity</b>: Linear to n.
  1492. inline iterator insert(const_iterator pos, size_type n, const value_type& x)
  1493. {
  1494. //Range check of p is done by insert()
  1495. return this->insert(pos, c_it(x, n), c_it());
  1496. }
  1497. //! <b>Requires</b>: pos must be a valid iterator of *this.
  1498. //!
  1499. //! <b>Effects</b>: Insert a copy of the [first, last) range before pos.
  1500. //!
  1501. //! <b>Returns</b>: an iterator to the first inserted element or pos if first == last.
  1502. //!
  1503. //! <b>Throws</b>: If memory allocation throws, T's constructor from a
  1504. //! dereferenced InIt throws or T's copy constructor throws.
  1505. //!
  1506. //! <b>Complexity</b>: Linear to distance [first, last).
  1507. template <class InIt>
  1508. iterator insert(const_iterator pos, InIt first, InIt last
  1509. #if !defined(BOOST_CONTAINER_DOXYGEN_INVOKED)
  1510. , typename dtl::disable_if_or
  1511. < void
  1512. , dtl::is_convertible<InIt, size_type>
  1513. , dtl::is_not_input_iterator<InIt>
  1514. >::type * = 0
  1515. #endif
  1516. )
  1517. {
  1518. BOOST_ASSERT(this->priv_in_range_or_end(pos));
  1519. size_type n = 0;
  1520. iterator it(pos.unconst());
  1521. for(;first != last; ++first, ++n){
  1522. it = this->emplace(it, *first);
  1523. ++it;
  1524. }
  1525. it -= difference_type(n);
  1526. return it;
  1527. }
  1528. #if !defined(BOOST_NO_CXX11_HDR_INITIALIZER_LIST)
  1529. //! <b>Requires</b>: pos must be a valid iterator of *this.
  1530. //!
  1531. //! <b>Effects</b>: Insert a copy of the [il.begin(), il.end()) range before pos.
  1532. //!
  1533. //! <b>Returns</b>: an iterator to the first inserted element or pos if il.begin() == il.end().
  1534. //!
  1535. //! <b>Throws</b>: If memory allocation throws, T's constructor from a
  1536. //! dereferenced std::initializer_list throws or T's copy constructor throws.
  1537. //!
  1538. //! <b>Complexity</b>: Linear to distance [il.begin(), il.end()).
  1539. inline iterator insert(const_iterator pos, std::initializer_list<value_type> il)
  1540. {
  1541. //Range check os pos is done in insert()
  1542. return insert(pos, il.begin(), il.end());
  1543. }
  1544. #endif
  1545. #if !defined(BOOST_CONTAINER_DOXYGEN_INVOKED)
  1546. template <class FwdIt>
  1547. inline iterator insert(const_iterator p, FwdIt first, FwdIt last
  1548. #if !defined(BOOST_CONTAINER_DOXYGEN_INVOKED)
  1549. , typename dtl::disable_if_or
  1550. < void
  1551. , dtl::is_convertible<FwdIt, size_type>
  1552. , dtl::is_input_iterator<FwdIt>
  1553. >::type * = 0
  1554. #endif
  1555. )
  1556. {
  1557. BOOST_ASSERT(this->priv_in_range_or_end(p));
  1558. dtl::insert_range_proxy<ValAllocator, FwdIt> proxy(first);
  1559. return priv_insert_aux_impl(p, boost::container::iterator_udistance(first, last), proxy);
  1560. }
  1561. #endif
  1562. //! <b>Effects</b>: Removes the first element from the deque.
  1563. //!
  1564. //! <b>Throws</b>: Nothing.
  1565. //!
  1566. //! <b>Complexity</b>: Constant time.
  1567. void pop_front() BOOST_NOEXCEPT_OR_NOTHROW
  1568. {
  1569. BOOST_ASSERT(!this->empty());
  1570. if (this->members_.m_start.m_cur != this->members_.m_start.m_last - 1) {
  1571. allocator_traits_type::destroy
  1572. ( this->alloc()
  1573. , boost::movelib::to_raw_pointer(this->members_.m_start.m_cur)
  1574. );
  1575. ++this->members_.m_start.m_cur;
  1576. }
  1577. else
  1578. this->priv_pop_front_aux();
  1579. }
  1580. //! <b>Effects</b>: Removes the last element from the deque.
  1581. //!
  1582. //! <b>Throws</b>: Nothing.
  1583. //!
  1584. //! <b>Complexity</b>: Constant time.
  1585. void pop_back() BOOST_NOEXCEPT_OR_NOTHROW
  1586. {
  1587. BOOST_ASSERT(!this->empty());
  1588. if (this->members_.m_finish.m_cur != this->members_.m_finish.m_first) {
  1589. --this->members_.m_finish.m_cur;
  1590. allocator_traits_type::destroy
  1591. ( this->alloc()
  1592. , boost::movelib::to_raw_pointer(this->members_.m_finish.m_cur)
  1593. );
  1594. }
  1595. else
  1596. this->priv_pop_back_aux();
  1597. }
  1598. //! <b>Effects</b>: Erases the element at p.
  1599. //!
  1600. //! <b>Throws</b>: Nothing.
  1601. //!
  1602. //! <b>Complexity</b>: Linear to the elements between pos and the
  1603. //! last element (if pos is near the end) or the first element
  1604. //! if(pos is near the beginning).
  1605. //! Constant if pos is the first or the last element.
  1606. iterator erase(const_iterator pos) BOOST_NOEXCEPT_OR_NOTHROW
  1607. {
  1608. BOOST_ASSERT(this->priv_in_range(pos));
  1609. iterator next = pos.unconst();
  1610. ++next;
  1611. size_type index = size_type(pos - this->members_.m_start);
  1612. if (index < (this->size()/2)) {
  1613. boost::container::move_backward(this->begin(), pos.unconst(), next);
  1614. pop_front();
  1615. }
  1616. else {
  1617. boost::container::move(next, this->end(), pos.unconst());
  1618. pop_back();
  1619. }
  1620. return this->members_.m_start + difference_type(index);
  1621. }
  1622. //! <b>Effects</b>: Erases the elements pointed by [first, last).
  1623. //!
  1624. //! <b>Throws</b>: Nothing.
  1625. //!
  1626. //! <b>Complexity</b>: Linear to the distance between first and
  1627. //! last plus the elements between pos and the
  1628. //! last element (if pos is near the end) or the first element
  1629. //! if(pos is near the beginning).
  1630. iterator erase(const_iterator first, const_iterator last) BOOST_NOEXCEPT_OR_NOTHROW
  1631. {
  1632. BOOST_ASSERT(first == last ||
  1633. (first < last && this->priv_in_range(first) && this->priv_in_range_or_end(last)));
  1634. if (first == this->members_.m_start && last == this->members_.m_finish) {
  1635. this->clear();
  1636. return this->members_.m_finish;
  1637. }
  1638. else {
  1639. const size_type n = static_cast<size_type>(last - first);
  1640. const size_type elems_before = static_cast<size_type>(first - this->members_.m_start);
  1641. if (elems_before < (this->size() - n) - elems_before) {
  1642. boost::container::move_backward(begin(), first.unconst(), last.unconst());
  1643. iterator new_start = this->members_.m_start + difference_type(n);
  1644. this->priv_destroy_range(this->members_.m_start, new_start);
  1645. this->priv_destroy_nodes(this->members_.m_start.m_node, new_start.m_node);
  1646. this->members_.m_start = new_start;
  1647. }
  1648. else {
  1649. boost::container::move(last.unconst(), end(), first.unconst());
  1650. iterator new_finish = this->members_.m_finish - difference_type(n);
  1651. this->priv_destroy_range(new_finish, this->members_.m_finish);
  1652. this->priv_destroy_nodes(new_finish.m_node + 1, this->members_.m_finish.m_node + 1);
  1653. this->members_.m_finish = new_finish;
  1654. }
  1655. return this->members_.m_start + difference_type(elems_before);
  1656. }
  1657. }
  1658. //! <b>Effects</b>: Swaps the contents of *this and x.
  1659. //!
  1660. //! <b>Throws</b>: Nothing.
  1661. //!
  1662. //! <b>Complexity</b>: Constant.
  1663. inline void swap(deque &x)
  1664. BOOST_NOEXCEPT_IF(allocator_traits_type::propagate_on_container_swap::value
  1665. || allocator_traits_type::is_always_equal::value)
  1666. {
  1667. this->swap_members(x);
  1668. dtl::bool_<allocator_traits_type::propagate_on_container_swap::value> flag;
  1669. dtl::swap_alloc(this->alloc(), x.alloc(), flag);
  1670. dtl::swap_alloc(this->ptr_alloc(), x.ptr_alloc(), flag);
  1671. }
  1672. //! <b>Effects</b>: Erases all the elements of the deque.
  1673. //!
  1674. //! <b>Throws</b>: Nothing.
  1675. //!
  1676. //! <b>Complexity</b>: Linear to the number of elements in the deque.
  1677. void clear() BOOST_NOEXCEPT_OR_NOTHROW
  1678. {
  1679. if (this->members_.m_finish != this->members_.m_start) {
  1680. for (index_pointer node = this->members_.m_start.m_node + 1;
  1681. node < this->members_.m_finish.m_node;
  1682. ++node) {
  1683. this->priv_destroy_range(*node, *node + get_block_ssize());
  1684. this->priv_deallocate_node(*node);
  1685. }
  1686. if (this->members_.m_start.m_node != this->members_.m_finish.m_node) {
  1687. this->priv_destroy_range(this->members_.m_start.m_cur, this->members_.m_start.m_last);
  1688. this->priv_destroy_range(this->members_.m_finish.m_first, this->members_.m_finish.m_cur);
  1689. this->priv_deallocate_node(this->members_.m_finish.m_first);
  1690. }
  1691. else
  1692. this->priv_destroy_range(this->members_.m_start.m_cur, this->members_.m_finish.m_cur);
  1693. this->members_.m_finish = this->members_.m_start;
  1694. }
  1695. }
  1696. //! <b>Effects</b>: Returns true if x and y are equal
  1697. //!
  1698. //! <b>Complexity</b>: Linear to the number of elements in the container.
  1699. BOOST_CONTAINER_ATTRIBUTE_NODISCARD inline
  1700. friend bool operator==(const deque& x, const deque& y)
  1701. { return x.size() == y.size() && ::boost::container::algo_equal(x.begin(), x.end(), y.begin()); }
  1702. //! <b>Effects</b>: Returns true if x and y are unequal
  1703. //!
  1704. //! <b>Complexity</b>: Linear to the number of elements in the container.
  1705. BOOST_CONTAINER_ATTRIBUTE_NODISCARD inline
  1706. friend bool operator!=(const deque& x, const deque& y)
  1707. { return !(x == y); }
  1708. //! <b>Effects</b>: Returns true if x is less than y
  1709. //!
  1710. //! <b>Complexity</b>: Linear to the number of elements in the container.
  1711. BOOST_CONTAINER_ATTRIBUTE_NODISCARD inline
  1712. friend bool operator<(const deque& x, const deque& y)
  1713. { return ::boost::container::algo_lexicographical_compare(x.begin(), x.end(), y.begin(), y.end()); }
  1714. //! <b>Effects</b>: Returns true if x is greater than y
  1715. //!
  1716. //! <b>Complexity</b>: Linear to the number of elements in the container.
  1717. BOOST_CONTAINER_ATTRIBUTE_NODISCARD inline
  1718. friend bool operator>(const deque& x, const deque& y)
  1719. { return y < x; }
  1720. //! <b>Effects</b>: Returns true if x is equal or less than y
  1721. //!
  1722. //! <b>Complexity</b>: Linear to the number of elements in the container.
  1723. BOOST_CONTAINER_ATTRIBUTE_NODISCARD inline
  1724. friend bool operator<=(const deque& x, const deque& y)
  1725. { return !(y < x); }
  1726. //! <b>Effects</b>: Returns true if x is equal or greater than y
  1727. //!
  1728. //! <b>Complexity</b>: Linear to the number of elements in the container.
  1729. BOOST_CONTAINER_ATTRIBUTE_NODISCARD inline
  1730. friend bool operator>=(const deque& x, const deque& y)
  1731. { return !(x < y); }
  1732. //! <b>Effects</b>: x.swap(y)
  1733. //!
  1734. //! <b>Complexity</b>: Constant.
  1735. inline friend void swap(deque& x, deque& y)
  1736. BOOST_NOEXCEPT_IF(BOOST_NOEXCEPT(x.swap(y)))
  1737. { x.swap(y); }
  1738. #ifndef BOOST_CONTAINER_DOXYGEN_INVOKED
  1739. private:
  1740. inline size_type priv_index_of(const_iterator p) const
  1741. {
  1742. BOOST_ASSERT(this->cbegin() <= p);
  1743. BOOST_ASSERT(p <= this->cend());
  1744. return static_cast<size_type>(p - this->cbegin());
  1745. }
  1746. void priv_erase_last_n(size_type n)
  1747. {
  1748. if(n == this->size()) {
  1749. this->clear();
  1750. }
  1751. else {
  1752. iterator new_finish = this->members_.m_finish - difference_type(n);
  1753. this->priv_destroy_range(new_finish, this->members_.m_finish);
  1754. this->priv_destroy_nodes(new_finish.m_node + 1, this->members_.m_finish.m_node + 1);
  1755. this->members_.m_finish = new_finish;
  1756. }
  1757. }
  1758. void priv_throw_if_out_of_range(size_type n) const
  1759. {
  1760. if (n >= this->size())
  1761. throw_out_of_range("deque::at out of range");
  1762. }
  1763. inline bool priv_in_range(const_iterator pos) const
  1764. {
  1765. return (this->begin() <= pos) && (pos < this->end());
  1766. }
  1767. inline bool priv_in_range_or_end(const_iterator pos) const
  1768. {
  1769. return (this->begin() <= pos) && (pos <= this->end());
  1770. }
  1771. template <class U>
  1772. iterator priv_insert(const_iterator p, BOOST_FWD_REF(U) x)
  1773. {
  1774. BOOST_ASSERT(this->priv_in_range_or_end(p));
  1775. if (p == cbegin()){
  1776. this->push_front(::boost::forward<U>(x));
  1777. return begin();
  1778. }
  1779. else if (p == cend()){
  1780. this->push_back(::boost::forward<U>(x));
  1781. return --end();
  1782. }
  1783. else {
  1784. return priv_insert_aux_impl
  1785. ( p, (size_type)1
  1786. , dtl::get_insert_value_proxy<iterator, ValAllocator>(::boost::forward<U>(x)));
  1787. }
  1788. }
  1789. template <class U>
  1790. void priv_push_front(BOOST_FWD_REF(U) x)
  1791. {
  1792. if(this->priv_push_front_simple_available()){
  1793. allocator_traits_type::construct
  1794. ( this->alloc(), this->priv_push_front_simple_pos(), ::boost::forward<U>(x));
  1795. this->priv_push_front_simple_commit();
  1796. }
  1797. else{
  1798. priv_insert_aux_impl
  1799. ( this->cbegin(), (size_type)1
  1800. , dtl::get_insert_value_proxy<iterator, ValAllocator>(::boost::forward<U>(x)));
  1801. }
  1802. }
  1803. template <class U>
  1804. void priv_push_back(BOOST_FWD_REF(U) x)
  1805. {
  1806. if(this->priv_push_back_simple_available()){
  1807. allocator_traits_type::construct
  1808. ( this->alloc(), this->priv_push_back_simple_pos(), ::boost::forward<U>(x));
  1809. this->priv_push_back_simple_commit();
  1810. }
  1811. else{
  1812. priv_insert_aux_impl
  1813. ( this->cend(), (size_type)1
  1814. , dtl::get_insert_value_proxy<iterator, ValAllocator>(::boost::forward<U>(x)));
  1815. }
  1816. }
  1817. inline bool priv_push_back_simple_available() const
  1818. {
  1819. return this->members_.m_map &&
  1820. (this->members_.m_finish.m_cur != (this->members_.m_finish.m_last - 1));
  1821. }
  1822. inline T *priv_push_back_simple_pos() const
  1823. {
  1824. return boost::movelib::to_raw_pointer(this->members_.m_finish.m_cur);
  1825. }
  1826. inline void priv_push_back_simple_commit()
  1827. {
  1828. ++this->members_.m_finish.m_cur;
  1829. }
  1830. inline bool priv_push_front_simple_available() const
  1831. {
  1832. return this->members_.m_map &&
  1833. (this->members_.m_start.m_cur != this->members_.m_start.m_first);
  1834. }
  1835. inline T *priv_push_front_simple_pos() const
  1836. { return boost::movelib::to_raw_pointer(this->members_.m_start.m_cur) - 1; }
  1837. inline void priv_push_front_simple_commit()
  1838. { --this->members_.m_start.m_cur; }
  1839. void priv_destroy_range(iterator p, iterator p2)
  1840. {
  1841. if(!Base::traits_t::trivial_dctr){
  1842. for(;p != p2; ++p){
  1843. allocator_traits_type::destroy(this->alloc(), boost::movelib::iterator_to_raw_pointer(p));
  1844. }
  1845. }
  1846. }
  1847. void priv_destroy_range(pointer p, pointer p2)
  1848. {
  1849. if(!Base::traits_t::trivial_dctr){
  1850. for(;p != p2; ++p){
  1851. allocator_traits_type::destroy(this->alloc(), boost::movelib::iterator_to_raw_pointer(p));
  1852. }
  1853. }
  1854. }
  1855. template<class InsertProxy>
  1856. iterator priv_insert_aux_impl(const_iterator p, size_type n, InsertProxy proxy)
  1857. {
  1858. iterator pos(p.unconst());
  1859. const size_type pos_n = size_type(p - this->cbegin());
  1860. if(!this->members_.m_map){
  1861. this->priv_initialize_map(0);
  1862. pos = this->begin();
  1863. }
  1864. const size_type elemsbefore = static_cast<size_type>(pos - this->members_.m_start);
  1865. const size_type length = this->size();
  1866. if (elemsbefore < length / 2) {
  1867. const iterator new_start = this->priv_reserve_elements_at_front(n);
  1868. const iterator old_start = this->members_.m_start;
  1869. if(!elemsbefore){
  1870. proxy.uninitialized_copy_n_and_update(this->alloc(), new_start, n);
  1871. this->members_.m_start = new_start;
  1872. }
  1873. else{
  1874. pos = this->members_.m_start + difference_type(elemsbefore);
  1875. if (elemsbefore >= n) {
  1876. const iterator start_n = this->members_.m_start + difference_type(n);
  1877. ::boost::container::uninitialized_move_alloc
  1878. (this->alloc(), this->members_.m_start, start_n, new_start);
  1879. this->members_.m_start = new_start;
  1880. boost::container::move(start_n, pos, old_start);
  1881. proxy.copy_n_and_update(this->alloc(), pos - difference_type(n), n);
  1882. }
  1883. else {
  1884. const size_type mid_count = n - elemsbefore;
  1885. const iterator mid_start = old_start - difference_type(mid_count);
  1886. proxy.uninitialized_copy_n_and_update(this->alloc(), mid_start, mid_count);
  1887. this->members_.m_start = mid_start;
  1888. ::boost::container::uninitialized_move_alloc
  1889. (this->alloc(), old_start, pos, new_start);
  1890. this->members_.m_start = new_start;
  1891. proxy.copy_n_and_update(this->alloc(), old_start, elemsbefore);
  1892. }
  1893. }
  1894. }
  1895. else {
  1896. const iterator new_finish = this->priv_reserve_elements_at_back(n);
  1897. const iterator old_finish = this->members_.m_finish;
  1898. const size_type elemsafter = length - elemsbefore;
  1899. if(!elemsafter){
  1900. proxy.uninitialized_copy_n_and_update(this->alloc(), old_finish, n);
  1901. this->members_.m_finish = new_finish;
  1902. }
  1903. else{
  1904. pos = old_finish - difference_type(elemsafter);
  1905. if (elemsafter >= n) {
  1906. iterator finish_n = old_finish - difference_type(n);
  1907. ::boost::container::uninitialized_move_alloc
  1908. (this->alloc(), finish_n, old_finish, old_finish);
  1909. this->members_.m_finish = new_finish;
  1910. boost::container::move_backward(pos, finish_n, old_finish);
  1911. proxy.copy_n_and_update(this->alloc(), pos, n);
  1912. }
  1913. else {
  1914. const size_type raw_gap = n - elemsafter;
  1915. ::boost::container::uninitialized_move_alloc
  1916. (this->alloc(), pos, old_finish, old_finish + difference_type(raw_gap));
  1917. BOOST_CONTAINER_TRY{
  1918. proxy.copy_n_and_update(this->alloc(), pos, elemsafter);
  1919. proxy.uninitialized_copy_n_and_update(this->alloc(), old_finish, raw_gap);
  1920. }
  1921. BOOST_CONTAINER_CATCH(...){
  1922. this->priv_destroy_range(old_finish, old_finish + difference_type(elemsafter));
  1923. BOOST_CONTAINER_RETHROW
  1924. }
  1925. BOOST_CONTAINER_CATCH_END
  1926. this->members_.m_finish = new_finish;
  1927. }
  1928. }
  1929. }
  1930. return this->begin() + difference_type(pos_n);
  1931. }
  1932. template <class InsertProxy>
  1933. iterator priv_insert_back_aux_impl(size_type n, InsertProxy proxy)
  1934. {
  1935. if(!this->members_.m_map){
  1936. this->priv_initialize_map(0);
  1937. }
  1938. iterator new_finish = this->priv_reserve_elements_at_back(n);
  1939. iterator old_finish = this->members_.m_finish;
  1940. proxy.uninitialized_copy_n_and_update(this->alloc(), old_finish, n);
  1941. this->members_.m_finish = new_finish;
  1942. return iterator(this->members_.m_finish - difference_type(n));
  1943. }
  1944. template <class InsertProxy>
  1945. iterator priv_insert_front_aux_impl(size_type n, InsertProxy proxy)
  1946. {
  1947. if(!this->members_.m_map){
  1948. this->priv_initialize_map(0);
  1949. }
  1950. iterator new_start = this->priv_reserve_elements_at_front(n);
  1951. proxy.uninitialized_copy_n_and_update(this->alloc(), new_start, n);
  1952. this->members_.m_start = new_start;
  1953. return new_start;
  1954. }
  1955. inline iterator priv_fill_insert(const_iterator pos, size_type n, const value_type& x)
  1956. {
  1957. return this->insert(pos, c_it(x, n), c_it());
  1958. }
  1959. // Precondition: this->members_.m_start and this->members_.m_finish have already been initialized,
  1960. // but none of the deque's elements have yet been constructed.
  1961. void priv_fill_initialize(const value_type& value)
  1962. {
  1963. index_pointer cur = this->members_.m_start.m_node;
  1964. BOOST_CONTAINER_TRY {
  1965. for ( ; cur < this->members_.m_finish.m_node; ++cur){
  1966. boost::container::uninitialized_fill_alloc
  1967. (this->alloc(), *cur, *cur + get_block_ssize(), value);
  1968. }
  1969. boost::container::uninitialized_fill_alloc
  1970. (this->alloc(), this->members_.m_finish.m_first, this->members_.m_finish.m_cur, value);
  1971. }
  1972. BOOST_CONTAINER_CATCH(...){
  1973. this->priv_destroy_range(this->members_.m_start, iterator(*cur, cur, get_block_size()));
  1974. BOOST_CONTAINER_RETHROW
  1975. }
  1976. BOOST_CONTAINER_CATCH_END
  1977. }
  1978. template <class InIt>
  1979. void priv_range_initialize(InIt first, InIt last, typename iterator_enable_if_tag<InIt, std::input_iterator_tag>::type* =0)
  1980. {
  1981. this->priv_initialize_map(0);
  1982. BOOST_CONTAINER_TRY {
  1983. for ( ; first != last; ++first)
  1984. this->emplace_back(*first);
  1985. }
  1986. BOOST_CONTAINER_CATCH(...){
  1987. this->clear();
  1988. BOOST_CONTAINER_RETHROW
  1989. }
  1990. BOOST_CONTAINER_CATCH_END
  1991. }
  1992. template <class FwdIt>
  1993. void priv_range_initialize(FwdIt first, FwdIt last, typename iterator_disable_if_tag<FwdIt, std::input_iterator_tag>::type* =0)
  1994. {
  1995. size_type n = 0;
  1996. n = boost::container::iterator_udistance(first, last);
  1997. this->priv_initialize_map(n);
  1998. index_pointer cur_node = this->members_.m_start.m_node;
  1999. BOOST_CONTAINER_TRY {
  2000. for (; cur_node < this->members_.m_finish.m_node; ++cur_node) {
  2001. FwdIt mid = first;
  2002. boost::container::iterator_uadvance(mid, get_block_size());
  2003. ::boost::container::uninitialized_copy_alloc(this->alloc(), first, mid, *cur_node);
  2004. first = mid;
  2005. }
  2006. ::boost::container::uninitialized_copy_alloc(this->alloc(), first, last, this->members_.m_finish.m_first);
  2007. }
  2008. BOOST_CONTAINER_CATCH(...){
  2009. this->priv_destroy_range(this->members_.m_start, iterator(*cur_node, cur_node, get_block_size()));
  2010. BOOST_CONTAINER_RETHROW
  2011. }
  2012. BOOST_CONTAINER_CATCH_END
  2013. }
  2014. // Called only if this->members_.m_finish.m_cur == this->members_.m_finish.m_first.
  2015. void priv_pop_back_aux() BOOST_NOEXCEPT_OR_NOTHROW
  2016. {
  2017. this->priv_deallocate_node(this->members_.m_finish.m_first);
  2018. this->members_.m_finish.priv_set_node(this->members_.m_finish.m_node - 1, get_block_size());
  2019. this->members_.m_finish.m_cur = this->members_.m_finish.m_last - 1;
  2020. allocator_traits_type::destroy
  2021. ( this->alloc()
  2022. , boost::movelib::to_raw_pointer(this->members_.m_finish.m_cur)
  2023. );
  2024. }
  2025. // Called only if this->members_.m_start.m_cur == this->members_.m_start.m_last - 1. Note that
  2026. // if the deque has at least one element (a precondition for this member
  2027. // function), and if this->members_.m_start.m_cur == this->members_.m_start.m_last, then the deque
  2028. // must have at least two nodes.
  2029. void priv_pop_front_aux() BOOST_NOEXCEPT_OR_NOTHROW
  2030. {
  2031. allocator_traits_type::destroy
  2032. ( this->alloc()
  2033. , boost::movelib::to_raw_pointer(this->members_.m_start.m_cur)
  2034. );
  2035. this->priv_deallocate_node(this->members_.m_start.m_first);
  2036. this->members_.m_start.priv_set_node(this->members_.m_start.m_node + 1, get_block_size());
  2037. this->members_.m_start.m_cur = this->members_.m_start.m_first;
  2038. }
  2039. iterator priv_reserve_elements_at_front(size_type n)
  2040. {
  2041. size_type vacancies = size_type(this->members_.m_start.m_cur - this->members_.m_start.m_first);
  2042. if (n > vacancies){
  2043. size_type new_elems = n-vacancies;
  2044. size_type new_nodes = (new_elems + get_block_size() - 1u) / get_block_size();
  2045. size_type s = (size_type)(this->members_.m_start.m_node - this->members_.m_map);
  2046. if (new_nodes > s){
  2047. this->priv_reallocate_map(new_nodes, true);
  2048. }
  2049. size_type i = 1;
  2050. BOOST_CONTAINER_TRY {
  2051. for (; i <= new_nodes; ++i)
  2052. *(this->members_.m_start.m_node - difference_type(i)) = this->priv_allocate_node();
  2053. }
  2054. BOOST_CONTAINER_CATCH(...) {
  2055. for (size_type j = 1; j < i; ++j)
  2056. this->priv_deallocate_node(*(this->members_.m_start.m_node - difference_type(j)));
  2057. BOOST_CONTAINER_RETHROW
  2058. }
  2059. BOOST_CONTAINER_CATCH_END
  2060. }
  2061. return this->members_.m_start - difference_type(n);
  2062. }
  2063. iterator priv_reserve_elements_at_back(size_type n)
  2064. {
  2065. size_type vacancies = size_type(this->members_.m_finish.m_last - this->members_.m_finish.m_cur - 1);
  2066. if (n > vacancies){
  2067. size_type new_elems = size_type(n - vacancies);
  2068. size_type new_nodes = size_type(new_elems + get_block_size() - 1u)/get_block_size();
  2069. size_type s = (size_type)(this->members_.m_map_size - size_type(this->members_.m_finish.m_node - this->members_.m_map));
  2070. if (new_nodes + 1 > s){
  2071. this->priv_reallocate_map(new_nodes, false);
  2072. }
  2073. size_type i = 1;
  2074. BOOST_CONTAINER_TRY {
  2075. for (; i <= new_nodes; ++i)
  2076. *(this->members_.m_finish.m_node + difference_type(i)) = this->priv_allocate_node();
  2077. }
  2078. BOOST_CONTAINER_CATCH(...) {
  2079. for (size_type j = 1; j < i; ++j)
  2080. this->priv_deallocate_node(*(this->members_.m_finish.m_node + difference_type(j)));
  2081. BOOST_CONTAINER_RETHROW
  2082. }
  2083. BOOST_CONTAINER_CATCH_END
  2084. }
  2085. return this->members_.m_finish + difference_type(n);
  2086. }
  2087. void priv_reallocate_map(size_type nodes_to_add, bool add_at_front)
  2088. {
  2089. size_type old_num_nodes = size_type(this->members_.m_finish.m_node - this->members_.m_start.m_node + 1);
  2090. size_type new_num_nodes = old_num_nodes + nodes_to_add;
  2091. index_pointer new_nstart;
  2092. if (this->members_.m_map_size > 2 * new_num_nodes) {
  2093. new_nstart = this->members_.m_map + difference_type(this->members_.m_map_size - new_num_nodes) / 2
  2094. + difference_type(add_at_front ? nodes_to_add : 0u);
  2095. if (new_nstart < this->members_.m_start.m_node)
  2096. boost::container::move(this->members_.m_start.m_node, this->members_.m_finish.m_node + 1, new_nstart);
  2097. else
  2098. boost::container::move_backward
  2099. (this->members_.m_start.m_node, this->members_.m_finish.m_node + 1, new_nstart + difference_type(old_num_nodes));
  2100. }
  2101. else {
  2102. size_type new_map_size =
  2103. this->members_.m_map_size + dtl::max_value(this->members_.m_map_size, nodes_to_add) + 2;
  2104. index_pointer new_map = this->priv_allocate_map(new_map_size);
  2105. new_nstart = new_map + difference_type(new_map_size - new_num_nodes) / 2
  2106. + difference_type(add_at_front ? nodes_to_add : 0u);
  2107. boost::container::move(this->members_.m_start.m_node, this->members_.m_finish.m_node + 1, new_nstart);
  2108. this->priv_deallocate_map(this->members_.m_map, this->members_.m_map_size);
  2109. this->members_.m_map = new_map;
  2110. this->members_.m_map_size = new_map_size;
  2111. }
  2112. this->members_.m_start.priv_set_node(new_nstart, get_block_size());
  2113. this->members_.m_finish.priv_set_node(new_nstart + difference_type(old_num_nodes - 1u), get_block_size());
  2114. }
  2115. #endif //#ifndef BOOST_CONTAINER_DOXYGEN_INVOKED
  2116. };
  2117. #ifndef BOOST_CONTAINER_NO_CXX17_CTAD
  2118. template <typename InputIterator>
  2119. deque(InputIterator, InputIterator) -> deque<typename iterator_traits<InputIterator>::value_type>;
  2120. template <typename InputIterator, typename Allocator>
  2121. deque(InputIterator, InputIterator, Allocator const&) -> deque<typename iterator_traits<InputIterator>::value_type, Allocator>;
  2122. #endif
  2123. }}
  2124. #ifndef BOOST_CONTAINER_DOXYGEN_INVOKED
  2125. namespace boost {
  2126. //!has_trivial_destructor_after_move<> == true_type
  2127. //!specialization for optimizations
  2128. template <class T, class Allocator, class Options>
  2129. struct has_trivial_destructor_after_move<boost::container::deque<T, Allocator, Options> >
  2130. {
  2131. typedef typename boost::container::deque<T, Allocator, Options>::allocator_type allocator_type;
  2132. typedef typename ::boost::container::allocator_traits<allocator_type>::pointer pointer;
  2133. static const bool value = ::boost::has_trivial_destructor_after_move<allocator_type>::value &&
  2134. ::boost::has_trivial_destructor_after_move<pointer>::value;
  2135. };
  2136. }
  2137. #endif //#ifndef BOOST_CONTAINER_DOXYGEN_INVOKED
  2138. #include <boost/container/detail/config_end.hpp>
  2139. #endif // #ifndef BOOST_CONTAINER_DEQUE_HPP