xref: /openbsd-src/gnu/gcc/libstdc++-v3/include/debug/hash_map.h (revision 404b540a9034ac75a6199ad1a32d1bbc7a0d4210)
1*404b540aSrobert // Debugging hash_map 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_map.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_MAP_H
36*404b540aSrobert #define _GLIBCXX_DEBUG_HASH_MAP_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, typename _Tp,
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_map
50*404b540aSrobert     : public _GLIBCXX_EXT::hash_map<_Value, _Tp, _HashFcn, _EqualKey, _Alloc>,
51*404b540aSrobert       public __gnu_debug::_Safe_sequence<hash_map<_Value, _Tp, _HashFcn,
52*404b540aSrobert 						 _EqualKey, _Alloc> >
53*404b540aSrobert     {
54*404b540aSrobert       typedef _GLIBCXX_EXT::hash_map<_Value, _Tp, _HashFcn, _EqualKey, _Alloc>
55*404b540aSrobert       							_Base;
56*404b540aSrobert       typedef __gnu_debug::_Safe_sequence<hash_map> 	_Safe_base;
57*404b540aSrobert 
58*404b540aSrobert     public:
59*404b540aSrobert       typedef typename _Base::key_type        key_type;
60*404b540aSrobert       typedef typename _Base::data_type       data_type;
61*404b540aSrobert       typedef typename _Base::mapped_type     mapped_type;
62*404b540aSrobert       typedef typename _Base::value_type      value_type;
63*404b540aSrobert       typedef typename _Base::hasher          hasher;
64*404b540aSrobert       typedef typename _Base::key_equal       key_equal;
65*404b540aSrobert       typedef typename _Base::size_type       size_type;
66*404b540aSrobert       typedef typename _Base::difference_type difference_type;
67*404b540aSrobert       typedef typename _Base::pointer         pointer;
68*404b540aSrobert       typedef typename _Base::const_pointer   const_pointer;
69*404b540aSrobert       typedef typename _Base::reference       reference;
70*404b540aSrobert       typedef typename _Base::const_reference const_reference;
71*404b540aSrobert 
72*404b540aSrobert       typedef __gnu_debug::_Safe_iterator<typename _Base::iterator, hash_map>
73*404b540aSrobert 					      iterator;
74*404b540aSrobert       typedef __gnu_debug::_Safe_iterator<typename _Base::const_iterator,
75*404b540aSrobert 					  hash_map>
76*404b540aSrobert 					      const_iterator;
77*404b540aSrobert 
78*404b540aSrobert       typedef typename _Base::allocator_type  allocator_type;
79*404b540aSrobert 
80*404b540aSrobert       using _Base::hash_funct;
81*404b540aSrobert       using _Base::key_eq;
82*404b540aSrobert       using _Base::get_allocator;
83*404b540aSrobert 
hash_map()84*404b540aSrobert       hash_map() { }
85*404b540aSrobert 
hash_map(size_type __n)86*404b540aSrobert       explicit hash_map(size_type __n) : _Base(__n) { }
87*404b540aSrobert 
hash_map(size_type __n,const hasher & __hf)88*404b540aSrobert       hash_map(size_type __n, const hasher& __hf) : _Base(__n, __hf) { }
89*404b540aSrobert 
90*404b540aSrobert       hash_map(size_type __n, const hasher& __hf, const key_equal& __eql,
91*404b540aSrobert 	       const allocator_type& __a = allocator_type())
_Base(__n,__hf,__eql,__a)92*404b540aSrobert       : _Base(__n, __hf, __eql, __a) { }
93*404b540aSrobert 
94*404b540aSrobert       template<typename _InputIterator>
hash_map(_InputIterator __f,_InputIterator __l)95*404b540aSrobert         hash_map(_InputIterator __f, _InputIterator __l)
96*404b540aSrobert         : _Base(__gnu_debug::__check_valid_range(__f, __l), __l) { }
97*404b540aSrobert 
98*404b540aSrobert       template<typename _InputIterator>
hash_map(_InputIterator __f,_InputIterator __l,size_type __n)99*404b540aSrobert         hash_map(_InputIterator __f, _InputIterator __l, size_type __n)
100*404b540aSrobert 	: _Base(__gnu_debug::__check_valid_range(__f, __l), __l, __n) { }
101*404b540aSrobert 
102*404b540aSrobert       template<typename _InputIterator>
hash_map(_InputIterator __f,_InputIterator __l,size_type __n,const hasher & __hf)103*404b540aSrobert         hash_map(_InputIterator __f, _InputIterator __l, size_type __n,
104*404b540aSrobert 		 const hasher& __hf)
105*404b540aSrobert         : _Base(__gnu_debug::__check_valid_range(__f, __l), __l, __n, __hf) { }
106*404b540aSrobert 
107*404b540aSrobert       template<typename _InputIterator>
108*404b540aSrobert         hash_map(_InputIterator __f, _InputIterator __l, size_type __n,
109*404b540aSrobert 		 const hasher& __hf, const key_equal& __eql,
110*404b540aSrobert 		 const allocator_type& __a = allocator_type())
_Base(__gnu_debug::__check_valid_range (__f,__l),__l,__n,__hf,__eql,__a)111*404b540aSrobert 	: _Base(__gnu_debug::__check_valid_range(__f, __l), __l, __n, __hf,
112*404b540aSrobert 		__eql, __a) { }
113*404b540aSrobert 
hash_map(const _Base & __x)114*404b540aSrobert       hash_map(const _Base& __x) : _Base(__x), _Safe_base() { }
115*404b540aSrobert 
116*404b540aSrobert       using _Base::size;
117*404b540aSrobert       using _Base::max_size;
118*404b540aSrobert       using _Base::empty;
119*404b540aSrobert 
120*404b540aSrobert       void
swap(hash_map & __x)121*404b540aSrobert       swap(hash_map& __x)
122*404b540aSrobert       {
123*404b540aSrobert 	_Base::swap(__x);
124*404b540aSrobert 	this->_M_swap(__x);
125*404b540aSrobert       }
126*404b540aSrobert 
127*404b540aSrobert       iterator
begin()128*404b540aSrobert       begin() { return iterator(_Base::begin(), this); }
129*404b540aSrobert 
130*404b540aSrobert       iterator
end()131*404b540aSrobert       end() { return iterator(_Base::end(),   this); }
132*404b540aSrobert 
133*404b540aSrobert       const_iterator
begin()134*404b540aSrobert       begin() const
135*404b540aSrobert       { return const_iterator(_Base::begin(), this); }
136*404b540aSrobert 
137*404b540aSrobert       const_iterator
end()138*404b540aSrobert       end() const
139*404b540aSrobert       { return const_iterator(_Base::end(),   this); }
140*404b540aSrobert 
141*404b540aSrobert       std::pair<iterator, bool>
insert(const value_type & __obj)142*404b540aSrobert       insert(const value_type& __obj)
143*404b540aSrobert       {
144*404b540aSrobert 	std::pair<typename _Base::iterator, bool> __res = _Base::insert(__obj);
145*404b540aSrobert 	return std::make_pair(iterator(__res.first, this), __res.second);
146*404b540aSrobert       }
147*404b540aSrobert 
148*404b540aSrobert       void
insert(const value_type * __first,const value_type * __last)149*404b540aSrobert       insert(const value_type* __first, const value_type* __last)
150*404b540aSrobert       {
151*404b540aSrobert 	__glibcxx_check_valid_range(__first, __last);
152*404b540aSrobert 	_Base::insert(__first, __last);
153*404b540aSrobert       }
154*404b540aSrobert 
155*404b540aSrobert      template<typename _InputIterator>
156*404b540aSrobert         void
insert(_InputIterator __first,_InputIterator __last)157*404b540aSrobert         insert(_InputIterator __first, _InputIterator __last)
158*404b540aSrobert         {
159*404b540aSrobert 	  __glibcxx_check_valid_range(__first, __last);
160*404b540aSrobert 	  _Base::insert(__first.base(), __last.base());
161*404b540aSrobert 	}
162*404b540aSrobert 
163*404b540aSrobert 
164*404b540aSrobert       std::pair<iterator, bool>
insert_noresize(const value_type & __obj)165*404b540aSrobert       insert_noresize(const value_type& __obj)
166*404b540aSrobert       {
167*404b540aSrobert 	std::pair<typename _Base::iterator, bool> __res =
168*404b540aSrobert 	                                        _Base::insert_noresize(__obj);
169*404b540aSrobert 	return std::make_pair(iterator(__res.first, this), __res.second);
170*404b540aSrobert       }
171*404b540aSrobert 
172*404b540aSrobert       iterator
find(const key_type & __key)173*404b540aSrobert       find(const key_type& __key)
174*404b540aSrobert       { return iterator(_Base::find(__key), this); }
175*404b540aSrobert 
176*404b540aSrobert       const_iterator
find(const key_type & __key)177*404b540aSrobert       find(const key_type& __key) const
178*404b540aSrobert       { return const_iterator(_Base::find(__key), this); }
179*404b540aSrobert 
180*404b540aSrobert       using _Base::operator[];
181*404b540aSrobert       using _Base::count;
182*404b540aSrobert 
183*404b540aSrobert       std::pair<iterator, iterator>
equal_range(const key_type & __key)184*404b540aSrobert       equal_range(const key_type& __key)
185*404b540aSrobert       {
186*404b540aSrobert 	typedef typename _Base::iterator _Base_iterator;
187*404b540aSrobert 	std::pair<_Base_iterator, _Base_iterator> __res =
188*404b540aSrobert 	                  _Base::equal_range(__key);
189*404b540aSrobert 	return std::make_pair(iterator(__res.first, this),
190*404b540aSrobert 			      iterator(__res.second, this));
191*404b540aSrobert       }
192*404b540aSrobert 
193*404b540aSrobert       std::pair<const_iterator, const_iterator>
equal_range(const key_type & __key)194*404b540aSrobert       equal_range(const key_type& __key) const
195*404b540aSrobert       {
196*404b540aSrobert 	typedef typename _Base::const_iterator _Base_iterator;
197*404b540aSrobert 	std::pair<_Base_iterator, _Base_iterator> __res =
198*404b540aSrobert 	_Base::equal_range(__key);
199*404b540aSrobert 	return std::make_pair(const_iterator(__res.first, this),
200*404b540aSrobert 			      const_iterator(__res.second, this));
201*404b540aSrobert       }
202*404b540aSrobert 
203*404b540aSrobert       size_type
erase(const key_type & __key)204*404b540aSrobert       erase(const key_type& __key)
205*404b540aSrobert       {
206*404b540aSrobert 	iterator __victim(_Base::find(__key), this);
207*404b540aSrobert 	if (__victim != end())
208*404b540aSrobert 	  return this->erase(__victim), 1;
209*404b540aSrobert 	else
210*404b540aSrobert 	  return 0;
211*404b540aSrobert       }
212*404b540aSrobert 
213*404b540aSrobert       void
erase(iterator __it)214*404b540aSrobert       erase(iterator __it)
215*404b540aSrobert       {
216*404b540aSrobert 	__glibcxx_check_erase(__it);
217*404b540aSrobert 	__it._M_invalidate();
218*404b540aSrobert 	_Base::erase(__it.base());
219*404b540aSrobert       }
220*404b540aSrobert 
221*404b540aSrobert       void
erase(iterator __first,iterator __last)222*404b540aSrobert       erase(iterator __first, iterator __last)
223*404b540aSrobert       {
224*404b540aSrobert 	__glibcxx_check_erase_range(__first, __last);
225*404b540aSrobert 	for (iterator __tmp = __first; __tmp != __last;)
226*404b540aSrobert 	{
227*404b540aSrobert 	  iterator __victim = __tmp++;
228*404b540aSrobert 	  __victim._M_invalidate();
229*404b540aSrobert 	}
230*404b540aSrobert 	_Base::erase(__first.base(), __last.base());
231*404b540aSrobert       }
232*404b540aSrobert 
233*404b540aSrobert       void
clear()234*404b540aSrobert       clear()
235*404b540aSrobert       {
236*404b540aSrobert 	_Base::clear();
237*404b540aSrobert 	this->_M_invalidate_all();
238*404b540aSrobert       }
239*404b540aSrobert 
240*404b540aSrobert       using _Base::resize;
241*404b540aSrobert       using _Base::bucket_count;
242*404b540aSrobert       using _Base::max_bucket_count;
243*404b540aSrobert       using _Base::elems_in_bucket;
244*404b540aSrobert 
245*404b540aSrobert       _Base&
_M_base()246*404b540aSrobert       _M_base()       { return *this; }
247*404b540aSrobert 
248*404b540aSrobert       const _Base&
_M_base()249*404b540aSrobert       _M_base() const { return *this; }
250*404b540aSrobert 
251*404b540aSrobert     private:
252*404b540aSrobert       void
_M_invalidate_all()253*404b540aSrobert       _M_invalidate_all()
254*404b540aSrobert       {
255*404b540aSrobert 	typedef typename _Base::const_iterator _Base_const_iterator;
256*404b540aSrobert 	typedef __gnu_debug::_Not_equal_to<_Base_const_iterator> _Not_equal;
257*404b540aSrobert 	this->_M_invalidate_if(_Not_equal(_M_base().end()));
258*404b540aSrobert       }
259*404b540aSrobert     };
260*404b540aSrobert 
261*404b540aSrobert   template<typename _Value, typename _Tp, typename _HashFcn,
262*404b540aSrobert 	   typename _EqualKey, typename _Alloc>
263*404b540aSrobert     inline bool
264*404b540aSrobert     operator==(const hash_map<_Value, _Tp, _HashFcn, _EqualKey, _Alloc>& __x,
265*404b540aSrobert 	       const hash_map<_Value, _Tp, _HashFcn, _EqualKey, _Alloc>& __y)
266*404b540aSrobert     { return __x._M_base() == __y._M_base(); }
267*404b540aSrobert 
268*404b540aSrobert   template<typename _Value, typename _Tp, typename _HashFcn,
269*404b540aSrobert 	   typename _EqualKey, typename _Alloc>
270*404b540aSrobert     inline bool
271*404b540aSrobert     operator!=(const hash_map<_Value, _Tp, _HashFcn, _EqualKey, _Alloc>& __x,
272*404b540aSrobert 	       const hash_map<_Value, _Tp, _HashFcn, _EqualKey, _Alloc>& __y)
273*404b540aSrobert     { return __x._M_base() != __y._M_base(); }
274*404b540aSrobert 
275*404b540aSrobert   template<typename _Value, typename _Tp, typename _HashFcn,
276*404b540aSrobert 	   typename _EqualKey, typename _Alloc>
277*404b540aSrobert     inline void
swap(hash_map<_Value,_Tp,_HashFcn,_EqualKey,_Alloc> & __x,hash_map<_Value,_Tp,_HashFcn,_EqualKey,_Alloc> & __y)278*404b540aSrobert     swap(hash_map<_Value, _Tp, _HashFcn, _EqualKey, _Alloc>& __x,
279*404b540aSrobert 	 hash_map<_Value, _Tp, _HashFcn, _EqualKey, _Alloc>& __y)
280*404b540aSrobert     { __x.swap(__y); }
281*404b540aSrobert } // namespace __debug
282*404b540aSrobert } // namespace __gnu_cxx
283*404b540aSrobert 
284*404b540aSrobert #endif
285