blob: b74540be7f477a4fbae2bb3d7e6c8d3db4f75a92 [file] [log] [blame]
Howard Hinnant3e519522010-05-11 19:42:16 +00001// -*- C++ -*-
2//===-------------------------- hash_map ----------------------------------===//
3//
Howard Hinnant5b08a8a2010-05-11 21:36:01 +00004// The LLVM Compiler Infrastructure
Howard Hinnant3e519522010-05-11 19:42:16 +00005//
Howard Hinnant412dbeb2010-11-16 22:09:02 +00006// This file is dual licensed under the MIT and the University of Illinois Open
7// Source Licenses. See LICENSE.TXT for details.
Howard Hinnant3e519522010-05-11 19:42:16 +00008//
9//===----------------------------------------------------------------------===//
10
11#ifndef _LIBCPP_HASH_MAP
12#define _LIBCPP_HASH_MAP
13
14/*
15
16 hash_map synopsis
17
18namespace __gnu_cxx
19{
20
21template <class Key, class T, class Hash = hash<Key>, class Pred = equal_to<Key>,
22 class Alloc = allocator<pair<const Key, T>>>
23class hash_map
24{
25public:
26 // types
27 typedef Key key_type;
28 typedef T mapped_type;
29 typedef Hash hasher;
30 typedef Pred key_equal;
31 typedef Alloc allocator_type;
32 typedef pair<const key_type, mapped_type> value_type;
33 typedef value_type& reference;
34 typedef const value_type& const_reference;
35 typedef typename allocator_traits<allocator_type>::pointer pointer;
36 typedef typename allocator_traits<allocator_type>::const_pointer const_pointer;
37 typedef typename allocator_traits<allocator_type>::size_type size_type;
38 typedef typename allocator_traits<allocator_type>::difference_type difference_type;
39
40 typedef /unspecified/ iterator;
41 typedef /unspecified/ const_iterator;
42
43 explicit hash_map(size_type n = 193, const hasher& hf = hasher(),
44 const key_equal& eql = key_equal(),
45 const allocator_type& a = allocator_type());
46 template <class InputIterator>
47 hash_map(InputIterator f, InputIterator l,
48 size_type n = 193, const hasher& hf = hasher(),
49 const key_equal& eql = key_equal(),
50 const allocator_type& a = allocator_type());
51 hash_map(const hash_map&);
52 ~hash_map();
53 hash_map& operator=(const hash_map&);
54
55 allocator_type get_allocator() const;
56
57 bool empty() const;
58 size_type size() const;
59 size_type max_size() const;
60
61 iterator begin();
62 iterator end();
63 const_iterator begin() const;
64 const_iterator end() const;
65
66 pair<iterator, bool> insert(const value_type& obj);
67 template <class InputIterator>
68 void insert(InputIterator first, InputIterator last);
69
70 void erase(const_iterator position);
71 size_type erase(const key_type& k);
72 void erase(const_iterator first, const_iterator last);
73 void clear();
74
75 void swap(hash_map&);
76
77 hasher hash_funct() const;
78 key_equal key_eq() const;
79
80 iterator find(const key_type& k);
81 const_iterator find(const key_type& k) const;
82 size_type count(const key_type& k) const;
83 pair<iterator, iterator> equal_range(const key_type& k);
84 pair<const_iterator, const_iterator> equal_range(const key_type& k) const;
85
86 mapped_type& operator[](const key_type& k);
87
88 size_type bucket_count() const;
89 size_type max_bucket_count() const;
90
91 size_type elems_in_bucket(size_type n) const;
92
93 void resize(size_type n);
94};
95
96template <class Key, class T, class Hash, class Pred, class Alloc>
97 void swap(hash_map<Key, T, Hash, Pred, Alloc>& x,
98 hash_map<Key, T, Hash, Pred, Alloc>& y);
99
100template <class Key, class T, class Hash, class Pred, class Alloc>
101 bool
102 operator==(const hash_map<Key, T, Hash, Pred, Alloc>& x,
103 const hash_map<Key, T, Hash, Pred, Alloc>& y);
104
105template <class Key, class T, class Hash, class Pred, class Alloc>
106 bool
107 operator!=(const hash_map<Key, T, Hash, Pred, Alloc>& x,
108 const hash_map<Key, T, Hash, Pred, Alloc>& y);
109
110template <class Key, class T, class Hash = hash<Key>, class Pred = equal_to<Key>,
111 class Alloc = allocator<pair<const Key, T>>>
112class hash_multimap
113{
114public:
115 // types
116 typedef Key key_type;
117 typedef T mapped_type;
118 typedef Hash hasher;
119 typedef Pred key_equal;
120 typedef Alloc allocator_type;
121 typedef pair<const key_type, mapped_type> value_type;
122 typedef value_type& reference;
123 typedef const value_type& const_reference;
124 typedef typename allocator_traits<allocator_type>::pointer pointer;
125 typedef typename allocator_traits<allocator_type>::const_pointer const_pointer;
126 typedef typename allocator_traits<allocator_type>::size_type size_type;
127 typedef typename allocator_traits<allocator_type>::difference_type difference_type;
128
129 typedef /unspecified/ iterator;
130 typedef /unspecified/ const_iterator;
131
132 explicit hash_multimap(size_type n = 193, const hasher& hf = hasher(),
133 const key_equal& eql = key_equal(),
134 const allocator_type& a = allocator_type());
135 template <class InputIterator>
136 hash_multimap(InputIterator f, InputIterator l,
137 size_type n = 193, const hasher& hf = hasher(),
138 const key_equal& eql = key_equal(),
139 const allocator_type& a = allocator_type());
140 explicit hash_multimap(const allocator_type&);
141 hash_multimap(const hash_multimap&);
142 ~hash_multimap();
143 hash_multimap& operator=(const hash_multimap&);
144
145 allocator_type get_allocator() const;
146
147 bool empty() const;
148 size_type size() const;
149 size_type max_size() const;
150
151 iterator begin();
152 iterator end();
153 const_iterator begin() const;
154 const_iterator end() const;
155
156 iterator insert(const value_type& obj);
157 template <class InputIterator>
158 void insert(InputIterator first, InputIterator last);
159
160 void erase(const_iterator position);
161 size_type erase(const key_type& k);
162 void erase(const_iterator first, const_iterator last);
163 void clear();
164
165 void swap(hash_multimap&);
166
167 hasher hash_funct() const;
168 key_equal key_eq() const;
169
170 iterator find(const key_type& k);
171 const_iterator find(const key_type& k) const;
172 size_type count(const key_type& k) const;
173 pair<iterator, iterator> equal_range(const key_type& k);
174 pair<const_iterator, const_iterator> equal_range(const key_type& k) const;
175
176 size_type bucket_count() const;
177 size_type max_bucket_count() const;
178
179 size_type elems_in_bucket(size_type n) const;
180
181 void resize(size_type n);
182};
183
184template <class Key, class T, class Hash, class Pred, class Alloc>
185 void swap(hash_multimap<Key, T, Hash, Pred, Alloc>& x,
186 hash_multimap<Key, T, Hash, Pred, Alloc>& y);
187
188template <class Key, class T, class Hash, class Pred, class Alloc>
189 bool
190 operator==(const hash_multimap<Key, T, Hash, Pred, Alloc>& x,
191 const hash_multimap<Key, T, Hash, Pred, Alloc>& y);
192
193template <class Key, class T, class Hash, class Pred, class Alloc>
194 bool
195 operator!=(const hash_multimap<Key, T, Hash, Pred, Alloc>& x,
196 const hash_multimap<Key, T, Hash, Pred, Alloc>& y);
197
198} // __gnu_cxx
199
200*/
201
202#include <__config>
203#include <__hash_table>
204#include <functional>
205#include <stdexcept>
206
Howard Hinnant1dba4452011-07-24 23:59:50 +0000207#if __DEPRECATED
Howard Hinnant3e519522010-05-11 19:42:16 +0000208#warning Use of the header <ext/hash_map> is deprecated. Migrate to <unordered_map>
Howard Hinnant1dba4452011-07-24 23:59:50 +0000209#endif
Howard Hinnant3e519522010-05-11 19:42:16 +0000210
211#pragma GCC system_header
212
213namespace __gnu_cxx {
214
215using namespace std;
216
217template <class _Tp, class _Hash, bool = is_empty<_Hash>::value>
218class __hash_map_hasher
219 : private _Hash
220{
221public:
Howard Hinnantfb100022010-09-21 21:28:23 +0000222 _LIBCPP_INLINE_VISIBILITY __hash_map_hasher() : _Hash() {}
223 _LIBCPP_INLINE_VISIBILITY __hash_map_hasher(const _Hash& __h) : _Hash(__h) {}
224 _LIBCPP_INLINE_VISIBILITY const _Hash& hash_function() const {return *this;}
225 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +0000226 size_t operator()(const _Tp& __x) const
227 {return static_cast<const _Hash&>(*this)(__x.first);}
Howard Hinnantfb100022010-09-21 21:28:23 +0000228 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +0000229 size_t operator()(const typename _Tp::first_type& __x) const
230 {return static_cast<const _Hash&>(*this)(__x);}
231};
232
233template <class _Tp, class _Hash>
234class __hash_map_hasher<_Tp, _Hash, false>
235{
236 _Hash __hash_;
237public:
Howard Hinnantfb100022010-09-21 21:28:23 +0000238 _LIBCPP_INLINE_VISIBILITY __hash_map_hasher() : __hash_() {}
239 _LIBCPP_INLINE_VISIBILITY __hash_map_hasher(const _Hash& __h) : __hash_(__h) {}
240 _LIBCPP_INLINE_VISIBILITY const _Hash& hash_function() const {return __hash_;}
241 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +0000242 size_t operator()(const _Tp& __x) const
243 {return __hash_(__x.first);}
Howard Hinnantfb100022010-09-21 21:28:23 +0000244 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +0000245 size_t operator()(const typename _Tp::first_type& __x) const
246 {return __hash_(__x);}
247};
248
249template <class _Tp, class _Pred, bool = is_empty<_Pred>::value>
250class __hash_map_equal
251 : private _Pred
252{
253public:
Howard Hinnantfb100022010-09-21 21:28:23 +0000254 _LIBCPP_INLINE_VISIBILITY __hash_map_equal() : _Pred() {}
255 _LIBCPP_INLINE_VISIBILITY __hash_map_equal(const _Pred& __p) : _Pred(__p) {}
256 _LIBCPP_INLINE_VISIBILITY const _Pred& key_eq() const {return *this;}
257 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +0000258 bool operator()(const _Tp& __x, const _Tp& __y) const
259 {return static_cast<const _Pred&>(*this)(__x.first, __y.first);}
Howard Hinnantfb100022010-09-21 21:28:23 +0000260 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +0000261 bool operator()(const typename _Tp::first_type& __x, const _Tp& __y) const
262 {return static_cast<const _Pred&>(*this)(__x, __y.first);}
Howard Hinnantfb100022010-09-21 21:28:23 +0000263 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +0000264 bool operator()(const _Tp& __x, const typename _Tp::first_type& __y) const
265 {return static_cast<const _Pred&>(*this)(__x.first, __y);}
Howard Hinnantfb100022010-09-21 21:28:23 +0000266 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantb3371f62010-08-22 00:02:43 +0000267 bool operator()(const typename _Tp::first_type& __x,
Howard Hinnant3e519522010-05-11 19:42:16 +0000268 const typename _Tp::first_type& __y) const
269 {return static_cast<const _Pred&>(*this)(__x, __y);}
270};
271
272template <class _Tp, class _Pred>
273class __hash_map_equal<_Tp, _Pred, false>
274{
275 _Pred __pred_;
276public:
Howard Hinnantfb100022010-09-21 21:28:23 +0000277 _LIBCPP_INLINE_VISIBILITY __hash_map_equal() : __pred_() {}
278 _LIBCPP_INLINE_VISIBILITY __hash_map_equal(const _Pred& __p) : __pred_(__p) {}
279 _LIBCPP_INLINE_VISIBILITY const _Pred& key_eq() const {return __pred_;}
280 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +0000281 bool operator()(const _Tp& __x, const _Tp& __y) const
282 {return __pred_(__x.first, __y.first);}
Howard Hinnantfb100022010-09-21 21:28:23 +0000283 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +0000284 bool operator()(const typename _Tp::first_type& __x, const _Tp& __y) const
285 {return __pred_(__x, __y.first);}
Howard Hinnantfb100022010-09-21 21:28:23 +0000286 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +0000287 bool operator()(const _Tp& __x, const typename _Tp::first_type& __y) const
288 {return __pred_(__x.first, __y);}
Howard Hinnantfb100022010-09-21 21:28:23 +0000289 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +0000290 bool operator()(const typename _Tp::first_type& __x,
291 const typename _Tp::first_type& __y) const
292 {return __pred_(__x, __y);}
293};
294
295template <class _Alloc>
296class __hash_map_node_destructor
297{
298 typedef _Alloc allocator_type;
299 typedef allocator_traits<allocator_type> __alloc_traits;
300 typedef typename __alloc_traits::value_type::value_type value_type;
301public:
302 typedef typename __alloc_traits::pointer pointer;
303private:
304 typedef typename value_type::first_type first_type;
305 typedef typename value_type::second_type second_type;
306
307 allocator_type& __na_;
308
309 __hash_map_node_destructor& operator=(const __hash_map_node_destructor&);
310
311public:
312 bool __first_constructed;
313 bool __second_constructed;
314
Howard Hinnantfb100022010-09-21 21:28:23 +0000315 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +0000316 explicit __hash_map_node_destructor(allocator_type& __na)
317 : __na_(__na),
318 __first_constructed(false),
319 __second_constructed(false)
320 {}
321
Howard Hinnant7609c9b2010-09-04 23:28:19 +0000322#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantfb100022010-09-21 21:28:23 +0000323 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +0000324 __hash_map_node_destructor(__hash_node_destructor<allocator_type>&& __x)
325 : __na_(__x.__na_),
326 __first_constructed(__x.__value_constructed),
327 __second_constructed(__x.__value_constructed)
328 {
329 __x.__value_constructed = false;
330 }
Howard Hinnant7609c9b2010-09-04 23:28:19 +0000331#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantfb100022010-09-21 21:28:23 +0000332 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +0000333 __hash_map_node_destructor(const __hash_node_destructor<allocator_type>& __x)
334 : __na_(__x.__na_),
335 __first_constructed(__x.__value_constructed),
336 __second_constructed(__x.__value_constructed)
337 {
338 const_cast<bool&>(__x.__value_constructed) = false;
339 }
Howard Hinnant7609c9b2010-09-04 23:28:19 +0000340#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant3e519522010-05-11 19:42:16 +0000341
Howard Hinnantfb100022010-09-21 21:28:23 +0000342 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +0000343 void operator()(pointer __p)
344 {
345 if (__second_constructed)
Howard Hinnantce48a112011-06-30 21:18:19 +0000346 __alloc_traits::destroy(__na_, _VSTD::addressof(__p->__value_.second));
Howard Hinnant3e519522010-05-11 19:42:16 +0000347 if (__first_constructed)
Howard Hinnantce48a112011-06-30 21:18:19 +0000348 __alloc_traits::destroy(__na_, _VSTD::addressof(__p->__value_.first));
Howard Hinnant3e519522010-05-11 19:42:16 +0000349 if (__p)
350 __alloc_traits::deallocate(__na_, __p, 1);
351 }
352};
353
354template <class _HashIterator>
Howard Hinnantfb100022010-09-21 21:28:23 +0000355class _LIBCPP_VISIBLE __hash_map_iterator
Howard Hinnant3e519522010-05-11 19:42:16 +0000356{
357 _HashIterator __i_;
358
359 typedef pointer_traits<typename _HashIterator::pointer> __pointer_traits;
360 typedef const typename _HashIterator::value_type::first_type key_type;
361 typedef typename _HashIterator::value_type::second_type mapped_type;
362public:
363 typedef forward_iterator_tag iterator_category;
364 typedef pair<key_type, mapped_type> value_type;
365 typedef typename _HashIterator::difference_type difference_type;
366 typedef value_type& reference;
367 typedef typename __pointer_traits::template
368#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
369 rebind<value_type>
370#else
371 rebind<value_type>::other
372#endif
373 pointer;
374
Howard Hinnantfb100022010-09-21 21:28:23 +0000375 _LIBCPP_INLINE_VISIBILITY __hash_map_iterator() {}
Howard Hinnant3e519522010-05-11 19:42:16 +0000376
Howard Hinnantfb100022010-09-21 21:28:23 +0000377 _LIBCPP_INLINE_VISIBILITY __hash_map_iterator(_HashIterator __i) : __i_(__i) {}
Howard Hinnant3e519522010-05-11 19:42:16 +0000378
Howard Hinnantfb100022010-09-21 21:28:23 +0000379 _LIBCPP_INLINE_VISIBILITY reference operator*() const {return *operator->();}
380 _LIBCPP_INLINE_VISIBILITY pointer operator->() const {return (pointer)__i_.operator->();}
Howard Hinnant3e519522010-05-11 19:42:16 +0000381
Howard Hinnantfb100022010-09-21 21:28:23 +0000382 _LIBCPP_INLINE_VISIBILITY __hash_map_iterator& operator++() {++__i_; return *this;}
383 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +0000384 __hash_map_iterator operator++(int)
385 {
386 __hash_map_iterator __t(*this);
387 ++(*this);
388 return __t;
389 }
390
Howard Hinnantfb100022010-09-21 21:28:23 +0000391 friend _LIBCPP_INLINE_VISIBILITY
392 bool operator==(const __hash_map_iterator& __x, const __hash_map_iterator& __y)
Howard Hinnant3e519522010-05-11 19:42:16 +0000393 {return __x.__i_ == __y.__i_;}
Howard Hinnantfb100022010-09-21 21:28:23 +0000394 friend _LIBCPP_INLINE_VISIBILITY
395 bool operator!=(const __hash_map_iterator& __x, const __hash_map_iterator& __y)
Howard Hinnant3e519522010-05-11 19:42:16 +0000396 {return __x.__i_ != __y.__i_;}
397
Howard Hinnantfb100022010-09-21 21:28:23 +0000398 template <class, class, class, class, class> friend class _LIBCPP_VISIBLE hash_map;
399 template <class, class, class, class, class> friend class _LIBCPP_VISIBLE hash_multimap;
400 template <class> friend class _LIBCPP_VISIBLE __hash_const_iterator;
401 template <class> friend class _LIBCPP_VISIBLE __hash_const_local_iterator;
402 template <class> friend class _LIBCPP_VISIBLE __hash_map_const_iterator;
Howard Hinnant3e519522010-05-11 19:42:16 +0000403};
404
405template <class _HashIterator>
Howard Hinnantfb100022010-09-21 21:28:23 +0000406class _LIBCPP_VISIBLE __hash_map_const_iterator
Howard Hinnant3e519522010-05-11 19:42:16 +0000407{
408 _HashIterator __i_;
409
410 typedef pointer_traits<typename _HashIterator::pointer> __pointer_traits;
411 typedef const typename _HashIterator::value_type::first_type key_type;
412 typedef typename _HashIterator::value_type::second_type mapped_type;
413public:
414 typedef forward_iterator_tag iterator_category;
415 typedef pair<key_type, mapped_type> value_type;
416 typedef typename _HashIterator::difference_type difference_type;
417 typedef const value_type& reference;
418 typedef typename __pointer_traits::template
419#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
420 rebind<value_type>
421#else
422 rebind<value_type>::other
423#endif
424 pointer;
425
Howard Hinnantfb100022010-09-21 21:28:23 +0000426 _LIBCPP_INLINE_VISIBILITY __hash_map_const_iterator() {}
Howard Hinnant3e519522010-05-11 19:42:16 +0000427
Howard Hinnantfb100022010-09-21 21:28:23 +0000428 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +0000429 __hash_map_const_iterator(_HashIterator __i) : __i_(__i) {}
Howard Hinnantfb100022010-09-21 21:28:23 +0000430 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +0000431 __hash_map_const_iterator(
432 __hash_map_iterator<typename _HashIterator::__non_const_iterator> __i)
433 : __i_(__i.__i_) {}
434
Howard Hinnantfb100022010-09-21 21:28:23 +0000435 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +0000436 reference operator*() const {return *operator->();}
Howard Hinnantfb100022010-09-21 21:28:23 +0000437 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +0000438 pointer operator->() const {return (pointer)__i_.operator->();}
439
Howard Hinnantfb100022010-09-21 21:28:23 +0000440 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +0000441 __hash_map_const_iterator& operator++() {++__i_; return *this;}
Howard Hinnantfb100022010-09-21 21:28:23 +0000442 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +0000443 __hash_map_const_iterator operator++(int)
444 {
445 __hash_map_const_iterator __t(*this);
446 ++(*this);
447 return __t;
448 }
449
Howard Hinnantfb100022010-09-21 21:28:23 +0000450 friend _LIBCPP_INLINE_VISIBILITY
451 bool operator==(const __hash_map_const_iterator& __x, const __hash_map_const_iterator& __y)
Howard Hinnant3e519522010-05-11 19:42:16 +0000452 {return __x.__i_ == __y.__i_;}
Howard Hinnantfb100022010-09-21 21:28:23 +0000453 friend _LIBCPP_INLINE_VISIBILITY
454 bool operator!=(const __hash_map_const_iterator& __x, const __hash_map_const_iterator& __y)
Howard Hinnant3e519522010-05-11 19:42:16 +0000455 {return __x.__i_ != __y.__i_;}
456
Howard Hinnantfb100022010-09-21 21:28:23 +0000457 template <class, class, class, class, class> friend class _LIBCPP_VISIBLE hash_map;
458 template <class, class, class, class, class> friend class _LIBCPP_VISIBLE hash_multimap;
459 template <class> friend class _LIBCPP_VISIBLE __hash_const_iterator;
460 template <class> friend class _LIBCPP_VISIBLE __hash_const_local_iterator;
Howard Hinnant3e519522010-05-11 19:42:16 +0000461};
462
463template <class _Key, class _Tp, class _Hash = hash<_Key>, class _Pred = equal_to<_Key>,
464 class _Alloc = allocator<pair<const _Key, _Tp> > >
Howard Hinnantfb100022010-09-21 21:28:23 +0000465class _LIBCPP_VISIBLE hash_map
Howard Hinnant3e519522010-05-11 19:42:16 +0000466{
467public:
468 // types
469 typedef _Key key_type;
470 typedef _Tp mapped_type;
471 typedef _Hash hasher;
472 typedef _Pred key_equal;
473 typedef _Alloc allocator_type;
474 typedef pair<const key_type, mapped_type> value_type;
475 typedef value_type& reference;
476 typedef const value_type& const_reference;
477
478private:
479 typedef pair<key_type, mapped_type> __value_type;
480 typedef __hash_map_hasher<__value_type, hasher> __hasher;
481 typedef __hash_map_equal<__value_type, key_equal> __key_equal;
482 typedef typename allocator_traits<allocator_type>::template
483#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
484 rebind_alloc<__value_type>
485#else
486 rebind_alloc<__value_type>::other
487#endif
488 __allocator_type;
489
490 typedef __hash_table<__value_type, __hasher,
491 __key_equal, __allocator_type> __table;
492
493 __table __table_;
494
495 typedef typename __table::__node_pointer __node_pointer;
496 typedef typename __table::__node_const_pointer __node_const_pointer;
497 typedef typename __table::__node_traits __node_traits;
498 typedef typename __table::__node_allocator __node_allocator;
499 typedef typename __table::__node __node;
500 typedef __hash_map_node_destructor<__node_allocator> _D;
501 typedef unique_ptr<__node, _D> __node_holder;
502 typedef allocator_traits<allocator_type> __alloc_traits;
503public:
504 typedef typename __alloc_traits::pointer pointer;
505 typedef typename __alloc_traits::const_pointer const_pointer;
506 typedef typename __alloc_traits::size_type size_type;
507 typedef typename __alloc_traits::difference_type difference_type;
508
509 typedef __hash_map_iterator<typename __table::iterator> iterator;
510 typedef __hash_map_const_iterator<typename __table::const_iterator> const_iterator;
511
Howard Hinnantfb100022010-09-21 21:28:23 +0000512 _LIBCPP_INLINE_VISIBILITY hash_map() {__table_.rehash(193);}
Howard Hinnant3e519522010-05-11 19:42:16 +0000513 explicit hash_map(size_type __n, const hasher& __hf = hasher(),
514 const key_equal& __eql = key_equal());
515 hash_map(size_type __n, const hasher& __hf,
516 const key_equal& __eql,
517 const allocator_type& __a);
518 template <class _InputIterator>
519 hash_map(_InputIterator __first, _InputIterator __last);
520 template <class _InputIterator>
521 hash_map(_InputIterator __first, _InputIterator __last,
522 size_type __n, const hasher& __hf = hasher(),
523 const key_equal& __eql = key_equal());
524 template <class _InputIterator>
525 hash_map(_InputIterator __first, _InputIterator __last,
526 size_type __n, const hasher& __hf,
527 const key_equal& __eql,
528 const allocator_type& __a);
529 hash_map(const hash_map& __u);
530
Howard Hinnantfb100022010-09-21 21:28:23 +0000531 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +0000532 allocator_type get_allocator() const
533 {return allocator_type(__table_.__node_alloc());}
534
Howard Hinnantfb100022010-09-21 21:28:23 +0000535 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +0000536 bool empty() const {return __table_.size() == 0;}
Howard Hinnantfb100022010-09-21 21:28:23 +0000537 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +0000538 size_type size() const {return __table_.size();}
Howard Hinnantfb100022010-09-21 21:28:23 +0000539 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +0000540 size_type max_size() const {return __table_.max_size();}
541
Howard Hinnantfb100022010-09-21 21:28:23 +0000542 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +0000543 iterator begin() {return __table_.begin();}
Howard Hinnantfb100022010-09-21 21:28:23 +0000544 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +0000545 iterator end() {return __table_.end();}
Howard Hinnantfb100022010-09-21 21:28:23 +0000546 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +0000547 const_iterator begin() const {return __table_.begin();}
Howard Hinnantfb100022010-09-21 21:28:23 +0000548 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +0000549 const_iterator end() const {return __table_.end();}
550
Howard Hinnantfb100022010-09-21 21:28:23 +0000551 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +0000552 pair<iterator, bool> insert(const value_type& __x)
553 {return __table_.__insert_unique(__x);}
554 template <class _InputIterator>
555 void insert(_InputIterator __first, _InputIterator __last);
556
Howard Hinnantfb100022010-09-21 21:28:23 +0000557 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +0000558 void erase(const_iterator __p) {__table_.erase(__p.__i_);}
Howard Hinnantfb100022010-09-21 21:28:23 +0000559 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +0000560 size_type erase(const key_type& __k) {return __table_.__erase_unique(__k);}
Howard Hinnantfb100022010-09-21 21:28:23 +0000561 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +0000562 void erase(const_iterator __first, const_iterator __last)
563 {__table_.erase(__first.__i_, __last.__i_);}
Howard Hinnantfb100022010-09-21 21:28:23 +0000564 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +0000565 void clear() {__table_.clear();}
566
Howard Hinnantfb100022010-09-21 21:28:23 +0000567 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +0000568 void swap(hash_map& __u) {__table_.swap(__u.__table_);}
569
Howard Hinnantfb100022010-09-21 21:28:23 +0000570 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +0000571 hasher hash_funct() const
572 {return __table_.hash_function().hash_function();}
Howard Hinnantfb100022010-09-21 21:28:23 +0000573 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +0000574 key_equal key_eq() const
575 {return __table_.key_eq().key_eq();}
576
Howard Hinnantfb100022010-09-21 21:28:23 +0000577 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +0000578 iterator find(const key_type& __k) {return __table_.find(__k);}
Howard Hinnantfb100022010-09-21 21:28:23 +0000579 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +0000580 const_iterator find(const key_type& __k) const {return __table_.find(__k);}
Howard Hinnantfb100022010-09-21 21:28:23 +0000581 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +0000582 size_type count(const key_type& __k) const {return __table_.__count_unique(__k);}
Howard Hinnantfb100022010-09-21 21:28:23 +0000583 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +0000584 pair<iterator, iterator> equal_range(const key_type& __k)
585 {return __table_.__equal_range_unique(__k);}
Howard Hinnantfb100022010-09-21 21:28:23 +0000586 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +0000587 pair<const_iterator, const_iterator> equal_range(const key_type& __k) const
588 {return __table_.__equal_range_unique(__k);}
589
590 mapped_type& operator[](const key_type& __k);
591
Howard Hinnantfb100022010-09-21 21:28:23 +0000592 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +0000593 size_type bucket_count() const {return __table_.bucket_count();}
Howard Hinnantfb100022010-09-21 21:28:23 +0000594 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +0000595 size_type max_bucket_count() const {return __table_.max_bucket_count();}
596
Howard Hinnantfb100022010-09-21 21:28:23 +0000597 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +0000598 size_type elems_in_bucket(size_type __n) const
599 {return __table_.bucket_size(__n);}
600
Howard Hinnantfb100022010-09-21 21:28:23 +0000601 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +0000602 void resize(size_type __n) {__table_.rehash(__n);}
603
604private:
605 __node_holder __construct_node(const key_type& __k);
606};
607
608template <class _Key, class _Tp, class _Hash, class _Pred, class _Alloc>
609hash_map<_Key, _Tp, _Hash, _Pred, _Alloc>::hash_map(
610 size_type __n, const hasher& __hf, const key_equal& __eql)
611 : __table_(__hf, __eql)
612{
613 __table_.rehash(__n);
614}
615
616template <class _Key, class _Tp, class _Hash, class _Pred, class _Alloc>
617hash_map<_Key, _Tp, _Hash, _Pred, _Alloc>::hash_map(
618 size_type __n, const hasher& __hf, const key_equal& __eql,
619 const allocator_type& __a)
620 : __table_(__hf, __eql, __a)
621{
622 __table_.rehash(__n);
623}
624
625template <class _Key, class _Tp, class _Hash, class _Pred, class _Alloc>
626template <class _InputIterator>
627hash_map<_Key, _Tp, _Hash, _Pred, _Alloc>::hash_map(
628 _InputIterator __first, _InputIterator __last)
629{
630 __table_.rehash(193);
631 insert(__first, __last);
632}
633
634template <class _Key, class _Tp, class _Hash, class _Pred, class _Alloc>
635template <class _InputIterator>
636hash_map<_Key, _Tp, _Hash, _Pred, _Alloc>::hash_map(
637 _InputIterator __first, _InputIterator __last, size_type __n,
638 const hasher& __hf, const key_equal& __eql)
639 : __table_(__hf, __eql)
640{
641 __table_.rehash(__n);
642 insert(__first, __last);
643}
644
645template <class _Key, class _Tp, class _Hash, class _Pred, class _Alloc>
646template <class _InputIterator>
647hash_map<_Key, _Tp, _Hash, _Pred, _Alloc>::hash_map(
648 _InputIterator __first, _InputIterator __last, size_type __n,
649 const hasher& __hf, const key_equal& __eql, const allocator_type& __a)
650 : __table_(__hf, __eql, __a)
651{
652 __table_.rehash(__n);
653 insert(__first, __last);
654}
655
656template <class _Key, class _Tp, class _Hash, class _Pred, class _Alloc>
657hash_map<_Key, _Tp, _Hash, _Pred, _Alloc>::hash_map(
658 const hash_map& __u)
659 : __table_(__u.__table_)
660{
661 __table_.rehash(__u.bucket_count());
662 insert(__u.begin(), __u.end());
663}
664
665template <class _Key, class _Tp, class _Hash, class _Pred, class _Alloc>
666typename hash_map<_Key, _Tp, _Hash, _Pred, _Alloc>::__node_holder
667hash_map<_Key, _Tp, _Hash, _Pred, _Alloc>::__construct_node(const key_type& __k)
668{
669 __node_allocator& __na = __table_.__node_alloc();
670 __node_holder __h(__node_traits::allocate(__na, 1), _D(__na));
Howard Hinnantce48a112011-06-30 21:18:19 +0000671 __node_traits::construct(__na, _VSTD::addressof(__h->__value_.first), __k);
Howard Hinnant3e519522010-05-11 19:42:16 +0000672 __h.get_deleter().__first_constructed = true;
Howard Hinnantce48a112011-06-30 21:18:19 +0000673 __node_traits::construct(__na, _VSTD::addressof(__h->__value_.second));
Howard Hinnant3e519522010-05-11 19:42:16 +0000674 __h.get_deleter().__second_constructed = true;
Howard Hinnantce48a112011-06-30 21:18:19 +0000675 return _VSTD::move(__h);
Howard Hinnant3e519522010-05-11 19:42:16 +0000676}
677
678template <class _Key, class _Tp, class _Hash, class _Pred, class _Alloc>
679template <class _InputIterator>
Howard Hinnantfb100022010-09-21 21:28:23 +0000680inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +0000681void
682hash_map<_Key, _Tp, _Hash, _Pred, _Alloc>::insert(_InputIterator __first,
683 _InputIterator __last)
684{
685 for (; __first != __last; ++__first)
686 __table_.__insert_unique(*__first);
687}
688
689template <class _Key, class _Tp, class _Hash, class _Pred, class _Alloc>
690_Tp&
691hash_map<_Key, _Tp, _Hash, _Pred, _Alloc>::operator[](const key_type& __k)
692{
693 iterator __i = find(__k);
694 if (__i != end())
695 return __i->second;
696 __node_holder __h = __construct_node(__k);
697 pair<iterator, bool> __r = __table_.__node_insert_unique(__h.get());
698 __h.release();
699 return __r.first->second;
700}
701
702template <class _Key, class _Tp, class _Hash, class _Pred, class _Alloc>
Howard Hinnantfb100022010-09-21 21:28:23 +0000703inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +0000704void
705swap(hash_map<_Key, _Tp, _Hash, _Pred, _Alloc>& __x,
706 hash_map<_Key, _Tp, _Hash, _Pred, _Alloc>& __y)
707{
708 __x.swap(__y);
709}
710
711template <class _Key, class _Tp, class _Hash, class _Pred, class _Alloc>
712bool
713operator==(const hash_map<_Key, _Tp, _Hash, _Pred, _Alloc>& __x,
714 const hash_map<_Key, _Tp, _Hash, _Pred, _Alloc>& __y)
715{
716 if (__x.size() != __y.size())
717 return false;
718 typedef typename hash_map<_Key, _Tp, _Hash, _Pred, _Alloc>::const_iterator
719 const_iterator;
720 for (const_iterator __i = __x.begin(), __ex = __x.end(), __ey = __y.end();
721 __i != __ex; ++__i)
722 {
723 const_iterator __j = __y.find(__i->first);
724 if (__j == __ey || !(*__i == *__j))
725 return false;
726 }
727 return true;
728}
729
730template <class _Key, class _Tp, class _Hash, class _Pred, class _Alloc>
Howard Hinnantfb100022010-09-21 21:28:23 +0000731inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +0000732bool
733operator!=(const hash_map<_Key, _Tp, _Hash, _Pred, _Alloc>& __x,
734 const hash_map<_Key, _Tp, _Hash, _Pred, _Alloc>& __y)
735{
736 return !(__x == __y);
737}
738
739template <class _Key, class _Tp, class _Hash = hash<_Key>, class _Pred = equal_to<_Key>,
740 class _Alloc = allocator<pair<const _Key, _Tp> > >
Howard Hinnantfb100022010-09-21 21:28:23 +0000741class _LIBCPP_VISIBLE hash_multimap
Howard Hinnant3e519522010-05-11 19:42:16 +0000742{
743public:
744 // types
745 typedef _Key key_type;
746 typedef _Tp mapped_type;
747 typedef _Hash hasher;
748 typedef _Pred key_equal;
749 typedef _Alloc allocator_type;
750 typedef pair<const key_type, mapped_type> value_type;
751 typedef value_type& reference;
752 typedef const value_type& const_reference;
753
754private:
755 typedef pair<key_type, mapped_type> __value_type;
756 typedef __hash_map_hasher<__value_type, hasher> __hasher;
757 typedef __hash_map_equal<__value_type, key_equal> __key_equal;
758 typedef typename allocator_traits<allocator_type>::template
759#ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
760 rebind_alloc<__value_type>
761#else
762 rebind_alloc<__value_type>::other
763#endif
764 __allocator_type;
765
766 typedef __hash_table<__value_type, __hasher,
767 __key_equal, __allocator_type> __table;
768
769 __table __table_;
770
771 typedef typename __table::__node_traits __node_traits;
772 typedef typename __table::__node_allocator __node_allocator;
773 typedef typename __table::__node __node;
774 typedef __hash_map_node_destructor<__node_allocator> _D;
775 typedef unique_ptr<__node, _D> __node_holder;
776 typedef allocator_traits<allocator_type> __alloc_traits;
777public:
778 typedef typename __alloc_traits::pointer pointer;
779 typedef typename __alloc_traits::const_pointer const_pointer;
780 typedef typename __alloc_traits::size_type size_type;
781 typedef typename __alloc_traits::difference_type difference_type;
782
783 typedef __hash_map_iterator<typename __table::iterator> iterator;
784 typedef __hash_map_const_iterator<typename __table::const_iterator> const_iterator;
785
Howard Hinnantfb100022010-09-21 21:28:23 +0000786 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +0000787 hash_multimap() {__table_.rehash(193);}
788 explicit hash_multimap(size_type __n, const hasher& __hf = hasher(),
789 const key_equal& __eql = key_equal());
790 hash_multimap(size_type __n, const hasher& __hf,
791 const key_equal& __eql,
792 const allocator_type& __a);
793 template <class _InputIterator>
794 hash_multimap(_InputIterator __first, _InputIterator __last);
795 template <class _InputIterator>
796 hash_multimap(_InputIterator __first, _InputIterator __last,
797 size_type __n, const hasher& __hf = hasher(),
798 const key_equal& __eql = key_equal());
799 template <class _InputIterator>
800 hash_multimap(_InputIterator __first, _InputIterator __last,
801 size_type __n, const hasher& __hf,
802 const key_equal& __eql,
803 const allocator_type& __a);
804 hash_multimap(const hash_multimap& __u);
805
Howard Hinnantfb100022010-09-21 21:28:23 +0000806 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +0000807 allocator_type get_allocator() const
808 {return allocator_type(__table_.__node_alloc());}
809
Howard Hinnantfb100022010-09-21 21:28:23 +0000810 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +0000811 bool empty() const {return __table_.size() == 0;}
Howard Hinnantfb100022010-09-21 21:28:23 +0000812 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +0000813 size_type size() const {return __table_.size();}
Howard Hinnantfb100022010-09-21 21:28:23 +0000814 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +0000815 size_type max_size() const {return __table_.max_size();}
816
Howard Hinnantfb100022010-09-21 21:28:23 +0000817 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +0000818 iterator begin() {return __table_.begin();}
Howard Hinnantfb100022010-09-21 21:28:23 +0000819 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +0000820 iterator end() {return __table_.end();}
Howard Hinnantfb100022010-09-21 21:28:23 +0000821 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +0000822 const_iterator begin() const {return __table_.begin();}
Howard Hinnantfb100022010-09-21 21:28:23 +0000823 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +0000824 const_iterator end() const {return __table_.end();}
825
Howard Hinnantfb100022010-09-21 21:28:23 +0000826 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +0000827 iterator insert(const value_type& __x) {return __table_.__insert_multi(__x);}
828 template <class _InputIterator>
829 void insert(_InputIterator __first, _InputIterator __last);
830
Howard Hinnantfb100022010-09-21 21:28:23 +0000831 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +0000832 void erase(const_iterator __p) {__table_.erase(__p.__i_);}
Howard Hinnantfb100022010-09-21 21:28:23 +0000833 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +0000834 size_type erase(const key_type& __k) {return __table_.__erase_multi(__k);}
Howard Hinnantfb100022010-09-21 21:28:23 +0000835 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +0000836 void erase(const_iterator __first, const_iterator __last)
837 {__table_.erase(__first.__i_, __last.__i_);}
Howard Hinnantfb100022010-09-21 21:28:23 +0000838 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +0000839 void clear() {__table_.clear();}
840
Howard Hinnantfb100022010-09-21 21:28:23 +0000841 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +0000842 void swap(hash_multimap& __u) {__table_.swap(__u.__table_);}
843
Howard Hinnantfb100022010-09-21 21:28:23 +0000844 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +0000845 hasher hash_funct() const
846 {return __table_.hash_function().hash_function();}
Howard Hinnantfb100022010-09-21 21:28:23 +0000847 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +0000848 key_equal key_eq() const
849 {return __table_.key_eq().key_eq();}
850
Howard Hinnantfb100022010-09-21 21:28:23 +0000851 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +0000852 iterator find(const key_type& __k) {return __table_.find(__k);}
Howard Hinnantfb100022010-09-21 21:28:23 +0000853 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +0000854 const_iterator find(const key_type& __k) const {return __table_.find(__k);}
Howard Hinnantfb100022010-09-21 21:28:23 +0000855 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +0000856 size_type count(const key_type& __k) const {return __table_.__count_multi(__k);}
Howard Hinnantfb100022010-09-21 21:28:23 +0000857 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +0000858 pair<iterator, iterator> equal_range(const key_type& __k)
859 {return __table_.__equal_range_multi(__k);}
Howard Hinnantfb100022010-09-21 21:28:23 +0000860 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +0000861 pair<const_iterator, const_iterator> equal_range(const key_type& __k) const
862 {return __table_.__equal_range_multi(__k);}
863
Howard Hinnantfb100022010-09-21 21:28:23 +0000864 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +0000865 size_type bucket_count() const {return __table_.bucket_count();}
Howard Hinnantfb100022010-09-21 21:28:23 +0000866 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +0000867 size_type max_bucket_count() const {return __table_.max_bucket_count();}
868
Howard Hinnantfb100022010-09-21 21:28:23 +0000869 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +0000870 size_type elems_in_bucket(size_type __n) const
871 {return __table_.bucket_size(__n);}
872
Howard Hinnantfb100022010-09-21 21:28:23 +0000873 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +0000874 void resize(size_type __n) {__table_.rehash(__n);}
875};
876
877template <class _Key, class _Tp, class _Hash, class _Pred, class _Alloc>
878hash_multimap<_Key, _Tp, _Hash, _Pred, _Alloc>::hash_multimap(
879 size_type __n, const hasher& __hf, const key_equal& __eql)
880 : __table_(__hf, __eql)
881{
882 __table_.rehash(__n);
883}
884
885template <class _Key, class _Tp, class _Hash, class _Pred, class _Alloc>
886hash_multimap<_Key, _Tp, _Hash, _Pred, _Alloc>::hash_multimap(
887 size_type __n, const hasher& __hf, const key_equal& __eql,
888 const allocator_type& __a)
889 : __table_(__hf, __eql, __a)
890{
891 __table_.rehash(__n);
892}
893
894template <class _Key, class _Tp, class _Hash, class _Pred, class _Alloc>
895template <class _InputIterator>
896hash_multimap<_Key, _Tp, _Hash, _Pred, _Alloc>::hash_multimap(
897 _InputIterator __first, _InputIterator __last)
898{
899 __table_.rehash(193);
900 insert(__first, __last);
901}
902
903template <class _Key, class _Tp, class _Hash, class _Pred, class _Alloc>
904template <class _InputIterator>
905hash_multimap<_Key, _Tp, _Hash, _Pred, _Alloc>::hash_multimap(
906 _InputIterator __first, _InputIterator __last, size_type __n,
907 const hasher& __hf, const key_equal& __eql)
908 : __table_(__hf, __eql)
909{
910 __table_.rehash(__n);
911 insert(__first, __last);
912}
913
914template <class _Key, class _Tp, class _Hash, class _Pred, class _Alloc>
915template <class _InputIterator>
916hash_multimap<_Key, _Tp, _Hash, _Pred, _Alloc>::hash_multimap(
917 _InputIterator __first, _InputIterator __last, size_type __n,
918 const hasher& __hf, const key_equal& __eql, const allocator_type& __a)
919 : __table_(__hf, __eql, __a)
920{
921 __table_.rehash(__n);
922 insert(__first, __last);
923}
924
925template <class _Key, class _Tp, class _Hash, class _Pred, class _Alloc>
926hash_multimap<_Key, _Tp, _Hash, _Pred, _Alloc>::hash_multimap(
927 const hash_multimap& __u)
928 : __table_(__u.__table_)
929{
930 __table_.rehash(__u.bucket_count());
931 insert(__u.begin(), __u.end());
932}
933
934template <class _Key, class _Tp, class _Hash, class _Pred, class _Alloc>
935template <class _InputIterator>
Howard Hinnantfb100022010-09-21 21:28:23 +0000936inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +0000937void
938hash_multimap<_Key, _Tp, _Hash, _Pred, _Alloc>::insert(_InputIterator __first,
939 _InputIterator __last)
940{
941 for (; __first != __last; ++__first)
942 __table_.__insert_multi(*__first);
943}
944
945template <class _Key, class _Tp, class _Hash, class _Pred, class _Alloc>
Howard Hinnantfb100022010-09-21 21:28:23 +0000946inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +0000947void
948swap(hash_multimap<_Key, _Tp, _Hash, _Pred, _Alloc>& __x,
949 hash_multimap<_Key, _Tp, _Hash, _Pred, _Alloc>& __y)
950{
951 __x.swap(__y);
952}
953
954template <class _Key, class _Tp, class _Hash, class _Pred, class _Alloc>
955bool
956operator==(const hash_multimap<_Key, _Tp, _Hash, _Pred, _Alloc>& __x,
957 const hash_multimap<_Key, _Tp, _Hash, _Pred, _Alloc>& __y)
958{
959 if (__x.size() != __y.size())
960 return false;
961 typedef typename hash_multimap<_Key, _Tp, _Hash, _Pred, _Alloc>::const_iterator
962 const_iterator;
963 typedef pair<const_iterator, const_iterator> _EqRng;
964 for (const_iterator __i = __x.begin(), __ex = __x.end(); __i != __ex;)
965 {
966 _EqRng __xeq = __x.equal_range(__i->first);
967 _EqRng __yeq = __y.equal_range(__i->first);
Howard Hinnantce48a112011-06-30 21:18:19 +0000968 if (_VSTD::distance(__xeq.first, __xeq.second) !=
969 _VSTD::distance(__yeq.first, __yeq.second) ||
970 !_VSTD::is_permutation(__xeq.first, __xeq.second, __yeq.first))
Howard Hinnant3e519522010-05-11 19:42:16 +0000971 return false;
972 __i = __xeq.second;
973 }
974 return true;
975}
976
977template <class _Key, class _Tp, class _Hash, class _Pred, class _Alloc>
Howard Hinnantfb100022010-09-21 21:28:23 +0000978inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant3e519522010-05-11 19:42:16 +0000979bool
980operator!=(const hash_multimap<_Key, _Tp, _Hash, _Pred, _Alloc>& __x,
981 const hash_multimap<_Key, _Tp, _Hash, _Pred, _Alloc>& __y)
982{
983 return !(__x == __y);
984}
985
986} // __gnu_cxx
987
988#endif // _LIBCPP_HASH_MAP