Merge from mainline
[official-gcc.git] / libstdc++-v3 / include / debug / hash_map.h
blobd431d7d454dd3f129283e1427d553ab8839f6d79
1 // Debugging hash_map implementation -*- C++ -*-
3 // Copyright (C) 2003, 2005, 2006
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.
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.
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, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
20 // USA.
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.
31 #ifndef _GLIBCXX_DEBUG_HASH_MAP_H
32 #define _GLIBCXX_DEBUG_HASH_MAP_H 1
34 #include <debug/safe_sequence.h>
35 #include <debug/safe_iterator.h>
37 namespace __gnu_cxx
39 namespace __gnu_debug
41 template<typename _Value, typename _Tp,
42 typename _HashFcn = __gnu_cxx::hash<_Value>,
43 typename _EqualKey = std::equal_to<_Value>,
44 typename _Alloc = std::allocator<_Value> >
45 class hash_map
46 : public __gnu_cxx::hash_map<_Value, _Tp, _HashFcn, _EqualKey, _Alloc>,
47 public __gnu_debug::_Safe_sequence<hash_map<_Value, _Tp, _HashFcn,
48 _EqualKey, _Alloc> >
50 typedef __gnu_cxx::hash_map<_Value, _Tp, _HashFcn, _EqualKey, _Alloc>
51 _Base;
52 typedef __gnu_debug::_Safe_sequence<hash_map> _Safe_base;
54 public:
55 typedef typename _Base::key_type key_type;
56 typedef typename _Base::data_type data_type;
57 typedef typename _Base::mapped_type mapped_type;
58 typedef typename _Base::value_type value_type;
59 typedef typename _Base::hasher hasher;
60 typedef typename _Base::key_equal key_equal;
61 typedef typename _Base::size_type size_type;
62 typedef typename _Base::difference_type difference_type;
63 typedef typename _Base::pointer pointer;
64 typedef typename _Base::const_pointer const_pointer;
65 typedef typename _Base::reference reference;
66 typedef typename _Base::const_reference const_reference;
68 typedef __gnu_debug::_Safe_iterator<typename _Base::iterator, hash_map>
69 iterator;
70 typedef __gnu_debug::_Safe_iterator<typename _Base::const_iterator,
71 hash_map>
72 const_iterator;
74 typedef typename _Base::allocator_type allocator_type;
76 using _Base::hash_funct;
77 using _Base::key_eq;
78 using _Base::get_allocator;
80 hash_map() { }
82 explicit hash_map(size_type __n) : _Base(__n) { }
84 hash_map(size_type __n, const hasher& __hf) : _Base(__n, __hf) { }
86 hash_map(size_type __n, const hasher& __hf, const key_equal& __eql,
87 const allocator_type& __a = allocator_type())
88 : _Base(__n, __hf, __eql, __a) { }
90 template<typename _InputIterator>
91 hash_map(_InputIterator __f, _InputIterator __l)
92 : _Base(__gnu_debug::__check_valid_range(__f, __l), __l) { }
94 template<typename _InputIterator>
95 hash_map(_InputIterator __f, _InputIterator __l, size_type __n)
96 : _Base(__gnu_debug::__check_valid_range(__f, __l), __l, __n) { }
98 template<typename _InputIterator>
99 hash_map(_InputIterator __f, _InputIterator __l, size_type __n,
100 const hasher& __hf)
101 : _Base(__gnu_debug::__check_valid_range(__f, __l), __l, __n, __hf) { }
103 template<typename _InputIterator>
104 hash_map(_InputIterator __f, _InputIterator __l, size_type __n,
105 const hasher& __hf, const key_equal& __eql,
106 const allocator_type& __a = allocator_type())
107 : _Base(__gnu_debug::__check_valid_range(__f, __l), __l, __n, __hf,
108 __eql, __a) { }
110 hash_map(const _Base& __x) : _Base(__x), _Safe_base() { }
112 using _Base::size;
113 using _Base::max_size;
114 using _Base::empty;
116 void
117 swap(hash_map& __x)
119 _Base::swap(__x);
120 this->_M_swap(__x);
123 iterator
124 begin() { return iterator(_Base::begin(), this); }
126 iterator
127 end() { return iterator(_Base::end(), this); }
129 const_iterator
130 begin() const
131 { return const_iterator(_Base::begin(), this); }
133 const_iterator
134 end() const
135 { return const_iterator(_Base::end(), this); }
137 std::pair<iterator, bool>
138 insert(const value_type& __obj)
140 std::pair<typename _Base::iterator, bool> __res = _Base::insert(__obj);
141 return std::make_pair(iterator(__res.first, this), __res.second);
144 template <typename _InputIterator>
145 void
146 insert(_InputIterator __first, _InputIterator __last)
148 __glibcxx_check_valid_range(__first, __last);
149 _Base::insert(__first.base(), __last.base());
153 std::pair<iterator, bool>
154 insert_noresize(const value_type& __obj)
156 std::pair<typename _Base::iterator, bool> __res =
157 _Base::insert_noresize(__obj);
158 return std::make_pair(iterator(__res.first, this), __res.second);
161 iterator
162 find(const key_type& __key)
163 { return iterator(_Base::find(__key), this); }
165 const_iterator
166 find(const key_type& __key) const
167 { return const_iterator(_Base::find(__key), this); }
169 using _Base::operator[];
170 using _Base::count;
172 std::pair<iterator, iterator>
173 equal_range(const key_type& __key)
175 typedef typename _Base::iterator _Base_iterator;
176 std::pair<_Base_iterator, _Base_iterator> __res =
177 _Base::equal_range(__key);
178 return std::make_pair(iterator(__res.first, this),
179 iterator(__res.second, this));
182 std::pair<const_iterator, const_iterator>
183 equal_range(const key_type& __key) const
185 typedef typename _Base::const_iterator _Base_iterator;
186 std::pair<_Base_iterator, _Base_iterator> __res =
187 _Base::equal_range(__key);
188 return std::make_pair(const_iterator(__res.first, this),
189 const_iterator(__res.second, this));
192 size_type
193 erase(const key_type& __key)
195 iterator __victim(_Base::find(__key), this);
196 if (__victim != end())
197 return this->erase(__victim), 1;
198 else
199 return 0;
202 void
203 erase(iterator __it)
205 __glibcxx_check_erase(__it);
206 __it._M_invalidate();
207 _Base::erase(__it.base());
210 void
211 erase(iterator __first, iterator __last)
213 __glibcxx_check_erase_range(__first, __last);
214 for (iterator __tmp = __first; __tmp != __last;)
216 iterator __victim = __tmp++;
217 __victim._M_invalidate();
219 _Base::erase(__first.base(), __last.base());
222 void
223 clear()
225 _Base::clear();
226 this->_M_invalidate_all();
229 using _Base::resize;
230 using _Base::bucket_count;
231 using _Base::max_bucket_count;
232 using _Base::elems_in_bucket;
234 _Base&
235 _M_base() { return *this; }
237 const _Base&
238 _M_base() const { return *this; }
240 private:
241 void
242 _M_invalidate_all()
244 typedef typename _Base::const_iterator _Base_const_iterator;
245 typedef __gnu_debug::_Not_equal_to<_Base_const_iterator> _Not_equal;
246 this->_M_invalidate_if(_Not_equal(_M_base().end()));
250 template<typename _Value, typename _Tp, typename _HashFcn,
251 typename _EqualKey, typename _Alloc>
252 inline bool
253 operator==(const hash_map<_Value, _Tp, _HashFcn, _EqualKey, _Alloc>& __x,
254 const hash_map<_Value, _Tp, _HashFcn, _EqualKey, _Alloc>& __y)
255 { return __x._M_base() == __y._M_base(); }
257 template<typename _Value, typename _Tp, typename _HashFcn,
258 typename _EqualKey, typename _Alloc>
259 inline bool
260 operator!=(const hash_map<_Value, _Tp, _HashFcn, _EqualKey, _Alloc>& __x,
261 const hash_map<_Value, _Tp, _HashFcn, _EqualKey, _Alloc>& __y)
262 { return __x._M_base() != __y._M_base(); }
264 template<typename _Value, typename _Tp, typename _HashFcn,
265 typename _EqualKey, typename _Alloc>
266 inline void
267 swap(hash_map<_Value, _Tp, _HashFcn, _EqualKey, _Alloc>& __x,
268 hash_map<_Value, _Tp, _HashFcn, _EqualKey, _Alloc>& __y)
269 { __x.swap(__y); }
270 } // namespace __gnu_debug
271 } // namespace __gnu_cxx
273 #endif