2 //===-------------------------- hash_map ----------------------------------===//
4 // The LLVM Compiler Infrastructure
6 // This file is dual licensed under the MIT and the University of Illinois Open
7 // Source Licenses. See LICENSE.TXT for details.
9 //===----------------------------------------------------------------------===//
11 #ifndef _LIBCPP_HASH_MAP
12 #define _LIBCPP_HASH_MAP
21 template <class Key, class T, class Hash = hash<Key>, class Pred = equal_to<Key>,
22 class Alloc = allocator<pair<const Key, T>>>
28 typedef T mapped_type;
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;
40 typedef /unspecified/ iterator;
41 typedef /unspecified/ const_iterator;
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&);
53 hash_map& operator=(const hash_map&);
55 allocator_type get_allocator() const;
58 size_type size() const;
59 size_type max_size() const;
63 const_iterator begin() const;
64 const_iterator end() const;
66 pair<iterator, bool> insert(const value_type& obj);
67 template <class InputIterator>
68 void insert(InputIterator first, InputIterator last);
70 void erase(const_iterator position);
71 size_type erase(const key_type& k);
72 void erase(const_iterator first, const_iterator last);
77 hasher hash_funct() const;
78 key_equal key_eq() const;
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;
86 mapped_type& operator[](const key_type& k);
88 size_type bucket_count() const;
89 size_type max_bucket_count() const;
91 size_type elems_in_bucket(size_type n) const;
93 void resize(size_type n);
96 template <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);
100 template <class Key, class T, class Hash, class Pred, class Alloc>
102 operator==(const hash_map<Key, T, Hash, Pred, Alloc>& x,
103 const hash_map<Key, T, Hash, Pred, Alloc>& y);
105 template <class Key, class T, class Hash, class Pred, class Alloc>
107 operator!=(const hash_map<Key, T, Hash, Pred, Alloc>& x,
108 const hash_map<Key, T, Hash, Pred, Alloc>& y);
110 template <class Key, class T, class Hash = hash<Key>, class Pred = equal_to<Key>,
111 class Alloc = allocator<pair<const Key, T>>>
116 typedef Key key_type;
117 typedef T mapped_type;
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;
129 typedef /unspecified/ iterator;
130 typedef /unspecified/ const_iterator;
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&);
143 hash_multimap& operator=(const hash_multimap&);
145 allocator_type get_allocator() const;
148 size_type size() const;
149 size_type max_size() const;
153 const_iterator begin() const;
154 const_iterator end() const;
156 iterator insert(const value_type& obj);
157 template <class InputIterator>
158 void insert(InputIterator first, InputIterator last);
160 void erase(const_iterator position);
161 size_type erase(const key_type& k);
162 void erase(const_iterator first, const_iterator last);
165 void swap(hash_multimap&);
167 hasher hash_funct() const;
168 key_equal key_eq() const;
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;
176 size_type bucket_count() const;
177 size_type max_bucket_count() const;
179 size_type elems_in_bucket(size_type n) const;
181 void resize(size_type n);
184 template <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);
188 template <class Key, class T, class Hash, class Pred, class Alloc>
190 operator==(const hash_multimap<Key, T, Hash, Pred, Alloc>& x,
191 const hash_multimap<Key, T, Hash, Pred, Alloc>& y);
193 template <class Key, class T, class Hash, class Pred, class Alloc>
195 operator!=(const hash_multimap<Key, T, Hash, Pred, Alloc>& x,
196 const hash_multimap<Key, T, Hash, Pred, Alloc>& y);
203 #include <__hash_table>
204 #include <functional>
206 #include <ext/__hash>
209 #warning Use of the header <ext/hash_map> is deprecated. Migrate to <unordered_map>
212 #pragma GCC system_header
214 namespace __gnu_cxx {
218 template <class _Tp, class _Hash, bool = is_empty<_Hash>::value
219 #if __has_feature(is_final)
220 && !__is_final(_Hash)
223 class __hash_map_hasher
227 _LIBCPP_INLINE_VISIBILITY __hash_map_hasher() : _Hash() {}
228 _LIBCPP_INLINE_VISIBILITY __hash_map_hasher(const _Hash& __h) : _Hash(__h) {}
229 _LIBCPP_INLINE_VISIBILITY const _Hash& hash_function() const {return *this;}
230 _LIBCPP_INLINE_VISIBILITY
231 size_t operator()(const _Tp& __x) const
232 {return static_cast<const _Hash&>(*this)(__x.first);}
233 _LIBCPP_INLINE_VISIBILITY
234 size_t operator()(const typename _Tp::first_type& __x) const
235 {return static_cast<const _Hash&>(*this)(__x);}
238 template <class _Tp, class _Hash>
239 class __hash_map_hasher<_Tp, _Hash, false>
243 _LIBCPP_INLINE_VISIBILITY __hash_map_hasher() : __hash_() {}
244 _LIBCPP_INLINE_VISIBILITY __hash_map_hasher(const _Hash& __h) : __hash_(__h) {}
245 _LIBCPP_INLINE_VISIBILITY const _Hash& hash_function() const {return __hash_;}
246 _LIBCPP_INLINE_VISIBILITY
247 size_t operator()(const _Tp& __x) const
248 {return __hash_(__x.first);}
249 _LIBCPP_INLINE_VISIBILITY
250 size_t operator()(const typename _Tp::first_type& __x) const
251 {return __hash_(__x);}
254 template <class _Tp, class _Pred, bool = is_empty<_Pred>::value
255 #if __has_feature(is_final)
256 && !__is_final(_Pred)
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 allocator_traits<allocator_type> __alloc_traits;
309 typedef typename __alloc_traits::value_type::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_HAS_NO_RVALUE_REFERENCES
332 _LIBCPP_INLINE_VISIBILITY
333 __hash_map_node_destructor(__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_HAS_NO_RVALUE_REFERENCES
341 _LIBCPP_INLINE_VISIBILITY
342 __hash_map_node_destructor(const __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_HAS_NO_RVALUE_REFERENCES
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_TYPE_VIS __hash_map_iterator
368 typedef pointer_traits<typename _HashIterator::pointer> __pointer_traits;
369 typedef const typename _HashIterator::value_type::first_type key_type;
370 typedef typename _HashIterator::value_type::second_type mapped_type;
372 typedef forward_iterator_tag iterator_category;
373 typedef pair<key_type, mapped_type> value_type;
374 typedef typename _HashIterator::difference_type difference_type;
375 typedef value_type& reference;
376 typedef typename __pointer_traits::template
377 #ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
380 rebind<value_type>::other
384 _LIBCPP_INLINE_VISIBILITY __hash_map_iterator() {}
386 _LIBCPP_INLINE_VISIBILITY __hash_map_iterator(_HashIterator __i) : __i_(__i) {}
388 _LIBCPP_INLINE_VISIBILITY reference operator*() const {return *operator->();}
389 _LIBCPP_INLINE_VISIBILITY pointer operator->() const {return (pointer)__i_.operator->();}
391 _LIBCPP_INLINE_VISIBILITY __hash_map_iterator& operator++() {++__i_; return *this;}
392 _LIBCPP_INLINE_VISIBILITY
393 __hash_map_iterator operator++(int)
395 __hash_map_iterator __t(*this);
400 friend _LIBCPP_INLINE_VISIBILITY
401 bool operator==(const __hash_map_iterator& __x, const __hash_map_iterator& __y)
402 {return __x.__i_ == __y.__i_;}
403 friend _LIBCPP_INLINE_VISIBILITY
404 bool operator!=(const __hash_map_iterator& __x, const __hash_map_iterator& __y)
405 {return __x.__i_ != __y.__i_;}
407 template <class, class, class, class, class> friend class _LIBCPP_TYPE_VIS hash_map;
408 template <class, class, class, class, class> friend class _LIBCPP_TYPE_VIS hash_multimap;
409 template <class> friend class _LIBCPP_TYPE_VIS __hash_const_iterator;
410 template <class> friend class _LIBCPP_TYPE_VIS __hash_const_local_iterator;
411 template <class> friend class _LIBCPP_TYPE_VIS __hash_map_const_iterator;
414 template <class _HashIterator>
415 class _LIBCPP_TYPE_VIS __hash_map_const_iterator
419 typedef pointer_traits<typename _HashIterator::pointer> __pointer_traits;
420 typedef const typename _HashIterator::value_type::first_type key_type;
421 typedef typename _HashIterator::value_type::second_type mapped_type;
423 typedef forward_iterator_tag iterator_category;
424 typedef pair<key_type, mapped_type> value_type;
425 typedef typename _HashIterator::difference_type difference_type;
426 typedef const value_type& reference;
427 typedef typename __pointer_traits::template
428 #ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
431 rebind<value_type>::other
435 _LIBCPP_INLINE_VISIBILITY __hash_map_const_iterator() {}
437 _LIBCPP_INLINE_VISIBILITY
438 __hash_map_const_iterator(_HashIterator __i) : __i_(__i) {}
439 _LIBCPP_INLINE_VISIBILITY
440 __hash_map_const_iterator(
441 __hash_map_iterator<typename _HashIterator::__non_const_iterator> __i)
444 _LIBCPP_INLINE_VISIBILITY
445 reference operator*() const {return *operator->();}
446 _LIBCPP_INLINE_VISIBILITY
447 pointer operator->() const {return (pointer)__i_.operator->();}
449 _LIBCPP_INLINE_VISIBILITY
450 __hash_map_const_iterator& operator++() {++__i_; return *this;}
451 _LIBCPP_INLINE_VISIBILITY
452 __hash_map_const_iterator operator++(int)
454 __hash_map_const_iterator __t(*this);
459 friend _LIBCPP_INLINE_VISIBILITY
460 bool operator==(const __hash_map_const_iterator& __x, const __hash_map_const_iterator& __y)
461 {return __x.__i_ == __y.__i_;}
462 friend _LIBCPP_INLINE_VISIBILITY
463 bool operator!=(const __hash_map_const_iterator& __x, const __hash_map_const_iterator& __y)
464 {return __x.__i_ != __y.__i_;}
466 template <class, class, class, class, class> friend class _LIBCPP_TYPE_VIS hash_map;
467 template <class, class, class, class, class> friend class _LIBCPP_TYPE_VIS hash_multimap;
468 template <class> friend class _LIBCPP_TYPE_VIS __hash_const_iterator;
469 template <class> friend class _LIBCPP_TYPE_VIS __hash_const_local_iterator;
472 template <class _Key, class _Tp, class _Hash = hash<_Key>, class _Pred = equal_to<_Key>,
473 class _Alloc = allocator<pair<const _Key, _Tp> > >
474 class _LIBCPP_TYPE_VIS hash_map
478 typedef _Key key_type;
479 typedef _Tp mapped_type;
480 typedef _Tp data_type;
481 typedef _Hash hasher;
482 typedef _Pred key_equal;
483 typedef _Alloc allocator_type;
484 typedef pair<const key_type, mapped_type> value_type;
485 typedef value_type& reference;
486 typedef const value_type& const_reference;
489 typedef pair<key_type, mapped_type> __value_type;
490 typedef __hash_map_hasher<__value_type, hasher> __hasher;
491 typedef __hash_map_equal<__value_type, key_equal> __key_equal;
492 typedef typename allocator_traits<allocator_type>::template
493 #ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
494 rebind_alloc<__value_type>
496 rebind_alloc<__value_type>::other
500 typedef __hash_table<__value_type, __hasher,
501 __key_equal, __allocator_type> __table;
505 typedef typename __table::__node_pointer __node_pointer;
506 typedef typename __table::__node_const_pointer __node_const_pointer;
507 typedef typename __table::__node_traits __node_traits;
508 typedef typename __table::__node_allocator __node_allocator;
509 typedef typename __table::__node __node;
510 typedef __hash_map_node_destructor<__node_allocator> _Dp;
511 typedef unique_ptr<__node, _Dp> __node_holder;
512 typedef allocator_traits<allocator_type> __alloc_traits;
514 typedef typename __alloc_traits::pointer pointer;
515 typedef typename __alloc_traits::const_pointer const_pointer;
516 typedef typename __alloc_traits::size_type size_type;
517 typedef typename __alloc_traits::difference_type difference_type;
519 typedef __hash_map_iterator<typename __table::iterator> iterator;
520 typedef __hash_map_const_iterator<typename __table::const_iterator> const_iterator;
522 _LIBCPP_INLINE_VISIBILITY hash_map() {__table_.rehash(193);}
523 explicit hash_map(size_type __n, const hasher& __hf = hasher(),
524 const key_equal& __eql = key_equal());
525 hash_map(size_type __n, const hasher& __hf,
526 const key_equal& __eql,
527 const allocator_type& __a);
528 template <class _InputIterator>
529 hash_map(_InputIterator __first, _InputIterator __last);
530 template <class _InputIterator>
531 hash_map(_InputIterator __first, _InputIterator __last,
532 size_type __n, const hasher& __hf = hasher(),
533 const key_equal& __eql = key_equal());
534 template <class _InputIterator>
535 hash_map(_InputIterator __first, _InputIterator __last,
536 size_type __n, const hasher& __hf,
537 const key_equal& __eql,
538 const allocator_type& __a);
539 hash_map(const hash_map& __u);
541 _LIBCPP_INLINE_VISIBILITY
542 allocator_type get_allocator() const
543 {return allocator_type(__table_.__node_alloc());}
545 _LIBCPP_INLINE_VISIBILITY
546 bool empty() const {return __table_.size() == 0;}
547 _LIBCPP_INLINE_VISIBILITY
548 size_type size() const {return __table_.size();}
549 _LIBCPP_INLINE_VISIBILITY
550 size_type max_size() const {return __table_.max_size();}
552 _LIBCPP_INLINE_VISIBILITY
553 iterator begin() {return __table_.begin();}
554 _LIBCPP_INLINE_VISIBILITY
555 iterator end() {return __table_.end();}
556 _LIBCPP_INLINE_VISIBILITY
557 const_iterator begin() const {return __table_.begin();}
558 _LIBCPP_INLINE_VISIBILITY
559 const_iterator end() const {return __table_.end();}
561 _LIBCPP_INLINE_VISIBILITY
562 pair<iterator, bool> insert(const value_type& __x)
563 {return __table_.__insert_unique(__x);}
564 _LIBCPP_INLINE_VISIBILITY
565 iterator insert(const_iterator, const value_type& __x) {return insert(__x).first;}
566 template <class _InputIterator>
567 void insert(_InputIterator __first, _InputIterator __last);
569 _LIBCPP_INLINE_VISIBILITY
570 void erase(const_iterator __p) {__table_.erase(__p.__i_);}
571 _LIBCPP_INLINE_VISIBILITY
572 size_type erase(const key_type& __k) {return __table_.__erase_unique(__k);}
573 _LIBCPP_INLINE_VISIBILITY
574 void erase(const_iterator __first, const_iterator __last)
575 {__table_.erase(__first.__i_, __last.__i_);}
576 _LIBCPP_INLINE_VISIBILITY
577 void clear() {__table_.clear();}
579 _LIBCPP_INLINE_VISIBILITY
580 void swap(hash_map& __u) {__table_.swap(__u.__table_);}
582 _LIBCPP_INLINE_VISIBILITY
583 hasher hash_funct() const
584 {return __table_.hash_function().hash_function();}
585 _LIBCPP_INLINE_VISIBILITY
586 key_equal key_eq() const
587 {return __table_.key_eq().key_eq();}
589 _LIBCPP_INLINE_VISIBILITY
590 iterator find(const key_type& __k) {return __table_.find(__k);}
591 _LIBCPP_INLINE_VISIBILITY
592 const_iterator find(const key_type& __k) const {return __table_.find(__k);}
593 _LIBCPP_INLINE_VISIBILITY
594 size_type count(const key_type& __k) const {return __table_.__count_unique(__k);}
595 _LIBCPP_INLINE_VISIBILITY
596 pair<iterator, iterator> equal_range(const key_type& __k)
597 {return __table_.__equal_range_unique(__k);}
598 _LIBCPP_INLINE_VISIBILITY
599 pair<const_iterator, const_iterator> equal_range(const key_type& __k) const
600 {return __table_.__equal_range_unique(__k);}
602 mapped_type& operator[](const key_type& __k);
604 _LIBCPP_INLINE_VISIBILITY
605 size_type bucket_count() const {return __table_.bucket_count();}
606 _LIBCPP_INLINE_VISIBILITY
607 size_type max_bucket_count() const {return __table_.max_bucket_count();}
609 _LIBCPP_INLINE_VISIBILITY
610 size_type elems_in_bucket(size_type __n) const
611 {return __table_.bucket_size(__n);}
613 _LIBCPP_INLINE_VISIBILITY
614 void resize(size_type __n) {__table_.rehash(__n);}
617 __node_holder __construct_node(const key_type& __k);
620 template <class _Key, class _Tp, class _Hash, class _Pred, class _Alloc>
621 hash_map<_Key, _Tp, _Hash, _Pred, _Alloc>::hash_map(
622 size_type __n, const hasher& __hf, const key_equal& __eql)
623 : __table_(__hf, __eql)
625 __table_.rehash(__n);
628 template <class _Key, class _Tp, class _Hash, class _Pred, class _Alloc>
629 hash_map<_Key, _Tp, _Hash, _Pred, _Alloc>::hash_map(
630 size_type __n, const hasher& __hf, const key_equal& __eql,
631 const allocator_type& __a)
632 : __table_(__hf, __eql, __a)
634 __table_.rehash(__n);
637 template <class _Key, class _Tp, class _Hash, class _Pred, class _Alloc>
638 template <class _InputIterator>
639 hash_map<_Key, _Tp, _Hash, _Pred, _Alloc>::hash_map(
640 _InputIterator __first, _InputIterator __last)
642 __table_.rehash(193);
643 insert(__first, __last);
646 template <class _Key, class _Tp, class _Hash, class _Pred, class _Alloc>
647 template <class _InputIterator>
648 hash_map<_Key, _Tp, _Hash, _Pred, _Alloc>::hash_map(
649 _InputIterator __first, _InputIterator __last, size_type __n,
650 const hasher& __hf, const key_equal& __eql)
651 : __table_(__hf, __eql)
653 __table_.rehash(__n);
654 insert(__first, __last);
657 template <class _Key, class _Tp, class _Hash, class _Pred, class _Alloc>
658 template <class _InputIterator>
659 hash_map<_Key, _Tp, _Hash, _Pred, _Alloc>::hash_map(
660 _InputIterator __first, _InputIterator __last, size_type __n,
661 const hasher& __hf, const key_equal& __eql, const allocator_type& __a)
662 : __table_(__hf, __eql, __a)
664 __table_.rehash(__n);
665 insert(__first, __last);
668 template <class _Key, class _Tp, class _Hash, class _Pred, class _Alloc>
669 hash_map<_Key, _Tp, _Hash, _Pred, _Alloc>::hash_map(
671 : __table_(__u.__table_)
673 __table_.rehash(__u.bucket_count());
674 insert(__u.begin(), __u.end());
677 template <class _Key, class _Tp, class _Hash, class _Pred, class _Alloc>
678 typename hash_map<_Key, _Tp, _Hash, _Pred, _Alloc>::__node_holder
679 hash_map<_Key, _Tp, _Hash, _Pred, _Alloc>::__construct_node(const key_type& __k)
681 __node_allocator& __na = __table_.__node_alloc();
682 __node_holder __h(__node_traits::allocate(__na, 1), _Dp(__na));
683 __node_traits::construct(__na, _VSTD::addressof(__h->__value_.first), __k);
684 __h.get_deleter().__first_constructed = true;
685 __node_traits::construct(__na, _VSTD::addressof(__h->__value_.second));
686 __h.get_deleter().__second_constructed = true;
687 return _VSTD::move(__h);
690 template <class _Key, class _Tp, class _Hash, class _Pred, class _Alloc>
691 template <class _InputIterator>
692 inline _LIBCPP_INLINE_VISIBILITY
694 hash_map<_Key, _Tp, _Hash, _Pred, _Alloc>::insert(_InputIterator __first,
695 _InputIterator __last)
697 for (; __first != __last; ++__first)
698 __table_.__insert_unique(*__first);
701 template <class _Key, class _Tp, class _Hash, class _Pred, class _Alloc>
703 hash_map<_Key, _Tp, _Hash, _Pred, _Alloc>::operator[](const key_type& __k)
705 iterator __i = find(__k);
708 __node_holder __h = __construct_node(__k);
709 pair<iterator, bool> __r = __table_.__node_insert_unique(__h.get());
711 return __r.first->second;
714 template <class _Key, class _Tp, class _Hash, class _Pred, class _Alloc>
715 inline _LIBCPP_INLINE_VISIBILITY
717 swap(hash_map<_Key, _Tp, _Hash, _Pred, _Alloc>& __x,
718 hash_map<_Key, _Tp, _Hash, _Pred, _Alloc>& __y)
723 template <class _Key, class _Tp, class _Hash, class _Pred, class _Alloc>
725 operator==(const hash_map<_Key, _Tp, _Hash, _Pred, _Alloc>& __x,
726 const hash_map<_Key, _Tp, _Hash, _Pred, _Alloc>& __y)
728 if (__x.size() != __y.size())
730 typedef typename hash_map<_Key, _Tp, _Hash, _Pred, _Alloc>::const_iterator
732 for (const_iterator __i = __x.begin(), __ex = __x.end(), __ey = __y.end();
735 const_iterator __j = __y.find(__i->first);
736 if (__j == __ey || !(*__i == *__j))
742 template <class _Key, class _Tp, class _Hash, class _Pred, class _Alloc>
743 inline _LIBCPP_INLINE_VISIBILITY
745 operator!=(const hash_map<_Key, _Tp, _Hash, _Pred, _Alloc>& __x,
746 const hash_map<_Key, _Tp, _Hash, _Pred, _Alloc>& __y)
748 return !(__x == __y);
751 template <class _Key, class _Tp, class _Hash = hash<_Key>, class _Pred = equal_to<_Key>,
752 class _Alloc = allocator<pair<const _Key, _Tp> > >
753 class _LIBCPP_TYPE_VIS hash_multimap
757 typedef _Key key_type;
758 typedef _Tp mapped_type;
759 typedef _Tp data_type;
760 typedef _Hash hasher;
761 typedef _Pred key_equal;
762 typedef _Alloc allocator_type;
763 typedef pair<const key_type, mapped_type> value_type;
764 typedef value_type& reference;
765 typedef const value_type& const_reference;
768 typedef pair<key_type, mapped_type> __value_type;
769 typedef __hash_map_hasher<__value_type, hasher> __hasher;
770 typedef __hash_map_equal<__value_type, key_equal> __key_equal;
771 typedef typename allocator_traits<allocator_type>::template
772 #ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
773 rebind_alloc<__value_type>
775 rebind_alloc<__value_type>::other
779 typedef __hash_table<__value_type, __hasher,
780 __key_equal, __allocator_type> __table;
784 typedef typename __table::__node_traits __node_traits;
785 typedef typename __table::__node_allocator __node_allocator;
786 typedef typename __table::__node __node;
787 typedef __hash_map_node_destructor<__node_allocator> _Dp;
788 typedef unique_ptr<__node, _Dp> __node_holder;
789 typedef allocator_traits<allocator_type> __alloc_traits;
791 typedef typename __alloc_traits::pointer pointer;
792 typedef typename __alloc_traits::const_pointer const_pointer;
793 typedef typename __alloc_traits::size_type size_type;
794 typedef typename __alloc_traits::difference_type difference_type;
796 typedef __hash_map_iterator<typename __table::iterator> iterator;
797 typedef __hash_map_const_iterator<typename __table::const_iterator> const_iterator;
799 _LIBCPP_INLINE_VISIBILITY
800 hash_multimap() {__table_.rehash(193);}
801 explicit hash_multimap(size_type __n, const hasher& __hf = hasher(),
802 const key_equal& __eql = key_equal());
803 hash_multimap(size_type __n, const hasher& __hf,
804 const key_equal& __eql,
805 const allocator_type& __a);
806 template <class _InputIterator>
807 hash_multimap(_InputIterator __first, _InputIterator __last);
808 template <class _InputIterator>
809 hash_multimap(_InputIterator __first, _InputIterator __last,
810 size_type __n, const hasher& __hf = hasher(),
811 const key_equal& __eql = key_equal());
812 template <class _InputIterator>
813 hash_multimap(_InputIterator __first, _InputIterator __last,
814 size_type __n, const hasher& __hf,
815 const key_equal& __eql,
816 const allocator_type& __a);
817 hash_multimap(const hash_multimap& __u);
819 _LIBCPP_INLINE_VISIBILITY
820 allocator_type get_allocator() const
821 {return allocator_type(__table_.__node_alloc());}
823 _LIBCPP_INLINE_VISIBILITY
824 bool empty() const {return __table_.size() == 0;}
825 _LIBCPP_INLINE_VISIBILITY
826 size_type size() const {return __table_.size();}
827 _LIBCPP_INLINE_VISIBILITY
828 size_type max_size() const {return __table_.max_size();}
830 _LIBCPP_INLINE_VISIBILITY
831 iterator begin() {return __table_.begin();}
832 _LIBCPP_INLINE_VISIBILITY
833 iterator end() {return __table_.end();}
834 _LIBCPP_INLINE_VISIBILITY
835 const_iterator begin() const {return __table_.begin();}
836 _LIBCPP_INLINE_VISIBILITY
837 const_iterator end() const {return __table_.end();}
839 _LIBCPP_INLINE_VISIBILITY
840 iterator insert(const value_type& __x) {return __table_.__insert_multi(__x);}
841 _LIBCPP_INLINE_VISIBILITY
842 iterator insert(const_iterator, const value_type& __x) {return insert(__x);}
843 template <class _InputIterator>
844 void insert(_InputIterator __first, _InputIterator __last);
846 _LIBCPP_INLINE_VISIBILITY
847 void erase(const_iterator __p) {__table_.erase(__p.__i_);}
848 _LIBCPP_INLINE_VISIBILITY
849 size_type erase(const key_type& __k) {return __table_.__erase_multi(__k);}
850 _LIBCPP_INLINE_VISIBILITY
851 void erase(const_iterator __first, const_iterator __last)
852 {__table_.erase(__first.__i_, __last.__i_);}
853 _LIBCPP_INLINE_VISIBILITY
854 void clear() {__table_.clear();}
856 _LIBCPP_INLINE_VISIBILITY
857 void swap(hash_multimap& __u) {__table_.swap(__u.__table_);}
859 _LIBCPP_INLINE_VISIBILITY
860 hasher hash_funct() const
861 {return __table_.hash_function().hash_function();}
862 _LIBCPP_INLINE_VISIBILITY
863 key_equal key_eq() const
864 {return __table_.key_eq().key_eq();}
866 _LIBCPP_INLINE_VISIBILITY
867 iterator find(const key_type& __k) {return __table_.find(__k);}
868 _LIBCPP_INLINE_VISIBILITY
869 const_iterator find(const key_type& __k) const {return __table_.find(__k);}
870 _LIBCPP_INLINE_VISIBILITY
871 size_type count(const key_type& __k) const {return __table_.__count_multi(__k);}
872 _LIBCPP_INLINE_VISIBILITY
873 pair<iterator, iterator> equal_range(const key_type& __k)
874 {return __table_.__equal_range_multi(__k);}
875 _LIBCPP_INLINE_VISIBILITY
876 pair<const_iterator, const_iterator> equal_range(const key_type& __k) const
877 {return __table_.__equal_range_multi(__k);}
879 _LIBCPP_INLINE_VISIBILITY
880 size_type bucket_count() const {return __table_.bucket_count();}
881 _LIBCPP_INLINE_VISIBILITY
882 size_type max_bucket_count() const {return __table_.max_bucket_count();}
884 _LIBCPP_INLINE_VISIBILITY
885 size_type elems_in_bucket(size_type __n) const
886 {return __table_.bucket_size(__n);}
888 _LIBCPP_INLINE_VISIBILITY
889 void resize(size_type __n) {__table_.rehash(__n);}
892 template <class _Key, class _Tp, class _Hash, class _Pred, class _Alloc>
893 hash_multimap<_Key, _Tp, _Hash, _Pred, _Alloc>::hash_multimap(
894 size_type __n, const hasher& __hf, const key_equal& __eql)
895 : __table_(__hf, __eql)
897 __table_.rehash(__n);
900 template <class _Key, class _Tp, class _Hash, class _Pred, class _Alloc>
901 hash_multimap<_Key, _Tp, _Hash, _Pred, _Alloc>::hash_multimap(
902 size_type __n, const hasher& __hf, const key_equal& __eql,
903 const allocator_type& __a)
904 : __table_(__hf, __eql, __a)
906 __table_.rehash(__n);
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)
914 __table_.rehash(193);
915 insert(__first, __last);
918 template <class _Key, class _Tp, class _Hash, class _Pred, class _Alloc>
919 template <class _InputIterator>
920 hash_multimap<_Key, _Tp, _Hash, _Pred, _Alloc>::hash_multimap(
921 _InputIterator __first, _InputIterator __last, size_type __n,
922 const hasher& __hf, const key_equal& __eql)
923 : __table_(__hf, __eql)
925 __table_.rehash(__n);
926 insert(__first, __last);
929 template <class _Key, class _Tp, class _Hash, class _Pred, class _Alloc>
930 template <class _InputIterator>
931 hash_multimap<_Key, _Tp, _Hash, _Pred, _Alloc>::hash_multimap(
932 _InputIterator __first, _InputIterator __last, size_type __n,
933 const hasher& __hf, const key_equal& __eql, const allocator_type& __a)
934 : __table_(__hf, __eql, __a)
936 __table_.rehash(__n);
937 insert(__first, __last);
940 template <class _Key, class _Tp, class _Hash, class _Pred, class _Alloc>
941 hash_multimap<_Key, _Tp, _Hash, _Pred, _Alloc>::hash_multimap(
942 const hash_multimap& __u)
943 : __table_(__u.__table_)
945 __table_.rehash(__u.bucket_count());
946 insert(__u.begin(), __u.end());
949 template <class _Key, class _Tp, class _Hash, class _Pred, class _Alloc>
950 template <class _InputIterator>
951 inline _LIBCPP_INLINE_VISIBILITY
953 hash_multimap<_Key, _Tp, _Hash, _Pred, _Alloc>::insert(_InputIterator __first,
954 _InputIterator __last)
956 for (; __first != __last; ++__first)
957 __table_.__insert_multi(*__first);
960 template <class _Key, class _Tp, class _Hash, class _Pred, class _Alloc>
961 inline _LIBCPP_INLINE_VISIBILITY
963 swap(hash_multimap<_Key, _Tp, _Hash, _Pred, _Alloc>& __x,
964 hash_multimap<_Key, _Tp, _Hash, _Pred, _Alloc>& __y)
969 template <class _Key, class _Tp, class _Hash, class _Pred, class _Alloc>
971 operator==(const hash_multimap<_Key, _Tp, _Hash, _Pred, _Alloc>& __x,
972 const hash_multimap<_Key, _Tp, _Hash, _Pred, _Alloc>& __y)
974 if (__x.size() != __y.size())
976 typedef typename hash_multimap<_Key, _Tp, _Hash, _Pred, _Alloc>::const_iterator
978 typedef pair<const_iterator, const_iterator> _EqRng;
979 for (const_iterator __i = __x.begin(), __ex = __x.end(); __i != __ex;)
981 _EqRng __xeq = __x.equal_range(__i->first);
982 _EqRng __yeq = __y.equal_range(__i->first);
983 if (_VSTD::distance(__xeq.first, __xeq.second) !=
984 _VSTD::distance(__yeq.first, __yeq.second) ||
985 !_VSTD::is_permutation(__xeq.first, __xeq.second, __yeq.first))
992 template <class _Key, class _Tp, class _Hash, class _Pred, class _Alloc>
993 inline _LIBCPP_INLINE_VISIBILITY
995 operator!=(const hash_multimap<_Key, _Tp, _Hash, _Pred, _Alloc>& __x,
996 const hash_multimap<_Key, _Tp, _Hash, _Pred, _Alloc>& __y)
998 return !(__x == __y);
1003 #endif // _LIBCPP_HASH_MAP