stl_vector.h

Go to the documentation of this file.
00001 // Vector implementation -*- C++ -*- 00002 00003 // Copyright (C) 2001, 2002, 2003, 2004 Free Software Foundation, Inc. 00004 // 00005 // This file is part of the GNU ISO C++ Library. This library is free 00006 // software; you can redistribute it and/or modify it under the 00007 // terms of the GNU General Public License as published by the 00008 // Free Software Foundation; either version 2, or (at your option) 00009 // any later version. 00010 00011 // This library is distributed in the hope that it will be useful, 00012 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00013 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00014 // GNU General Public License for more details. 00015 00016 // You should have received a copy of the GNU General Public License along 00017 // with this library; see the file COPYING. If not, write to the Free 00018 // Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, 00019 // USA. 00020 00021 // As a special exception, you may use this file as part of a free software 00022 // library without restriction. Specifically, if other files instantiate 00023 // templates or use macros or inline functions from this file, or you compile 00024 // this file and link it with other files to produce an executable, this 00025 // file does not by itself cause the resulting executable to be covered by 00026 // the GNU General Public License. This exception does not however 00027 // invalidate any other reasons why the executable file might be covered by 00028 // the GNU General Public License. 00029 00030 /* 00031 * 00032 * Copyright (c) 1994 00033 * Hewlett-Packard Company 00034 * 00035 * Permission to use, copy, modify, distribute and sell this software 00036 * and its documentation for any purpose is hereby granted without fee, 00037 * provided that the above copyright notice appear in all copies and 00038 * that both that copyright notice and this permission notice appear 00039 * in supporting documentation. Hewlett-Packard Company makes no 00040 * representations about the suitability of this software for any 00041 * purpose. It is provided "as is" without express or implied warranty. 00042 * 00043 * 00044 * Copyright (c) 1996 00045 * Silicon Graphics Computer Systems, Inc. 00046 * 00047 * Permission to use, copy, modify, distribute and sell this software 00048 * and its documentation for any purpose is hereby granted without fee, 00049 * provided that the above copyright notice appear in all copies and 00050 * that both that copyright notice and this permission notice appear 00051 * in supporting documentation. Silicon Graphics makes no 00052 * representations about the suitability of this software for any 00053 * purpose. It is provided "as is" without express or implied warranty. 00054 */ 00055 00056 /** @file stl_vector.h 00057 * This is an internal header file, included by other library headers. 00058 * You should not attempt to use it directly. 00059 */ 00060 00061 #ifndef _VECTOR_H 00062 #define _VECTOR_H 1 00063 00064 #include <bits/stl_iterator_base_funcs.h> 00065 #include <bits/functexcept.h> 00066 #include <bits/concept_check.h> 00067 00068 namespace _GLIBCXX_STD 00069 { 00070 /** 00071 * @if maint 00072 * See bits/stl_deque.h's _Deque_base for an explanation. 00073 * @endif 00074 */ 00075 template<typename _Tp, typename _Alloc> 00076 struct _Vector_base 00077 { 00078 struct _Vector_impl 00079 : public _Alloc 00080 { 00081 _Tp* _M_start; 00082 _Tp* _M_finish; 00083 _Tp* _M_end_of_storage; 00084 _Vector_impl(_Alloc const& __a) 00085 : _Alloc(__a), _M_start(0), _M_finish(0), _M_end_of_storage(0) 00086 { } 00087 }; 00088 00089 public: 00090 typedef _Alloc allocator_type; 00091 00092 allocator_type 00093 get_allocator() const 00094 { return *static_cast<const _Alloc*>(&this->_M_impl); } 00095 00096 _Vector_base(const allocator_type& __a) 00097 : _M_impl(__a) 00098 { } 00099 00100 _Vector_base(size_t __n, const allocator_type& __a) 00101 : _M_impl(__a) 00102 { 00103 this->_M_impl._M_start = this->_M_allocate(__n); 00104 this->_M_impl._M_finish = this->_M_impl._M_start; 00105 this->_M_impl._M_end_of_storage = this->_M_impl._M_start + __n; 00106 } 00107 00108 ~_Vector_base() 00109 { _M_deallocate(this->_M_impl._M_start, this->_M_impl._M_end_of_storage 00110 - this->_M_impl._M_start); } 00111 00112 public: 00113 _Vector_impl _M_impl; 00114 00115 _Tp* 00116 _M_allocate(size_t __n) 00117 { return _M_impl.allocate(__n); } 00118 00119 void 00120 _M_deallocate(_Tp* __p, size_t __n) 00121 { 00122 if (__p) 00123 _M_impl.deallocate(__p, __n); 00124 } 00125 }; 00126 00127 00128 /** 00129 * @brief A standard container which offers fixed time access to 00130 * individual elements in any order. 00131 * 00132 * @ingroup Containers 00133 * @ingroup Sequences 00134 * 00135 * Meets the requirements of a <a href="tables.html#65">container</a>, a 00136 * <a href="tables.html#66">reversible container</a>, and a 00137 * <a href="tables.html#67">sequence</a>, including the 00138 * <a href="tables.html#68">optional sequence requirements</a> with the 00139 * %exception of @c push_front and @c pop_front. 00140 * 00141 * In some terminology a %vector can be described as a dynamic 00142 * C-style array, it offers fast and efficient access to individual 00143 * elements in any order and saves the user from worrying about 00144 * memory and size allocation. Subscripting ( @c [] ) access is 00145 * also provided as with C-style arrays. 00146 */ 00147 template<typename _Tp, typename _Alloc = allocator<_Tp> > 00148 class vector : protected _Vector_base<_Tp, _Alloc> 00149 { 00150 // Concept requirements. 00151 __glibcxx_class_requires(_Tp, _SGIAssignableConcept) 00152 00153 typedef _Vector_base<_Tp, _Alloc> _Base; 00154 typedef vector<_Tp, _Alloc> vector_type; 00155 00156 public: 00157 typedef _Tp value_type; 00158 typedef typename _Alloc::pointer pointer; 00159 typedef typename _Alloc::const_pointer const_pointer; 00160 typedef typename _Alloc::reference reference; 00161 typedef typename _Alloc::const_reference const_reference; 00162 typedef __gnu_cxx::__normal_iterator<pointer, vector_type> iterator; 00163 typedef __gnu_cxx::__normal_iterator<const_pointer, vector_type> 00164 const_iterator; 00165 typedef std::reverse_iterator<const_iterator> const_reverse_iterator; 00166 typedef std::reverse_iterator<iterator> reverse_iterator; 00167 typedef size_t size_type; 00168 typedef ptrdiff_t difference_type; 00169 typedef typename _Base::allocator_type allocator_type; 00170 00171 protected: 00172 /** @if maint 00173 * These two functions and three data members are all from the 00174 * base class. They should be pretty self-explanatory, as 00175 * %vector uses a simple contiguous allocation scheme. @endif 00176 */ 00177 using _Base::_M_allocate; 00178 using _Base::_M_deallocate; 00179 using _Base::_M_impl; 00180 00181 public: 00182 // [23.2.4.1] construct/copy/destroy 00183 // (assign() and get_allocator() are also listed in this section) 00184 /** 00185 * @brief Default constructor creates no elements. 00186 */ 00187 explicit 00188 vector(const allocator_type& __a = allocator_type()) 00189 : _Base(__a) 00190 { } 00191 00192 /** 00193 * @brief Create a %vector with copies of an exemplar element. 00194 * @param n The number of elements to initially create. 00195 * @param value An element to copy. 00196 * 00197 * This constructor fills the %vector with @a n copies of @a value. 00198 */ 00199 vector(size_type __n, const value_type& __value, 00200 const allocator_type& __a = allocator_type()) 00201 : _Base(__n, __a) 00202 { 00203 std::__uninitialized_fill_n_a(this->_M_impl._M_start, __n, __value, 00204 this->get_allocator()); 00205 this->_M_impl._M_finish = this->_M_impl._M_start + __n; 00206 } 00207 00208 /** 00209 * @brief Create a %vector with default elements. 00210 * @param n The number of elements to initially create. 00211 * 00212 * This constructor fills the %vector with @a n copies of a 00213 * default-constructed element. 00214 */ 00215 explicit 00216 vector(size_type __n) 00217 : _Base(__n, allocator_type()) 00218 { 00219 std::__uninitialized_fill_n_a(this->_M_impl._M_start, __n, value_type(), 00220 this->get_allocator()); 00221 this->_M_impl._M_finish = this->_M_impl._M_start + __n; 00222 } 00223 00224 /** 00225 * @brief %Vector copy constructor. 00226 * @param x A %vector of identical element and allocator types. 00227 * 00228 * The newly-created %vector uses a copy of the allocation 00229 * object used by @a x. All the elements of @a x are copied, 00230 * but any extra memory in 00231 * @a x (for fast expansion) will not be copied. 00232 */ 00233 vector(const vector& __x) 00234 : _Base(__x.size(), __x.get_allocator()) 00235 { this->_M_impl._M_finish = 00236 std::__uninitialized_copy_a(__x.begin(), __x.end(), 00237 this->_M_impl._M_start, 00238 this->get_allocator()); 00239 } 00240 00241 /** 00242 * @brief Builds a %vector from a range. 00243 * @param first An input iterator. 00244 * @param last An input iterator. 00245 * 00246 * Create a %vector consisting of copies of the elements from 00247 * [first,last). 00248 * 00249 * If the iterators are forward, bidirectional, or 00250 * random-access, then this will call the elements' copy 00251 * constructor N times (where N is distance(first,last)) and do 00252 * no memory reallocation. But if only input iterators are 00253 * used, then this will do at most 2N calls to the copy 00254 * constructor, and logN memory reallocations. 00255 */ 00256 template<typename _InputIterator> 00257 vector(_InputIterator __first, _InputIterator __last, 00258 const allocator_type& __a = allocator_type()) 00259 : _Base(__a) 00260 { 00261 // Check whether it's an integral type. If so, it's not an iterator. 00262 typedef typename _Is_integer<_InputIterator>::_Integral _Integral; 00263 _M_initialize_dispatch(__first, __last, _Integral()); 00264 } 00265 00266 /** 00267 * The dtor only erases the elements, and note that if the 00268 * elements themselves are pointers, the pointed-to memory is 00269 * not touched in any way. Managing the pointer is the user's 00270 * responsibilty. 00271 */ 00272 ~vector() 00273 { std::_Destroy(this->_M_impl._M_start, this->_M_impl._M_finish, 00274 this->get_allocator()); 00275 } 00276 00277 /** 00278 * @brief %Vector assignment operator. 00279 * @param x A %vector of identical element and allocator types. 00280 * 00281 * All the elements of @a x are copied, but any extra memory in 00282 * @a x (for fast expansion) will not be copied. Unlike the 00283 * copy constructor, the allocator object is not copied. 00284 */ 00285 vector& 00286 operator=(const vector& __x); 00287 00288 /** 00289 * @brief Assigns a given value to a %vector. 00290 * @param n Number of elements to be assigned. 00291 * @param val Value to be assigned. 00292 * 00293 * This function fills a %vector with @a n copies of the given 00294 * value. Note that the assignment completely changes the 00295 * %vector and that the resulting %vector's size is the same as 00296 * the number of elements assigned. Old data may be lost. 00297 */ 00298 void 00299 assign(size_type __n, const value_type& __val) 00300 { _M_fill_assign(__n, __val); } 00301 00302 /** 00303 * @brief Assigns a range to a %vector. 00304 * @param first An input iterator. 00305 * @param last An input iterator. 00306 * 00307 * This function fills a %vector with copies of the elements in the 00308 * range [first,last). 00309 * 00310 * Note that the assignment completely changes the %vector and 00311 * that the resulting %vector's size is the same as the number 00312 * of elements assigned. Old data may be lost. 00313 */ 00314 template<typename _InputIterator> 00315 void 00316 assign(_InputIterator __first, _InputIterator __last) 00317 { 00318 // Check whether it's an integral type. If so, it's not an iterator. 00319 typedef typename _Is_integer<_InputIterator>::_Integral _Integral; 00320 _M_assign_dispatch(__first, __last, _Integral()); 00321 } 00322 00323 /// Get a copy of the memory allocation object. 00324 using _Base::get_allocator; 00325 00326 // iterators 00327 /** 00328 * Returns a read/write iterator that points to the first 00329 * element in the %vector. Iteration is done in ordinary 00330 * element order. 00331 */ 00332 iterator 00333 begin() 00334 { return iterator (this->_M_impl._M_start); } 00335 00336 /** 00337 * Returns a read-only (constant) iterator that points to the 00338 * first element in the %vector. Iteration is done in ordinary 00339 * element order. 00340 */ 00341 const_iterator 00342 begin() const 00343 { return const_iterator (this->_M_impl._M_start); } 00344 00345 /** 00346 * Returns a read/write iterator that points one past the last 00347 * element in the %vector. Iteration is done in ordinary 00348 * element order. 00349 */ 00350 iterator 00351 end() 00352 { return iterator (this->_M_impl._M_finish); } 00353 00354 /** 00355 * Returns a read-only (constant) iterator that points one past 00356 * the last element in the %vector. Iteration is done in 00357 * ordinary element order. 00358 */ 00359 const_iterator 00360 end() const 00361 { return const_iterator (this->_M_impl._M_finish); } 00362 00363 /** 00364 * Returns a read/write reverse iterator that points to the 00365 * last element in the %vector. Iteration is done in reverse 00366 * element order. 00367 */ 00368 reverse_iterator 00369 rbegin() 00370 { return reverse_iterator(end()); } 00371 00372 /** 00373 * Returns a read-only (constant) reverse iterator that points 00374 * to the last element in the %vector. Iteration is done in 00375 * reverse element order. 00376 */ 00377 const_reverse_iterator 00378 rbegin() const 00379 { return const_reverse_iterator(end()); } 00380 00381 /** 00382 * Returns a read/write reverse iterator that points to one 00383 * before the first element in the %vector. Iteration is done 00384 * in reverse element order. 00385 */ 00386 reverse_iterator 00387 rend() 00388 { return reverse_iterator(begin()); } 00389 00390 /** 00391 * Returns a read-only (constant) reverse iterator that points 00392 * to one before the first element in the %vector. Iteration 00393 * is done in reverse element order. 00394 */ 00395 const_reverse_iterator 00396 rend() const 00397 { return const_reverse_iterator(begin()); } 00398 00399 // [23.2.4.2] capacity 00400 /** Returns the number of elements in the %vector. */ 00401 size_type 00402 size() const 00403 { return size_type(end() - begin()); } 00404 00405 /** Returns the size() of the largest possible %vector. */ 00406 size_type 00407 max_size() const 00408 { return size_type(-1) / sizeof(value_type); } 00409 00410 /** 00411 * @brief Resizes the %vector to the specified number of elements. 00412 * @param new_size Number of elements the %vector should contain. 00413 * @param x Data with which new elements should be populated. 00414 * 00415 * This function will %resize the %vector to the specified 00416 * number of elements. If the number is smaller than the 00417 * %vector's current size the %vector is truncated, otherwise 00418 * the %vector is extended and new elements are populated with 00419 * given data. 00420 */ 00421 void 00422 resize(size_type __new_size, const value_type& __x) 00423 { 00424 if (__new_size < size()) 00425 erase(begin() + __new_size, end()); 00426 else 00427 insert(end(), __new_size - size(), __x); 00428 } 00429 00430 /** 00431 * @brief Resizes the %vector to the specified number of elements. 00432 * @param new_size Number of elements the %vector should contain. 00433 * 00434 * This function will resize the %vector to the specified 00435 * number of elements. If the number is smaller than the 00436 * %vector's current size the %vector is truncated, otherwise 00437 * the %vector is extended and new elements are 00438 * default-constructed. 00439 */ 00440 void 00441 resize(size_type __new_size) 00442 { resize(__new_size, value_type()); } 00443 00444 /** 00445 * Returns the total number of elements that the %vector can 00446 * hold before needing to allocate more memory. 00447 */ 00448 size_type 00449 capacity() const 00450 { return size_type(const_iterator(this->_M_impl._M_end_of_storage) 00451 - begin()); } 00452 00453 /** 00454 * Returns true if the %vector is empty. (Thus begin() would 00455 * equal end().) 00456 */ 00457 bool 00458 empty() const 00459 { return begin() == end(); } 00460 00461 /** 00462 * @brief Attempt to preallocate enough memory for specified number of 00463 * elements. 00464 * @param n Number of elements required. 00465 * @throw std::length_error If @a n exceeds @c max_size(). 00466 * 00467 * This function attempts to reserve enough memory for the 00468 * %vector to hold the specified number of elements. If the 00469 * number requested is more than max_size(), length_error is 00470 * thrown. 00471 * 00472 * The advantage of this function is that if optimal code is a 00473 * necessity and the user can determine the number of elements 00474 * that will be required, the user can reserve the memory in 00475 * %advance, and thus prevent a possible reallocation of memory 00476 * and copying of %vector data. 00477 */ 00478 void 00479 reserve(size_type __n); 00480 00481 // element access 00482 /** 00483 * @brief Subscript access to the data contained in the %vector. 00484 * @param n The index of the element for which data should be 00485 * accessed. 00486 * @return Read/write reference to data. 00487 * 00488 * This operator allows for easy, array-style, data access. 00489 * Note that data access with this operator is unchecked and 00490 * out_of_range lookups are not defined. (For checked lookups 00491 * see at().) 00492 */ 00493 reference 00494 operator[](size_type __n) 00495 { return *(begin() + __n); } 00496 00497 /** 00498 * @brief Subscript access to the data contained in the %vector. 00499 * @param n The index of the element for which data should be 00500 * accessed. 00501 * @return Read-only (constant) reference to data. 00502 * 00503 * This operator allows for easy, array-style, data access. 00504 * Note that data access with this operator is unchecked and 00505 * out_of_range lookups are not defined. (For checked lookups 00506 * see at().) 00507 */ 00508 const_reference 00509 operator[](size_type __n) const 00510 { return *(begin() + __n); } 00511 00512 protected: 00513 /// @if maint Safety check used only from at(). @endif 00514 void 00515 _M_range_check(size_type __n) const 00516 { 00517 if (__n >= this->size()) 00518 __throw_out_of_range(__N("vector::_M_range_check")); 00519 } 00520 00521 public: 00522 /** 00523 * @brief Provides access to the data contained in the %vector. 00524 * @param n The index of the element for which data should be 00525 * accessed. 00526 * @return Read/write reference to data. 00527 * @throw std::out_of_range If @a n is an invalid index. 00528 * 00529 * This function provides for safer data access. The parameter 00530 * is first checked that it is in the range of the vector. The 00531 * function throws out_of_range if the check fails. 00532 */ 00533 reference 00534 at(size_type __n) 00535 { 00536 _M_range_check(__n); 00537 return (*this)[__n]; 00538 } 00539 00540 /** 00541 * @brief Provides access to the data contained in the %vector. 00542 * @param n The index of the element for which data should be 00543 * accessed. 00544 * @return Read-only (constant) reference to data. 00545 * @throw std::out_of_range If @a n is an invalid index. 00546 * 00547 * This function provides for safer data access. The parameter 00548 * is first checked that it is in the range of the vector. The 00549 * function throws out_of_range if the check fails. 00550 */ 00551 const_reference 00552 at(size_type __n) const 00553 { 00554 _M_range_check(__n); 00555 return (*this)[__n]; 00556 } 00557 00558 /** 00559 * Returns a read/write reference to the data at the first 00560 * element of the %vector. 00561 */ 00562 reference 00563 front() 00564 { return *begin(); } 00565 00566 /** 00567 * Returns a read-only (constant) reference to the data at the first 00568 * element of the %vector. 00569 */ 00570 const_reference 00571 front() const 00572 { return *begin(); } 00573 00574 /** 00575 * Returns a read/write reference to the data at the last 00576 * element of the %vector. 00577 */ 00578 reference 00579 back() 00580 { return *(end() - 1); } 00581 00582 /** 00583 * Returns a read-only (constant) reference to the data at the 00584 * last element of the %vector. 00585 */ 00586 const_reference 00587 back() const 00588 { return *(end() - 1); } 00589 00590 // [23.2.4.3] modifiers 00591 /** 00592 * @brief Add data to the end of the %vector. 00593 * @param x Data to be added. 00594 * 00595 * This is a typical stack operation. The function creates an 00596 * element at the end of the %vector and assigns the given data 00597 * to it. Due to the nature of a %vector this operation can be 00598 * done in constant time if the %vector has preallocated space 00599 * available. 00600 */ 00601 void 00602 push_back(const value_type& __x) 00603 { 00604 if (this->_M_impl._M_finish != this->_M_impl._M_end_of_storage) 00605 { 00606 this->_M_impl.construct(this->_M_impl._M_finish, __x); 00607 ++this->_M_impl._M_finish; 00608 } 00609 else 00610 _M_insert_aux(end(), __x); 00611 } 00612 00613 /** 00614 * @brief Removes last element. 00615 * 00616 * This is a typical stack operation. It shrinks the %vector by one. 00617 * 00618 * Note that no data is returned, and if the last element's 00619 * data is needed, it should be retrieved before pop_back() is 00620 * called. 00621 */ 00622 void 00623 pop_back() 00624 { 00625 --this->_M_impl._M_finish; 00626 this->_M_impl.destroy(this->_M_impl._M_finish); 00627 } 00628 00629 /** 00630 * @brief Inserts given value into %vector before specified iterator. 00631 * @param position An iterator into the %vector. 00632 * @param x Data to be inserted. 00633 * @return An iterator that points to the inserted data. 00634 * 00635 * This function will insert a copy of the given value before 00636 * the specified location. Note that this kind of operation 00637 * could be expensive for a %vector and if it is frequently 00638 * used the user should consider using std::list. 00639 */ 00640 iterator 00641 insert(iterator __position, const value_type& __x); 00642 00643 /** 00644 * @brief Inserts a number of copies of given data into the %vector. 00645 * @param position An iterator into the %vector. 00646 * @param n Number of elements to be inserted. 00647 * @param x Data to be inserted. 00648 * 00649 * This function will insert a specified number of copies of 00650 * the given data before the location specified by @a position. 00651 * 00652 * Note that this kind of operation could be expensive for a 00653 * %vector and if it is frequently used the user should 00654 * consider using std::list. 00655 */ 00656 void 00657 insert(iterator __position, size_type __n, const value_type& __x) 00658 { _M_fill_insert(__position, __n, __x); } 00659 00660 /** 00661 * @brief Inserts a range into the %vector. 00662 * @param position An iterator into the %vector. 00663 * @param first An input iterator. 00664 * @param last An input iterator. 00665 * 00666 * This function will insert copies of the data in the range 00667 * [first,last) into the %vector before the location specified 00668 * by @a pos. 00669 * 00670 * Note that this kind of operation could be expensive for a 00671 * %vector and if it is frequently used the user should 00672 * consider using std::list. 00673 */ 00674 template<typename _InputIterator> 00675 void 00676 insert(iterator __position, _InputIterator __first, 00677 _InputIterator __last) 00678 { 00679 // Check whether it's an integral type. If so, it's not an iterator. 00680 typedef typename _Is_integer<_InputIterator>::_Integral _Integral; 00681 _M_insert_dispatch(__position, __first, __last, _Integral()); 00682 } 00683 00684 /** 00685 * @brief Remove element at given position. 00686 * @param position Iterator pointing to element to be erased. 00687 * @return An iterator pointing to the next element (or end()). 00688 * 00689 * This function will erase the element at the given position and thus 00690 * shorten the %vector by one. 00691 * 00692 * Note This operation could be expensive and if it is 00693 * frequently used the user should consider using std::list. 00694 * The user is also cautioned that this function only erases 00695 * the element, and that if the element is itself a pointer, 00696 * the pointed-to memory is not touched in any way. Managing 00697 * the pointer is the user's responsibilty. 00698 */ 00699 iterator 00700 erase(iterator __position); 00701 00702 /** 00703 * @brief Remove a range of elements. 00704 * @param first Iterator pointing to the first element to be erased. 00705 * @param last Iterator pointing to one past the last element to be 00706 * erased. 00707 * @return An iterator pointing to the element pointed to by @a last 00708 * prior to erasing (or end()). 00709 * 00710 * This function will erase the elements in the range [first,last) and 00711 * shorten the %vector accordingly. 00712 * 00713 * Note This operation could be expensive and if it is 00714 * frequently used the user should consider using std::list. 00715 * The user is also cautioned that this function only erases 00716 * the elements, and that if the elements themselves are 00717 * pointers, the pointed-to memory is not touched in any way. 00718 * Managing the pointer is the user's responsibilty. 00719 */ 00720 iterator 00721 erase(iterator __first, iterator __last); 00722 00723 /** 00724 * @brief Swaps data with another %vector. 00725 * @param x A %vector of the same element and allocator types. 00726 * 00727 * This exchanges the elements between two vectors in constant time. 00728 * (Three pointers, so it should be quite fast.) 00729 * Note that the global std::swap() function is specialized such that 00730 * std::swap(v1,v2) will feed to this function. 00731 */ 00732 void 00733 swap(vector& __x) 00734 { 00735 std::swap(this->_M_impl._M_start, __x._M_impl._M_start); 00736 std::swap(this->_M_impl._M_finish, __x._M_impl._M_finish); 00737 std::swap(this->_M_impl._M_end_of_storage, 00738 __x._M_impl._M_end_of_storage); 00739 } 00740 00741 /** 00742 * Erases all the elements. Note that this function only erases the 00743 * elements, and that if the elements themselves are pointers, the 00744 * pointed-to memory is not touched in any way. Managing the pointer is 00745 * the user's responsibilty. 00746 */ 00747 void 00748 clear() 00749 { erase(begin(), end()); } 00750 00751 protected: 00752 /** 00753 * @if maint 00754 * Memory expansion handler. Uses the member allocation function to 00755 * obtain @a n bytes of memory, and then copies [first,last) into it. 00756 * @endif 00757 */ 00758 template<typename _ForwardIterator> 00759 pointer 00760 _M_allocate_and_copy(size_type __n, 00761 _ForwardIterator __first, _ForwardIterator __last) 00762 { 00763 pointer __result = this->_M_allocate(__n); 00764 try 00765 { 00766 std::__uninitialized_copy_a(__first, __last, __result, 00767 this->get_allocator()); 00768 return __result; 00769 } 00770 catch(...) 00771 { 00772 _M_deallocate(__result, __n); 00773 __throw_exception_again; 00774 } 00775 } 00776 00777 00778 // Internal constructor functions follow. 00779 00780 // Called by the range constructor to implement [23.1.1]/9 00781 template<typename _Integer> 00782 void 00783 _M_initialize_dispatch(_Integer __n, _Integer __value, __true_type) 00784 { 00785 this->_M_impl._M_start = _M_allocate(__n); 00786 this->_M_impl._M_end_of_storage = this->_M_impl._M_start + __n; 00787 std::__uninitialized_fill_n_a(this->_M_impl._M_start, __n, __value, 00788 this->get_allocator()); 00789 this->_M_impl._M_finish = this->_M_impl._M_end_of_storage; 00790 } 00791 00792 // Called by the range constructor to implement [23.1.1]/9 00793 template<typename _InputIterator> 00794 void 00795 _M_initialize_dispatch(_InputIterator __first, _InputIterator __last, 00796 __false_type) 00797 { 00798 typedef typename iterator_traits<_InputIterator>::iterator_category 00799 _IterCategory; 00800 _M_range_initialize(__first, __last, _IterCategory()); 00801 } 00802 00803 // Called by the second initialize_dispatch above 00804 template<typename _InputIterator> 00805 void 00806 _M_range_initialize(_InputIterator __first, 00807 _InputIterator __last, input_iterator_tag) 00808 { 00809 for (; __first != __last; ++__first) 00810 push_back(*__first); 00811 } 00812 00813 // Called by the second initialize_dispatch above 00814 template<typename _ForwardIterator> 00815 void 00816 _M_range_initialize(_ForwardIterator __first, 00817 _ForwardIterator __last, forward_iterator_tag) 00818 { 00819 const size_type __n = std::distance(__first, __last); 00820 this->_M_impl._M_start = this->_M_allocate(__n); 00821 this->_M_impl._M_end_of_storage = this->_M_impl._M_start + __n; 00822 this->_M_impl._M_finish = 00823 std::__uninitialized_copy_a(__first, __last, 00824 this->_M_impl._M_start, 00825 this->get_allocator()); 00826 } 00827 00828 00829 // Internal assign functions follow. The *_aux functions do the actual 00830 // assignment work for the range versions. 00831 00832 // Called by the range assign to implement [23.1.1]/9 00833 template<typename _Integer> 00834 void 00835 _M_assign_dispatch(_Integer __n, _Integer __val, __true_type) 00836 { 00837 _M_fill_assign(static_cast<size_type>(__n), 00838 static_cast<value_type>(__val)); 00839 } 00840 00841 // Called by the range assign to implement [23.1.1]/9 00842 template<typename _InputIterator> 00843 void 00844 _M_assign_dispatch(_InputIterator __first, _InputIterator __last, 00845 __false_type) 00846 { 00847 typedef typename iterator_traits<_InputIterator>::iterator_category 00848 _IterCategory; 00849 _M_assign_aux(__first, __last, _IterCategory()); 00850 } 00851 00852 // Called by the second assign_dispatch above 00853 template<typename _InputIterator> 00854 void 00855 _M_assign_aux(_InputIterator __first, _InputIterator __last, 00856 input_iterator_tag); 00857 00858 // Called by the second assign_dispatch above 00859 template<typename _ForwardIterator> 00860 void 00861 _M_assign_aux(_ForwardIterator __first, _ForwardIterator __last, 00862 forward_iterator_tag); 00863 00864 // Called by assign(n,t), and the range assign when it turns out 00865 // to be the same thing. 00866 void 00867 _M_fill_assign(size_type __n, const value_type& __val); 00868 00869 00870 // Internal insert functions follow. 00871 00872 // Called by the range insert to implement [23.1.1]/9 00873 template<typename _Integer> 00874 void 00875 _M_insert_dispatch(iterator __pos, _Integer __n, _Integer __val, 00876 __true_type) 00877 { 00878 _M_fill_insert(__pos, static_cast<size_type>(__n), 00879 static_cast<value_type>(__val)); 00880 } 00881 00882 // Called by the range insert to implement [23.1.1]/9 00883 template<typename _InputIterator> 00884 void 00885 _M_insert_dispatch(iterator __pos, _InputIterator __first, 00886 _InputIterator __last, __false_type) 00887 { 00888 typedef typename iterator_traits<_InputIterator>::iterator_category 00889 _IterCategory; 00890 _M_range_insert(__pos, __first, __last, _IterCategory()); 00891 } 00892 00893 // Called by the second insert_dispatch above 00894 template<typename _InputIterator> 00895 void 00896 _M_range_insert(iterator __pos, _InputIterator __first, 00897 _InputIterator __last, input_iterator_tag); 00898 00899 // Called by the second insert_dispatch above 00900 template<typename _ForwardIterator> 00901 void 00902 _M_range_insert(iterator __pos, _ForwardIterator __first, 00903 _ForwardIterator __last, forward_iterator_tag); 00904 00905 // Called by insert(p,n,x), and the range insert when it turns out to be 00906 // the same thing. 00907 void 00908 _M_fill_insert(iterator __pos, size_type __n, const value_type& __x); 00909 00910 // Called by insert(p,x) 00911 void 00912 _M_insert_aux(iterator __position, const value_type& __x); 00913 }; 00914 00915 00916 /** 00917 * @brief Vector equality comparison. 00918 * @param x A %vector. 00919 * @param y A %vector of the same type as @a x. 00920 * @return True iff the size and elements of the vectors are equal. 00921 * 00922 * This is an equivalence relation. It is linear in the size of the 00923 * vectors. Vectors are considered equivalent if their sizes are equal, 00924 * and if corresponding elements compare equal. 00925 */ 00926 template<typename _Tp, typename _Alloc> 00927 inline bool 00928 operator==(const vector<_Tp, _Alloc>& __x, const vector<_Tp, _Alloc>& __y) 00929 { return (__x.size() == __y.size() 00930 && std::equal(__x.begin(), __x.end(), __y.begin())); } 00931 00932 /** 00933 * @brief Vector ordering relation. 00934 * @param x A %vector. 00935 * @param y A %vector of the same type as @a x. 00936 * @return True iff @a x is lexicographically less than @a y. 00937 * 00938 * This is a total ordering relation. It is linear in the size of the 00939 * vectors. The elements must be comparable with @c <. 00940 * 00941 * See std::lexicographical_compare() for how the determination is made. 00942 */ 00943 template<typename _Tp, typename _Alloc> 00944 inline bool 00945 operator<(const vector<_Tp, _Alloc>& __x, const vector<_Tp, _Alloc>& __y) 00946 { return std::lexicographical_compare(__x.begin(), __x.end(), 00947 __y.begin(), __y.end()); } 00948 00949 /// Based on operator== 00950 template<typename _Tp, typename _Alloc> 00951 inline bool 00952 operator!=(const vector<_Tp, _Alloc>& __x, const vector<_Tp, _Alloc>& __y) 00953 { return !(__x == __y); } 00954 00955 /// Based on operator< 00956 template<typename _Tp, typename _Alloc> 00957 inline bool 00958 operator>(const vector<_Tp, _Alloc>& __x, const vector<_Tp, _Alloc>& __y) 00959 { return __y < __x; } 00960 00961 /// Based on operator< 00962 template<typename _Tp, typename _Alloc> 00963 inline bool 00964 operator<=(const vector<_Tp, _Alloc>& __x, const vector<_Tp, _Alloc>& __y) 00965 { return !(__y < __x); } 00966 00967 /// Based on operator< 00968 template<typename _Tp, typename _Alloc> 00969 inline bool 00970 operator>=(const vector<_Tp, _Alloc>& __x, const vector<_Tp, _Alloc>& __y) 00971 { return !(__x < __y); } 00972 00973 /// See std::vector::swap(). 00974 template<typename _Tp, typename _Alloc> 00975 inline void 00976 swap(vector<_Tp, _Alloc>& __x, vector<_Tp, _Alloc>& __y) 00977 { __x.swap(__y); } 00978 } // namespace std 00979 00980 #endif /* _VECTOR_H */

Generated on Sun Sep 12 15:49:59 2004 for libstdc++ source by doxygen 1.3.8