1 // Debugging vector implementation -*- C++ -*-
3 // Copyright (C) 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011,
5 // Free Software Foundation, Inc.
7 // This file is part of the GNU ISO C++ Library. This library is free
8 // software; you can redistribute it and/or modify it under the
9 // terms of the GNU General Public License as published by the
10 // Free Software Foundation; either version 3, or (at your option)
13 // This library is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 // GNU General Public License for more details.
18 // Under Section 7 of GPL version 3, you are granted additional
19 // permissions described in the GCC Runtime Library Exception, version
20 // 3.1, as published by the Free Software Foundation.
22 // You should have received a copy of the GNU General Public License and
23 // a copy of the GCC Runtime Library Exception along with this program;
24 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
25 // <http://www.gnu.org/licenses/>.
27 /** @file debug/vector
28 * This file is a GNU debug extension to the Standard C++ Library.
31 #ifndef _GLIBCXX_DEBUG_VECTOR
32 #define _GLIBCXX_DEBUG_VECTOR 1
36 #include <debug/safe_sequence.h>
37 #include <debug/safe_iterator.h>
39 namespace std _GLIBCXX_VISIBILITY(default)
43 /// Class std::vector with safety/checking/debug instrumentation.
44 template<typename _Tp,
45 typename _Allocator = std::allocator<_Tp> >
47 : public _GLIBCXX_STD_C::vector<_Tp, _Allocator>,
48 public __gnu_debug::_Safe_sequence<vector<_Tp, _Allocator> >
50 typedef _GLIBCXX_STD_C::vector<_Tp, _Allocator> _Base;
52 typedef typename _Base::iterator _Base_iterator;
53 typedef typename _Base::const_iterator _Base_const_iterator;
54 typedef __gnu_debug::_Equal_to<_Base_const_iterator> _Equal;
56 #ifdef __GXX_EXPERIMENTAL_CXX0X__
57 typedef __gnu_cxx::__alloc_traits<_Allocator> _Alloc_traits;
61 typedef typename _Base::reference reference;
62 typedef typename _Base::const_reference const_reference;
64 typedef __gnu_debug::_Safe_iterator<_Base_iterator,vector>
66 typedef __gnu_debug::_Safe_iterator<_Base_const_iterator,vector>
69 typedef typename _Base::size_type size_type;
70 typedef typename _Base::difference_type difference_type;
72 typedef _Tp value_type;
73 typedef _Allocator allocator_type;
74 typedef typename _Base::pointer pointer;
75 typedef typename _Base::const_pointer const_pointer;
76 typedef std::reverse_iterator<iterator> reverse_iterator;
77 typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
79 // 23.2.4.1 construct/copy/destroy:
81 vector(const _Allocator& __a = _Allocator())
82 : _Base(__a), _M_guaranteed_capacity(0) { }
84 #ifdef __GXX_EXPERIMENTAL_CXX0X__
87 : _Base(__n), _M_guaranteed_capacity(__n) { }
89 vector(size_type __n, const _Tp& __value,
90 const _Allocator& __a = _Allocator())
91 : _Base(__n, __value, __a), _M_guaranteed_capacity(__n) { }
94 vector(size_type __n, const _Tp& __value = _Tp(),
95 const _Allocator& __a = _Allocator())
96 : _Base(__n, __value, __a), _M_guaranteed_capacity(__n) { }
99 #ifdef __GXX_EXPERIMENTAL_CXX0X__
100 template<class _InputIterator,
101 typename = std::_RequireInputIter<_InputIterator>>
103 template<class _InputIterator>
105 vector(_InputIterator __first, _InputIterator __last,
106 const _Allocator& __a = _Allocator())
107 : _Base(__gnu_debug::__base(__gnu_debug::__check_valid_range(__first,
109 __gnu_debug::__base(__last), __a),
110 _M_guaranteed_capacity(0)
111 { _M_update_guaranteed_capacity(); }
113 vector(const vector& __x)
114 : _Base(__x), _M_guaranteed_capacity(__x.size()) { }
116 /// Construction from a release-mode vector
117 vector(const _Base& __x)
118 : _Base(__x), _M_guaranteed_capacity(__x.size()) { }
120 #ifdef __GXX_EXPERIMENTAL_CXX0X__
121 vector(vector&& __x) noexcept
122 : _Base(std::move(__x)),
123 _M_guaranteed_capacity(this->size())
126 __x._M_guaranteed_capacity = 0;
129 vector(const vector& __x, const allocator_type& __a)
130 : _Base(__x, __a), _M_guaranteed_capacity(__x.size()) { }
132 vector(vector&& __x, const allocator_type& __a)
133 : _Base(std::move(__x), __a),
134 _M_guaranteed_capacity(this->size())
136 __x._M_invalidate_all();
137 __x._M_guaranteed_capacity = 0;
140 vector(initializer_list<value_type> __l,
141 const allocator_type& __a = allocator_type())
143 _M_guaranteed_capacity(__l.size()) { }
146 ~vector() _GLIBCXX_NOEXCEPT { }
149 operator=(const vector& __x)
151 static_cast<_Base&>(*this) = __x;
152 this->_M_invalidate_all();
153 _M_update_guaranteed_capacity();
157 #ifdef __GXX_EXPERIMENTAL_CXX0X__
159 operator=(vector&& __x) noexcept(_Alloc_traits::_S_nothrow_move())
161 _Base::operator=(std::move(__x));
162 this->_M_invalidate_all();
163 _M_update_guaranteed_capacity();
164 __x._M_invalidate_all();
165 __x._M_guaranteed_capacity = 0;
170 operator=(initializer_list<value_type> __l)
172 static_cast<_Base&>(*this) = __l;
173 this->_M_invalidate_all();
174 _M_update_guaranteed_capacity();
179 #ifdef __GXX_EXPERIMENTAL_CXX0X__
180 template<typename _InputIterator,
181 typename = std::_RequireInputIter<_InputIterator>>
183 template<typename _InputIterator>
186 assign(_InputIterator __first, _InputIterator __last)
188 __glibcxx_check_valid_range(__first, __last);
189 _Base::assign(__gnu_debug::__base(__first),
190 __gnu_debug::__base(__last));
191 this->_M_invalidate_all();
192 _M_update_guaranteed_capacity();
196 assign(size_type __n, const _Tp& __u)
198 _Base::assign(__n, __u);
199 this->_M_invalidate_all();
200 _M_update_guaranteed_capacity();
203 #ifdef __GXX_EXPERIMENTAL_CXX0X__
205 assign(initializer_list<value_type> __l)
208 this->_M_invalidate_all();
209 _M_update_guaranteed_capacity();
213 using _Base::get_allocator;
217 begin() _GLIBCXX_NOEXCEPT
218 { return iterator(_Base::begin(), this); }
221 begin() const _GLIBCXX_NOEXCEPT
222 { return const_iterator(_Base::begin(), this); }
225 end() _GLIBCXX_NOEXCEPT
226 { return iterator(_Base::end(), this); }
229 end() const _GLIBCXX_NOEXCEPT
230 { return const_iterator(_Base::end(), this); }
233 rbegin() _GLIBCXX_NOEXCEPT
234 { return reverse_iterator(end()); }
236 const_reverse_iterator
237 rbegin() const _GLIBCXX_NOEXCEPT
238 { return const_reverse_iterator(end()); }
241 rend() _GLIBCXX_NOEXCEPT
242 { return reverse_iterator(begin()); }
244 const_reverse_iterator
245 rend() const _GLIBCXX_NOEXCEPT
246 { return const_reverse_iterator(begin()); }
248 #ifdef __GXX_EXPERIMENTAL_CXX0X__
250 cbegin() const noexcept
251 { return const_iterator(_Base::begin(), this); }
254 cend() const noexcept
255 { return const_iterator(_Base::end(), this); }
257 const_reverse_iterator
258 crbegin() const noexcept
259 { return const_reverse_iterator(end()); }
261 const_reverse_iterator
262 crend() const noexcept
263 { return const_reverse_iterator(begin()); }
266 // 23.2.4.2 capacity:
268 using _Base::max_size;
270 #ifdef __GXX_EXPERIMENTAL_CXX0X__
272 resize(size_type __sz)
274 bool __realloc = _M_requires_reallocation(__sz);
275 if (__sz < this->size())
276 this->_M_invalidate_after_nth(__sz);
279 this->_M_invalidate_all();
280 _M_update_guaranteed_capacity();
284 resize(size_type __sz, const _Tp& __c)
286 bool __realloc = _M_requires_reallocation(__sz);
287 if (__sz < this->size())
288 this->_M_invalidate_after_nth(__sz);
289 _Base::resize(__sz, __c);
291 this->_M_invalidate_all();
292 _M_update_guaranteed_capacity();
296 resize(size_type __sz, _Tp __c = _Tp())
298 bool __realloc = _M_requires_reallocation(__sz);
299 if (__sz < this->size())
300 this->_M_invalidate_after_nth(__sz);
301 _Base::resize(__sz, __c);
303 this->_M_invalidate_all();
304 _M_update_guaranteed_capacity();
308 #ifdef __GXX_EXPERIMENTAL_CXX0X__
312 if (_Base::_M_shrink_to_fit())
314 _M_guaranteed_capacity = _Base::capacity();
315 this->_M_invalidate_all();
321 capacity() const _GLIBCXX_NOEXCEPT
323 #ifdef _GLIBCXX_DEBUG_PEDANTIC
324 return _M_guaranteed_capacity;
326 return _Base::capacity();
333 reserve(size_type __n)
335 bool __realloc = _M_requires_reallocation(__n);
337 if (__n > _M_guaranteed_capacity)
338 _M_guaranteed_capacity = __n;
340 this->_M_invalidate_all();
345 operator[](size_type __n)
347 __glibcxx_check_subscript(__n);
348 return _M_base()[__n];
352 operator[](size_type __n) const
354 __glibcxx_check_subscript(__n);
355 return _M_base()[__n];
363 __glibcxx_check_nonempty();
364 return _Base::front();
370 __glibcxx_check_nonempty();
371 return _Base::front();
377 __glibcxx_check_nonempty();
378 return _Base::back();
384 __glibcxx_check_nonempty();
385 return _Base::back();
388 // _GLIBCXX_RESOLVE_LIB_DEFECTS
389 // DR 464. Suggestion for new member functions in standard containers.
392 // 23.2.4.3 modifiers:
394 push_back(const _Tp& __x)
396 bool __realloc = _M_requires_reallocation(this->size() + 1);
397 _Base::push_back(__x);
399 this->_M_invalidate_all();
400 _M_update_guaranteed_capacity();
403 #ifdef __GXX_EXPERIMENTAL_CXX0X__
404 template<typename _Up = _Tp>
405 typename __gnu_cxx::__enable_if<!std::__are_same<_Up, bool>::__value,
408 { emplace_back(std::move(__x)); }
410 template<typename... _Args>
412 emplace_back(_Args&&... __args)
414 bool __realloc = _M_requires_reallocation(this->size() + 1);
415 _Base::emplace_back(std::forward<_Args>(__args)...);
417 this->_M_invalidate_all();
418 _M_update_guaranteed_capacity();
425 __glibcxx_check_nonempty();
426 this->_M_invalidate_if(_Equal(--_Base::end()));
430 #ifdef __GXX_EXPERIMENTAL_CXX0X__
431 template<typename... _Args>
433 emplace(iterator __position, _Args&&... __args)
435 __glibcxx_check_insert(__position);
436 bool __realloc = _M_requires_reallocation(this->size() + 1);
437 difference_type __offset = __position.base() - _Base::begin();
438 _Base_iterator __res = _Base::emplace(__position.base(),
439 std::forward<_Args>(__args)...);
441 this->_M_invalidate_all();
443 this->_M_invalidate_after_nth(__offset);
444 _M_update_guaranteed_capacity();
445 return iterator(__res, this);
450 insert(iterator __position, const _Tp& __x)
452 __glibcxx_check_insert(__position);
453 bool __realloc = _M_requires_reallocation(this->size() + 1);
454 difference_type __offset = __position.base() - _Base::begin();
455 _Base_iterator __res = _Base::insert(__position.base(), __x);
457 this->_M_invalidate_all();
459 this->_M_invalidate_after_nth(__offset);
460 _M_update_guaranteed_capacity();
461 return iterator(__res, this);
464 #ifdef __GXX_EXPERIMENTAL_CXX0X__
465 template<typename _Up = _Tp>
466 typename __gnu_cxx::__enable_if<!std::__are_same<_Up, bool>::__value,
468 insert(iterator __position, _Tp&& __x)
469 { return emplace(__position, std::move(__x)); }
472 insert(iterator __position, initializer_list<value_type> __l)
473 { this->insert(__position, __l.begin(), __l.end()); }
477 insert(iterator __position, size_type __n, const _Tp& __x)
479 __glibcxx_check_insert(__position);
480 bool __realloc = _M_requires_reallocation(this->size() + __n);
481 difference_type __offset = __position.base() - _Base::begin();
482 _Base::insert(__position.base(), __n, __x);
484 this->_M_invalidate_all();
486 this->_M_invalidate_after_nth(__offset);
487 _M_update_guaranteed_capacity();
490 #ifdef __GXX_EXPERIMENTAL_CXX0X__
491 template<class _InputIterator,
492 typename = std::_RequireInputIter<_InputIterator>>
494 template<class _InputIterator>
497 insert(iterator __position,
498 _InputIterator __first, _InputIterator __last)
500 __glibcxx_check_insert_range(__position, __first, __last);
502 /* Hard to guess if invalidation will occur, because __last
503 - __first can't be calculated in all cases, so we just
504 punt here by checking if it did occur. */
505 _Base_iterator __old_begin = _M_base().begin();
506 difference_type __offset = __position.base() - _Base::begin();
507 _Base::insert(__position.base(), __gnu_debug::__base(__first),
508 __gnu_debug::__base(__last));
510 if (_M_base().begin() != __old_begin)
511 this->_M_invalidate_all();
513 this->_M_invalidate_after_nth(__offset);
514 _M_update_guaranteed_capacity();
518 erase(iterator __position)
520 __glibcxx_check_erase(__position);
521 difference_type __offset = __position.base() - _Base::begin();
522 _Base_iterator __res = _Base::erase(__position.base());
523 this->_M_invalidate_after_nth(__offset);
524 return iterator(__res, this);
528 erase(iterator __first, iterator __last)
530 // _GLIBCXX_RESOLVE_LIB_DEFECTS
531 // 151. can't currently clear() empty container
532 __glibcxx_check_erase_range(__first, __last);
534 if (__first.base() != __last.base())
536 difference_type __offset = __first.base() - _Base::begin();
537 _Base_iterator __res = _Base::erase(__first.base(),
539 this->_M_invalidate_after_nth(__offset);
540 return iterator(__res, this);
548 #ifdef __GXX_EXPERIMENTAL_CXX0X__
549 noexcept(_Alloc_traits::_S_nothrow_swap())
554 std::swap(_M_guaranteed_capacity, __x._M_guaranteed_capacity);
558 clear() _GLIBCXX_NOEXCEPT
561 this->_M_invalidate_all();
562 _M_guaranteed_capacity = 0;
566 _M_base() _GLIBCXX_NOEXCEPT { return *this; }
569 _M_base() const _GLIBCXX_NOEXCEPT { return *this; }
572 size_type _M_guaranteed_capacity;
575 _M_requires_reallocation(size_type __elements)
576 { return __elements > this->capacity(); }
579 _M_update_guaranteed_capacity()
581 if (this->size() > _M_guaranteed_capacity)
582 _M_guaranteed_capacity = this->size();
586 _M_invalidate_after_nth(difference_type __n)
588 typedef __gnu_debug::_After_nth_from<_Base_const_iterator> _After_nth;
589 this->_M_invalidate_if(_After_nth(__n, _Base::begin()));
593 template<typename _Tp, typename _Alloc>
595 operator==(const vector<_Tp, _Alloc>& __lhs,
596 const vector<_Tp, _Alloc>& __rhs)
597 { return __lhs._M_base() == __rhs._M_base(); }
599 template<typename _Tp, typename _Alloc>
601 operator!=(const vector<_Tp, _Alloc>& __lhs,
602 const vector<_Tp, _Alloc>& __rhs)
603 { return __lhs._M_base() != __rhs._M_base(); }
605 template<typename _Tp, typename _Alloc>
607 operator<(const vector<_Tp, _Alloc>& __lhs,
608 const vector<_Tp, _Alloc>& __rhs)
609 { return __lhs._M_base() < __rhs._M_base(); }
611 template<typename _Tp, typename _Alloc>
613 operator<=(const vector<_Tp, _Alloc>& __lhs,
614 const vector<_Tp, _Alloc>& __rhs)
615 { return __lhs._M_base() <= __rhs._M_base(); }
617 template<typename _Tp, typename _Alloc>
619 operator>=(const vector<_Tp, _Alloc>& __lhs,
620 const vector<_Tp, _Alloc>& __rhs)
621 { return __lhs._M_base() >= __rhs._M_base(); }
623 template<typename _Tp, typename _Alloc>
625 operator>(const vector<_Tp, _Alloc>& __lhs,
626 const vector<_Tp, _Alloc>& __rhs)
627 { return __lhs._M_base() > __rhs._M_base(); }
629 template<typename _Tp, typename _Alloc>
631 swap(vector<_Tp, _Alloc>& __lhs, vector<_Tp, _Alloc>& __rhs)
632 { __lhs.swap(__rhs); }
634 } // namespace __debug
636 #ifdef __GXX_EXPERIMENTAL_CXX0X__
638 /// std::hash specialization for vector<bool>.
639 template<typename _Alloc>
640 struct hash<__debug::vector<bool, _Alloc>>
641 : public __hash_base<size_t, __debug::vector<bool, _Alloc>>
644 operator()(const __debug::vector<bool, _Alloc>& __b) const noexcept
645 { return std::hash<_GLIBCXX_STD_C::vector<bool, _Alloc>>()