1132720Skan// Debugging vector implementation -*- C++ -*-
2132720Skan
3169691Skan// Copyright (C) 2003, 2004, 2005
4132720Skan// Free Software Foundation, Inc.
5132720Skan//
6132720Skan// This file is part of the GNU ISO C++ Library.  This library is free
7132720Skan// software; you can redistribute it and/or modify it under the
8132720Skan// terms of the GNU General Public License as published by the
9132720Skan// Free Software Foundation; either version 2, or (at your option)
10132720Skan// any later version.
11132720Skan
12132720Skan// This library is distributed in the hope that it will be useful,
13132720Skan// but WITHOUT ANY WARRANTY; without even the implied warranty of
14132720Skan// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15132720Skan// GNU General Public License for more details.
16132720Skan
17132720Skan// You should have received a copy of the GNU General Public License along
18132720Skan// with this library; see the file COPYING.  If not, write to the Free
19169691Skan// Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
20132720Skan// USA.
21132720Skan
22132720Skan// As a special exception, you may use this file as part of a free software
23132720Skan// library without restriction.  Specifically, if other files instantiate
24132720Skan// templates or use macros or inline functions from this file, or you compile
25132720Skan// this file and link it with other files to produce an executable, this
26132720Skan// file does not by itself cause the resulting executable to be covered by
27132720Skan// the GNU General Public License.  This exception does not however
28132720Skan// invalidate any other reasons why the executable file might be covered by
29132720Skan// the GNU General Public License.
30132720Skan
31169691Skan/** @file debug/vector
32169691Skan *  This file is a GNU debug extension to the Standard C++ Library.
33169691Skan */
34169691Skan
35132720Skan#ifndef _GLIBCXX_DEBUG_VECTOR
36132720Skan#define _GLIBCXX_DEBUG_VECTOR 1
37132720Skan
38132720Skan#include <vector>
39169691Skan#include <utility>
40132720Skan#include <debug/safe_sequence.h>
41132720Skan#include <debug/safe_iterator.h>
42132720Skan
43169691Skannamespace std
44132720Skan{
45169691Skannamespace __debug
46169691Skan{
47132720Skan  template<typename _Tp,
48132720Skan	   typename _Allocator = std::allocator<_Tp> >
49132720Skan    class vector
50132720Skan    : public _GLIBCXX_STD::vector<_Tp, _Allocator>,
51132720Skan      public __gnu_debug::_Safe_sequence<vector<_Tp, _Allocator> >
52132720Skan    {
53132720Skan      typedef _GLIBCXX_STD::vector<_Tp, _Allocator> _Base;
54132720Skan      typedef __gnu_debug::_Safe_sequence<vector>              _Safe_base;
55132720Skan
56132720Skan      typedef typename _Base::const_iterator _Base_const_iterator;
57132720Skan      typedef __gnu_debug::_After_nth_from<_Base_const_iterator> _After_nth;
58132720Skan
59132720Skan    public:
60132720Skan      typedef typename _Base::reference             reference;
61132720Skan      typedef typename _Base::const_reference       const_reference;
62132720Skan
63132720Skan      typedef __gnu_debug::_Safe_iterator<typename _Base::iterator,vector>
64132720Skan      iterator;
65132720Skan      typedef __gnu_debug::_Safe_iterator<typename _Base::const_iterator,vector>
66132720Skan      const_iterator;
67132720Skan
68132720Skan      typedef typename _Base::size_type             size_type;
69132720Skan      typedef typename _Base::difference_type       difference_type;
70132720Skan
71132720Skan      typedef _Tp				    value_type;
72132720Skan      typedef _Allocator			    allocator_type;
73169691Skan      typedef typename _Base::pointer               pointer;
74169691Skan      typedef typename _Base::const_pointer         const_pointer;
75132720Skan      typedef std::reverse_iterator<iterator>       reverse_iterator;
76132720Skan      typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
77132720Skan
78132720Skan      // 23.2.4.1 construct/copy/destroy:
79132720Skan      explicit vector(const _Allocator& __a = _Allocator())
80132720Skan      : _Base(__a), _M_guaranteed_capacity(0) { }
81132720Skan
82132720Skan      explicit vector(size_type __n, const _Tp& __value = _Tp(),
83132720Skan		      const _Allocator& __a = _Allocator())
84132720Skan      : _Base(__n, __value, __a), _M_guaranteed_capacity(__n) { }
85132720Skan
86132720Skan      template<class _InputIterator>
87132720Skan        vector(_InputIterator __first, _InputIterator __last,
88132720Skan	       const _Allocator& __a = _Allocator())
89132720Skan	: _Base(__gnu_debug::__check_valid_range(__first, __last),
90132720Skan		__last, __a),
91132720Skan	  _M_guaranteed_capacity(0)
92132720Skan        { _M_update_guaranteed_capacity(); }
93132720Skan
94132720Skan      vector(const vector<_Tp,_Allocator>& __x)
95132720Skan      : _Base(__x), _Safe_base(), _M_guaranteed_capacity(__x.size()) { }
96132720Skan
97132720Skan      /// Construction from a release-mode vector
98132720Skan      vector(const _Base& __x)
99132720Skan      : _Base(__x), _Safe_base(), _M_guaranteed_capacity(__x.size()) { }
100132720Skan
101132720Skan      ~vector() { }
102132720Skan
103132720Skan      vector<_Tp,_Allocator>&
104132720Skan      operator=(const vector<_Tp,_Allocator>& __x)
105132720Skan      {
106132720Skan	static_cast<_Base&>(*this) = __x;
107132720Skan	this->_M_invalidate_all();
108132720Skan	_M_update_guaranteed_capacity();
109132720Skan	return *this;
110132720Skan      }
111132720Skan
112132720Skan      template<typename _InputIterator>
113132720Skan        void
114132720Skan        assign(_InputIterator __first, _InputIterator __last)
115132720Skan        {
116132720Skan	  __glibcxx_check_valid_range(__first, __last);
117132720Skan	  _Base::assign(__first, __last);
118132720Skan	  this->_M_invalidate_all();
119132720Skan	  _M_update_guaranteed_capacity();
120132720Skan	}
121132720Skan
122132720Skan      void
123132720Skan      assign(size_type __n, const _Tp& __u)
124132720Skan      {
125132720Skan	_Base::assign(__n, __u);
126132720Skan	this->_M_invalidate_all();
127132720Skan	_M_update_guaranteed_capacity();
128132720Skan      }
129132720Skan
130132720Skan      using _Base::get_allocator;
131132720Skan
132132720Skan      // iterators:
133132720Skan      iterator
134132720Skan      begin()
135132720Skan      { return iterator(_Base::begin(), this); }
136132720Skan
137132720Skan      const_iterator
138132720Skan      begin() const
139132720Skan      { return const_iterator(_Base::begin(), this); }
140132720Skan
141132720Skan      iterator
142132720Skan      end()
143132720Skan      { return iterator(_Base::end(), this); }
144132720Skan
145132720Skan      const_iterator
146132720Skan      end() const
147132720Skan      { return const_iterator(_Base::end(), this); }
148132720Skan
149132720Skan      reverse_iterator
150132720Skan      rbegin()
151132720Skan      { return reverse_iterator(end()); }
152132720Skan
153132720Skan      const_reverse_iterator
154132720Skan      rbegin() const
155132720Skan      { return const_reverse_iterator(end()); }
156132720Skan
157132720Skan      reverse_iterator
158132720Skan      rend()
159132720Skan      { return reverse_iterator(begin()); }
160132720Skan
161132720Skan      const_reverse_iterator
162132720Skan      rend() const
163132720Skan      { return const_reverse_iterator(begin()); }
164132720Skan
165132720Skan      // 23.2.4.2 capacity:
166132720Skan      using _Base::size;
167132720Skan      using _Base::max_size;
168132720Skan
169132720Skan      void
170132720Skan      resize(size_type __sz, _Tp __c = _Tp())
171132720Skan      {
172132720Skan	bool __realloc = _M_requires_reallocation(__sz);
173132720Skan	if (__sz < this->size())
174132720Skan	  this->_M_invalidate_if(_After_nth(__sz, _M_base().begin()));
175132720Skan	_Base::resize(__sz, __c);
176132720Skan	if (__realloc)
177132720Skan	  this->_M_invalidate_all();
178132720Skan      }
179132720Skan
180132720Skan      using _Base::capacity;
181132720Skan      using _Base::empty;
182132720Skan
183132720Skan      void
184132720Skan      reserve(size_type __n)
185132720Skan      {
186132720Skan	bool __realloc = _M_requires_reallocation(__n);
187132720Skan	_Base::reserve(__n);
188132720Skan	if (__n > _M_guaranteed_capacity)
189132720Skan	  _M_guaranteed_capacity = __n;
190132720Skan	if (__realloc)
191132720Skan	  this->_M_invalidate_all();
192132720Skan      }
193132720Skan
194132720Skan      // element access:
195132720Skan      reference
196132720Skan      operator[](size_type __n)
197132720Skan      {
198132720Skan	__glibcxx_check_subscript(__n);
199132720Skan	return _M_base()[__n];
200132720Skan      }
201132720Skan
202132720Skan      const_reference
203132720Skan      operator[](size_type __n) const
204132720Skan      {
205132720Skan	__glibcxx_check_subscript(__n);
206132720Skan	return _M_base()[__n];
207132720Skan      }
208132720Skan
209132720Skan      using _Base::at;
210132720Skan
211132720Skan      reference
212132720Skan      front()
213132720Skan      {
214132720Skan	__glibcxx_check_nonempty();
215132720Skan	return _Base::front();
216132720Skan      }
217132720Skan
218132720Skan      const_reference
219132720Skan      front() const
220132720Skan      {
221132720Skan	__glibcxx_check_nonempty();
222132720Skan	return _Base::front();
223132720Skan      }
224132720Skan
225132720Skan      reference
226132720Skan      back()
227132720Skan      {
228132720Skan	__glibcxx_check_nonempty();
229132720Skan	return _Base::back();
230132720Skan      }
231132720Skan
232132720Skan      const_reference
233132720Skan      back() const
234132720Skan      {
235132720Skan	__glibcxx_check_nonempty();
236132720Skan	return _Base::back();
237132720Skan      }
238132720Skan
239169691Skan      // _GLIBCXX_RESOLVE_LIB_DEFECTS
240169691Skan      // DR 464. Suggestion for new member functions in standard containers.
241169691Skan      using _Base::data;
242169691Skan
243132720Skan      // 23.2.4.3 modifiers:
244132720Skan      void
245132720Skan      push_back(const _Tp& __x)
246132720Skan      {
247132720Skan	bool __realloc = _M_requires_reallocation(this->size() + 1);
248132720Skan	_Base::push_back(__x);
249132720Skan	if (__realloc)
250132720Skan	  this->_M_invalidate_all();
251132720Skan	_M_update_guaranteed_capacity();
252132720Skan      }
253132720Skan
254132720Skan      void
255132720Skan      pop_back()
256132720Skan      {
257132720Skan	__glibcxx_check_nonempty();
258132720Skan	iterator __victim = end() - 1;
259132720Skan	__victim._M_invalidate();
260132720Skan	_Base::pop_back();
261132720Skan      }
262132720Skan
263132720Skan      iterator
264132720Skan      insert(iterator __position, const _Tp& __x)
265132720Skan      {
266132720Skan	__glibcxx_check_insert(__position);
267132720Skan	bool __realloc = _M_requires_reallocation(this->size() + 1);
268132720Skan	difference_type __offset = __position - begin();
269132720Skan	typename _Base::iterator __res = _Base::insert(__position.base(),__x);
270132720Skan	if (__realloc)
271132720Skan	  this->_M_invalidate_all();
272132720Skan	else
273132720Skan	  this->_M_invalidate_if(_After_nth(__offset, _M_base().begin()));
274132720Skan	_M_update_guaranteed_capacity();
275132720Skan	return iterator(__res, this);
276132720Skan      }
277132720Skan
278132720Skan      void
279132720Skan      insert(iterator __position, size_type __n, const _Tp& __x)
280132720Skan      {
281132720Skan	__glibcxx_check_insert(__position);
282132720Skan	bool __realloc = _M_requires_reallocation(this->size() + __n);
283132720Skan	difference_type __offset = __position - begin();
284132720Skan	_Base::insert(__position.base(), __n, __x);
285132720Skan	if (__realloc)
286132720Skan	  this->_M_invalidate_all();
287132720Skan	else
288132720Skan	  this->_M_invalidate_if(_After_nth(__offset, _M_base().begin()));
289132720Skan	_M_update_guaranteed_capacity();
290132720Skan      }
291132720Skan
292132720Skan      template<class _InputIterator>
293132720Skan        void
294132720Skan        insert(iterator __position,
295132720Skan	       _InputIterator __first, _InputIterator __last)
296132720Skan        {
297132720Skan	  __glibcxx_check_insert_range(__position, __first, __last);
298132720Skan
299132720Skan	  /* Hard to guess if invalidation will occur, because __last
300132720Skan	     - __first can't be calculated in all cases, so we just
301132720Skan	     punt here by checking if it did occur. */
302132720Skan	  typename _Base::iterator __old_begin = _M_base().begin();
303132720Skan	  difference_type __offset = __position - begin();
304132720Skan	  _Base::insert(__position.base(), __first, __last);
305132720Skan
306132720Skan	  if (_M_base().begin() != __old_begin)
307132720Skan	    this->_M_invalidate_all();
308132720Skan	  else
309132720Skan	    this->_M_invalidate_if(_After_nth(__offset, _M_base().begin()));
310132720Skan	  _M_update_guaranteed_capacity();
311132720Skan	}
312132720Skan
313132720Skan      iterator
314132720Skan      erase(iterator __position)
315132720Skan      {
316132720Skan	__glibcxx_check_erase(__position);
317132720Skan	difference_type __offset = __position - begin();
318132720Skan	typename _Base::iterator __res = _Base::erase(__position.base());
319132720Skan	this->_M_invalidate_if(_After_nth(__offset, _M_base().begin()));
320132720Skan	return iterator(__res, this);
321132720Skan      }
322132720Skan
323132720Skan      iterator
324132720Skan      erase(iterator __first, iterator __last)
325132720Skan      {
326132720Skan	// _GLIBCXX_RESOLVE_LIB_DEFECTS
327132720Skan	// 151. can't currently clear() empty container
328132720Skan	__glibcxx_check_erase_range(__first, __last);
329132720Skan
330132720Skan	difference_type __offset = __first - begin();
331132720Skan	typename _Base::iterator __res = _Base::erase(__first.base(),
332132720Skan							 __last.base());
333132720Skan	this->_M_invalidate_if(_After_nth(__offset, _M_base().begin()));
334132720Skan	return iterator(__res, this);
335132720Skan      }
336132720Skan
337132720Skan      void
338132720Skan      swap(vector<_Tp,_Allocator>& __x)
339132720Skan      {
340132720Skan	_Base::swap(__x);
341132720Skan	this->_M_swap(__x);
342132720Skan        std::swap(_M_guaranteed_capacity, __x._M_guaranteed_capacity);
343132720Skan      }
344132720Skan
345132720Skan      void
346132720Skan      clear()
347132720Skan      {
348132720Skan	_Base::clear();
349132720Skan	this->_M_invalidate_all();
350132720Skan        _M_guaranteed_capacity = 0;
351132720Skan      }
352132720Skan
353132720Skan      _Base&
354132720Skan      _M_base() { return *this; }
355132720Skan
356132720Skan      const _Base&
357132720Skan      _M_base() const { return *this; }
358132720Skan
359132720Skan    private:
360132720Skan      size_type _M_guaranteed_capacity;
361132720Skan
362132720Skan      bool
363132720Skan      _M_requires_reallocation(size_type __elements)
364132720Skan      {
365132720Skan#ifdef _GLIBCXX_DEBUG_PEDANTIC
366132720Skan	return __elements > this->capacity();
367132720Skan#else
368132720Skan	return __elements > _M_guaranteed_capacity;
369132720Skan#endif
370132720Skan      }
371132720Skan
372132720Skan      void
373132720Skan      _M_update_guaranteed_capacity()
374132720Skan      {
375132720Skan	if (this->size() > _M_guaranteed_capacity)
376132720Skan	  _M_guaranteed_capacity = this->size();
377132720Skan      }
378132720Skan    };
379132720Skan
380132720Skan  template<typename _Tp, typename _Alloc>
381132720Skan    inline bool
382132720Skan    operator==(const vector<_Tp, _Alloc>& __lhs,
383132720Skan	       const vector<_Tp, _Alloc>& __rhs)
384132720Skan    { return __lhs._M_base() == __rhs._M_base(); }
385132720Skan
386132720Skan  template<typename _Tp, typename _Alloc>
387132720Skan    inline bool
388132720Skan    operator!=(const vector<_Tp, _Alloc>& __lhs,
389132720Skan	       const vector<_Tp, _Alloc>& __rhs)
390132720Skan    { return __lhs._M_base() != __rhs._M_base(); }
391132720Skan
392132720Skan  template<typename _Tp, typename _Alloc>
393132720Skan    inline bool
394132720Skan    operator<(const vector<_Tp, _Alloc>& __lhs,
395132720Skan	      const vector<_Tp, _Alloc>& __rhs)
396132720Skan    { return __lhs._M_base() < __rhs._M_base(); }
397132720Skan
398132720Skan  template<typename _Tp, typename _Alloc>
399132720Skan    inline bool
400132720Skan    operator<=(const vector<_Tp, _Alloc>& __lhs,
401132720Skan	       const vector<_Tp, _Alloc>& __rhs)
402132720Skan    { return __lhs._M_base() <= __rhs._M_base(); }
403132720Skan
404132720Skan  template<typename _Tp, typename _Alloc>
405132720Skan    inline bool
406132720Skan    operator>=(const vector<_Tp, _Alloc>& __lhs,
407132720Skan	       const vector<_Tp, _Alloc>& __rhs)
408132720Skan    { return __lhs._M_base() >= __rhs._M_base(); }
409132720Skan
410132720Skan  template<typename _Tp, typename _Alloc>
411132720Skan    inline bool
412132720Skan    operator>(const vector<_Tp, _Alloc>& __lhs,
413132720Skan	      const vector<_Tp, _Alloc>& __rhs)
414132720Skan    { return __lhs._M_base() > __rhs._M_base(); }
415132720Skan
416132720Skan  template<typename _Tp, typename _Alloc>
417132720Skan    inline void
418132720Skan    swap(vector<_Tp, _Alloc>& __lhs, vector<_Tp, _Alloc>& __rhs)
419132720Skan    { __lhs.swap(__rhs); }
420169691Skan} // namespace __debug
421169691Skan} // namespace std
422132720Skan
423132720Skan#endif
424