deque.hpp 85 KB

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