1 // Debugging vector implementation -*- C++ -*-
3 // Copyright (C) 2003, 2004, 2005, 2006, 2007, 2008
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 2, 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 // You should have received a copy of the GNU General Public License along
18 // with this library; see the file COPYING. If not, write to the Free
19 // Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
22 // As a special exception, you may use this file as part of a free software
23 // library without restriction. Specifically, if other files instantiate
24 // templates or use macros or inline functions from this file, or you compile
25 // this file and link it with other files to produce an executable, this
26 // file does not by itself cause the resulting executable to be covered by
27 // the GNU General Public License. This exception does not however
28 // invalidate any other reasons why the executable file might be covered by
29 // the GNU General Public License.
31 /** @file debug/vector
32 * This file is a GNU debug extension to the Standard C++ Library.
35 #ifndef _GLIBCXX_DEBUG_VECTOR
36 #define _GLIBCXX_DEBUG_VECTOR 1
40 #include <debug/safe_sequence.h>
41 #include <debug/safe_iterator.h>
47 template<typename _Tp,
48 typename _Allocator = std::allocator<_Tp> >
50 : public _GLIBCXX_STD_D::vector<_Tp, _Allocator>,
51 public __gnu_debug::_Safe_sequence<vector<_Tp, _Allocator> >
53 typedef _GLIBCXX_STD_D::vector<_Tp, _Allocator> _Base;
54 typedef __gnu_debug::_Safe_sequence<vector> _Safe_base;
56 typedef typename _Base::const_iterator _Base_const_iterator;
57 typedef __gnu_debug::_After_nth_from<_Base_const_iterator> _After_nth;
60 typedef typename _Base::reference reference;
61 typedef typename _Base::const_reference const_reference;
63 typedef __gnu_debug::_Safe_iterator<typename _Base::iterator,vector>
65 typedef __gnu_debug::_Safe_iterator<typename _Base::const_iterator,vector>
68 typedef typename _Base::size_type size_type;
69 typedef typename _Base::difference_type difference_type;
71 typedef _Tp value_type;
72 typedef _Allocator allocator_type;
73 typedef typename _Base::pointer pointer;
74 typedef typename _Base::const_pointer const_pointer;
75 typedef std::reverse_iterator<iterator> reverse_iterator;
76 typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
78 // 23.2.4.1 construct/copy/destroy:
79 explicit vector(const _Allocator& __a = _Allocator())
80 : _Base(__a), _M_guaranteed_capacity(0) { }
82 explicit vector(size_type __n, const _Tp& __value = _Tp(),
83 const _Allocator& __a = _Allocator())
84 : _Base(__n, __value, __a), _M_guaranteed_capacity(__n) { }
86 template<class _InputIterator>
87 vector(_InputIterator __first, _InputIterator __last,
88 const _Allocator& __a = _Allocator())
89 : _Base(__gnu_debug::__check_valid_range(__first, __last),
91 _M_guaranteed_capacity(0)
92 { _M_update_guaranteed_capacity(); }
94 vector(const vector& __x)
95 : _Base(__x), _Safe_base(), _M_guaranteed_capacity(__x.size()) { }
97 /// Construction from a release-mode vector
98 vector(const _Base& __x)
99 : _Base(__x), _Safe_base(), _M_guaranteed_capacity(__x.size()) { }
101 #ifdef __GXX_EXPERIMENTAL_CXX0X__
103 : _Base(std::forward<vector>(__x)), _Safe_base(),
104 _M_guaranteed_capacity(this->size())
107 __x._M_guaranteed_capacity = 0;
114 operator=(const vector& __x)
116 static_cast<_Base&>(*this) = __x;
117 this->_M_invalidate_all();
118 _M_update_guaranteed_capacity();
122 #ifdef __GXX_EXPERIMENTAL_CXX0X__
124 operator=(vector&& __x)
133 template<typename _InputIterator>
135 assign(_InputIterator __first, _InputIterator __last)
137 __glibcxx_check_valid_range(__first, __last);
138 _Base::assign(__first, __last);
139 this->_M_invalidate_all();
140 _M_update_guaranteed_capacity();
144 assign(size_type __n, const _Tp& __u)
146 _Base::assign(__n, __u);
147 this->_M_invalidate_all();
148 _M_update_guaranteed_capacity();
151 using _Base::get_allocator;
156 { return iterator(_Base::begin(), this); }
160 { return const_iterator(_Base::begin(), this); }
164 { return iterator(_Base::end(), this); }
168 { return const_iterator(_Base::end(), this); }
172 { return reverse_iterator(end()); }
174 const_reverse_iterator
176 { return const_reverse_iterator(end()); }
180 { return reverse_iterator(begin()); }
182 const_reverse_iterator
184 { return const_reverse_iterator(begin()); }
186 #ifdef __GXX_EXPERIMENTAL_CXX0X__
189 { return const_iterator(_Base::begin(), this); }
193 { return const_iterator(_Base::end(), this); }
195 const_reverse_iterator
197 { return const_reverse_iterator(end()); }
199 const_reverse_iterator
201 { return const_reverse_iterator(begin()); }
204 // 23.2.4.2 capacity:
206 using _Base::max_size;
209 resize(size_type __sz, _Tp __c = _Tp())
211 bool __realloc = _M_requires_reallocation(__sz);
212 if (__sz < this->size())
213 this->_M_invalidate_if(_After_nth(__sz, _M_base().begin()));
214 _Base::resize(__sz, __c);
216 this->_M_invalidate_all();
222 #ifdef _GLIBCXX_DEBUG_PEDANTIC
223 return _M_guaranteed_capacity;
225 return _Base::capacity();
232 reserve(size_type __n)
234 bool __realloc = _M_requires_reallocation(__n);
236 if (__n > _M_guaranteed_capacity)
237 _M_guaranteed_capacity = __n;
239 this->_M_invalidate_all();
244 operator[](size_type __n)
246 __glibcxx_check_subscript(__n);
247 return _M_base()[__n];
251 operator[](size_type __n) const
253 __glibcxx_check_subscript(__n);
254 return _M_base()[__n];
262 __glibcxx_check_nonempty();
263 return _Base::front();
269 __glibcxx_check_nonempty();
270 return _Base::front();
276 __glibcxx_check_nonempty();
277 return _Base::back();
283 __glibcxx_check_nonempty();
284 return _Base::back();
287 // _GLIBCXX_RESOLVE_LIB_DEFECTS
288 // DR 464. Suggestion for new member functions in standard containers.
291 // 23.2.4.3 modifiers:
292 #ifndef __GXX_EXPERIMENTAL_CXX0X__
294 push_back(const _Tp& __x)
296 bool __realloc = _M_requires_reallocation(this->size() + 1);
297 _Base::push_back(__x);
299 this->_M_invalidate_all();
300 _M_update_guaranteed_capacity();
303 template<typename... _Args>
305 push_back(_Args&&... __args)
307 bool __realloc = _M_requires_reallocation(this->size() + 1);
308 _Base::push_back(std::forward<_Args>(__args)...);
310 this->_M_invalidate_all();
311 _M_update_guaranteed_capacity();
318 __glibcxx_check_nonempty();
319 iterator __victim = end() - 1;
320 __victim._M_invalidate();
324 #ifdef __GXX_EXPERIMENTAL_CXX0X__
325 template<typename... _Args>
327 emplace(iterator __position, _Args&&... __args)
329 __glibcxx_check_insert(__position);
330 bool __realloc = _M_requires_reallocation(this->size() + 1);
331 difference_type __offset = __position - begin();
332 typename _Base::iterator __res = _Base::emplace(__position.base(),
333 std::forward<_Args>(__args)...);
335 this->_M_invalidate_all();
337 this->_M_invalidate_if(_After_nth(__offset, _M_base().begin()));
338 _M_update_guaranteed_capacity();
339 return iterator(__res, this);
344 insert(iterator __position, const _Tp& __x)
346 __glibcxx_check_insert(__position);
347 bool __realloc = _M_requires_reallocation(this->size() + 1);
348 difference_type __offset = __position - begin();
349 typename _Base::iterator __res = _Base::insert(__position.base(),__x);
351 this->_M_invalidate_all();
353 this->_M_invalidate_if(_After_nth(__offset, _M_base().begin()));
354 _M_update_guaranteed_capacity();
355 return iterator(__res, this);
358 #ifdef __GXX_EXPERIMENTAL_CXX0X__
360 insert(iterator __position, _Tp&& __x)
361 { return emplace(__position, std::move(__x)); }
365 insert(iterator __position, size_type __n, const _Tp& __x)
367 __glibcxx_check_insert(__position);
368 bool __realloc = _M_requires_reallocation(this->size() + __n);
369 difference_type __offset = __position - begin();
370 _Base::insert(__position.base(), __n, __x);
372 this->_M_invalidate_all();
374 this->_M_invalidate_if(_After_nth(__offset, _M_base().begin()));
375 _M_update_guaranteed_capacity();
378 template<class _InputIterator>
380 insert(iterator __position,
381 _InputIterator __first, _InputIterator __last)
383 __glibcxx_check_insert_range(__position, __first, __last);
385 /* Hard to guess if invalidation will occur, because __last
386 - __first can't be calculated in all cases, so we just
387 punt here by checking if it did occur. */
388 typename _Base::iterator __old_begin = _M_base().begin();
389 difference_type __offset = __position - begin();
390 _Base::insert(__position.base(), __first, __last);
392 if (_M_base().begin() != __old_begin)
393 this->_M_invalidate_all();
395 this->_M_invalidate_if(_After_nth(__offset, _M_base().begin()));
396 _M_update_guaranteed_capacity();
400 erase(iterator __position)
402 __glibcxx_check_erase(__position);
403 difference_type __offset = __position - begin();
404 typename _Base::iterator __res = _Base::erase(__position.base());
405 this->_M_invalidate_if(_After_nth(__offset, _M_base().begin()));
406 return iterator(__res, this);
410 erase(iterator __first, iterator __last)
412 // _GLIBCXX_RESOLVE_LIB_DEFECTS
413 // 151. can't currently clear() empty container
414 __glibcxx_check_erase_range(__first, __last);
416 difference_type __offset = __first - begin();
417 typename _Base::iterator __res = _Base::erase(__first.base(),
419 this->_M_invalidate_if(_After_nth(__offset, _M_base().begin()));
420 return iterator(__res, this);
424 #ifdef __GXX_EXPERIMENTAL_CXX0X__
432 std::swap(_M_guaranteed_capacity, __x._M_guaranteed_capacity);
439 this->_M_invalidate_all();
440 _M_guaranteed_capacity = 0;
444 _M_base() { return *this; }
447 _M_base() const { return *this; }
450 size_type _M_guaranteed_capacity;
453 _M_requires_reallocation(size_type __elements)
454 { return __elements > this->capacity(); }
457 _M_update_guaranteed_capacity()
459 if (this->size() > _M_guaranteed_capacity)
460 _M_guaranteed_capacity = this->size();
464 template<typename _Tp, typename _Alloc>
466 operator==(const vector<_Tp, _Alloc>& __lhs,
467 const vector<_Tp, _Alloc>& __rhs)
468 { return __lhs._M_base() == __rhs._M_base(); }
470 template<typename _Tp, typename _Alloc>
472 operator!=(const vector<_Tp, _Alloc>& __lhs,
473 const vector<_Tp, _Alloc>& __rhs)
474 { return __lhs._M_base() != __rhs._M_base(); }
476 template<typename _Tp, typename _Alloc>
478 operator<(const vector<_Tp, _Alloc>& __lhs,
479 const vector<_Tp, _Alloc>& __rhs)
480 { return __lhs._M_base() < __rhs._M_base(); }
482 template<typename _Tp, typename _Alloc>
484 operator<=(const vector<_Tp, _Alloc>& __lhs,
485 const vector<_Tp, _Alloc>& __rhs)
486 { return __lhs._M_base() <= __rhs._M_base(); }
488 template<typename _Tp, typename _Alloc>
490 operator>=(const vector<_Tp, _Alloc>& __lhs,
491 const vector<_Tp, _Alloc>& __rhs)
492 { return __lhs._M_base() >= __rhs._M_base(); }
494 template<typename _Tp, typename _Alloc>
496 operator>(const vector<_Tp, _Alloc>& __lhs,
497 const vector<_Tp, _Alloc>& __rhs)
498 { return __lhs._M_base() > __rhs._M_base(); }
500 template<typename _Tp, typename _Alloc>
502 swap(vector<_Tp, _Alloc>& __lhs, vector<_Tp, _Alloc>& __rhs)
503 { __lhs.swap(__rhs); }
505 #ifdef __GXX_EXPERIMENTAL_CXX0X__
506 template<typename _Tp, typename _Alloc>
508 swap(vector<_Tp, _Alloc>&& __lhs, vector<_Tp, _Alloc>& __rhs)
509 { __lhs.swap(__rhs); }
511 template<typename _Tp, typename _Alloc>
513 swap(vector<_Tp, _Alloc>& __lhs, vector<_Tp, _Alloc>&& __rhs)
514 { __lhs.swap(__rhs); }
517 } // namespace __debug