deque.hpp 80 KB

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