Merge from mainline
[official-gcc.git] / libstdc++-v3 / include / debug / hash_set.h
blobf5477f099cb58f40def5ab1d94af2a4aefd5baea
1 // Debugging hash_set 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_SET_H
32 #define _GLIBCXX_DEBUG_HASH_SET_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,
42 typename _HashFcn = __gnu_cxx::hash<_Value>,
43 typename _EqualKey = std::equal_to<_Value>,
44 typename _Alloc = std::allocator<_Value> >
45 class hash_set
46 : public __gnu_cxx::hash_set<_Value, _HashFcn, _EqualKey, _Alloc>,
47 public __gnu_debug::_Safe_sequence<hash_set<_Value, _HashFcn, _EqualKey,
48 _Alloc> >
50 typedef __gnu_cxx::hash_set<_Value, _HashFcn, _EqualKey, _Alloc> _Base;
51 typedef __gnu_debug::_Safe_sequence<hash_set> _Safe_base;
53 public:
54 typedef typename _Base::key_type key_type;
55 typedef typename _Base::value_type value_type;
56 typedef typename _Base::hasher hasher;
57 typedef typename _Base::key_equal key_equal;
58 typedef typename _Base::size_type size_type;
59 typedef typename _Base::difference_type difference_type;
60 typedef typename _Base::pointer pointer;
61 typedef typename _Base::const_pointer const_pointer;
62 typedef typename _Base::reference reference;
63 typedef typename _Base::const_reference const_reference;
65 typedef __gnu_debug::_Safe_iterator<typename _Base::iterator, hash_set>
66 iterator;
67 typedef __gnu_debug::_Safe_iterator<typename _Base::const_iterator,
68 hash_set>
69 const_iterator;
71 typedef typename _Base::allocator_type allocator_type;
73 using _Base::hash_funct;
74 using _Base::key_eq;
75 using _Base::get_allocator;
77 hash_set() { }
79 explicit hash_set(size_type __n) : _Base(__n) { }
81 hash_set(size_type __n, const hasher& __hf) : _Base(__n, __hf) { }
83 hash_set(size_type __n, const hasher& __hf, const key_equal& __eql,
84 const allocator_type& __a = allocator_type())
85 : _Base(__n, __hf, __eql, __a) { }
87 template<typename _InputIterator>
88 hash_set(_InputIterator __f, _InputIterator __l)
89 : _Base(__gnu_debug::__check_valid_range(__f, __l), __l) { }
91 template<typename _InputIterator>
92 hash_set(_InputIterator __f, _InputIterator __l, size_type __n)
93 : _Base(__gnu_debug::__check_valid_range(__f, __l), __l, __n) { }
95 template<typename _InputIterator>
96 hash_set(_InputIterator __f, _InputIterator __l, size_type __n,
97 const hasher& __hf)
98 : _Base(__gnu_debug::__check_valid_range(__f, __l), __l, __n, __hf) { }
100 template<typename _InputIterator>
101 hash_set(_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) { }
107 hash_set(const _Base& __x) : _Base(__x), _Safe_base() { }
109 using _Base::size;
110 using _Base::max_size;
111 using _Base::empty;
113 void
114 swap(hash_set& __x)
116 _Base::swap(__x);
117 this->_M_swap(__x);
120 iterator
121 begin() const { return iterator(_Base::begin(), this); }
123 iterator
124 end() const { return iterator(_Base::end(), this); }
126 std::pair<iterator, bool>
127 insert(const value_type& __obj)
129 std::pair<typename _Base::iterator, bool> __res =
130 _Base::insert(__obj);
131 return std::make_pair(iterator(__res.first, this), __res.second);
134 template <typename _InputIterator>
135 void
136 insert(_InputIterator __first, _InputIterator __last)
138 __glibcxx_check_valid_range(__first, __last);
139 _Base::insert(__first.base(), __last.base());
143 std::pair<iterator, bool>
144 insert_noresize(const value_type& __obj)
146 std::pair<typename _Base::iterator, bool> __res =
147 _Base::insert_noresize(__obj);
148 return std::make_pair(iterator(__res.first, this), __res.second);
151 iterator
152 find(const key_type& __key) const
153 { return iterator(_Base::find(__key), this); }
155 using _Base::count;
157 std::pair<iterator, iterator>
158 equal_range(const key_type& __key) const
160 typedef typename _Base::iterator _Base_iterator;
161 std::pair<_Base_iterator, _Base_iterator> __res =
162 _Base::equal_range(__key);
163 return std::make_pair(iterator(__res.first, this),
164 iterator(__res.second, this));
167 size_type
168 erase(const key_type& __key)
170 iterator __victim(_Base::find(__key), this);
171 if (__victim != end())
172 return this->erase(__victim), 1;
173 else
174 return 0;
177 void
178 erase(iterator __it)
180 __glibcxx_check_erase(__it);
181 __it._M_invalidate();
182 _Base::erase(__it.base());
185 void
186 erase(iterator __first, iterator __last)
188 __glibcxx_check_erase_range(__first, __last);
189 for (iterator __tmp = __first; __tmp != __last;)
191 iterator __victim = __tmp++;
192 __victim._M_invalidate();
194 _Base::erase(__first.base(), __last.base());
197 void
198 clear()
200 _Base::clear();
201 this->_M_invalidate_all();
204 using _Base::resize;
205 using _Base::bucket_count;
206 using _Base::max_bucket_count;
207 using _Base::elems_in_bucket;
209 _Base&
210 _M_base() { return *this; }
212 const _Base&
213 _M_base() const { return *this; }
215 private:
216 void
217 _M_invalidate_all()
219 typedef typename _Base::const_iterator _Base_const_iterator;
220 typedef __gnu_debug::_Not_equal_to<_Base_const_iterator> _Not_equal;
221 this->_M_invalidate_if(_Not_equal(_M_base().end()));
225 template<typename _Value, typename _HashFcn, typename _EqualKey,
226 typename _Alloc>
227 inline bool
228 operator==(const hash_set<_Value, _HashFcn, _EqualKey, _Alloc>& __x,
229 const hash_set<_Value, _HashFcn, _EqualKey, _Alloc>& __y)
230 { return __x._M_base() == __y._M_base(); }
232 template<typename _Value, typename _HashFcn, typename _EqualKey,
233 typename _Alloc>
234 inline bool
235 operator!=(const hash_set<_Value, _HashFcn, _EqualKey, _Alloc>& __x,
236 const hash_set<_Value, _HashFcn, _EqualKey, _Alloc>& __y)
237 { return __x._M_base() != __y._M_base(); }
239 template<typename _Value, typename _HashFcn, typename _EqualKey,
240 typename _Alloc>
241 inline void
242 swap(hash_set<_Value, _HashFcn, _EqualKey, _Alloc>& __x,
243 hash_set<_Value, _HashFcn, _EqualKey, _Alloc>& __y)
244 { __x.swap(__y); }
245 } // namespace __gnu_debug
246 } // namespace __gnu_cxx
248 #endif