deque.hpp 85 KB

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