stl_deque.h, [...]: Reformat to (mostly) match C++STYLE.
[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 // Since this entire file is within namespace std, there's no reason to
69 // waste two spaces along the left column. Thus the leading indentation is
70 // slightly violated from here on.
71 namespace std
72 {
73
74 /// @if maint Primary default version. @endif
75 /**
76 * @if maint
77 * See bits/stl_deque.h's _Deque_alloc_base for an explanation.
78 * @endif
79 */
80 template <class _Tp, class _Allocator, bool _IsStatic>
81 class _Vector_alloc_base
82 {
83 public:
84 typedef typename _Alloc_traits<_Tp, _Allocator>::allocator_type
85 allocator_type;
86
87 allocator_type
88 get_allocator() const { return _M_data_allocator; }
89
90 _Vector_alloc_base(const allocator_type& __a)
91 : _M_data_allocator(__a), _M_start(0), _M_finish(0), _M_end_of_storage(0)
92 {}
93
94 protected:
95 allocator_type _M_data_allocator;
96 _Tp* _M_start;
97 _Tp* _M_finish;
98 _Tp* _M_end_of_storage;
99
100 _Tp*
101 _M_allocate(size_t __n) { return _M_data_allocator.allocate(__n); }
102
103 void
104 _M_deallocate(_Tp* __p, size_t __n)
105 { if (__p) _M_data_allocator.deallocate(__p, __n); }
106 };
107
108 /// @if maint Specialization for instanceless allocators. @endif
109 template <class _Tp, class _Allocator>
110 class _Vector_alloc_base<_Tp, _Allocator, true>
111 {
112 public:
113 typedef typename _Alloc_traits<_Tp, _Allocator>::allocator_type
114 allocator_type;
115
116 allocator_type
117 get_allocator() const { return allocator_type(); }
118
119 _Vector_alloc_base(const allocator_type&)
120 : _M_start(0), _M_finish(0), _M_end_of_storage(0)
121 {}
122
123 protected:
124 _Tp* _M_start;
125 _Tp* _M_finish;
126 _Tp* _M_end_of_storage;
127
128 typedef typename _Alloc_traits<_Tp, _Allocator>::_Alloc_type _Alloc_type;
129
130 _Tp*
131 _M_allocate(size_t __n) { return _Alloc_type::allocate(__n); }
132
133 void
134 _M_deallocate(_Tp* __p, size_t __n) { _Alloc_type::deallocate(__p, __n);}
135 };
136
137
138 /**
139 * @if maint
140 * See bits/stl_deque.h's _Deque_base for an explanation.
141 * @endif
142 */
143 template <class _Tp, class _Alloc>
144 struct _Vector_base
145 : public _Vector_alloc_base<_Tp, _Alloc,
146 _Alloc_traits<_Tp, _Alloc>::_S_instanceless>
147 {
148 public:
149 typedef _Vector_alloc_base<_Tp, _Alloc,
150 _Alloc_traits<_Tp, _Alloc>::_S_instanceless>
151 _Base;
152 typedef typename _Base::allocator_type allocator_type;
153
154 _Vector_base(const allocator_type& __a)
155 : _Base(__a) {}
156 _Vector_base(size_t __n, const allocator_type& __a)
157 : _Base(__a)
158 {
159 _M_start = _M_allocate(__n);
160 _M_finish = _M_start;
161 _M_end_of_storage = _M_start + __n;
162 }
163
164 ~_Vector_base() { _M_deallocate(_M_start, _M_end_of_storage - _M_start); }
165 };
166
167
168 /**
169 * @brief A standard container which offers fixed time access to individual
170 * elements in any order.
171 *
172 * @ingroup Containers
173 * @ingroup Sequences
174 *
175 * Meets the requirements of a <a href="tables.html#65">container</a>, a
176 * <a href="tables.html#66">reversible container</a>, and a
177 * <a href="tables.html#67">sequence</a>, including the
178 * <a href="tables.html#68">optional sequence requirements</a> with the
179 * %exception of @c push_front and @c pop_front.
180 *
181 * In some terminology a %vector can be described as a dynamic C-style array,
182 * it offers fast and efficient access to individual elements in any order
183 * and saves the user from worrying about memory and size allocation.
184 * Subscripting ( @c [] ) access is also provided as with C-style arrays.
185 */
186 template <class _Tp, class _Alloc = allocator<_Tp> >
187 class vector : protected _Vector_base<_Tp, _Alloc>
188 {
189 // concept requirements
190 __glibcpp_class_requires(_Tp, _SGIAssignableConcept)
191
192 typedef _Vector_base<_Tp, _Alloc> _Base;
193 typedef vector<_Tp, _Alloc> vector_type;
194
195 public:
196 typedef _Tp value_type;
197 typedef value_type* pointer;
198 typedef const value_type* const_pointer;
199 typedef __gnu_cxx::__normal_iterator<pointer, vector_type> iterator;
200 typedef __gnu_cxx::__normal_iterator<const_pointer, vector_type>
201 const_iterator;
202 typedef reverse_iterator<const_iterator> const_reverse_iterator;
203 typedef reverse_iterator<iterator> reverse_iterator;
204 typedef value_type& reference;
205 typedef const value_type& const_reference;
206 typedef size_t size_type;
207 typedef ptrdiff_t difference_type;
208 typedef typename _Base::allocator_type allocator_type;
209
210 protected:
211 /** @if maint
212 * These two functions and three data members are all from the top-most
213 * base class, which varies depending on the type of %allocator. They
214 * should be pretty self-explanatory, as %vector uses a simple contiguous
215 * allocation scheme.
216 * @endif
217 */
218 using _Base::_M_allocate;
219 using _Base::_M_deallocate;
220 using _Base::_M_start;
221 using _Base::_M_finish;
222 using _Base::_M_end_of_storage;
223
224 public:
225 // [23.2.4.1] construct/copy/destroy
226 // (assign() and get_allocator() are also listed in this section)
227 /**
228 * @brief Default constructor creates no elements.
229 */
230 explicit
231 vector(const allocator_type& __a = allocator_type())
232 : _Base(__a) {}
233
234 /**
235 * @brief Create a %vector with copies of an exemplar element.
236 * @param n The number of elements to initially create.
237 * @param value An element to copy.
238 *
239 * This constructor fills the %vector with @a n copies of @a value.
240 */
241 vector(size_type __n, const value_type& __value,
242 const allocator_type& __a = allocator_type())
243 : _Base(__n, __a)
244 { _M_finish = uninitialized_fill_n(_M_start, __n, __value); }
245
246 /**
247 * @brief Create a %vector with default elements.
248 * @param n The number of elements to initially create.
249 *
250 * This constructor fills the %vector with @a n copies of a
251 * default-constructed element.
252 */
253 explicit
254 vector(size_type __n)
255 : _Base(__n, allocator_type())
256 { _M_finish = uninitialized_fill_n(_M_start, __n, _Tp()); }
257
258 /**
259 * @brief %Vector copy constructor.
260 * @param x A %vector of identical element and allocator types.
261 *
262 * The newly-created %vector uses a copy of the allocation object used
263 * by @a x. All the elements of @a x are copied, but any extra memory in
264 * @a x (for fast expansion) will not be copied.
265 */
266 vector(const vector& __x)
267 : _Base(__x.size(), __x.get_allocator())
268 { _M_finish = uninitialized_copy(__x.begin(), __x.end(), _M_start); }
269
270 /**
271 * @brief Builds a %vector from a range.
272 * @param first An input iterator.
273 * @param last An input iterator.
274 *
275 * Creats a %vector consisting of copies of the elements from [first,last).
276 *
277 * If the iterators are forward, bidirectional, or random-access, then
278 * this will call the elements' copy constructor N times (where N is
279 * distance(first,last)) and do no memory reallocation. But if only
280 * input iterators are used, then this will do at most 2N calls to the
281 * copy constructor, and logN memory reallocations.
282 */
283 template <class _InputIterator>
284 vector(_InputIterator __first, _InputIterator __last,
285 const allocator_type& __a = allocator_type())
286 : _Base(__a)
287 {
288 // Check whether it's an integral type. If so, it's not an iterator.
289 typedef typename _Is_integer<_InputIterator>::_Integral _Integral;
290 _M_initialize_dispatch(__first, __last, _Integral());
291 }
292
293 /**
294 * The dtor only erases the elements, and note that if the elements
295 * themselves are pointers, the pointed-to memory is not touched in any
296 * way. Managing the pointer is the user's responsibilty.
297 */
298 ~vector() { _Destroy(_M_start, _M_finish); }
299
300 /**
301 * @brief %Vector assignment operator.
302 * @param x A %vector of identical element and allocator types.
303 *
304 * All the elements of @a x are copied, but any extra memory in @a x (for
305 * fast expansion) will not be copied. Unlike the copy constructor, the
306 * allocator object is not copied.
307 */
308 vector&
309 operator=(const vector& __x);
310
311 /**
312 * @brief Assigns a given value to a %vector.
313 * @param n Number of elements to be assigned.
314 * @param val Value to be assigned.
315 *
316 * This function fills a %vector with @a n copies of the given value.
317 * Note that the assignment completely changes the %vector and that the
318 * resulting %vector's size is the same as the number of elements assigned.
319 * Old data may be lost.
320 */
321 void
322 assign(size_type __n, const value_type& __val) { _M_fill_assign(__n, __val); }
323
324 /**
325 * @brief Assigns a range to a %vector.
326 * @param first An input iterator.
327 * @param last An input iterator.
328 *
329 * This function fills a %vector with copies of the elements in the
330 * range [first,last).
331 *
332 * Note that the assignment completely changes the %vector and that the
333 * resulting %vector's size is the same as the number of elements assigned.
334 * Old data may be lost.
335 */
336 template<class _InputIterator>
337 void
338 assign(_InputIterator __first, _InputIterator __last)
339 {
340 // Check whether it's an integral type. If so, it's not an iterator.
341 typedef typename _Is_integer<_InputIterator>::_Integral _Integral;
342 _M_assign_dispatch(__first, __last, _Integral());
343 }
344
345 /// Get a copy of the memory allocation object.
346 allocator_type
347 get_allocator() const { return _Base::get_allocator(); }
348
349 // iterators
350 /**
351 * Returns a read/write iterator that points to the first element in the
352 * %vector. Iteration is done in ordinary element order.
353 */
354 iterator
355 begin() { return iterator (_M_start); }
356
357 /**
358 * Returns a read-only (constant) iterator that points to the first element
359 * in the %vector. Iteration is done in ordinary element order.
360 */
361 const_iterator
362 begin() const { return const_iterator (_M_start); }
363
364 /**
365 * Returns a read/write iterator that points one past the last element in
366 * the %vector. Iteration is done in ordinary element order.
367 */
368 iterator
369 end() { return iterator (_M_finish); }
370
371 /**
372 * Returns a read-only (constant) iterator that points one past the last
373 * element in the %vector. Iteration is done in ordinary element order.
374 */
375 const_iterator
376 end() const { return const_iterator (_M_finish); }
377
378 /**
379 * Returns a read/write reverse iterator that points to the last element in
380 * the %vector. Iteration is done in reverse element order.
381 */
382 reverse_iterator
383 rbegin() { return reverse_iterator(end()); }
384
385 /**
386 * Returns a read-only (constant) reverse iterator that points to the last
387 * element in the %vector. Iteration is done in reverse element order.
388 */
389 const_reverse_iterator
390 rbegin() const { return const_reverse_iterator(end()); }
391
392 /**
393 * Returns a read/write reverse iterator that points to one before the
394 * first element in the %vector. Iteration is done in reverse element
395 * order.
396 */
397 reverse_iterator
398 rend() { return reverse_iterator(begin()); }
399
400 /**
401 * Returns a read-only (constant) reverse iterator that points to one
402 * before the first element in the %vector. Iteration is done in reverse
403 * element order.
404 */
405 const_reverse_iterator
406 rend() const { return const_reverse_iterator(begin()); }
407
408 // [23.2.4.2] capacity
409 /** Returns the number of elements in the %vector. */
410 size_type
411 size() const { return size_type(end() - begin()); }
412
413 /** Returns the size() of the largest possible %vector. */
414 size_type
415 max_size() const { return size_type(-1) / sizeof(value_type); }
416
417 /**
418 * @brief Resizes the %vector to the specified number of elements.
419 * @param new_size Number of elements the %vector should contain.
420 * @param x Data with which new elements should be populated.
421 *
422 * This function will %resize the %vector to the specified number of
423 * elements. If the number is smaller than the %vector's current size the
424 * %vector is truncated, otherwise the %vector is extended and new elements
425 * are populated with given data.
426 */
427 void
428 resize(size_type __new_size, const value_type& __x)
429 {
430 if (__new_size < size())
431 erase(begin() + __new_size, end());
432 else
433 insert(end(), __new_size - size(), __x);
434 }
435
436 /**
437 * @brief Resizes the %vector to the specified number of elements.
438 * @param new_size Number of elements the %vector should contain.
439 *
440 * This function will resize the %vector to the specified number of
441 * elements. If the number is smaller than the %vector's current size the
442 * %vector is truncated, otherwise the %vector is extended and new elements
443 * are default-constructed.
444 */
445 void
446 resize(size_type __new_size) { resize(__new_size, value_type()); }
447
448 /**
449 * Returns the total number of elements that the %vector can hold before
450 * needing to allocate more memory.
451 */
452 size_type
453 capacity() const
454 { return size_type(const_iterator(_M_end_of_storage) - begin()); }
455
456 /**
457 * Returns true if the %vector is empty. (Thus begin() would equal end().)
458 */
459 bool
460 empty() const { return begin() == end(); }
461
462 /**
463 * @brief Attempt to preallocate enough memory for specified number of
464 * elements.
465 * @param n Number of elements required.
466 * @throw std::length_error If @a n exceeds @c max_size().
467 *
468 * This function attempts to reserve enough memory for the %vector to hold
469 * the specified number of elements. If the number requested is more than
470 * max_size(), length_error is thrown.
471 *
472 * The advantage of this function is that if optimal code is a necessity
473 * and the user can determine the number of elements that will be required,
474 * the user can reserve the memory in %advance, and thus prevent a possible
475 * reallocation of memory and copying of %vector data.
476 */
477 void
478 reserve(size_type __n) // FIXME should be out of class
479 {
480 if (capacity() < __n)
481 {
482 const size_type __old_size = size();
483 pointer __tmp = _M_allocate_and_copy(__n, _M_start, _M_finish);
484 _Destroy(_M_start, _M_finish);
485 _M_deallocate(_M_start, _M_end_of_storage - _M_start);
486 _M_start = __tmp;
487 _M_finish = __tmp + __old_size;
488 _M_end_of_storage = _M_start + __n;
489 }
490 }
491
492 // element access
493 /**
494 * @brief Subscript access to the data contained in the %vector.
495 * @param n The index of the element for which data should be accessed.
496 * @return Read/write reference to data.
497 *
498 * This operator allows for easy, array-style, data access.
499 * Note that data access with this operator is unchecked and out_of_range
500 * lookups are not defined. (For checked lookups see at().)
501 */
502 reference
503 operator[](size_type __n) { return *(begin() + __n); }
504 // XXX do we need to convert to normal_iterator first?
505
506 /**
507 * @brief Subscript access to the data contained in the %vector.
508 * @param n The index of the element for which data should be accessed.
509 * @return Read-only (constant) reference to data.
510 *
511 * This operator allows for easy, array-style, data access.
512 * Note that data access with this operator is unchecked and out_of_range
513 * lookups are not defined. (For checked lookups see at().)
514 */
515 const_reference
516 operator[](size_type __n) const { return *(begin() + __n); }
517
518 protected:
519 /// @if maint Safety check used only from at(). @endif
520 void
521 _M_range_check(size_type __n) const
522 {
523 if (__n >= this->size())
524 __throw_out_of_range("vector [] access out of range");
525 }
526
527 public:
528 /**
529 * @brief Provides access to the data contained in the %vector.
530 * @param n The index of the element for which data should be accessed.
531 * @return Read/write reference to data.
532 * @throw std::out_of_range If @a n is an invalid index.
533 *
534 * This function provides for safer data access. The parameter is first
535 * checked that it is in the range of the vector. The function throws
536 * out_of_range if the check fails.
537 */
538 reference
539 at(size_type __n) { _M_range_check(__n); return (*this)[__n]; }
540
541 /**
542 * @brief Provides access to the data contained in the %vector.
543 * @param n The index of the element for which data should be accessed.
544 * @return Read-only (constant) reference to data.
545 * @throw std::out_of_range If @a n is an invalid index.
546 *
547 * This function provides for safer data access. The parameter is first
548 * checked that it is in the range of the vector. The function throws
549 * out_of_range if the check fails.
550 */
551 const_reference
552 at(size_type __n) const { _M_range_check(__n); return (*this)[__n]; }
553
554 /**
555 * Returns a read/write reference to the data at the first element of the
556 * %vector.
557 */
558 reference
559 front() { return *begin(); }
560 // XXX do we need to convert to normal_iterator first?
561
562 /**
563 * Returns a read-only (constant) reference to the data at the first
564 * element of the %vector.
565 */
566 const_reference
567 front() const { return *begin(); }
568
569 /**
570 * Returns a read/write reference to the data at the last element of the
571 * %vector.
572 */
573 reference
574 back() { return *(end() - 1); }
575
576 /**
577 * Returns a read-only (constant) reference to the data at the last
578 * element of the %vector.
579 */
580 const_reference
581 back() const { return *(end() - 1); }
582
583 // [23.2.4.3] modifiers
584 /**
585 * @brief Add data to the end of the %vector.
586 * @param x Data to be added.
587 *
588 * This is a typical stack operation. The function creates an element at
589 * the end of the %vector and assigns the given data to it.
590 * Due to the nature of a %vector this operation can be done in constant
591 * time if the %vector has preallocated space available.
592 */
593 void
594 push_back(const value_type& __x)
595 {
596 if (_M_finish != _M_end_of_storage) {
597 _Construct(_M_finish, __x);
598 ++_M_finish;
599 }
600 else
601 _M_insert_aux(end(), __x);
602 }
603
604 /**
605 * @brief Removes last element.
606 *
607 * This is a typical stack operation. It shrinks the %vector by one.
608 *
609 * Note that no data is returned, and if the last element's data is
610 * needed, it should be retrieved before pop_back() is called.
611 */
612 void
613 pop_back()
614 {
615 --_M_finish;
616 _Destroy(_M_finish);
617 }
618
619 /**
620 * @brief Inserts given value into %vector before specified iterator.
621 * @param position An iterator into the %vector.
622 * @param x Data to be inserted.
623 * @return An iterator that points to the inserted data.
624 *
625 * This function will insert a copy of the given value before the specified
626 * location.
627 * Note that this kind of operation could be expensive for a %vector and if
628 * it is frequently used the user should consider using std::list.
629 */
630 iterator
631 insert(iterator __position, const value_type& __x)
632 {
633 size_type __n = __position - begin();
634 if (_M_finish != _M_end_of_storage && __position == end())
635 {
636 _Construct(_M_finish, __x);
637 ++_M_finish;
638 }
639 else
640 _M_insert_aux(__position, __x);
641 return begin() + __n;
642 }
643
644 #ifdef _GLIBCPP_DEPRECATED
645 /**
646 * @brief Inserts an element into the %vector.
647 * @param position An iterator into the %vector.
648 * @return An iterator that points to the inserted element.
649 *
650 * This function will insert a default-constructed element before the
651 * specified location. You should consider using
652 * insert(position,value_type()) instead.
653 * Note that this kind of operation could be expensive for a vector and if
654 * it is frequently used the user should consider using std::list.
655 *
656 * @note This was deprecated in 3.2 and will be removed in 3.3. You must
657 * define @c _GLIBCPP_DEPRECATED to make this visible in 3.2; see
658 * c++config.h.
659 */
660 iterator
661 insert(iterator __position)
662 { return insert(__position, value_type()); }
663 #endif
664
665 /**
666 * @brief Inserts a number of copies of given data into the %vector.
667 * @param position An iterator into the %vector.
668 * @param n Number of elements to be inserted.
669 * @param x Data to be inserted.
670 *
671 * This function will insert a specified number of copies of the given data
672 * before the location specified by @a position.
673 *
674 * Note that this kind of operation could be expensive for a %vector and if
675 * it is frequently used the user should consider using std::list.
676 */
677 void
678 insert (iterator __pos, size_type __n, const value_type& __x)
679 { _M_fill_insert(__pos, __n, __x); }
680
681 /**
682 * @brief Inserts a range into the %vector.
683 * @param pos An iterator into the %vector.
684 * @param first An input iterator.
685 * @param last An input iterator.
686 *
687 * This function will insert copies of the data in the range [first,last)
688 * into the %vector before the location specified by @a pos.
689 *
690 * Note that this kind of operation could be expensive for a %vector and if
691 * it is frequently used the user should consider using std::list.
692 */
693 template<class _InputIterator>
694 void
695 insert(iterator __pos, _InputIterator __first, _InputIterator __last)
696 {
697 // Check whether it's an integral type. If so, it's not an iterator.
698 typedef typename _Is_integer<_InputIterator>::_Integral _Integral;
699 _M_insert_dispatch(__pos, __first, __last, _Integral());
700 }
701
702 /**
703 * @brief Remove element at given position.
704 * @param position Iterator pointing to element to be erased.
705 * @return An iterator pointing to the next element (or end()).
706 *
707 * This function will erase the element at the given position and thus
708 * shorten the %vector by one.
709 *
710 * Note This operation could be expensive and if it is frequently used the
711 * user should consider using std::list. The user is also cautioned that
712 * this function only erases the element, and that if the element is itself
713 * a pointer, the pointed-to memory is not touched in any way. Managing
714 * the pointer is the user's responsibilty.
715 */
716 iterator
717 erase(iterator __position)
718 {
719 if (__position + 1 != end())
720 copy(__position + 1, end(), __position);
721 --_M_finish;
722 _Destroy(_M_finish);
723 return __position;
724 }
725
726 /**
727 * @brief Remove a range of elements.
728 * @param first Iterator pointing to the first element to be erased.
729 * @param last Iterator pointing to one past the last element to be erased.
730 * @return An iterator pointing to the element pointed to by @a last
731 * prior to erasing (or end()).
732 *
733 * This function will erase the elements in the range [first,last) and
734 * shorten the %vector accordingly.
735 *
736 * Note This operation could be expensive and if it is frequently used the
737 * user should consider using std::list. The user is also cautioned that
738 * this function only erases the elements, and that if the elements
739 * themselves are pointers, the pointed-to memory is not touched in any
740 * way. Managing the pointer is the user's responsibilty.
741 */
742 iterator
743 erase(iterator __first, iterator __last)
744 {
745 iterator __i(copy(__last, end(), __first));
746 _Destroy(__i, end());
747 _M_finish = _M_finish - (__last - __first);
748 return __first;
749 }
750
751 /**
752 * @brief Swaps data with another %vector.
753 * @param x A %vector of the same element and allocator types.
754 *
755 * This exchanges the elements between two vectors in constant time.
756 * (Three pointers, so it should be quite fast.)
757 * Note that the global std::swap() function is specialized such that
758 * std::swap(v1,v2) will feed to this function.
759 */
760 void
761 swap(vector& __x)
762 {
763 std::swap(_M_start, __x._M_start);
764 std::swap(_M_finish, __x._M_finish);
765 std::swap(_M_end_of_storage, __x._M_end_of_storage);
766 }
767
768 /**
769 * Erases all the elements. Note that this function only erases the
770 * elements, and that if the elements themselves are pointers, the
771 * pointed-to memory is not touched in any way. Managing the pointer is
772 * the user's responsibilty.
773 */
774 void
775 clear() { erase(begin(), end()); }
776
777 protected:
778 /**
779 * @if maint
780 * Memory expansion handler. Uses the member allocation function to
781 * obtain @a n bytes of memory, and then copies [first,last) into it.
782 * @endif
783 */
784 template <class _ForwardIterator>
785 pointer
786 _M_allocate_and_copy(size_type __n,
787 _ForwardIterator __first, _ForwardIterator __last)
788 {
789 pointer __result = _M_allocate(__n);
790 try
791 {
792 uninitialized_copy(__first, __last, __result);
793 return __result;
794 }
795 catch(...)
796 {
797 _M_deallocate(__result, __n);
798 __throw_exception_again;
799 }
800 }
801
802
803 // Internal constructor functions follow.
804
805 // called by the range constructor to implement [23.1.1]/9
806 template<class _Integer>
807 void
808 _M_initialize_dispatch(_Integer __n, _Integer __value, __true_type)
809 {
810 _M_start = _M_allocate(__n);
811 _M_end_of_storage = _M_start + __n;
812 _M_finish = uninitialized_fill_n(_M_start, __n, __value);
813 }
814
815 // called by the range constructor to implement [23.1.1]/9
816 template<class _InputIter>
817 void
818 _M_initialize_dispatch(_InputIter __first, _InputIter __last, __false_type)
819 {
820 typedef typename iterator_traits<_InputIter>::iterator_category
821 _IterCategory;
822 _M_range_initialize(__first, __last, _IterCategory());
823 }
824
825 // called by the second initialize_dispatch above
826 template <class _InputIterator>
827 void
828 _M_range_initialize(_InputIterator __first,
829 _InputIterator __last, input_iterator_tag)
830 {
831 for ( ; __first != __last; ++__first)
832 push_back(*__first);
833 }
834
835 // called by the second initialize_dispatch above
836 template <class _ForwardIterator>
837 void _M_range_initialize(_ForwardIterator __first,
838 _ForwardIterator __last, forward_iterator_tag)
839 {
840 size_type __n = distance(__first, __last);
841 _M_start = _M_allocate(__n);
842 _M_end_of_storage = _M_start + __n;
843 _M_finish = uninitialized_copy(__first, __last, _M_start);
844 }
845
846
847 // Internal assign functions follow. The *_aux functions do the actual
848 // assignment work for the range versions.
849
850 // called by the range assign to implement [23.1.1]/9
851 template<class _Integer>
852 void
853 _M_assign_dispatch(_Integer __n, _Integer __val, __true_type)
854 {
855 _M_fill_assign(static_cast<size_type>(__n),
856 static_cast<value_type>(__val));
857 }
858
859 // called by the range assign to implement [23.1.1]/9
860 template<class _InputIter>
861 void
862 _M_assign_dispatch(_InputIter __first, _InputIter __last, __false_type)
863 {
864 typedef typename iterator_traits<_InputIter>::iterator_category
865 _IterCategory;
866 _M_assign_aux(__first, __last, _IterCategory());
867 }
868
869 // called by the second assign_dispatch above
870 template <class _InputIterator>
871 void
872 _M_assign_aux(_InputIterator __first, _InputIterator __last,
873 input_iterator_tag);
874
875 // called by the second assign_dispatch above
876 template <class _ForwardIterator>
877 void
878 _M_assign_aux(_ForwardIterator __first, _ForwardIterator __last,
879 forward_iterator_tag);
880
881 // Called by assign(n,t), and the range assign when it turns out to be the
882 // same thing.
883 void
884 _M_fill_assign(size_type __n, const value_type& __val);
885
886
887 // Internal insert functions follow.
888
889 // called by the range insert to implement [23.1.1]/9
890 template<class _Integer>
891 void
892 _M_insert_dispatch(iterator __pos, _Integer __n, _Integer __val,
893 __true_type)
894 {
895 _M_fill_insert(__pos, static_cast<size_type>(__n),
896 static_cast<value_type>(__val));
897 }
898
899 // called by the range insert to implement [23.1.1]/9
900 template<class _InputIterator>
901 void
902 _M_insert_dispatch(iterator __pos, _InputIterator __first,
903 _InputIterator __last, __false_type)
904 {
905 typedef typename iterator_traits<_InputIterator>::iterator_category
906 _IterCategory;
907 _M_range_insert(__pos, __first, __last, _IterCategory());
908 }
909
910 // called by the second insert_dispatch above
911 template <class _InputIterator>
912 void
913 _M_range_insert(iterator __pos,
914 _InputIterator __first, _InputIterator __last,
915 input_iterator_tag);
916
917 // called by the second insert_dispatch above
918 template <class _ForwardIterator>
919 void
920 _M_range_insert(iterator __pos,
921 _ForwardIterator __first, _ForwardIterator __last,
922 forward_iterator_tag);
923
924 // Called by insert(p,n,x), and the range insert when it turns out to be
925 // the same thing.
926 void
927 _M_fill_insert (iterator __pos, size_type __n, const value_type& __x);
928
929 // called by insert(p,x)
930 void
931 _M_insert_aux(iterator __position, const value_type& __x);
932
933 #ifdef _GLIBCPP_DEPRECATED
934 // unused now (same situation as in deque)
935 void _M_insert_aux(iterator __position);
936 #endif
937 };
938
939
940 /**
941 * @brief Vector equality comparison.
942 * @param x A %vector.
943 * @param y A %vector of the same type as @a x.
944 * @return True iff the size and elements of the vectors are equal.
945 *
946 * This is an equivalence relation. It is linear in the size of the
947 * vectors. Vectors are considered equivalent if their sizes are equal,
948 * and if corresponding elements compare equal.
949 */
950 template <class _Tp, class _Alloc>
951 inline bool
952 operator==(const vector<_Tp, _Alloc>& __x, const vector<_Tp, _Alloc>& __y)
953 {
954 return __x.size() == __y.size() &&
955 equal(__x.begin(), __x.end(), __y.begin());
956 }
957
958 /**
959 * @brief Vector ordering relation.
960 * @param x A %vector.
961 * @param y A %vector of the same type as @a x.
962 * @return True iff @a x is lexographically less than @a y.
963 *
964 * This is a total ordering relation. It is linear in the size of the
965 * vectors. The elements must be comparable with @c <.
966 *
967 * See std::lexographical_compare() for how the determination is made.
968 */
969 template <class _Tp, class _Alloc>
970 inline bool
971 operator<(const vector<_Tp, _Alloc>& __x, const vector<_Tp, _Alloc>& __y)
972 {
973 return lexicographical_compare(__x.begin(), __x.end(),
974 __y.begin(), __y.end());
975 }
976
977 /// Based on operator==
978 template <class _Tp, class _Alloc>
979 inline bool
980 operator!=(const vector<_Tp, _Alloc>& __x, const vector<_Tp, _Alloc>& __y) {
981 return !(__x == __y);
982 }
983
984 /// Based on operator<
985 template <class _Tp, class _Alloc>
986 inline bool
987 operator>(const vector<_Tp, _Alloc>& __x, const vector<_Tp, _Alloc>& __y) {
988 return __y < __x;
989 }
990
991 /// Based on operator<
992 template <class _Tp, class _Alloc>
993 inline bool
994 operator<=(const vector<_Tp, _Alloc>& __x, const vector<_Tp, _Alloc>& __y) {
995 return !(__y < __x);
996 }
997
998 /// Based on operator<
999 template <class _Tp, class _Alloc>
1000 inline bool
1001 operator>=(const vector<_Tp, _Alloc>& __x, const vector<_Tp, _Alloc>& __y) {
1002 return !(__x < __y);
1003 }
1004
1005 /// See std::vector::swap().
1006 template <class _Tp, class _Alloc>
1007 inline void swap(vector<_Tp, _Alloc>& __x, vector<_Tp, _Alloc>& __y)
1008 {
1009 __x.swap(__y);
1010 }
1011
1012
1013 template <class _Tp, class _Alloc>
1014 vector<_Tp,_Alloc>&
1015 vector<_Tp,_Alloc>::operator=(const vector<_Tp,_Alloc>& __x)
1016 {
1017 if (&__x != this) {
1018 const size_type __xlen = __x.size();
1019 if (__xlen > capacity()) {
1020 pointer __tmp = _M_allocate_and_copy(__xlen, __x.begin(), __x.end());
1021 _Destroy(_M_start, _M_finish);
1022 _M_deallocate(_M_start, _M_end_of_storage - _M_start);
1023 _M_start = __tmp;
1024 _M_end_of_storage = _M_start + __xlen;
1025 }
1026 else if (size() >= __xlen) {
1027 iterator __i(copy(__x.begin(), __x.end(), begin()));
1028 _Destroy(__i, end());
1029 }
1030 else {
1031 copy(__x.begin(), __x.begin() + size(), _M_start);
1032 uninitialized_copy(__x.begin() + size(), __x.end(), _M_finish);
1033 }
1034 _M_finish = _M_start + __xlen;
1035 }
1036 return *this;
1037 }
1038
1039 template <class _Tp, class _Alloc>
1040 void
1041 vector<_Tp, _Alloc>::_M_fill_assign(size_t __n, const value_type& __val)
1042 {
1043 if (__n > capacity()) {
1044 vector __tmp(__n, __val, get_allocator());
1045 __tmp.swap(*this);
1046 }
1047 else if (__n > size()) {
1048 fill(begin(), end(), __val);
1049 _M_finish = uninitialized_fill_n(_M_finish, __n - size(), __val);
1050 }
1051 else
1052 erase(fill_n(begin(), __n, __val), end());
1053 }
1054
1055 template <class _Tp, class _Alloc> template <class _InputIter>
1056 void vector<_Tp, _Alloc>::_M_assign_aux(_InputIter __first, _InputIter __last,
1057 input_iterator_tag) {
1058 iterator __cur(begin());
1059 for ( ; __first != __last && __cur != end(); ++__cur, ++__first)
1060 *__cur = *__first;
1061 if (__first == __last)
1062 erase(__cur, end());
1063 else
1064 insert(end(), __first, __last);
1065 }
1066
1067 template <class _Tp, class _Alloc> template <class _ForwardIter>
1068 void
1069 vector<_Tp, _Alloc>::_M_assign_aux(_ForwardIter __first, _ForwardIter __last,
1070 forward_iterator_tag) {
1071 size_type __len = distance(__first, __last);
1072
1073 if (__len > capacity()) {
1074 pointer __tmp(_M_allocate_and_copy(__len, __first, __last));
1075 _Destroy(_M_start, _M_finish);
1076 _M_deallocate(_M_start, _M_end_of_storage - _M_start);
1077 _M_start = __tmp;
1078 _M_end_of_storage = _M_finish = _M_start + __len;
1079 }
1080 else if (size() >= __len) {
1081 iterator __new_finish(copy(__first, __last, _M_start));
1082 _Destroy(__new_finish, end());
1083 _M_finish = __new_finish.base();
1084 }
1085 else {
1086 _ForwardIter __mid = __first;
1087 advance(__mid, size());
1088 copy(__first, __mid, _M_start);
1089 _M_finish = uninitialized_copy(__mid, __last, _M_finish);
1090 }
1091 }
1092
1093 template <class _Tp, class _Alloc>
1094 void
1095 vector<_Tp, _Alloc>::_M_insert_aux(iterator __position, const _Tp& __x)
1096 {
1097 if (_M_finish != _M_end_of_storage) {
1098 _Construct(_M_finish, *(_M_finish - 1));
1099 ++_M_finish;
1100 _Tp __x_copy = __x;
1101 copy_backward(__position, iterator(_M_finish - 2), iterator(_M_finish- 1));
1102 *__position = __x_copy;
1103 }
1104 else {
1105 const size_type __old_size = size();
1106 const size_type __len = __old_size != 0 ? 2 * __old_size : 1;
1107 iterator __new_start(_M_allocate(__len));
1108 iterator __new_finish(__new_start);
1109 try {
1110 __new_finish = uninitialized_copy(iterator(_M_start), __position,
1111 __new_start);
1112 _Construct(__new_finish.base(), __x);
1113 ++__new_finish;
1114 __new_finish = uninitialized_copy(__position, iterator(_M_finish),
1115 __new_finish);
1116 }
1117 catch(...)
1118 {
1119 _Destroy(__new_start,__new_finish);
1120 _M_deallocate(__new_start.base(),__len);
1121 __throw_exception_again;
1122 }
1123 _Destroy(begin(), end());
1124 _M_deallocate(_M_start, _M_end_of_storage - _M_start);
1125 _M_start = __new_start.base();
1126 _M_finish = __new_finish.base();
1127 _M_end_of_storage = __new_start.base() + __len;
1128 }
1129 }
1130
1131 #ifdef _GLIBCPP_DEPRECATED
1132 template <class _Tp, class _Alloc>
1133 void
1134 vector<_Tp, _Alloc>::_M_insert_aux(iterator __position)
1135 {
1136 if (_M_finish != _M_end_of_storage) {
1137 _Construct(_M_finish, *(_M_finish - 1));
1138 ++_M_finish;
1139 copy_backward(__position, iterator(_M_finish - 2),
1140 iterator(_M_finish - 1));
1141 *__position = _Tp();
1142 }
1143 else {
1144 const size_type __old_size = size();
1145 const size_type __len = __old_size != 0 ? 2 * __old_size : 1;
1146 pointer __new_start = _M_allocate(__len);
1147 pointer __new_finish = __new_start;
1148 try {
1149 __new_finish = uninitialized_copy(iterator(_M_start), __position,
1150 __new_start);
1151 _Construct(__new_finish);
1152 ++__new_finish;
1153 __new_finish = uninitialized_copy(__position, iterator(_M_finish),
1154 __new_finish);
1155 }
1156 catch(...)
1157 {
1158 _Destroy(__new_start,__new_finish);
1159 _M_deallocate(__new_start,__len);
1160 __throw_exception_again;
1161 }
1162 _Destroy(begin(), end());
1163 _M_deallocate(_M_start, _M_end_of_storage - _M_start);
1164 _M_start = __new_start;
1165 _M_finish = __new_finish;
1166 _M_end_of_storage = __new_start + __len;
1167 }
1168 }
1169 #endif
1170
1171 template <class _Tp, class _Alloc>
1172 void vector<_Tp, _Alloc>::_M_fill_insert(iterator __position, size_type __n,
1173 const _Tp& __x)
1174 {
1175 if (__n != 0) {
1176 if (size_type(_M_end_of_storage - _M_finish) >= __n) {
1177 _Tp __x_copy = __x;
1178 const size_type __elems_after = end() - __position;
1179 iterator __old_finish(_M_finish);
1180 if (__elems_after > __n) {
1181 uninitialized_copy(_M_finish - __n, _M_finish, _M_finish);
1182 _M_finish += __n;
1183 copy_backward(__position, __old_finish - __n, __old_finish);
1184 fill(__position, __position + __n, __x_copy);
1185 }
1186 else {
1187 uninitialized_fill_n(_M_finish, __n - __elems_after, __x_copy);
1188 _M_finish += __n - __elems_after;
1189 uninitialized_copy(__position, __old_finish, _M_finish);
1190 _M_finish += __elems_after;
1191 fill(__position, __old_finish, __x_copy);
1192 }
1193 }
1194 else {
1195 const size_type __old_size = size();
1196 const size_type __len = __old_size + max(__old_size, __n);
1197 iterator __new_start(_M_allocate(__len));
1198 iterator __new_finish(__new_start);
1199 try {
1200 __new_finish = uninitialized_copy(begin(), __position, __new_start);
1201 __new_finish = uninitialized_fill_n(__new_finish, __n, __x);
1202 __new_finish
1203 = uninitialized_copy(__position, end(), __new_finish);
1204 }
1205 catch(...)
1206 {
1207 _Destroy(__new_start,__new_finish);
1208 _M_deallocate(__new_start.base(),__len);
1209 __throw_exception_again;
1210 }
1211 _Destroy(_M_start, _M_finish);
1212 _M_deallocate(_M_start, _M_end_of_storage - _M_start);
1213 _M_start = __new_start.base();
1214 _M_finish = __new_finish.base();
1215 _M_end_of_storage = __new_start.base() + __len;
1216 }
1217 }
1218 }
1219
1220 template <class _Tp, class _Alloc> template <class _InputIterator>
1221 void
1222 vector<_Tp, _Alloc>::_M_range_insert(iterator __pos,
1223 _InputIterator __first,
1224 _InputIterator __last,
1225 input_iterator_tag)
1226 {
1227 for ( ; __first != __last; ++__first) {
1228 __pos = insert(__pos, *__first);
1229 ++__pos;
1230 }
1231 }
1232
1233 template <class _Tp, class _Alloc> template <class _ForwardIterator>
1234 void
1235 vector<_Tp, _Alloc>::_M_range_insert(iterator __position,
1236 _ForwardIterator __first,
1237 _ForwardIterator __last,
1238 forward_iterator_tag)
1239 {
1240 if (__first != __last) {
1241 size_type __n = distance(__first, __last);
1242 if (size_type(_M_end_of_storage - _M_finish) >= __n) {
1243 const size_type __elems_after = end() - __position;
1244 iterator __old_finish(_M_finish);
1245 if (__elems_after > __n) {
1246 uninitialized_copy(_M_finish - __n, _M_finish, _M_finish);
1247 _M_finish += __n;
1248 copy_backward(__position, __old_finish - __n, __old_finish);
1249 copy(__first, __last, __position);
1250 }
1251 else {
1252 _ForwardIterator __mid = __first;
1253 advance(__mid, __elems_after);
1254 uninitialized_copy(__mid, __last, _M_finish);
1255 _M_finish += __n - __elems_after;
1256 uninitialized_copy(__position, __old_finish, _M_finish);
1257 _M_finish += __elems_after;
1258 copy(__first, __mid, __position);
1259 }
1260 }
1261 else {
1262 const size_type __old_size = size();
1263 const size_type __len = __old_size + max(__old_size, __n);
1264 iterator __new_start(_M_allocate(__len));
1265 iterator __new_finish(__new_start);
1266 try {
1267 __new_finish = uninitialized_copy(iterator(_M_start),
1268 __position, __new_start);
1269 __new_finish = uninitialized_copy(__first, __last, __new_finish);
1270 __new_finish
1271 = uninitialized_copy(__position, iterator(_M_finish), __new_finish);
1272 }
1273 catch(...)
1274 {
1275 _Destroy(__new_start,__new_finish);
1276 _M_deallocate(__new_start.base(), __len);
1277 __throw_exception_again;
1278 }
1279 _Destroy(_M_start, _M_finish);
1280 _M_deallocate(_M_start, _M_end_of_storage - _M_start);
1281 _M_start = __new_start.base();
1282 _M_finish = __new_finish.base();
1283 _M_end_of_storage = __new_start.base() + __len;
1284 }
1285 }
1286 }
1287
1288 } // namespace std
1289
1290 #endif /* __GLIBCPP_INTERNAL_VECTOR_H */
1291