1132720Skan// Debugging set 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/set.h
32169691Skan *  This file is a GNU debug extension to the Standard C++ Library.
33169691Skan */
34169691Skan
35132720Skan#ifndef _GLIBCXX_DEBUG_SET_H
36132720Skan#define _GLIBCXX_DEBUG_SET_H 1
37132720Skan
38132720Skan#include <debug/safe_sequence.h>
39132720Skan#include <debug/safe_iterator.h>
40132720Skan#include <utility>
41132720Skan
42169691Skannamespace std
43132720Skan{
44169691Skannamespace __debug
45169691Skan{
46132720Skan  template<typename _Key, typename _Compare = std::less<_Key>,
47132720Skan	   typename _Allocator = std::allocator<_Key> >
48132720Skan    class set
49132720Skan    : public _GLIBCXX_STD::set<_Key,_Compare,_Allocator>,
50132720Skan      public __gnu_debug::_Safe_sequence<set<_Key, _Compare, _Allocator> >
51132720Skan    {
52132720Skan      typedef _GLIBCXX_STD::set<_Key,_Compare,_Allocator> _Base;
53132720Skan      typedef __gnu_debug::_Safe_sequence<set> _Safe_base;
54132720Skan
55132720Skan    public:
56132720Skan      // types:
57132720Skan      typedef _Key				    key_type;
58132720Skan      typedef _Key				    value_type;
59132720Skan      typedef _Compare				    key_compare;
60132720Skan      typedef _Compare				    value_compare;
61132720Skan      typedef _Allocator			    allocator_type;
62169691Skan      typedef typename _Base::reference             reference;
63169691Skan      typedef typename _Base::const_reference       const_reference;
64132720Skan
65132720Skan      typedef __gnu_debug::_Safe_iterator<typename _Base::iterator, set>
66132720Skan                                                    iterator;
67132720Skan      typedef __gnu_debug::_Safe_iterator<typename _Base::const_iterator, set>
68132720Skan                                                    const_iterator;
69132720Skan
70132720Skan      typedef typename _Base::size_type             size_type;
71132720Skan      typedef typename _Base::difference_type       difference_type;
72169691Skan      typedef typename _Base::pointer               pointer;
73169691Skan      typedef typename _Base::const_pointer         const_pointer;
74132720Skan      typedef std::reverse_iterator<iterator>       reverse_iterator;
75132720Skan      typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
76132720Skan
77132720Skan      // 23.3.3.1 construct/copy/destroy:
78132720Skan      explicit set(const _Compare& __comp = _Compare(),
79132720Skan		   const _Allocator& __a = _Allocator())
80132720Skan      : _Base(__comp, __a) { }
81132720Skan
82132720Skan      template<typename _InputIterator>
83132720Skan        set(_InputIterator __first, _InputIterator __last,
84132720Skan	    const _Compare& __comp = _Compare(),
85132720Skan	    const _Allocator& __a = _Allocator())
86132720Skan	: _Base(__gnu_debug::__check_valid_range(__first, __last), __last,
87132720Skan		__comp, __a) { }
88132720Skan
89132720Skan      set(const set<_Key,_Compare,_Allocator>& __x)
90132720Skan      : _Base(__x), _Safe_base() { }
91132720Skan
92132720Skan      set(const _Base& __x) : _Base(__x), _Safe_base() { }
93132720Skan
94132720Skan      ~set() { }
95132720Skan
96132720Skan      set<_Key,_Compare,_Allocator>&
97132720Skan      operator=(const set<_Key,_Compare,_Allocator>& __x)
98132720Skan      {
99132720Skan	*static_cast<_Base*>(this) = __x;
100132720Skan	this->_M_invalidate_all();
101132720Skan	return *this;
102132720Skan      }
103132720Skan
104132720Skan      using _Base::get_allocator;
105132720Skan
106132720Skan      // iterators:
107132720Skan      iterator
108132720Skan      begin()
109132720Skan      { return iterator(_Base::begin(), this); }
110132720Skan
111132720Skan      const_iterator
112132720Skan      begin() const
113132720Skan      { return const_iterator(_Base::begin(), this); }
114132720Skan
115132720Skan      iterator
116132720Skan      end()
117132720Skan      { return iterator(_Base::end(), this); }
118132720Skan
119132720Skan      const_iterator
120132720Skan      end() const
121132720Skan      { return const_iterator(_Base::end(), this); }
122132720Skan
123132720Skan      reverse_iterator
124132720Skan      rbegin()
125132720Skan      { return reverse_iterator(end()); }
126132720Skan
127132720Skan      const_reverse_iterator
128132720Skan      rbegin() const
129132720Skan      { return const_reverse_iterator(end()); }
130132720Skan
131132720Skan      reverse_iterator
132132720Skan      rend()
133132720Skan      { return reverse_iterator(begin()); }
134132720Skan
135132720Skan      const_reverse_iterator
136132720Skan      rend() const
137132720Skan      { return const_reverse_iterator(begin()); }
138132720Skan
139132720Skan      // capacity:
140132720Skan      using _Base::empty;
141132720Skan      using _Base::size;
142132720Skan      using _Base::max_size;
143132720Skan
144132720Skan      // modifiers:
145132720Skan      std::pair<iterator, bool>
146132720Skan      insert(const value_type& __x)
147132720Skan      {
148132720Skan	typedef typename _Base::iterator _Base_iterator;
149132720Skan	std::pair<_Base_iterator, bool> __res = _Base::insert(__x);
150132720Skan	return std::pair<iterator, bool>(iterator(__res.first, this),
151132720Skan					 __res.second);
152132720Skan      }
153132720Skan
154132720Skan      iterator
155132720Skan      insert(iterator __position, const value_type& __x)
156132720Skan      {
157132720Skan	__glibcxx_check_insert(__position);
158132720Skan	return iterator(_Base::insert(__position.base(), __x), this);
159132720Skan      }
160132720Skan
161132720Skan      template <typename _InputIterator>
162132720Skan        void
163132720Skan        insert(_InputIterator __first, _InputIterator __last)
164132720Skan        {
165132720Skan	  __glibcxx_check_valid_range(__first, __last);
166132720Skan	  _Base::insert(__first, __last);
167132720Skan	}
168132720Skan
169132720Skan      void
170132720Skan      erase(iterator __position)
171132720Skan      {
172132720Skan	__glibcxx_check_erase(__position);
173132720Skan	__position._M_invalidate();
174132720Skan	_Base::erase(__position.base());
175132720Skan      }
176132720Skan
177132720Skan      size_type
178132720Skan      erase(const key_type& __x)
179132720Skan      {
180132720Skan	iterator __victim = find(__x);
181132720Skan	if (__victim == end())
182132720Skan          return 0;
183132720Skan	else
184132720Skan        {
185132720Skan	  __victim._M_invalidate();
186132720Skan	  _Base::erase(__victim.base());
187132720Skan	  return 1;
188132720Skan        }
189132720Skan      }
190132720Skan
191132720Skan      void
192132720Skan      erase(iterator __first, iterator __last)
193132720Skan      {
194132720Skan	// _GLIBCXX_RESOLVE_LIB_DEFECTS
195132720Skan	// 151. can't currently clear() empty container
196132720Skan	__glibcxx_check_erase_range(__first, __last);
197132720Skan
198132720Skan	while (__first != __last)
199132720Skan        this->erase(__first++);
200132720Skan      }
201132720Skan
202132720Skan      void
203132720Skan      swap(set<_Key,_Compare,_Allocator>& __x)
204132720Skan      {
205132720Skan	_Base::swap(__x);
206132720Skan	this->_M_swap(__x);
207132720Skan      }
208132720Skan
209132720Skan      void
210132720Skan      clear()
211132720Skan      { this->erase(begin(), end()); }
212132720Skan
213132720Skan      // observers:
214132720Skan      using _Base::key_comp;
215132720Skan      using _Base::value_comp;
216132720Skan
217132720Skan      // set operations:
218132720Skan      iterator
219132720Skan      find(const key_type& __x)
220132720Skan      { return iterator(_Base::find(__x), this); }
221132720Skan
222132720Skan      // _GLIBCXX_RESOLVE_LIB_DEFECTS
223132720Skan      // 214. set::find() missing const overload
224132720Skan      const_iterator
225132720Skan      find(const key_type& __x) const
226132720Skan      { return const_iterator(_Base::find(__x), this); }
227132720Skan
228132720Skan      using _Base::count;
229132720Skan
230132720Skan      iterator
231132720Skan      lower_bound(const key_type& __x)
232132720Skan      { return iterator(_Base::lower_bound(__x), this); }
233132720Skan
234132720Skan      // _GLIBCXX_RESOLVE_LIB_DEFECTS
235132720Skan      // 214. set::find() missing const overload
236132720Skan      const_iterator
237132720Skan      lower_bound(const key_type& __x) const
238132720Skan      { return const_iterator(_Base::lower_bound(__x), this); }
239132720Skan
240132720Skan      iterator
241132720Skan      upper_bound(const key_type& __x)
242132720Skan      { return iterator(_Base::upper_bound(__x), this); }
243132720Skan
244132720Skan      // _GLIBCXX_RESOLVE_LIB_DEFECTS
245132720Skan      // 214. set::find() missing const overload
246132720Skan      const_iterator
247132720Skan      upper_bound(const key_type& __x) const
248132720Skan      { return const_iterator(_Base::upper_bound(__x), this); }
249132720Skan
250132720Skan      std::pair<iterator,iterator>
251132720Skan      equal_range(const key_type& __x)
252132720Skan      {
253132720Skan	typedef typename _Base::iterator _Base_iterator;
254132720Skan	std::pair<_Base_iterator, _Base_iterator> __res =
255132720Skan        _Base::equal_range(__x);
256132720Skan	return std::make_pair(iterator(__res.first, this),
257132720Skan			      iterator(__res.second, this));
258132720Skan      }
259132720Skan
260132720Skan      // _GLIBCXX_RESOLVE_LIB_DEFECTS
261132720Skan      // 214. set::find() missing const overload
262132720Skan      std::pair<const_iterator,const_iterator>
263132720Skan      equal_range(const key_type& __x) const
264132720Skan      {
265132720Skan	typedef typename _Base::const_iterator _Base_iterator;
266132720Skan	std::pair<_Base_iterator, _Base_iterator> __res =
267132720Skan        _Base::equal_range(__x);
268132720Skan	return std::make_pair(const_iterator(__res.first, this),
269132720Skan			      const_iterator(__res.second, this));
270132720Skan      }
271132720Skan
272132720Skan      _Base&
273132720Skan      _M_base() { return *this; }
274132720Skan
275132720Skan      const _Base&
276132720Skan      _M_base() const { return *this; }
277132720Skan
278132720Skan    private:
279132720Skan      void
280132720Skan      _M_invalidate_all()
281132720Skan      {
282132720Skan	typedef typename _Base::const_iterator _Base_const_iterator;
283132720Skan	typedef __gnu_debug::_Not_equal_to<_Base_const_iterator> _Not_equal;
284132720Skan	this->_M_invalidate_if(_Not_equal(_M_base().end()));
285132720Skan      }
286132720Skan    };
287132720Skan
288132720Skan  template<typename _Key, typename _Compare, typename _Allocator>
289132720Skan    inline bool
290132720Skan    operator==(const set<_Key,_Compare,_Allocator>& __lhs,
291132720Skan	       const set<_Key,_Compare,_Allocator>& __rhs)
292132720Skan    { return __lhs._M_base() == __rhs._M_base(); }
293132720Skan
294132720Skan  template<typename _Key, typename _Compare, typename _Allocator>
295132720Skan    inline bool
296132720Skan    operator!=(const set<_Key,_Compare,_Allocator>& __lhs,
297132720Skan	       const set<_Key,_Compare,_Allocator>& __rhs)
298132720Skan    { return __lhs._M_base() != __rhs._M_base(); }
299132720Skan
300132720Skan  template<typename _Key, typename _Compare, typename _Allocator>
301132720Skan    inline bool
302132720Skan    operator<(const set<_Key,_Compare,_Allocator>& __lhs,
303132720Skan	      const set<_Key,_Compare,_Allocator>& __rhs)
304132720Skan    { return __lhs._M_base() < __rhs._M_base(); }
305132720Skan
306132720Skan  template<typename _Key, typename _Compare, typename _Allocator>
307132720Skan    inline bool
308132720Skan    operator<=(const set<_Key,_Compare,_Allocator>& __lhs,
309132720Skan	       const set<_Key,_Compare,_Allocator>& __rhs)
310132720Skan    { return __lhs._M_base() <= __rhs._M_base(); }
311132720Skan
312132720Skan  template<typename _Key, typename _Compare, typename _Allocator>
313132720Skan    inline bool
314132720Skan    operator>=(const set<_Key,_Compare,_Allocator>& __lhs,
315132720Skan	       const set<_Key,_Compare,_Allocator>& __rhs)
316132720Skan    { return __lhs._M_base() >= __rhs._M_base(); }
317132720Skan
318132720Skan  template<typename _Key, typename _Compare, typename _Allocator>
319132720Skan    inline bool
320132720Skan    operator>(const set<_Key,_Compare,_Allocator>& __lhs,
321132720Skan	      const set<_Key,_Compare,_Allocator>& __rhs)
322132720Skan    { return __lhs._M_base() > __rhs._M_base(); }
323132720Skan
324132720Skan  template<typename _Key, typename _Compare, typename _Allocator>
325132720Skan    void
326132720Skan    swap(set<_Key,_Compare,_Allocator>& __x,
327132720Skan	 set<_Key,_Compare,_Allocator>& __y)
328132720Skan    { return __x.swap(__y); }
329169691Skan} // namespace __debug
330169691Skan} // namespace std
331132720Skan
332132720Skan#endif
333