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
00058
00059
00060
00061 #ifndef _DEQUE_H
00062 #define _DEQUE_H 1
00063
00064 #include <bits/concept_check.h>
00065 #include <bits/stl_iterator_base_types.h>
00066 #include <bits/stl_iterator_base_funcs.h>
00067
00068 namespace _GLIBCXX_STD
00069 {
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082 inline size_t
00083 __deque_buf_size(size_t __size)
00084 { return __size < 512 ? size_t(512 / __size) : size_t(1); }
00085
00086
00087
00088
00089
00090
00091
00092
00093
00094
00095
00096
00097
00098
00099
00100 template<typename _Tp, typename _Ref, typename _Ptr>
00101 struct _Deque_iterator
00102 {
00103 typedef _Deque_iterator<_Tp, _Tp&, _Tp*> iterator;
00104 typedef _Deque_iterator<_Tp, const _Tp&, const _Tp*> const_iterator;
00105
00106 static size_t _S_buffer_size()
00107 { return __deque_buf_size(sizeof(_Tp)); }
00108
00109 typedef random_access_iterator_tag iterator_category;
00110 typedef _Tp value_type;
00111 typedef _Ptr pointer;
00112 typedef _Ref reference;
00113 typedef size_t size_type;
00114 typedef ptrdiff_t difference_type;
00115 typedef _Tp** _Map_pointer;
00116 typedef _Deque_iterator _Self;
00117
00118 _Tp* _M_cur;
00119 _Tp* _M_first;
00120 _Tp* _M_last;
00121 _Map_pointer _M_node;
00122
00123 _Deque_iterator(_Tp* __x, _Map_pointer __y)
00124 : _M_cur(__x), _M_first(*__y),
00125 _M_last(*__y + _S_buffer_size()), _M_node(__y) {}
00126
00127 _Deque_iterator() : _M_cur(0), _M_first(0), _M_last(0), _M_node(0) {}
00128
00129 _Deque_iterator(const iterator& __x)
00130 : _M_cur(__x._M_cur), _M_first(__x._M_first),
00131 _M_last(__x._M_last), _M_node(__x._M_node) {}
00132
00133 reference
00134 operator*() const
00135 { return *_M_cur; }
00136
00137 pointer
00138 operator->() const
00139 { return _M_cur; }
00140
00141 _Self&
00142 operator++()
00143 {
00144 ++_M_cur;
00145 if (_M_cur == _M_last)
00146 {
00147 _M_set_node(_M_node + 1);
00148 _M_cur = _M_first;
00149 }
00150 return *this;
00151 }
00152
00153 _Self
00154 operator++(int)
00155 {
00156 _Self __tmp = *this;
00157 ++*this;
00158 return __tmp;
00159 }
00160
00161 _Self&
00162 operator--()
00163 {
00164 if (_M_cur == _M_first)
00165 {
00166 _M_set_node(_M_node - 1);
00167 _M_cur = _M_last;
00168 }
00169 --_M_cur;
00170 return *this;
00171 }
00172
00173 _Self
00174 operator--(int)
00175 {
00176 _Self __tmp = *this;
00177 --*this;
00178 return __tmp;
00179 }
00180
00181 _Self&
00182 operator+=(difference_type __n)
00183 {
00184 const difference_type __offset = __n + (_M_cur - _M_first);
00185 if (__offset >= 0 && __offset < difference_type(_S_buffer_size()))
00186 _M_cur += __n;
00187 else
00188 {
00189 const difference_type __node_offset =
00190 __offset > 0 ? __offset / difference_type(_S_buffer_size())
00191 : -difference_type((-__offset - 1)
00192 / _S_buffer_size()) - 1;
00193 _M_set_node(_M_node + __node_offset);
00194 _M_cur = _M_first + (__offset - __node_offset
00195 * difference_type(_S_buffer_size()));
00196 }
00197 return *this;
00198 }
00199
00200 _Self
00201 operator+(difference_type __n) const
00202 {
00203 _Self __tmp = *this;
00204 return __tmp += __n;
00205 }
00206
00207 _Self&
00208 operator-=(difference_type __n)
00209 { return *this += -__n; }
00210
00211 _Self
00212 operator-(difference_type __n) const
00213 {
00214 _Self __tmp = *this;
00215 return __tmp -= __n;
00216 }
00217
00218 reference
00219 operator[](difference_type __n) const
00220 { return *(*this + __n); }
00221
00222
00223
00224
00225
00226
00227
00228 void
00229 _M_set_node(_Map_pointer __new_node)
00230 {
00231 _M_node = __new_node;
00232 _M_first = *__new_node;
00233 _M_last = _M_first + difference_type(_S_buffer_size());
00234 }
00235 };
00236
00237
00238
00239
00240 template<typename _Tp, typename _Ref, typename _Ptr>
00241 inline bool
00242 operator==(const _Deque_iterator<_Tp, _Ref, _Ptr>& __x,
00243 const _Deque_iterator<_Tp, _Ref, _Ptr>& __y)
00244 { return __x._M_cur == __y._M_cur; }
00245
00246 template<typename _Tp, typename _RefL, typename _PtrL,
00247 typename _RefR, typename _PtrR>
00248 inline bool
00249 operator==(const _Deque_iterator<_Tp, _RefL, _PtrL>& __x,
00250 const _Deque_iterator<_Tp, _RefR, _PtrR>& __y)
00251 { return __x._M_cur == __y._M_cur; }
00252
00253 template<typename _Tp, typename _Ref, typename _Ptr>
00254 inline bool
00255 operator!=(const _Deque_iterator<_Tp, _Ref, _Ptr>& __x,
00256 const _Deque_iterator<_Tp, _Ref, _Ptr>& __y)
00257 { return !(__x == __y); }
00258
00259 template<typename _Tp, typename _RefL, typename _PtrL,
00260 typename _RefR, typename _PtrR>
00261 inline bool
00262 operator!=(const _Deque_iterator<_Tp, _RefL, _PtrL>& __x,
00263 const _Deque_iterator<_Tp, _RefR, _PtrR>& __y)
00264 { return !(__x == __y); }
00265
00266 template<typename _Tp, typename _Ref, typename _Ptr>
00267 inline bool
00268 operator<(const _Deque_iterator<_Tp, _Ref, _Ptr>& __x,
00269 const _Deque_iterator<_Tp, _Ref, _Ptr>& __y)
00270 { return (__x._M_node == __y._M_node) ? (__x._M_cur < __y._M_cur)
00271 : (__x._M_node < __y._M_node); }
00272
00273 template<typename _Tp, typename _RefL, typename _PtrL,
00274 typename _RefR, typename _PtrR>
00275 inline bool
00276 operator<(const _Deque_iterator<_Tp, _RefL, _PtrL>& __x,
00277 const _Deque_iterator<_Tp, _RefR, _PtrR>& __y)
00278 { return (__x._M_node == __y._M_node) ? (__x._M_cur < __y._M_cur)
00279 : (__x._M_node < __y._M_node); }
00280
00281 template<typename _Tp, typename _Ref, typename _Ptr>
00282 inline bool
00283 operator>(const _Deque_iterator<_Tp, _Ref, _Ptr>& __x,
00284 const _Deque_iterator<_Tp, _Ref, _Ptr>& __y)
00285 { return __y < __x; }
00286
00287 template<typename _Tp, typename _RefL, typename _PtrL,
00288 typename _RefR, typename _PtrR>
00289 inline bool
00290 operator>(const _Deque_iterator<_Tp, _RefL, _PtrL>& __x,
00291 const _Deque_iterator<_Tp, _RefR, _PtrR>& __y)
00292 { return __y < __x; }
00293
00294 template<typename _Tp, typename _Ref, typename _Ptr>
00295 inline bool
00296 operator<=(const _Deque_iterator<_Tp, _Ref, _Ptr>& __x,
00297 const _Deque_iterator<_Tp, _Ref, _Ptr>& __y)
00298 { return !(__y < __x); }
00299
00300 template<typename _Tp, typename _RefL, typename _PtrL,
00301 typename _RefR, typename _PtrR>
00302 inline bool
00303 operator<=(const _Deque_iterator<_Tp, _RefL, _PtrL>& __x,
00304 const _Deque_iterator<_Tp, _RefR, _PtrR>& __y)
00305 { return !(__y < __x); }
00306
00307 template<typename _Tp, typename _Ref, typename _Ptr>
00308 inline bool
00309 operator>=(const _Deque_iterator<_Tp, _Ref, _Ptr>& __x,
00310 const _Deque_iterator<_Tp, _Ref, _Ptr>& __y)
00311 { return !(__x < __y); }
00312
00313 template<typename _Tp, typename _RefL, typename _PtrL,
00314 typename _RefR, typename _PtrR>
00315 inline bool
00316 operator>=(const _Deque_iterator<_Tp, _RefL, _PtrL>& __x,
00317 const _Deque_iterator<_Tp, _RefR, _PtrR>& __y)
00318 { return !(__x < __y); }
00319
00320
00321
00322
00323
00324 template<typename _Tp, typename _RefL, typename _PtrL,
00325 typename _RefR, typename _PtrR>
00326 inline typename _Deque_iterator<_Tp, _RefL, _PtrL>::difference_type
00327 operator-(const _Deque_iterator<_Tp, _RefL, _PtrL>& __x,
00328 const _Deque_iterator<_Tp, _RefR, _PtrR>& __y)
00329 {
00330 return typename _Deque_iterator<_Tp, _RefL, _PtrL>::difference_type
00331 (_Deque_iterator<_Tp, _RefL, _PtrL>::_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 _Ref, typename _Ptr>
00337 inline _Deque_iterator<_Tp, _Ref, _Ptr>
00338 operator+(ptrdiff_t __n, const _Deque_iterator<_Tp, _Ref, _Ptr>& __x)
00339 { return __x + __n; }
00340
00341
00342
00343
00344
00345
00346
00347
00348
00349
00350
00351
00352
00353 template<typename _Tp, typename _Alloc>
00354 class _Deque_base
00355 {
00356 public:
00357 typedef _Alloc allocator_type;
00358
00359 allocator_type
00360 get_allocator() const
00361 { return *static_cast<const _Alloc*>(&this->_M_impl); }
00362
00363 typedef _Deque_iterator<_Tp, _Tp&, _Tp*> iterator;
00364 typedef _Deque_iterator<_Tp, const _Tp&, const _Tp*> const_iterator;
00365
00366 _Deque_base(const allocator_type& __a, size_t __num_elements)
00367 : _M_impl(__a)
00368 { _M_initialize_map(__num_elements); }
00369
00370 _Deque_base(const allocator_type& __a)
00371 : _M_impl(__a)
00372 { }
00373
00374 ~_Deque_base();
00375
00376 protected:
00377
00378
00379
00380 struct _Deque_impl
00381 : public _Alloc
00382 {
00383 _Tp** _M_map;
00384 size_t _M_map_size;
00385 iterator _M_start;
00386 iterator _M_finish;
00387
00388 _Deque_impl(const _Alloc& __a)
00389 : _Alloc(__a), _M_map(0), _M_map_size(0), _M_start(), _M_finish()
00390 { }
00391 };
00392
00393 typedef typename _Alloc::template rebind<_Tp*>::other _Map_alloc_type;
00394 _Map_alloc_type _M_get_map_allocator() const
00395 { return _Map_alloc_type(this->get_allocator()); }
00396
00397 _Tp*
00398 _M_allocate_node()
00399 { return _M_impl._Alloc::allocate(__deque_buf_size(sizeof(_Tp))); }
00400
00401 void
00402 _M_deallocate_node(_Tp* __p)
00403 { _M_impl._Alloc::deallocate(__p, __deque_buf_size(sizeof(_Tp))); }
00404
00405 _Tp**
00406 _M_allocate_map(size_t __n)
00407 { return _M_get_map_allocator().allocate(__n); }
00408
00409 void
00410 _M_deallocate_map(_Tp** __p, size_t __n)
00411 { _M_get_map_allocator().deallocate(__p, __n); }
00412
00413 protected:
00414 void _M_initialize_map(size_t);
00415 void _M_create_nodes(_Tp** __nstart, _Tp** __nfinish);
00416 void _M_destroy_nodes(_Tp** __nstart, _Tp** __nfinish);
00417 enum { _S_initial_map_size = 8 };
00418
00419 _Deque_impl _M_impl;
00420 };
00421
00422 template<typename _Tp, typename _Alloc>
00423 _Deque_base<_Tp, _Alloc>::
00424 ~_Deque_base()
00425 {
00426 if (this->_M_impl._M_map)
00427 {
00428 _M_destroy_nodes(this->_M_impl._M_start._M_node,
00429 this->_M_impl._M_finish._M_node + 1);
00430 _M_deallocate_map(this->_M_impl._M_map, this->_M_impl._M_map_size);
00431 }
00432 }
00433
00434
00435
00436
00437
00438
00439
00440
00441
00442
00443
00444 template<typename _Tp, typename _Alloc>
00445 void
00446 _Deque_base<_Tp, _Alloc>::
00447 _M_initialize_map(size_t __num_elements)
00448 {
00449 const size_t __num_nodes = (__num_elements/ __deque_buf_size(sizeof(_Tp))
00450 + 1);
00451
00452 this->_M_impl._M_map_size = std::max((size_t) _S_initial_map_size,
00453 size_t(__num_nodes + 2));
00454 this->_M_impl._M_map = _M_allocate_map(this->_M_impl._M_map_size);
00455
00456
00457
00458
00459
00460
00461 _Tp** __nstart = (this->_M_impl._M_map
00462 + (this->_M_impl._M_map_size - __num_nodes) / 2);
00463 _Tp** __nfinish = __nstart + __num_nodes;
00464
00465 try
00466 { _M_create_nodes(__nstart, __nfinish); }
00467 catch(...)
00468 {
00469 _M_deallocate_map(this->_M_impl._M_map, this->_M_impl._M_map_size);
00470 this->_M_impl._M_map = 0;
00471 this->_M_impl._M_map_size = 0;
00472 __throw_exception_again;
00473 }
00474
00475 this->_M_impl._M_start._M_set_node(__nstart);
00476 this->_M_impl._M_finish._M_set_node(__nfinish - 1);
00477 this->_M_impl._M_start._M_cur = _M_impl._M_start._M_first;
00478 this->_M_impl._M_finish._M_cur = (this->_M_impl._M_finish._M_first
00479 + __num_elements
00480 % __deque_buf_size(sizeof(_Tp)));
00481 }
00482
00483 template<typename _Tp, typename _Alloc>
00484 void
00485 _Deque_base<_Tp, _Alloc>::
00486 _M_create_nodes(_Tp** __nstart, _Tp** __nfinish)
00487 {
00488 _Tp** __cur;
00489 try
00490 {
00491 for (__cur = __nstart; __cur < __nfinish; ++__cur)
00492 *__cur = this->_M_allocate_node();
00493 }
00494 catch(...)
00495 {
00496 _M_destroy_nodes(__nstart, __cur);
00497 __throw_exception_again;
00498 }
00499 }
00500
00501 template<typename _Tp, typename _Alloc>
00502 void
00503 _Deque_base<_Tp, _Alloc>::
00504 _M_destroy_nodes(_Tp** __nstart, _Tp** __nfinish)
00505 {
00506 for (_Tp** __n = __nstart; __n < __nfinish; ++__n)
00507 _M_deallocate_node(*__n);
00508 }
00509
00510
00511
00512
00513
00514
00515
00516
00517
00518
00519
00520
00521
00522
00523
00524
00525
00526
00527
00528
00529
00530
00531
00532
00533
00534
00535
00536
00537
00538
00539
00540
00541
00542
00543
00544
00545
00546
00547
00548
00549
00550
00551
00552
00553
00554
00555
00556
00557
00558
00559
00560
00561
00562
00563
00564
00565
00566
00567
00568
00569
00570
00571
00572
00573
00574
00575
00576
00577
00578
00579
00580
00581
00582
00583
00584
00585
00586
00587
00588
00589
00590
00591
00592
00593
00594 template<typename _Tp, typename _Alloc = allocator<_Tp> >
00595 class deque : protected _Deque_base<_Tp, _Alloc>
00596 {
00597
00598 __glibcxx_class_requires(_Tp, _SGIAssignableConcept)
00599
00600 typedef _Deque_base<_Tp, _Alloc> _Base;
00601
00602 public:
00603 typedef _Tp value_type;
00604 typedef typename _Alloc::pointer pointer;
00605 typedef typename _Alloc::const_pointer const_pointer;
00606 typedef typename _Alloc::reference reference;
00607 typedef typename _Alloc::const_reference const_reference;
00608 typedef typename _Base::iterator iterator;
00609 typedef typename _Base::const_iterator const_iterator;
00610 typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
00611 typedef std::reverse_iterator<iterator> reverse_iterator;
00612 typedef size_t size_type;
00613 typedef ptrdiff_t difference_type;
00614 typedef typename _Base::allocator_type allocator_type;
00615
00616 protected:
00617 typedef pointer* _Map_pointer;
00618
00619 static size_t _S_buffer_size()
00620 { return __deque_buf_size(sizeof(_Tp)); }
00621
00622
00623 using _Base::_M_initialize_map;
00624 using _Base::_M_create_nodes;
00625 using _Base::_M_destroy_nodes;
00626 using _Base::_M_allocate_node;
00627 using _Base::_M_deallocate_node;
00628 using _Base::_M_allocate_map;
00629 using _Base::_M_deallocate_map;
00630
00631
00632
00633
00634
00635
00636 using _Base::_M_impl;
00637
00638 public:
00639
00640
00641
00642
00643
00644 explicit
00645 deque(const allocator_type& __a = allocator_type())
00646 : _Base(__a, 0) {}
00647
00648
00649
00650
00651
00652
00653
00654
00655 deque(size_type __n, const value_type& __value,
00656 const allocator_type& __a = allocator_type())
00657 : _Base(__a, __n)
00658 { _M_fill_initialize(__value); }
00659
00660
00661
00662
00663
00664
00665
00666
00667 explicit
00668 deque(size_type __n)
00669 : _Base(allocator_type(), __n)
00670 { _M_fill_initialize(value_type()); }
00671
00672
00673
00674
00675
00676
00677
00678
00679 deque(const deque& __x)
00680 : _Base(__x.get_allocator(), __x.size())
00681 { std::__uninitialized_copy_a(__x.begin(), __x.end(),
00682 this->_M_impl._M_start,
00683 this->get_allocator()); }
00684
00685
00686
00687
00688
00689
00690
00691
00692
00693
00694
00695
00696
00697
00698
00699 template<typename _InputIterator>
00700 deque(_InputIterator __first, _InputIterator __last,
00701 const allocator_type& __a = allocator_type())
00702 : _Base(__a)
00703 {
00704
00705 typedef typename std::__is_integer<_InputIterator>::__type _Integral;
00706 _M_initialize_dispatch(__first, __last, _Integral());
00707 }
00708
00709
00710
00711
00712
00713
00714 ~deque()
00715 { std::_Destroy(this->_M_impl._M_start, this->_M_impl._M_finish,
00716 this->get_allocator()); }
00717
00718
00719
00720
00721
00722
00723
00724
00725 deque&
00726 operator=(const deque& __x);
00727
00728
00729
00730
00731
00732
00733
00734
00735
00736
00737
00738 void
00739 assign(size_type __n, const value_type& __val)
00740 { _M_fill_assign(__n, __val); }
00741
00742
00743
00744
00745
00746
00747
00748
00749
00750
00751
00752
00753
00754 template<typename _InputIterator>
00755 void
00756 assign(_InputIterator __first, _InputIterator __last)
00757 {
00758 typedef typename std::__is_integer<_InputIterator>::__type _Integral;
00759 _M_assign_dispatch(__first, __last, _Integral());
00760 }
00761
00762
00763 allocator_type
00764 get_allocator() const
00765 { return _Base::get_allocator(); }
00766
00767
00768
00769
00770
00771
00772 iterator
00773 begin()
00774 { return this->_M_impl._M_start; }
00775
00776
00777
00778
00779
00780 const_iterator
00781 begin() const
00782 { return this->_M_impl._M_start; }
00783
00784
00785
00786
00787
00788
00789 iterator
00790 end()
00791 { return this->_M_impl._M_finish; }
00792
00793
00794
00795
00796
00797
00798 const_iterator
00799 end() const
00800 { return this->_M_impl._M_finish; }
00801
00802
00803
00804
00805
00806
00807 reverse_iterator
00808 rbegin()
00809 { return reverse_iterator(this->_M_impl._M_finish); }
00810
00811
00812
00813
00814
00815
00816 const_reverse_iterator
00817 rbegin() const
00818 { return const_reverse_iterator(this->_M_impl._M_finish); }
00819
00820
00821
00822
00823
00824
00825 reverse_iterator
00826 rend() { return reverse_iterator(this->_M_impl._M_start); }
00827
00828
00829
00830
00831
00832
00833 const_reverse_iterator
00834 rend() const
00835 { return const_reverse_iterator(this->_M_impl._M_start); }
00836
00837
00838
00839 size_type
00840 size() const
00841 { return this->_M_impl._M_finish - this->_M_impl._M_start; }
00842
00843
00844 size_type
00845 max_size() const
00846 { return size_type(-1); }
00847
00848
00849
00850
00851
00852
00853
00854
00855
00856
00857
00858
00859 void
00860 resize(size_type __new_size, const value_type& __x)
00861 {
00862 const size_type __len = size();
00863 if (__new_size < __len)
00864 erase(this->_M_impl._M_start + __new_size, this->_M_impl._M_finish);
00865 else
00866 insert(this->_M_impl._M_finish, __new_size - __len, __x);
00867 }
00868
00869
00870
00871
00872
00873
00874
00875
00876
00877
00878 void
00879 resize(size_type new_size)
00880 { resize(new_size, value_type()); }
00881
00882
00883
00884
00885
00886 bool
00887 empty() const
00888 { return this->_M_impl._M_finish == this->_M_impl._M_start; }
00889
00890
00891
00892
00893
00894
00895
00896
00897
00898
00899
00900
00901
00902 reference
00903 operator[](size_type __n)
00904 { return this->_M_impl._M_start[difference_type(__n)]; }
00905
00906
00907
00908
00909
00910
00911
00912
00913
00914
00915
00916
00917 const_reference
00918 operator[](size_type __n) const
00919 { return this->_M_impl._M_start[difference_type(__n)]; }
00920
00921 protected:
00922
00923 void
00924 _M_range_check(size_type __n) const
00925 {
00926 if (__n >= this->size())
00927 __throw_out_of_range(__N("deque::_M_range_check"));
00928 }
00929
00930 public:
00931
00932
00933
00934
00935
00936
00937
00938
00939
00940
00941
00942 reference
00943 at(size_type __n)
00944 {
00945 _M_range_check(__n);
00946 return (*this)[__n];
00947 }
00948
00949
00950
00951
00952
00953
00954
00955
00956
00957
00958
00959
00960 const_reference
00961 at(size_type __n) const
00962 {
00963 _M_range_check(__n);
00964 return (*this)[__n];
00965 }
00966
00967
00968
00969
00970
00971 reference
00972 front()
00973 { return *begin(); }
00974
00975
00976
00977
00978
00979 const_reference
00980 front() const
00981 { return *begin(); }
00982
00983
00984
00985
00986
00987 reference
00988 back()
00989 {
00990 iterator __tmp = end();
00991 --__tmp;
00992 return *__tmp;
00993 }
00994
00995
00996
00997
00998
00999 const_reference
01000 back() const
01001 {
01002 const_iterator __tmp = end();
01003 --__tmp;
01004 return *__tmp;
01005 }
01006
01007
01008
01009
01010
01011
01012
01013
01014
01015
01016
01017 void
01018 push_front(const value_type& __x)
01019 {
01020 if (this->_M_impl._M_start._M_cur != this->_M_impl._M_start._M_first)
01021 {
01022 this->_M_impl.construct(this->_M_impl._M_start._M_cur - 1, __x);
01023 --this->_M_impl._M_start._M_cur;
01024 }
01025 else
01026 _M_push_front_aux(__x);
01027 }
01028
01029
01030
01031
01032
01033
01034
01035
01036
01037
01038 void
01039 push_back(const value_type& __x)
01040 {
01041 if (this->_M_impl._M_finish._M_cur
01042 != this->_M_impl._M_finish._M_last - 1)
01043 {
01044 this->_M_impl.construct(this->_M_impl._M_finish._M_cur, __x);
01045 ++this->_M_impl._M_finish._M_cur;
01046 }
01047 else
01048 _M_push_back_aux(__x);
01049 }
01050
01051
01052
01053
01054
01055
01056
01057
01058
01059 void
01060 pop_front()
01061 {
01062 if (this->_M_impl._M_start._M_cur
01063 != this->_M_impl._M_start._M_last - 1)
01064 {
01065 this->_M_impl.destroy(this->_M_impl._M_start._M_cur);
01066 ++this->_M_impl._M_start._M_cur;
01067 }
01068 else
01069 _M_pop_front_aux();
01070 }
01071
01072
01073
01074
01075
01076
01077
01078
01079
01080 void
01081 pop_back()
01082 {
01083 if (this->_M_impl._M_finish._M_cur
01084 != this->_M_impl._M_finish._M_first)
01085 {
01086 --this->_M_impl._M_finish._M_cur;
01087 this->_M_impl.destroy(this->_M_impl._M_finish._M_cur);
01088 }
01089 else
01090 _M_pop_back_aux();
01091 }
01092
01093
01094
01095
01096
01097
01098
01099
01100
01101
01102 iterator
01103 insert(iterator position, const value_type& __x);
01104
01105
01106
01107
01108
01109
01110
01111
01112
01113
01114 void
01115 insert(iterator __position, size_type __n, const value_type& __x)
01116 { _M_fill_insert(__position, __n, __x); }
01117
01118
01119
01120
01121
01122
01123
01124
01125
01126
01127
01128 template<typename _InputIterator>
01129 void
01130 insert(iterator __position, _InputIterator __first,
01131 _InputIterator __last)
01132 {
01133
01134 typedef typename std::__is_integer<_InputIterator>::__type _Integral;
01135 _M_insert_dispatch(__position, __first, __last, _Integral());
01136 }
01137
01138
01139
01140
01141
01142
01143
01144
01145
01146
01147
01148
01149
01150
01151 iterator
01152 erase(iterator __position);
01153
01154
01155
01156
01157
01158
01159
01160
01161
01162
01163
01164
01165
01166
01167
01168
01169
01170 iterator
01171 erase(iterator __first, iterator __last);
01172
01173
01174
01175
01176
01177
01178
01179
01180
01181
01182 void
01183 swap(deque& __x)
01184 {
01185 std::swap(this->_M_impl._M_start, __x._M_impl._M_start);
01186 std::swap(this->_M_impl._M_finish, __x._M_impl._M_finish);
01187 std::swap(this->_M_impl._M_map, __x._M_impl._M_map);
01188 std::swap(this->_M_impl._M_map_size, __x._M_impl._M_map_size);
01189 }
01190
01191
01192
01193
01194
01195
01196
01197 void clear();
01198
01199 protected:
01200
01201
01202
01203 template<typename _Integer>
01204 void
01205 _M_initialize_dispatch(_Integer __n, _Integer __x, __true_type)
01206 {
01207 _M_initialize_map(__n);
01208 _M_fill_initialize(__x);
01209 }
01210
01211
01212 template<typename _InputIterator>
01213 void
01214 _M_initialize_dispatch(_InputIterator __first, _InputIterator __last,
01215 __false_type)
01216 {
01217 typedef typename iterator_traits<_InputIterator>::iterator_category
01218 _IterCategory;
01219 _M_range_initialize(__first, __last, _IterCategory());
01220 }
01221
01222
01223
01224
01225
01226
01227
01228
01229
01230
01231
01232
01233
01234
01235
01236 template<typename _InputIterator>
01237 void
01238 _M_range_initialize(_InputIterator __first, _InputIterator __last,
01239 input_iterator_tag);
01240
01241
01242 template<typename _ForwardIterator>
01243 void
01244 _M_range_initialize(_ForwardIterator __first, _ForwardIterator __last,
01245 forward_iterator_tag);
01246
01247
01248
01249
01250
01251
01252
01253
01254
01255
01256
01257
01258
01259
01260 void
01261 _M_fill_initialize(const value_type& __value);
01262
01263
01264
01265
01266
01267 template<typename _Integer>
01268 void
01269 _M_assign_dispatch(_Integer __n, _Integer __val, __true_type)
01270 {
01271 _M_fill_assign(static_cast<size_type>(__n),
01272 static_cast<value_type>(__val));
01273 }
01274
01275
01276 template<typename _InputIterator>
01277 void
01278 _M_assign_dispatch(_InputIterator __first, _InputIterator __last,
01279 __false_type)
01280 {
01281 typedef typename iterator_traits<_InputIterator>::iterator_category
01282 _IterCategory;
01283 _M_assign_aux(__first, __last, _IterCategory());
01284 }
01285
01286
01287 template<typename _InputIterator>
01288 void
01289 _M_assign_aux(_InputIterator __first, _InputIterator __last,
01290 input_iterator_tag);
01291
01292
01293 template<typename _ForwardIterator>
01294 void
01295 _M_assign_aux(_ForwardIterator __first, _ForwardIterator __last,
01296 forward_iterator_tag)
01297 {
01298 const size_type __len = std::distance(__first, __last);
01299 if (__len > size())
01300 {
01301 _ForwardIterator __mid = __first;
01302 std::advance(__mid, size());
01303 std::copy(__first, __mid, begin());
01304 insert(end(), __mid, __last);
01305 }
01306 else
01307 erase(std::copy(__first, __last, begin()), end());
01308 }
01309
01310
01311
01312 void
01313 _M_fill_assign(size_type __n, const value_type& __val)
01314 {
01315 if (__n > size())
01316 {
01317 std::fill(begin(), end(), __val);
01318 insert(end(), __n - size(), __val);
01319 }
01320 else
01321 {
01322 erase(begin() + __n, end());
01323 std::fill(begin(), end(), __val);
01324 }
01325 }
01326
01327
01328
01329
01330
01331
01332
01333 void _M_push_back_aux(const value_type&);
01334 void _M_push_front_aux(const value_type&);
01335 void _M_pop_back_aux();
01336 void _M_pop_front_aux();
01337
01338
01339
01340
01341
01342
01343 template<typename _Integer>
01344 void
01345 _M_insert_dispatch(iterator __pos,
01346 _Integer __n, _Integer __x, __true_type)
01347 {
01348 _M_fill_insert(__pos, static_cast<size_type>(__n),
01349 static_cast<value_type>(__x));
01350 }
01351
01352
01353 template<typename _InputIterator>
01354 void
01355 _M_insert_dispatch(iterator __pos,
01356 _InputIterator __first, _InputIterator __last,
01357 __false_type)
01358 {
01359 typedef typename iterator_traits<_InputIterator>::iterator_category
01360 _IterCategory;
01361 _M_range_insert_aux(__pos, __first, __last, _IterCategory());
01362 }
01363
01364
01365 template<typename _InputIterator>
01366 void
01367 _M_range_insert_aux(iterator __pos, _InputIterator __first,
01368 _InputIterator __last, input_iterator_tag);
01369
01370
01371 template<typename _ForwardIterator>
01372 void
01373 _M_range_insert_aux(iterator __pos, _ForwardIterator __first,
01374 _ForwardIterator __last, forward_iterator_tag);
01375
01376
01377
01378
01379 void
01380 _M_fill_insert(iterator __pos, size_type __n, const value_type& __x);
01381
01382
01383 iterator
01384 _M_insert_aux(iterator __pos, const value_type& __x);
01385
01386
01387 void
01388 _M_insert_aux(iterator __pos, size_type __n, const value_type& __x);
01389
01390
01391 template<typename _ForwardIterator>
01392 void
01393 _M_insert_aux(iterator __pos,
01394 _ForwardIterator __first, _ForwardIterator __last,
01395 size_type __n);
01396
01397
01398
01399
01400
01401
01402
01403
01404 iterator
01405 _M_reserve_elements_at_front(size_type __n)
01406 {
01407 const size_type __vacancies = this->_M_impl._M_start._M_cur
01408 - this->_M_impl._M_start._M_first;
01409 if (__n > __vacancies)
01410 _M_new_elements_at_front(__n - __vacancies);
01411 return this->_M_impl._M_start - difference_type(__n);
01412 }
01413
01414 iterator
01415 _M_reserve_elements_at_back(size_type __n)
01416 {
01417 const size_type __vacancies = (this->_M_impl._M_finish._M_last
01418 - this->_M_impl._M_finish._M_cur) - 1;
01419 if (__n > __vacancies)
01420 _M_new_elements_at_back(__n - __vacancies);
01421 return this->_M_impl._M_finish + difference_type(__n);
01422 }
01423
01424 void
01425 _M_new_elements_at_front(size_type __new_elements);
01426
01427 void
01428 _M_new_elements_at_back(size_type __new_elements);
01429
01430
01431
01432
01433
01434
01435
01436
01437
01438
01439
01440
01441
01442 void
01443 _M_reserve_map_at_back (size_type __nodes_to_add = 1)
01444 {
01445 if (__nodes_to_add + 1 > this->_M_impl._M_map_size
01446 - (this->_M_impl._M_finish._M_node - this->_M_impl._M_map))
01447 _M_reallocate_map(__nodes_to_add, false);
01448 }
01449
01450 void
01451 _M_reserve_map_at_front (size_type __nodes_to_add = 1)
01452 {
01453 if (__nodes_to_add > size_type(this->_M_impl._M_start._M_node
01454 - this->_M_impl._M_map))
01455 _M_reallocate_map(__nodes_to_add, true);
01456 }
01457
01458 void
01459 _M_reallocate_map(size_type __nodes_to_add, bool __add_at_front);
01460
01461 };
01462
01463
01464
01465
01466
01467
01468
01469
01470
01471
01472
01473
01474 template<typename _Tp, typename _Alloc>
01475 inline bool
01476 operator==(const deque<_Tp, _Alloc>& __x,
01477 const deque<_Tp, _Alloc>& __y)
01478 { return __x.size() == __y.size()
01479 && std::equal(__x.begin(), __x.end(), __y.begin()); }
01480
01481
01482
01483
01484
01485
01486
01487
01488
01489
01490
01491
01492 template<typename _Tp, typename _Alloc>
01493 inline bool
01494 operator<(const deque<_Tp, _Alloc>& __x,
01495 const deque<_Tp, _Alloc>& __y)
01496 { return lexicographical_compare(__x.begin(), __x.end(),
01497 __y.begin(), __y.end()); }
01498
01499
01500 template<typename _Tp, typename _Alloc>
01501 inline bool
01502 operator!=(const deque<_Tp, _Alloc>& __x,
01503 const deque<_Tp, _Alloc>& __y)
01504 { return !(__x == __y); }
01505
01506
01507 template<typename _Tp, typename _Alloc>
01508 inline bool
01509 operator>(const deque<_Tp, _Alloc>& __x,
01510 const deque<_Tp, _Alloc>& __y)
01511 { return __y < __x; }
01512
01513
01514 template<typename _Tp, typename _Alloc>
01515 inline bool
01516 operator<=(const deque<_Tp, _Alloc>& __x,
01517 const deque<_Tp, _Alloc>& __y)
01518 { return !(__y < __x); }
01519
01520
01521 template<typename _Tp, typename _Alloc>
01522 inline bool
01523 operator>=(const deque<_Tp, _Alloc>& __x,
01524 const deque<_Tp, _Alloc>& __y)
01525 { return !(__x < __y); }
01526
01527
01528 template<typename _Tp, typename _Alloc>
01529 inline void
01530 swap(deque<_Tp,_Alloc>& __x, deque<_Tp,_Alloc>& __y)
01531 { __x.swap(__y); }
01532 }
01533
01534 #endif