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