deque.hpp 84 KB

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