1 // Debugging vector implementation -*- C++ -*-
3 // Copyright (C) 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010
4 // Free Software Foundation, Inc.
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 3, or (at your option)
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.
17 // Under Section 7 of GPL version 3, you are granted additional
18 // permissions described in the GCC Runtime Library Exception, version
19 // 3.1, as published by the Free Software Foundation.
21 // You should have received a copy of the GNU General Public License and
22 // a copy of the GCC Runtime Library Exception along with this program;
23 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
24 // <http://www.gnu.org/licenses/>.
26 /** @file debug/vector
27 * This file is a GNU debug extension to the Standard C++ Library.
30 #ifndef _GLIBCXX_DEBUG_VECTOR
31 #define _GLIBCXX_DEBUG_VECTOR 1
35 #include <debug/safe_sequence.h>
36 #include <debug/safe_iterator.h>
38 namespace std _GLIBCXX_VISIBILITY(default)
42 /// Class std::vector with safety/checking/debug instrumentation.
43 template<typename _Tp,
44 typename _Allocator = std::allocator<_Tp> >
46 : public _GLIBCXX_STD_C::vector<_Tp, _Allocator>,
47 public __gnu_debug::_Safe_sequence<vector<_Tp, _Allocator> >
49 typedef _GLIBCXX_STD_C::vector<_Tp, _Allocator> _Base;
50 typedef __gnu_debug::_Safe_sequence<vector> _Safe_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;
57 typedef typename _Base::reference reference;
58 typedef typename _Base::const_reference const_reference;
60 typedef __gnu_debug::_Safe_iterator<_Base_iterator,vector>
62 typedef __gnu_debug::_Safe_iterator<_Base_const_iterator,vector>
65 typedef typename _Base::size_type size_type;
66 typedef typename _Base::difference_type difference_type;
68 typedef _Tp value_type;
69 typedef _Allocator allocator_type;
70 typedef typename _Base::pointer pointer;
71 typedef typename _Base::const_pointer const_pointer;
72 typedef std::reverse_iterator<iterator> reverse_iterator;
73 typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
75 // 23.2.4.1 construct/copy/destroy:
77 vector(const _Allocator& __a = _Allocator())
78 : _Base(__a), _M_guaranteed_capacity(0) { }
80 #ifdef __GXX_EXPERIMENTAL_CXX0X__
83 : _Base(__n), _M_guaranteed_capacity(__n) { }
85 vector(size_type __n, const _Tp& __value,
86 const _Allocator& __a = _Allocator())
87 : _Base(__n, __value, __a), _M_guaranteed_capacity(__n) { }
90 vector(size_type __n, const _Tp& __value = _Tp(),
91 const _Allocator& __a = _Allocator())
92 : _Base(__n, __value, __a), _M_guaranteed_capacity(__n) { }
95 template<class _InputIterator>
96 vector(_InputIterator __first, _InputIterator __last,
97 const _Allocator& __a = _Allocator())
98 : _Base(__gnu_debug::__base(__gnu_debug::__check_valid_range(__first,
100 __gnu_debug::__base(__last), __a),
101 _M_guaranteed_capacity(0)
102 { _M_update_guaranteed_capacity(); }
104 vector(const vector& __x)
105 : _Base(__x), _Safe_base(), _M_guaranteed_capacity(__x.size()) { }
107 /// Construction from a release-mode vector
108 vector(const _Base& __x)
109 : _Base(__x), _Safe_base(), _M_guaranteed_capacity(__x.size()) { }
111 #ifdef __GXX_EXPERIMENTAL_CXX0X__
113 : _Base(std::move(__x)), _Safe_base(),
114 _M_guaranteed_capacity(this->size())
117 __x._M_guaranteed_capacity = 0;
120 vector(initializer_list<value_type> __l,
121 const allocator_type& __a = allocator_type())
122 : _Base(__l, __a), _Safe_base(),
123 _M_guaranteed_capacity(__l.size()) { }
129 operator=(const vector& __x)
131 static_cast<_Base&>(*this) = __x;
132 this->_M_invalidate_all();
133 _M_update_guaranteed_capacity();
137 #ifdef __GXX_EXPERIMENTAL_CXX0X__
139 operator=(vector&& __x)
149 operator=(initializer_list<value_type> __l)
151 static_cast<_Base&>(*this) = __l;
152 this->_M_invalidate_all();
153 _M_update_guaranteed_capacity();
158 template<typename _InputIterator>
160 assign(_InputIterator __first, _InputIterator __last)
162 __glibcxx_check_valid_range(__first, __last);
163 _Base::assign(__gnu_debug::__base(__first),
164 __gnu_debug::__base(__last));
165 this->_M_invalidate_all();
166 _M_update_guaranteed_capacity();
170 assign(size_type __n, const _Tp& __u)
172 _Base::assign(__n, __u);
173 this->_M_invalidate_all();
174 _M_update_guaranteed_capacity();
177 #ifdef __GXX_EXPERIMENTAL_CXX0X__
179 assign(initializer_list<value_type> __l)
182 this->_M_invalidate_all();
183 _M_update_guaranteed_capacity();
187 using _Base::get_allocator;
192 { return iterator(_Base::begin(), this); }
196 { return const_iterator(_Base::begin(), this); }
200 { return iterator(_Base::end(), this); }
204 { return const_iterator(_Base::end(), this); }
208 { return reverse_iterator(end()); }
210 const_reverse_iterator
212 { return const_reverse_iterator(end()); }
216 { return reverse_iterator(begin()); }
218 const_reverse_iterator
220 { return const_reverse_iterator(begin()); }
222 #ifdef __GXX_EXPERIMENTAL_CXX0X__
225 { return const_iterator(_Base::begin(), this); }
229 { return const_iterator(_Base::end(), this); }
231 const_reverse_iterator
233 { return const_reverse_iterator(end()); }
235 const_reverse_iterator
237 { return const_reverse_iterator(begin()); }
240 // 23.2.4.2 capacity:
242 using _Base::max_size;
244 #ifdef __GXX_EXPERIMENTAL_CXX0X__
246 resize(size_type __sz)
248 bool __realloc = _M_requires_reallocation(__sz);
249 if (__sz < this->size())
250 this->_M_invalidate_after_nth(__sz);
253 this->_M_invalidate_all();
254 _M_update_guaranteed_capacity();
258 resize(size_type __sz, const _Tp& __c)
260 bool __realloc = _M_requires_reallocation(__sz);
261 if (__sz < this->size())
262 this->_M_invalidate_after_nth(__sz);
263 _Base::resize(__sz, __c);
265 this->_M_invalidate_all();
266 _M_update_guaranteed_capacity();
270 resize(size_type __sz, _Tp __c = _Tp())
272 bool __realloc = _M_requires_reallocation(__sz);
273 if (__sz < this->size())
274 this->_M_invalidate_after_nth(__sz);
275 _Base::resize(__sz, __c);
277 this->_M_invalidate_all();
278 _M_update_guaranteed_capacity();
282 #ifdef __GXX_EXPERIMENTAL_CXX0X__
283 using _Base::shrink_to_fit;
289 #ifdef _GLIBCXX_DEBUG_PEDANTIC
290 return _M_guaranteed_capacity;
292 return _Base::capacity();
299 reserve(size_type __n)
301 bool __realloc = _M_requires_reallocation(__n);
303 if (__n > _M_guaranteed_capacity)
304 _M_guaranteed_capacity = __n;
306 this->_M_invalidate_all();
311 operator[](size_type __n)
313 __glibcxx_check_subscript(__n);
314 return _M_base()[__n];
318 operator[](size_type __n) const
320 __glibcxx_check_subscript(__n);
321 return _M_base()[__n];
329 __glibcxx_check_nonempty();
330 return _Base::front();
336 __glibcxx_check_nonempty();
337 return _Base::front();
343 __glibcxx_check_nonempty();
344 return _Base::back();
350 __glibcxx_check_nonempty();
351 return _Base::back();
354 // _GLIBCXX_RESOLVE_LIB_DEFECTS
355 // DR 464. Suggestion for new member functions in standard containers.
358 // 23.2.4.3 modifiers:
360 push_back(const _Tp& __x)
362 bool __realloc = _M_requires_reallocation(this->size() + 1);
363 _Base::push_back(__x);
365 this->_M_invalidate_all();
366 _M_update_guaranteed_capacity();
369 #ifdef __GXX_EXPERIMENTAL_CXX0X__
370 template<typename _Up = _Tp>
371 typename __gnu_cxx::__enable_if<!std::__are_same<_Up, bool>::__value,
374 { emplace_back(std::move(__x)); }
376 template<typename... _Args>
378 emplace_back(_Args&&... __args)
380 bool __realloc = _M_requires_reallocation(this->size() + 1);
381 _Base::emplace_back(std::forward<_Args>(__args)...);
383 this->_M_invalidate_all();
384 _M_update_guaranteed_capacity();
391 __glibcxx_check_nonempty();
392 this->_M_invalidate_if(_Equal(--_Base::end()));
396 #ifdef __GXX_EXPERIMENTAL_CXX0X__
397 template<typename... _Args>
399 emplace(iterator __position, _Args&&... __args)
401 __glibcxx_check_insert(__position);
402 bool __realloc = _M_requires_reallocation(this->size() + 1);
403 difference_type __offset = __position.base() - _Base::begin();
404 _Base_iterator __res = _Base::emplace(__position.base(),
405 std::forward<_Args>(__args)...);
407 this->_M_invalidate_all();
409 this->_M_invalidate_after_nth(__offset);
410 _M_update_guaranteed_capacity();
411 return iterator(__res, this);
416 insert(iterator __position, const _Tp& __x)
418 __glibcxx_check_insert(__position);
419 bool __realloc = _M_requires_reallocation(this->size() + 1);
420 difference_type __offset = __position.base() - _Base::begin();
421 _Base_iterator __res = _Base::insert(__position.base(), __x);
423 this->_M_invalidate_all();
425 this->_M_invalidate_after_nth(__offset);
426 _M_update_guaranteed_capacity();
427 return iterator(__res, this);
430 #ifdef __GXX_EXPERIMENTAL_CXX0X__
431 template<typename _Up = _Tp>
432 typename __gnu_cxx::__enable_if<!std::__are_same<_Up, bool>::__value,
434 insert(iterator __position, _Tp&& __x)
435 { return emplace(__position, std::move(__x)); }
438 insert(iterator __position, initializer_list<value_type> __l)
439 { this->insert(__position, __l.begin(), __l.end()); }
443 insert(iterator __position, size_type __n, const _Tp& __x)
445 __glibcxx_check_insert(__position);
446 bool __realloc = _M_requires_reallocation(this->size() + __n);
447 difference_type __offset = __position.base() - _Base::begin();
448 _Base::insert(__position.base(), __n, __x);
450 this->_M_invalidate_all();
452 this->_M_invalidate_after_nth(__offset);
453 _M_update_guaranteed_capacity();
456 template<class _InputIterator>
458 insert(iterator __position,
459 _InputIterator __first, _InputIterator __last)
461 __glibcxx_check_insert_range(__position, __first, __last);
463 /* Hard to guess if invalidation will occur, because __last
464 - __first can't be calculated in all cases, so we just
465 punt here by checking if it did occur. */
466 _Base_iterator __old_begin = _M_base().begin();
467 difference_type __offset = __position.base() - _Base::begin();
468 _Base::insert(__position.base(), __gnu_debug::__base(__first),
469 __gnu_debug::__base(__last));
471 if (_M_base().begin() != __old_begin)
472 this->_M_invalidate_all();
474 this->_M_invalidate_after_nth(__offset);
475 _M_update_guaranteed_capacity();
479 erase(iterator __position)
481 __glibcxx_check_erase(__position);
482 difference_type __offset = __position.base() - _Base::begin();
483 _Base_iterator __res = _Base::erase(__position.base());
484 this->_M_invalidate_after_nth(__offset);
485 return iterator(__res, this);
489 erase(iterator __first, iterator __last)
491 // _GLIBCXX_RESOLVE_LIB_DEFECTS
492 // 151. can't currently clear() empty container
493 __glibcxx_check_erase_range(__first, __last);
495 difference_type __offset = __first.base() - _Base::begin();
496 _Base_iterator __res = _Base::erase(__first.base(),
498 this->_M_invalidate_after_nth(__offset);
499 return iterator(__res, this);
507 std::swap(_M_guaranteed_capacity, __x._M_guaranteed_capacity);
514 this->_M_invalidate_all();
515 _M_guaranteed_capacity = 0;
519 _M_base() { return *this; }
522 _M_base() const { return *this; }
525 size_type _M_guaranteed_capacity;
528 _M_requires_reallocation(size_type __elements)
529 { return __elements > this->capacity(); }
532 _M_update_guaranteed_capacity()
534 if (this->size() > _M_guaranteed_capacity)
535 _M_guaranteed_capacity = this->size();
539 _M_invalidate_after_nth(difference_type __n)
541 typedef __gnu_debug::_After_nth_from<_Base_const_iterator> _After_nth;
542 this->_M_invalidate_if(_After_nth(__n, _Base::begin()));
546 template<typename _Tp, typename _Alloc>
548 operator==(const vector<_Tp, _Alloc>& __lhs,
549 const vector<_Tp, _Alloc>& __rhs)
550 { return __lhs._M_base() == __rhs._M_base(); }
552 template<typename _Tp, typename _Alloc>
554 operator!=(const vector<_Tp, _Alloc>& __lhs,
555 const vector<_Tp, _Alloc>& __rhs)
556 { return __lhs._M_base() != __rhs._M_base(); }
558 template<typename _Tp, typename _Alloc>
560 operator<(const vector<_Tp, _Alloc>& __lhs,
561 const vector<_Tp, _Alloc>& __rhs)
562 { return __lhs._M_base() < __rhs._M_base(); }
564 template<typename _Tp, typename _Alloc>
566 operator<=(const vector<_Tp, _Alloc>& __lhs,
567 const vector<_Tp, _Alloc>& __rhs)
568 { return __lhs._M_base() <= __rhs._M_base(); }
570 template<typename _Tp, typename _Alloc>
572 operator>=(const vector<_Tp, _Alloc>& __lhs,
573 const vector<_Tp, _Alloc>& __rhs)
574 { return __lhs._M_base() >= __rhs._M_base(); }
576 template<typename _Tp, typename _Alloc>
578 operator>(const vector<_Tp, _Alloc>& __lhs,
579 const vector<_Tp, _Alloc>& __rhs)
580 { return __lhs._M_base() > __rhs._M_base(); }
582 template<typename _Tp, typename _Alloc>
584 swap(vector<_Tp, _Alloc>& __lhs, vector<_Tp, _Alloc>& __rhs)
585 { __lhs.swap(__rhs); }
587 } // namespace __debug
589 #ifdef __GXX_EXPERIMENTAL_CXX0X__
591 /// std::hash specialization for vector<bool>.
592 template<typename _Alloc>
593 struct hash<__debug::vector<bool, _Alloc>>
594 : public __hash_base<size_t, __debug::vector<bool, _Alloc>>
597 operator()(const __debug::vector<bool, _Alloc>& __b) const
598 { return std::hash<_GLIBCXX_STD_C::vector<bool, _Alloc>>()