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