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)
137 operator=(initializer_list<value_type> __l)
139 static_cast<_Base&>(*this) = __l;
140 this->_M_invalidate_all();
141 _M_update_guaranteed_capacity();
146 template<typename _InputIterator>
148 assign(_InputIterator __first, _InputIterator __last)
150 __glibcxx_check_valid_range(__first, __last);
151 _Base::assign(__first, __last);
152 this->_M_invalidate_all();
153 _M_update_guaranteed_capacity();
157 assign(size_type __n, const _Tp& __u)
159 _Base::assign(__n, __u);
160 this->_M_invalidate_all();
161 _M_update_guaranteed_capacity();
164 #ifdef __GXX_EXPERIMENTAL_CXX0X__
166 assign(initializer_list<value_type> __l)
169 this->_M_invalidate_all();
170 _M_update_guaranteed_capacity();
174 using _Base::get_allocator;
179 { return iterator(_Base::begin(), this); }
183 { return const_iterator(_Base::begin(), this); }
187 { return iterator(_Base::end(), this); }
191 { return const_iterator(_Base::end(), this); }
195 { return reverse_iterator(end()); }
197 const_reverse_iterator
199 { return const_reverse_iterator(end()); }
203 { return reverse_iterator(begin()); }
205 const_reverse_iterator
207 { return const_reverse_iterator(begin()); }
209 #ifdef __GXX_EXPERIMENTAL_CXX0X__
212 { return const_iterator(_Base::begin(), this); }
216 { return const_iterator(_Base::end(), this); }
218 const_reverse_iterator
220 { return const_reverse_iterator(end()); }
222 const_reverse_iterator
224 { return const_reverse_iterator(begin()); }
227 // 23.2.4.2 capacity:
229 using _Base::max_size;
232 resize(size_type __sz, _Tp __c = _Tp())
234 bool __realloc = _M_requires_reallocation(__sz);
235 if (__sz < this->size())
236 this->_M_invalidate_if(_After_nth(__sz, _M_base().begin()));
237 _Base::resize(__sz, __c);
239 this->_M_invalidate_all();
242 #ifdef __GXX_EXPERIMENTAL_CXX0X__
243 using _Base::shrink_to_fit;
249 #ifdef _GLIBCXX_DEBUG_PEDANTIC
250 return _M_guaranteed_capacity;
252 return _Base::capacity();
259 reserve(size_type __n)
261 bool __realloc = _M_requires_reallocation(__n);
263 if (__n > _M_guaranteed_capacity)
264 _M_guaranteed_capacity = __n;
266 this->_M_invalidate_all();
271 operator[](size_type __n)
273 __glibcxx_check_subscript(__n);
274 return _M_base()[__n];
278 operator[](size_type __n) const
280 __glibcxx_check_subscript(__n);
281 return _M_base()[__n];
289 __glibcxx_check_nonempty();
290 return _Base::front();
296 __glibcxx_check_nonempty();
297 return _Base::front();
303 __glibcxx_check_nonempty();
304 return _Base::back();
310 __glibcxx_check_nonempty();
311 return _Base::back();
314 // _GLIBCXX_RESOLVE_LIB_DEFECTS
315 // DR 464. Suggestion for new member functions in standard containers.
318 // 23.2.4.3 modifiers:
320 push_back(const _Tp& __x)
322 bool __realloc = _M_requires_reallocation(this->size() + 1);
323 _Base::push_back(__x);
325 this->_M_invalidate_all();
326 _M_update_guaranteed_capacity();
329 #ifdef __GXX_EXPERIMENTAL_CXX0X__
330 template<typename _Up = _Tp>
331 typename __gnu_cxx::__enable_if<!std::__are_same<_Up, bool>::__value,
334 { emplace_back(std::move(__x)); }
336 template<typename... _Args>
338 emplace_back(_Args&&... __args)
340 bool __realloc = _M_requires_reallocation(this->size() + 1);
341 _Base::emplace_back(std::forward<_Args>(__args)...);
343 this->_M_invalidate_all();
344 _M_update_guaranteed_capacity();
351 __glibcxx_check_nonempty();
352 iterator __victim = end() - 1;
353 __victim._M_invalidate();
357 #ifdef __GXX_EXPERIMENTAL_CXX0X__
358 template<typename... _Args>
360 emplace(iterator __position, _Args&&... __args)
362 __glibcxx_check_insert(__position);
363 bool __realloc = _M_requires_reallocation(this->size() + 1);
364 difference_type __offset = __position - begin();
365 typename _Base::iterator __res = _Base::emplace(__position.base(),
366 std::forward<_Args>(__args)...);
368 this->_M_invalidate_all();
370 this->_M_invalidate_if(_After_nth(__offset, _M_base().begin()));
371 _M_update_guaranteed_capacity();
372 return iterator(__res, this);
377 insert(iterator __position, const _Tp& __x)
379 __glibcxx_check_insert(__position);
380 bool __realloc = _M_requires_reallocation(this->size() + 1);
381 difference_type __offset = __position - begin();
382 typename _Base::iterator __res = _Base::insert(__position.base(),__x);
384 this->_M_invalidate_all();
386 this->_M_invalidate_if(_After_nth(__offset, _M_base().begin()));
387 _M_update_guaranteed_capacity();
388 return iterator(__res, this);
391 #ifdef __GXX_EXPERIMENTAL_CXX0X__
392 template<typename _Up = _Tp>
393 typename __gnu_cxx::__enable_if<!std::__are_same<_Up, bool>::__value,
395 insert(iterator __position, _Tp&& __x)
396 { return emplace(__position, std::move(__x)); }
399 insert(iterator __position, initializer_list<value_type> __l)
400 { this->insert(__position, __l.begin(), __l.end()); }
404 insert(iterator __position, size_type __n, const _Tp& __x)
406 __glibcxx_check_insert(__position);
407 bool __realloc = _M_requires_reallocation(this->size() + __n);
408 difference_type __offset = __position - begin();
409 _Base::insert(__position.base(), __n, __x);
411 this->_M_invalidate_all();
413 this->_M_invalidate_if(_After_nth(__offset, _M_base().begin()));
414 _M_update_guaranteed_capacity();
417 template<class _InputIterator>
419 insert(iterator __position,
420 _InputIterator __first, _InputIterator __last)
422 __glibcxx_check_insert_range(__position, __first, __last);
424 /* Hard to guess if invalidation will occur, because __last
425 - __first can't be calculated in all cases, so we just
426 punt here by checking if it did occur. */
427 typename _Base::iterator __old_begin = _M_base().begin();
428 difference_type __offset = __position - begin();
429 _Base::insert(__position.base(), __first, __last);
431 if (_M_base().begin() != __old_begin)
432 this->_M_invalidate_all();
434 this->_M_invalidate_if(_After_nth(__offset, _M_base().begin()));
435 _M_update_guaranteed_capacity();
439 erase(iterator __position)
441 __glibcxx_check_erase(__position);
442 difference_type __offset = __position - begin();
443 typename _Base::iterator __res = _Base::erase(__position.base());
444 this->_M_invalidate_if(_After_nth(__offset, _M_base().begin()));
445 return iterator(__res, this);
449 erase(iterator __first, iterator __last)
451 // _GLIBCXX_RESOLVE_LIB_DEFECTS
452 // 151. can't currently clear() empty container
453 __glibcxx_check_erase_range(__first, __last);
455 difference_type __offset = __first - begin();
456 typename _Base::iterator __res = _Base::erase(__first.base(),
458 this->_M_invalidate_if(_After_nth(__offset, _M_base().begin()));
459 return iterator(__res, this);
467 std::swap(_M_guaranteed_capacity, __x._M_guaranteed_capacity);
474 this->_M_invalidate_all();
475 _M_guaranteed_capacity = 0;
479 _M_base() { return *this; }
482 _M_base() const { return *this; }
485 size_type _M_guaranteed_capacity;
488 _M_requires_reallocation(size_type __elements)
489 { return __elements > this->capacity(); }
492 _M_update_guaranteed_capacity()
494 if (this->size() > _M_guaranteed_capacity)
495 _M_guaranteed_capacity = this->size();
499 template<typename _Tp, typename _Alloc>
501 operator==(const vector<_Tp, _Alloc>& __lhs,
502 const vector<_Tp, _Alloc>& __rhs)
503 { return __lhs._M_base() == __rhs._M_base(); }
505 template<typename _Tp, typename _Alloc>
507 operator!=(const vector<_Tp, _Alloc>& __lhs,
508 const vector<_Tp, _Alloc>& __rhs)
509 { return __lhs._M_base() != __rhs._M_base(); }
511 template<typename _Tp, typename _Alloc>
513 operator<(const vector<_Tp, _Alloc>& __lhs,
514 const vector<_Tp, _Alloc>& __rhs)
515 { return __lhs._M_base() < __rhs._M_base(); }
517 template<typename _Tp, typename _Alloc>
519 operator<=(const vector<_Tp, _Alloc>& __lhs,
520 const vector<_Tp, _Alloc>& __rhs)
521 { return __lhs._M_base() <= __rhs._M_base(); }
523 template<typename _Tp, typename _Alloc>
525 operator>=(const vector<_Tp, _Alloc>& __lhs,
526 const vector<_Tp, _Alloc>& __rhs)
527 { return __lhs._M_base() >= __rhs._M_base(); }
529 template<typename _Tp, typename _Alloc>
531 operator>(const vector<_Tp, _Alloc>& __lhs,
532 const vector<_Tp, _Alloc>& __rhs)
533 { return __lhs._M_base() > __rhs._M_base(); }
535 template<typename _Tp, typename _Alloc>
537 swap(vector<_Tp, _Alloc>& __lhs, vector<_Tp, _Alloc>& __rhs)
538 { __lhs.swap(__rhs); }
540 } // namespace __debug