]> git.buserror.net Git - polintos/scott/priv.git/blob - include/c++/stl/stl/_unordered_map.h
Add STLport 5.1.4
[polintos/scott/priv.git] / include / c++ / stl / stl / _unordered_map.h
1 /*
2  * Copyright (c) 2004
3  * Francois Dumont
4  *
5  * This material is provided "as is", with absolutely no warranty expressed
6  * or implied. Any use is at your own risk.
7  *
8  * Permission to use or copy this software for any purpose is hereby granted
9  * without fee, provided the above notices are retained on all copies.
10  * Permission to modify the code and to distribute modified code is granted,
11  * provided the above notices are retained, and a notice that the code was
12  * modified is included with the above copyright notice.
13  *
14  */
15
16 /* NOTE: This is an internal header file, included by other STL headers.
17  *   You should not attempt to use it directly.
18  */
19
20 #ifndef _STLP_INTERNAL_UNORDERED_MAP_H
21 #define _STLP_INTERNAL_UNORDERED_MAP_H
22
23 #ifndef _STLP_INTERNAL_HASHTABLE_H
24 #  include <stl/_hashtable.h>
25 #endif
26
27 _STLP_BEGIN_NAMESPACE
28
29 //Specific iterator traits creation
30 _STLP_CREATE_HASH_ITERATOR_TRAITS(UnorderedMapTraitsT, traits)
31
32 template <class _Key, class _Tp, _STLP_DFL_TMPL_PARAM(_HashFcn,hash<_Key>),
33           _STLP_DFL_TMPL_PARAM(_EqualKey,equal_to<_Key>),
34           _STLP_DEFAULT_PAIR_ALLOCATOR_SELECT(const _Key, _Tp) >
35 class unordered_map
36 #if defined (_STLP_USE_PARTIAL_SPEC_WORKAROUND)
37                : public __stlport_class<unordered_map<_Key, _Tp, _HashFcn, _EqualKey, _Alloc> >
38 #endif
39 {
40 private:
41   typedef unordered_map<_Key, _Tp, _HashFcn, _EqualKey, _Alloc> _Self;
42 public:
43   typedef _Key key_type;
44   typedef _Tp data_type;
45   typedef _Tp mapped_type;
46 #if !defined (__DMC__)
47   typedef pair<const key_type, data_type> value_type;
48 #else
49   typedef pair<key_type, data_type> value_type;
50 #endif
51 private:
52   //Specific iterator traits creation
53   typedef _STLP_PRIV _UnorderedMapTraitsT<value_type> _UnorderedMapTraits;
54
55 public:
56   typedef hashtable<value_type, key_type, _HashFcn, _UnorderedMapTraits,
57                     _STLP_SELECT1ST(value_type,  _Key), _EqualKey, _Alloc > _Ht;
58
59   typedef typename _Ht::hasher hasher;
60   typedef typename _Ht::key_equal key_equal;
61
62   typedef typename _Ht::size_type size_type;
63   typedef typename _Ht::difference_type difference_type;
64   typedef typename _Ht::pointer pointer;
65   typedef typename _Ht::const_pointer const_pointer;
66   typedef typename _Ht::reference reference;
67   typedef typename _Ht::const_reference const_reference;
68
69   typedef typename _Ht::iterator iterator;
70   typedef typename _Ht::const_iterator const_iterator;
71   typedef typename _Ht::local_iterator local_iterator;
72   typedef typename _Ht::const_local_iterator const_local_iterator;
73
74   typedef typename _Ht::allocator_type allocator_type;
75
76   hasher hash_function() const { return _M_ht.hash_funct(); }
77   key_equal key_eq() const { return _M_ht.key_eq(); }
78   allocator_type get_allocator() const { return _M_ht.get_allocator(); }
79
80 private:
81   _Ht _M_ht;
82   _STLP_KEY_TYPE_FOR_CONT_EXT(key_type)
83
84 public:
85   explicit unordered_map(size_type __n = 100, const hasher& __hf = hasher(),
86                          const key_equal& __eql = key_equal(),
87                          const allocator_type& __a = allocator_type())
88     : _M_ht(__n, __hf, __eql, __a) {}
89
90   unordered_map(__move_source<_Self> src)
91     : _M_ht(__move_source<_Ht>(src.get()._M_ht)) {}
92
93 #if defined (_STLP_MEMBER_TEMPLATES)
94   template <class _InputIterator>
95   unordered_map(_InputIterator __f, _InputIterator __l,
96                 size_type __n = 100, const hasher& __hf = hasher(),
97                 const key_equal& __eql = key_equal(),
98                 const allocator_type& __a = allocator_type())
99     : _M_ht(__n, __hf, __eql, __a)
100   { _M_ht.insert_unique(__f, __l); }
101 #else
102   unordered_map(const value_type* __f, const value_type* __l,
103                 size_type __n = 100, const hasher& __hf = hasher(),
104                 const key_equal& __eql = key_equal(),
105                 const allocator_type& __a = allocator_type())
106     : _M_ht(__n, __hf, __eql, __a)
107   { _M_ht.insert_unique(__f, __l); }
108
109   unordered_map(const_iterator __f, const_iterator __l,
110                 size_type __n = 100, const hasher& __hf = hasher(),
111                 const key_equal& __eql = key_equal(),
112                 const allocator_type& __a = allocator_type())
113     : _M_ht(__n, __hf, __eql, __a)
114   { _M_ht.insert_unique(__f, __l); }
115 #endif /*_STLP_MEMBER_TEMPLATES */
116
117   _Self& operator = (const _Self& __other)
118   { _M_ht = __other._M_ht; return *this; }
119
120   size_type size() const { return _M_ht.size(); }
121   size_type max_size() const { return _M_ht.max_size(); }
122   bool empty() const { return _M_ht.empty(); }
123   void swap(_Self& __hs) { _M_ht.swap(__hs._M_ht); }
124
125   iterator begin() { return _M_ht.begin(); }
126   iterator end() { return _M_ht.end(); }
127   const_iterator begin() const { return _M_ht.begin(); }
128   const_iterator end() const { return _M_ht.end(); }
129
130   pair<iterator,bool> insert(const value_type& __obj)
131   { return _M_ht.insert_unique(__obj); }
132   iterator insert(const_iterator /*__hint*/, const value_type& __obj)
133   { return _M_ht.insert_unique(__obj); }
134 #if defined (_STLP_MEMBER_TEMPLATES)
135   template <class _InputIterator>
136   void insert(_InputIterator __f, _InputIterator __l)
137 #else
138   void insert(const value_type* __f, const value_type* __l)
139   { _M_ht.insert_unique(__f,__l); }
140   void insert(const_iterator __f, const_iterator __l)
141 #endif /*_STLP_MEMBER_TEMPLATES */
142   { _M_ht.insert_unique(__f, __l); }
143
144   _STLP_TEMPLATE_FOR_CONT_EXT
145   iterator find(const _KT& __key) { return _M_ht.find(__key); }
146   _STLP_TEMPLATE_FOR_CONT_EXT
147   const_iterator find(const _KT& __key) const { return _M_ht.find(__key); }
148
149   _STLP_TEMPLATE_FOR_CONT_EXT
150   _Tp& operator[](const _KT& __key) {
151     iterator __it = _M_ht.find(__key);
152     return (__it == _M_ht.end() ?
153       _M_ht._M_insert(value_type(__key, _STLP_DEFAULT_CONSTRUCTED(_Tp))).second :
154       (*__it).second );
155   }
156
157   _STLP_TEMPLATE_FOR_CONT_EXT
158   size_type count(const _KT& __key) const { return _M_ht.count(__key); }
159
160   _STLP_TEMPLATE_FOR_CONT_EXT
161   pair<iterator, iterator> equal_range(const _KT& __key)
162   { return _M_ht.equal_range(__key); }
163   _STLP_TEMPLATE_FOR_CONT_EXT
164   pair<const_iterator, const_iterator> equal_range(const _KT& __key) const
165   { return _M_ht.equal_range(__key); }
166
167   size_type erase(const key_type& __key) {return _M_ht.erase(__key); }
168   void erase(const_iterator __it) { _M_ht.erase(__it); }
169   void erase(const_iterator __f, const_iterator __l) { _M_ht.erase(__f, __l); }
170   void clear() { _M_ht.clear(); }
171
172   size_type bucket_count() const { return _M_ht.bucket_count(); }
173   size_type max_bucket_count() const { return _M_ht.max_bucket_count(); }
174   size_type bucket_size(size_type __n) const { return _M_ht.elems_in_bucket(__n); }
175   _STLP_TEMPLATE_FOR_CONT_EXT
176   size_type bucket(const _KT& __k) const { return _M_ht.bucket(__k); }
177   local_iterator begin(size_type __n) { return _M_ht.begin(__n); }
178   local_iterator end(size_type __n) { return _M_ht.end(__n); }
179   const_local_iterator begin(size_type __n) const { return _M_ht.begin(__n); }
180   const_local_iterator end(size_type __n) const { return _M_ht.end(__n); }
181
182   float load_factor() const { return _M_ht.load_factor(); }
183   float max_load_factor() const { return _M_ht.max_load_factor(); }
184   void max_load_factor(float __val) { _M_ht.max_load_factor(__val); }
185   void rehash(size_type __hint) { _M_ht.rehash(__hint); }
186 };
187
188 //Specific iterator traits creation
189 _STLP_CREATE_HASH_ITERATOR_TRAITS(UnorderedMultimapTraitsT, traits)
190
191 template <class _Key, class _Tp, _STLP_DFL_TMPL_PARAM(_HashFcn,hash<_Key>),
192           _STLP_DFL_TMPL_PARAM(_EqualKey,equal_to<_Key>),
193           _STLP_DEFAULT_PAIR_ALLOCATOR_SELECT(const _Key, _Tp) >
194 class unordered_multimap
195 #if defined (_STLP_USE_PARTIAL_SPEC_WORKAROUND)
196                     : public __stlport_class<unordered_multimap<_Key, _Tp, _HashFcn, _EqualKey, _Alloc> >
197 #endif
198 {
199 private:
200   typedef unordered_multimap<_Key, _Tp, _HashFcn, _EqualKey, _Alloc> _Self;
201 public:
202   typedef _Key key_type;
203   typedef _Tp data_type;
204   typedef _Tp mapped_type;
205 #if !defined (__DMC__)
206   typedef pair<const key_type, data_type> value_type;
207 #else
208   typedef pair<key_type, data_type> value_type;
209 #endif
210 private:
211   //Specific iterator traits creation
212   typedef _STLP_PRIV _UnorderedMultimapTraitsT<value_type> _UnorderedMultimapTraits;
213
214 public:
215   typedef hashtable<value_type, key_type, _HashFcn, _UnorderedMultimapTraits,
216                     _STLP_SELECT1ST(value_type,  _Key), _EqualKey, _Alloc > _Ht;
217
218   typedef typename _Ht::hasher hasher;
219   typedef typename _Ht::key_equal key_equal;
220
221   typedef typename _Ht::size_type size_type;
222   typedef typename _Ht::difference_type difference_type;
223   typedef typename _Ht::pointer pointer;
224   typedef typename _Ht::const_pointer const_pointer;
225   typedef typename _Ht::reference reference;
226   typedef typename _Ht::const_reference const_reference;
227
228   typedef typename _Ht::iterator iterator;
229   typedef typename _Ht::const_iterator const_iterator;
230   typedef typename _Ht::local_iterator local_iterator;
231   typedef typename _Ht::const_local_iterator const_local_iterator;
232
233   typedef typename _Ht::allocator_type allocator_type;
234
235   hasher hash_function() const { return _M_ht.hash_funct(); }
236   key_equal key_eq() const { return _M_ht.key_eq(); }
237   allocator_type get_allocator() const { return _M_ht.get_allocator(); }
238
239 private:
240   _Ht _M_ht;
241   _STLP_KEY_TYPE_FOR_CONT_EXT(key_type)
242
243 public:
244   explicit unordered_multimap(size_type __n = 100, const hasher& __hf = hasher(),
245                               const key_equal& __eql = key_equal(),
246                               const allocator_type& __a = allocator_type())
247     : _M_ht(__n, __hf, __eql, __a) {}
248
249   unordered_multimap(__move_source<_Self> src)
250     : _M_ht(__move_source<_Ht>(src.get()._M_ht)) {}
251
252 #if defined (_STLP_MEMBER_TEMPLATES)
253   template <class _InputIterator>
254   unordered_multimap(_InputIterator __f, _InputIterator __l,
255                      size_type __n = 100, const hasher& __hf = hasher(),
256                      const key_equal& __eql = key_equal(),
257                      const allocator_type& __a = allocator_type())
258     : _M_ht(__n, __hf, __eql, __a)
259   { _M_ht.insert_equal(__f, __l); }
260 #else
261   unordered_multimap(const value_type* __f, const value_type* __l,
262                      size_type __n = 100, const hasher& __hf = hasher(),
263                      const key_equal& __eql = key_equal(),
264                      const allocator_type& __a = allocator_type())
265     : _M_ht(__n, __hf, __eql, __a)
266   { _M_ht.insert_equal(__f, __l); }
267
268   unordered_multimap(const_iterator __f, const_iterator __l,
269                      size_type __n = 100, const hasher& __hf = hasher(),
270                      const key_equal& __eql = key_equal(),
271                      const allocator_type& __a = allocator_type())
272     : _M_ht(__n, __hf, __eql, __a)
273   { _M_ht.insert_equal(__f, __l); }
274 #endif /*_STLP_MEMBER_TEMPLATES */
275
276   _Self& operator = (const _Self& __other)
277   { _M_ht = __other._M_ht; return *this; }
278
279   size_type size() const { return _M_ht.size(); }
280   size_type max_size() const { return _M_ht.max_size(); }
281   bool empty() const { return _M_ht.empty(); }
282   void swap(_Self& __hs) { _M_ht.swap(__hs._M_ht); }
283
284   iterator begin() { return _M_ht.begin(); }
285   iterator end() { return _M_ht.end(); }
286   const_iterator begin() const { return _M_ht.begin(); }
287   const_iterator end() const { return _M_ht.end(); }
288
289   iterator insert(const value_type& __obj)
290   { return _M_ht.insert_equal(__obj); }
291   iterator insert(const_iterator /*__hint*/, const value_type& __obj)
292   { return _M_ht.insert_equal(__obj); }
293 #if defined (_STLP_MEMBER_TEMPLATES)
294   template <class _InputIterator>
295   void insert(_InputIterator __f, _InputIterator __l)
296 #else
297   void insert(const value_type* __f, const value_type* __l)
298   { _M_ht.insert_equal(__f,__l); }
299   void insert(const_iterator __f, const_iterator __l)
300 #endif /*_STLP_MEMBER_TEMPLATES */
301   { _M_ht.insert_equal(__f, __l); }
302
303   _STLP_TEMPLATE_FOR_CONT_EXT
304   iterator find(const _KT& __key) { return _M_ht.find(__key); }
305   _STLP_TEMPLATE_FOR_CONT_EXT
306   const_iterator find(const _KT& __key) const { return _M_ht.find(__key); }
307
308   _STLP_TEMPLATE_FOR_CONT_EXT
309   size_type count(const _KT& __key) const { return _M_ht.count(__key); }
310
311   _STLP_TEMPLATE_FOR_CONT_EXT
312   pair<iterator, iterator> equal_range(const _KT& __key)
313   { return _M_ht.equal_range(__key); }
314   _STLP_TEMPLATE_FOR_CONT_EXT
315   pair<const_iterator, const_iterator> equal_range(const _KT& __key) const
316   { return _M_ht.equal_range(__key); }
317
318   size_type erase(const key_type& __key) {return _M_ht.erase(__key); }
319   void erase(const_iterator __it) { _M_ht.erase(__it); }
320   void erase(const_iterator __f, const_iterator __l) { _M_ht.erase(__f, __l); }
321   void clear() { _M_ht.clear(); }
322
323   size_type bucket_count() const { return _M_ht.bucket_count(); }
324   size_type max_bucket_count() const { return _M_ht.max_bucket_count(); }
325   size_type bucket_size(size_type __n) const { return _M_ht.elems_in_bucket(__n); }
326   _STLP_TEMPLATE_FOR_CONT_EXT
327   size_type bucket(const _KT& __k) const { return _M_ht.bucket(__k); }
328   local_iterator begin(size_type __n) { return _M_ht.begin(__n); }
329   local_iterator end(size_type __n) { return _M_ht.end(__n); }
330   const_local_iterator begin(size_type __n) const { return _M_ht.begin(__n); }
331   const_local_iterator end(size_type __n) const { return _M_ht.end(__n); }
332
333   float load_factor() const { return _M_ht.load_factor(); }
334   float max_load_factor() const { return _M_ht.max_load_factor(); }
335   void max_load_factor(float __val) { _M_ht.max_load_factor(__val); }
336   void rehash(size_type __hint) { _M_ht.rehash(__hint); }
337 };
338
339 #define _STLP_TEMPLATE_HEADER template <class _Key, class _Tp, class _HashFcn, class _EqlKey, class _Alloc>
340 #define _STLP_TEMPLATE_CONTAINER unordered_map<_Key,_Tp,_HashFcn,_EqlKey,_Alloc>
341
342 #include <stl/_relops_hash_cont.h>
343
344 #undef _STLP_TEMPLATE_CONTAINER
345 #define _STLP_TEMPLATE_CONTAINER unordered_multimap<_Key,_Tp,_HashFcn,_EqlKey,_Alloc>
346
347 #include <stl/_relops_hash_cont.h>
348
349 #undef _STLP_TEMPLATE_CONTAINER
350 #undef _STLP_TEMPLATE_HEADER
351
352 // Specialization of insert_iterator so that it will work for unordered_map
353 // and unordered_multimap.
354
355 #if defined (_STLP_CLASS_PARTIAL_SPECIALIZATION)
356 template <class _Key, class _Tp, class _HashFn,  class _EqKey, class _Alloc>
357 struct __move_traits<unordered_map<_Key, _Tp, _HashFn, _EqKey, _Alloc> > :
358   _STLP_PRIV __move_traits_help<typename unordered_map<_Key, _Tp, _HashFn, _EqKey, _Alloc>::_Ht>
359 {};
360
361 template <class _Key, class _Tp, class _HashFn,  class _EqKey, class _Alloc>
362 struct __move_traits<unordered_multimap<_Key, _Tp, _HashFn, _EqKey, _Alloc> > :
363   _STLP_PRIV __move_traits_help<typename unordered_map<_Key, _Tp, _HashFn, _EqKey, _Alloc>::_Ht>
364 {};
365
366 template <class _Key, class _Tp, class _HashFn,  class _EqKey, class _Alloc>
367 class insert_iterator<unordered_map<_Key, _Tp, _HashFn, _EqKey, _Alloc> > {
368 protected:
369   typedef unordered_map<_Key, _Tp, _HashFn, _EqKey, _Alloc> _Container;
370   _Container* container;
371 public:
372   typedef _Container          container_type;
373   typedef output_iterator_tag iterator_category;
374   typedef void                value_type;
375   typedef void                difference_type;
376   typedef void                pointer;
377   typedef void                reference;
378
379   insert_iterator(_Container& __x) : container(&__x) {}
380   insert_iterator(_Container& __x, typename _Container::iterator)
381     : container(&__x) {}
382   insert_iterator<_Container>&
383   operator=(const typename _Container::value_type& __val) {
384     container->insert(__val);
385     return *this;
386   }
387   insert_iterator<_Container>& operator*() { return *this; }
388   insert_iterator<_Container>& operator++() { return *this; }
389   insert_iterator<_Container>& operator++(int) { return *this; }
390 };
391
392 template <class _Key, class _Tp, class _HashFn,  class _EqKey, class _Alloc>
393 class insert_iterator<unordered_multimap<_Key, _Tp, _HashFn, _EqKey, _Alloc> > {
394 protected:
395   typedef unordered_multimap<_Key, _Tp, _HashFn, _EqKey, _Alloc> _Container;
396   _Container* container;
397   typename _Container::iterator iter;
398 public:
399   typedef _Container          container_type;
400   typedef output_iterator_tag iterator_category;
401   typedef void                value_type;
402   typedef void                difference_type;
403   typedef void                pointer;
404   typedef void                reference;
405
406   insert_iterator(_Container& __x) : container(&__x) {}
407   insert_iterator(_Container& __x, typename _Container::iterator)
408     : container(&__x) {}
409   insert_iterator<_Container>&
410   operator=(const typename _Container::value_type& __val) {
411     container->insert(__val);
412     return *this;
413   }
414   insert_iterator<_Container>& operator*() { return *this; }
415   insert_iterator<_Container>& operator++() { return *this; }
416   insert_iterator<_Container>& operator++(int) { return *this; }
417 };
418
419 #endif /* _STLP_CLASS_PARTIAL_SPECIALIZATION */
420
421 _STLP_END_NAMESPACE
422
423 #endif /* _STLP_INTERNAL_UNORDERED_MAP_H */
424
425 // Local Variables:
426 // mode:C++
427 // End: