Intro.3: Date tweak.
[gcc.git] / libstdc++-v3 / include / bits / stl_vector.h
1 // Vector implementation -*- C++ -*-
2
3 // Copyright (C) 2001, 2002 Free Software Foundation, Inc.
4 //
5 // This file is part of the GNU ISO C++ Library. This library is free
6 // software; you can redistribute it and/or modify it under the
7 // terms of the GNU General Public License as published by the
8 // Free Software Foundation; either version 2, or (at your option)
9 // any later version.
10
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
15
16 // You should have received a copy of the GNU General Public License along
17 // with this library; see the file COPYING. If not, write to the Free
18 // Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307,
19 // USA.
20
21 // As a special exception, you may use this file as part of a free software
22 // library without restriction. Specifically, if other files instantiate
23 // templates or use macros or inline functions from this file, or you compile
24 // this file and link it with other files to produce an executable, this
25 // file does not by itself cause the resulting executable to be covered by
26 // the GNU General Public License. This exception does not however
27 // invalidate any other reasons why the executable file might be covered by
28 // the GNU General Public License.
29
30 /*
31 *
32 * Copyright (c) 1994
33 * Hewlett-Packard Company
34 *
35 * Permission to use, copy, modify, distribute and sell this software
36 * and its documentation for any purpose is hereby granted without fee,
37 * provided that the above copyright notice appear in all copies and
38 * that both that copyright notice and this permission notice appear
39 * in supporting documentation. Hewlett-Packard Company makes no
40 * representations about the suitability of this software for any
41 * purpose. It is provided "as is" without express or implied warranty.
42 *
43 *
44 * Copyright (c) 1996
45 * Silicon Graphics Computer Systems, Inc.
46 *
47 * Permission to use, copy, modify, distribute and sell this software
48 * and its documentation for any purpose is hereby granted without fee,
49 * provided that the above copyright notice appear in all copies and
50 * that both that copyright notice and this permission notice appear
51 * in supporting documentation. Silicon Graphics makes no
52 * representations about the suitability of this software for any
53 * purpose. It is provided "as is" without express or implied warranty.
54 */
55
56 /** @file stl_vector.h
57 * This is an internal header file, included by other library headers.
58 * You should not attempt to use it directly.
59 */
60
61 #ifndef __GLIBCPP_INTERNAL_VECTOR_H
62 #define __GLIBCPP_INTERNAL_VECTOR_H
63
64 #include <bits/stl_iterator_base_funcs.h>
65 #include <bits/functexcept.h>
66 #include <bits/concept_check.h>
67
68 namespace std
69 {
70
71 // The vector base class serves two purposes. First, its constructor
72 // and destructor allocate (but don't initialize) storage. This makes
73 // exception safety easier. Second, the base class encapsulates all of
74 // the differences between SGI-style allocators and standard-conforming
75 // allocators.
76
77 // Base class for ordinary allocators.
78 template <class _Tp, class _Allocator, bool _IsStatic>
79 class _Vector_alloc_base {
80 public:
81 typedef typename _Alloc_traits<_Tp, _Allocator>::allocator_type
82 allocator_type;
83 allocator_type get_allocator() const { return _M_data_allocator; }
84
85 _Vector_alloc_base(const allocator_type& __a)
86 : _M_data_allocator(__a), _M_start(0), _M_finish(0), _M_end_of_storage(0)
87 {}
88
89 protected:
90 allocator_type _M_data_allocator;
91 _Tp* _M_start;
92 _Tp* _M_finish;
93 _Tp* _M_end_of_storage;
94
95 _Tp* _M_allocate(size_t __n)
96 { return _M_data_allocator.allocate(__n); }
97 void _M_deallocate(_Tp* __p, size_t __n)
98 { if (__p) _M_data_allocator.deallocate(__p, __n); }
99 };
100
101 // Specialization for allocators that have the property that we don't
102 // actually have to store an allocator object.
103 template <class _Tp, class _Allocator>
104 class _Vector_alloc_base<_Tp, _Allocator, true> {
105 public:
106 typedef typename _Alloc_traits<_Tp, _Allocator>::allocator_type
107 allocator_type;
108 allocator_type get_allocator() const { return allocator_type(); }
109
110 _Vector_alloc_base(const allocator_type&)
111 : _M_start(0), _M_finish(0), _M_end_of_storage(0)
112 {}
113
114 protected:
115 _Tp* _M_start;
116 _Tp* _M_finish;
117 _Tp* _M_end_of_storage;
118
119 typedef typename _Alloc_traits<_Tp, _Allocator>::_Alloc_type _Alloc_type;
120 _Tp* _M_allocate(size_t __n)
121 { return _Alloc_type::allocate(__n); }
122 void _M_deallocate(_Tp* __p, size_t __n)
123 { _Alloc_type::deallocate(__p, __n);}
124 };
125
126 template <class _Tp, class _Alloc>
127 struct _Vector_base
128 : public _Vector_alloc_base<_Tp, _Alloc,
129 _Alloc_traits<_Tp, _Alloc>::_S_instanceless>
130 {
131 typedef _Vector_alloc_base<_Tp, _Alloc,
132 _Alloc_traits<_Tp, _Alloc>::_S_instanceless>
133 _Base;
134 typedef typename _Base::allocator_type allocator_type;
135
136 _Vector_base(const allocator_type& __a) : _Base(__a) {}
137 _Vector_base(size_t __n, const allocator_type& __a) : _Base(__a) {
138 _M_start = _M_allocate(__n);
139 _M_finish = _M_start;
140 _M_end_of_storage = _M_start + __n;
141 }
142
143 ~_Vector_base() { _M_deallocate(_M_start, _M_end_of_storage - _M_start); }
144 };
145
146
147 /**
148 * @brief A standard container which offers fixed time access to individual
149 * elements in any order.
150 *
151 * @ingroup Containers
152 * @ingroup Sequences
153 *
154 * Meets the requirements of a <a href="tables.html#65">container</a>, a
155 * <a href="tables.html#66">reversible container</a>, and a
156 * <a href="tables.html#67">sequence</a>, including the
157 * <a href="tables.html#68">optional sequence requirements</a> with the
158 * %exception of @c push_front and @c pop_front.
159 *
160 * In some terminology a vector can be described as a dynamic C-style array,
161 * it offers fast and efficient access to individual elements in any order
162 * and saves the user from worrying about memory and size allocation.
163 * Subscripting ( [] ) access is also provided as with C-style arrays.
164 */
165 template <class _Tp, class _Alloc = allocator<_Tp> >
166 class vector : protected _Vector_base<_Tp, _Alloc>
167 {
168 // concept requirements
169 __glibcpp_class_requires(_Tp, _SGIAssignableConcept)
170
171 private:
172 typedef _Vector_base<_Tp, _Alloc> _Base;
173 typedef vector<_Tp, _Alloc> vector_type;
174 public:
175 typedef _Tp value_type;
176 typedef value_type* pointer;
177 typedef const value_type* const_pointer;
178 typedef __normal_iterator<pointer, vector_type> iterator;
179 typedef __normal_iterator<const_pointer, vector_type> const_iterator;
180 typedef value_type& reference;
181 typedef const value_type& const_reference;
182 typedef size_t size_type;
183 typedef ptrdiff_t difference_type;
184
185 typedef typename _Base::allocator_type allocator_type;
186 allocator_type get_allocator() const { return _Base::get_allocator(); }
187
188 typedef reverse_iterator<const_iterator> const_reverse_iterator;
189 typedef reverse_iterator<iterator> reverse_iterator;
190
191 protected:
192 using _Base::_M_allocate;
193 using _Base::_M_deallocate;
194 using _Base::_M_start;
195 using _Base::_M_finish;
196 using _Base::_M_end_of_storage;
197
198 protected:
199 void _M_insert_aux(iterator __position, const _Tp& __x);
200 void _M_insert_aux(iterator __position);
201
202 public:
203 /**
204 * Returns a read/write iterator that points to the first element in the
205 * vector. Iteration is done in ordinary element order.
206 */
207 iterator begin() { return iterator (_M_start); }
208
209 /**
210 * Returns a read-only (constant) iterator that points to the first element
211 * in the vector. Iteration is done in ordinary element order.
212 */
213 const_iterator begin() const
214 { return const_iterator (_M_start); }
215
216 /**
217 * Returns a read/write iterator that points one past the last element in
218 * the vector. Iteration is done in ordinary element order.
219 */
220 iterator end() { return iterator (_M_finish); }
221
222 /**
223 * Returns a read-only (constant) iterator that points one past the last
224 * element in the vector. Iteration is done in ordinary element order.
225 */
226 const_iterator end() const { return const_iterator (_M_finish); }
227
228 /**
229 * Returns a read/write reverse iterator that points to the last element in
230 * the vector. Iteration is done in reverse element order.
231 */
232 reverse_iterator rbegin()
233 { return reverse_iterator(end()); }
234
235 /**
236 * Returns a read-only (constant) reverse iterator that points to the last
237 * element in the vector. Iteration is done in reverse element order.
238 */
239 const_reverse_iterator rbegin() const
240 { return const_reverse_iterator(end()); }
241
242 /**
243 * Returns a read/write reverse iterator that points to one before the
244 * first element in the vector. Iteration is done in reverse element
245 * order.
246 */
247 reverse_iterator rend()
248 { return reverse_iterator(begin()); }
249
250 /**
251 * Returns a read-only (constant) reverse iterator that points to one
252 * before the first element in the vector. Iteration is done in reverse
253 * element order.
254 */
255 const_reverse_iterator rend() const
256 { return const_reverse_iterator(begin()); }
257
258 /** Returns the number of elements in the vector. */
259 size_type size() const
260 { return size_type(end() - begin()); }
261
262 /** Returns the size of the largest possible vector. */
263 size_type max_size() const
264 { return size_type(-1) / sizeof(_Tp); }
265
266 /**
267 * Returns the amount of memory that has been alocated for the current
268 * elements (?).
269 */
270 size_type capacity() const
271 { return size_type(const_iterator(_M_end_of_storage) - begin()); }
272
273 /**
274 * Returns true if the vector is empty. (Thus begin() would equal end().)
275 */
276 bool empty() const
277 { return begin() == end(); }
278
279 /**
280 * @brief Subscript access to the data contained in the vector.
281 * @param n The element for which data should be accessed.
282 * @return Read/write reference to data.
283 *
284 * This operator allows for easy, array-style, data access.
285 * Note that data access with this operator is unchecked and out_of_range
286 * lookups are not defined. (For checked lookups see at().)
287 */
288 reference operator[](size_type __n) { return *(begin() + __n); }
289
290 /**
291 * @brief Subscript access to the data contained in the vector.
292 * @param n The element for which data should be accessed.
293 * @return Read-only (constant) reference to data.
294 *
295 * This operator allows for easy, array-style, data access.
296 * Note that data access with this operator is unchecked and out_of_range
297 * lookups are not defined. (For checked lookups see at().)
298 */
299 const_reference operator[](size_type __n) const { return *(begin() + __n); }
300
301 void _M_range_check(size_type __n) const {
302 if (__n >= this->size())
303 __throw_out_of_range("vector");
304 }
305
306 /**
307 * @brief Provides access to the data contained in the vector.
308 * @param n The element for which data should be accessed.
309 * @return Read/write reference to data.
310 *
311 * This function provides for safer data access. The parameter is first
312 * checked that it is in the range of the vector. The function throws
313 * out_of_range if the check fails.
314 */
315 reference at(size_type __n)
316 { _M_range_check(__n); return (*this)[__n]; }
317
318 /**
319 * @brief Provides access to the data contained in the vector.
320 * @param n The element for which data should be accessed.
321 * @return Read-only (constant) reference to data.
322 *
323 * This function provides for safer data access. The parameter is first
324 * checked that it is in the range of the vector. The function throws
325 * out_of_range if the check fails.
326 */
327 const_reference at(size_type __n) const
328 { _M_range_check(__n); return (*this)[__n]; }
329
330
331 explicit vector(const allocator_type& __a = allocator_type())
332 : _Base(__a) {}
333
334 vector(size_type __n, const _Tp& __value,
335 const allocator_type& __a = allocator_type())
336 : _Base(__n, __a)
337 { _M_finish = uninitialized_fill_n(_M_start, __n, __value); }
338
339 explicit vector(size_type __n)
340 : _Base(__n, allocator_type())
341 { _M_finish = uninitialized_fill_n(_M_start, __n, _Tp()); }
342
343 vector(const vector<_Tp, _Alloc>& __x)
344 : _Base(__x.size(), __x.get_allocator())
345 { _M_finish = uninitialized_copy(__x.begin(), __x.end(), _M_start); }
346
347 // Check whether it's an integral type. If so, it's not an iterator.
348 template <class _InputIterator>
349 vector(_InputIterator __first, _InputIterator __last,
350 const allocator_type& __a = allocator_type())
351 : _Base(__a)
352 {
353 typedef typename _Is_integer<_InputIterator>::_Integral _Integral;
354 _M_initialize_aux(__first, __last, _Integral());
355 }
356
357 template <class _Integer>
358 void _M_initialize_aux(_Integer __n, _Integer __value, __true_type)
359 {
360 _M_start = _M_allocate(__n);
361 _M_end_of_storage = _M_start + __n;
362 _M_finish = uninitialized_fill_n(_M_start, __n, __value);
363 }
364
365 template<class _InputIterator>
366 void
367 _M_initialize_aux(_InputIterator __first, _InputIterator __last, __false_type)
368 {
369 typedef typename iterator_traits<_InputIterator>::iterator_category _IterCategory;
370 _M_range_initialize(__first, __last, _IterCategory());
371 }
372
373 ~vector()
374 { _Destroy(_M_start, _M_finish); }
375
376 vector<_Tp, _Alloc>& operator=(const vector<_Tp, _Alloc>& __x);
377
378 /**
379 * @brief Attempt to preallocate enough memory for specified number of
380 * elements.
381 * @param n Number of elements required
382 *
383 * This function attempts to reserve enough memory for the vector to hold
384 * the specified number of elements. If the number requested is more than
385 * max_size() length_error is thrown.
386 *
387 * The advantage of this function is that if optimal code is a necessity
388 * and the user can determine the number of elements that will be required
389 * the user can reserve the memory and thus prevent a possible
390 * reallocation of memory and copy of vector data.
391 */
392 void reserve(size_type __n) {
393 if (capacity() < __n) {
394 const size_type __old_size = size();
395 pointer __tmp = _M_allocate_and_copy(__n, _M_start, _M_finish);
396 _Destroy(_M_start, _M_finish);
397 _M_deallocate(_M_start, _M_end_of_storage - _M_start);
398 _M_start = __tmp;
399 _M_finish = __tmp + __old_size;
400 _M_end_of_storage = _M_start + __n;
401 }
402 }
403
404 // assign(), a generalized assignment member function. Two
405 // versions: one that takes a count, and one that takes a range.
406 // The range version is a member template, so we dispatch on whether
407 // or not the type is an integer.
408
409 /**
410 * @brief Assigns a given value or range to a vector.
411 * @param n Number of elements to be assigned.
412 * @param val Value to be assigned.
413 *
414 * This function can be used to assign a range to a vector or fill it
415 * with a specified number of copies of the given value.
416 * Note that the assignment completely changes the vector and that the
417 * resulting vector's size is the same as the number of elements assigned.
418 * Old data may be lost.
419 */
420 void assign(size_type __n, const _Tp& __val) { _M_fill_assign(__n, __val); }
421 void _M_fill_assign(size_type __n, const _Tp& __val);
422
423 template<class _InputIterator>
424 void
425 assign(_InputIterator __first, _InputIterator __last)
426 {
427 typedef typename _Is_integer<_InputIterator>::_Integral _Integral;
428 _M_assign_dispatch(__first, __last, _Integral());
429 }
430
431 template<class _Integer>
432 void
433 _M_assign_dispatch(_Integer __n, _Integer __val, __true_type)
434 { _M_fill_assign((size_type) __n, (_Tp) __val); }
435
436 template<class _InputIter>
437 void
438 _M_assign_dispatch(_InputIter __first, _InputIter __last, __false_type)
439 {
440 typedef typename iterator_traits<_InputIter>::iterator_category _IterCategory;
441 _M_assign_aux(__first, __last, _IterCategory());
442 }
443
444 template <class _InputIterator>
445 void _M_assign_aux(_InputIterator __first, _InputIterator __last,
446 input_iterator_tag);
447
448 template <class _ForwardIterator>
449 void _M_assign_aux(_ForwardIterator __first, _ForwardIterator __last,
450 forward_iterator_tag);
451
452 /**
453 * Returns a read/write reference to the data at the first element of the
454 * vector.
455 */
456 reference front() { return *begin(); }
457
458 /**
459 * Returns a read-only (constant) reference to the data at the first
460 * element of the vector.
461 */
462 const_reference front() const { return *begin(); }
463
464 /**
465 * Returns a read/write reference to the data at the last element of the
466 * vector.
467 */
468 reference back() { return *(end() - 1); }
469
470 /**
471 * Returns a read-only (constant) reference to the data at the first
472 * element of the vector.
473 */
474 const_reference back() const { return *(end() - 1); }
475
476 /**
477 * @brief Add data to the end of the vector.
478 * @param x Data to be added.
479 *
480 * This is a typical stack operation. The function creates an element at
481 * the end of the vector and assigns the given data to it.
482 * Due to the nature of a vector this operation can be done in constant
483 * time if the vector has preallocated space available.
484 */
485 void
486 push_back(const _Tp& __x)
487 {
488 if (_M_finish != _M_end_of_storage) {
489 _Construct(_M_finish, __x);
490 ++_M_finish;
491 }
492 else
493 _M_insert_aux(end(), __x);
494 }
495
496 #ifdef _GLIBCPP_DEPRECATED
497 /**
498 * Add an element to the end of the vector. The element is
499 * default-constructed.
500 *
501 * @note You must define _GLIBCPP_DEPRECATED to make this visible; see
502 * c++config.h.
503 */
504 void
505 push_back()
506 {
507 if (_M_finish != _M_end_of_storage) {
508 _Construct(_M_finish);
509 ++_M_finish;
510 }
511 else
512 _M_insert_aux(end());
513 }
514 #endif
515
516 void
517 swap(vector<_Tp, _Alloc>& __x)
518 {
519 std::swap(_M_start, __x._M_start);
520 std::swap(_M_finish, __x._M_finish);
521 std::swap(_M_end_of_storage, __x._M_end_of_storage);
522 }
523
524 /**
525 * @brief Inserts given value into vector at specified element.
526 * @param position An iterator that points to the element where data
527 * should be inserted.
528 * @param x Data to be inserted.
529 * @return An iterator that points to the inserted data.
530 *
531 * This function will insert the given value into the specified location.
532 * Note that this kind of operation could be expensive for a vector and if
533 * it is frequently used the user should consider using std::list.
534 */
535 iterator
536 insert(iterator __position, const _Tp& __x)
537 {
538 size_type __n = __position - begin();
539 if (_M_finish != _M_end_of_storage && __position == end()) {
540 _Construct(_M_finish, __x);
541 ++_M_finish;
542 }
543 else
544 _M_insert_aux(iterator(__position), __x);
545 return begin() + __n;
546 }
547
548 /**
549 * @brief Inserts an empty element into the vector.
550 * @param position An iterator that points to the element where empty
551 * element should be inserted.
552 * @param x Data to be inserted.
553 * @return An iterator that points to the inserted element.
554 *
555 * This function will insert an empty element into the specified location.
556 * Note that this kind of operation could be expensive for a vector and if
557 * it is frequently used the user should consider using std::list.
558 */
559 iterator
560 insert(iterator __position)
561 {
562 size_type __n = __position - begin();
563 if (_M_finish != _M_end_of_storage && __position == end()) {
564 _Construct(_M_finish);
565 ++_M_finish;
566 }
567 else
568 _M_insert_aux(iterator(__position));
569 return begin() + __n;
570 }
571
572 // Check whether it's an integral type. If so, it's not an iterator.
573 template<class _InputIterator>
574 void
575 insert(iterator __pos, _InputIterator __first, _InputIterator __last)
576 {
577 typedef typename _Is_integer<_InputIterator>::_Integral _Integral;
578 _M_insert_dispatch(__pos, __first, __last, _Integral());
579 }
580
581 template <class _Integer>
582 void
583 _M_insert_dispatch(iterator __pos, _Integer __n, _Integer __val, __true_type)
584 { _M_fill_insert(__pos, static_cast<size_type>(__n), static_cast<_Tp>(__val)); }
585
586 template<class _InputIterator>
587 void
588 _M_insert_dispatch(iterator __pos,
589 _InputIterator __first, _InputIterator __last,
590 __false_type)
591 {
592 typedef typename iterator_traits<_InputIterator>::iterator_category _IterCategory;
593 _M_range_insert(__pos, __first, __last, _IterCategory());
594 }
595
596 /**
597 * @brief Inserts a number of copies of given data into the vector.
598 * @param position An iterator that points to the element where data
599 * should be inserted.
600 * @param n Amount of elements to be inserted.
601 * @param x Data to be inserted.
602 *
603 * This function will insert a specified number of copies of the given data
604 * into the specified location.
605 *
606 * Note that this kind of operation could be expensive for a vector and if
607 * it is frequently used the user should consider using std::list.
608 */
609 void insert (iterator __pos, size_type __n, const _Tp& __x)
610 { _M_fill_insert(__pos, __n, __x); }
611
612 void _M_fill_insert (iterator __pos, size_type __n, const _Tp& __x);
613
614 /**
615 * @brief Removes last element from vector.
616 *
617 * This is a typical stack operation. It allows us to shrink the vector by
618 * one.
619 *
620 * Note that no data is returned and if last element's data is needed it
621 * should be retrieved before pop_back() is called.
622 */
623 void pop_back() {
624 --_M_finish;
625 _Destroy(_M_finish);
626 }
627
628 /**
629 * @brief Remove element at given position
630 * @param position Iterator pointing to element to be erased.
631 * @return Doc Me! (Iterator pointing to new element at old location?)
632 *
633 * This function will erase the element at the given position and thus
634 * shorten the vector by one.
635 *
636 * Note This operation could be expensive and if it is frequently used the
637 * user should consider using std::list. The user is also cautioned that
638 * this function only erases the element, and that if the element is itself
639 * a pointer, the pointed-to memory is not touched in any way. Managing
640 * the pointer is the user's responsibilty.
641 */
642 iterator erase(iterator __position) {
643 if (__position + 1 != end())
644 copy(__position + 1, end(), __position);
645 --_M_finish;
646 _Destroy(_M_finish);
647 return __position;
648 }
649
650 /**
651 * @brief Remove a range of elements from a vector.
652 * @param first Iterator pointing to the first element to be erased.
653 * @param last Iterator pointing to the last element to be erased.
654 * @return Doc Me! (Iterator pointing to new element at old location?)
655 *
656 * This function will erase the elements in the given range and shorten the
657 * vector accordingly.
658 *
659 * Note This operation could be expensive and if it is frequently used the
660 * user should consider using std::list. The user is also cautioned that
661 * this function only erases the elements, and that if the elements
662 * themselves are pointers, the pointed-to memory is not touched in any
663 * way. Managing the pointer is the user's responsibilty.
664 */
665 iterator erase(iterator __first, iterator __last) {
666 iterator __i(copy(__last, end(), __first));
667 _Destroy(__i, end());
668 _M_finish = _M_finish - (__last - __first);
669 return __first;
670 }
671
672 /**
673 * @brief Resizes the vector to the specified number of elements.
674 * @param new_size Number of elements the vector should contain.
675 * @param x Data with which new elements should be populated.
676 *
677 * This function will resize the vector to the specified number of
678 * elements. If the number is smaller than the vector's current size the
679 * vector is truncated, otherwise the vector is extended and new elements
680 * are populated with given data.
681 */
682 void resize(size_type __new_size, const _Tp& __x) {
683 if (__new_size < size())
684 erase(begin() + __new_size, end());
685 else
686 insert(end(), __new_size - size(), __x);
687 }
688
689 /**
690 * @brief Resizes the vector to the specified number of elements.
691 * @param new_size Number of elements the vector should contain.
692 *
693 * This function will resize the vector to the specified number of
694 * elements. If the number is smaller than the vector's current size the
695 * vector is truncated, otherwise the vector is extended and new elements
696 * are left uninitialized.
697 */
698 void resize(size_type __new_size) { resize(__new_size, _Tp()); }
699
700 /**
701 * Erases all elements in vector. Note that this function only erases the
702 * elements, and that if the elements themselves are pointers, the
703 * pointed-to memory is not touched in any way. Managing the pointer is
704 * the user's responsibilty.
705 */
706 void clear() { erase(begin(), end()); }
707
708 protected:
709
710 template <class _ForwardIterator>
711 pointer _M_allocate_and_copy(size_type __n, _ForwardIterator __first,
712 _ForwardIterator __last)
713 {
714 pointer __result = _M_allocate(__n);
715 try {
716 uninitialized_copy(__first, __last, __result);
717 return __result;
718 }
719 catch(...)
720 {
721 _M_deallocate(__result, __n);
722 __throw_exception_again;
723 }
724 }
725
726 template <class _InputIterator>
727 void _M_range_initialize(_InputIterator __first,
728 _InputIterator __last, input_iterator_tag)
729 {
730 for ( ; __first != __last; ++__first)
731 push_back(*__first);
732 }
733
734 // This function is only called by the constructor.
735 template <class _ForwardIterator>
736 void _M_range_initialize(_ForwardIterator __first,
737 _ForwardIterator __last, forward_iterator_tag)
738 {
739 size_type __n = distance(__first, __last);
740 _M_start = _M_allocate(__n);
741 _M_end_of_storage = _M_start + __n;
742 _M_finish = uninitialized_copy(__first, __last, _M_start);
743 }
744
745 template <class _InputIterator>
746 void _M_range_insert(iterator __pos,
747 _InputIterator __first, _InputIterator __last,
748 input_iterator_tag);
749
750 template <class _ForwardIterator>
751 void _M_range_insert(iterator __pos,
752 _ForwardIterator __first, _ForwardIterator __last,
753 forward_iterator_tag);
754 };
755
756 template <class _Tp, class _Alloc>
757 inline bool
758 operator==(const vector<_Tp, _Alloc>& __x, const vector<_Tp, _Alloc>& __y)
759 {
760 return __x.size() == __y.size() &&
761 equal(__x.begin(), __x.end(), __y.begin());
762 }
763
764 template <class _Tp, class _Alloc>
765 inline bool
766 operator<(const vector<_Tp, _Alloc>& __x, const vector<_Tp, _Alloc>& __y)
767 {
768 return lexicographical_compare(__x.begin(), __x.end(),
769 __y.begin(), __y.end());
770 }
771
772 template <class _Tp, class _Alloc>
773 inline void swap(vector<_Tp, _Alloc>& __x, vector<_Tp, _Alloc>& __y)
774 {
775 __x.swap(__y);
776 }
777
778 template <class _Tp, class _Alloc>
779 inline bool
780 operator!=(const vector<_Tp, _Alloc>& __x, const vector<_Tp, _Alloc>& __y) {
781 return !(__x == __y);
782 }
783
784 template <class _Tp, class _Alloc>
785 inline bool
786 operator>(const vector<_Tp, _Alloc>& __x, const vector<_Tp, _Alloc>& __y) {
787 return __y < __x;
788 }
789
790 template <class _Tp, class _Alloc>
791 inline bool
792 operator<=(const vector<_Tp, _Alloc>& __x, const vector<_Tp, _Alloc>& __y) {
793 return !(__y < __x);
794 }
795
796 template <class _Tp, class _Alloc>
797 inline bool
798 operator>=(const vector<_Tp, _Alloc>& __x, const vector<_Tp, _Alloc>& __y) {
799 return !(__x < __y);
800 }
801
802 template <class _Tp, class _Alloc>
803 vector<_Tp,_Alloc>&
804 vector<_Tp,_Alloc>::operator=(const vector<_Tp, _Alloc>& __x)
805 {
806 if (&__x != this) {
807 const size_type __xlen = __x.size();
808 if (__xlen > capacity()) {
809 pointer __tmp = _M_allocate_and_copy(__xlen, __x.begin(), __x.end());
810 _Destroy(_M_start, _M_finish);
811 _M_deallocate(_M_start, _M_end_of_storage - _M_start);
812 _M_start = __tmp;
813 _M_end_of_storage = _M_start + __xlen;
814 }
815 else if (size() >= __xlen) {
816 iterator __i(copy(__x.begin(), __x.end(), begin()));
817 _Destroy(__i, end());
818 }
819 else {
820 copy(__x.begin(), __x.begin() + size(), _M_start);
821 uninitialized_copy(__x.begin() + size(), __x.end(), _M_finish);
822 }
823 _M_finish = _M_start + __xlen;
824 }
825 return *this;
826 }
827
828 template <class _Tp, class _Alloc>
829 void vector<_Tp, _Alloc>::_M_fill_assign(size_t __n, const value_type& __val)
830 {
831 if (__n > capacity()) {
832 vector<_Tp, _Alloc> __tmp(__n, __val, get_allocator());
833 __tmp.swap(*this);
834 }
835 else if (__n > size()) {
836 fill(begin(), end(), __val);
837 _M_finish = uninitialized_fill_n(_M_finish, __n - size(), __val);
838 }
839 else
840 erase(fill_n(begin(), __n, __val), end());
841 }
842
843 template <class _Tp, class _Alloc> template <class _InputIter>
844 void vector<_Tp, _Alloc>::_M_assign_aux(_InputIter __first, _InputIter __last,
845 input_iterator_tag) {
846 iterator __cur(begin());
847 for ( ; __first != __last && __cur != end(); ++__cur, ++__first)
848 *__cur = *__first;
849 if (__first == __last)
850 erase(__cur, end());
851 else
852 insert(end(), __first, __last);
853 }
854
855 template <class _Tp, class _Alloc> template <class _ForwardIter>
856 void
857 vector<_Tp, _Alloc>::_M_assign_aux(_ForwardIter __first, _ForwardIter __last,
858 forward_iterator_tag) {
859 size_type __len = distance(__first, __last);
860
861 if (__len > capacity()) {
862 pointer __tmp(_M_allocate_and_copy(__len, __first, __last));
863 _Destroy(_M_start, _M_finish);
864 _M_deallocate(_M_start, _M_end_of_storage - _M_start);
865 _M_start = __tmp;
866 _M_end_of_storage = _M_finish = _M_start + __len;
867 }
868 else if (size() >= __len) {
869 iterator __new_finish(copy(__first, __last, _M_start));
870 _Destroy(__new_finish, end());
871 _M_finish = __new_finish.base();
872 }
873 else {
874 _ForwardIter __mid = __first;
875 advance(__mid, size());
876 copy(__first, __mid, _M_start);
877 _M_finish = uninitialized_copy(__mid, __last, _M_finish);
878 }
879 }
880
881 template <class _Tp, class _Alloc>
882 void
883 vector<_Tp, _Alloc>::_M_insert_aux(iterator __position, const _Tp& __x)
884 {
885 if (_M_finish != _M_end_of_storage) {
886 _Construct(_M_finish, *(_M_finish - 1));
887 ++_M_finish;
888 _Tp __x_copy = __x;
889 copy_backward(__position, iterator(_M_finish - 2), iterator(_M_finish- 1));
890 *__position = __x_copy;
891 }
892 else {
893 const size_type __old_size = size();
894 const size_type __len = __old_size != 0 ? 2 * __old_size : 1;
895 iterator __new_start(_M_allocate(__len));
896 iterator __new_finish(__new_start);
897 try {
898 __new_finish = uninitialized_copy(iterator(_M_start), __position,
899 __new_start);
900 _Construct(__new_finish.base(), __x);
901 ++__new_finish;
902 __new_finish = uninitialized_copy(__position, iterator(_M_finish),
903 __new_finish);
904 }
905 catch(...)
906 {
907 _Destroy(__new_start,__new_finish);
908 _M_deallocate(__new_start.base(),__len);
909 __throw_exception_again;
910 }
911 _Destroy(begin(), end());
912 _M_deallocate(_M_start, _M_end_of_storage - _M_start);
913 _M_start = __new_start.base();
914 _M_finish = __new_finish.base();
915 _M_end_of_storage = __new_start.base() + __len;
916 }
917 }
918
919 template <class _Tp, class _Alloc>
920 void
921 vector<_Tp, _Alloc>::_M_insert_aux(iterator __position)
922 {
923 if (_M_finish != _M_end_of_storage) {
924 _Construct(_M_finish, *(_M_finish - 1));
925 ++_M_finish;
926 copy_backward(__position, iterator(_M_finish - 2),
927 iterator(_M_finish - 1));
928 *__position = _Tp();
929 }
930 else {
931 const size_type __old_size = size();
932 const size_type __len = __old_size != 0 ? 2 * __old_size : 1;
933 pointer __new_start = _M_allocate(__len);
934 pointer __new_finish = __new_start;
935 try {
936 __new_finish = uninitialized_copy(iterator(_M_start), __position,
937 __new_start);
938 _Construct(__new_finish);
939 ++__new_finish;
940 __new_finish = uninitialized_copy(__position, iterator(_M_finish),
941 __new_finish);
942 }
943 catch(...)
944 {
945 _Destroy(__new_start,__new_finish);
946 _M_deallocate(__new_start,__len);
947 __throw_exception_again;
948 }
949 _Destroy(begin(), end());
950 _M_deallocate(_M_start, _M_end_of_storage - _M_start);
951 _M_start = __new_start;
952 _M_finish = __new_finish;
953 _M_end_of_storage = __new_start + __len;
954 }
955 }
956
957 template <class _Tp, class _Alloc>
958 void vector<_Tp, _Alloc>::_M_fill_insert(iterator __position, size_type __n,
959 const _Tp& __x)
960 {
961 if (__n != 0) {
962 if (size_type(_M_end_of_storage - _M_finish) >= __n) {
963 _Tp __x_copy = __x;
964 const size_type __elems_after = end() - __position;
965 iterator __old_finish(_M_finish);
966 if (__elems_after > __n) {
967 uninitialized_copy(_M_finish - __n, _M_finish, _M_finish);
968 _M_finish += __n;
969 copy_backward(__position, __old_finish - __n, __old_finish);
970 fill(__position, __position + __n, __x_copy);
971 }
972 else {
973 uninitialized_fill_n(_M_finish, __n - __elems_after, __x_copy);
974 _M_finish += __n - __elems_after;
975 uninitialized_copy(__position, __old_finish, _M_finish);
976 _M_finish += __elems_after;
977 fill(__position, __old_finish, __x_copy);
978 }
979 }
980 else {
981 const size_type __old_size = size();
982 const size_type __len = __old_size + max(__old_size, __n);
983 iterator __new_start(_M_allocate(__len));
984 iterator __new_finish(__new_start);
985 try {
986 __new_finish = uninitialized_copy(begin(), __position, __new_start);
987 __new_finish = uninitialized_fill_n(__new_finish, __n, __x);
988 __new_finish
989 = uninitialized_copy(__position, end(), __new_finish);
990 }
991 catch(...)
992 {
993 _Destroy(__new_start,__new_finish);
994 _M_deallocate(__new_start.base(),__len);
995 __throw_exception_again;
996 }
997 _Destroy(_M_start, _M_finish);
998 _M_deallocate(_M_start, _M_end_of_storage - _M_start);
999 _M_start = __new_start.base();
1000 _M_finish = __new_finish.base();
1001 _M_end_of_storage = __new_start.base() + __len;
1002 }
1003 }
1004 }
1005
1006 template <class _Tp, class _Alloc> template <class _InputIterator>
1007 void
1008 vector<_Tp, _Alloc>::_M_range_insert(iterator __pos,
1009 _InputIterator __first,
1010 _InputIterator __last,
1011 input_iterator_tag)
1012 {
1013 for ( ; __first != __last; ++__first) {
1014 __pos = insert(__pos, *__first);
1015 ++__pos;
1016 }
1017 }
1018
1019 template <class _Tp, class _Alloc> template <class _ForwardIterator>
1020 void
1021 vector<_Tp, _Alloc>::_M_range_insert(iterator __position,
1022 _ForwardIterator __first,
1023 _ForwardIterator __last,
1024 forward_iterator_tag)
1025 {
1026 if (__first != __last) {
1027 size_type __n = distance(__first, __last);
1028 if (size_type(_M_end_of_storage - _M_finish) >= __n) {
1029 const size_type __elems_after = end() - __position;
1030 iterator __old_finish(_M_finish);
1031 if (__elems_after > __n) {
1032 uninitialized_copy(_M_finish - __n, _M_finish, _M_finish);
1033 _M_finish += __n;
1034 copy_backward(__position, __old_finish - __n, __old_finish);
1035 copy(__first, __last, __position);
1036 }
1037 else {
1038 _ForwardIterator __mid = __first;
1039 advance(__mid, __elems_after);
1040 uninitialized_copy(__mid, __last, _M_finish);
1041 _M_finish += __n - __elems_after;
1042 uninitialized_copy(__position, __old_finish, _M_finish);
1043 _M_finish += __elems_after;
1044 copy(__first, __mid, __position);
1045 }
1046 }
1047 else {
1048 const size_type __old_size = size();
1049 const size_type __len = __old_size + max(__old_size, __n);
1050 iterator __new_start(_M_allocate(__len));
1051 iterator __new_finish(__new_start);
1052 try {
1053 __new_finish = uninitialized_copy(iterator(_M_start),
1054 __position, __new_start);
1055 __new_finish = uninitialized_copy(__first, __last, __new_finish);
1056 __new_finish
1057 = uninitialized_copy(__position, iterator(_M_finish), __new_finish);
1058 }
1059 catch(...)
1060 {
1061 _Destroy(__new_start,__new_finish);
1062 _M_deallocate(__new_start.base(), __len);
1063 __throw_exception_again;
1064 }
1065 _Destroy(_M_start, _M_finish);
1066 _M_deallocate(_M_start, _M_end_of_storage - _M_start);
1067 _M_start = __new_start.base();
1068 _M_finish = __new_finish.base();
1069 _M_end_of_storage = __new_start.base() + __len;
1070 }
1071 }
1072 }
1073
1074 } // namespace std
1075
1076 #endif /* __GLIBCPP_INTERNAL_VECTOR_H */
1077
1078 // Local Variables:
1079 // mode:C++
1080 // End: