2 //===-------------------------- hash_map ----------------------------------===//
4 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5 // See https://llvm.org/LICENSE.txt for license information.
6 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
8 //===----------------------------------------------------------------------===//
10 #ifndef _LIBCPP_HASH_MAP
11 #define _LIBCPP_HASH_MAP
20 template <class Key, class T, class Hash = hash<Key>, class Pred = equal_to<Key>,
21 class Alloc = allocator<pair<const Key, T>>>
27 typedef T mapped_type;
29 typedef Pred key_equal;
30 typedef Alloc allocator_type;
31 typedef pair<const key_type, mapped_type> value_type;
32 typedef value_type& reference;
33 typedef const value_type& const_reference;
34 typedef typename allocator_traits<allocator_type>::pointer pointer;
35 typedef typename allocator_traits<allocator_type>::const_pointer const_pointer;
36 typedef typename allocator_traits<allocator_type>::size_type size_type;
37 typedef typename allocator_traits<allocator_type>::difference_type difference_type;
39 typedef /unspecified/ iterator;
40 typedef /unspecified/ const_iterator;
42 explicit hash_map(size_type n = 193, const hasher& hf = hasher(),
43 const key_equal& eql = key_equal(),
44 const allocator_type& a = allocator_type());
45 template <class InputIterator>
46 hash_map(InputIterator f, InputIterator l,
47 size_type n = 193, const hasher& hf = hasher(),
48 const key_equal& eql = key_equal(),
49 const allocator_type& a = allocator_type());
50 hash_map(const hash_map&);
52 hash_map& operator=(const hash_map&);
54 allocator_type get_allocator() const;
57 size_type size() const;
58 size_type max_size() const;
62 const_iterator begin() const;
63 const_iterator end() const;
65 pair<iterator, bool> insert(const value_type& obj);
66 template <class InputIterator>
67 void insert(InputIterator first, InputIterator last);
69 void erase(const_iterator position);
70 size_type erase(const key_type& k);
71 void erase(const_iterator first, const_iterator last);
76 hasher hash_funct() const;
77 key_equal key_eq() const;
79 iterator find(const key_type& k);
80 const_iterator find(const key_type& k) const;
81 size_type count(const key_type& k) const;
82 pair<iterator, iterator> equal_range(const key_type& k);
83 pair<const_iterator, const_iterator> equal_range(const key_type& k) const;
85 mapped_type& operator[](const key_type& k);
87 size_type bucket_count() const;
88 size_type max_bucket_count() const;
90 size_type elems_in_bucket(size_type n) const;
92 void resize(size_type n);
95 template <class Key, class T, class Hash, class Pred, class Alloc>
96 void swap(hash_map<Key, T, Hash, Pred, Alloc>& x,
97 hash_map<Key, T, Hash, Pred, Alloc>& y);
99 template <class Key, class T, class Hash, class Pred, class Alloc>
101 operator==(const hash_map<Key, T, Hash, Pred, Alloc>& x,
102 const hash_map<Key, T, Hash, Pred, Alloc>& y);
104 template <class Key, class T, class Hash, class Pred, class Alloc>
106 operator!=(const hash_map<Key, T, Hash, Pred, Alloc>& x,
107 const hash_map<Key, T, Hash, Pred, Alloc>& y);
109 template <class Key, class T, class Hash = hash<Key>, class Pred = equal_to<Key>,
110 class Alloc = allocator<pair<const Key, T>>>
115 typedef Key key_type;
116 typedef T mapped_type;
118 typedef Pred key_equal;
119 typedef Alloc allocator_type;
120 typedef pair<const key_type, mapped_type> value_type;
121 typedef value_type& reference;
122 typedef const value_type& const_reference;
123 typedef typename allocator_traits<allocator_type>::pointer pointer;
124 typedef typename allocator_traits<allocator_type>::const_pointer const_pointer;
125 typedef typename allocator_traits<allocator_type>::size_type size_type;
126 typedef typename allocator_traits<allocator_type>::difference_type difference_type;
128 typedef /unspecified/ iterator;
129 typedef /unspecified/ const_iterator;
131 explicit hash_multimap(size_type n = 193, const hasher& hf = hasher(),
132 const key_equal& eql = key_equal(),
133 const allocator_type& a = allocator_type());
134 template <class InputIterator>
135 hash_multimap(InputIterator f, InputIterator l,
136 size_type n = 193, const hasher& hf = hasher(),
137 const key_equal& eql = key_equal(),
138 const allocator_type& a = allocator_type());
139 explicit hash_multimap(const allocator_type&);
140 hash_multimap(const hash_multimap&);
142 hash_multimap& operator=(const hash_multimap&);
144 allocator_type get_allocator() const;
147 size_type size() const;
148 size_type max_size() const;
152 const_iterator begin() const;
153 const_iterator end() const;
155 iterator insert(const value_type& obj);
156 template <class InputIterator>
157 void insert(InputIterator first, InputIterator last);
159 void erase(const_iterator position);
160 size_type erase(const key_type& k);
161 void erase(const_iterator first, const_iterator last);
164 void swap(hash_multimap&);
166 hasher hash_funct() const;
167 key_equal key_eq() const;
169 iterator find(const key_type& k);
170 const_iterator find(const key_type& k) const;
171 size_type count(const key_type& k) const;
172 pair<iterator, iterator> equal_range(const key_type& k);
173 pair<const_iterator, const_iterator> equal_range(const key_type& k) const;
175 size_type bucket_count() const;
176 size_type max_bucket_count() const;
178 size_type elems_in_bucket(size_type n) const;
180 void resize(size_type n);
183 template <class Key, class T, class Hash, class Pred, class Alloc>
184 void swap(hash_multimap<Key, T, Hash, Pred, Alloc>& x,
185 hash_multimap<Key, T, Hash, Pred, Alloc>& y);
187 template <class Key, class T, class Hash, class Pred, class Alloc>
189 operator==(const hash_multimap<Key, T, Hash, Pred, Alloc>& x,
190 const hash_multimap<Key, T, Hash, Pred, Alloc>& y);
192 template <class Key, class T, class Hash, class Pred, class Alloc>
194 operator!=(const hash_multimap<Key, T, Hash, Pred, Alloc>& x,
195 const hash_multimap<Key, T, Hash, Pred, Alloc>& y);
202 #include <__hash_table>
203 #include <functional>
205 #include <type_traits>
206 #include <ext/__hash>
209 #if defined(_LIBCPP_WARNING)
210 _LIBCPP_WARNING("Use of the header <ext/hash_map> is deprecated. Migrate to <unordered_map>")
212 # warning Use of the header <ext/hash_map> is deprecated. Migrate to <unordered_map>
216 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
217 #pragma GCC system_header
220 namespace __gnu_cxx {
222 template <class _Tp, class _Hash,
223 bool = std::is_empty<_Hash>::value && !std::__libcpp_is_final<_Hash>::value
225 class __hash_map_hasher
229 _LIBCPP_INLINE_VISIBILITY __hash_map_hasher() : _Hash() {}
230 _LIBCPP_INLINE_VISIBILITY __hash_map_hasher(const _Hash& __h) : _Hash(__h) {}
231 _LIBCPP_INLINE_VISIBILITY const _Hash& hash_function() const {return *this;}
232 _LIBCPP_INLINE_VISIBILITY
233 size_t operator()(const _Tp& __x) const
234 {return static_cast<const _Hash&>(*this)(__x.first);}
235 _LIBCPP_INLINE_VISIBILITY
236 size_t operator()(const typename _Tp::first_type& __x) const
237 {return static_cast<const _Hash&>(*this)(__x);}
240 template <class _Tp, class _Hash>
241 class __hash_map_hasher<_Tp, _Hash, false>
245 _LIBCPP_INLINE_VISIBILITY __hash_map_hasher() : __hash_() {}
246 _LIBCPP_INLINE_VISIBILITY __hash_map_hasher(const _Hash& __h) : __hash_(__h) {}
247 _LIBCPP_INLINE_VISIBILITY const _Hash& hash_function() const {return __hash_;}
248 _LIBCPP_INLINE_VISIBILITY
249 size_t operator()(const _Tp& __x) const
250 {return __hash_(__x.first);}
251 _LIBCPP_INLINE_VISIBILITY
252 size_t operator()(const typename _Tp::first_type& __x) const
253 {return __hash_(__x);}
256 template <class _Tp, class _Pred,
257 bool = std::is_empty<_Pred>::value && !std::__libcpp_is_final<_Pred>::value
259 class __hash_map_equal
263 _LIBCPP_INLINE_VISIBILITY __hash_map_equal() : _Pred() {}
264 _LIBCPP_INLINE_VISIBILITY __hash_map_equal(const _Pred& __p) : _Pred(__p) {}
265 _LIBCPP_INLINE_VISIBILITY const _Pred& key_eq() const {return *this;}
266 _LIBCPP_INLINE_VISIBILITY
267 bool operator()(const _Tp& __x, const _Tp& __y) const
268 {return static_cast<const _Pred&>(*this)(__x.first, __y.first);}
269 _LIBCPP_INLINE_VISIBILITY
270 bool operator()(const typename _Tp::first_type& __x, const _Tp& __y) const
271 {return static_cast<const _Pred&>(*this)(__x, __y.first);}
272 _LIBCPP_INLINE_VISIBILITY
273 bool operator()(const _Tp& __x, const typename _Tp::first_type& __y) const
274 {return static_cast<const _Pred&>(*this)(__x.first, __y);}
275 _LIBCPP_INLINE_VISIBILITY
276 bool operator()(const typename _Tp::first_type& __x,
277 const typename _Tp::first_type& __y) const
278 {return static_cast<const _Pred&>(*this)(__x, __y);}
281 template <class _Tp, class _Pred>
282 class __hash_map_equal<_Tp, _Pred, false>
286 _LIBCPP_INLINE_VISIBILITY __hash_map_equal() : __pred_() {}
287 _LIBCPP_INLINE_VISIBILITY __hash_map_equal(const _Pred& __p) : __pred_(__p) {}
288 _LIBCPP_INLINE_VISIBILITY const _Pred& key_eq() const {return __pred_;}
289 _LIBCPP_INLINE_VISIBILITY
290 bool operator()(const _Tp& __x, const _Tp& __y) const
291 {return __pred_(__x.first, __y.first);}
292 _LIBCPP_INLINE_VISIBILITY
293 bool operator()(const typename _Tp::first_type& __x, const _Tp& __y) const
294 {return __pred_(__x, __y.first);}
295 _LIBCPP_INLINE_VISIBILITY
296 bool operator()(const _Tp& __x, const typename _Tp::first_type& __y) const
297 {return __pred_(__x.first, __y);}
298 _LIBCPP_INLINE_VISIBILITY
299 bool operator()(const typename _Tp::first_type& __x,
300 const typename _Tp::first_type& __y) const
301 {return __pred_(__x, __y);}
304 template <class _Alloc>
305 class __hash_map_node_destructor
307 typedef _Alloc allocator_type;
308 typedef std::allocator_traits<allocator_type> __alloc_traits;
309 typedef typename __alloc_traits::value_type::__node_value_type value_type;
311 typedef typename __alloc_traits::pointer pointer;
313 typedef typename value_type::first_type first_type;
314 typedef typename value_type::second_type second_type;
316 allocator_type& __na_;
318 __hash_map_node_destructor& operator=(const __hash_map_node_destructor&);
321 bool __first_constructed;
322 bool __second_constructed;
324 _LIBCPP_INLINE_VISIBILITY
325 explicit __hash_map_node_destructor(allocator_type& __na)
327 __first_constructed(false),
328 __second_constructed(false)
331 #ifndef _LIBCPP_CXX03_LANG
332 _LIBCPP_INLINE_VISIBILITY
333 __hash_map_node_destructor(std::__hash_node_destructor<allocator_type>&& __x)
335 __first_constructed(__x.__value_constructed),
336 __second_constructed(__x.__value_constructed)
338 __x.__value_constructed = false;
340 #else // _LIBCPP_CXX03_LANG
341 _LIBCPP_INLINE_VISIBILITY
342 __hash_map_node_destructor(const std::__hash_node_destructor<allocator_type>& __x)
344 __first_constructed(__x.__value_constructed),
345 __second_constructed(__x.__value_constructed)
347 const_cast<bool&>(__x.__value_constructed) = false;
349 #endif // _LIBCPP_CXX03_LANG
351 _LIBCPP_INLINE_VISIBILITY
352 void operator()(pointer __p)
354 if (__second_constructed)
355 __alloc_traits::destroy(__na_, _VSTD::addressof(__p->__value_.second));
356 if (__first_constructed)
357 __alloc_traits::destroy(__na_, _VSTD::addressof(__p->__value_.first));
359 __alloc_traits::deallocate(__na_, __p, 1);
363 template <class _HashIterator>
364 class _LIBCPP_TEMPLATE_VIS __hash_map_iterator
368 typedef const typename _HashIterator::value_type::first_type key_type;
369 typedef typename _HashIterator::value_type::second_type mapped_type;
371 typedef std::forward_iterator_tag iterator_category;
372 typedef std::pair<key_type, mapped_type> value_type;
373 typedef typename _HashIterator::difference_type difference_type;
374 typedef value_type& reference;
375 typedef typename std::__rebind_pointer<typename _HashIterator::pointer, value_type>::type
378 _LIBCPP_INLINE_VISIBILITY __hash_map_iterator() {}
380 _LIBCPP_INLINE_VISIBILITY __hash_map_iterator(_HashIterator __i) : __i_(__i) {}
382 _LIBCPP_INLINE_VISIBILITY reference operator*() const {return *operator->();}
383 _LIBCPP_INLINE_VISIBILITY pointer operator->() const {return (pointer)__i_.operator->();}
385 _LIBCPP_INLINE_VISIBILITY __hash_map_iterator& operator++() {++__i_; return *this;}
386 _LIBCPP_INLINE_VISIBILITY
387 __hash_map_iterator operator++(int)
389 __hash_map_iterator __t(*this);
394 friend _LIBCPP_INLINE_VISIBILITY
395 bool operator==(const __hash_map_iterator& __x, const __hash_map_iterator& __y)
396 {return __x.__i_ == __y.__i_;}
397 friend _LIBCPP_INLINE_VISIBILITY
398 bool operator!=(const __hash_map_iterator& __x, const __hash_map_iterator& __y)
399 {return __x.__i_ != __y.__i_;}
401 template <class, class, class, class, class> friend class _LIBCPP_TEMPLATE_VIS hash_map;
402 template <class, class, class, class, class> friend class _LIBCPP_TEMPLATE_VIS hash_multimap;
403 template <class> friend class _LIBCPP_TEMPLATE_VIS __hash_const_iterator;
404 template <class> friend class _LIBCPP_TEMPLATE_VIS __hash_const_local_iterator;
405 template <class> friend class _LIBCPP_TEMPLATE_VIS __hash_map_const_iterator;
408 template <class _HashIterator>
409 class _LIBCPP_TEMPLATE_VIS __hash_map_const_iterator
413 typedef const typename _HashIterator::value_type::first_type key_type;
414 typedef typename _HashIterator::value_type::second_type mapped_type;
416 typedef std::forward_iterator_tag iterator_category;
417 typedef std::pair<key_type, mapped_type> value_type;
418 typedef typename _HashIterator::difference_type difference_type;
419 typedef const value_type& reference;
420 typedef typename std::__rebind_pointer<typename _HashIterator::pointer, const value_type>::type
423 _LIBCPP_INLINE_VISIBILITY __hash_map_const_iterator() {}
425 _LIBCPP_INLINE_VISIBILITY
426 __hash_map_const_iterator(_HashIterator __i) : __i_(__i) {}
427 _LIBCPP_INLINE_VISIBILITY
428 __hash_map_const_iterator(
429 __hash_map_iterator<typename _HashIterator::__non_const_iterator> __i)
432 _LIBCPP_INLINE_VISIBILITY
433 reference operator*() const {return *operator->();}
434 _LIBCPP_INLINE_VISIBILITY
435 pointer operator->() const {return (pointer)__i_.operator->();}
437 _LIBCPP_INLINE_VISIBILITY
438 __hash_map_const_iterator& operator++() {++__i_; return *this;}
439 _LIBCPP_INLINE_VISIBILITY
440 __hash_map_const_iterator operator++(int)
442 __hash_map_const_iterator __t(*this);
447 friend _LIBCPP_INLINE_VISIBILITY
448 bool operator==(const __hash_map_const_iterator& __x, const __hash_map_const_iterator& __y)
449 {return __x.__i_ == __y.__i_;}
450 friend _LIBCPP_INLINE_VISIBILITY
451 bool operator!=(const __hash_map_const_iterator& __x, const __hash_map_const_iterator& __y)
452 {return __x.__i_ != __y.__i_;}
454 template <class, class, class, class, class> friend class _LIBCPP_TEMPLATE_VIS hash_map;
455 template <class, class, class, class, class> friend class _LIBCPP_TEMPLATE_VIS hash_multimap;
456 template <class> friend class _LIBCPP_TEMPLATE_VIS __hash_const_iterator;
457 template <class> friend class _LIBCPP_TEMPLATE_VIS __hash_const_local_iterator;
460 template <class _Key, class _Tp, class _Hash = hash<_Key>, class _Pred = std::equal_to<_Key>,
461 class _Alloc = std::allocator<std::pair<const _Key, _Tp> > >
462 class _LIBCPP_TEMPLATE_VIS hash_map
466 typedef _Key key_type;
467 typedef _Tp mapped_type;
468 typedef _Tp data_type;
469 typedef _Hash hasher;
470 typedef _Pred key_equal;
471 typedef _Alloc allocator_type;
472 typedef std::pair<const key_type, mapped_type> value_type;
473 typedef value_type& reference;
474 typedef const value_type& const_reference;
477 typedef std::pair<key_type, mapped_type> __value_type;
478 typedef __hash_map_hasher<__value_type, hasher> __hasher;
479 typedef __hash_map_equal<__value_type, key_equal> __key_equal;
480 typedef typename std::__rebind_alloc_helper<
481 std::allocator_traits<allocator_type>, __value_type>::type __allocator_type;
483 typedef std::__hash_table<__value_type, __hasher,
484 __key_equal, __allocator_type> __table;
488 typedef typename __table::__node_pointer __node_pointer;
489 typedef typename __table::__node_const_pointer __node_const_pointer;
490 typedef typename __table::__node_traits __node_traits;
491 typedef typename __table::__node_allocator __node_allocator;
492 typedef typename __table::__node __node;
493 typedef __hash_map_node_destructor<__node_allocator> _Dp;
494 typedef std::unique_ptr<__node, _Dp> __node_holder;
495 typedef std::allocator_traits<allocator_type> __alloc_traits;
497 typedef typename __alloc_traits::pointer pointer;
498 typedef typename __alloc_traits::const_pointer const_pointer;
499 typedef typename __alloc_traits::size_type size_type;
500 typedef typename __alloc_traits::difference_type difference_type;
502 typedef __hash_map_iterator<typename __table::iterator> iterator;
503 typedef __hash_map_const_iterator<typename __table::const_iterator> const_iterator;
505 _LIBCPP_INLINE_VISIBILITY hash_map() {__table_.rehash(193);}
506 explicit hash_map(size_type __n, const hasher& __hf = hasher(),
507 const key_equal& __eql = key_equal());
508 hash_map(size_type __n, const hasher& __hf,
509 const key_equal& __eql,
510 const allocator_type& __a);
511 template <class _InputIterator>
512 hash_map(_InputIterator __first, _InputIterator __last);
513 template <class _InputIterator>
514 hash_map(_InputIterator __first, _InputIterator __last,
515 size_type __n, const hasher& __hf = hasher(),
516 const key_equal& __eql = key_equal());
517 template <class _InputIterator>
518 hash_map(_InputIterator __first, _InputIterator __last,
519 size_type __n, const hasher& __hf,
520 const key_equal& __eql,
521 const allocator_type& __a);
522 hash_map(const hash_map& __u);
524 _LIBCPP_INLINE_VISIBILITY
525 allocator_type get_allocator() const
526 {return allocator_type(__table_.__node_alloc());}
528 _LIBCPP_INLINE_VISIBILITY
529 bool empty() const {return __table_.size() == 0;}
530 _LIBCPP_INLINE_VISIBILITY
531 size_type size() const {return __table_.size();}
532 _LIBCPP_INLINE_VISIBILITY
533 size_type max_size() const {return __table_.max_size();}
535 _LIBCPP_INLINE_VISIBILITY
536 iterator begin() {return __table_.begin();}
537 _LIBCPP_INLINE_VISIBILITY
538 iterator end() {return __table_.end();}
539 _LIBCPP_INLINE_VISIBILITY
540 const_iterator begin() const {return __table_.begin();}
541 _LIBCPP_INLINE_VISIBILITY
542 const_iterator end() const {return __table_.end();}
544 _LIBCPP_INLINE_VISIBILITY
545 std::pair<iterator, bool> insert(const value_type& __x)
546 {return __table_.__insert_unique(__x);}
547 _LIBCPP_INLINE_VISIBILITY
548 iterator insert(const_iterator, const value_type& __x) {return insert(__x).first;}
549 template <class _InputIterator>
550 _LIBCPP_INLINE_VISIBILITY
551 void insert(_InputIterator __first, _InputIterator __last);
553 _LIBCPP_INLINE_VISIBILITY
554 void erase(const_iterator __p) {__table_.erase(__p.__i_);}
555 _LIBCPP_INLINE_VISIBILITY
556 size_type erase(const key_type& __k) {return __table_.__erase_unique(__k);}
557 _LIBCPP_INLINE_VISIBILITY
558 void erase(const_iterator __first, const_iterator __last)
559 {__table_.erase(__first.__i_, __last.__i_);}
560 _LIBCPP_INLINE_VISIBILITY
561 void clear() {__table_.clear();}
563 _LIBCPP_INLINE_VISIBILITY
564 void swap(hash_map& __u) {__table_.swap(__u.__table_);}
566 _LIBCPP_INLINE_VISIBILITY
567 hasher hash_funct() const
568 {return __table_.hash_function().hash_function();}
569 _LIBCPP_INLINE_VISIBILITY
570 key_equal key_eq() const
571 {return __table_.key_eq().key_eq();}
573 _LIBCPP_INLINE_VISIBILITY
574 iterator find(const key_type& __k) {return __table_.find(__k);}
575 _LIBCPP_INLINE_VISIBILITY
576 const_iterator find(const key_type& __k) const {return __table_.find(__k);}
577 _LIBCPP_INLINE_VISIBILITY
578 size_type count(const key_type& __k) const {return __table_.__count_unique(__k);}
579 _LIBCPP_INLINE_VISIBILITY
580 std::pair<iterator, iterator> equal_range(const key_type& __k)
581 {return __table_.__equal_range_unique(__k);}
582 _LIBCPP_INLINE_VISIBILITY
583 std::pair<const_iterator, const_iterator> equal_range(const key_type& __k) const
584 {return __table_.__equal_range_unique(__k);}
586 mapped_type& operator[](const key_type& __k);
588 _LIBCPP_INLINE_VISIBILITY
589 size_type bucket_count() const {return __table_.bucket_count();}
590 _LIBCPP_INLINE_VISIBILITY
591 size_type max_bucket_count() const {return __table_.max_bucket_count();}
593 _LIBCPP_INLINE_VISIBILITY
594 size_type elems_in_bucket(size_type __n) const
595 {return __table_.bucket_size(__n);}
597 _LIBCPP_INLINE_VISIBILITY
598 void resize(size_type __n) {__table_.rehash(__n);}
601 __node_holder __construct_node(const key_type& __k);
604 template <class _Key, class _Tp, class _Hash, class _Pred, class _Alloc>
605 hash_map<_Key, _Tp, _Hash, _Pred, _Alloc>::hash_map(
606 size_type __n, const hasher& __hf, const key_equal& __eql)
607 : __table_(__hf, __eql)
609 __table_.rehash(__n);
612 template <class _Key, class _Tp, class _Hash, class _Pred, class _Alloc>
613 hash_map<_Key, _Tp, _Hash, _Pred, _Alloc>::hash_map(
614 size_type __n, const hasher& __hf, const key_equal& __eql,
615 const allocator_type& __a)
616 : __table_(__hf, __eql, __a)
618 __table_.rehash(__n);
621 template <class _Key, class _Tp, class _Hash, class _Pred, class _Alloc>
622 template <class _InputIterator>
623 hash_map<_Key, _Tp, _Hash, _Pred, _Alloc>::hash_map(
624 _InputIterator __first, _InputIterator __last)
626 __table_.rehash(193);
627 insert(__first, __last);
630 template <class _Key, class _Tp, class _Hash, class _Pred, class _Alloc>
631 template <class _InputIterator>
632 hash_map<_Key, _Tp, _Hash, _Pred, _Alloc>::hash_map(
633 _InputIterator __first, _InputIterator __last, size_type __n,
634 const hasher& __hf, const key_equal& __eql)
635 : __table_(__hf, __eql)
637 __table_.rehash(__n);
638 insert(__first, __last);
641 template <class _Key, class _Tp, class _Hash, class _Pred, class _Alloc>
642 template <class _InputIterator>
643 hash_map<_Key, _Tp, _Hash, _Pred, _Alloc>::hash_map(
644 _InputIterator __first, _InputIterator __last, size_type __n,
645 const hasher& __hf, const key_equal& __eql, const allocator_type& __a)
646 : __table_(__hf, __eql, __a)
648 __table_.rehash(__n);
649 insert(__first, __last);
652 template <class _Key, class _Tp, class _Hash, class _Pred, class _Alloc>
653 hash_map<_Key, _Tp, _Hash, _Pred, _Alloc>::hash_map(
655 : __table_(__u.__table_)
657 __table_.rehash(__u.bucket_count());
658 insert(__u.begin(), __u.end());
661 template <class _Key, class _Tp, class _Hash, class _Pred, class _Alloc>
662 typename hash_map<_Key, _Tp, _Hash, _Pred, _Alloc>::__node_holder
663 hash_map<_Key, _Tp, _Hash, _Pred, _Alloc>::__construct_node(const key_type& __k)
665 __node_allocator& __na = __table_.__node_alloc();
666 __node_holder __h(__node_traits::allocate(__na, 1), _Dp(__na));
667 __node_traits::construct(__na, _VSTD::addressof(__h->__value_.first), __k);
668 __h.get_deleter().__first_constructed = true;
669 __node_traits::construct(__na, _VSTD::addressof(__h->__value_.second));
670 __h.get_deleter().__second_constructed = true;
671 return _LIBCPP_EXPLICIT_MOVE(__h); // explicitly moved for C++03
674 template <class _Key, class _Tp, class _Hash, class _Pred, class _Alloc>
675 template <class _InputIterator>
678 hash_map<_Key, _Tp, _Hash, _Pred, _Alloc>::insert(_InputIterator __first,
679 _InputIterator __last)
681 for (; __first != __last; ++__first)
682 __table_.__insert_unique(*__first);
685 template <class _Key, class _Tp, class _Hash, class _Pred, class _Alloc>
687 hash_map<_Key, _Tp, _Hash, _Pred, _Alloc>::operator[](const key_type& __k)
689 iterator __i = find(__k);
692 __node_holder __h = __construct_node(__k);
693 std::pair<iterator, bool> __r = __table_.__node_insert_unique(__h.get());
695 return __r.first->second;
698 template <class _Key, class _Tp, class _Hash, class _Pred, class _Alloc>
699 inline _LIBCPP_INLINE_VISIBILITY
701 swap(hash_map<_Key, _Tp, _Hash, _Pred, _Alloc>& __x,
702 hash_map<_Key, _Tp, _Hash, _Pred, _Alloc>& __y)
707 template <class _Key, class _Tp, class _Hash, class _Pred, class _Alloc>
709 operator==(const hash_map<_Key, _Tp, _Hash, _Pred, _Alloc>& __x,
710 const hash_map<_Key, _Tp, _Hash, _Pred, _Alloc>& __y)
712 if (__x.size() != __y.size())
714 typedef typename hash_map<_Key, _Tp, _Hash, _Pred, _Alloc>::const_iterator
716 for (const_iterator __i = __x.begin(), __ex = __x.end(), __ey = __y.end();
719 const_iterator __j = __y.find(__i->first);
720 if (__j == __ey || !(*__i == *__j))
726 template <class _Key, class _Tp, class _Hash, class _Pred, class _Alloc>
727 inline _LIBCPP_INLINE_VISIBILITY
729 operator!=(const hash_map<_Key, _Tp, _Hash, _Pred, _Alloc>& __x,
730 const hash_map<_Key, _Tp, _Hash, _Pred, _Alloc>& __y)
732 return !(__x == __y);
735 template <class _Key, class _Tp, class _Hash = hash<_Key>, class _Pred = std::equal_to<_Key>,
736 class _Alloc = std::allocator<std::pair<const _Key, _Tp> > >
737 class _LIBCPP_TEMPLATE_VIS hash_multimap
741 typedef _Key key_type;
742 typedef _Tp mapped_type;
743 typedef _Tp data_type;
744 typedef _Hash hasher;
745 typedef _Pred key_equal;
746 typedef _Alloc allocator_type;
747 typedef std::pair<const key_type, mapped_type> value_type;
748 typedef value_type& reference;
749 typedef const value_type& const_reference;
752 typedef std::pair<key_type, mapped_type> __value_type;
753 typedef __hash_map_hasher<__value_type, hasher> __hasher;
754 typedef __hash_map_equal<__value_type, key_equal> __key_equal;
755 typedef typename std::__rebind_alloc_helper<std::allocator_traits<allocator_type>, __value_type>::type __allocator_type;
757 typedef std::__hash_table<__value_type, __hasher,
758 __key_equal, __allocator_type> __table;
762 typedef typename __table::__node_traits __node_traits;
763 typedef typename __table::__node_allocator __node_allocator;
764 typedef typename __table::__node __node;
765 typedef __hash_map_node_destructor<__node_allocator> _Dp;
766 typedef std::unique_ptr<__node, _Dp> __node_holder;
767 typedef std::allocator_traits<allocator_type> __alloc_traits;
769 typedef typename __alloc_traits::pointer pointer;
770 typedef typename __alloc_traits::const_pointer const_pointer;
771 typedef typename __alloc_traits::size_type size_type;
772 typedef typename __alloc_traits::difference_type difference_type;
774 typedef __hash_map_iterator<typename __table::iterator> iterator;
775 typedef __hash_map_const_iterator<typename __table::const_iterator> const_iterator;
777 _LIBCPP_INLINE_VISIBILITY
778 hash_multimap() {__table_.rehash(193);}
779 explicit hash_multimap(size_type __n, const hasher& __hf = hasher(),
780 const key_equal& __eql = key_equal());
781 hash_multimap(size_type __n, const hasher& __hf,
782 const key_equal& __eql,
783 const allocator_type& __a);
784 template <class _InputIterator>
785 hash_multimap(_InputIterator __first, _InputIterator __last);
786 template <class _InputIterator>
787 hash_multimap(_InputIterator __first, _InputIterator __last,
788 size_type __n, const hasher& __hf = hasher(),
789 const key_equal& __eql = key_equal());
790 template <class _InputIterator>
791 hash_multimap(_InputIterator __first, _InputIterator __last,
792 size_type __n, const hasher& __hf,
793 const key_equal& __eql,
794 const allocator_type& __a);
795 hash_multimap(const hash_multimap& __u);
797 _LIBCPP_INLINE_VISIBILITY
798 allocator_type get_allocator() const
799 {return allocator_type(__table_.__node_alloc());}
801 _LIBCPP_INLINE_VISIBILITY
802 bool empty() const {return __table_.size() == 0;}
803 _LIBCPP_INLINE_VISIBILITY
804 size_type size() const {return __table_.size();}
805 _LIBCPP_INLINE_VISIBILITY
806 size_type max_size() const {return __table_.max_size();}
808 _LIBCPP_INLINE_VISIBILITY
809 iterator begin() {return __table_.begin();}
810 _LIBCPP_INLINE_VISIBILITY
811 iterator end() {return __table_.end();}
812 _LIBCPP_INLINE_VISIBILITY
813 const_iterator begin() const {return __table_.begin();}
814 _LIBCPP_INLINE_VISIBILITY
815 const_iterator end() const {return __table_.end();}
817 _LIBCPP_INLINE_VISIBILITY
818 iterator insert(const value_type& __x) {return __table_.__insert_multi(__x);}
819 _LIBCPP_INLINE_VISIBILITY
820 iterator insert(const_iterator, const value_type& __x) {return insert(__x);}
821 template <class _InputIterator>
822 _LIBCPP_INLINE_VISIBILITY
823 void insert(_InputIterator __first, _InputIterator __last);
825 _LIBCPP_INLINE_VISIBILITY
826 void erase(const_iterator __p) {__table_.erase(__p.__i_);}
827 _LIBCPP_INLINE_VISIBILITY
828 size_type erase(const key_type& __k) {return __table_.__erase_multi(__k);}
829 _LIBCPP_INLINE_VISIBILITY
830 void erase(const_iterator __first, const_iterator __last)
831 {__table_.erase(__first.__i_, __last.__i_);}
832 _LIBCPP_INLINE_VISIBILITY
833 void clear() {__table_.clear();}
835 _LIBCPP_INLINE_VISIBILITY
836 void swap(hash_multimap& __u) {__table_.swap(__u.__table_);}
838 _LIBCPP_INLINE_VISIBILITY
839 hasher hash_funct() const
840 {return __table_.hash_function().hash_function();}
841 _LIBCPP_INLINE_VISIBILITY
842 key_equal key_eq() const
843 {return __table_.key_eq().key_eq();}
845 _LIBCPP_INLINE_VISIBILITY
846 iterator find(const key_type& __k) {return __table_.find(__k);}
847 _LIBCPP_INLINE_VISIBILITY
848 const_iterator find(const key_type& __k) const {return __table_.find(__k);}
849 _LIBCPP_INLINE_VISIBILITY
850 size_type count(const key_type& __k) const {return __table_.__count_multi(__k);}
851 _LIBCPP_INLINE_VISIBILITY
852 std::pair<iterator, iterator> equal_range(const key_type& __k)
853 {return __table_.__equal_range_multi(__k);}
854 _LIBCPP_INLINE_VISIBILITY
855 std::pair<const_iterator, const_iterator> equal_range(const key_type& __k) const
856 {return __table_.__equal_range_multi(__k);}
858 _LIBCPP_INLINE_VISIBILITY
859 size_type bucket_count() const {return __table_.bucket_count();}
860 _LIBCPP_INLINE_VISIBILITY
861 size_type max_bucket_count() const {return __table_.max_bucket_count();}
863 _LIBCPP_INLINE_VISIBILITY
864 size_type elems_in_bucket(size_type __n) const
865 {return __table_.bucket_size(__n);}
867 _LIBCPP_INLINE_VISIBILITY
868 void resize(size_type __n) {__table_.rehash(__n);}
871 template <class _Key, class _Tp, class _Hash, class _Pred, class _Alloc>
872 hash_multimap<_Key, _Tp, _Hash, _Pred, _Alloc>::hash_multimap(
873 size_type __n, const hasher& __hf, const key_equal& __eql)
874 : __table_(__hf, __eql)
876 __table_.rehash(__n);
879 template <class _Key, class _Tp, class _Hash, class _Pred, class _Alloc>
880 hash_multimap<_Key, _Tp, _Hash, _Pred, _Alloc>::hash_multimap(
881 size_type __n, const hasher& __hf, const key_equal& __eql,
882 const allocator_type& __a)
883 : __table_(__hf, __eql, __a)
885 __table_.rehash(__n);
888 template <class _Key, class _Tp, class _Hash, class _Pred, class _Alloc>
889 template <class _InputIterator>
890 hash_multimap<_Key, _Tp, _Hash, _Pred, _Alloc>::hash_multimap(
891 _InputIterator __first, _InputIterator __last)
893 __table_.rehash(193);
894 insert(__first, __last);
897 template <class _Key, class _Tp, class _Hash, class _Pred, class _Alloc>
898 template <class _InputIterator>
899 hash_multimap<_Key, _Tp, _Hash, _Pred, _Alloc>::hash_multimap(
900 _InputIterator __first, _InputIterator __last, size_type __n,
901 const hasher& __hf, const key_equal& __eql)
902 : __table_(__hf, __eql)
904 __table_.rehash(__n);
905 insert(__first, __last);
908 template <class _Key, class _Tp, class _Hash, class _Pred, class _Alloc>
909 template <class _InputIterator>
910 hash_multimap<_Key, _Tp, _Hash, _Pred, _Alloc>::hash_multimap(
911 _InputIterator __first, _InputIterator __last, size_type __n,
912 const hasher& __hf, const key_equal& __eql, const allocator_type& __a)
913 : __table_(__hf, __eql, __a)
915 __table_.rehash(__n);
916 insert(__first, __last);
919 template <class _Key, class _Tp, class _Hash, class _Pred, class _Alloc>
920 hash_multimap<_Key, _Tp, _Hash, _Pred, _Alloc>::hash_multimap(
921 const hash_multimap& __u)
922 : __table_(__u.__table_)
924 __table_.rehash(__u.bucket_count());
925 insert(__u.begin(), __u.end());
928 template <class _Key, class _Tp, class _Hash, class _Pred, class _Alloc>
929 template <class _InputIterator>
932 hash_multimap<_Key, _Tp, _Hash, _Pred, _Alloc>::insert(_InputIterator __first,
933 _InputIterator __last)
935 for (; __first != __last; ++__first)
936 __table_.__insert_multi(*__first);
939 template <class _Key, class _Tp, class _Hash, class _Pred, class _Alloc>
940 inline _LIBCPP_INLINE_VISIBILITY
942 swap(hash_multimap<_Key, _Tp, _Hash, _Pred, _Alloc>& __x,
943 hash_multimap<_Key, _Tp, _Hash, _Pred, _Alloc>& __y)
948 template <class _Key, class _Tp, class _Hash, class _Pred, class _Alloc>
950 operator==(const hash_multimap<_Key, _Tp, _Hash, _Pred, _Alloc>& __x,
951 const hash_multimap<_Key, _Tp, _Hash, _Pred, _Alloc>& __y)
953 if (__x.size() != __y.size())
955 typedef typename hash_multimap<_Key, _Tp, _Hash, _Pred, _Alloc>::const_iterator
957 typedef std::pair<const_iterator, const_iterator> _EqRng;
958 for (const_iterator __i = __x.begin(), __ex = __x.end(); __i != __ex;)
960 _EqRng __xeq = __x.equal_range(__i->first);
961 _EqRng __yeq = __y.equal_range(__i->first);
962 if (_VSTD::distance(__xeq.first, __xeq.second) !=
963 _VSTD::distance(__yeq.first, __yeq.second) ||
964 !_VSTD::is_permutation(__xeq.first, __xeq.second, __yeq.first))
971 template <class _Key, class _Tp, class _Hash, class _Pred, class _Alloc>
972 inline _LIBCPP_INLINE_VISIBILITY
974 operator!=(const hash_multimap<_Key, _Tp, _Hash, _Pred, _Alloc>& __x,
975 const hash_multimap<_Key, _Tp, _Hash, _Pred, _Alloc>& __y)
977 return !(__x == __y);
982 #endif // _LIBCPP_HASH_MAP