1 // Hashing set implementation -*- C++ -*-
3 // Copyright (C) 2001, 2002, 2004, 2005, 2006, 2009, 2010
4 // Free Software Foundation, Inc.
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 3, or (at your option)
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 // Under Section 7 of GPL version 3, you are granted additional
18 // permissions described in the GCC Runtime Library Exception, version
19 // 3.1, as published by the Free Software Foundation.
21 // You should have received a copy of the GNU General Public License and
22 // a copy of the GCC Runtime Library Exception along with this program;
23 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
24 // <http://www.gnu.org/licenses/>.
28 * Silicon Graphics Computer Systems, Inc.
30 * Permission to use, copy, modify, distribute and sell this software
31 * and its documentation for any purpose is hereby granted without fee,
32 * provided that the above copyright notice appear in all copies and
33 * that both that copyright notice and this permission notice appear
34 * in supporting documentation. Silicon Graphics makes no
35 * representations about the suitability of this software for any
36 * purpose. It is provided "as is" without express or implied warranty.
40 * Hewlett-Packard Company
42 * Permission to use, copy, modify, distribute and sell this software
43 * and its documentation for any purpose is hereby granted without fee,
44 * provided that the above copyright notice appear in all copies and
45 * that both that copyright notice and this permission notice appear
46 * in supporting documentation. Hewlett-Packard Company makes no
47 * representations about the suitability of this software for any
48 * purpose. It is provided "as is" without express or implied warranty.
52 /** @file backward/hash_set
53 * This file is a GNU extension to the Standard C++ Library (possibly
54 * containing extensions from the HP/SGI STL subset).
57 #ifndef _BACKWARD_HASH_SET
58 #define _BACKWARD_HASH_SET 1
60 #include "backward_warning.h"
61 #include <bits/c++config.h>
62 #include <backward/hashtable.h>
63 #include <bits/concept_check.h>
65 _GLIBCXX_BEGIN_NAMESPACE(__gnu_cxx)
73 * This is an SGI extension.
74 * @ingroup SGIextensions
77 template<class _Value, class _HashFcn = hash<_Value>,
78 class _EqualKey = equal_to<_Value>,
79 class _Alloc = allocator<_Value> >
82 // concept requirements
83 __glibcxx_class_requires(_Value, _SGIAssignableConcept)
84 __glibcxx_class_requires3(_HashFcn, size_t, _Value, _UnaryFunctionConcept)
85 __glibcxx_class_requires3(_EqualKey, _Value, _Value, _BinaryPredicateConcept)
88 typedef hashtable<_Value, _Value, _HashFcn, _Identity<_Value>,
89 _EqualKey, _Alloc> _Ht;
93 typedef typename _Ht::key_type key_type;
94 typedef typename _Ht::value_type value_type;
95 typedef typename _Ht::hasher hasher;
96 typedef typename _Ht::key_equal key_equal;
98 typedef typename _Ht::size_type size_type;
99 typedef typename _Ht::difference_type difference_type;
100 typedef typename _Alloc::pointer pointer;
101 typedef typename _Alloc::const_pointer const_pointer;
102 typedef typename _Alloc::reference reference;
103 typedef typename _Alloc::const_reference const_reference;
105 typedef typename _Ht::const_iterator iterator;
106 typedef typename _Ht::const_iterator const_iterator;
108 typedef typename _Ht::allocator_type allocator_type;
112 { return _M_ht.hash_funct(); }
116 { return _M_ht.key_eq(); }
119 get_allocator() const
120 { return _M_ht.get_allocator(); }
123 : _M_ht(100, hasher(), key_equal(), allocator_type()) {}
126 hash_set(size_type __n)
127 : _M_ht(__n, hasher(), key_equal(), allocator_type()) {}
129 hash_set(size_type __n, const hasher& __hf)
130 : _M_ht(__n, __hf, key_equal(), allocator_type()) {}
132 hash_set(size_type __n, const hasher& __hf, const key_equal& __eql,
133 const allocator_type& __a = allocator_type())
134 : _M_ht(__n, __hf, __eql, __a) {}
136 template<class _InputIterator>
137 hash_set(_InputIterator __f, _InputIterator __l)
138 : _M_ht(100, hasher(), key_equal(), allocator_type())
139 { _M_ht.insert_unique(__f, __l); }
141 template<class _InputIterator>
142 hash_set(_InputIterator __f, _InputIterator __l, size_type __n)
143 : _M_ht(__n, hasher(), key_equal(), allocator_type())
144 { _M_ht.insert_unique(__f, __l); }
146 template<class _InputIterator>
147 hash_set(_InputIterator __f, _InputIterator __l, size_type __n,
149 : _M_ht(__n, __hf, key_equal(), allocator_type())
150 { _M_ht.insert_unique(__f, __l); }
152 template<class _InputIterator>
153 hash_set(_InputIterator __f, _InputIterator __l, size_type __n,
154 const hasher& __hf, const key_equal& __eql,
155 const allocator_type& __a = allocator_type())
156 : _M_ht(__n, __hf, __eql, __a)
157 { _M_ht.insert_unique(__f, __l); }
161 { return _M_ht.size(); }
165 { return _M_ht.max_size(); }
169 { return _M_ht.empty(); }
173 { _M_ht.swap(__hs._M_ht); }
175 template<class _Val, class _HF, class _EqK, class _Al>
177 operator==(const hash_set<_Val, _HF, _EqK, _Al>&,
178 const hash_set<_Val, _HF, _EqK, _Al>&);
182 { return _M_ht.begin(); }
186 { return _M_ht.end(); }
189 insert(const value_type& __obj)
191 pair<typename _Ht::iterator, bool> __p = _M_ht.insert_unique(__obj);
192 return pair<iterator,bool>(__p.first, __p.second);
195 template<class _InputIterator>
197 insert(_InputIterator __f, _InputIterator __l)
198 { _M_ht.insert_unique(__f, __l); }
201 insert_noresize(const value_type& __obj)
203 pair<typename _Ht::iterator, bool> __p
204 = _M_ht.insert_unique_noresize(__obj);
205 return pair<iterator, bool>(__p.first, __p.second);
209 find(const key_type& __key) const
210 { return _M_ht.find(__key); }
213 count(const key_type& __key) const
214 { return _M_ht.count(__key); }
216 pair<iterator, iterator>
217 equal_range(const key_type& __key) const
218 { return _M_ht.equal_range(__key); }
221 erase(const key_type& __key)
222 {return _M_ht.erase(__key); }
226 { _M_ht.erase(__it); }
229 erase(iterator __f, iterator __l)
230 { _M_ht.erase(__f, __l); }
237 resize(size_type __hint)
238 { _M_ht.resize(__hint); }
242 { return _M_ht.bucket_count(); }
245 max_bucket_count() const
246 { return _M_ht.max_bucket_count(); }
249 elems_in_bucket(size_type __n) const
250 { return _M_ht.elems_in_bucket(__n); }
253 template<class _Value, class _HashFcn, class _EqualKey, class _Alloc>
255 operator==(const hash_set<_Value, _HashFcn, _EqualKey, _Alloc>& __hs1,
256 const hash_set<_Value, _HashFcn, _EqualKey, _Alloc>& __hs2)
257 { return __hs1._M_ht == __hs2._M_ht; }
259 template<class _Value, class _HashFcn, class _EqualKey, class _Alloc>
261 operator!=(const hash_set<_Value, _HashFcn, _EqualKey, _Alloc>& __hs1,
262 const hash_set<_Value, _HashFcn, _EqualKey, _Alloc>& __hs2)
263 { return !(__hs1 == __hs2); }
265 template<class _Val, class _HashFcn, class _EqualKey, class _Alloc>
267 swap(hash_set<_Val, _HashFcn, _EqualKey, _Alloc>& __hs1,
268 hash_set<_Val, _HashFcn, _EqualKey, _Alloc>& __hs2)
269 { __hs1.swap(__hs2); }
273 * This is an SGI extension.
274 * @ingroup SGIextensions
277 template<class _Value,
278 class _HashFcn = hash<_Value>,
279 class _EqualKey = equal_to<_Value>,
280 class _Alloc = allocator<_Value> >
283 // concept requirements
284 __glibcxx_class_requires(_Value, _SGIAssignableConcept)
285 __glibcxx_class_requires3(_HashFcn, size_t, _Value, _UnaryFunctionConcept)
286 __glibcxx_class_requires3(_EqualKey, _Value, _Value, _BinaryPredicateConcept)
289 typedef hashtable<_Value, _Value, _HashFcn, _Identity<_Value>,
290 _EqualKey, _Alloc> _Ht;
294 typedef typename _Ht::key_type key_type;
295 typedef typename _Ht::value_type value_type;
296 typedef typename _Ht::hasher hasher;
297 typedef typename _Ht::key_equal key_equal;
299 typedef typename _Ht::size_type size_type;
300 typedef typename _Ht::difference_type difference_type;
301 typedef typename _Alloc::pointer pointer;
302 typedef typename _Alloc::const_pointer const_pointer;
303 typedef typename _Alloc::reference reference;
304 typedef typename _Alloc::const_reference const_reference;
306 typedef typename _Ht::const_iterator iterator;
307 typedef typename _Ht::const_iterator const_iterator;
309 typedef typename _Ht::allocator_type allocator_type;
313 { return _M_ht.hash_funct(); }
317 { return _M_ht.key_eq(); }
320 get_allocator() const
321 { return _M_ht.get_allocator(); }
324 : _M_ht(100, hasher(), key_equal(), allocator_type()) {}
327 hash_multiset(size_type __n)
328 : _M_ht(__n, hasher(), key_equal(), allocator_type()) {}
330 hash_multiset(size_type __n, const hasher& __hf)
331 : _M_ht(__n, __hf, key_equal(), allocator_type()) {}
333 hash_multiset(size_type __n, const hasher& __hf, const key_equal& __eql,
334 const allocator_type& __a = allocator_type())
335 : _M_ht(__n, __hf, __eql, __a) {}
337 template<class _InputIterator>
338 hash_multiset(_InputIterator __f, _InputIterator __l)
339 : _M_ht(100, hasher(), key_equal(), allocator_type())
340 { _M_ht.insert_equal(__f, __l); }
342 template<class _InputIterator>
343 hash_multiset(_InputIterator __f, _InputIterator __l, size_type __n)
344 : _M_ht(__n, hasher(), key_equal(), allocator_type())
345 { _M_ht.insert_equal(__f, __l); }
347 template<class _InputIterator>
348 hash_multiset(_InputIterator __f, _InputIterator __l, size_type __n,
350 : _M_ht(__n, __hf, key_equal(), allocator_type())
351 { _M_ht.insert_equal(__f, __l); }
353 template<class _InputIterator>
354 hash_multiset(_InputIterator __f, _InputIterator __l, size_type __n,
355 const hasher& __hf, const key_equal& __eql,
356 const allocator_type& __a = allocator_type())
357 : _M_ht(__n, __hf, __eql, __a)
358 { _M_ht.insert_equal(__f, __l); }
362 { return _M_ht.size(); }
366 { return _M_ht.max_size(); }
370 { return _M_ht.empty(); }
373 swap(hash_multiset& hs)
374 { _M_ht.swap(hs._M_ht); }
376 template<class _Val, class _HF, class _EqK, class _Al>
378 operator==(const hash_multiset<_Val, _HF, _EqK, _Al>&,
379 const hash_multiset<_Val, _HF, _EqK, _Al>&);
383 { return _M_ht.begin(); }
387 { return _M_ht.end(); }
390 insert(const value_type& __obj)
391 { return _M_ht.insert_equal(__obj); }
393 template<class _InputIterator>
395 insert(_InputIterator __f, _InputIterator __l)
396 { _M_ht.insert_equal(__f,__l); }
399 insert_noresize(const value_type& __obj)
400 { return _M_ht.insert_equal_noresize(__obj); }
403 find(const key_type& __key) const
404 { return _M_ht.find(__key); }
407 count(const key_type& __key) const
408 { return _M_ht.count(__key); }
410 pair<iterator, iterator>
411 equal_range(const key_type& __key) const
412 { return _M_ht.equal_range(__key); }
415 erase(const key_type& __key)
416 { return _M_ht.erase(__key); }
420 { _M_ht.erase(__it); }
423 erase(iterator __f, iterator __l)
424 { _M_ht.erase(__f, __l); }
431 resize(size_type __hint)
432 { _M_ht.resize(__hint); }
436 { return _M_ht.bucket_count(); }
439 max_bucket_count() const
440 { return _M_ht.max_bucket_count(); }
443 elems_in_bucket(size_type __n) const
444 { return _M_ht.elems_in_bucket(__n); }
447 template<class _Val, class _HashFcn, class _EqualKey, class _Alloc>
449 operator==(const hash_multiset<_Val, _HashFcn, _EqualKey, _Alloc>& __hs1,
450 const hash_multiset<_Val, _HashFcn, _EqualKey, _Alloc>& __hs2)
451 { return __hs1._M_ht == __hs2._M_ht; }
453 template<class _Val, class _HashFcn, class _EqualKey, class _Alloc>
455 operator!=(const hash_multiset<_Val, _HashFcn, _EqualKey, _Alloc>& __hs1,
456 const hash_multiset<_Val, _HashFcn, _EqualKey, _Alloc>& __hs2)
457 { return !(__hs1 == __hs2); }
459 template<class _Val, class _HashFcn, class _EqualKey, class _Alloc>
461 swap(hash_multiset<_Val, _HashFcn, _EqualKey, _Alloc>& __hs1,
462 hash_multiset<_Val, _HashFcn, _EqualKey, _Alloc>& __hs2)
463 { __hs1.swap(__hs2); }
465 _GLIBCXX_END_NAMESPACE
467 _GLIBCXX_BEGIN_NAMESPACE(std)
469 // Specialization of insert_iterator so that it will work for hash_set
470 // and hash_multiset.
471 template<class _Value, class _HashFcn, class _EqualKey, class _Alloc>
472 class insert_iterator<__gnu_cxx::hash_set<_Value, _HashFcn,
476 typedef __gnu_cxx::hash_set<_Value, _HashFcn, _EqualKey, _Alloc>
478 _Container* container;
481 typedef _Container container_type;
482 typedef output_iterator_tag iterator_category;
483 typedef void value_type;
484 typedef void difference_type;
485 typedef void pointer;
486 typedef void reference;
488 insert_iterator(_Container& __x)
491 insert_iterator(_Container& __x, typename _Container::iterator)
494 insert_iterator<_Container>&
495 operator=(const typename _Container::value_type& __value)
497 container->insert(__value);
501 insert_iterator<_Container>&
505 insert_iterator<_Container>&
509 insert_iterator<_Container>&
514 template<class _Value, class _HashFcn, class _EqualKey, class _Alloc>
515 class insert_iterator<__gnu_cxx::hash_multiset<_Value, _HashFcn,
519 typedef __gnu_cxx::hash_multiset<_Value, _HashFcn, _EqualKey, _Alloc>
521 _Container* container;
522 typename _Container::iterator iter;
525 typedef _Container container_type;
526 typedef output_iterator_tag iterator_category;
527 typedef void value_type;
528 typedef void difference_type;
529 typedef void pointer;
530 typedef void reference;
532 insert_iterator(_Container& __x)
535 insert_iterator(_Container& __x, typename _Container::iterator)
538 insert_iterator<_Container>&
539 operator=(const typename _Container::value_type& __value)
541 container->insert(__value);
545 insert_iterator<_Container>&
549 insert_iterator<_Container>&
553 insert_iterator<_Container>&
554 operator++(int) { return *this; }
557 _GLIBCXX_END_NAMESPACE