1 // Hashing map implementation -*- C++ -*-
3 // Copyright (C) 2001, 2002, 2004, 2005 Free Software Foundation, Inc.
5 // This file is part of the GNU ISO C++ Library. This library is free
6 // software; you can redistribute it and/or modify it under the
7 // terms of the GNU General Public License as published by the
8 // Free Software Foundation; either version 2, or (at your option)
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // You should have received a copy of the GNU General Public License along
17 // with this library; see the file COPYING. If not, write to the Free
18 // Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
21 // As a special exception, you may use this file as part of a free software
22 // library without restriction. Specifically, if other files instantiate
23 // templates or use macros or inline functions from this file, or you compile
24 // this file and link it with other files to produce an executable, this
25 // file does not by itself cause the resulting executable to be covered by
26 // the GNU General Public License. This exception does not however
27 // invalidate any other reasons why the executable file might be covered by
28 // the GNU General Public License.
32 * Silicon Graphics Computer Systems, Inc.
34 * Permission to use, copy, modify, distribute and sell this software
35 * and its documentation for any purpose is hereby granted without fee,
36 * provided that the above copyright notice appear in all copies and
37 * that both that copyright notice and this permission notice appear
38 * in supporting documentation. Silicon Graphics makes no
39 * representations about the suitability of this software for any
40 * purpose. It is provided "as is" without express or implied warranty.
44 * Hewlett-Packard Company
46 * Permission to use, copy, modify, distribute and sell this software
47 * and its documentation for any purpose is hereby granted without fee,
48 * provided that the above copyright notice appear in all copies and
49 * that both that copyright notice and this permission notice appear
50 * in supporting documentation. Hewlett-Packard Company makes no
51 * representations about the suitability of this software for any
52 * purpose. It is provided "as is" without express or implied warranty.
56 /** @file ext/hash_map
57 * This file is a GNU extension to the Standard C++ Library (possibly
58 * containing extensions from the HP/SGI STL subset).
64 #include <ext/hashtable.h>
65 #include <bits/concept_check.h>
72 using std::_Select1st;
74 // Forward declaration of equality operator; needed for friend
76 template<class _Key, class _Tp, class _HashFcn = hash<_Key>,
77 class _EqualKey = equal_to<_Key>, class _Alloc = allocator<_Tp> >
80 template<class _Key, class _Tp, class _HashFn, class _EqKey, class _Alloc>
82 operator==(const hash_map<_Key, _Tp, _HashFn, _EqKey, _Alloc>&,
83 const hash_map<_Key, _Tp, _HashFn, _EqKey, _Alloc>&);
86 * This is an SGI extension.
87 * @ingroup SGIextensions
90 template <class _Key, class _Tp, class _HashFcn, class _EqualKey,
95 typedef hashtable<pair<const _Key, _Tp>,_Key, _HashFcn,
96 _Select1st<pair<const _Key, _Tp> >,
97 _EqualKey, _Alloc> _Ht;
102 typedef typename _Ht::key_type key_type;
103 typedef _Tp data_type;
104 typedef _Tp mapped_type;
105 typedef typename _Ht::value_type value_type;
106 typedef typename _Ht::hasher hasher;
107 typedef typename _Ht::key_equal key_equal;
109 typedef typename _Ht::size_type size_type;
110 typedef typename _Ht::difference_type difference_type;
111 typedef typename _Ht::pointer pointer;
112 typedef typename _Ht::const_pointer const_pointer;
113 typedef typename _Ht::reference reference;
114 typedef typename _Ht::const_reference const_reference;
116 typedef typename _Ht::iterator iterator;
117 typedef typename _Ht::const_iterator const_iterator;
119 typedef typename _Ht::allocator_type allocator_type;
123 { return _M_ht.hash_funct(); }
127 { return _M_ht.key_eq(); }
130 get_allocator() const
131 { return _M_ht.get_allocator(); }
135 : _M_ht(100, hasher(), key_equal(), allocator_type()) {}
138 hash_map(size_type __n)
139 : _M_ht(__n, hasher(), key_equal(), allocator_type()) {}
141 hash_map(size_type __n, const hasher& __hf)
142 : _M_ht(__n, __hf, key_equal(), allocator_type()) {}
144 hash_map(size_type __n, const hasher& __hf, const key_equal& __eql,
145 const allocator_type& __a = allocator_type())
146 : _M_ht(__n, __hf, __eql, __a) {}
148 template <class _InputIterator>
149 hash_map(_InputIterator __f, _InputIterator __l)
150 : _M_ht(100, hasher(), key_equal(), allocator_type())
151 { _M_ht.insert_unique(__f, __l); }
153 template <class _InputIterator>
154 hash_map(_InputIterator __f, _InputIterator __l, size_type __n)
155 : _M_ht(__n, hasher(), key_equal(), allocator_type())
156 { _M_ht.insert_unique(__f, __l); }
158 template <class _InputIterator>
159 hash_map(_InputIterator __f, _InputIterator __l, size_type __n,
161 : _M_ht(__n, __hf, key_equal(), allocator_type())
162 { _M_ht.insert_unique(__f, __l); }
164 template <class _InputIterator>
165 hash_map(_InputIterator __f, _InputIterator __l, size_type __n,
166 const hasher& __hf, const key_equal& __eql,
167 const allocator_type& __a = allocator_type())
168 : _M_ht(__n, __hf, __eql, __a)
169 { _M_ht.insert_unique(__f, __l); }
174 { return _M_ht.size(); }
178 { return _M_ht.max_size(); }
182 { return _M_ht.empty(); }
186 { _M_ht.swap(__hs._M_ht); }
188 template <class _K1, class _T1, class _HF, class _EqK, class _Al>
190 operator== (const hash_map<_K1, _T1, _HF, _EqK, _Al>&,
191 const hash_map<_K1, _T1, _HF, _EqK, _Al>&);
195 { return _M_ht.begin(); }
199 { return _M_ht.end(); }
203 { return _M_ht.begin(); }
207 { return _M_ht.end(); }
211 insert(const value_type& __obj)
212 { return _M_ht.insert_unique(__obj); }
214 template <class _InputIterator>
216 insert(_InputIterator __f, _InputIterator __l)
217 { _M_ht.insert_unique(__f, __l); }
220 insert_noresize(const value_type& __obj)
221 { return _M_ht.insert_unique_noresize(__obj); }
224 find(const key_type& __key)
225 { return _M_ht.find(__key); }
228 find(const key_type& __key) const
229 { return _M_ht.find(__key); }
232 operator[](const key_type& __key)
233 { return _M_ht.find_or_insert(value_type(__key, _Tp())).second; }
236 count(const key_type& __key) const
237 { return _M_ht.count(__key); }
239 pair<iterator, iterator>
240 equal_range(const key_type& __key)
241 { return _M_ht.equal_range(__key); }
243 pair<const_iterator, const_iterator>
244 equal_range(const key_type& __key) const
245 { return _M_ht.equal_range(__key); }
248 erase(const key_type& __key)
249 {return _M_ht.erase(__key); }
253 { _M_ht.erase(__it); }
256 erase(iterator __f, iterator __l)
257 { _M_ht.erase(__f, __l); }
264 resize(size_type __hint)
265 { _M_ht.resize(__hint); }
269 { return _M_ht.bucket_count(); }
272 max_bucket_count() const
273 { return _M_ht.max_bucket_count(); }
276 elems_in_bucket(size_type __n) const
277 { return _M_ht.elems_in_bucket(__n); }
280 template <class _Key, class _Tp, class _HashFcn, class _EqlKey, class _Alloc>
282 operator==(const hash_map<_Key, _Tp, _HashFcn, _EqlKey, _Alloc>& __hm1,
283 const hash_map<_Key, _Tp, _HashFcn, _EqlKey, _Alloc>& __hm2)
284 { return __hm1._M_ht == __hm2._M_ht; }
286 template <class _Key, class _Tp, class _HashFcn, class _EqlKey, class _Alloc>
288 operator!=(const hash_map<_Key, _Tp, _HashFcn, _EqlKey, _Alloc>& __hm1,
289 const hash_map<_Key, _Tp, _HashFcn, _EqlKey, _Alloc>& __hm2)
290 { return !(__hm1 == __hm2); }
292 template <class _Key, class _Tp, class _HashFcn, class _EqlKey, class _Alloc>
294 swap(hash_map<_Key, _Tp, _HashFcn, _EqlKey, _Alloc>& __hm1,
295 hash_map<_Key, _Tp, _HashFcn, _EqlKey, _Alloc>& __hm2)
296 { __hm1.swap(__hm2); }
298 // Forward declaration of equality operator; needed for friend declaration.
300 template <class _Key, class _Tp,
301 class _HashFcn = hash<_Key>,
302 class _EqualKey = equal_to<_Key>,
303 class _Alloc = allocator<_Tp> >
306 template <class _Key, class _Tp, class _HF, class _EqKey, class _Alloc>
308 operator==(const hash_multimap<_Key, _Tp, _HF, _EqKey, _Alloc>& __hm1,
309 const hash_multimap<_Key, _Tp, _HF, _EqKey, _Alloc>& __hm2);
312 * This is an SGI extension.
313 * @ingroup SGIextensions
316 template <class _Key, class _Tp, class _HashFcn, class _EqualKey,
320 // concept requirements
321 __glibcxx_class_requires(_Key, _SGIAssignableConcept)
322 __glibcxx_class_requires(_Tp, _SGIAssignableConcept)
323 __glibcxx_class_requires3(_HashFcn, size_t, _Key, _UnaryFunctionConcept)
324 __glibcxx_class_requires3(_EqualKey, _Key, _Key, _BinaryPredicateConcept)
327 typedef hashtable<pair<const _Key, _Tp>, _Key, _HashFcn,
328 _Select1st<pair<const _Key, _Tp> >, _EqualKey, _Alloc>
334 typedef typename _Ht::key_type key_type;
335 typedef _Tp data_type;
336 typedef _Tp mapped_type;
337 typedef typename _Ht::value_type value_type;
338 typedef typename _Ht::hasher hasher;
339 typedef typename _Ht::key_equal key_equal;
341 typedef typename _Ht::size_type size_type;
342 typedef typename _Ht::difference_type difference_type;
343 typedef typename _Ht::pointer pointer;
344 typedef typename _Ht::const_pointer const_pointer;
345 typedef typename _Ht::reference reference;
346 typedef typename _Ht::const_reference const_reference;
348 typedef typename _Ht::iterator iterator;
349 typedef typename _Ht::const_iterator const_iterator;
351 typedef typename _Ht::allocator_type allocator_type;
355 { return _M_ht.hash_funct(); }
359 { return _M_ht.key_eq(); }
362 get_allocator() const
363 { return _M_ht.get_allocator(); }
367 : _M_ht(100, hasher(), key_equal(), allocator_type()) {}
370 hash_multimap(size_type __n)
371 : _M_ht(__n, hasher(), key_equal(), allocator_type()) {}
373 hash_multimap(size_type __n, const hasher& __hf)
374 : _M_ht(__n, __hf, key_equal(), allocator_type()) {}
376 hash_multimap(size_type __n, const hasher& __hf, const key_equal& __eql,
377 const allocator_type& __a = allocator_type())
378 : _M_ht(__n, __hf, __eql, __a) {}
380 template <class _InputIterator>
381 hash_multimap(_InputIterator __f, _InputIterator __l)
382 : _M_ht(100, hasher(), key_equal(), allocator_type())
383 { _M_ht.insert_equal(__f, __l); }
385 template <class _InputIterator>
386 hash_multimap(_InputIterator __f, _InputIterator __l, size_type __n)
387 : _M_ht(__n, hasher(), key_equal(), allocator_type())
388 { _M_ht.insert_equal(__f, __l); }
390 template <class _InputIterator>
391 hash_multimap(_InputIterator __f, _InputIterator __l, size_type __n,
393 : _M_ht(__n, __hf, key_equal(), allocator_type())
394 { _M_ht.insert_equal(__f, __l); }
396 template <class _InputIterator>
397 hash_multimap(_InputIterator __f, _InputIterator __l, size_type __n,
398 const hasher& __hf, const key_equal& __eql,
399 const allocator_type& __a = allocator_type())
400 : _M_ht(__n, __hf, __eql, __a)
401 { _M_ht.insert_equal(__f, __l); }
406 { return _M_ht.size(); }
410 { return _M_ht.max_size(); }
414 { return _M_ht.empty(); }
417 swap(hash_multimap& __hs)
418 { _M_ht.swap(__hs._M_ht); }
420 template <class _K1, class _T1, class _HF, class _EqK, class _Al>
422 operator==(const hash_multimap<_K1, _T1, _HF, _EqK, _Al>&,
423 const hash_multimap<_K1, _T1, _HF, _EqK, _Al>&);
427 { return _M_ht.begin(); }
431 { return _M_ht.end(); }
435 { return _M_ht.begin(); }
439 { return _M_ht.end(); }
443 insert(const value_type& __obj)
444 { return _M_ht.insert_equal(__obj); }
446 template <class _InputIterator>
448 insert(_InputIterator __f, _InputIterator __l)
449 { _M_ht.insert_equal(__f,__l); }
452 insert_noresize(const value_type& __obj)
453 { return _M_ht.insert_equal_noresize(__obj); }
456 find(const key_type& __key)
457 { return _M_ht.find(__key); }
460 find(const key_type& __key) const
461 { return _M_ht.find(__key); }
464 count(const key_type& __key) const
465 { return _M_ht.count(__key); }
467 pair<iterator, iterator>
468 equal_range(const key_type& __key)
469 { return _M_ht.equal_range(__key); }
471 pair<const_iterator, const_iterator>
472 equal_range(const key_type& __key) const
473 { return _M_ht.equal_range(__key); }
476 erase(const key_type& __key)
477 { return _M_ht.erase(__key); }
481 { _M_ht.erase(__it); }
484 erase(iterator __f, iterator __l)
485 { _M_ht.erase(__f, __l); }
493 resize(size_type __hint)
494 { _M_ht.resize(__hint); }
498 { return _M_ht.bucket_count(); }
501 max_bucket_count() const
502 { return _M_ht.max_bucket_count(); }
505 elems_in_bucket(size_type __n) const
506 { return _M_ht.elems_in_bucket(__n); }
509 template <class _Key, class _Tp, class _HF, class _EqKey, class _Alloc>
511 operator==(const hash_multimap<_Key, _Tp, _HF, _EqKey, _Alloc>& __hm1,
512 const hash_multimap<_Key, _Tp, _HF, _EqKey, _Alloc>& __hm2)
513 { return __hm1._M_ht == __hm2._M_ht; }
515 template <class _Key, class _Tp, class _HF, class _EqKey, class _Alloc>
517 operator!=(const hash_multimap<_Key, _Tp, _HF, _EqKey, _Alloc>& __hm1,
518 const hash_multimap<_Key, _Tp, _HF, _EqKey, _Alloc>& __hm2)
519 { return !(__hm1 == __hm2); }
521 template <class _Key, class _Tp, class _HashFcn, class _EqlKey, class _Alloc>
523 swap(hash_multimap<_Key, _Tp, _HashFcn, _EqlKey, _Alloc>& __hm1,
524 hash_multimap<_Key, _Tp, _HashFcn, _EqlKey, _Alloc>& __hm2)
525 { __hm1.swap(__hm2); }
527 } // namespace __gnu_cxx
531 // Specialization of insert_iterator so that it will work for hash_map
532 // and hash_multimap.
534 template <class _Key, class _Tp, class _HashFn, class _EqKey, class _Alloc>
535 class insert_iterator<__gnu_cxx::hash_map<_Key, _Tp, _HashFn,
539 typedef __gnu_cxx::hash_map<_Key, _Tp, _HashFn, _EqKey, _Alloc>
541 _Container* container;
544 typedef _Container container_type;
545 typedef output_iterator_tag iterator_category;
546 typedef void value_type;
547 typedef void difference_type;
548 typedef void pointer;
549 typedef void reference;
551 insert_iterator(_Container& __x)
554 insert_iterator(_Container& __x, typename _Container::iterator)
557 insert_iterator<_Container>&
558 operator=(const typename _Container::value_type& __value)
560 container->insert(__value);
564 insert_iterator<_Container>&
568 insert_iterator<_Container>&
569 operator++() { return *this; }
571 insert_iterator<_Container>&
576 template <class _Key, class _Tp, class _HashFn, class _EqKey, class _Alloc>
577 class insert_iterator<__gnu_cxx::hash_multimap<_Key, _Tp, _HashFn,
581 typedef __gnu_cxx::hash_multimap<_Key, _Tp, _HashFn, _EqKey, _Alloc>
583 _Container* container;
584 typename _Container::iterator iter;
587 typedef _Container container_type;
588 typedef output_iterator_tag iterator_category;
589 typedef void value_type;
590 typedef void difference_type;
591 typedef void pointer;
592 typedef void reference;
594 insert_iterator(_Container& __x)
597 insert_iterator(_Container& __x, typename _Container::iterator)
600 insert_iterator<_Container>&
601 operator=(const typename _Container::value_type& __value)
603 container->insert(__value);
607 insert_iterator<_Container>&
611 insert_iterator<_Container>&
615 insert_iterator<_Container>&
621 #ifdef _GLIBCXX_DEBUG
622 # include <debug/hash_map>