Makefile.am: Add new files.
[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 <typename _Tp, typename _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 <typename _Tp, typename _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 <typename _Tp, typename _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 <typename _Tp, typename _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 <typename _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<typename _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);
479
480 // element access
481 /**
482 * @brief Subscript access to the data contained in the %vector.
483 * @param n The index of the element for which data should be accessed.
484 * @return Read/write reference to data.
485 *
486 * This operator allows for easy, array-style, data access.
487 * Note that data access with this operator is unchecked and out_of_range
488 * lookups are not defined. (For checked lookups see at().)
489 */
490 reference
491 operator[](size_type __n) { return *(begin() + __n); }
492 // XXX do we need to convert to normal_iterator first?
493
494 /**
495 * @brief Subscript access to the data contained in the %vector.
496 * @param n The index of the element for which data should be accessed.
497 * @return Read-only (constant) reference to data.
498 *
499 * This operator allows for easy, array-style, data access.
500 * Note that data access with this operator is unchecked and out_of_range
501 * lookups are not defined. (For checked lookups see at().)
502 */
503 const_reference
504 operator[](size_type __n) const { return *(begin() + __n); }
505
506 protected:
507 /// @if maint Safety check used only from at(). @endif
508 void
509 _M_range_check(size_type __n) const
510 {
511 if (__n >= this->size())
512 __throw_out_of_range("vector [] access out of range");
513 }
514
515 public:
516 /**
517 * @brief Provides access to the data contained in the %vector.
518 * @param n The index of the element for which data should be accessed.
519 * @return Read/write reference to data.
520 * @throw std::out_of_range If @a n is an invalid index.
521 *
522 * This function provides for safer data access. The parameter is first
523 * checked that it is in the range of the vector. The function throws
524 * out_of_range if the check fails.
525 */
526 reference
527 at(size_type __n) { _M_range_check(__n); return (*this)[__n]; }
528
529 /**
530 * @brief Provides access to the data contained in the %vector.
531 * @param n The index of the element for which data should be accessed.
532 * @return Read-only (constant) reference to data.
533 * @throw std::out_of_range If @a n is an invalid index.
534 *
535 * This function provides for safer data access. The parameter is first
536 * checked that it is in the range of the vector. The function throws
537 * out_of_range if the check fails.
538 */
539 const_reference
540 at(size_type __n) const { _M_range_check(__n); return (*this)[__n]; }
541
542 /**
543 * Returns a read/write reference to the data at the first element of the
544 * %vector.
545 */
546 reference
547 front() { return *begin(); }
548 // XXX do we need to convert to normal_iterator first?
549
550 /**
551 * Returns a read-only (constant) reference to the data at the first
552 * element of the %vector.
553 */
554 const_reference
555 front() const { return *begin(); }
556
557 /**
558 * Returns a read/write reference to the data at the last element of the
559 * %vector.
560 */
561 reference
562 back() { return *(end() - 1); }
563
564 /**
565 * Returns a read-only (constant) reference to the data at the last
566 * element of the %vector.
567 */
568 const_reference
569 back() const { return *(end() - 1); }
570
571 // [23.2.4.3] modifiers
572 /**
573 * @brief Add data to the end of the %vector.
574 * @param x Data to be added.
575 *
576 * This is a typical stack operation. The function creates an element at
577 * the end of the %vector and assigns the given data to it.
578 * Due to the nature of a %vector this operation can be done in constant
579 * time if the %vector has preallocated space available.
580 */
581 void
582 push_back(const value_type& __x)
583 {
584 if (_M_finish != _M_end_of_storage)
585 {
586 _Construct(_M_finish, __x);
587 ++_M_finish;
588 }
589 else
590 _M_insert_aux(end(), __x);
591 }
592
593 /**
594 * @brief Removes last element.
595 *
596 * This is a typical stack operation. It shrinks the %vector by one.
597 *
598 * Note that no data is returned, and if the last element's data is
599 * needed, it should be retrieved before pop_back() is called.
600 */
601 void
602 pop_back()
603 {
604 --_M_finish;
605 _Destroy(_M_finish);
606 }
607
608 /**
609 * @brief Inserts given value into %vector before specified iterator.
610 * @param position An iterator into the %vector.
611 * @param x Data to be inserted.
612 * @return An iterator that points to the inserted data.
613 *
614 * This function will insert a copy of the given value before the specified
615 * location.
616 * Note that this kind of operation could be expensive for a %vector and if
617 * it is frequently used the user should consider using std::list.
618 */
619 iterator
620 insert(iterator __position, const value_type& __x);
621
622 #ifdef _GLIBCPP_DEPRECATED
623 /**
624 * @brief Inserts an element into the %vector.
625 * @param position An iterator into the %vector.
626 * @return An iterator that points to the inserted element.
627 *
628 * This function will insert a default-constructed element before the
629 * specified location. You should consider using
630 * insert(position,value_type()) instead.
631 * Note that this kind of operation could be expensive for a vector and if
632 * it is frequently used the user should consider using std::list.
633 *
634 * @note This was deprecated in 3.2 and will be removed in 3.3. You must
635 * define @c _GLIBCPP_DEPRECATED to make this visible in 3.2; see
636 * c++config.h.
637 */
638 iterator
639 insert(iterator __position)
640 { return insert(__position, value_type()); }
641 #endif
642
643 /**
644 * @brief Inserts a number of copies of given data into the %vector.
645 * @param position An iterator into the %vector.
646 * @param n Number of elements to be inserted.
647 * @param x Data to be inserted.
648 *
649 * This function will insert a specified number of copies of the given data
650 * before the location specified by @a position.
651 *
652 * Note that this kind of operation could be expensive for a %vector and if
653 * it is frequently used the user should consider using std::list.
654 */
655 void
656 insert (iterator __pos, size_type __n, const value_type& __x)
657 { _M_fill_insert(__pos, __n, __x); }
658
659 /**
660 * @brief Inserts a range into the %vector.
661 * @param pos An iterator into the %vector.
662 * @param first An input iterator.
663 * @param last An input iterator.
664 *
665 * This function will insert copies of the data in the range [first,last)
666 * into the %vector before the location specified by @a pos.
667 *
668 * Note that this kind of operation could be expensive for a %vector and if
669 * it is frequently used the user should consider using std::list.
670 */
671 template<typename _InputIterator>
672 void
673 insert(iterator __pos, _InputIterator __first, _InputIterator __last)
674 {
675 // Check whether it's an integral type. If so, it's not an iterator.
676 typedef typename _Is_integer<_InputIterator>::_Integral _Integral;
677 _M_insert_dispatch(__pos, __first, __last, _Integral());
678 }
679
680 /**
681 * @brief Remove element at given position.
682 * @param position Iterator pointing to element to be erased.
683 * @return An iterator pointing to the next element (or end()).
684 *
685 * This function will erase the element at the given position and thus
686 * shorten the %vector by one.
687 *
688 * Note This operation could be expensive and if it is frequently used the
689 * user should consider using std::list. The user is also cautioned that
690 * this function only erases the element, and that if the element is itself
691 * a pointer, the pointed-to memory is not touched in any way. Managing
692 * the pointer is the user's responsibilty.
693 */
694 iterator
695 erase(iterator __position);
696
697 /**
698 * @brief Remove a range of elements.
699 * @param first Iterator pointing to the first element to be erased.
700 * @param last Iterator pointing to one past the last element to be erased.
701 * @return An iterator pointing to the element pointed to by @a last
702 * prior to erasing (or end()).
703 *
704 * This function will erase the elements in the range [first,last) and
705 * shorten the %vector accordingly.
706 *
707 * Note This operation could be expensive and if it is frequently used the
708 * user should consider using std::list. The user is also cautioned that
709 * this function only erases the elements, and that if the elements
710 * themselves are pointers, the pointed-to memory is not touched in any
711 * way. Managing the pointer is the user's responsibilty.
712 */
713 iterator
714 erase(iterator __first, iterator __last);
715
716 /**
717 * @brief Swaps data with another %vector.
718 * @param x A %vector of the same element and allocator types.
719 *
720 * This exchanges the elements between two vectors in constant time.
721 * (Three pointers, so it should be quite fast.)
722 * Note that the global std::swap() function is specialized such that
723 * std::swap(v1,v2) will feed to this function.
724 */
725 void
726 swap(vector& __x)
727 {
728 std::swap(_M_start, __x._M_start);
729 std::swap(_M_finish, __x._M_finish);
730 std::swap(_M_end_of_storage, __x._M_end_of_storage);
731 }
732
733 /**
734 * Erases all the elements. Note that this function only erases the
735 * elements, and that if the elements themselves are pointers, the
736 * pointed-to memory is not touched in any way. Managing the pointer is
737 * the user's responsibilty.
738 */
739 void
740 clear() { erase(begin(), end()); }
741
742 protected:
743 /**
744 * @if maint
745 * Memory expansion handler. Uses the member allocation function to
746 * obtain @a n bytes of memory, and then copies [first,last) into it.
747 * @endif
748 */
749 template <typename _ForwardIterator>
750 pointer
751 _M_allocate_and_copy(size_type __n,
752 _ForwardIterator __first, _ForwardIterator __last)
753 {
754 pointer __result = _M_allocate(__n);
755 try
756 {
757 uninitialized_copy(__first, __last, __result);
758 return __result;
759 }
760 catch(...)
761 {
762 _M_deallocate(__result, __n);
763 __throw_exception_again;
764 }
765 }
766
767
768 // Internal constructor functions follow.
769
770 // called by the range constructor to implement [23.1.1]/9
771 template<typename _Integer>
772 void
773 _M_initialize_dispatch(_Integer __n, _Integer __value, __true_type)
774 {
775 _M_start = _M_allocate(__n);
776 _M_end_of_storage = _M_start + __n;
777 _M_finish = uninitialized_fill_n(_M_start, __n, __value);
778 }
779
780 // called by the range constructor to implement [23.1.1]/9
781 template<typename _InputIter>
782 void
783 _M_initialize_dispatch(_InputIter __first, _InputIter __last, __false_type)
784 {
785 typedef typename iterator_traits<_InputIter>::iterator_category
786 _IterCategory;
787 _M_range_initialize(__first, __last, _IterCategory());
788 }
789
790 // called by the second initialize_dispatch above
791 template <typename _InputIterator>
792 void
793 _M_range_initialize(_InputIterator __first,
794 _InputIterator __last, input_iterator_tag)
795 {
796 for ( ; __first != __last; ++__first)
797 push_back(*__first);
798 }
799
800 // called by the second initialize_dispatch above
801 template <typename _ForwardIterator>
802 void _M_range_initialize(_ForwardIterator __first,
803 _ForwardIterator __last, forward_iterator_tag)
804 {
805 size_type __n = distance(__first, __last);
806 _M_start = _M_allocate(__n);
807 _M_end_of_storage = _M_start + __n;
808 _M_finish = uninitialized_copy(__first, __last, _M_start);
809 }
810
811
812 // Internal assign functions follow. The *_aux functions do the actual
813 // assignment work for the range versions.
814
815 // called by the range assign to implement [23.1.1]/9
816 template<typename _Integer>
817 void
818 _M_assign_dispatch(_Integer __n, _Integer __val, __true_type)
819 {
820 _M_fill_assign(static_cast<size_type>(__n),
821 static_cast<value_type>(__val));
822 }
823
824 // called by the range assign to implement [23.1.1]/9
825 template<typename _InputIter>
826 void
827 _M_assign_dispatch(_InputIter __first, _InputIter __last, __false_type)
828 {
829 typedef typename iterator_traits<_InputIter>::iterator_category
830 _IterCategory;
831 _M_assign_aux(__first, __last, _IterCategory());
832 }
833
834 // called by the second assign_dispatch above
835 template <typename _InputIterator>
836 void
837 _M_assign_aux(_InputIterator __first, _InputIterator __last,
838 input_iterator_tag);
839
840 // called by the second assign_dispatch above
841 template <typename _ForwardIterator>
842 void
843 _M_assign_aux(_ForwardIterator __first, _ForwardIterator __last,
844 forward_iterator_tag);
845
846 // Called by assign(n,t), and the range assign when it turns out to be the
847 // same thing.
848 void
849 _M_fill_assign(size_type __n, const value_type& __val);
850
851
852 // Internal insert functions follow.
853
854 // called by the range insert to implement [23.1.1]/9
855 template<typename _Integer>
856 void
857 _M_insert_dispatch(iterator __pos, _Integer __n, _Integer __val,
858 __true_type)
859 {
860 _M_fill_insert(__pos, static_cast<size_type>(__n),
861 static_cast<value_type>(__val));
862 }
863
864 // called by the range insert to implement [23.1.1]/9
865 template<typename _InputIterator>
866 void
867 _M_insert_dispatch(iterator __pos, _InputIterator __first,
868 _InputIterator __last, __false_type)
869 {
870 typedef typename iterator_traits<_InputIterator>::iterator_category
871 _IterCategory;
872 _M_range_insert(__pos, __first, __last, _IterCategory());
873 }
874
875 // called by the second insert_dispatch above
876 template <typename _InputIterator>
877 void
878 _M_range_insert(iterator __pos,
879 _InputIterator __first, _InputIterator __last,
880 input_iterator_tag);
881
882 // called by the second insert_dispatch above
883 template <typename _ForwardIterator>
884 void
885 _M_range_insert(iterator __pos,
886 _ForwardIterator __first, _ForwardIterator __last,
887 forward_iterator_tag);
888
889 // Called by insert(p,n,x), and the range insert when it turns out to be
890 // the same thing.
891 void
892 _M_fill_insert (iterator __pos, size_type __n, const value_type& __x);
893
894 // called by insert(p,x)
895 void
896 _M_insert_aux(iterator __position, const value_type& __x);
897
898 #ifdef _GLIBCPP_DEPRECATED
899 // unused now (same situation as in deque)
900 void _M_insert_aux(iterator __position);
901 #endif
902 };
903
904
905 /**
906 * @brief Vector equality comparison.
907 * @param x A %vector.
908 * @param y A %vector of the same type as @a x.
909 * @return True iff the size and elements of the vectors are equal.
910 *
911 * This is an equivalence relation. It is linear in the size of the
912 * vectors. Vectors are considered equivalent if their sizes are equal,
913 * and if corresponding elements compare equal.
914 */
915 template <typename _Tp, typename _Alloc>
916 inline bool
917 operator==(const vector<_Tp, _Alloc>& __x, const vector<_Tp, _Alloc>& __y)
918 {
919 return __x.size() == __y.size() &&
920 equal(__x.begin(), __x.end(), __y.begin());
921 }
922
923 /**
924 * @brief Vector ordering relation.
925 * @param x A %vector.
926 * @param y A %vector of the same type as @a x.
927 * @return True iff @a x is lexographically less than @a y.
928 *
929 * This is a total ordering relation. It is linear in the size of the
930 * vectors. The elements must be comparable with @c <.
931 *
932 * See std::lexographical_compare() for how the determination is made.
933 */
934 template <typename _Tp, typename _Alloc>
935 inline bool
936 operator<(const vector<_Tp, _Alloc>& __x, const vector<_Tp, _Alloc>& __y)
937 {
938 return lexicographical_compare(__x.begin(), __x.end(),
939 __y.begin(), __y.end());
940 }
941
942 /// Based on operator==
943 template <typename _Tp, typename _Alloc>
944 inline bool
945 operator!=(const vector<_Tp, _Alloc>& __x, const vector<_Tp, _Alloc>& __y) {
946 return !(__x == __y);
947 }
948
949 /// Based on operator<
950 template <typename _Tp, typename _Alloc>
951 inline bool
952 operator>(const vector<_Tp, _Alloc>& __x, const vector<_Tp, _Alloc>& __y) {
953 return __y < __x;
954 }
955
956 /// Based on operator<
957 template <typename _Tp, typename _Alloc>
958 inline bool
959 operator<=(const vector<_Tp, _Alloc>& __x, const vector<_Tp, _Alloc>& __y) {
960 return !(__y < __x);
961 }
962
963 /// Based on operator<
964 template <typename _Tp, typename _Alloc>
965 inline bool
966 operator>=(const vector<_Tp, _Alloc>& __x, const vector<_Tp, _Alloc>& __y) {
967 return !(__x < __y);
968 }
969
970 /// See std::vector::swap().
971 template <typename _Tp, typename _Alloc>
972 inline void swap(vector<_Tp, _Alloc>& __x, vector<_Tp, _Alloc>& __y)
973 {
974 __x.swap(__y);
975 }
976
977 } // namespace std
978
979 #endif /* __GLIBCPP_INTERNAL_VECTOR_H */
980