xref: /openbsd-src/gnu/gcc/libstdc++-v3/include/debug/hash_set.h (revision 404b540a9034ac75a6199ad1a32d1bbc7a0d4210)
1*404b540aSrobert // Debugging hash_set implementation -*- C++ -*-
2*404b540aSrobert 
3*404b540aSrobert // Copyright (C) 2003, 2005, 2006
4*404b540aSrobert // Free Software Foundation, Inc.
5*404b540aSrobert //
6*404b540aSrobert // This file is part of the GNU ISO C++ Library.  This library is free
7*404b540aSrobert // software; you can redistribute it and/or modify it under the
8*404b540aSrobert // terms of the GNU General Public License as published by the
9*404b540aSrobert // Free Software Foundation; either version 2, or (at your option)
10*404b540aSrobert // any later version.
11*404b540aSrobert 
12*404b540aSrobert // This library is distributed in the hope that it will be useful,
13*404b540aSrobert // but WITHOUT ANY WARRANTY; without even the implied warranty of
14*404b540aSrobert // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15*404b540aSrobert // GNU General Public License for more details.
16*404b540aSrobert 
17*404b540aSrobert // You should have received a copy of the GNU General Public License along
18*404b540aSrobert // with this library; see the file COPYING.  If not, write to the Free
19*404b540aSrobert // Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
20*404b540aSrobert // USA.
21*404b540aSrobert 
22*404b540aSrobert // As a special exception, you may use this file as part of a free software
23*404b540aSrobert // library without restriction.  Specifically, if other files instantiate
24*404b540aSrobert // templates or use macros or inline functions from this file, or you compile
25*404b540aSrobert // this file and link it with other files to produce an executable, this
26*404b540aSrobert // file does not by itself cause the resulting executable to be covered by
27*404b540aSrobert // the GNU General Public License.  This exception does not however
28*404b540aSrobert // invalidate any other reasons why the executable file might be covered by
29*404b540aSrobert // the GNU General Public License.
30*404b540aSrobert 
31*404b540aSrobert /** @file debug/hash_set.h
32*404b540aSrobert  *  This file is a GNU debug extension to the Standard C++ Library.
33*404b540aSrobert  */
34*404b540aSrobert 
35*404b540aSrobert #ifndef _GLIBCXX_DEBUG_HASH_SET_H
36*404b540aSrobert #define _GLIBCXX_DEBUG_HASH_SET_H 1
37*404b540aSrobert 
38*404b540aSrobert #include <debug/safe_sequence.h>
39*404b540aSrobert #include <debug/safe_iterator.h>
40*404b540aSrobert 
41*404b540aSrobert namespace __gnu_cxx
42*404b540aSrobert {
43*404b540aSrobert namespace __debug
44*404b540aSrobert {
45*404b540aSrobert   template<typename _Value,
46*404b540aSrobert 	   typename _HashFcn  = __gnu_cxx::hash<_Value>,
47*404b540aSrobert 	   typename _EqualKey = std::equal_to<_Value>,
48*404b540aSrobert 	   typename _Alloc =  std::allocator<_Value> >
49*404b540aSrobert     class hash_set
50*404b540aSrobert     : public _GLIBCXX_EXT::hash_set<_Value, _HashFcn, _EqualKey,_Alloc>,
51*404b540aSrobert       public __gnu_debug::_Safe_sequence<hash_set<_Value, _HashFcn, _EqualKey,
52*404b540aSrobert 						  _Alloc> >
53*404b540aSrobert     {
54*404b540aSrobert       typedef _GLIBCXX_EXT::hash_set<_Value, _HashFcn, _EqualKey,_Alloc> _Base;
55*404b540aSrobert       typedef __gnu_debug::_Safe_sequence<hash_set> _Safe_base;
56*404b540aSrobert 
57*404b540aSrobert     public:
58*404b540aSrobert       typedef typename _Base::key_type        key_type;
59*404b540aSrobert       typedef typename _Base::value_type      value_type;
60*404b540aSrobert       typedef typename _Base::hasher          hasher;
61*404b540aSrobert       typedef typename _Base::key_equal       key_equal;
62*404b540aSrobert       typedef typename _Base::size_type       size_type;
63*404b540aSrobert       typedef typename _Base::difference_type difference_type;
64*404b540aSrobert       typedef typename _Base::pointer         pointer;
65*404b540aSrobert       typedef typename _Base::const_pointer   const_pointer;
66*404b540aSrobert       typedef typename _Base::reference       reference;
67*404b540aSrobert       typedef typename _Base::const_reference const_reference;
68*404b540aSrobert 
69*404b540aSrobert       typedef __gnu_debug::_Safe_iterator<typename _Base::iterator, hash_set>
70*404b540aSrobert                                               iterator;
71*404b540aSrobert       typedef __gnu_debug::_Safe_iterator<typename _Base::const_iterator,
72*404b540aSrobert 					  hash_set>
73*404b540aSrobert                                               const_iterator;
74*404b540aSrobert 
75*404b540aSrobert       typedef typename _Base::allocator_type allocator_type;
76*404b540aSrobert 
77*404b540aSrobert       using _Base::hash_funct;
78*404b540aSrobert       using _Base::key_eq;
79*404b540aSrobert       using _Base::get_allocator;
80*404b540aSrobert 
hash_set()81*404b540aSrobert       hash_set() { }
82*404b540aSrobert 
hash_set(size_type __n)83*404b540aSrobert       explicit hash_set(size_type __n) : _Base(__n) { }
84*404b540aSrobert 
hash_set(size_type __n,const hasher & __hf)85*404b540aSrobert       hash_set(size_type __n, const hasher& __hf) : _Base(__n, __hf) { }
86*404b540aSrobert 
87*404b540aSrobert       hash_set(size_type __n, const hasher& __hf, const key_equal& __eql,
88*404b540aSrobert 	       const allocator_type& __a = allocator_type())
_Base(__n,__hf,__eql,__a)89*404b540aSrobert       : _Base(__n, __hf, __eql, __a) { }
90*404b540aSrobert 
91*404b540aSrobert       template<typename _InputIterator>
hash_set(_InputIterator __f,_InputIterator __l)92*404b540aSrobert         hash_set(_InputIterator __f, _InputIterator __l)
93*404b540aSrobert         : _Base(__gnu_debug::__check_valid_range(__f, __l), __l) { }
94*404b540aSrobert 
95*404b540aSrobert       template<typename _InputIterator>
hash_set(_InputIterator __f,_InputIterator __l,size_type __n)96*404b540aSrobert         hash_set(_InputIterator __f, _InputIterator __l, size_type __n)
97*404b540aSrobert 	: _Base(__gnu_debug::__check_valid_range(__f, __l), __l, __n) { }
98*404b540aSrobert 
99*404b540aSrobert       template<typename _InputIterator>
hash_set(_InputIterator __f,_InputIterator __l,size_type __n,const hasher & __hf)100*404b540aSrobert         hash_set(_InputIterator __f, _InputIterator __l, size_type __n,
101*404b540aSrobert 		 const hasher& __hf)
102*404b540aSrobert 	: _Base(__gnu_debug::__check_valid_range(__f, __l), __l, __n, __hf) { }
103*404b540aSrobert 
104*404b540aSrobert       template<typename _InputIterator>
105*404b540aSrobert         hash_set(_InputIterator __f, _InputIterator __l, size_type __n,
106*404b540aSrobert 		 const hasher& __hf, const key_equal& __eql,
107*404b540aSrobert 		 const allocator_type& __a = allocator_type())
_Base(__gnu_debug::__check_valid_range (__f,__l),__l,__n,__hf,__eql,__a)108*404b540aSrobert 	: _Base(__gnu_debug::__check_valid_range(__f, __l), __l, __n, __hf,
109*404b540aSrobert 		__eql, __a) { }
110*404b540aSrobert 
hash_set(const _Base & __x)111*404b540aSrobert       hash_set(const _Base& __x) : _Base(__x), _Safe_base() { }
112*404b540aSrobert 
113*404b540aSrobert       using _Base::size;
114*404b540aSrobert       using _Base::max_size;
115*404b540aSrobert       using _Base::empty;
116*404b540aSrobert 
117*404b540aSrobert       void
swap(hash_set & __x)118*404b540aSrobert       swap(hash_set& __x)
119*404b540aSrobert       {
120*404b540aSrobert 	_Base::swap(__x);
121*404b540aSrobert 	this->_M_swap(__x);
122*404b540aSrobert       }
123*404b540aSrobert 
124*404b540aSrobert       iterator
begin()125*404b540aSrobert       begin() const { return iterator(_Base::begin(), this); }
126*404b540aSrobert 
127*404b540aSrobert       iterator
end()128*404b540aSrobert       end() const   { return iterator(_Base::end(),   this); }
129*404b540aSrobert 
130*404b540aSrobert       std::pair<iterator, bool>
insert(const value_type & __obj)131*404b540aSrobert       insert(const value_type& __obj)
132*404b540aSrobert       {
133*404b540aSrobert 	std::pair<typename _Base::iterator, bool> __res =
134*404b540aSrobert         _Base::insert(__obj);
135*404b540aSrobert 	return std::make_pair(iterator(__res.first, this), __res.second);
136*404b540aSrobert       }
137*404b540aSrobert 
138*404b540aSrobert       void
insert(const value_type * __first,const value_type * __last)139*404b540aSrobert       insert(const value_type* __first, const value_type* __last)
140*404b540aSrobert       {
141*404b540aSrobert 	__glibcxx_check_valid_range(__first, __last);
142*404b540aSrobert 	_Base::insert(__first, __last);
143*404b540aSrobert       }
144*404b540aSrobert 
145*404b540aSrobert       template<typename _InputIterator>
146*404b540aSrobert         void
insert(_InputIterator __first,_InputIterator __last)147*404b540aSrobert         insert(_InputIterator __first, _InputIterator __last)
148*404b540aSrobert         {
149*404b540aSrobert 	  __glibcxx_check_valid_range(__first, __last);
150*404b540aSrobert 	  _Base::insert(__first.base(), __last.base());
151*404b540aSrobert 	}
152*404b540aSrobert 
153*404b540aSrobert 
154*404b540aSrobert       std::pair<iterator, bool>
insert_noresize(const value_type & __obj)155*404b540aSrobert       insert_noresize(const value_type& __obj)
156*404b540aSrobert       {
157*404b540aSrobert 	std::pair<typename _Base::iterator, bool> __res =
158*404b540aSrobert         _Base::insert_noresize(__obj);
159*404b540aSrobert 	return std::make_pair(iterator(__res.first, this), __res.second);
160*404b540aSrobert       }
161*404b540aSrobert 
162*404b540aSrobert       iterator
find(const key_type & __key)163*404b540aSrobert       find(const key_type& __key) const
164*404b540aSrobert       { return iterator(_Base::find(__key), this); }
165*404b540aSrobert 
166*404b540aSrobert       using _Base::count;
167*404b540aSrobert 
168*404b540aSrobert       std::pair<iterator, iterator>
equal_range(const key_type & __key)169*404b540aSrobert       equal_range(const key_type& __key) const
170*404b540aSrobert       {
171*404b540aSrobert 	typedef typename _Base::iterator _Base_iterator;
172*404b540aSrobert 	std::pair<_Base_iterator, _Base_iterator> __res =
173*404b540aSrobert 	  _Base::equal_range(__key);
174*404b540aSrobert 	return std::make_pair(iterator(__res.first, this),
175*404b540aSrobert 			      iterator(__res.second, this));
176*404b540aSrobert       }
177*404b540aSrobert 
178*404b540aSrobert       size_type
erase(const key_type & __key)179*404b540aSrobert       erase(const key_type& __key)
180*404b540aSrobert       {
181*404b540aSrobert 	iterator __victim(_Base::find(__key), this);
182*404b540aSrobert 	if (__victim != end())
183*404b540aSrobert 	  return this->erase(__victim), 1;
184*404b540aSrobert 	else
185*404b540aSrobert 	  return 0;
186*404b540aSrobert       }
187*404b540aSrobert 
188*404b540aSrobert       void
erase(iterator __it)189*404b540aSrobert       erase(iterator __it)
190*404b540aSrobert       {
191*404b540aSrobert 	__glibcxx_check_erase(__it);
192*404b540aSrobert 	__it._M_invalidate();
193*404b540aSrobert 	_Base::erase(__it.base());
194*404b540aSrobert       }
195*404b540aSrobert 
196*404b540aSrobert       void
erase(iterator __first,iterator __last)197*404b540aSrobert       erase(iterator __first, iterator __last)
198*404b540aSrobert       {
199*404b540aSrobert 	__glibcxx_check_erase_range(__first, __last);
200*404b540aSrobert 	for (iterator __tmp = __first; __tmp != __last;)
201*404b540aSrobert 	{
202*404b540aSrobert 	  iterator __victim = __tmp++;
203*404b540aSrobert 	  __victim._M_invalidate();
204*404b540aSrobert 	}
205*404b540aSrobert 	_Base::erase(__first.base(), __last.base());
206*404b540aSrobert       }
207*404b540aSrobert 
208*404b540aSrobert       void
clear()209*404b540aSrobert       clear()
210*404b540aSrobert       {
211*404b540aSrobert 	_Base::clear();
212*404b540aSrobert 	this->_M_invalidate_all();
213*404b540aSrobert       }
214*404b540aSrobert 
215*404b540aSrobert       using _Base::resize;
216*404b540aSrobert       using _Base::bucket_count;
217*404b540aSrobert       using _Base::max_bucket_count;
218*404b540aSrobert       using _Base::elems_in_bucket;
219*404b540aSrobert 
220*404b540aSrobert       _Base&
_M_base()221*404b540aSrobert       _M_base()       { return *this; }
222*404b540aSrobert 
223*404b540aSrobert       const _Base&
_M_base()224*404b540aSrobert       _M_base() const { return *this; }
225*404b540aSrobert 
226*404b540aSrobert     private:
227*404b540aSrobert       void
_M_invalidate_all()228*404b540aSrobert       _M_invalidate_all()
229*404b540aSrobert       {
230*404b540aSrobert 	typedef typename _Base::const_iterator _Base_const_iterator;
231*404b540aSrobert 	typedef __gnu_debug::_Not_equal_to<_Base_const_iterator> _Not_equal;
232*404b540aSrobert 	this->_M_invalidate_if(_Not_equal(_M_base().end()));
233*404b540aSrobert       }
234*404b540aSrobert     };
235*404b540aSrobert 
236*404b540aSrobert   template<typename _Value, typename _HashFcn, typename _EqualKey,
237*404b540aSrobert 	   typename _Alloc>
238*404b540aSrobert     inline bool
239*404b540aSrobert     operator==(const hash_set<_Value, _HashFcn, _EqualKey, _Alloc>& __x,
240*404b540aSrobert 	       const hash_set<_Value, _HashFcn, _EqualKey, _Alloc>& __y)
241*404b540aSrobert     { return __x._M_base() == __y._M_base(); }
242*404b540aSrobert 
243*404b540aSrobert   template<typename _Value, typename _HashFcn, typename _EqualKey,
244*404b540aSrobert 	   typename _Alloc>
245*404b540aSrobert     inline bool
246*404b540aSrobert     operator!=(const hash_set<_Value, _HashFcn, _EqualKey, _Alloc>& __x,
247*404b540aSrobert 	       const hash_set<_Value, _HashFcn, _EqualKey, _Alloc>& __y)
248*404b540aSrobert     { return __x._M_base() != __y._M_base(); }
249*404b540aSrobert 
250*404b540aSrobert   template<typename _Value, typename _HashFcn, typename _EqualKey,
251*404b540aSrobert 	   typename _Alloc>
252*404b540aSrobert     inline void
swap(hash_set<_Value,_HashFcn,_EqualKey,_Alloc> & __x,hash_set<_Value,_HashFcn,_EqualKey,_Alloc> & __y)253*404b540aSrobert     swap(hash_set<_Value, _HashFcn, _EqualKey, _Alloc>& __x,
254*404b540aSrobert 	 hash_set<_Value, _HashFcn, _EqualKey, _Alloc>& __y)
255*404b540aSrobert     { __x.swap(__y); }
256*404b540aSrobert } // namespace __debug
257*404b540aSrobert } // namespace __gnu_cxx
258*404b540aSrobert 
259*404b540aSrobert #endif
260