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