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>
42 /// Class std::vector with safety/checking/debug instrumentation.
43 template<typename _Tp,
44 typename _Allocator = std::allocator<_Tp> >
46 : public _GLIBCXX_STD_D::vector<_Tp, _Allocator>,
47 public __gnu_debug::_Safe_sequence<vector<_Tp, _Allocator> >
49 typedef _GLIBCXX_STD_D::vector<_Tp, _Allocator> _Base;
50 typedef __gnu_debug::_Safe_sequence<vector> _Safe_base;
52 typedef typename _Base::const_iterator _Base_const_iterator;
53 typedef __gnu_debug::_After_nth_from<_Base_const_iterator> _After_nth;
56 typedef typename _Base::reference reference;
57 typedef typename _Base::const_reference const_reference;
59 typedef __gnu_debug::_Safe_iterator<typename _Base::iterator,vector>
61 typedef __gnu_debug::_Safe_iterator<typename _Base::const_iterator,vector>
64 typedef typename _Base::size_type size_type;
65 typedef typename _Base::difference_type difference_type;
67 typedef _Tp value_type;
68 typedef _Allocator allocator_type;
69 typedef typename _Base::pointer pointer;
70 typedef typename _Base::const_pointer const_pointer;
71 typedef std::reverse_iterator<iterator> reverse_iterator;
72 typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
74 // 23.2.4.1 construct/copy/destroy:
75 explicit vector(const _Allocator& __a = _Allocator())
76 : _Base(__a), _M_guaranteed_capacity(0) { }
78 explicit vector(size_type __n, const _Tp& __value = _Tp(),
79 const _Allocator& __a = _Allocator())
80 : _Base(__n, __value, __a), _M_guaranteed_capacity(__n) { }
82 template<class _InputIterator>
83 vector(_InputIterator __first, _InputIterator __last,
84 const _Allocator& __a = _Allocator())
85 : _Base(__gnu_debug::__check_valid_range(__first, __last),
87 _M_guaranteed_capacity(0)
88 { _M_update_guaranteed_capacity(); }
90 vector(const vector& __x)
91 : _Base(__x), _Safe_base(), _M_guaranteed_capacity(__x.size()) { }
93 /// Construction from a release-mode vector
94 vector(const _Base& __x)
95 : _Base(__x), _Safe_base(), _M_guaranteed_capacity(__x.size()) { }
97 #ifdef __GXX_EXPERIMENTAL_CXX0X__
99 : _Base(std::forward<vector>(__x)), _Safe_base(),
100 _M_guaranteed_capacity(this->size())
103 __x._M_guaranteed_capacity = 0;
106 vector(initializer_list<value_type> __l,
107 const allocator_type& __a = allocator_type())
108 : _Base(__l, __a), _Safe_base(),
109 _M_guaranteed_capacity(__l.size()) { }
115 operator=(const vector& __x)
117 static_cast<_Base&>(*this) = __x;
118 this->_M_invalidate_all();
119 _M_update_guaranteed_capacity();
123 #ifdef __GXX_EXPERIMENTAL_CXX0X__
125 operator=(vector&& __x)
135 operator=(initializer_list<value_type> __l)
137 static_cast<_Base&>(*this) = __l;
138 this->_M_invalidate_all();
139 _M_update_guaranteed_capacity();
144 template<typename _InputIterator>
146 assign(_InputIterator __first, _InputIterator __last)
148 __glibcxx_check_valid_range(__first, __last);
149 _Base::assign(__first, __last);
150 this->_M_invalidate_all();
151 _M_update_guaranteed_capacity();
155 assign(size_type __n, const _Tp& __u)
157 _Base::assign(__n, __u);
158 this->_M_invalidate_all();
159 _M_update_guaranteed_capacity();
162 #ifdef __GXX_EXPERIMENTAL_CXX0X__
164 assign(initializer_list<value_type> __l)
167 this->_M_invalidate_all();
168 _M_update_guaranteed_capacity();
172 using _Base::get_allocator;
177 { return iterator(_Base::begin(), this); }
181 { return const_iterator(_Base::begin(), this); }
185 { return iterator(_Base::end(), this); }
189 { return const_iterator(_Base::end(), this); }
193 { return reverse_iterator(end()); }
195 const_reverse_iterator
197 { return const_reverse_iterator(end()); }
201 { return reverse_iterator(begin()); }
203 const_reverse_iterator
205 { return const_reverse_iterator(begin()); }
207 #ifdef __GXX_EXPERIMENTAL_CXX0X__
210 { return const_iterator(_Base::begin(), this); }
214 { return const_iterator(_Base::end(), this); }
216 const_reverse_iterator
218 { return const_reverse_iterator(end()); }
220 const_reverse_iterator
222 { return const_reverse_iterator(begin()); }
225 // 23.2.4.2 capacity:
227 using _Base::max_size;
230 resize(size_type __sz, _Tp __c = _Tp())
232 bool __realloc = _M_requires_reallocation(__sz);
233 if (__sz < this->size())
234 this->_M_invalidate_if(_After_nth(__sz, _M_base().begin()));
235 _Base::resize(__sz, __c);
237 this->_M_invalidate_all();
238 _M_update_guaranteed_capacity();
241 #ifdef __GXX_EXPERIMENTAL_CXX0X__
242 using _Base::shrink_to_fit;
248 #ifdef _GLIBCXX_DEBUG_PEDANTIC
249 return _M_guaranteed_capacity;
251 return _Base::capacity();
258 reserve(size_type __n)
260 bool __realloc = _M_requires_reallocation(__n);
262 if (__n > _M_guaranteed_capacity)
263 _M_guaranteed_capacity = __n;
265 this->_M_invalidate_all();
270 operator[](size_type __n)
272 __glibcxx_check_subscript(__n);
273 return _M_base()[__n];
277 operator[](size_type __n) const
279 __glibcxx_check_subscript(__n);
280 return _M_base()[__n];
288 __glibcxx_check_nonempty();
289 return _Base::front();
295 __glibcxx_check_nonempty();
296 return _Base::front();
302 __glibcxx_check_nonempty();
303 return _Base::back();
309 __glibcxx_check_nonempty();
310 return _Base::back();
313 // _GLIBCXX_RESOLVE_LIB_DEFECTS
314 // DR 464. Suggestion for new member functions in standard containers.
317 // 23.2.4.3 modifiers:
319 push_back(const _Tp& __x)
321 bool __realloc = _M_requires_reallocation(this->size() + 1);
322 _Base::push_back(__x);
324 this->_M_invalidate_all();
325 _M_update_guaranteed_capacity();
328 #ifdef __GXX_EXPERIMENTAL_CXX0X__
329 template<typename _Up = _Tp>
330 typename __gnu_cxx::__enable_if<!std::__are_same<_Up, bool>::__value,
333 { emplace_back(std::move(__x)); }
335 template<typename... _Args>
337 emplace_back(_Args&&... __args)
339 bool __realloc = _M_requires_reallocation(this->size() + 1);
340 _Base::emplace_back(std::forward<_Args>(__args)...);
342 this->_M_invalidate_all();
343 _M_update_guaranteed_capacity();
350 __glibcxx_check_nonempty();
351 iterator __victim = end() - 1;
352 __victim._M_invalidate();
356 #ifdef __GXX_EXPERIMENTAL_CXX0X__
357 template<typename... _Args>
359 emplace(iterator __position, _Args&&... __args)
361 __glibcxx_check_insert(__position);
362 bool __realloc = _M_requires_reallocation(this->size() + 1);
363 difference_type __offset = __position - begin();
364 typename _Base::iterator __res = _Base::emplace(__position.base(),
365 std::forward<_Args>(__args)...);
367 this->_M_invalidate_all();
369 this->_M_invalidate_if(_After_nth(__offset, _M_base().begin()));
370 _M_update_guaranteed_capacity();
371 return iterator(__res, this);
376 insert(iterator __position, const _Tp& __x)
378 __glibcxx_check_insert(__position);
379 bool __realloc = _M_requires_reallocation(this->size() + 1);
380 difference_type __offset = __position - begin();
381 typename _Base::iterator __res = _Base::insert(__position.base(),__x);
383 this->_M_invalidate_all();
385 this->_M_invalidate_if(_After_nth(__offset, _M_base().begin()));
386 _M_update_guaranteed_capacity();
387 return iterator(__res, this);
390 #ifdef __GXX_EXPERIMENTAL_CXX0X__
391 template<typename _Up = _Tp>
392 typename __gnu_cxx::__enable_if<!std::__are_same<_Up, bool>::__value,
394 insert(iterator __position, _Tp&& __x)
395 { return emplace(__position, std::move(__x)); }
398 insert(iterator __position, initializer_list<value_type> __l)
399 { this->insert(__position, __l.begin(), __l.end()); }
403 insert(iterator __position, size_type __n, const _Tp& __x)
405 __glibcxx_check_insert(__position);
406 bool __realloc = _M_requires_reallocation(this->size() + __n);
407 difference_type __offset = __position - begin();
408 _Base::insert(__position.base(), __n, __x);
410 this->_M_invalidate_all();
412 this->_M_invalidate_if(_After_nth(__offset, _M_base().begin()));
413 _M_update_guaranteed_capacity();
416 template<class _InputIterator>
418 insert(iterator __position,
419 _InputIterator __first, _InputIterator __last)
421 __glibcxx_check_insert_range(__position, __first, __last);
423 /* Hard to guess if invalidation will occur, because __last
424 - __first can't be calculated in all cases, so we just
425 punt here by checking if it did occur. */
426 typename _Base::iterator __old_begin = _M_base().begin();
427 difference_type __offset = __position - begin();
428 _Base::insert(__position.base(), __first, __last);
430 if (_M_base().begin() != __old_begin)
431 this->_M_invalidate_all();
433 this->_M_invalidate_if(_After_nth(__offset, _M_base().begin()));
434 _M_update_guaranteed_capacity();
438 erase(iterator __position)
440 __glibcxx_check_erase(__position);
441 difference_type __offset = __position - begin();
442 typename _Base::iterator __res = _Base::erase(__position.base());
443 this->_M_invalidate_if(_After_nth(__offset, _M_base().begin()));
444 return iterator(__res, this);
448 erase(iterator __first, iterator __last)
450 // _GLIBCXX_RESOLVE_LIB_DEFECTS
451 // 151. can't currently clear() empty container
452 __glibcxx_check_erase_range(__first, __last);
454 difference_type __offset = __first - begin();
455 typename _Base::iterator __res = _Base::erase(__first.base(),
457 this->_M_invalidate_if(_After_nth(__offset, _M_base().begin()));
458 return iterator(__res, this);
466 std::swap(_M_guaranteed_capacity, __x._M_guaranteed_capacity);
473 this->_M_invalidate_all();
474 _M_guaranteed_capacity = 0;
478 _M_base() { return *this; }
481 _M_base() const { return *this; }
484 size_type _M_guaranteed_capacity;
487 _M_requires_reallocation(size_type __elements)
488 { return __elements > this->capacity(); }
491 _M_update_guaranteed_capacity()
493 if (this->size() > _M_guaranteed_capacity)
494 _M_guaranteed_capacity = this->size();
498 template<typename _Tp, typename _Alloc>
500 operator==(const vector<_Tp, _Alloc>& __lhs,
501 const vector<_Tp, _Alloc>& __rhs)
502 { return __lhs._M_base() == __rhs._M_base(); }
504 template<typename _Tp, typename _Alloc>
506 operator!=(const vector<_Tp, _Alloc>& __lhs,
507 const vector<_Tp, _Alloc>& __rhs)
508 { return __lhs._M_base() != __rhs._M_base(); }
510 template<typename _Tp, typename _Alloc>
512 operator<(const vector<_Tp, _Alloc>& __lhs,
513 const vector<_Tp, _Alloc>& __rhs)
514 { return __lhs._M_base() < __rhs._M_base(); }
516 template<typename _Tp, typename _Alloc>
518 operator<=(const vector<_Tp, _Alloc>& __lhs,
519 const vector<_Tp, _Alloc>& __rhs)
520 { return __lhs._M_base() <= __rhs._M_base(); }
522 template<typename _Tp, typename _Alloc>
524 operator>=(const vector<_Tp, _Alloc>& __lhs,
525 const vector<_Tp, _Alloc>& __rhs)
526 { return __lhs._M_base() >= __rhs._M_base(); }
528 template<typename _Tp, typename _Alloc>
530 operator>(const vector<_Tp, _Alloc>& __lhs,
531 const vector<_Tp, _Alloc>& __rhs)
532 { return __lhs._M_base() > __rhs._M_base(); }
534 template<typename _Tp, typename _Alloc>
536 swap(vector<_Tp, _Alloc>& __lhs, vector<_Tp, _Alloc>& __rhs)
537 { __lhs.swap(__rhs); }
539 } // namespace __debug
541 #ifdef __GXX_EXPERIMENTAL_CXX0X__
543 /// std::hash specialization for vector<bool>.
544 template<typename _Alloc>
545 struct hash<__debug::vector<bool, _Alloc>>
546 : public std::unary_function<__debug::vector<bool, _Alloc>, size_t>
549 operator()(const __debug::vector<bool, _Alloc>& __b) const
550 { return std::hash<_GLIBCXX_STD_D::vector<bool, _Alloc>>()