OSDN Git Service

2000-04-21 Benjamin Kosnik <bkoz@redhat.com>
[pf3gnuchains/gcc-fork.git] / libstdc++-v3 / ext / hash_map
1 /*
2  * Copyright (c) 1996
3  * Silicon Graphics Computer Systems, Inc.
4  *
5  * Permission to use, copy, modify, distribute and sell this software
6  * and its documentation for any purpose is hereby granted without fee,
7  * provided that the above copyright notice appear in all copies and
8  * that both that copyright notice and this permission notice appear
9  * in supporting documentation.  Silicon Graphics makes no
10  * representations about the suitability of this software for any
11  * purpose.  It is provided "as is" without express or implied warranty.
12  *
13  *
14  * Copyright (c) 1994
15  * Hewlett-Packard Company
16  *
17  * Permission to use, copy, modify, distribute and sell this software
18  * and its documentation for any purpose is hereby granted without fee,
19  * provided that the above copyright notice appear in all copies and
20  * that both that copyright notice and this permission notice appear
21  * in supporting documentation.  Hewlett-Packard Company makes no
22  * representations about the suitability of this software for any
23  * purpose.  It is provided "as is" without express or implied warranty.
24  *
25  */
26
27 /* NOTE: This is an internal header file, included by other STL headers.
28  *   You should not attempt to use it directly.
29  */
30
31 #ifndef __SGI_STL_INTERNAL_HASH_MAP_H
32 #define __SGI_STL_INTERNAL_HASH_MAP_H
33
34 #include <ext/stl_hashtable.h>
35
36 __STL_BEGIN_NAMESPACE
37
38 #if defined(__sgi) && !defined(__GNUC__) && (_MIPS_SIM != _MIPS_SIM_ABI32)
39 #pragma set woff 1174
40 #pragma set woff 1375
41 #endif
42
43 // Forward declaration of equality operator; needed for friend declaration.
44
45 template <class _Key, class _Tp,
46           class _HashFcn  = hash<_Key>,
47           class _EqualKey = equal_to<_Key>,
48           class _Alloc =  allocator<_Tp> >
49 class hash_map;
50
51 template <class _Key, class _Tp, class _HashFn, class _EqKey, class _Alloc>
52 inline bool operator==(const hash_map<_Key, _Tp, _HashFn, _EqKey, _Alloc>&,
53                        const hash_map<_Key, _Tp, _HashFn, _EqKey, _Alloc>&);
54
55 template <class _Key, class _Tp, class _HashFcn, class _EqualKey,
56           class _Alloc>
57 class hash_map
58 {
59 private:
60   typedef hashtable<pair<const _Key,_Tp>,_Key,_HashFcn,
61                     _Select1st<pair<const _Key,_Tp> >,_EqualKey,_Alloc> _Ht;
62   _Ht _M_ht;
63
64 public:
65   typedef typename _Ht::key_type key_type;
66   typedef _Tp data_type;
67   typedef _Tp mapped_type;
68   typedef typename _Ht::value_type value_type;
69   typedef typename _Ht::hasher hasher;
70   typedef typename _Ht::key_equal key_equal;
71   
72   typedef typename _Ht::size_type size_type;
73   typedef typename _Ht::difference_type difference_type;
74   typedef typename _Ht::pointer pointer;
75   typedef typename _Ht::const_pointer const_pointer;
76   typedef typename _Ht::reference reference;
77   typedef typename _Ht::const_reference const_reference;
78
79   typedef typename _Ht::iterator iterator;
80   typedef typename _Ht::const_iterator const_iterator;
81
82   typedef typename _Ht::allocator_type allocator_type;
83
84   hasher hash_funct() const { return _M_ht.hash_funct(); }
85   key_equal key_eq() const { return _M_ht.key_eq(); }
86   allocator_type get_allocator() const { return _M_ht.get_allocator(); }
87
88 public:
89   hash_map() : _M_ht(100, hasher(), key_equal(), allocator_type()) {}
90   explicit hash_map(size_type __n)
91     : _M_ht(__n, hasher(), key_equal(), allocator_type()) {}
92   hash_map(size_type __n, const hasher& __hf)
93     : _M_ht(__n, __hf, key_equal(), allocator_type()) {}
94   hash_map(size_type __n, const hasher& __hf, const key_equal& __eql,
95            const allocator_type& __a = allocator_type())
96     : _M_ht(__n, __hf, __eql, __a) {}
97
98 #ifdef __STL_MEMBER_TEMPLATES
99   template <class _InputIterator>
100   hash_map(_InputIterator __f, _InputIterator __l)
101     : _M_ht(100, hasher(), key_equal(), allocator_type())
102     { _M_ht.insert_unique(__f, __l); }
103   template <class _InputIterator>
104   hash_map(_InputIterator __f, _InputIterator __l, size_type __n)
105     : _M_ht(__n, hasher(), key_equal(), allocator_type())
106     { _M_ht.insert_unique(__f, __l); }
107   template <class _InputIterator>
108   hash_map(_InputIterator __f, _InputIterator __l, size_type __n,
109            const hasher& __hf)
110     : _M_ht(__n, __hf, key_equal(), allocator_type())
111     { _M_ht.insert_unique(__f, __l); }
112   template <class _InputIterator>
113   hash_map(_InputIterator __f, _InputIterator __l, size_type __n,
114            const hasher& __hf, const key_equal& __eql,
115            const allocator_type& __a = allocator_type())
116     : _M_ht(__n, __hf, __eql, __a)
117     { _M_ht.insert_unique(__f, __l); }
118
119 #else
120   hash_map(const value_type* __f, const value_type* __l)
121     : _M_ht(100, hasher(), key_equal(), allocator_type())
122     { _M_ht.insert_unique(__f, __l); }
123   hash_map(const value_type* __f, const value_type* __l, size_type __n)
124     : _M_ht(__n, hasher(), key_equal(), allocator_type())
125     { _M_ht.insert_unique(__f, __l); }
126   hash_map(const value_type* __f, const value_type* __l, size_type __n,
127            const hasher& __hf)
128     : _M_ht(__n, __hf, key_equal(), allocator_type())
129     { _M_ht.insert_unique(__f, __l); }
130   hash_map(const value_type* __f, const value_type* __l, size_type __n,
131            const hasher& __hf, const key_equal& __eql,
132            const allocator_type& __a = allocator_type())
133     : _M_ht(__n, __hf, __eql, __a)
134     { _M_ht.insert_unique(__f, __l); }
135
136   hash_map(const_iterator __f, const_iterator __l)
137     : _M_ht(100, hasher(), key_equal(), allocator_type())
138     { _M_ht.insert_unique(__f, __l); }
139   hash_map(const_iterator __f, const_iterator __l, size_type __n)
140     : _M_ht(__n, hasher(), key_equal(), allocator_type())
141     { _M_ht.insert_unique(__f, __l); }
142   hash_map(const_iterator __f, const_iterator __l, size_type __n,
143            const hasher& __hf)
144     : _M_ht(__n, __hf, key_equal(), allocator_type())
145     { _M_ht.insert_unique(__f, __l); }
146   hash_map(const_iterator __f, const_iterator __l, size_type __n,
147            const hasher& __hf, const key_equal& __eql,
148            const allocator_type& __a = allocator_type())
149     : _M_ht(__n, __hf, __eql, __a)
150     { _M_ht.insert_unique(__f, __l); }
151 #endif /*__STL_MEMBER_TEMPLATES */
152
153 public:
154   size_type size() const { return _M_ht.size(); }
155   size_type max_size() const { return _M_ht.max_size(); }
156   bool empty() const { return _M_ht.empty(); }
157   void swap(hash_map& __hs) { _M_ht.swap(__hs._M_ht); }
158
159 #ifdef __STL_MEMBER_TEMPLATES
160   template <class _K1, class _T1, class _HF, class _EqK, class _Al>
161   friend bool operator== (const hash_map<_K1, _T1, _HF, _EqK, _Al>&,
162                           const hash_map<_K1, _T1, _HF, _EqK, _Al>&);
163 #else /* __STL_MEMBER_TEMPLATES */
164   friend bool __STD_QUALIFIER
165   operator== __STL_NULL_TMPL_ARGS (const hash_map&, const hash_map&);
166 #endif /* __STL_MEMBER_TEMPLATES */
167
168
169   iterator begin() { return _M_ht.begin(); }
170   iterator end() { return _M_ht.end(); }
171   const_iterator begin() const { return _M_ht.begin(); }
172   const_iterator end() const { return _M_ht.end(); }
173
174 public:
175   pair<iterator,bool> insert(const value_type& __obj)
176     { return _M_ht.insert_unique(__obj); }
177 #ifdef __STL_MEMBER_TEMPLATES
178   template <class _InputIterator>
179   void insert(_InputIterator __f, _InputIterator __l)
180     { _M_ht.insert_unique(__f,__l); }
181 #else
182   void insert(const value_type* __f, const value_type* __l) {
183     _M_ht.insert_unique(__f,__l);
184   }
185   void insert(const_iterator __f, const_iterator __l)
186     { _M_ht.insert_unique(__f, __l); }
187 #endif /*__STL_MEMBER_TEMPLATES */
188   pair<iterator,bool> insert_noresize(const value_type& __obj)
189     { return _M_ht.insert_unique_noresize(__obj); }    
190
191   iterator find(const key_type& __key) { return _M_ht.find(__key); }
192   const_iterator find(const key_type& __key) const 
193     { return _M_ht.find(__key); }
194
195   _Tp& operator[](const key_type& __key) {
196     return _M_ht.find_or_insert(value_type(__key, _Tp())).second;
197   }
198
199   size_type count(const key_type& __key) const { return _M_ht.count(__key); }
200   
201   pair<iterator, iterator> equal_range(const key_type& __key)
202     { return _M_ht.equal_range(__key); }
203   pair<const_iterator, const_iterator>
204   equal_range(const key_type& __key) const
205     { return _M_ht.equal_range(__key); }
206
207   size_type erase(const key_type& __key) {return _M_ht.erase(__key); }
208   void erase(iterator __it) { _M_ht.erase(__it); }
209   void erase(iterator __f, iterator __l) { _M_ht.erase(__f, __l); }
210   void clear() { _M_ht.clear(); }
211
212   void resize(size_type __hint) { _M_ht.resize(__hint); }
213   size_type bucket_count() const { return _M_ht.bucket_count(); }
214   size_type max_bucket_count() const { return _M_ht.max_bucket_count(); }
215   size_type elems_in_bucket(size_type __n) const
216     { return _M_ht.elems_in_bucket(__n); }
217 };
218
219 template <class _Key, class _Tp, class _HashFcn, class _EqlKey, class _Alloc>
220 inline bool 
221 operator==(const hash_map<_Key,_Tp,_HashFcn,_EqlKey,_Alloc>& __hm1,
222            const hash_map<_Key,_Tp,_HashFcn,_EqlKey,_Alloc>& __hm2)
223 {
224   return __hm1._M_ht == __hm2._M_ht;
225 }
226
227 #ifdef __STL_FUNCTION_TMPL_PARTIAL_ORDER
228
229 template <class _Key, class _Tp, class _HashFcn, class _EqlKey, class _Alloc>
230 inline bool 
231 operator!=(const hash_map<_Key,_Tp,_HashFcn,_EqlKey,_Alloc>& __hm1,
232            const hash_map<_Key,_Tp,_HashFcn,_EqlKey,_Alloc>& __hm2) {
233   return !(__hm1 == __hm2);
234 }
235
236 template <class _Key, class _Tp, class _HashFcn, class _EqlKey, class _Alloc>
237 inline void 
238 swap(hash_map<_Key,_Tp,_HashFcn,_EqlKey,_Alloc>& __hm1,
239      hash_map<_Key,_Tp,_HashFcn,_EqlKey,_Alloc>& __hm2)
240 {
241   __hm1.swap(__hm2);
242 }
243
244 #endif /* __STL_FUNCTION_TMPL_PARTIAL_ORDER */
245
246 // Forward declaration of equality operator; needed for friend declaration.
247
248 template <class _Key, class _Tp,
249           class _HashFcn  = hash<_Key>,
250           class _EqualKey = equal_to<_Key>,
251           class _Alloc =  allocator<_Tp> >
252 class hash_multimap;
253
254 template <class _Key, class _Tp, class _HF, class _EqKey, class _Alloc>
255 inline bool 
256 operator==(const hash_multimap<_Key,_Tp,_HF,_EqKey,_Alloc>& __hm1,
257            const hash_multimap<_Key,_Tp,_HF,_EqKey,_Alloc>& __hm2);
258
259 template <class _Key, class _Tp, class _HashFcn, class _EqualKey, 
260           class _Alloc>
261 class hash_multimap
262 {
263 private:
264   typedef hashtable<pair<const _Key, _Tp>, _Key, _HashFcn,
265                     _Select1st<pair<const _Key, _Tp> >, _EqualKey, _Alloc> 
266           _Ht;
267   _Ht _M_ht;
268
269 public:
270   typedef typename _Ht::key_type key_type;
271   typedef _Tp data_type;
272   typedef _Tp mapped_type;
273   typedef typename _Ht::value_type value_type;
274   typedef typename _Ht::hasher hasher;
275   typedef typename _Ht::key_equal key_equal;
276
277   typedef typename _Ht::size_type size_type;
278   typedef typename _Ht::difference_type difference_type;
279   typedef typename _Ht::pointer pointer;
280   typedef typename _Ht::const_pointer const_pointer;
281   typedef typename _Ht::reference reference;
282   typedef typename _Ht::const_reference const_reference;
283
284   typedef typename _Ht::iterator iterator;
285   typedef typename _Ht::const_iterator const_iterator;
286
287   typedef typename _Ht::allocator_type allocator_type;
288
289   hasher hash_funct() const { return _M_ht.hash_funct(); }
290   key_equal key_eq() const { return _M_ht.key_eq(); }
291   allocator_type get_allocator() const { return _M_ht.get_allocator(); }
292
293 public:
294   hash_multimap() : _M_ht(100, hasher(), key_equal(), allocator_type()) {}
295   explicit hash_multimap(size_type __n)
296     : _M_ht(__n, hasher(), key_equal(), allocator_type()) {}
297   hash_multimap(size_type __n, const hasher& __hf)
298     : _M_ht(__n, __hf, key_equal(), allocator_type()) {}
299   hash_multimap(size_type __n, const hasher& __hf, const key_equal& __eql,
300                 const allocator_type& __a = allocator_type())
301     : _M_ht(__n, __hf, __eql, __a) {}
302
303 #ifdef __STL_MEMBER_TEMPLATES
304   template <class _InputIterator>
305   hash_multimap(_InputIterator __f, _InputIterator __l)
306     : _M_ht(100, hasher(), key_equal(), allocator_type())
307     { _M_ht.insert_equal(__f, __l); }
308   template <class _InputIterator>
309   hash_multimap(_InputIterator __f, _InputIterator __l, size_type __n)
310     : _M_ht(__n, hasher(), key_equal(), allocator_type())
311     { _M_ht.insert_equal(__f, __l); }
312   template <class _InputIterator>
313   hash_multimap(_InputIterator __f, _InputIterator __l, size_type __n,
314                 const hasher& __hf)
315     : _M_ht(__n, __hf, key_equal(), allocator_type())
316     { _M_ht.insert_equal(__f, __l); }
317   template <class _InputIterator>
318   hash_multimap(_InputIterator __f, _InputIterator __l, size_type __n,
319                 const hasher& __hf, const key_equal& __eql,
320                 const allocator_type& __a = allocator_type())
321     : _M_ht(__n, __hf, __eql, __a)
322     { _M_ht.insert_equal(__f, __l); }
323
324 #else
325   hash_multimap(const value_type* __f, const value_type* __l)
326     : _M_ht(100, hasher(), key_equal(), allocator_type())
327     { _M_ht.insert_equal(__f, __l); }
328   hash_multimap(const value_type* __f, const value_type* __l, size_type __n)
329     : _M_ht(__n, hasher(), key_equal(), allocator_type())
330     { _M_ht.insert_equal(__f, __l); }
331   hash_multimap(const value_type* __f, const value_type* __l, size_type __n,
332                 const hasher& __hf)
333     : _M_ht(__n, __hf, key_equal(), allocator_type())
334     { _M_ht.insert_equal(__f, __l); }
335   hash_multimap(const value_type* __f, const value_type* __l, size_type __n,
336                 const hasher& __hf, const key_equal& __eql,
337                 const allocator_type& __a = allocator_type())
338     : _M_ht(__n, __hf, __eql, __a)
339     { _M_ht.insert_equal(__f, __l); }
340
341   hash_multimap(const_iterator __f, const_iterator __l)
342     : _M_ht(100, hasher(), key_equal(), allocator_type())
343     { _M_ht.insert_equal(__f, __l); }
344   hash_multimap(const_iterator __f, const_iterator __l, size_type __n)
345     : _M_ht(__n, hasher(), key_equal(), allocator_type())
346     { _M_ht.insert_equal(__f, __l); }
347   hash_multimap(const_iterator __f, const_iterator __l, size_type __n,
348                 const hasher& __hf)
349     : _M_ht(__n, __hf, key_equal(), allocator_type())
350     { _M_ht.insert_equal(__f, __l); }
351   hash_multimap(const_iterator __f, const_iterator __l, size_type __n,
352                 const hasher& __hf, const key_equal& __eql,
353                 const allocator_type& __a = allocator_type())
354     : _M_ht(__n, __hf, __eql, __a)
355     { _M_ht.insert_equal(__f, __l); }
356 #endif /*__STL_MEMBER_TEMPLATES */
357
358 public:
359   size_type size() const { return _M_ht.size(); }
360   size_type max_size() const { return _M_ht.max_size(); }
361   bool empty() const { return _M_ht.empty(); }
362   void swap(hash_multimap& __hs) { _M_ht.swap(__hs._M_ht); }
363
364 #ifdef __STL_MEMBER_TEMPLATES
365   template <class _K1, class _T1, class _HF, class _EqK, class _Al>
366   friend bool operator== (const hash_multimap<_K1, _T1, _HF, _EqK, _Al>&,
367                           const hash_multimap<_K1, _T1, _HF, _EqK, _Al>&);
368 #else /* __STL_MEMBER_TEMPLATES */
369   friend bool __STD_QUALIFIER
370   operator== __STL_NULL_TMPL_ARGS (const hash_multimap&,const hash_multimap&);
371 #endif /* __STL_MEMBER_TEMPLATES */
372
373   iterator begin() { return _M_ht.begin(); }
374   iterator end() { return _M_ht.end(); }
375   const_iterator begin() const { return _M_ht.begin(); }
376   const_iterator end() const { return _M_ht.end(); }
377
378 public:
379   iterator insert(const value_type& __obj) 
380     { return _M_ht.insert_equal(__obj); }
381 #ifdef __STL_MEMBER_TEMPLATES
382   template <class _InputIterator>
383   void insert(_InputIterator __f, _InputIterator __l) 
384     { _M_ht.insert_equal(__f,__l); }
385 #else
386   void insert(const value_type* __f, const value_type* __l) {
387     _M_ht.insert_equal(__f,__l);
388   }
389   void insert(const_iterator __f, const_iterator __l) 
390     { _M_ht.insert_equal(__f, __l); }
391 #endif /*__STL_MEMBER_TEMPLATES */
392   iterator insert_noresize(const value_type& __obj)
393     { return _M_ht.insert_equal_noresize(__obj); }    
394
395   iterator find(const key_type& __key) { return _M_ht.find(__key); }
396   const_iterator find(const key_type& __key) const 
397     { return _M_ht.find(__key); }
398
399   size_type count(const key_type& __key) const { return _M_ht.count(__key); }
400   
401   pair<iterator, iterator> equal_range(const key_type& __key)
402     { return _M_ht.equal_range(__key); }
403   pair<const_iterator, const_iterator>
404   equal_range(const key_type& __key) const
405     { return _M_ht.equal_range(__key); }
406
407   size_type erase(const key_type& __key) {return _M_ht.erase(__key); }
408   void erase(iterator __it) { _M_ht.erase(__it); }
409   void erase(iterator __f, iterator __l) { _M_ht.erase(__f, __l); }
410   void clear() { _M_ht.clear(); }
411
412 public:
413   void resize(size_type __hint) { _M_ht.resize(__hint); }
414   size_type bucket_count() const { return _M_ht.bucket_count(); }
415   size_type max_bucket_count() const { return _M_ht.max_bucket_count(); }
416   size_type elems_in_bucket(size_type __n) const
417     { return _M_ht.elems_in_bucket(__n); }
418 };
419
420 template <class _Key, class _Tp, class _HF, class _EqKey, class _Alloc>
421 inline bool 
422 operator==(const hash_multimap<_Key,_Tp,_HF,_EqKey,_Alloc>& __hm1,
423            const hash_multimap<_Key,_Tp,_HF,_EqKey,_Alloc>& __hm2)
424 {
425   return __hm1._M_ht == __hm2._M_ht;
426 }
427
428 #ifdef __STL_FUNCTION_TMPL_PARTIAL_ORDER
429
430 template <class _Key, class _Tp, class _HF, class _EqKey, class _Alloc>
431 inline bool 
432 operator!=(const hash_multimap<_Key,_Tp,_HF,_EqKey,_Alloc>& __hm1,
433            const hash_multimap<_Key,_Tp,_HF,_EqKey,_Alloc>& __hm2) {
434   return !(__hm1 == __hm2);
435 }
436
437 template <class _Key, class _Tp, class _HashFcn, class _EqlKey, class _Alloc>
438 inline void 
439 swap(hash_multimap<_Key,_Tp,_HashFcn,_EqlKey,_Alloc>& __hm1,
440      hash_multimap<_Key,_Tp,_HashFcn,_EqlKey,_Alloc>& __hm2)
441 {
442   __hm1.swap(__hm2);
443 }
444
445 #endif /* __STL_FUNCTION_TMPL_PARTIAL_ORDER */
446
447 // Specialization of insert_iterator so that it will work for hash_map
448 // and hash_multimap.
449
450 #ifdef __STL_CLASS_PARTIAL_SPECIALIZATION
451
452 template <class _Key, class _Tp, class _HashFn,  class _EqKey, class _Alloc>
453 class insert_iterator<hash_map<_Key, _Tp, _HashFn, _EqKey, _Alloc> > {
454 protected:
455   typedef hash_map<_Key, _Tp, _HashFn, _EqKey, _Alloc> _Container;
456   _Container* container;
457 public:
458   typedef _Container          container_type;
459   typedef output_iterator_tag iterator_category;
460   typedef void                value_type;
461   typedef void                difference_type;
462   typedef void                pointer;
463   typedef void                reference;
464
465   insert_iterator(_Container& __x) : container(&__x) {}
466   insert_iterator(_Container& __x, typename _Container::iterator)
467     : container(&__x) {}
468   insert_iterator<_Container>&
469   operator=(const typename _Container::value_type& __value) { 
470     container->insert(__value);
471     return *this;
472   }
473   insert_iterator<_Container>& operator*() { return *this; }
474   insert_iterator<_Container>& operator++() { return *this; }
475   insert_iterator<_Container>& operator++(int) { return *this; }
476 };
477
478 template <class _Key, class _Tp, class _HashFn,  class _EqKey, class _Alloc>
479 class insert_iterator<hash_multimap<_Key, _Tp, _HashFn, _EqKey, _Alloc> > {
480 protected:
481   typedef hash_multimap<_Key, _Tp, _HashFn, _EqKey, _Alloc> _Container;
482   _Container* container;
483   typename _Container::iterator iter;
484 public:
485   typedef _Container          container_type;
486   typedef output_iterator_tag iterator_category;
487   typedef void                value_type;
488   typedef void                difference_type;
489   typedef void                pointer;
490   typedef void                reference;
491
492   insert_iterator(_Container& __x) : container(&__x) {}
493   insert_iterator(_Container& __x, typename _Container::iterator)
494     : container(&__x) {}
495   insert_iterator<_Container>&
496   operator=(const typename _Container::value_type& __value) { 
497     container->insert(__value);
498     return *this;
499   }
500   insert_iterator<_Container>& operator*() { return *this; }
501   insert_iterator<_Container>& operator++() { return *this; }
502   insert_iterator<_Container>& operator++(int) { return *this; }
503 };
504
505 #endif /* __STL_CLASS_PARTIAL_SPECIALIZATION */
506
507 #if defined(__sgi) && !defined(__GNUC__) && (_MIPS_SIM != _MIPS_SIM_ABI32)
508 #pragma reset woff 1174
509 #pragma reset woff 1375
510 #endif
511
512 __STL_END_NAMESPACE
513
514 #endif /* __SGI_STL_INTERNAL_HASH_MAP_H */
515
516 // Local Variables:
517 // mode:C++
518 // End: