1 // Hashing map 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_map
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_MAP
58 #define _BACKWARD_HASH_MAP 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)
70 using std::_Select1st;
73 * This is an SGI extension.
74 * @ingroup SGIextensions
77 template<class _Key, class _Tp, class _HashFn = hash<_Key>,
78 class _EqualKey = equal_to<_Key>, class _Alloc = allocator<_Tp> >
82 typedef hashtable<pair<const _Key, _Tp>,_Key, _HashFn,
83 _Select1st<pair<const _Key, _Tp> >,
84 _EqualKey, _Alloc> _Ht;
89 typedef typename _Ht::key_type key_type;
90 typedef _Tp data_type;
91 typedef _Tp mapped_type;
92 typedef typename _Ht::value_type value_type;
93 typedef typename _Ht::hasher hasher;
94 typedef typename _Ht::key_equal key_equal;
96 typedef typename _Ht::size_type size_type;
97 typedef typename _Ht::difference_type difference_type;
98 typedef typename _Ht::pointer pointer;
99 typedef typename _Ht::const_pointer const_pointer;
100 typedef typename _Ht::reference reference;
101 typedef typename _Ht::const_reference const_reference;
103 typedef typename _Ht::iterator iterator;
104 typedef typename _Ht::const_iterator const_iterator;
106 typedef typename _Ht::allocator_type allocator_type;
110 { return _M_ht.hash_funct(); }
114 { return _M_ht.key_eq(); }
117 get_allocator() const
118 { return _M_ht.get_allocator(); }
121 : _M_ht(100, hasher(), key_equal(), allocator_type()) {}
124 hash_map(size_type __n)
125 : _M_ht(__n, hasher(), key_equal(), allocator_type()) {}
127 hash_map(size_type __n, const hasher& __hf)
128 : _M_ht(__n, __hf, key_equal(), allocator_type()) {}
130 hash_map(size_type __n, const hasher& __hf, const key_equal& __eql,
131 const allocator_type& __a = allocator_type())
132 : _M_ht(__n, __hf, __eql, __a) {}
134 template<class _InputIterator>
135 hash_map(_InputIterator __f, _InputIterator __l)
136 : _M_ht(100, hasher(), key_equal(), allocator_type())
137 { _M_ht.insert_unique(__f, __l); }
139 template<class _InputIterator>
140 hash_map(_InputIterator __f, _InputIterator __l, size_type __n)
141 : _M_ht(__n, hasher(), key_equal(), allocator_type())
142 { _M_ht.insert_unique(__f, __l); }
144 template<class _InputIterator>
145 hash_map(_InputIterator __f, _InputIterator __l, size_type __n,
147 : _M_ht(__n, __hf, key_equal(), allocator_type())
148 { _M_ht.insert_unique(__f, __l); }
150 template<class _InputIterator>
151 hash_map(_InputIterator __f, _InputIterator __l, size_type __n,
152 const hasher& __hf, const key_equal& __eql,
153 const allocator_type& __a = allocator_type())
154 : _M_ht(__n, __hf, __eql, __a)
155 { _M_ht.insert_unique(__f, __l); }
159 { return _M_ht.size(); }
163 { return _M_ht.max_size(); }
167 { return _M_ht.empty(); }
171 { _M_ht.swap(__hs._M_ht); }
173 template<class _K1, class _T1, class _HF, class _EqK, class _Al>
175 operator== (const hash_map<_K1, _T1, _HF, _EqK, _Al>&,
176 const hash_map<_K1, _T1, _HF, _EqK, _Al>&);
180 { return _M_ht.begin(); }
184 { return _M_ht.end(); }
188 { return _M_ht.begin(); }
192 { return _M_ht.end(); }
195 insert(const value_type& __obj)
196 { return _M_ht.insert_unique(__obj); }
198 template<class _InputIterator>
200 insert(_InputIterator __f, _InputIterator __l)
201 { _M_ht.insert_unique(__f, __l); }
204 insert_noresize(const value_type& __obj)
205 { return _M_ht.insert_unique_noresize(__obj); }
208 find(const key_type& __key)
209 { return _M_ht.find(__key); }
212 find(const key_type& __key) const
213 { return _M_ht.find(__key); }
216 operator[](const key_type& __key)
217 { return _M_ht.find_or_insert(value_type(__key, _Tp())).second; }
220 count(const key_type& __key) const
221 { return _M_ht.count(__key); }
223 pair<iterator, iterator>
224 equal_range(const key_type& __key)
225 { return _M_ht.equal_range(__key); }
227 pair<const_iterator, const_iterator>
228 equal_range(const key_type& __key) const
229 { return _M_ht.equal_range(__key); }
232 erase(const key_type& __key)
233 {return _M_ht.erase(__key); }
237 { _M_ht.erase(__it); }
240 erase(iterator __f, iterator __l)
241 { _M_ht.erase(__f, __l); }
248 resize(size_type __hint)
249 { _M_ht.resize(__hint); }
253 { return _M_ht.bucket_count(); }
256 max_bucket_count() const
257 { return _M_ht.max_bucket_count(); }
260 elems_in_bucket(size_type __n) const
261 { return _M_ht.elems_in_bucket(__n); }
264 template<class _Key, class _Tp, class _HashFn, class _EqlKey, class _Alloc>
266 operator==(const hash_map<_Key, _Tp, _HashFn, _EqlKey, _Alloc>& __hm1,
267 const hash_map<_Key, _Tp, _HashFn, _EqlKey, _Alloc>& __hm2)
268 { return __hm1._M_ht == __hm2._M_ht; }
270 template<class _Key, class _Tp, class _HashFn, class _EqlKey, class _Alloc>
272 operator!=(const hash_map<_Key, _Tp, _HashFn, _EqlKey, _Alloc>& __hm1,
273 const hash_map<_Key, _Tp, _HashFn, _EqlKey, _Alloc>& __hm2)
274 { return !(__hm1 == __hm2); }
276 template<class _Key, class _Tp, class _HashFn, class _EqlKey, class _Alloc>
278 swap(hash_map<_Key, _Tp, _HashFn, _EqlKey, _Alloc>& __hm1,
279 hash_map<_Key, _Tp, _HashFn, _EqlKey, _Alloc>& __hm2)
280 { __hm1.swap(__hm2); }
284 * This is an SGI extension.
285 * @ingroup SGIextensions
288 template<class _Key, class _Tp,
289 class _HashFn = hash<_Key>,
290 class _EqualKey = equal_to<_Key>,
291 class _Alloc = allocator<_Tp> >
294 // concept requirements
295 __glibcxx_class_requires(_Key, _SGIAssignableConcept)
296 __glibcxx_class_requires(_Tp, _SGIAssignableConcept)
297 __glibcxx_class_requires3(_HashFn, size_t, _Key, _UnaryFunctionConcept)
298 __glibcxx_class_requires3(_EqualKey, _Key, _Key, _BinaryPredicateConcept)
301 typedef hashtable<pair<const _Key, _Tp>, _Key, _HashFn,
302 _Select1st<pair<const _Key, _Tp> >, _EqualKey, _Alloc>
308 typedef typename _Ht::key_type key_type;
309 typedef _Tp data_type;
310 typedef _Tp mapped_type;
311 typedef typename _Ht::value_type value_type;
312 typedef typename _Ht::hasher hasher;
313 typedef typename _Ht::key_equal key_equal;
315 typedef typename _Ht::size_type size_type;
316 typedef typename _Ht::difference_type difference_type;
317 typedef typename _Ht::pointer pointer;
318 typedef typename _Ht::const_pointer const_pointer;
319 typedef typename _Ht::reference reference;
320 typedef typename _Ht::const_reference const_reference;
322 typedef typename _Ht::iterator iterator;
323 typedef typename _Ht::const_iterator const_iterator;
325 typedef typename _Ht::allocator_type allocator_type;
329 { return _M_ht.hash_funct(); }
333 { return _M_ht.key_eq(); }
336 get_allocator() const
337 { return _M_ht.get_allocator(); }
340 : _M_ht(100, hasher(), key_equal(), allocator_type()) {}
343 hash_multimap(size_type __n)
344 : _M_ht(__n, hasher(), key_equal(), allocator_type()) {}
346 hash_multimap(size_type __n, const hasher& __hf)
347 : _M_ht(__n, __hf, key_equal(), allocator_type()) {}
349 hash_multimap(size_type __n, const hasher& __hf, const key_equal& __eql,
350 const allocator_type& __a = allocator_type())
351 : _M_ht(__n, __hf, __eql, __a) {}
353 template<class _InputIterator>
354 hash_multimap(_InputIterator __f, _InputIterator __l)
355 : _M_ht(100, hasher(), key_equal(), allocator_type())
356 { _M_ht.insert_equal(__f, __l); }
358 template<class _InputIterator>
359 hash_multimap(_InputIterator __f, _InputIterator __l, size_type __n)
360 : _M_ht(__n, hasher(), key_equal(), allocator_type())
361 { _M_ht.insert_equal(__f, __l); }
363 template<class _InputIterator>
364 hash_multimap(_InputIterator __f, _InputIterator __l, size_type __n,
366 : _M_ht(__n, __hf, key_equal(), allocator_type())
367 { _M_ht.insert_equal(__f, __l); }
369 template<class _InputIterator>
370 hash_multimap(_InputIterator __f, _InputIterator __l, size_type __n,
371 const hasher& __hf, const key_equal& __eql,
372 const allocator_type& __a = allocator_type())
373 : _M_ht(__n, __hf, __eql, __a)
374 { _M_ht.insert_equal(__f, __l); }
378 { return _M_ht.size(); }
382 { return _M_ht.max_size(); }
386 { return _M_ht.empty(); }
389 swap(hash_multimap& __hs)
390 { _M_ht.swap(__hs._M_ht); }
392 template<class _K1, class _T1, class _HF, class _EqK, class _Al>
394 operator==(const hash_multimap<_K1, _T1, _HF, _EqK, _Al>&,
395 const hash_multimap<_K1, _T1, _HF, _EqK, _Al>&);
399 { return _M_ht.begin(); }
403 { return _M_ht.end(); }
407 { return _M_ht.begin(); }
411 { return _M_ht.end(); }
414 insert(const value_type& __obj)
415 { return _M_ht.insert_equal(__obj); }
417 template<class _InputIterator>
419 insert(_InputIterator __f, _InputIterator __l)
420 { _M_ht.insert_equal(__f,__l); }
423 insert_noresize(const value_type& __obj)
424 { return _M_ht.insert_equal_noresize(__obj); }
427 find(const key_type& __key)
428 { return _M_ht.find(__key); }
431 find(const key_type& __key) const
432 { return _M_ht.find(__key); }
435 count(const key_type& __key) const
436 { return _M_ht.count(__key); }
438 pair<iterator, iterator>
439 equal_range(const key_type& __key)
440 { return _M_ht.equal_range(__key); }
442 pair<const_iterator, const_iterator>
443 equal_range(const key_type& __key) const
444 { return _M_ht.equal_range(__key); }
447 erase(const key_type& __key)
448 { return _M_ht.erase(__key); }
452 { _M_ht.erase(__it); }
455 erase(iterator __f, iterator __l)
456 { _M_ht.erase(__f, __l); }
463 resize(size_type __hint)
464 { _M_ht.resize(__hint); }
468 { return _M_ht.bucket_count(); }
471 max_bucket_count() const
472 { return _M_ht.max_bucket_count(); }
475 elems_in_bucket(size_type __n) const
476 { return _M_ht.elems_in_bucket(__n); }
479 template<class _Key, class _Tp, class _HF, class _EqKey, class _Alloc>
481 operator==(const hash_multimap<_Key, _Tp, _HF, _EqKey, _Alloc>& __hm1,
482 const hash_multimap<_Key, _Tp, _HF, _EqKey, _Alloc>& __hm2)
483 { return __hm1._M_ht == __hm2._M_ht; }
485 template<class _Key, class _Tp, class _HF, class _EqKey, class _Alloc>
487 operator!=(const hash_multimap<_Key, _Tp, _HF, _EqKey, _Alloc>& __hm1,
488 const hash_multimap<_Key, _Tp, _HF, _EqKey, _Alloc>& __hm2)
489 { return !(__hm1 == __hm2); }
491 template<class _Key, class _Tp, class _HashFn, class _EqlKey, class _Alloc>
493 swap(hash_multimap<_Key, _Tp, _HashFn, _EqlKey, _Alloc>& __hm1,
494 hash_multimap<_Key, _Tp, _HashFn, _EqlKey, _Alloc>& __hm2)
495 { __hm1.swap(__hm2); }
497 _GLIBCXX_END_NAMESPACE
499 _GLIBCXX_BEGIN_NAMESPACE(std)
501 // Specialization of insert_iterator so that it will work for hash_map
502 // and hash_multimap.
503 template<class _Key, class _Tp, class _HashFn, class _EqKey, class _Alloc>
504 class insert_iterator<__gnu_cxx::hash_map<_Key, _Tp, _HashFn,
508 typedef __gnu_cxx::hash_map<_Key, _Tp, _HashFn, _EqKey, _Alloc>
510 _Container* container;
513 typedef _Container container_type;
514 typedef output_iterator_tag iterator_category;
515 typedef void value_type;
516 typedef void difference_type;
517 typedef void pointer;
518 typedef void reference;
520 insert_iterator(_Container& __x)
523 insert_iterator(_Container& __x, typename _Container::iterator)
526 insert_iterator<_Container>&
527 operator=(const typename _Container::value_type& __value)
529 container->insert(__value);
533 insert_iterator<_Container>&
537 insert_iterator<_Container>&
538 operator++() { return *this; }
540 insert_iterator<_Container>&
545 template<class _Key, class _Tp, class _HashFn, class _EqKey, class _Alloc>
546 class insert_iterator<__gnu_cxx::hash_multimap<_Key, _Tp, _HashFn,
550 typedef __gnu_cxx::hash_multimap<_Key, _Tp, _HashFn, _EqKey, _Alloc>
552 _Container* container;
553 typename _Container::iterator iter;
556 typedef _Container container_type;
557 typedef output_iterator_tag iterator_category;
558 typedef void value_type;
559 typedef void difference_type;
560 typedef void pointer;
561 typedef void reference;
563 insert_iterator(_Container& __x)
566 insert_iterator(_Container& __x, typename _Container::iterator)
569 insert_iterator<_Container>&
570 operator=(const typename _Container::value_type& __value)
572 container->insert(__value);
576 insert_iterator<_Container>&
580 insert_iterator<_Container>&
584 insert_iterator<_Container>&
589 _GLIBCXX_END_NAMESPACE