00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057 #ifndef _STL_DEQUE_H
00058 #define _STL_DEQUE_H 1
00059
00060 #include <bits/concept_check.h>
00061 #include <bits/stl_iterator_base_types.h>
00062 #include <bits/stl_iterator_base_funcs.h>
00063 #include <initializer_list>
00064
00065 _GLIBCXX_BEGIN_NESTED_NAMESPACE(std, _GLIBCXX_STD_D)
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080 #ifndef _GLIBCXX_DEQUE_BUF_SIZE
00081 #define _GLIBCXX_DEQUE_BUF_SIZE 512
00082 #endif
00083
00084 inline size_t
00085 __deque_buf_size(size_t __size)
00086 { return (__size < _GLIBCXX_DEQUE_BUF_SIZE
00087 ? size_t(_GLIBCXX_DEQUE_BUF_SIZE / __size) : size_t(1)); }
00088
00089
00090
00091
00092
00093
00094
00095
00096
00097
00098
00099
00100
00101 template<typename _Tp, typename _Ref, typename _Ptr>
00102 struct _Deque_iterator
00103 {
00104 typedef _Deque_iterator<_Tp, _Tp&, _Tp*> iterator;
00105 typedef _Deque_iterator<_Tp, const _Tp&, const _Tp*> const_iterator;
00106
00107 static size_t _S_buffer_size()
00108 { return __deque_buf_size(sizeof(_Tp)); }
00109
00110 typedef std::random_access_iterator_tag iterator_category;
00111 typedef _Tp value_type;
00112 typedef _Ptr pointer;
00113 typedef _Ref reference;
00114 typedef size_t size_type;
00115 typedef ptrdiff_t difference_type;
00116 typedef _Tp** _Map_pointer;
00117 typedef _Deque_iterator _Self;
00118
00119 _Tp* _M_cur;
00120 _Tp* _M_first;
00121 _Tp* _M_last;
00122 _Map_pointer _M_node;
00123
00124 _Deque_iterator(_Tp* __x, _Map_pointer __y)
00125 : _M_cur(__x), _M_first(*__y),
00126 _M_last(*__y + _S_buffer_size()), _M_node(__y) { }
00127
00128 _Deque_iterator()
00129 : _M_cur(0), _M_first(0), _M_last(0), _M_node(0) { }
00130
00131 _Deque_iterator(const iterator& __x)
00132 : _M_cur(__x._M_cur), _M_first(__x._M_first),
00133 _M_last(__x._M_last), _M_node(__x._M_node) { }
00134
00135 reference
00136 operator*() const
00137 { return *_M_cur; }
00138
00139 pointer
00140 operator->() const
00141 { return _M_cur; }
00142
00143 _Self&
00144 operator++()
00145 {
00146 ++_M_cur;
00147 if (_M_cur == _M_last)
00148 {
00149 _M_set_node(_M_node + 1);
00150 _M_cur = _M_first;
00151 }
00152 return *this;
00153 }
00154
00155 _Self
00156 operator++(int)
00157 {
00158 _Self __tmp = *this;
00159 ++*this;
00160 return __tmp;
00161 }
00162
00163 _Self&
00164 operator--()
00165 {
00166 if (_M_cur == _M_first)
00167 {
00168 _M_set_node(_M_node - 1);
00169 _M_cur = _M_last;
00170 }
00171 --_M_cur;
00172 return *this;
00173 }
00174
00175 _Self
00176 operator--(int)
00177 {
00178 _Self __tmp = *this;
00179 --*this;
00180 return __tmp;
00181 }
00182
00183 _Self&
00184 operator+=(difference_type __n)
00185 {
00186 const difference_type __offset = __n + (_M_cur - _M_first);
00187 if (__offset >= 0 && __offset < difference_type(_S_buffer_size()))
00188 _M_cur += __n;
00189 else
00190 {
00191 const difference_type __node_offset =
00192 __offset > 0 ? __offset / difference_type(_S_buffer_size())
00193 : -difference_type((-__offset - 1)
00194 / _S_buffer_size()) - 1;
00195 _M_set_node(_M_node + __node_offset);
00196 _M_cur = _M_first + (__offset - __node_offset
00197 * difference_type(_S_buffer_size()));
00198 }
00199 return *this;
00200 }
00201
00202 _Self
00203 operator+(difference_type __n) const
00204 {
00205 _Self __tmp = *this;
00206 return __tmp += __n;
00207 }
00208
00209 _Self&
00210 operator-=(difference_type __n)
00211 { return *this += -__n; }
00212
00213 _Self
00214 operator-(difference_type __n) const
00215 {
00216 _Self __tmp = *this;
00217 return __tmp -= __n;
00218 }
00219
00220 reference
00221 operator[](difference_type __n) const
00222 { return *(*this + __n); }
00223
00224
00225
00226
00227
00228
00229 void
00230 _M_set_node(_Map_pointer __new_node)
00231 {
00232 _M_node = __new_node;
00233 _M_first = *__new_node;
00234 _M_last = _M_first + difference_type(_S_buffer_size());
00235 }
00236 };
00237
00238
00239
00240
00241 template<typename _Tp, typename _Ref, typename _Ptr>
00242 inline bool
00243 operator==(const _Deque_iterator<_Tp, _Ref, _Ptr>& __x,
00244 const _Deque_iterator<_Tp, _Ref, _Ptr>& __y)
00245 { return __x._M_cur == __y._M_cur; }
00246
00247 template<typename _Tp, typename _RefL, typename _PtrL,
00248 typename _RefR, typename _PtrR>
00249 inline bool
00250 operator==(const _Deque_iterator<_Tp, _RefL, _PtrL>& __x,
00251 const _Deque_iterator<_Tp, _RefR, _PtrR>& __y)
00252 { return __x._M_cur == __y._M_cur; }
00253
00254 template<typename _Tp, typename _Ref, typename _Ptr>
00255 inline bool
00256 operator!=(const _Deque_iterator<_Tp, _Ref, _Ptr>& __x,
00257 const _Deque_iterator<_Tp, _Ref, _Ptr>& __y)
00258 { return !(__x == __y); }
00259
00260 template<typename _Tp, typename _RefL, typename _PtrL,
00261 typename _RefR, typename _PtrR>
00262 inline bool
00263 operator!=(const _Deque_iterator<_Tp, _RefL, _PtrL>& __x,
00264 const _Deque_iterator<_Tp, _RefR, _PtrR>& __y)
00265 { return !(__x == __y); }
00266
00267 template<typename _Tp, typename _Ref, typename _Ptr>
00268 inline bool
00269 operator<(const _Deque_iterator<_Tp, _Ref, _Ptr>& __x,
00270 const _Deque_iterator<_Tp, _Ref, _Ptr>& __y)
00271 { return (__x._M_node == __y._M_node) ? (__x._M_cur < __y._M_cur)
00272 : (__x._M_node < __y._M_node); }
00273
00274 template<typename _Tp, typename _RefL, typename _PtrL,
00275 typename _RefR, typename _PtrR>
00276 inline bool
00277 operator<(const _Deque_iterator<_Tp, _RefL, _PtrL>& __x,
00278 const _Deque_iterator<_Tp, _RefR, _PtrR>& __y)
00279 { return (__x._M_node == __y._M_node) ? (__x._M_cur < __y._M_cur)
00280 : (__x._M_node < __y._M_node); }
00281
00282 template<typename _Tp, typename _Ref, typename _Ptr>
00283 inline bool
00284 operator>(const _Deque_iterator<_Tp, _Ref, _Ptr>& __x,
00285 const _Deque_iterator<_Tp, _Ref, _Ptr>& __y)
00286 { return __y < __x; }
00287
00288 template<typename _Tp, typename _RefL, typename _PtrL,
00289 typename _RefR, typename _PtrR>
00290 inline bool
00291 operator>(const _Deque_iterator<_Tp, _RefL, _PtrL>& __x,
00292 const _Deque_iterator<_Tp, _RefR, _PtrR>& __y)
00293 { return __y < __x; }
00294
00295 template<typename _Tp, typename _Ref, typename _Ptr>
00296 inline bool
00297 operator<=(const _Deque_iterator<_Tp, _Ref, _Ptr>& __x,
00298 const _Deque_iterator<_Tp, _Ref, _Ptr>& __y)
00299 { return !(__y < __x); }
00300
00301 template<typename _Tp, typename _RefL, typename _PtrL,
00302 typename _RefR, typename _PtrR>
00303 inline bool
00304 operator<=(const _Deque_iterator<_Tp, _RefL, _PtrL>& __x,
00305 const _Deque_iterator<_Tp, _RefR, _PtrR>& __y)
00306 { return !(__y < __x); }
00307
00308 template<typename _Tp, typename _Ref, typename _Ptr>
00309 inline bool
00310 operator>=(const _Deque_iterator<_Tp, _Ref, _Ptr>& __x,
00311 const _Deque_iterator<_Tp, _Ref, _Ptr>& __y)
00312 { return !(__x < __y); }
00313
00314 template<typename _Tp, typename _RefL, typename _PtrL,
00315 typename _RefR, typename _PtrR>
00316 inline bool
00317 operator>=(const _Deque_iterator<_Tp, _RefL, _PtrL>& __x,
00318 const _Deque_iterator<_Tp, _RefR, _PtrR>& __y)
00319 { return !(__x < __y); }
00320
00321
00322
00323
00324
00325 template<typename _Tp, typename _Ref, typename _Ptr>
00326 inline typename _Deque_iterator<_Tp, _Ref, _Ptr>::difference_type
00327 operator-(const _Deque_iterator<_Tp, _Ref, _Ptr>& __x,
00328 const _Deque_iterator<_Tp, _Ref, _Ptr>& __y)
00329 {
00330 return typename _Deque_iterator<_Tp, _Ref, _Ptr>::difference_type
00331 (_Deque_iterator<_Tp, _Ref, _Ptr>::_S_buffer_size())
00332 * (__x._M_node - __y._M_node - 1) + (__x._M_cur - __x._M_first)
00333 + (__y._M_last - __y._M_cur);
00334 }
00335
00336 template<typename _Tp, typename _RefL, typename _PtrL,
00337 typename _RefR, typename _PtrR>
00338 inline typename _Deque_iterator<_Tp, _RefL, _PtrL>::difference_type
00339 operator-(const _Deque_iterator<_Tp, _RefL, _PtrL>& __x,
00340 const _Deque_iterator<_Tp, _RefR, _PtrR>& __y)
00341 {
00342 return typename _Deque_iterator<_Tp, _RefL, _PtrL>::difference_type
00343 (_Deque_iterator<_Tp, _RefL, _PtrL>::_S_buffer_size())
00344 * (__x._M_node - __y._M_node - 1) + (__x._M_cur - __x._M_first)
00345 + (__y._M_last - __y._M_cur);
00346 }
00347
00348 template<typename _Tp, typename _Ref, typename _Ptr>
00349 inline _Deque_iterator<_Tp, _Ref, _Ptr>
00350 operator+(ptrdiff_t __n, const _Deque_iterator<_Tp, _Ref, _Ptr>& __x)
00351 { return __x + __n; }
00352
00353 template<typename _Tp>
00354 void
00355 fill(const _Deque_iterator<_Tp, _Tp&, _Tp*>&,
00356 const _Deque_iterator<_Tp, _Tp&, _Tp*>&, const _Tp&);
00357
00358 template<typename _Tp>
00359 _Deque_iterator<_Tp, _Tp&, _Tp*>
00360 copy(_Deque_iterator<_Tp, const _Tp&, const _Tp*>,
00361 _Deque_iterator<_Tp, const _Tp&, const _Tp*>,
00362 _Deque_iterator<_Tp, _Tp&, _Tp*>);
00363
00364 template<typename _Tp>
00365 inline _Deque_iterator<_Tp, _Tp&, _Tp*>
00366 copy(_Deque_iterator<_Tp, _Tp&, _Tp*> __first,
00367 _Deque_iterator<_Tp, _Tp&, _Tp*> __last,
00368 _Deque_iterator<_Tp, _Tp&, _Tp*> __result)
00369 { return std::copy(_Deque_iterator<_Tp, const _Tp&, const _Tp*>(__first),
00370 _Deque_iterator<_Tp, const _Tp&, const _Tp*>(__last),
00371 __result); }
00372
00373 template<typename _Tp>
00374 _Deque_iterator<_Tp, _Tp&, _Tp*>
00375 copy_backward(_Deque_iterator<_Tp, const _Tp&, const _Tp*>,
00376 _Deque_iterator<_Tp, const _Tp&, const _Tp*>,
00377 _Deque_iterator<_Tp, _Tp&, _Tp*>);
00378
00379 template<typename _Tp>
00380 inline _Deque_iterator<_Tp, _Tp&, _Tp*>
00381 copy_backward(_Deque_iterator<_Tp, _Tp&, _Tp*> __first,
00382 _Deque_iterator<_Tp, _Tp&, _Tp*> __last,
00383 _Deque_iterator<_Tp, _Tp&, _Tp*> __result)
00384 { return std::copy_backward(_Deque_iterator<_Tp,
00385 const _Tp&, const _Tp*>(__first),
00386 _Deque_iterator<_Tp,
00387 const _Tp&, const _Tp*>(__last),
00388 __result); }
00389
00390 #ifdef __GXX_EXPERIMENTAL_CXX0X__
00391 template<typename _Tp>
00392 _Deque_iterator<_Tp, _Tp&, _Tp*>
00393 move(_Deque_iterator<_Tp, const _Tp&, const _Tp*>,
00394 _Deque_iterator<_Tp, const _Tp&, const _Tp*>,
00395 _Deque_iterator<_Tp, _Tp&, _Tp*>);
00396
00397 template<typename _Tp>
00398 inline _Deque_iterator<_Tp, _Tp&, _Tp*>
00399 move(_Deque_iterator<_Tp, _Tp&, _Tp*> __first,
00400 _Deque_iterator<_Tp, _Tp&, _Tp*> __last,
00401 _Deque_iterator<_Tp, _Tp&, _Tp*> __result)
00402 { return std::move(_Deque_iterator<_Tp, const _Tp&, const _Tp*>(__first),
00403 _Deque_iterator<_Tp, const _Tp&, const _Tp*>(__last),
00404 __result); }
00405
00406 template<typename _Tp>
00407 _Deque_iterator<_Tp, _Tp&, _Tp*>
00408 move_backward(_Deque_iterator<_Tp, const _Tp&, const _Tp*>,
00409 _Deque_iterator<_Tp, const _Tp&, const _Tp*>,
00410 _Deque_iterator<_Tp, _Tp&, _Tp*>);
00411
00412 template<typename _Tp>
00413 inline _Deque_iterator<_Tp, _Tp&, _Tp*>
00414 move_backward(_Deque_iterator<_Tp, _Tp&, _Tp*> __first,
00415 _Deque_iterator<_Tp, _Tp&, _Tp*> __last,
00416 _Deque_iterator<_Tp, _Tp&, _Tp*> __result)
00417 { return std::move_backward(_Deque_iterator<_Tp,
00418 const _Tp&, const _Tp*>(__first),
00419 _Deque_iterator<_Tp,
00420 const _Tp&, const _Tp*>(__last),
00421 __result); }
00422 #endif
00423
00424
00425
00426
00427
00428
00429
00430
00431
00432
00433
00434 template<typename _Tp, typename _Alloc>
00435 class _Deque_base
00436 {
00437 public:
00438 typedef _Alloc allocator_type;
00439
00440 allocator_type
00441 get_allocator() const
00442 { return allocator_type(_M_get_Tp_allocator()); }
00443
00444 typedef _Deque_iterator<_Tp, _Tp&, _Tp*> iterator;
00445 typedef _Deque_iterator<_Tp, const _Tp&, const _Tp*> const_iterator;
00446
00447 _Deque_base()
00448 : _M_impl()
00449 { _M_initialize_map(0); }
00450
00451 _Deque_base(const allocator_type& __a, size_t __num_elements)
00452 : _M_impl(__a)
00453 { _M_initialize_map(__num_elements); }
00454
00455 _Deque_base(const allocator_type& __a)
00456 : _M_impl(__a)
00457 { }
00458
00459 #ifdef __GXX_EXPERIMENTAL_CXX0X__
00460 _Deque_base(_Deque_base&& __x)
00461 : _M_impl(__x._M_get_Tp_allocator())
00462 {
00463 _M_initialize_map(0);
00464 if (__x._M_impl._M_map)
00465 {
00466 std::swap(this->_M_impl._M_start, __x._M_impl._M_start);
00467 std::swap(this->_M_impl._M_finish, __x._M_impl._M_finish);
00468 std::swap(this->_M_impl._M_map, __x._M_impl._M_map);
00469 std::swap(this->_M_impl._M_map_size, __x._M_impl._M_map_size);
00470 }
00471 }
00472 #endif
00473
00474 ~_Deque_base();
00475
00476 protected:
00477
00478
00479
00480 typedef typename _Alloc::template rebind<_Tp*>::other _Map_alloc_type;
00481
00482 typedef typename _Alloc::template rebind<_Tp>::other _Tp_alloc_type;
00483
00484 struct _Deque_impl
00485 : public _Tp_alloc_type
00486 {
00487 _Tp** _M_map;
00488 size_t _M_map_size;
00489 iterator _M_start;
00490 iterator _M_finish;
00491
00492 _Deque_impl()
00493 : _Tp_alloc_type(), _M_map(0), _M_map_size(0),
00494 _M_start(), _M_finish()
00495 { }
00496
00497 _Deque_impl(const _Tp_alloc_type& __a)
00498 : _Tp_alloc_type(__a), _M_map(0), _M_map_size(0),
00499 _M_start(), _M_finish()
00500 { }
00501 };
00502
00503 _Tp_alloc_type&
00504 _M_get_Tp_allocator()
00505 { return *static_cast<_Tp_alloc_type*>(&this->_M_impl); }
00506
00507 const _Tp_alloc_type&
00508 _M_get_Tp_allocator() const
00509 { return *static_cast<const _Tp_alloc_type*>(&this->_M_impl); }
00510
00511 _Map_alloc_type
00512 _M_get_map_allocator() const
00513 { return _Map_alloc_type(_M_get_Tp_allocator()); }
00514
00515 _Tp*
00516 _M_allocate_node()
00517 {
00518 return _M_impl._Tp_alloc_type::allocate(__deque_buf_size(sizeof(_Tp)));
00519 }
00520
00521 void
00522 _M_deallocate_node(_Tp* __p)
00523 {
00524 _M_impl._Tp_alloc_type::deallocate(__p, __deque_buf_size(sizeof(_Tp)));
00525 }
00526
00527 _Tp**
00528 _M_allocate_map(size_t __n)
00529 { return _M_get_map_allocator().allocate(__n); }
00530
00531 void
00532 _M_deallocate_map(_Tp** __p, size_t __n)
00533 { _M_get_map_allocator().deallocate(__p, __n); }
00534
00535 protected:
00536 void _M_initialize_map(size_t);
00537 void _M_create_nodes(_Tp** __nstart, _Tp** __nfinish);
00538 void _M_destroy_nodes(_Tp** __nstart, _Tp** __nfinish);
00539 enum { _S_initial_map_size = 8 };
00540
00541 _Deque_impl _M_impl;
00542 };
00543
00544 template<typename _Tp, typename _Alloc>
00545 _Deque_base<_Tp, _Alloc>::
00546 ~_Deque_base()
00547 {
00548 if (this->_M_impl._M_map)
00549 {
00550 _M_destroy_nodes(this->_M_impl._M_start._M_node,
00551 this->_M_impl._M_finish._M_node + 1);
00552 _M_deallocate_map(this->_M_impl._M_map, this->_M_impl._M_map_size);
00553 }
00554 }
00555
00556
00557
00558
00559
00560
00561
00562
00563
00564 template<typename _Tp, typename _Alloc>
00565 void
00566 _Deque_base<_Tp, _Alloc>::
00567 _M_initialize_map(size_t __num_elements)
00568 {
00569 const size_t __num_nodes = (__num_elements/ __deque_buf_size(sizeof(_Tp))
00570 + 1);
00571
00572 this->_M_impl._M_map_size = std::max((size_t) _S_initial_map_size,
00573 size_t(__num_nodes + 2));
00574 this->_M_impl._M_map = _M_allocate_map(this->_M_impl._M_map_size);
00575
00576
00577
00578
00579
00580
00581 _Tp** __nstart = (this->_M_impl._M_map
00582 + (this->_M_impl._M_map_size - __num_nodes) / 2);
00583 _Tp** __nfinish = __nstart + __num_nodes;
00584
00585 __try
00586 { _M_create_nodes(__nstart, __nfinish); }
00587 __catch(...)
00588 {
00589 _M_deallocate_map(this->_M_impl._M_map, this->_M_impl._M_map_size);
00590 this->_M_impl._M_map = 0;
00591 this->_M_impl._M_map_size = 0;
00592 __throw_exception_again;
00593 }
00594
00595 this->_M_impl._M_start._M_set_node(__nstart);
00596 this->_M_impl._M_finish._M_set_node(__nfinish - 1);
00597 this->_M_impl._M_start._M_cur = _M_impl._M_start._M_first;
00598 this->_M_impl._M_finish._M_cur = (this->_M_impl._M_finish._M_first
00599 + __num_elements
00600 % __deque_buf_size(sizeof(_Tp)));
00601 }
00602
00603 template<typename _Tp, typename _Alloc>
00604 void
00605 _Deque_base<_Tp, _Alloc>::
00606 _M_create_nodes(_Tp** __nstart, _Tp** __nfinish)
00607 {
00608 _Tp** __cur;
00609 __try
00610 {
00611 for (__cur = __nstart; __cur < __nfinish; ++__cur)
00612 *__cur = this->_M_allocate_node();
00613 }
00614 __catch(...)
00615 {
00616 _M_destroy_nodes(__nstart, __cur);
00617 __throw_exception_again;
00618 }
00619 }
00620
00621 template<typename _Tp, typename _Alloc>
00622 void
00623 _Deque_base<_Tp, _Alloc>::
00624 _M_destroy_nodes(_Tp** __nstart, _Tp** __nfinish)
00625 {
00626 for (_Tp** __n = __nstart; __n < __nfinish; ++__n)
00627 _M_deallocate_node(*__n);
00628 }
00629
00630
00631
00632
00633
00634
00635
00636
00637
00638
00639
00640
00641
00642
00643
00644
00645
00646
00647
00648
00649
00650
00651
00652
00653
00654
00655
00656
00657
00658
00659
00660
00661
00662
00663
00664
00665
00666
00667
00668
00669
00670
00671
00672
00673
00674
00675
00676
00677
00678
00679
00680
00681
00682
00683
00684
00685
00686
00687
00688
00689
00690
00691
00692
00693
00694
00695
00696
00697
00698
00699
00700
00701
00702
00703
00704
00705
00706
00707
00708
00709
00710
00711 template<typename _Tp, typename _Alloc = std::allocator<_Tp> >
00712 class deque : protected _Deque_base<_Tp, _Alloc>
00713 {
00714
00715 typedef typename _Alloc::value_type _Alloc_value_type;
00716 __glibcxx_class_requires(_Tp, _SGIAssignableConcept)
00717 __glibcxx_class_requires2(_Tp, _Alloc_value_type, _SameTypeConcept)
00718
00719 typedef _Deque_base<_Tp, _Alloc> _Base;
00720 typedef typename _Base::_Tp_alloc_type _Tp_alloc_type;
00721
00722 public:
00723 typedef _Tp value_type;
00724 typedef typename _Tp_alloc_type::pointer pointer;
00725 typedef typename _Tp_alloc_type::const_pointer const_pointer;
00726 typedef typename _Tp_alloc_type::reference reference;
00727 typedef typename _Tp_alloc_type::const_reference const_reference;
00728 typedef typename _Base::iterator iterator;
00729 typedef typename _Base::const_iterator const_iterator;
00730 typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
00731 typedef std::reverse_iterator<iterator> reverse_iterator;
00732 typedef size_t size_type;
00733 typedef ptrdiff_t difference_type;
00734 typedef _Alloc allocator_type;
00735
00736 protected:
00737 typedef pointer* _Map_pointer;
00738
00739 static size_t _S_buffer_size()
00740 { return __deque_buf_size(sizeof(_Tp)); }
00741
00742
00743 using _Base::_M_initialize_map;
00744 using _Base::_M_create_nodes;
00745 using _Base::_M_destroy_nodes;
00746 using _Base::_M_allocate_node;
00747 using _Base::_M_deallocate_node;
00748 using _Base::_M_allocate_map;
00749 using _Base::_M_deallocate_map;
00750 using _Base::_M_get_Tp_allocator;
00751
00752
00753
00754
00755
00756 using _Base::_M_impl;
00757
00758 public:
00759
00760
00761
00762
00763
00764 deque()
00765 : _Base() { }
00766
00767
00768
00769
00770
00771 explicit
00772 deque(const allocator_type& __a)
00773 : _Base(__a, 0) { }
00774
00775
00776
00777
00778
00779
00780
00781
00782
00783 explicit
00784 deque(size_type __n, const value_type& __value = value_type(),
00785 const allocator_type& __a = allocator_type())
00786 : _Base(__a, __n)
00787 { _M_fill_initialize(__value); }
00788
00789
00790
00791
00792
00793
00794
00795
00796 deque(const deque& __x)
00797 : _Base(__x._M_get_Tp_allocator(), __x.size())
00798 { std::__uninitialized_copy_a(__x.begin(), __x.end(),
00799 this->_M_impl._M_start,
00800 _M_get_Tp_allocator()); }
00801
00802 #ifdef __GXX_EXPERIMENTAL_CXX0X__
00803
00804
00805
00806
00807
00808
00809
00810 deque(deque&& __x)
00811 : _Base(std::forward<_Base>(__x)) { }
00812
00813
00814
00815
00816
00817
00818
00819
00820
00821
00822
00823
00824 deque(initializer_list<value_type> __l,
00825 const allocator_type& __a = allocator_type())
00826 : _Base(__a)
00827 {
00828 _M_range_initialize(__l.begin(), __l.end(),
00829 random_access_iterator_tag());
00830 }
00831 #endif
00832
00833
00834
00835
00836
00837
00838
00839
00840
00841
00842
00843
00844
00845
00846
00847
00848 template<typename _InputIterator>
00849 deque(_InputIterator __first, _InputIterator __last,
00850 const allocator_type& __a = allocator_type())
00851 : _Base(__a)
00852 {
00853
00854 typedef typename std::__is_integer<_InputIterator>::__type _Integral;
00855 _M_initialize_dispatch(__first, __last, _Integral());
00856 }
00857
00858
00859
00860
00861
00862
00863 ~deque()
00864 { _M_destroy_data(begin(), end(), _M_get_Tp_allocator()); }
00865
00866
00867
00868
00869
00870
00871
00872
00873 deque&
00874 operator=(const deque& __x);
00875
00876 #ifdef __GXX_EXPERIMENTAL_CXX0X__
00877
00878
00879
00880
00881
00882
00883
00884 deque&
00885 operator=(deque&& __x)
00886 {
00887
00888
00889 this->clear();
00890 this->swap(__x);
00891 return *this;
00892 }
00893
00894
00895
00896
00897
00898
00899
00900
00901
00902
00903
00904
00905 deque&
00906 operator=(initializer_list<value_type> __l)
00907 {
00908 this->assign(__l.begin(), __l.end());
00909 return *this;
00910 }
00911 #endif
00912
00913
00914
00915
00916
00917
00918
00919
00920
00921
00922
00923 void
00924 assign(size_type __n, const value_type& __val)
00925 { _M_fill_assign(__n, __val); }
00926
00927
00928
00929
00930
00931
00932
00933
00934
00935
00936
00937
00938
00939 template<typename _InputIterator>
00940 void
00941 assign(_InputIterator __first, _InputIterator __last)
00942 {
00943 typedef typename std::__is_integer<_InputIterator>::__type _Integral;
00944 _M_assign_dispatch(__first, __last, _Integral());
00945 }
00946
00947 #ifdef __GXX_EXPERIMENTAL_CXX0X__
00948
00949
00950
00951
00952
00953
00954
00955
00956
00957
00958
00959 void
00960 assign(initializer_list<value_type> __l)
00961 { this->assign(__l.begin(), __l.end()); }
00962 #endif
00963
00964
00965 allocator_type
00966 get_allocator() const
00967 { return _Base::get_allocator(); }
00968
00969
00970
00971
00972
00973
00974 iterator
00975 begin()
00976 { return this->_M_impl._M_start; }
00977
00978
00979
00980
00981
00982 const_iterator
00983 begin() const
00984 { return this->_M_impl._M_start; }
00985
00986
00987
00988
00989
00990
00991 iterator
00992 end()
00993 { return this->_M_impl._M_finish; }
00994
00995
00996
00997
00998
00999
01000 const_iterator
01001 end() const
01002 { return this->_M_impl._M_finish; }
01003
01004
01005
01006
01007
01008
01009 reverse_iterator
01010 rbegin()
01011 { return reverse_iterator(this->_M_impl._M_finish); }
01012
01013
01014
01015
01016
01017
01018 const_reverse_iterator
01019 rbegin() const
01020 { return const_reverse_iterator(this->_M_impl._M_finish); }
01021
01022
01023
01024
01025
01026
01027 reverse_iterator
01028 rend()
01029 { return reverse_iterator(this->_M_impl._M_start); }
01030
01031
01032
01033
01034
01035
01036 const_reverse_iterator
01037 rend() const
01038 { return const_reverse_iterator(this->_M_impl._M_start); }
01039
01040 #ifdef __GXX_EXPERIMENTAL_CXX0X__
01041
01042
01043
01044
01045 const_iterator
01046 cbegin() const
01047 { return this->_M_impl._M_start; }
01048
01049
01050
01051
01052
01053
01054 const_iterator
01055 cend() const
01056 { return this->_M_impl._M_finish; }
01057
01058
01059
01060
01061
01062
01063 const_reverse_iterator
01064 crbegin() const
01065 { return const_reverse_iterator(this->_M_impl._M_finish); }
01066
01067
01068
01069
01070
01071
01072 const_reverse_iterator
01073 crend() const
01074 { return const_reverse_iterator(this->_M_impl._M_start); }
01075 #endif
01076
01077
01078
01079 size_type
01080 size() const
01081 { return this->_M_impl._M_finish - this->_M_impl._M_start; }
01082
01083
01084 size_type
01085 max_size() const
01086 { return _M_get_Tp_allocator().max_size(); }
01087
01088
01089
01090
01091
01092
01093
01094
01095
01096
01097
01098
01099 void
01100 resize(size_type __new_size, value_type __x = value_type())
01101 {
01102 const size_type __len = size();
01103 if (__new_size < __len)
01104 _M_erase_at_end(this->_M_impl._M_start + difference_type(__new_size));
01105 else
01106 insert(this->_M_impl._M_finish, __new_size - __len, __x);
01107 }
01108
01109 #ifdef __GXX_EXPERIMENTAL_CXX0X__
01110
01111 void
01112 shrink_to_fit()
01113 { std::__shrink_to_fit<deque>::_S_do_it(*this); }
01114 #endif
01115
01116
01117
01118
01119
01120 bool
01121 empty() const
01122 { return this->_M_impl._M_finish == this->_M_impl._M_start; }
01123
01124
01125
01126
01127
01128
01129
01130
01131
01132
01133
01134
01135
01136 reference
01137 operator[](size_type __n)
01138 { return this->_M_impl._M_start[difference_type(__n)]; }
01139
01140
01141
01142
01143
01144
01145
01146
01147
01148
01149
01150
01151 const_reference
01152 operator[](size_type __n) const
01153 { return this->_M_impl._M_start[difference_type(__n)]; }
01154
01155 protected:
01156
01157 void
01158 _M_range_check(size_type __n) const
01159 {
01160 if (__n >= this->size())
01161 __throw_out_of_range(__N("deque::_M_range_check"));
01162 }
01163
01164 public:
01165
01166
01167
01168
01169
01170
01171
01172
01173
01174
01175
01176 reference
01177 at(size_type __n)
01178 {
01179 _M_range_check(__n);
01180 return (*this)[__n];
01181 }
01182
01183
01184
01185
01186
01187
01188
01189
01190
01191
01192
01193
01194 const_reference
01195 at(size_type __n) const
01196 {
01197 _M_range_check(__n);
01198 return (*this)[__n];
01199 }
01200
01201
01202
01203
01204
01205 reference
01206 front()
01207 { return *begin(); }
01208
01209
01210
01211
01212
01213 const_reference
01214 front() const
01215 { return *begin(); }
01216
01217
01218
01219
01220
01221 reference
01222 back()
01223 {
01224 iterator __tmp = end();
01225 --__tmp;
01226 return *__tmp;
01227 }
01228
01229
01230
01231
01232
01233 const_reference
01234 back() const
01235 {
01236 const_iterator __tmp = end();
01237 --__tmp;
01238 return *__tmp;
01239 }
01240
01241
01242
01243
01244
01245
01246
01247
01248
01249
01250
01251 void
01252 push_front(const value_type& __x)
01253 {
01254 if (this->_M_impl._M_start._M_cur != this->_M_impl._M_start._M_first)
01255 {
01256 this->_M_impl.construct(this->_M_impl._M_start._M_cur - 1, __x);
01257 --this->_M_impl._M_start._M_cur;
01258 }
01259 else
01260 _M_push_front_aux(__x);
01261 }
01262
01263 #ifdef __GXX_EXPERIMENTAL_CXX0X__
01264 void
01265 push_front(value_type&& __x)
01266 { emplace_front(std::move(__x)); }
01267
01268 template<typename... _Args>
01269 void
01270 emplace_front(_Args&&... __args);
01271 #endif
01272
01273
01274
01275
01276
01277
01278
01279
01280
01281
01282 void
01283 push_back(const value_type& __x)
01284 {
01285 if (this->_M_impl._M_finish._M_cur
01286 != this->_M_impl._M_finish._M_last - 1)
01287 {
01288 this->_M_impl.construct(this->_M_impl._M_finish._M_cur, __x);
01289 ++this->_M_impl._M_finish._M_cur;
01290 }
01291 else
01292 _M_push_back_aux(__x);
01293 }
01294
01295 #ifdef __GXX_EXPERIMENTAL_CXX0X__
01296 void
01297 push_back(value_type&& __x)
01298 { emplace_back(std::move(__x)); }
01299
01300 template<typename... _Args>
01301 void
01302 emplace_back(_Args&&... __args);
01303 #endif
01304
01305
01306
01307
01308
01309
01310
01311
01312
01313 void
01314 pop_front()
01315 {
01316 if (this->_M_impl._M_start._M_cur
01317 != this->_M_impl._M_start._M_last - 1)
01318 {
01319 this->_M_impl.destroy(this->_M_impl._M_start._M_cur);
01320 ++this->_M_impl._M_start._M_cur;
01321 }
01322 else
01323 _M_pop_front_aux();
01324 }
01325
01326
01327
01328
01329
01330
01331
01332
01333
01334 void
01335 pop_back()
01336 {
01337 if (this->_M_impl._M_finish._M_cur
01338 != this->_M_impl._M_finish._M_first)
01339 {
01340 --this->_M_impl._M_finish._M_cur;
01341 this->_M_impl.destroy(this->_M_impl._M_finish._M_cur);
01342 }
01343 else
01344 _M_pop_back_aux();
01345 }
01346
01347 #ifdef __GXX_EXPERIMENTAL_CXX0X__
01348
01349
01350
01351
01352
01353
01354
01355
01356
01357 template<typename... _Args>
01358 iterator
01359 emplace(iterator __position, _Args&&... __args);
01360 #endif
01361
01362
01363
01364
01365
01366
01367
01368
01369
01370
01371 iterator
01372 insert(iterator __position, const value_type& __x);
01373
01374 #ifdef __GXX_EXPERIMENTAL_CXX0X__
01375
01376
01377
01378
01379
01380
01381
01382
01383
01384 iterator
01385 insert(iterator __position, value_type&& __x)
01386 { return emplace(__position, std::move(__x)); }
01387
01388
01389
01390
01391
01392
01393
01394
01395
01396
01397 void
01398 insert(iterator __p, initializer_list<value_type> __l)
01399 { this->insert(__p, __l.begin(), __l.end()); }
01400 #endif
01401
01402
01403
01404
01405
01406
01407
01408
01409
01410
01411 void
01412 insert(iterator __position, size_type __n, const value_type& __x)
01413 { _M_fill_insert(__position, __n, __x); }
01414
01415
01416
01417
01418
01419
01420
01421
01422
01423
01424
01425 template<typename _InputIterator>
01426 void
01427 insert(iterator __position, _InputIterator __first,
01428 _InputIterator __last)
01429 {
01430
01431 typedef typename std::__is_integer<_InputIterator>::__type _Integral;
01432 _M_insert_dispatch(__position, __first, __last, _Integral());
01433 }
01434
01435
01436
01437
01438
01439
01440
01441
01442
01443
01444
01445
01446
01447
01448 iterator
01449 erase(iterator __position);
01450
01451
01452
01453
01454
01455
01456
01457
01458
01459
01460
01461
01462
01463
01464
01465
01466
01467 iterator
01468 erase(iterator __first, iterator __last);
01469
01470
01471
01472
01473
01474
01475
01476
01477
01478
01479 void
01480 swap(deque& __x)
01481 {
01482 std::swap(this->_M_impl._M_start, __x._M_impl._M_start);
01483 std::swap(this->_M_impl._M_finish, __x._M_impl._M_finish);
01484 std::swap(this->_M_impl._M_map, __x._M_impl._M_map);
01485 std::swap(this->_M_impl._M_map_size, __x._M_impl._M_map_size);
01486
01487
01488
01489 std::__alloc_swap<_Tp_alloc_type>::_S_do_it(_M_get_Tp_allocator(),
01490 __x._M_get_Tp_allocator());
01491 }
01492
01493
01494
01495
01496
01497
01498
01499 void
01500 clear()
01501 { _M_erase_at_end(begin()); }
01502
01503 protected:
01504
01505
01506
01507
01508
01509
01510 template<typename _Integer>
01511 void
01512 _M_initialize_dispatch(_Integer __n, _Integer __x, __true_type)
01513 {
01514 _M_initialize_map(static_cast<size_type>(__n));
01515 _M_fill_initialize(__x);
01516 }
01517
01518
01519 template<typename _InputIterator>
01520 void
01521 _M_initialize_dispatch(_InputIterator __first, _InputIterator __last,
01522 __false_type)
01523 {
01524 typedef typename std::iterator_traits<_InputIterator>::
01525 iterator_category _IterCategory;
01526 _M_range_initialize(__first, __last, _IterCategory());
01527 }
01528
01529
01530
01531
01532
01533
01534
01535
01536
01537
01538
01539
01540
01541 template<typename _InputIterator>
01542 void
01543 _M_range_initialize(_InputIterator __first, _InputIterator __last,
01544 std::input_iterator_tag);
01545
01546
01547 template<typename _ForwardIterator>
01548 void
01549 _M_range_initialize(_ForwardIterator __first, _ForwardIterator __last,
01550 std::forward_iterator_tag);
01551
01552
01553
01554
01555
01556
01557
01558
01559
01560
01561
01562
01563 void
01564 _M_fill_initialize(const value_type& __value);
01565
01566
01567
01568
01569
01570
01571
01572
01573 template<typename _Integer>
01574 void
01575 _M_assign_dispatch(_Integer __n, _Integer __val, __true_type)
01576 { _M_fill_assign(__n, __val); }
01577
01578
01579 template<typename _InputIterator>
01580 void
01581 _M_assign_dispatch(_InputIterator __first, _InputIterator __last,
01582 __false_type)
01583 {
01584 typedef typename std::iterator_traits<_InputIterator>::
01585 iterator_category _IterCategory;
01586 _M_assign_aux(__first, __last, _IterCategory());
01587 }
01588
01589
01590 template<typename _InputIterator>
01591 void
01592 _M_assign_aux(_InputIterator __first, _InputIterator __last,
01593 std::input_iterator_tag);
01594
01595
01596 template<typename _ForwardIterator>
01597 void
01598 _M_assign_aux(_ForwardIterator __first, _ForwardIterator __last,
01599 std::forward_iterator_tag)
01600 {
01601 const size_type __len = std::distance(__first, __last);
01602 if (__len > size())
01603 {
01604 _ForwardIterator __mid = __first;
01605 std::advance(__mid, size());
01606 std::copy(__first, __mid, begin());
01607 insert(end(), __mid, __last);
01608 }
01609 else
01610 _M_erase_at_end(std::copy(__first, __last, begin()));
01611 }
01612
01613
01614
01615 void
01616 _M_fill_assign(size_type __n, const value_type& __val)
01617 {
01618 if (__n > size())
01619 {
01620 std::fill(begin(), end(), __val);
01621 insert(end(), __n - size(), __val);
01622 }
01623 else
01624 {
01625 _M_erase_at_end(begin() + difference_type(__n));
01626 std::fill(begin(), end(), __val);
01627 }
01628 }
01629
01630
01631
01632 #ifndef __GXX_EXPERIMENTAL_CXX0X__
01633 void _M_push_back_aux(const value_type&);
01634
01635 void _M_push_front_aux(const value_type&);
01636 #else
01637 template<typename... _Args>
01638 void _M_push_back_aux(_Args&&... __args);
01639
01640 template<typename... _Args>
01641 void _M_push_front_aux(_Args&&... __args);
01642 #endif
01643
01644 void _M_pop_back_aux();
01645
01646 void _M_pop_front_aux();
01647
01648
01649
01650
01651
01652
01653
01654
01655
01656 template<typename _Integer>
01657 void
01658 _M_insert_dispatch(iterator __pos,
01659 _Integer __n, _Integer __x, __true_type)
01660 { _M_fill_insert(__pos, __n, __x); }
01661
01662
01663 template<typename _InputIterator>
01664 void
01665 _M_insert_dispatch(iterator __pos,
01666 _InputIterator __first, _InputIterator __last,
01667 __false_type)
01668 {
01669 typedef typename std::iterator_traits<_InputIterator>::
01670 iterator_category _IterCategory;
01671 _M_range_insert_aux(__pos, __first, __last, _IterCategory());
01672 }
01673
01674
01675 template<typename _InputIterator>
01676 void
01677 _M_range_insert_aux(iterator __pos, _InputIterator __first,
01678 _InputIterator __last, std::input_iterator_tag);
01679
01680
01681 template<typename _ForwardIterator>
01682 void
01683 _M_range_insert_aux(iterator __pos, _ForwardIterator __first,
01684 _ForwardIterator __last, std::forward_iterator_tag);
01685
01686
01687
01688
01689 void
01690 _M_fill_insert(iterator __pos, size_type __n, const value_type& __x);
01691
01692
01693 #ifndef __GXX_EXPERIMENTAL_CXX0X__
01694 iterator
01695 _M_insert_aux(iterator __pos, const value_type& __x);
01696 #else
01697 template<typename... _Args>
01698 iterator
01699 _M_insert_aux(iterator __pos, _Args&&... __args);
01700 #endif
01701
01702
01703 void
01704 _M_insert_aux(iterator __pos, size_type __n, const value_type& __x);
01705
01706
01707 template<typename _ForwardIterator>
01708 void
01709 _M_insert_aux(iterator __pos,
01710 _ForwardIterator __first, _ForwardIterator __last,
01711 size_type __n);
01712
01713
01714
01715
01716 void
01717 _M_destroy_data_aux(iterator __first, iterator __last);
01718
01719
01720
01721 template<typename _Alloc1>
01722 void
01723 _M_destroy_data(iterator __first, iterator __last, const _Alloc1&)
01724 { _M_destroy_data_aux(__first, __last); }
01725
01726 void
01727 _M_destroy_data(iterator __first, iterator __last,
01728 const std::allocator<_Tp>&)
01729 {
01730 if (!__has_trivial_destructor(value_type))
01731 _M_destroy_data_aux(__first, __last);
01732 }
01733
01734
01735 void
01736 _M_erase_at_begin(iterator __pos)
01737 {
01738 _M_destroy_data(begin(), __pos, _M_get_Tp_allocator());
01739 _M_destroy_nodes(this->_M_impl._M_start._M_node, __pos._M_node);
01740 this->_M_impl._M_start = __pos;
01741 }
01742
01743
01744
01745 void
01746 _M_erase_at_end(iterator __pos)
01747 {
01748 _M_destroy_data(__pos, end(), _M_get_Tp_allocator());
01749 _M_destroy_nodes(__pos._M_node + 1,
01750 this->_M_impl._M_finish._M_node + 1);
01751 this->_M_impl._M_finish = __pos;
01752 }
01753
01754
01755
01756 iterator
01757 _M_reserve_elements_at_front(size_type __n)
01758 {
01759 const size_type __vacancies = this->_M_impl._M_start._M_cur
01760 - this->_M_impl._M_start._M_first;
01761 if (__n > __vacancies)
01762 _M_new_elements_at_front(__n - __vacancies);
01763 return this->_M_impl._M_start - difference_type(__n);
01764 }
01765
01766 iterator
01767 _M_reserve_elements_at_back(size_type __n)
01768 {
01769 const size_type __vacancies = (this->_M_impl._M_finish._M_last
01770 - this->_M_impl._M_finish._M_cur) - 1;
01771 if (__n > __vacancies)
01772 _M_new_elements_at_back(__n - __vacancies);
01773 return this->_M_impl._M_finish + difference_type(__n);
01774 }
01775
01776 void
01777 _M_new_elements_at_front(size_type __new_elements);
01778
01779 void
01780 _M_new_elements_at_back(size_type __new_elements);
01781
01782
01783
01784
01785
01786
01787
01788
01789
01790
01791
01792 void
01793 _M_reserve_map_at_back(size_type __nodes_to_add = 1)
01794 {
01795 if (__nodes_to_add + 1 > this->_M_impl._M_map_size
01796 - (this->_M_impl._M_finish._M_node - this->_M_impl._M_map))
01797 _M_reallocate_map(__nodes_to_add, false);
01798 }
01799
01800 void
01801 _M_reserve_map_at_front(size_type __nodes_to_add = 1)
01802 {
01803 if (__nodes_to_add > size_type(this->_M_impl._M_start._M_node
01804 - this->_M_impl._M_map))
01805 _M_reallocate_map(__nodes_to_add, true);
01806 }
01807
01808 void
01809 _M_reallocate_map(size_type __nodes_to_add, bool __add_at_front);
01810
01811 };
01812
01813
01814
01815
01816
01817
01818
01819
01820
01821
01822
01823
01824 template<typename _Tp, typename _Alloc>
01825 inline bool
01826 operator==(const deque<_Tp, _Alloc>& __x,
01827 const deque<_Tp, _Alloc>& __y)
01828 { return __x.size() == __y.size()
01829 && std::equal(__x.begin(), __x.end(), __y.begin()); }
01830
01831
01832
01833
01834
01835
01836
01837
01838
01839
01840
01841
01842 template<typename _Tp, typename _Alloc>
01843 inline bool
01844 operator<(const deque<_Tp, _Alloc>& __x,
01845 const deque<_Tp, _Alloc>& __y)
01846 { return std::lexicographical_compare(__x.begin(), __x.end(),
01847 __y.begin(), __y.end()); }
01848
01849
01850 template<typename _Tp, typename _Alloc>
01851 inline bool
01852 operator!=(const deque<_Tp, _Alloc>& __x,
01853 const deque<_Tp, _Alloc>& __y)
01854 { return !(__x == __y); }
01855
01856
01857 template<typename _Tp, typename _Alloc>
01858 inline bool
01859 operator>(const deque<_Tp, _Alloc>& __x,
01860 const deque<_Tp, _Alloc>& __y)
01861 { return __y < __x; }
01862
01863
01864 template<typename _Tp, typename _Alloc>
01865 inline bool
01866 operator<=(const deque<_Tp, _Alloc>& __x,
01867 const deque<_Tp, _Alloc>& __y)
01868 { return !(__y < __x); }
01869
01870
01871 template<typename _Tp, typename _Alloc>
01872 inline bool
01873 operator>=(const deque<_Tp, _Alloc>& __x,
01874 const deque<_Tp, _Alloc>& __y)
01875 { return !(__x < __y); }
01876
01877
01878 template<typename _Tp, typename _Alloc>
01879 inline void
01880 swap(deque<_Tp,_Alloc>& __x, deque<_Tp,_Alloc>& __y)
01881 { __x.swap(__y); }
01882
01883 #undef _GLIBCXX_DEQUE_BUF_SIZE
01884
01885 _GLIBCXX_END_NESTED_NAMESPACE
01886
01887 #endif