hash_map revision 97403
198944Sobrien// Hashing map implementation -*- C++ -*- 2130803Smarcel 398944Sobrien// Copyright (C) 2001, 2002 Free Software Foundation, Inc. 498944Sobrien// 598944Sobrien// This file is part of the GNU ISO C++ Library. This library is free 698944Sobrien// software; you can redistribute it and/or modify it under the 798944Sobrien// terms of the GNU General Public License as published by the 898944Sobrien// Free Software Foundation; either version 2, or (at your option) 998944Sobrien// any later version. 1098944Sobrien 1198944Sobrien// This library is distributed in the hope that it will be useful, 1298944Sobrien// but WITHOUT ANY WARRANTY; without even the implied warranty of 1398944Sobrien// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 1498944Sobrien// GNU General Public License for more details. 1598944Sobrien 1698944Sobrien// You should have received a copy of the GNU General Public License along 1798944Sobrien// with this library; see the file COPYING. If not, write to the Free 1898944Sobrien// Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, 1998944Sobrien// USA. 2098944Sobrien 2198944Sobrien// As a special exception, you may use this file as part of a free software 2298944Sobrien// library without restriction. Specifically, if other files instantiate 2398944Sobrien// templates or use macros or inline functions from this file, or you compile 2498944Sobrien// this file and link it with other files to produce an executable, this 2598944Sobrien// file does not by itself cause the resulting executable to be covered by 2698944Sobrien// the GNU General Public License. This exception does not however 2798944Sobrien// invalidate any other reasons why the executable file might be covered by 28130803Smarcel// the GNU General Public License. 29130803Smarcel 30130803Smarcel/* 31130803Smarcel * Copyright (c) 1996 32130803Smarcel * Silicon Graphics Computer Systems, Inc. 33130803Smarcel * 34130803Smarcel * Permission to use, copy, modify, distribute and sell this software 35130803Smarcel * and its documentation for any purpose is hereby granted without fee, 36130803Smarcel * provided that the above copyright notice appear in all copies and 37130803Smarcel * that both that copyright notice and this permission notice appear 38130803Smarcel * in supporting documentation. Silicon Graphics makes no 39130803Smarcel * representations about the suitability of this software for any 40130803Smarcel * purpose. It is provided "as is" without express or implied warranty. 41130803Smarcel * 42130803Smarcel * 43130803Smarcel * Copyright (c) 1994 44130803Smarcel * Hewlett-Packard Company 45130803Smarcel * 46130803Smarcel * Permission to use, copy, modify, distribute and sell this software 4798944Sobrien * and its documentation for any purpose is hereby granted without fee, 4898944Sobrien * provided that the above copyright notice appear in all copies and 49130803Smarcel * that both that copyright notice and this permission notice appear 5098944Sobrien * in supporting documentation. Hewlett-Packard Company makes no 5198944Sobrien * representations about the suitability of this software for any 5298944Sobrien * purpose. It is provided "as is" without express or implied warranty. 5398944Sobrien * 54130803Smarcel */ 5598944Sobrien 5698944Sobrien/** @file ext/hash_map 5798944Sobrien * This file is a GNU extension to the Standard C++ Library (possibly 5898944Sobrien * containing extensions from the HP/SGI STL subset). You should only 5998944Sobrien * include this header if you are using GCC 3 or later. 6098944Sobrien */ 6198944Sobrien 6298944Sobrien#ifndef __SGI_STL_INTERNAL_HASH_MAP_H 6398944Sobrien#define __SGI_STL_INTERNAL_HASH_MAP_H 6498944Sobrien 6598944Sobrien#include <ext/stl_hashtable.h> 6698944Sobrien#include <bits/concept_check.h> 6798944Sobrien 6898944Sobriennamespace __gnu_cxx 6998944Sobrien{ 7098944Sobrienusing std::equal_to; 7198944Sobrienusing std::allocator; 7298944Sobrienusing std::pair; 7398944Sobrienusing std::_Select1st; 7498944Sobrien 7598944Sobrien// Forward declaration of equality operator; needed for friend declaration. 7698944Sobrien 7798944Sobrientemplate <class _Key, class _Tp, 7898944Sobrien class _HashFcn = hash<_Key>, 7998944Sobrien class _EqualKey = equal_to<_Key>, 8098944Sobrien class _Alloc = allocator<_Tp> > 8198944Sobrienclass hash_map; 8298944Sobrien 8398944Sobrientemplate <class _Key, class _Tp, class _HashFn, class _EqKey, class _Alloc> 8498944Sobrieninline bool operator==(const hash_map<_Key, _Tp, _HashFn, _EqKey, _Alloc>&, 8598944Sobrien const hash_map<_Key, _Tp, _HashFn, _EqKey, _Alloc>&); 8698944Sobrien 8798944Sobrientemplate <class _Key, class _Tp, class _HashFcn, class _EqualKey, 8898944Sobrien class _Alloc> 8998944Sobrienclass hash_map 9098944Sobrien{ 9198944Sobrienprivate: 9298944Sobrien typedef hashtable<pair<const _Key,_Tp>,_Key,_HashFcn, 9398944Sobrien _Select1st<pair<const _Key,_Tp> >,_EqualKey,_Alloc> _Ht; 9498944Sobrien _Ht _M_ht; 9598944Sobrien 9698944Sobrienpublic: 9798944Sobrien typedef typename _Ht::key_type key_type; 9898944Sobrien typedef _Tp data_type; 9998944Sobrien typedef _Tp mapped_type; 10098944Sobrien typedef typename _Ht::value_type value_type; 101130803Smarcel typedef typename _Ht::hasher hasher; 10298944Sobrien typedef typename _Ht::key_equal key_equal; 103130803Smarcel 104130803Smarcel typedef typename _Ht::size_type size_type; 105130803Smarcel typedef typename _Ht::difference_type difference_type; 10698944Sobrien typedef typename _Ht::pointer pointer; 10798944Sobrien typedef typename _Ht::const_pointer const_pointer; 108130803Smarcel typedef typename _Ht::reference reference; 109130803Smarcel typedef typename _Ht::const_reference const_reference; 110130803Smarcel 111130803Smarcel typedef typename _Ht::iterator iterator; 112130803Smarcel typedef typename _Ht::const_iterator const_iterator; 113130803Smarcel 11498944Sobrien typedef typename _Ht::allocator_type allocator_type; 11598944Sobrien 116130803Smarcel hasher hash_funct() const { return _M_ht.hash_funct(); } 117130803Smarcel key_equal key_eq() const { return _M_ht.key_eq(); } 118130803Smarcel allocator_type get_allocator() const { return _M_ht.get_allocator(); } 11998944Sobrien 12098944Sobrienpublic: 12198944Sobrien hash_map() : _M_ht(100, hasher(), key_equal(), allocator_type()) {} 12298944Sobrien explicit hash_map(size_type __n) 12398944Sobrien : _M_ht(__n, hasher(), key_equal(), allocator_type()) {} 12498944Sobrien hash_map(size_type __n, const hasher& __hf) 12598944Sobrien : _M_ht(__n, __hf, key_equal(), allocator_type()) {} 12698944Sobrien hash_map(size_type __n, const hasher& __hf, const key_equal& __eql, 12798944Sobrien const allocator_type& __a = allocator_type()) 12898944Sobrien : _M_ht(__n, __hf, __eql, __a) {} 12998944Sobrien 13098944Sobrien template <class _InputIterator> 13198944Sobrien hash_map(_InputIterator __f, _InputIterator __l) 13298944Sobrien : _M_ht(100, hasher(), key_equal(), allocator_type()) 13398944Sobrien { _M_ht.insert_unique(__f, __l); } 13498944Sobrien template <class _InputIterator> 13598944Sobrien hash_map(_InputIterator __f, _InputIterator __l, size_type __n) 13698944Sobrien : _M_ht(__n, hasher(), key_equal(), allocator_type()) 13798944Sobrien { _M_ht.insert_unique(__f, __l); } 13898944Sobrien template <class _InputIterator> 13998944Sobrien hash_map(_InputIterator __f, _InputIterator __l, size_type __n, 14098944Sobrien const hasher& __hf) 14198944Sobrien : _M_ht(__n, __hf, key_equal(), allocator_type()) 14298944Sobrien { _M_ht.insert_unique(__f, __l); } 14398944Sobrien template <class _InputIterator> 14498944Sobrien hash_map(_InputIterator __f, _InputIterator __l, size_type __n, 14598944Sobrien const hasher& __hf, const key_equal& __eql, 14698944Sobrien const allocator_type& __a = allocator_type()) 14798944Sobrien : _M_ht(__n, __hf, __eql, __a) 14898944Sobrien { _M_ht.insert_unique(__f, __l); } 14998944Sobrien 150public: 151 size_type size() const { return _M_ht.size(); } 152 size_type max_size() const { return _M_ht.max_size(); } 153 bool empty() const { return _M_ht.empty(); } 154 void swap(hash_map& __hs) { _M_ht.swap(__hs._M_ht); } 155 156 template <class _K1, class _T1, class _HF, class _EqK, class _Al> 157 friend bool operator== (const hash_map<_K1, _T1, _HF, _EqK, _Al>&, 158 const hash_map<_K1, _T1, _HF, _EqK, _Al>&); 159 160 iterator begin() { return _M_ht.begin(); } 161 iterator end() { return _M_ht.end(); } 162 const_iterator begin() const { return _M_ht.begin(); } 163 const_iterator end() const { return _M_ht.end(); } 164 165public: 166 pair<iterator,bool> insert(const value_type& __obj) 167 { return _M_ht.insert_unique(__obj); } 168 template <class _InputIterator> 169 void insert(_InputIterator __f, _InputIterator __l) 170 { _M_ht.insert_unique(__f,__l); } 171 pair<iterator,bool> insert_noresize(const value_type& __obj) 172 { return _M_ht.insert_unique_noresize(__obj); } 173 174 iterator find(const key_type& __key) { return _M_ht.find(__key); } 175 const_iterator find(const key_type& __key) const 176 { return _M_ht.find(__key); } 177 178 _Tp& operator[](const key_type& __key) { 179 return _M_ht.find_or_insert(value_type(__key, _Tp())).second; 180 } 181 182 size_type count(const key_type& __key) const { return _M_ht.count(__key); } 183 184 pair<iterator, iterator> equal_range(const key_type& __key) 185 { return _M_ht.equal_range(__key); } 186 pair<const_iterator, const_iterator> 187 equal_range(const key_type& __key) const 188 { return _M_ht.equal_range(__key); } 189 190 size_type erase(const key_type& __key) {return _M_ht.erase(__key); } 191 void erase(iterator __it) { _M_ht.erase(__it); } 192 void erase(iterator __f, iterator __l) { _M_ht.erase(__f, __l); } 193 void clear() { _M_ht.clear(); } 194 195 void resize(size_type __hint) { _M_ht.resize(__hint); } 196 size_type bucket_count() const { return _M_ht.bucket_count(); } 197 size_type max_bucket_count() const { return _M_ht.max_bucket_count(); } 198 size_type elems_in_bucket(size_type __n) const 199 { return _M_ht.elems_in_bucket(__n); } 200}; 201 202template <class _Key, class _Tp, class _HashFcn, class _EqlKey, class _Alloc> 203inline bool 204operator==(const hash_map<_Key,_Tp,_HashFcn,_EqlKey,_Alloc>& __hm1, 205 const hash_map<_Key,_Tp,_HashFcn,_EqlKey,_Alloc>& __hm2) 206{ 207 return __hm1._M_ht == __hm2._M_ht; 208} 209 210template <class _Key, class _Tp, class _HashFcn, class _EqlKey, class _Alloc> 211inline bool 212operator!=(const hash_map<_Key,_Tp,_HashFcn,_EqlKey,_Alloc>& __hm1, 213 const hash_map<_Key,_Tp,_HashFcn,_EqlKey,_Alloc>& __hm2) { 214 return !(__hm1 == __hm2); 215} 216 217template <class _Key, class _Tp, class _HashFcn, class _EqlKey, class _Alloc> 218inline void 219swap(hash_map<_Key,_Tp,_HashFcn,_EqlKey,_Alloc>& __hm1, 220 hash_map<_Key,_Tp,_HashFcn,_EqlKey,_Alloc>& __hm2) 221{ 222 __hm1.swap(__hm2); 223} 224 225// Forward declaration of equality operator; needed for friend declaration. 226 227template <class _Key, class _Tp, 228 class _HashFcn = hash<_Key>, 229 class _EqualKey = equal_to<_Key>, 230 class _Alloc = allocator<_Tp> > 231class hash_multimap; 232 233template <class _Key, class _Tp, class _HF, class _EqKey, class _Alloc> 234inline bool 235operator==(const hash_multimap<_Key,_Tp,_HF,_EqKey,_Alloc>& __hm1, 236 const hash_multimap<_Key,_Tp,_HF,_EqKey,_Alloc>& __hm2); 237 238template <class _Key, class _Tp, class _HashFcn, class _EqualKey, class _Alloc> 239class hash_multimap 240{ 241 // concept requirements 242 __glibcpp_class_requires(_Key, _SGIAssignableConcept) 243 __glibcpp_class_requires(_Tp, _SGIAssignableConcept) 244 __glibcpp_class_requires3(_HashFcn, size_t, _Key, _UnaryFunctionConcept); 245 __glibcpp_class_requires3(_EqualKey, _Key, _Key, _BinaryPredicateConcept); 246 247private: 248 typedef hashtable<pair<const _Key, _Tp>, _Key, _HashFcn, 249 _Select1st<pair<const _Key, _Tp> >, _EqualKey, _Alloc> 250 _Ht; 251 _Ht _M_ht; 252 253public: 254 typedef typename _Ht::key_type key_type; 255 typedef _Tp data_type; 256 typedef _Tp mapped_type; 257 typedef typename _Ht::value_type value_type; 258 typedef typename _Ht::hasher hasher; 259 typedef typename _Ht::key_equal key_equal; 260 261 typedef typename _Ht::size_type size_type; 262 typedef typename _Ht::difference_type difference_type; 263 typedef typename _Ht::pointer pointer; 264 typedef typename _Ht::const_pointer const_pointer; 265 typedef typename _Ht::reference reference; 266 typedef typename _Ht::const_reference const_reference; 267 268 typedef typename _Ht::iterator iterator; 269 typedef typename _Ht::const_iterator const_iterator; 270 271 typedef typename _Ht::allocator_type allocator_type; 272 273 hasher hash_funct() const { return _M_ht.hash_funct(); } 274 key_equal key_eq() const { return _M_ht.key_eq(); } 275 allocator_type get_allocator() const { return _M_ht.get_allocator(); } 276 277public: 278 hash_multimap() : _M_ht(100, hasher(), key_equal(), allocator_type()) {} 279 explicit hash_multimap(size_type __n) 280 : _M_ht(__n, hasher(), key_equal(), allocator_type()) {} 281 hash_multimap(size_type __n, const hasher& __hf) 282 : _M_ht(__n, __hf, key_equal(), allocator_type()) {} 283 hash_multimap(size_type __n, const hasher& __hf, const key_equal& __eql, 284 const allocator_type& __a = allocator_type()) 285 : _M_ht(__n, __hf, __eql, __a) {} 286 287 template <class _InputIterator> 288 hash_multimap(_InputIterator __f, _InputIterator __l) 289 : _M_ht(100, hasher(), key_equal(), allocator_type()) 290 { _M_ht.insert_equal(__f, __l); } 291 template <class _InputIterator> 292 hash_multimap(_InputIterator __f, _InputIterator __l, size_type __n) 293 : _M_ht(__n, hasher(), key_equal(), allocator_type()) 294 { _M_ht.insert_equal(__f, __l); } 295 template <class _InputIterator> 296 hash_multimap(_InputIterator __f, _InputIterator __l, size_type __n, 297 const hasher& __hf) 298 : _M_ht(__n, __hf, key_equal(), allocator_type()) 299 { _M_ht.insert_equal(__f, __l); } 300 template <class _InputIterator> 301 hash_multimap(_InputIterator __f, _InputIterator __l, size_type __n, 302 const hasher& __hf, const key_equal& __eql, 303 const allocator_type& __a = allocator_type()) 304 : _M_ht(__n, __hf, __eql, __a) 305 { _M_ht.insert_equal(__f, __l); } 306 307public: 308 size_type size() const { return _M_ht.size(); } 309 size_type max_size() const { return _M_ht.max_size(); } 310 bool empty() const { return _M_ht.empty(); } 311 void swap(hash_multimap& __hs) { _M_ht.swap(__hs._M_ht); } 312 313 template <class _K1, class _T1, class _HF, class _EqK, class _Al> 314 friend bool operator== (const hash_multimap<_K1, _T1, _HF, _EqK, _Al>&, 315 const hash_multimap<_K1, _T1, _HF, _EqK, _Al>&); 316 317 iterator begin() { return _M_ht.begin(); } 318 iterator end() { return _M_ht.end(); } 319 const_iterator begin() const { return _M_ht.begin(); } 320 const_iterator end() const { return _M_ht.end(); } 321 322public: 323 iterator insert(const value_type& __obj) 324 { return _M_ht.insert_equal(__obj); } 325 template <class _InputIterator> 326 void insert(_InputIterator __f, _InputIterator __l) 327 { _M_ht.insert_equal(__f,__l); } 328 iterator insert_noresize(const value_type& __obj) 329 { return _M_ht.insert_equal_noresize(__obj); } 330 331 iterator find(const key_type& __key) { return _M_ht.find(__key); } 332 const_iterator find(const key_type& __key) const 333 { return _M_ht.find(__key); } 334 335 size_type count(const key_type& __key) const { return _M_ht.count(__key); } 336 337 pair<iterator, iterator> equal_range(const key_type& __key) 338 { return _M_ht.equal_range(__key); } 339 pair<const_iterator, const_iterator> 340 equal_range(const key_type& __key) const 341 { return _M_ht.equal_range(__key); } 342 343 size_type erase(const key_type& __key) {return _M_ht.erase(__key); } 344 void erase(iterator __it) { _M_ht.erase(__it); } 345 void erase(iterator __f, iterator __l) { _M_ht.erase(__f, __l); } 346 void clear() { _M_ht.clear(); } 347 348public: 349 void resize(size_type __hint) { _M_ht.resize(__hint); } 350 size_type bucket_count() const { return _M_ht.bucket_count(); } 351 size_type max_bucket_count() const { return _M_ht.max_bucket_count(); } 352 size_type elems_in_bucket(size_type __n) const 353 { return _M_ht.elems_in_bucket(__n); } 354}; 355 356template <class _Key, class _Tp, class _HF, class _EqKey, class _Alloc> 357inline bool 358operator==(const hash_multimap<_Key,_Tp,_HF,_EqKey,_Alloc>& __hm1, 359 const hash_multimap<_Key,_Tp,_HF,_EqKey,_Alloc>& __hm2) 360{ 361 return __hm1._M_ht == __hm2._M_ht; 362} 363 364template <class _Key, class _Tp, class _HF, class _EqKey, class _Alloc> 365inline bool 366operator!=(const hash_multimap<_Key,_Tp,_HF,_EqKey,_Alloc>& __hm1, 367 const hash_multimap<_Key,_Tp,_HF,_EqKey,_Alloc>& __hm2) { 368 return !(__hm1 == __hm2); 369} 370 371template <class _Key, class _Tp, class _HashFcn, class _EqlKey, class _Alloc> 372inline void 373swap(hash_multimap<_Key,_Tp,_HashFcn,_EqlKey,_Alloc>& __hm1, 374 hash_multimap<_Key,_Tp,_HashFcn,_EqlKey,_Alloc>& __hm2) 375{ 376 __hm1.swap(__hm2); 377} 378 379} // namespace __gnu_cxx 380 381namespace std 382{ 383// Specialization of insert_iterator so that it will work for hash_map 384// and hash_multimap. 385 386template <class _Key, class _Tp, class _HashFn, class _EqKey, class _Alloc> 387class insert_iterator<__gnu_cxx::hash_map<_Key, _Tp, _HashFn, _EqKey, _Alloc> > { 388protected: 389 typedef __gnu_cxx::hash_map<_Key, _Tp, _HashFn, _EqKey, _Alloc> _Container; 390 _Container* container; 391public: 392 typedef _Container container_type; 393 typedef output_iterator_tag iterator_category; 394 typedef void value_type; 395 typedef void difference_type; 396 typedef void pointer; 397 typedef void reference; 398 399 insert_iterator(_Container& __x) : container(&__x) {} 400 insert_iterator(_Container& __x, typename _Container::iterator) 401 : container(&__x) {} 402 insert_iterator<_Container>& 403 operator=(const typename _Container::value_type& __value) { 404 container->insert(__value); 405 return *this; 406 } 407 insert_iterator<_Container>& operator*() { return *this; } 408 insert_iterator<_Container>& operator++() { return *this; } 409 insert_iterator<_Container>& operator++(int) { return *this; } 410}; 411 412template <class _Key, class _Tp, class _HashFn, class _EqKey, class _Alloc> 413class insert_iterator<__gnu_cxx::hash_multimap<_Key, _Tp, _HashFn, _EqKey, _Alloc> > { 414protected: 415 typedef __gnu_cxx::hash_multimap<_Key, _Tp, _HashFn, _EqKey, _Alloc> _Container; 416 _Container* container; 417 typename _Container::iterator iter; 418public: 419 typedef _Container container_type; 420 typedef output_iterator_tag iterator_category; 421 typedef void value_type; 422 typedef void difference_type; 423 typedef void pointer; 424 typedef void reference; 425 426 insert_iterator(_Container& __x) : container(&__x) {} 427 insert_iterator(_Container& __x, typename _Container::iterator) 428 : container(&__x) {} 429 insert_iterator<_Container>& 430 operator=(const typename _Container::value_type& __value) { 431 container->insert(__value); 432 return *this; 433 } 434 insert_iterator<_Container>& operator*() { return *this; } 435 insert_iterator<_Container>& operator++() { return *this; } 436 insert_iterator<_Container>& operator++(int) { return *this; } 437}; 438 439} // namespace std 440 441#endif /* __SGI_STL_INTERNAL_HASH_MAP_H */ 442 443// Local Variables: 444// mode:C++ 445// End: 446