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_;
319 bool __first_constructed;
320 bool __second_constructed;
322 __hash_map_node_destructor(__hash_map_node_destructor const&) = default;
323 __hash_map_node_destructor& operator=(const __hash_map_node_destructor&) = delete;
325 _LIBCPP_INLINE_VISIBILITY
326 explicit __hash_map_node_destructor(allocator_type& __na)
328 __first_constructed(false),
329 __second_constructed(false)
332 #ifndef _LIBCPP_CXX03_LANG
333 _LIBCPP_INLINE_VISIBILITY
334 __hash_map_node_destructor(std::__hash_node_destructor<allocator_type>&& __x)
336 __first_constructed(__x.__value_constructed),
337 __second_constructed(__x.__value_constructed)
339 __x.__value_constructed = false;
341 #else // _LIBCPP_CXX03_LANG
342 _LIBCPP_INLINE_VISIBILITY
343 __hash_map_node_destructor(const std::__hash_node_destructor<allocator_type>& __x)
345 __first_constructed(__x.__value_constructed),
346 __second_constructed(__x.__value_constructed)
348 const_cast<bool&>(__x.__value_constructed) = false;
350 #endif // _LIBCPP_CXX03_LANG
352 _LIBCPP_INLINE_VISIBILITY
353 void operator()(pointer __p)
355 if (__second_constructed)
356 __alloc_traits::destroy(__na_, _VSTD::addressof(__p->__value_.second));
357 if (__first_constructed)
358 __alloc_traits::destroy(__na_, _VSTD::addressof(__p->__value_.first));
360 __alloc_traits::deallocate(__na_, __p, 1);
364 template <class _HashIterator>
365 class _LIBCPP_TEMPLATE_VIS __hash_map_iterator
369 typedef const typename _HashIterator::value_type::first_type key_type;
370 typedef typename _HashIterator::value_type::second_type mapped_type;
372 typedef std::forward_iterator_tag iterator_category;
373 typedef std::pair<key_type, mapped_type> value_type;
374 typedef typename _HashIterator::difference_type difference_type;
375 typedef value_type& reference;
376 typedef typename std::__rebind_pointer<typename _HashIterator::pointer, value_type>::type
379 _LIBCPP_INLINE_VISIBILITY __hash_map_iterator() {}
381 _LIBCPP_INLINE_VISIBILITY __hash_map_iterator(_HashIterator __i) : __i_(__i) {}
383 _LIBCPP_INLINE_VISIBILITY reference operator*() const {return *operator->();}
384 _LIBCPP_INLINE_VISIBILITY pointer operator->() const {return (pointer)__i_.operator->();}
386 _LIBCPP_INLINE_VISIBILITY __hash_map_iterator& operator++() {++__i_; return *this;}
387 _LIBCPP_INLINE_VISIBILITY
388 __hash_map_iterator operator++(int)
390 __hash_map_iterator __t(*this);
395 friend _LIBCPP_INLINE_VISIBILITY
396 bool operator==(const __hash_map_iterator& __x, const __hash_map_iterator& __y)
397 {return __x.__i_ == __y.__i_;}
398 friend _LIBCPP_INLINE_VISIBILITY
399 bool operator!=(const __hash_map_iterator& __x, const __hash_map_iterator& __y)
400 {return __x.__i_ != __y.__i_;}
402 template <class, class, class, class, class> friend class _LIBCPP_TEMPLATE_VIS hash_map;
403 template <class, class, class, class, class> friend class _LIBCPP_TEMPLATE_VIS hash_multimap;
404 template <class> friend class _LIBCPP_TEMPLATE_VIS __hash_const_iterator;
405 template <class> friend class _LIBCPP_TEMPLATE_VIS __hash_const_local_iterator;
406 template <class> friend class _LIBCPP_TEMPLATE_VIS __hash_map_const_iterator;
409 template <class _HashIterator>
410 class _LIBCPP_TEMPLATE_VIS __hash_map_const_iterator
414 typedef const typename _HashIterator::value_type::first_type key_type;
415 typedef typename _HashIterator::value_type::second_type mapped_type;
417 typedef std::forward_iterator_tag iterator_category;
418 typedef std::pair<key_type, mapped_type> value_type;
419 typedef typename _HashIterator::difference_type difference_type;
420 typedef const value_type& reference;
421 typedef typename std::__rebind_pointer<typename _HashIterator::pointer, const value_type>::type
424 _LIBCPP_INLINE_VISIBILITY __hash_map_const_iterator() {}
426 _LIBCPP_INLINE_VISIBILITY
427 __hash_map_const_iterator(_HashIterator __i) : __i_(__i) {}
428 _LIBCPP_INLINE_VISIBILITY
429 __hash_map_const_iterator(
430 __hash_map_iterator<typename _HashIterator::__non_const_iterator> __i)
433 _LIBCPP_INLINE_VISIBILITY
434 reference operator*() const {return *operator->();}
435 _LIBCPP_INLINE_VISIBILITY
436 pointer operator->() const {return (pointer)__i_.operator->();}
438 _LIBCPP_INLINE_VISIBILITY
439 __hash_map_const_iterator& operator++() {++__i_; return *this;}
440 _LIBCPP_INLINE_VISIBILITY
441 __hash_map_const_iterator operator++(int)
443 __hash_map_const_iterator __t(*this);
448 friend _LIBCPP_INLINE_VISIBILITY
449 bool operator==(const __hash_map_const_iterator& __x, const __hash_map_const_iterator& __y)
450 {return __x.__i_ == __y.__i_;}
451 friend _LIBCPP_INLINE_VISIBILITY
452 bool operator!=(const __hash_map_const_iterator& __x, const __hash_map_const_iterator& __y)
453 {return __x.__i_ != __y.__i_;}
455 template <class, class, class, class, class> friend class _LIBCPP_TEMPLATE_VIS hash_map;
456 template <class, class, class, class, class> friend class _LIBCPP_TEMPLATE_VIS hash_multimap;
457 template <class> friend class _LIBCPP_TEMPLATE_VIS __hash_const_iterator;
458 template <class> friend class _LIBCPP_TEMPLATE_VIS __hash_const_local_iterator;
461 template <class _Key, class _Tp, class _Hash = hash<_Key>, class _Pred = std::equal_to<_Key>,
462 class _Alloc = std::allocator<std::pair<const _Key, _Tp> > >
463 class _LIBCPP_TEMPLATE_VIS hash_map
467 typedef _Key key_type;
468 typedef _Tp mapped_type;
469 typedef _Tp data_type;
470 typedef _Hash hasher;
471 typedef _Pred key_equal;
472 typedef _Alloc allocator_type;
473 typedef std::pair<const key_type, mapped_type> value_type;
474 typedef value_type& reference;
475 typedef const value_type& const_reference;
478 typedef std::pair<key_type, mapped_type> __value_type;
479 typedef __hash_map_hasher<__value_type, hasher> __hasher;
480 typedef __hash_map_equal<__value_type, key_equal> __key_equal;
481 typedef typename std::__rebind_alloc_helper<
482 std::allocator_traits<allocator_type>, __value_type>::type __allocator_type;
484 typedef std::__hash_table<__value_type, __hasher,
485 __key_equal, __allocator_type> __table;
489 typedef typename __table::__node_pointer __node_pointer;
490 typedef typename __table::__node_const_pointer __node_const_pointer;
491 typedef typename __table::__node_traits __node_traits;
492 typedef typename __table::__node_allocator __node_allocator;
493 typedef typename __table::__node __node;
494 typedef __hash_map_node_destructor<__node_allocator> _Dp;
495 typedef std::unique_ptr<__node, _Dp> __node_holder;
496 typedef std::allocator_traits<allocator_type> __alloc_traits;
498 typedef typename __alloc_traits::pointer pointer;
499 typedef typename __alloc_traits::const_pointer const_pointer;
500 typedef typename __alloc_traits::size_type size_type;
501 typedef typename __alloc_traits::difference_type difference_type;
503 typedef __hash_map_iterator<typename __table::iterator> iterator;
504 typedef __hash_map_const_iterator<typename __table::const_iterator> const_iterator;
506 _LIBCPP_INLINE_VISIBILITY hash_map() {__table_.rehash(193);}
507 explicit hash_map(size_type __n, const hasher& __hf = hasher(),
508 const key_equal& __eql = key_equal());
509 hash_map(size_type __n, const hasher& __hf,
510 const key_equal& __eql,
511 const allocator_type& __a);
512 template <class _InputIterator>
513 hash_map(_InputIterator __first, _InputIterator __last);
514 template <class _InputIterator>
515 hash_map(_InputIterator __first, _InputIterator __last,
516 size_type __n, const hasher& __hf = hasher(),
517 const key_equal& __eql = key_equal());
518 template <class _InputIterator>
519 hash_map(_InputIterator __first, _InputIterator __last,
520 size_type __n, const hasher& __hf,
521 const key_equal& __eql,
522 const allocator_type& __a);
523 hash_map(const hash_map& __u);
525 _LIBCPP_INLINE_VISIBILITY
526 allocator_type get_allocator() const
527 {return allocator_type(__table_.__node_alloc());}
529 _LIBCPP_INLINE_VISIBILITY
530 bool empty() const {return __table_.size() == 0;}
531 _LIBCPP_INLINE_VISIBILITY
532 size_type size() const {return __table_.size();}
533 _LIBCPP_INLINE_VISIBILITY
534 size_type max_size() const {return __table_.max_size();}
536 _LIBCPP_INLINE_VISIBILITY
537 iterator begin() {return __table_.begin();}
538 _LIBCPP_INLINE_VISIBILITY
539 iterator end() {return __table_.end();}
540 _LIBCPP_INLINE_VISIBILITY
541 const_iterator begin() const {return __table_.begin();}
542 _LIBCPP_INLINE_VISIBILITY
543 const_iterator end() const {return __table_.end();}
545 _LIBCPP_INLINE_VISIBILITY
546 std::pair<iterator, bool> insert(const value_type& __x)
547 {return __table_.__insert_unique(__x);}
548 _LIBCPP_INLINE_VISIBILITY
549 iterator insert(const_iterator, const value_type& __x) {return insert(__x).first;}
550 template <class _InputIterator>
551 _LIBCPP_INLINE_VISIBILITY
552 void insert(_InputIterator __first, _InputIterator __last);
554 _LIBCPP_INLINE_VISIBILITY
555 void erase(const_iterator __p) {__table_.erase(__p.__i_);}
556 _LIBCPP_INLINE_VISIBILITY
557 size_type erase(const key_type& __k) {return __table_.__erase_unique(__k);}
558 _LIBCPP_INLINE_VISIBILITY
559 void erase(const_iterator __first, const_iterator __last)
560 {__table_.erase(__first.__i_, __last.__i_);}
561 _LIBCPP_INLINE_VISIBILITY
562 void clear() {__table_.clear();}
564 _LIBCPP_INLINE_VISIBILITY
565 void swap(hash_map& __u) {__table_.swap(__u.__table_);}
567 _LIBCPP_INLINE_VISIBILITY
568 hasher hash_funct() const
569 {return __table_.hash_function().hash_function();}
570 _LIBCPP_INLINE_VISIBILITY
571 key_equal key_eq() const
572 {return __table_.key_eq().key_eq();}
574 _LIBCPP_INLINE_VISIBILITY
575 iterator find(const key_type& __k) {return __table_.find(__k);}
576 _LIBCPP_INLINE_VISIBILITY
577 const_iterator find(const key_type& __k) const {return __table_.find(__k);}
578 _LIBCPP_INLINE_VISIBILITY
579 size_type count(const key_type& __k) const {return __table_.__count_unique(__k);}
580 _LIBCPP_INLINE_VISIBILITY
581 std::pair<iterator, iterator> equal_range(const key_type& __k)
582 {return __table_.__equal_range_unique(__k);}
583 _LIBCPP_INLINE_VISIBILITY
584 std::pair<const_iterator, const_iterator> equal_range(const key_type& __k) const
585 {return __table_.__equal_range_unique(__k);}
587 mapped_type& operator[](const key_type& __k);
589 _LIBCPP_INLINE_VISIBILITY
590 size_type bucket_count() const {return __table_.bucket_count();}
591 _LIBCPP_INLINE_VISIBILITY
592 size_type max_bucket_count() const {return __table_.max_bucket_count();}
594 _LIBCPP_INLINE_VISIBILITY
595 size_type elems_in_bucket(size_type __n) const
596 {return __table_.bucket_size(__n);}
598 _LIBCPP_INLINE_VISIBILITY
599 void resize(size_type __n) {__table_.rehash(__n);}
602 __node_holder __construct_node(const key_type& __k);
605 template <class _Key, class _Tp, class _Hash, class _Pred, class _Alloc>
606 hash_map<_Key, _Tp, _Hash, _Pred, _Alloc>::hash_map(
607 size_type __n, const hasher& __hf, const key_equal& __eql)
608 : __table_(__hf, __eql)
610 __table_.rehash(__n);
613 template <class _Key, class _Tp, class _Hash, class _Pred, class _Alloc>
614 hash_map<_Key, _Tp, _Hash, _Pred, _Alloc>::hash_map(
615 size_type __n, const hasher& __hf, const key_equal& __eql,
616 const allocator_type& __a)
617 : __table_(__hf, __eql, __a)
619 __table_.rehash(__n);
622 template <class _Key, class _Tp, class _Hash, class _Pred, class _Alloc>
623 template <class _InputIterator>
624 hash_map<_Key, _Tp, _Hash, _Pred, _Alloc>::hash_map(
625 _InputIterator __first, _InputIterator __last)
627 __table_.rehash(193);
628 insert(__first, __last);
631 template <class _Key, class _Tp, class _Hash, class _Pred, class _Alloc>
632 template <class _InputIterator>
633 hash_map<_Key, _Tp, _Hash, _Pred, _Alloc>::hash_map(
634 _InputIterator __first, _InputIterator __last, size_type __n,
635 const hasher& __hf, const key_equal& __eql)
636 : __table_(__hf, __eql)
638 __table_.rehash(__n);
639 insert(__first, __last);
642 template <class _Key, class _Tp, class _Hash, class _Pred, class _Alloc>
643 template <class _InputIterator>
644 hash_map<_Key, _Tp, _Hash, _Pred, _Alloc>::hash_map(
645 _InputIterator __first, _InputIterator __last, size_type __n,
646 const hasher& __hf, const key_equal& __eql, const allocator_type& __a)
647 : __table_(__hf, __eql, __a)
649 __table_.rehash(__n);
650 insert(__first, __last);
653 template <class _Key, class _Tp, class _Hash, class _Pred, class _Alloc>
654 hash_map<_Key, _Tp, _Hash, _Pred, _Alloc>::hash_map(
656 : __table_(__u.__table_)
658 __table_.rehash(__u.bucket_count());
659 insert(__u.begin(), __u.end());
662 template <class _Key, class _Tp, class _Hash, class _Pred, class _Alloc>
663 typename hash_map<_Key, _Tp, _Hash, _Pred, _Alloc>::__node_holder
664 hash_map<_Key, _Tp, _Hash, _Pred, _Alloc>::__construct_node(const key_type& __k)
666 __node_allocator& __na = __table_.__node_alloc();
667 __node_holder __h(__node_traits::allocate(__na, 1), _Dp(__na));
668 __node_traits::construct(__na, _VSTD::addressof(__h->__value_.first), __k);
669 __h.get_deleter().__first_constructed = true;
670 __node_traits::construct(__na, _VSTD::addressof(__h->__value_.second));
671 __h.get_deleter().__second_constructed = true;
672 return _LIBCPP_EXPLICIT_MOVE(__h); // explicitly moved for C++03
675 template <class _Key, class _Tp, class _Hash, class _Pred, class _Alloc>
676 template <class _InputIterator>
679 hash_map<_Key, _Tp, _Hash, _Pred, _Alloc>::insert(_InputIterator __first,
680 _InputIterator __last)
682 for (; __first != __last; ++__first)
683 __table_.__insert_unique(*__first);
686 template <class _Key, class _Tp, class _Hash, class _Pred, class _Alloc>
688 hash_map<_Key, _Tp, _Hash, _Pred, _Alloc>::operator[](const key_type& __k)
690 iterator __i = find(__k);
693 __node_holder __h = __construct_node(__k);
694 std::pair<iterator, bool> __r = __table_.__node_insert_unique(__h.get());
696 return __r.first->second;
699 template <class _Key, class _Tp, class _Hash, class _Pred, class _Alloc>
700 inline _LIBCPP_INLINE_VISIBILITY
702 swap(hash_map<_Key, _Tp, _Hash, _Pred, _Alloc>& __x,
703 hash_map<_Key, _Tp, _Hash, _Pred, _Alloc>& __y)
708 template <class _Key, class _Tp, class _Hash, class _Pred, class _Alloc>
710 operator==(const hash_map<_Key, _Tp, _Hash, _Pred, _Alloc>& __x,
711 const hash_map<_Key, _Tp, _Hash, _Pred, _Alloc>& __y)
713 if (__x.size() != __y.size())
715 typedef typename hash_map<_Key, _Tp, _Hash, _Pred, _Alloc>::const_iterator
717 for (const_iterator __i = __x.begin(), __ex = __x.end(), __ey = __y.end();
720 const_iterator __j = __y.find(__i->first);
721 if (__j == __ey || !(*__i == *__j))
727 template <class _Key, class _Tp, class _Hash, class _Pred, class _Alloc>
728 inline _LIBCPP_INLINE_VISIBILITY
730 operator!=(const hash_map<_Key, _Tp, _Hash, _Pred, _Alloc>& __x,
731 const hash_map<_Key, _Tp, _Hash, _Pred, _Alloc>& __y)
733 return !(__x == __y);
736 template <class _Key, class _Tp, class _Hash = hash<_Key>, class _Pred = std::equal_to<_Key>,
737 class _Alloc = std::allocator<std::pair<const _Key, _Tp> > >
738 class _LIBCPP_TEMPLATE_VIS hash_multimap
742 typedef _Key key_type;
743 typedef _Tp mapped_type;
744 typedef _Tp data_type;
745 typedef _Hash hasher;
746 typedef _Pred key_equal;
747 typedef _Alloc allocator_type;
748 typedef std::pair<const key_type, mapped_type> value_type;
749 typedef value_type& reference;
750 typedef const value_type& const_reference;
753 typedef std::pair<key_type, mapped_type> __value_type;
754 typedef __hash_map_hasher<__value_type, hasher> __hasher;
755 typedef __hash_map_equal<__value_type, key_equal> __key_equal;
756 typedef typename std::__rebind_alloc_helper<std::allocator_traits<allocator_type>, __value_type>::type __allocator_type;
758 typedef std::__hash_table<__value_type, __hasher,
759 __key_equal, __allocator_type> __table;
763 typedef typename __table::__node_traits __node_traits;
764 typedef typename __table::__node_allocator __node_allocator;
765 typedef typename __table::__node __node;
766 typedef __hash_map_node_destructor<__node_allocator> _Dp;
767 typedef std::unique_ptr<__node, _Dp> __node_holder;
768 typedef std::allocator_traits<allocator_type> __alloc_traits;
770 typedef typename __alloc_traits::pointer pointer;
771 typedef typename __alloc_traits::const_pointer const_pointer;
772 typedef typename __alloc_traits::size_type size_type;
773 typedef typename __alloc_traits::difference_type difference_type;
775 typedef __hash_map_iterator<typename __table::iterator> iterator;
776 typedef __hash_map_const_iterator<typename __table::const_iterator> const_iterator;
778 _LIBCPP_INLINE_VISIBILITY
779 hash_multimap() {__table_.rehash(193);}
780 explicit hash_multimap(size_type __n, const hasher& __hf = hasher(),
781 const key_equal& __eql = key_equal());
782 hash_multimap(size_type __n, const hasher& __hf,
783 const key_equal& __eql,
784 const allocator_type& __a);
785 template <class _InputIterator>
786 hash_multimap(_InputIterator __first, _InputIterator __last);
787 template <class _InputIterator>
788 hash_multimap(_InputIterator __first, _InputIterator __last,
789 size_type __n, const hasher& __hf = hasher(),
790 const key_equal& __eql = key_equal());
791 template <class _InputIterator>
792 hash_multimap(_InputIterator __first, _InputIterator __last,
793 size_type __n, const hasher& __hf,
794 const key_equal& __eql,
795 const allocator_type& __a);
796 hash_multimap(const hash_multimap& __u);
798 _LIBCPP_INLINE_VISIBILITY
799 allocator_type get_allocator() const
800 {return allocator_type(__table_.__node_alloc());}
802 _LIBCPP_INLINE_VISIBILITY
803 bool empty() const {return __table_.size() == 0;}
804 _LIBCPP_INLINE_VISIBILITY
805 size_type size() const {return __table_.size();}
806 _LIBCPP_INLINE_VISIBILITY
807 size_type max_size() const {return __table_.max_size();}
809 _LIBCPP_INLINE_VISIBILITY
810 iterator begin() {return __table_.begin();}
811 _LIBCPP_INLINE_VISIBILITY
812 iterator end() {return __table_.end();}
813 _LIBCPP_INLINE_VISIBILITY
814 const_iterator begin() const {return __table_.begin();}
815 _LIBCPP_INLINE_VISIBILITY
816 const_iterator end() const {return __table_.end();}
818 _LIBCPP_INLINE_VISIBILITY
819 iterator insert(const value_type& __x) {return __table_.__insert_multi(__x);}
820 _LIBCPP_INLINE_VISIBILITY
821 iterator insert(const_iterator, const value_type& __x) {return insert(__x);}
822 template <class _InputIterator>
823 _LIBCPP_INLINE_VISIBILITY
824 void insert(_InputIterator __first, _InputIterator __last);
826 _LIBCPP_INLINE_VISIBILITY
827 void erase(const_iterator __p) {__table_.erase(__p.__i_);}
828 _LIBCPP_INLINE_VISIBILITY
829 size_type erase(const key_type& __k) {return __table_.__erase_multi(__k);}
830 _LIBCPP_INLINE_VISIBILITY
831 void erase(const_iterator __first, const_iterator __last)
832 {__table_.erase(__first.__i_, __last.__i_);}
833 _LIBCPP_INLINE_VISIBILITY
834 void clear() {__table_.clear();}
836 _LIBCPP_INLINE_VISIBILITY
837 void swap(hash_multimap& __u) {__table_.swap(__u.__table_);}
839 _LIBCPP_INLINE_VISIBILITY
840 hasher hash_funct() const
841 {return __table_.hash_function().hash_function();}
842 _LIBCPP_INLINE_VISIBILITY
843 key_equal key_eq() const
844 {return __table_.key_eq().key_eq();}
846 _LIBCPP_INLINE_VISIBILITY
847 iterator find(const key_type& __k) {return __table_.find(__k);}
848 _LIBCPP_INLINE_VISIBILITY
849 const_iterator find(const key_type& __k) const {return __table_.find(__k);}
850 _LIBCPP_INLINE_VISIBILITY
851 size_type count(const key_type& __k) const {return __table_.__count_multi(__k);}
852 _LIBCPP_INLINE_VISIBILITY
853 std::pair<iterator, iterator> equal_range(const key_type& __k)
854 {return __table_.__equal_range_multi(__k);}
855 _LIBCPP_INLINE_VISIBILITY
856 std::pair<const_iterator, const_iterator> equal_range(const key_type& __k) const
857 {return __table_.__equal_range_multi(__k);}
859 _LIBCPP_INLINE_VISIBILITY
860 size_type bucket_count() const {return __table_.bucket_count();}
861 _LIBCPP_INLINE_VISIBILITY
862 size_type max_bucket_count() const {return __table_.max_bucket_count();}
864 _LIBCPP_INLINE_VISIBILITY
865 size_type elems_in_bucket(size_type __n) const
866 {return __table_.bucket_size(__n);}
868 _LIBCPP_INLINE_VISIBILITY
869 void resize(size_type __n) {__table_.rehash(__n);}
872 template <class _Key, class _Tp, class _Hash, class _Pred, class _Alloc>
873 hash_multimap<_Key, _Tp, _Hash, _Pred, _Alloc>::hash_multimap(
874 size_type __n, const hasher& __hf, const key_equal& __eql)
875 : __table_(__hf, __eql)
877 __table_.rehash(__n);
880 template <class _Key, class _Tp, class _Hash, class _Pred, class _Alloc>
881 hash_multimap<_Key, _Tp, _Hash, _Pred, _Alloc>::hash_multimap(
882 size_type __n, const hasher& __hf, const key_equal& __eql,
883 const allocator_type& __a)
884 : __table_(__hf, __eql, __a)
886 __table_.rehash(__n);
889 template <class _Key, class _Tp, class _Hash, class _Pred, class _Alloc>
890 template <class _InputIterator>
891 hash_multimap<_Key, _Tp, _Hash, _Pred, _Alloc>::hash_multimap(
892 _InputIterator __first, _InputIterator __last)
894 __table_.rehash(193);
895 insert(__first, __last);
898 template <class _Key, class _Tp, class _Hash, class _Pred, class _Alloc>
899 template <class _InputIterator>
900 hash_multimap<_Key, _Tp, _Hash, _Pred, _Alloc>::hash_multimap(
901 _InputIterator __first, _InputIterator __last, size_type __n,
902 const hasher& __hf, const key_equal& __eql)
903 : __table_(__hf, __eql)
905 __table_.rehash(__n);
906 insert(__first, __last);
909 template <class _Key, class _Tp, class _Hash, class _Pred, class _Alloc>
910 template <class _InputIterator>
911 hash_multimap<_Key, _Tp, _Hash, _Pred, _Alloc>::hash_multimap(
912 _InputIterator __first, _InputIterator __last, size_type __n,
913 const hasher& __hf, const key_equal& __eql, const allocator_type& __a)
914 : __table_(__hf, __eql, __a)
916 __table_.rehash(__n);
917 insert(__first, __last);
920 template <class _Key, class _Tp, class _Hash, class _Pred, class _Alloc>
921 hash_multimap<_Key, _Tp, _Hash, _Pred, _Alloc>::hash_multimap(
922 const hash_multimap& __u)
923 : __table_(__u.__table_)
925 __table_.rehash(__u.bucket_count());
926 insert(__u.begin(), __u.end());
929 template <class _Key, class _Tp, class _Hash, class _Pred, class _Alloc>
930 template <class _InputIterator>
933 hash_multimap<_Key, _Tp, _Hash, _Pred, _Alloc>::insert(_InputIterator __first,
934 _InputIterator __last)
936 for (; __first != __last; ++__first)
937 __table_.__insert_multi(*__first);
940 template <class _Key, class _Tp, class _Hash, class _Pred, class _Alloc>
941 inline _LIBCPP_INLINE_VISIBILITY
943 swap(hash_multimap<_Key, _Tp, _Hash, _Pred, _Alloc>& __x,
944 hash_multimap<_Key, _Tp, _Hash, _Pred, _Alloc>& __y)
949 template <class _Key, class _Tp, class _Hash, class _Pred, class _Alloc>
951 operator==(const hash_multimap<_Key, _Tp, _Hash, _Pred, _Alloc>& __x,
952 const hash_multimap<_Key, _Tp, _Hash, _Pred, _Alloc>& __y)
954 if (__x.size() != __y.size())
956 typedef typename hash_multimap<_Key, _Tp, _Hash, _Pred, _Alloc>::const_iterator
958 typedef std::pair<const_iterator, const_iterator> _EqRng;
959 for (const_iterator __i = __x.begin(), __ex = __x.end(); __i != __ex;)
961 _EqRng __xeq = __x.equal_range(__i->first);
962 _EqRng __yeq = __y.equal_range(__i->first);
963 if (_VSTD::distance(__xeq.first, __xeq.second) !=
964 _VSTD::distance(__yeq.first, __yeq.second) ||
965 !_VSTD::is_permutation(__xeq.first, __xeq.second, __yeq.first))
972 template <class _Key, class _Tp, class _Hash, class _Pred, class _Alloc>
973 inline _LIBCPP_INLINE_VISIBILITY
975 operator!=(const hash_multimap<_Key, _Tp, _Hash, _Pred, _Alloc>& __x,
976 const hash_multimap<_Key, _Tp, _Hash, _Pred, _Alloc>& __y)
978 return !(__x == __y);
983 #endif // _LIBCPP_HASH_MAP