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();
240 #ifdef __GXX_EXPERIMENTAL_CXX0X__
241 using _Base::shrink_to_fit;
247 #ifdef _GLIBCXX_DEBUG_PEDANTIC
248 return _M_guaranteed_capacity;
250 return _Base::capacity();
257 reserve(size_type __n)
259 bool __realloc = _M_requires_reallocation(__n);
261 if (__n > _M_guaranteed_capacity)
262 _M_guaranteed_capacity = __n;
264 this->_M_invalidate_all();
269 operator[](size_type __n)
271 __glibcxx_check_subscript(__n);
272 return _M_base()[__n];
276 operator[](size_type __n) const
278 __glibcxx_check_subscript(__n);
279 return _M_base()[__n];
287 __glibcxx_check_nonempty();
288 return _Base::front();
294 __glibcxx_check_nonempty();
295 return _Base::front();
301 __glibcxx_check_nonempty();
302 return _Base::back();
308 __glibcxx_check_nonempty();
309 return _Base::back();
312 // _GLIBCXX_RESOLVE_LIB_DEFECTS
313 // DR 464. Suggestion for new member functions in standard containers.
316 // 23.2.4.3 modifiers:
318 push_back(const _Tp& __x)
320 bool __realloc = _M_requires_reallocation(this->size() + 1);
321 _Base::push_back(__x);
323 this->_M_invalidate_all();
324 _M_update_guaranteed_capacity();
327 #ifdef __GXX_EXPERIMENTAL_CXX0X__
328 template<typename _Up = _Tp>
329 typename __gnu_cxx::__enable_if<!std::__are_same<_Up, bool>::__value,
332 { emplace_back(std::move(__x)); }
334 template<typename... _Args>
336 emplace_back(_Args&&... __args)
338 bool __realloc = _M_requires_reallocation(this->size() + 1);
339 _Base::emplace_back(std::forward<_Args>(__args)...);
341 this->_M_invalidate_all();
342 _M_update_guaranteed_capacity();
349 __glibcxx_check_nonempty();
350 iterator __victim = end() - 1;
351 __victim._M_invalidate();
355 #ifdef __GXX_EXPERIMENTAL_CXX0X__
356 template<typename... _Args>
358 emplace(iterator __position, _Args&&... __args)
360 __glibcxx_check_insert(__position);
361 bool __realloc = _M_requires_reallocation(this->size() + 1);
362 difference_type __offset = __position - begin();
363 typename _Base::iterator __res = _Base::emplace(__position.base(),
364 std::forward<_Args>(__args)...);
366 this->_M_invalidate_all();
368 this->_M_invalidate_if(_After_nth(__offset, _M_base().begin()));
369 _M_update_guaranteed_capacity();
370 return iterator(__res, this);
375 insert(iterator __position, const _Tp& __x)
377 __glibcxx_check_insert(__position);
378 bool __realloc = _M_requires_reallocation(this->size() + 1);
379 difference_type __offset = __position - begin();
380 typename _Base::iterator __res = _Base::insert(__position.base(),__x);
382 this->_M_invalidate_all();
384 this->_M_invalidate_if(_After_nth(__offset, _M_base().begin()));
385 _M_update_guaranteed_capacity();
386 return iterator(__res, this);
389 #ifdef __GXX_EXPERIMENTAL_CXX0X__
390 template<typename _Up = _Tp>
391 typename __gnu_cxx::__enable_if<!std::__are_same<_Up, bool>::__value,
393 insert(iterator __position, _Tp&& __x)
394 { return emplace(__position, std::move(__x)); }
397 insert(iterator __position, initializer_list<value_type> __l)
398 { this->insert(__position, __l.begin(), __l.end()); }
402 insert(iterator __position, size_type __n, const _Tp& __x)
404 __glibcxx_check_insert(__position);
405 bool __realloc = _M_requires_reallocation(this->size() + __n);
406 difference_type __offset = __position - begin();
407 _Base::insert(__position.base(), __n, __x);
409 this->_M_invalidate_all();
411 this->_M_invalidate_if(_After_nth(__offset, _M_base().begin()));
412 _M_update_guaranteed_capacity();
415 template<class _InputIterator>
417 insert(iterator __position,
418 _InputIterator __first, _InputIterator __last)
420 __glibcxx_check_insert_range(__position, __first, __last);
422 /* Hard to guess if invalidation will occur, because __last
423 - __first can't be calculated in all cases, so we just
424 punt here by checking if it did occur. */
425 typename _Base::iterator __old_begin = _M_base().begin();
426 difference_type __offset = __position - begin();
427 _Base::insert(__position.base(), __first, __last);
429 if (_M_base().begin() != __old_begin)
430 this->_M_invalidate_all();
432 this->_M_invalidate_if(_After_nth(__offset, _M_base().begin()));
433 _M_update_guaranteed_capacity();
437 erase(iterator __position)
439 __glibcxx_check_erase(__position);
440 difference_type __offset = __position - begin();
441 typename _Base::iterator __res = _Base::erase(__position.base());
442 this->_M_invalidate_if(_After_nth(__offset, _M_base().begin()));
443 return iterator(__res, this);
447 erase(iterator __first, iterator __last)
449 // _GLIBCXX_RESOLVE_LIB_DEFECTS
450 // 151. can't currently clear() empty container
451 __glibcxx_check_erase_range(__first, __last);
453 difference_type __offset = __first - begin();
454 typename _Base::iterator __res = _Base::erase(__first.base(),
456 this->_M_invalidate_if(_After_nth(__offset, _M_base().begin()));
457 return iterator(__res, this);
465 std::swap(_M_guaranteed_capacity, __x._M_guaranteed_capacity);
472 this->_M_invalidate_all();
473 _M_guaranteed_capacity = 0;
477 _M_base() { return *this; }
480 _M_base() const { return *this; }
483 size_type _M_guaranteed_capacity;
486 _M_requires_reallocation(size_type __elements)
487 { return __elements > this->capacity(); }
490 _M_update_guaranteed_capacity()
492 if (this->size() > _M_guaranteed_capacity)
493 _M_guaranteed_capacity = this->size();
497 template<typename _Tp, typename _Alloc>
499 operator==(const vector<_Tp, _Alloc>& __lhs,
500 const vector<_Tp, _Alloc>& __rhs)
501 { return __lhs._M_base() == __rhs._M_base(); }
503 template<typename _Tp, typename _Alloc>
505 operator!=(const vector<_Tp, _Alloc>& __lhs,
506 const vector<_Tp, _Alloc>& __rhs)
507 { return __lhs._M_base() != __rhs._M_base(); }
509 template<typename _Tp, typename _Alloc>
511 operator<(const vector<_Tp, _Alloc>& __lhs,
512 const vector<_Tp, _Alloc>& __rhs)
513 { return __lhs._M_base() < __rhs._M_base(); }
515 template<typename _Tp, typename _Alloc>
517 operator<=(const vector<_Tp, _Alloc>& __lhs,
518 const vector<_Tp, _Alloc>& __rhs)
519 { return __lhs._M_base() <= __rhs._M_base(); }
521 template<typename _Tp, typename _Alloc>
523 operator>=(const vector<_Tp, _Alloc>& __lhs,
524 const vector<_Tp, _Alloc>& __rhs)
525 { return __lhs._M_base() >= __rhs._M_base(); }
527 template<typename _Tp, typename _Alloc>
529 operator>(const vector<_Tp, _Alloc>& __lhs,
530 const vector<_Tp, _Alloc>& __rhs)
531 { return __lhs._M_base() > __rhs._M_base(); }
533 template<typename _Tp, typename _Alloc>
535 swap(vector<_Tp, _Alloc>& __lhs, vector<_Tp, _Alloc>& __rhs)
536 { __lhs.swap(__rhs); }
538 } // namespace __debug