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