stl_vector.h

00001 /*
00002  *
00003  * Copyright (c) 1994
00004  * Hewlett-Packard Company
00005  *
00006  * Permission to use, copy, modify, distribute and sell this software
00007  * and its documentation for any purpose is hereby granted without fee,
00008  * provided that the above copyright notice appear in all copies and
00009  * that both that copyright notice and this permission notice appear
00010  * in supporting documentation.  Hewlett-Packard Company makes no
00011  * representations about the suitability of this software for any
00012  * purpose.  It is provided "as is" without express or implied warranty.
00013  *
00014  *
00015  * Copyright (c) 1996
00016  * Silicon Graphics Computer Systems, Inc.
00017  *
00018  * Permission to use, copy, modify, distribute and sell this software
00019  * and its documentation for any purpose is hereby granted without fee,
00020  * provided that the above copyright notice appear in all copies and
00021  * that both that copyright notice and this permission notice appear
00022  * in supporting documentation.  Silicon Graphics makes no
00023  * representations about the suitability of this software for any
00024  * purpose.  It is provided "as is" without express or implied warranty.
00025  */
00026 
00027 /* NOTE: This is an internal header file, included by other STL headers.
00028  *   You should not attempt to use it directly.
00029  */
00030 
00031 #ifndef __SGI_STL_INTERNAL_VECTOR_H
00032 #define __SGI_STL_INTERNAL_VECTOR_H
00033 
00034 #include <concept_checks.h>
00035 
00036 __STL_BEGIN_NAMESPACE 
00037 
00038 #if defined(__sgi) && !defined(__GNUC__) && (_MIPS_SIM != _MIPS_SIM_ABI32)
00039 #pragma set woff 1174
00040 #pragma set woff 1375
00041 #endif
00042 
00043 // The vector base class serves two purposes.  First, its constructor
00044 // and destructor allocate (but don't initialize) storage.  This makes
00045 // exception safety easier.  Second, the base class encapsulates all of
00046 // the differences between SGI-style allocators and standard-conforming
00047 // allocators.
00048 
00049 #ifdef __STL_USE_STD_ALLOCATORS
00050 
00051 // Base class for ordinary allocators.
00052 template <class _Tp, class _Allocator, bool _IsStatic>
00053 class _Vector_alloc_base {
00054 public:
00055   typedef typename _Alloc_traits<_Tp, _Allocator>::allocator_type
00056           allocator_type;
00057   allocator_type get_allocator() const { return _M_data_allocator; }
00058 
00059   _Vector_alloc_base(const allocator_type& __a)
00060     : _M_data_allocator(__a), _M_start(0), _M_finish(0), _M_end_of_storage(0) 
00061   {}
00062   
00063 protected:
00064   allocator_type _M_data_allocator;
00065   _Tp* _M_start;
00066   _Tp* _M_finish;
00067   _Tp* _M_end_of_storage;
00068 
00069   _Tp* _M_allocate(size_t __n)
00070     { return _M_data_allocator.allocate(__n); }
00071   void _M_deallocate(_Tp* __p, size_t __n)
00072     { if (__p) _M_data_allocator.deallocate(__p, __n); }
00073 };
00074 
00075 // Specialization for allocators that have the property that we don't
00076 // actually have to store an allocator object.  
00077 template <class _Tp, class _Allocator>
00078 class _Vector_alloc_base<_Tp, _Allocator, true> {
00079 public:
00080   typedef typename _Alloc_traits<_Tp, _Allocator>::allocator_type
00081           allocator_type;
00082   allocator_type get_allocator() const { return allocator_type(); }
00083 
00084   _Vector_alloc_base(const allocator_type&)
00085     : _M_start(0), _M_finish(0), _M_end_of_storage(0) 
00086   {}
00087   
00088 protected:
00089   _Tp* _M_start;
00090   _Tp* _M_finish;
00091   _Tp* _M_end_of_storage;
00092 
00093   typedef typename _Alloc_traits<_Tp, _Allocator>::_Alloc_type _Alloc_type;
00094   _Tp* _M_allocate(size_t __n)
00095     { return _Alloc_type::allocate(__n); }
00096   void _M_deallocate(_Tp* __p, size_t __n)
00097     { _Alloc_type::deallocate(__p, __n);}
00098 };
00099 
00100 template <class _Tp, class _Alloc>
00101 struct _Vector_base
00102   : public _Vector_alloc_base<_Tp, _Alloc,
00103                               _Alloc_traits<_Tp, _Alloc>::_S_instanceless>
00104 {
00105   typedef _Vector_alloc_base<_Tp, _Alloc, 
00106                              _Alloc_traits<_Tp, _Alloc>::_S_instanceless>
00107           _Base;
00108   typedef typename _Base::allocator_type allocator_type;
00109 
00110   _Vector_base(const allocator_type& __a) : _Base(__a) {}
00111   _Vector_base(size_t __n, const allocator_type& __a) : _Base(__a) {
00112     _M_start = _M_allocate(__n);
00113     _M_finish = _M_start;
00114     _M_end_of_storage = _M_start + __n;
00115   }
00116 
00117   ~_Vector_base() { _M_deallocate(_M_start, _M_end_of_storage - _M_start); }
00118 };    
00119 
00120 #else /* __STL_USE_STD_ALLOCATORS */
00121 
00122 template <class _Tp, class _Alloc> 
00123 class _Vector_base {
00124 public:
00125   typedef _Alloc allocator_type;
00126   allocator_type get_allocator() const { return allocator_type(); }
00127 
00128   _Vector_base(const _Alloc&)
00129     : _M_start(0), _M_finish(0), _M_end_of_storage(0) {}
00130   _Vector_base(size_t __n, const _Alloc&)
00131     : _M_start(0), _M_finish(0), _M_end_of_storage(0) 
00132   {
00133     _M_start = _M_allocate(__n);
00134     _M_finish = _M_start;
00135     _M_end_of_storage = _M_start + __n;
00136   }
00137 
00138   ~_Vector_base() { _M_deallocate(_M_start, _M_end_of_storage - _M_start); }
00139 
00140 protected:
00141   _Tp* _M_start;
00142   _Tp* _M_finish;
00143   _Tp* _M_end_of_storage;
00144 
00145   typedef simple_alloc<_Tp, _Alloc> _M_data_allocator;
00146   _Tp* _M_allocate(size_t __n)
00147     { return _M_data_allocator::allocate(__n); }
00148   void _M_deallocate(_Tp* __p, size_t __n) 
00149     { _M_data_allocator::deallocate(__p, __n); }
00150 };
00151 
00152 #endif /* __STL_USE_STD_ALLOCATORS */
00153 
00154 template <class _Tp, class _Alloc = __STL_DEFAULT_ALLOCATOR(_Tp) >
00155 class vector : protected _Vector_base<_Tp, _Alloc> 
00156 {
00157   // requirements:
00158 
00159   __STL_CLASS_REQUIRES(_Tp, _Assignable);
00160 
00161 private:
00162   typedef _Vector_base<_Tp, _Alloc> _Base;
00163 public:
00164   typedef _Tp value_type;
00165   typedef value_type* pointer;
00166   typedef const value_type* const_pointer;
00167   typedef value_type* iterator;
00168   typedef const value_type* const_iterator;
00169   typedef value_type& reference;
00170   typedef const value_type& const_reference;
00171   typedef size_t size_type;
00172   typedef ptrdiff_t difference_type;
00173 
00174   typedef typename _Base::allocator_type allocator_type;
00175   allocator_type get_allocator() const { return _Base::get_allocator(); }
00176 
00177 #ifdef __STL_CLASS_PARTIAL_SPECIALIZATION
00178   typedef reverse_iterator<const_iterator> const_reverse_iterator;
00179   typedef reverse_iterator<iterator> reverse_iterator;
00180 #else /* __STL_CLASS_PARTIAL_SPECIALIZATION */
00181   typedef reverse_iterator<const_iterator, value_type, const_reference, 
00182                            difference_type>  const_reverse_iterator;
00183   typedef reverse_iterator<iterator, value_type, reference, difference_type>
00184           reverse_iterator;
00185 #endif /* __STL_CLASS_PARTIAL_SPECIALIZATION */
00186 
00187 protected:
00188 #ifdef __STL_HAS_NAMESPACES
00189   using _Base::_M_allocate;
00190   using _Base::_M_deallocate;
00191   using _Base::_M_start;
00192   using _Base::_M_finish;
00193   using _Base::_M_end_of_storage;
00194 #endif /* __STL_HAS_NAMESPACES */
00195 
00196 protected:
00197   void _M_insert_aux(iterator __position, const _Tp& __x);
00198   void _M_insert_aux(iterator __position);
00199 
00200 public:
00201   iterator begin() { return _M_start; }
00202   const_iterator begin() const { return _M_start; }
00203   iterator end() { return _M_finish; }
00204   const_iterator end() const { return _M_finish; }
00205 
00206   reverse_iterator rbegin()
00207     { return reverse_iterator(end()); }
00208   const_reverse_iterator rbegin() const
00209     { return const_reverse_iterator(end()); }
00210   reverse_iterator rend()
00211     { return reverse_iterator(begin()); }
00212   const_reverse_iterator rend() const
00213     { return const_reverse_iterator(begin()); }
00214 
00215   size_type size() const
00216     { return size_type(end() - begin()); }
00217   size_type max_size() const
00218     { return size_type(-1) / sizeof(_Tp); }
00219   size_type capacity() const
00220     { return size_type(_M_end_of_storage - begin()); }
00221   bool empty() const
00222     { return begin() == end(); }
00223 
00224   reference operator[](size_type __n) { return *(begin() + __n); }
00225   const_reference operator[](size_type __n) const { return *(begin() + __n); }
00226 
00227 #ifdef __STL_THROW_RANGE_ERRORS
00228   void _M_range_check(size_type __n) const {
00229     if (__n >= this->size())
00230       __stl_throw_range_error("vector");
00231   }
00232 
00233   reference at(size_type __n)
00234     { _M_range_check(__n); return (*this)[__n]; }
00235   const_reference at(size_type __n) const
00236     { _M_range_check(__n); return (*this)[__n]; }
00237 #endif /* __STL_THROW_RANGE_ERRORS */
00238 
00239   explicit vector(const allocator_type& __a = allocator_type())
00240     : _Base(__a) {}
00241 
00242   vector(size_type __n, const _Tp& __value,
00243          const allocator_type& __a = allocator_type()) 
00244     : _Base(__n, __a)
00245     { _M_finish = uninitialized_fill_n(_M_start, __n, __value); }
00246 
00247   explicit vector(size_type __n)
00248     : _Base(__n, allocator_type())
00249     { _M_finish = uninitialized_fill_n(_M_start, __n, _Tp()); }
00250 
00251   vector(const vector<_Tp, _Alloc>& __x) 
00252     : _Base(__x.size(), __x.get_allocator())
00253     { _M_finish = uninitialized_copy(__x.begin(), __x.end(), _M_start); }
00254 
00255 #ifdef __STL_MEMBER_TEMPLATES
00256   // Check whether it's an integral type.  If so, it's not an iterator.
00257   template <class _InputIterator>
00258   vector(_InputIterator __first, _InputIterator __last,
00259          const allocator_type& __a = allocator_type()) : _Base(__a) {
00260     typedef typename _Is_integer<_InputIterator>::_Integral _Integral;
00261     _M_initialize_aux(__first, __last, _Integral());
00262   }
00263 
00264   template <class _Integer>
00265   void _M_initialize_aux(_Integer __n, _Integer __value, __true_type) {
00266     _M_start = _M_allocate(__n);
00267     _M_end_of_storage = _M_start + __n; 
00268     _M_finish = uninitialized_fill_n(_M_start, __n, __value);
00269   }
00270 
00271   template <class _InputIterator>
00272   void _M_initialize_aux(_InputIterator __first, _InputIterator __last,
00273                          __false_type) {
00274     _M_range_initialize(__first, __last, __ITERATOR_CATEGORY(__first));
00275   }
00276 
00277 #else
00278   vector(const _Tp* __first, const _Tp* __last,
00279          const allocator_type& __a = allocator_type())
00280     : _Base(__last - __first, __a) 
00281     { _M_finish = uninitialized_copy(__first, __last, _M_start); }
00282 #endif /* __STL_MEMBER_TEMPLATES */
00283 
00284   ~vector() { destroy(_M_start, _M_finish); }
00285 
00286   vector<_Tp, _Alloc>& operator=(const vector<_Tp, _Alloc>& __x);
00287   void reserve(size_type __n) {
00288     if (capacity() < __n) {
00289       const size_type __old_size = size();
00290       iterator __tmp = _M_allocate_and_copy(__n, _M_start, _M_finish);
00291       destroy(_M_start, _M_finish);
00292       _M_deallocate(_M_start, _M_end_of_storage - _M_start);
00293       _M_start = __tmp;
00294       _M_finish = __tmp + __old_size;
00295       _M_end_of_storage = _M_start + __n;
00296     }
00297   }
00298 
00299   // assign(), a generalized assignment member function.  Two
00300   // versions: one that takes a count, and one that takes a range.
00301   // The range version is a member template, so we dispatch on whether
00302   // or not the type is an integer.
00303 
00304   void assign(size_type __n, const _Tp& __val) { _M_fill_assign(__n, __val); }
00305   void _M_fill_assign(size_type __n, const _Tp& __val);
00306 
00307 #ifdef __STL_MEMBER_TEMPLATES
00308   
00309   template <class _InputIterator>
00310   void assign(_InputIterator __first, _InputIterator __last) {
00311     typedef typename _Is_integer<_InputIterator>::_Integral _Integral;
00312     _M_assign_dispatch(__first, __last, _Integral());
00313   }
00314 
00315   template <class _Integer>
00316   void _M_assign_dispatch(_Integer __n, _Integer __val, __true_type)
00317     { _M_fill_assign((size_type) __n, (_Tp) __val); }
00318 
00319   template <class _InputIter>
00320   void _M_assign_dispatch(_InputIter __first, _InputIter __last, __false_type)
00321     { _M_assign_aux(__first, __last, __ITERATOR_CATEGORY(__first)); }
00322 
00323   template <class _InputIterator>
00324   void _M_assign_aux(_InputIterator __first, _InputIterator __last,
00325                      input_iterator_tag);
00326 
00327   template <class _ForwardIterator>
00328   void _M_assign_aux(_ForwardIterator __first, _ForwardIterator __last,
00329                      forward_iterator_tag); 
00330 
00331 #endif /* __STL_MEMBER_TEMPLATES */
00332 
00333   reference front() { return *begin(); }
00334   const_reference front() const { return *begin(); }
00335   reference back() { return *(end() - 1); }
00336   const_reference back() const { return *(end() - 1); }
00337 
00338   void push_back(const _Tp& __x) {
00339     if (_M_finish != _M_end_of_storage) {
00340       construct(_M_finish, __x);
00341       ++_M_finish;
00342     }
00343     else
00344       _M_insert_aux(end(), __x);
00345   }
00346   void push_back() {
00347     if (_M_finish != _M_end_of_storage) {
00348       construct(_M_finish);
00349       ++_M_finish;
00350     }
00351     else
00352       _M_insert_aux(end());
00353   }
00354   void swap(vector<_Tp, _Alloc>& __x) {
00355     __STD::swap(_M_start, __x._M_start);
00356     __STD::swap(_M_finish, __x._M_finish);
00357     __STD::swap(_M_end_of_storage, __x._M_end_of_storage);
00358   }
00359 
00360   iterator insert(iterator __position, const _Tp& __x) {
00361     size_type __n = __position - begin();
00362     if (_M_finish != _M_end_of_storage && __position == end()) {
00363       construct(_M_finish, __x);
00364       ++_M_finish;
00365     }
00366     else
00367       _M_insert_aux(__position, __x);
00368     return begin() + __n;
00369   }
00370   iterator insert(iterator __position) {
00371     size_type __n = __position - begin();
00372     if (_M_finish != _M_end_of_storage && __position == end()) {
00373       construct(_M_finish);
00374       ++_M_finish;
00375     }
00376     else
00377       _M_insert_aux(__position);
00378     return begin() + __n;
00379   }
00380 #ifdef __STL_MEMBER_TEMPLATES
00381   // Check whether it's an integral type.  If so, it's not an iterator.
00382   template <class _InputIterator>
00383   void insert(iterator __pos, _InputIterator __first, _InputIterator __last) {
00384     typedef typename _Is_integer<_InputIterator>::_Integral _Integral;
00385     _M_insert_dispatch(__pos, __first, __last, _Integral());
00386   }
00387 
00388   template <class _Integer>
00389   void _M_insert_dispatch(iterator __pos, _Integer __n, _Integer __val,
00390                           __true_type)
00391     { _M_fill_insert(__pos, (size_type) __n, (_Tp) __val); }
00392 
00393   template <class _InputIterator>
00394   void _M_insert_dispatch(iterator __pos,
00395                           _InputIterator __first, _InputIterator __last,
00396                           __false_type) {
00397     _M_range_insert(__pos, __first, __last, __ITERATOR_CATEGORY(__first));
00398   }
00399 #else /* __STL_MEMBER_TEMPLATES */
00400   void insert(iterator __position,
00401               const_iterator __first, const_iterator __last);
00402 #endif /* __STL_MEMBER_TEMPLATES */
00403 
00404   void insert (iterator __pos, size_type __n, const _Tp& __x)
00405     { _M_fill_insert(__pos, __n, __x); }
00406 
00407   void _M_fill_insert (iterator __pos, size_type __n, const _Tp& __x);
00408 
00409   void pop_back() {
00410     --_M_finish;
00411     destroy(_M_finish);
00412   }
00413   iterator erase(iterator __position) {
00414     if (__position + 1 != end())
00415       copy(__position + 1, _M_finish, __position);
00416     --_M_finish;
00417     destroy(_M_finish);
00418     return __position;
00419   }
00420   iterator erase(iterator __first, iterator __last) {
00421     iterator __i = copy(__last, _M_finish, __first);
00422     destroy(__i, _M_finish);
00423     _M_finish = _M_finish - (__last - __first);
00424     return __first;
00425   }
00426 
00427   void resize(size_type __new_size, const _Tp& __x) {
00428     if (__new_size < size()) 
00429       erase(begin() + __new_size, end());
00430     else
00431       insert(end(), __new_size - size(), __x);
00432   }
00433   void resize(size_type __new_size) { resize(__new_size, _Tp()); }
00434   void clear() { erase(begin(), end()); }
00435 
00436 protected:
00437 
00438 #ifdef __STL_MEMBER_TEMPLATES
00439   template <class _ForwardIterator>
00440   iterator _M_allocate_and_copy(size_type __n, _ForwardIterator __first, 
00441                                                _ForwardIterator __last)
00442 {
00443     iterator __result = _M_allocate(__n);
00444     __STL_TRY {
00445       uninitialized_copy(__first, __last, __result);
00446       return __result;
00447     }
00448     __STL_UNWIND(_M_deallocate(__result, __n));
00449   }
00450 #else /* __STL_MEMBER_TEMPLATES */
00451   iterator _M_allocate_and_copy(size_type __n, const_iterator __first, 
00452                                                const_iterator __last)
00453   {
00454     iterator __result = _M_allocate(__n);
00455     __STL_TRY {
00456       uninitialized_copy(__first, __last, __result);
00457       return __result;
00458     }
00459     __STL_UNWIND(_M_deallocate(__result, __n));
00460   }
00461 #endif /* __STL_MEMBER_TEMPLATES */
00462 
00463 
00464 #ifdef __STL_MEMBER_TEMPLATES
00465   template <class _InputIterator>
00466   void _M_range_initialize(_InputIterator __first,  
00467                            _InputIterator __last, input_iterator_tag)
00468   {
00469     for ( ; __first != __last; ++__first)
00470       push_back(*__first);
00471   }
00472 
00473   // This function is only called by the constructor. 
00474   template <class _ForwardIterator>
00475   void _M_range_initialize(_ForwardIterator __first,
00476                            _ForwardIterator __last, forward_iterator_tag)
00477   {
00478     size_type __n = 0;
00479     distance(__first, __last, __n);
00480     _M_start = _M_allocate(__n);
00481     _M_end_of_storage = _M_start + __n;
00482     _M_finish = uninitialized_copy(__first, __last, _M_start);
00483   }
00484 
00485   template <class _InputIterator>
00486   void _M_range_insert(iterator __pos,
00487                        _InputIterator __first, _InputIterator __last,
00488                        input_iterator_tag);
00489 
00490   template <class _ForwardIterator>
00491   void _M_range_insert(iterator __pos,
00492                        _ForwardIterator __first, _ForwardIterator __last,
00493                        forward_iterator_tag);
00494 
00495 #endif /* __STL_MEMBER_TEMPLATES */
00496 };
00497 
00498 template <class _Tp, class _Alloc>
00499 inline bool 
00500 operator==(const vector<_Tp, _Alloc>& __x, const vector<_Tp, _Alloc>& __y)
00501 {
00502   return __x.size() == __y.size() &&
00503          equal(__x.begin(), __x.end(), __y.begin());
00504 }
00505 
00506 template <class _Tp, class _Alloc>
00507 inline bool 
00508 operator<(const vector<_Tp, _Alloc>& __x, const vector<_Tp, _Alloc>& __y)
00509 {
00510   return lexicographical_compare(__x.begin(), __x.end(), 
00511                                  __y.begin(), __y.end());
00512 }
00513 
00514 #ifdef __STL_FUNCTION_TMPL_PARTIAL_ORDER
00515 
00516 template <class _Tp, class _Alloc>
00517 inline void swap(vector<_Tp, _Alloc>& __x, vector<_Tp, _Alloc>& __y)
00518 {
00519   __x.swap(__y);
00520 }
00521 
00522 template <class _Tp, class _Alloc>
00523 inline bool
00524 operator!=(const vector<_Tp, _Alloc>& __x, const vector<_Tp, _Alloc>& __y) {
00525   return !(__x == __y);
00526 }
00527 
00528 template <class _Tp, class _Alloc>
00529 inline bool
00530 operator>(const vector<_Tp, _Alloc>& __x, const vector<_Tp, _Alloc>& __y) {
00531   return __y < __x;
00532 }
00533 
00534 template <class _Tp, class _Alloc>
00535 inline bool
00536 operator<=(const vector<_Tp, _Alloc>& __x, const vector<_Tp, _Alloc>& __y) {
00537   return !(__y < __x);
00538 }
00539 
00540 template <class _Tp, class _Alloc>
00541 inline bool
00542 operator>=(const vector<_Tp, _Alloc>& __x, const vector<_Tp, _Alloc>& __y) {
00543   return !(__x < __y);
00544 }
00545 
00546 #endif /* __STL_FUNCTION_TMPL_PARTIAL_ORDER */
00547 
00548 template <class _Tp, class _Alloc>
00549 vector<_Tp,_Alloc>& 
00550 vector<_Tp,_Alloc>::operator=(const vector<_Tp, _Alloc>& __x)
00551 {
00552   if (&__x != this) {
00553     const size_type __xlen = __x.size();
00554     if (__xlen > capacity()) {
00555       iterator __tmp = _M_allocate_and_copy(__xlen, __x.begin(), __x.end());
00556       destroy(_M_start, _M_finish);
00557       _M_deallocate(_M_start, _M_end_of_storage - _M_start);
00558       _M_start = __tmp;
00559       _M_end_of_storage = _M_start + __xlen;
00560     }
00561     else if (size() >= __xlen) {
00562       iterator __i = copy(__x.begin(), __x.end(), begin());
00563       destroy(__i, _M_finish);
00564     }
00565     else {
00566       copy(__x.begin(), __x.begin() + size(), _M_start);
00567       uninitialized_copy(__x.begin() + size(), __x.end(), _M_finish);
00568     }
00569     _M_finish = _M_start + __xlen;
00570   }
00571   return *this;
00572 }
00573 
00574 template <class _Tp, class _Alloc>
00575 void vector<_Tp, _Alloc>::_M_fill_assign(size_t __n, const value_type& __val) 
00576 {
00577   if (__n > capacity()) {
00578     vector<_Tp, _Alloc> __tmp(__n, __val, get_allocator());
00579     __tmp.swap(*this);
00580   }
00581   else if (__n > size()) {
00582     fill(begin(), end(), __val);
00583     _M_finish = uninitialized_fill_n(_M_finish, __n - size(), __val);
00584   }
00585   else
00586     erase(fill_n(begin(), __n, __val), end());
00587 }
00588 
00589 #ifdef __STL_MEMBER_TEMPLATES
00590 
00591 template <class _Tp, class _Alloc> template <class _InputIter>
00592 void vector<_Tp, _Alloc>::_M_assign_aux(_InputIter __first, _InputIter __last,
00593                                         input_iterator_tag) {
00594   iterator __cur = begin();
00595   for ( ; __first != __last && __cur != end(); ++__cur, ++__first)
00596     *__cur = *__first;
00597   if (__first == __last)
00598     erase(__cur, end());
00599   else
00600     insert(end(), __first, __last);
00601 }
00602 
00603 template <class _Tp, class _Alloc> template <class _ForwardIter>
00604 void
00605 vector<_Tp, _Alloc>::_M_assign_aux(_ForwardIter __first, _ForwardIter __last,
00606                                    forward_iterator_tag) {
00607   size_type __len = 0;
00608   distance(__first, __last, __len);
00609 
00610   if (__len > capacity()) {
00611     iterator __tmp = _M_allocate_and_copy(__len, __first, __last);
00612     destroy(_M_start, _M_finish);
00613     _M_deallocate(_M_start, _M_end_of_storage - _M_start);
00614     _M_start = __tmp;
00615     _M_end_of_storage = _M_finish = _M_start + __len;
00616   }
00617   else if (size() >= __len) {
00618     iterator __new_finish = copy(__first, __last, _M_start);
00619     destroy(__new_finish, _M_finish);
00620     _M_finish = __new_finish;
00621   }
00622   else {
00623     _ForwardIter __mid = __first;
00624     advance(__mid, size());
00625     copy(__first, __mid, _M_start);
00626     _M_finish = uninitialized_copy(__mid, __last, _M_finish);
00627   }
00628 }
00629 
00630 #endif /* __STL_MEMBER_TEMPLATES */
00631 
00632 template <class _Tp, class _Alloc>
00633 void 
00634 vector<_Tp, _Alloc>::_M_insert_aux(iterator __position, const _Tp& __x)
00635 {
00636   if (_M_finish != _M_end_of_storage) {
00637     construct(_M_finish, *(_M_finish - 1));
00638     ++_M_finish;
00639     _Tp __x_copy = __x;
00640     copy_backward(__position, _M_finish - 2, _M_finish - 1);
00641     *__position = __x_copy;
00642   }
00643   else {
00644     const size_type __old_size = size();
00645     const size_type __len = __old_size != 0 ? 2 * __old_size : 1;
00646     iterator __new_start = _M_allocate(__len);
00647     iterator __new_finish = __new_start;
00648     __STL_TRY {
00649       __new_finish = uninitialized_copy(_M_start, __position, __new_start);
00650       construct(__new_finish, __x);
00651       ++__new_finish;
00652       __new_finish = uninitialized_copy(__position, _M_finish, __new_finish);
00653     }
00654     __STL_UNWIND((destroy(__new_start,__new_finish), 
00655                   _M_deallocate(__new_start,__len)));
00656     destroy(begin(), end());
00657     _M_deallocate(_M_start, _M_end_of_storage - _M_start);
00658     _M_start = __new_start;
00659     _M_finish = __new_finish;
00660     _M_end_of_storage = __new_start + __len;
00661   }
00662 }
00663 
00664 template <class _Tp, class _Alloc>
00665 void 
00666 vector<_Tp, _Alloc>::_M_insert_aux(iterator __position)
00667 {
00668   if (_M_finish != _M_end_of_storage) {
00669     construct(_M_finish, *(_M_finish - 1));
00670     ++_M_finish;
00671     copy_backward(__position, _M_finish - 2, _M_finish - 1);
00672     *__position = _Tp();
00673   }
00674   else {
00675     const size_type __old_size = size();
00676     const size_type __len = __old_size != 0 ? 2 * __old_size : 1;
00677     iterator __new_start = _M_allocate(__len);
00678     iterator __new_finish = __new_start;
00679     __STL_TRY {
00680       __new_finish = uninitialized_copy(_M_start, __position, __new_start);
00681       construct(__new_finish);
00682       ++__new_finish;
00683       __new_finish = uninitialized_copy(__position, _M_finish, __new_finish);
00684     }
00685     __STL_UNWIND((destroy(__new_start,__new_finish), 
00686                   _M_deallocate(__new_start,__len)));
00687     destroy(begin(), end());
00688     _M_deallocate(_M_start, _M_end_of_storage - _M_start);
00689     _M_start = __new_start;
00690     _M_finish = __new_finish;
00691     _M_end_of_storage = __new_start + __len;
00692   }
00693 }
00694 
00695 template <class _Tp, class _Alloc>
00696 void vector<_Tp, _Alloc>::_M_fill_insert(iterator __position, size_type __n, 
00697                                          const _Tp& __x)
00698 {
00699   if (__n != 0) {
00700     if (size_type(_M_end_of_storage - _M_finish) >= __n) {
00701       _Tp __x_copy = __x;
00702       const size_type __elems_after = _M_finish - __position;
00703       iterator __old_finish = _M_finish;
00704       if (__elems_after > __n) {
00705         uninitialized_copy(_M_finish - __n, _M_finish, _M_finish);
00706         _M_finish += __n;
00707         copy_backward(__position, __old_finish - __n, __old_finish);
00708         fill(__position, __position + __n, __x_copy);
00709       }
00710       else {
00711         uninitialized_fill_n(_M_finish, __n - __elems_after, __x_copy);
00712         _M_finish += __n - __elems_after;
00713         uninitialized_copy(__position, __old_finish, _M_finish);
00714         _M_finish += __elems_after;
00715         fill(__position, __old_finish, __x_copy);
00716       }
00717     }
00718     else {
00719       const size_type __old_size = size();        
00720       const size_type __len = __old_size + max(__old_size, __n);
00721       iterator __new_start = _M_allocate(__len);
00722       iterator __new_finish = __new_start;
00723       __STL_TRY {
00724         __new_finish = uninitialized_copy(_M_start, __position, __new_start);
00725         __new_finish = uninitialized_fill_n(__new_finish, __n, __x);
00726         __new_finish
00727           = uninitialized_copy(__position, _M_finish, __new_finish);
00728       }
00729       __STL_UNWIND((destroy(__new_start,__new_finish), 
00730                     _M_deallocate(__new_start,__len)));
00731       destroy(_M_start, _M_finish);
00732       _M_deallocate(_M_start, _M_end_of_storage - _M_start);
00733       _M_start = __new_start;
00734       _M_finish = __new_finish;
00735       _M_end_of_storage = __new_start + __len;
00736     }
00737   }
00738 }
00739 
00740 #ifdef __STL_MEMBER_TEMPLATES
00741 
00742 template <class _Tp, class _Alloc> template <class _InputIterator>
00743 void 
00744 vector<_Tp, _Alloc>::_M_range_insert(iterator __pos, 
00745                                      _InputIterator __first, 
00746                                      _InputIterator __last,
00747                                      input_iterator_tag)
00748 {
00749   for ( ; __first != __last; ++__first) {
00750     __pos = insert(__pos, *__first);
00751     ++__pos;
00752   }
00753 }
00754 
00755 template <class _Tp, class _Alloc> template <class _ForwardIterator>
00756 void 
00757 vector<_Tp, _Alloc>::_M_range_insert(iterator __position,
00758                                      _ForwardIterator __first,
00759                                      _ForwardIterator __last,
00760                                      forward_iterator_tag)
00761 {
00762   if (__first != __last) {
00763     size_type __n = 0;
00764     distance(__first, __last, __n);
00765     if (size_type(_M_end_of_storage - _M_finish) >= __n) {
00766       const size_type __elems_after = _M_finish - __position;
00767       iterator __old_finish = _M_finish;
00768       if (__elems_after > __n) {
00769         uninitialized_copy(_M_finish - __n, _M_finish, _M_finish);
00770         _M_finish += __n;
00771         copy_backward(__position, __old_finish - __n, __old_finish);
00772         copy(__first, __last, __position);
00773       }
00774       else {
00775         _ForwardIterator __mid = __first;
00776         advance(__mid, __elems_after);
00777         uninitialized_copy(__mid, __last, _M_finish);
00778         _M_finish += __n - __elems_after;
00779         uninitialized_copy(__position, __old_finish, _M_finish);
00780         _M_finish += __elems_after;
00781         copy(__first, __mid, __position);
00782       }
00783     }
00784     else {
00785       const size_type __old_size = size();
00786       const size_type __len = __old_size + max(__old_size, __n);
00787       iterator __new_start = _M_allocate(__len);
00788       iterator __new_finish = __new_start;
00789       __STL_TRY {
00790         __new_finish = uninitialized_copy(_M_start, __position, __new_start);
00791         __new_finish = uninitialized_copy(__first, __last, __new_finish);
00792         __new_finish
00793           = uninitialized_copy(__position, _M_finish, __new_finish);
00794       }
00795       __STL_UNWIND((destroy(__new_start,__new_finish), 
00796                     _M_deallocate(__new_start,__len)));
00797       destroy(_M_start, _M_finish);
00798       _M_deallocate(_M_start, _M_end_of_storage - _M_start);
00799       _M_start = __new_start;
00800       _M_finish = __new_finish;
00801       _M_end_of_storage = __new_start + __len;
00802     }
00803   }
00804 }
00805 
00806 #else /* __STL_MEMBER_TEMPLATES */
00807 
00808 template <class _Tp, class _Alloc>
00809 void 
00810 vector<_Tp, _Alloc>::insert(iterator __position, 
00811                             const_iterator __first, 
00812                             const_iterator __last)
00813 {
00814   if (__first != __last) {
00815     size_type __n = 0;
00816     distance(__first, __last, __n);
00817     if (size_type(_M_end_of_storage - _M_finish) >= __n) {
00818       const size_type __elems_after = _M_finish - __position;
00819       iterator __old_finish = _M_finish;
00820       if (__elems_after > __n) {
00821         uninitialized_copy(_M_finish - __n, _M_finish, _M_finish);
00822         _M_finish += __n;
00823         copy_backward(__position, __old_finish - __n, __old_finish);
00824         copy(__first, __last, __position);
00825       }
00826       else {
00827         uninitialized_copy(__first + __elems_after, __last, _M_finish);
00828         _M_finish += __n - __elems_after;
00829         uninitialized_copy(__position, __old_finish, _M_finish);
00830         _M_finish += __elems_after;
00831         copy(__first, __first + __elems_after, __position);
00832       }
00833     }
00834     else {
00835       const size_type __old_size = size();
00836       const size_type __len = __old_size + max(__old_size, __n);
00837       iterator __new_start = _M_allocate(__len);
00838       iterator __new_finish = __new_start;
00839       __STL_TRY {
00840         __new_finish = uninitialized_copy(_M_start, __position, __new_start);
00841         __new_finish = uninitialized_copy(__first, __last, __new_finish);
00842         __new_finish
00843           = uninitialized_copy(__position, _M_finish, __new_finish);
00844       }
00845       __STL_UNWIND((destroy(__new_start,__new_finish),
00846                     _M_deallocate(__new_start,__len)));
00847       destroy(_M_start, _M_finish);
00848       _M_deallocate(_M_start, _M_end_of_storage - _M_start);
00849       _M_start = __new_start;
00850       _M_finish = __new_finish;
00851       _M_end_of_storage = __new_start + __len;
00852     }
00853   }
00854 }
00855 
00856 #endif /* __STL_MEMBER_TEMPLATES */
00857 
00858 #if defined(__sgi) && !defined(__GNUC__) && (_MIPS_SIM != _MIPS_SIM_ABI32)
00859 #pragma reset woff 1174
00860 #pragma reset woff 1375
00861 #endif
00862 
00863 __STL_END_NAMESPACE 
00864 
00865 #endif /* __SGI_STL_INTERNAL_VECTOR_H */
00866 
00867 // Local Variables:
00868 // mode:C++
00869 // End:

Generated on Mon Jun 5 10:20:45 2006 for Intelligence.kdevelop by  doxygen 1.4.6