xref: /freebsd/contrib/llvm-project/libcxx/include/ext/hash_map (revision 6580f5c38dd5b01aeeaed16b370f1a12423437f0)
1// -*- C++ -*-
2//===----------------------------------------------------------------------===//
3//
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
7//
8//===----------------------------------------------------------------------===//
9
10#ifndef _LIBCPP_HASH_MAP
11#define _LIBCPP_HASH_MAP
12
13/*
14
15    hash_map synopsis
16
17namespace __gnu_cxx
18{
19
20template <class Key, class T, class Hash = hash<Key>, class Pred = equal_to<Key>,
21          class Alloc = allocator<pair<const Key, T>>>
22class hash_map
23{
24public:
25    // types
26    typedef Key                                                        key_type;
27    typedef T                                                          mapped_type;
28    typedef Hash                                                       hasher;
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;
38
39    typedef /unspecified/ iterator;
40    typedef /unspecified/ const_iterator;
41
42    hash_map();
43    explicit hash_map(size_type n, 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    template <class InputIterator>
49    hash_map(InputIterator f, InputIterator l,
50                size_type n, const hasher& hf = hasher(),
51                const key_equal& eql = key_equal(),
52                const allocator_type& a = allocator_type());
53    hash_map(const hash_map&);
54    ~hash_map();
55    hash_map& operator=(const hash_map&);
56
57    allocator_type get_allocator() const;
58
59    bool      empty() const;
60    size_type size() const;
61    size_type max_size() const;
62
63    iterator       begin();
64    iterator       end();
65    const_iterator begin()  const;
66    const_iterator end()    const;
67
68    pair<iterator, bool> insert(const value_type& obj);
69    template <class InputIterator>
70        void insert(InputIterator first, InputIterator last);
71
72    void erase(const_iterator position);
73    size_type erase(const key_type& k);
74    void erase(const_iterator first, const_iterator last);
75    void clear();
76
77    void swap(hash_map&);
78
79    hasher hash_funct() const;
80    key_equal key_eq() const;
81
82    iterator       find(const key_type& k);
83    const_iterator find(const key_type& k) const;
84    size_type count(const key_type& k) const;
85    pair<iterator, iterator>             equal_range(const key_type& k);
86    pair<const_iterator, const_iterator> equal_range(const key_type& k) const;
87
88    mapped_type& operator[](const key_type& k);
89
90    size_type bucket_count() const;
91    size_type max_bucket_count() const;
92
93    size_type elems_in_bucket(size_type n) const;
94
95    void resize(size_type n);
96};
97
98template <class Key, class T, class Hash, class Pred, class Alloc>
99    void swap(hash_map<Key, T, Hash, Pred, Alloc>& x,
100              hash_map<Key, T, Hash, Pred, Alloc>& y);
101
102template <class Key, class T, class Hash, class Pred, class Alloc>
103    bool
104    operator==(const hash_map<Key, T, Hash, Pred, Alloc>& x,
105               const hash_map<Key, T, Hash, Pred, Alloc>& y);
106
107template <class Key, class T, class Hash, class Pred, class Alloc>
108    bool
109    operator!=(const hash_map<Key, T, Hash, Pred, Alloc>& x,
110               const hash_map<Key, T, Hash, Pred, Alloc>& y);
111
112template <class Key, class T, class Hash = hash<Key>, class Pred = equal_to<Key>,
113          class Alloc = allocator<pair<const Key, T>>>
114class hash_multimap
115{
116public:
117    // types
118    typedef Key                                                        key_type;
119    typedef T                                                          mapped_type;
120    typedef Hash                                                       hasher;
121    typedef Pred                                                       key_equal;
122    typedef Alloc                                                      allocator_type;
123    typedef pair<const key_type, mapped_type>                          value_type;
124    typedef value_type&                                                reference;
125    typedef const value_type&                                          const_reference;
126    typedef typename allocator_traits<allocator_type>::pointer         pointer;
127    typedef typename allocator_traits<allocator_type>::const_pointer   const_pointer;
128    typedef typename allocator_traits<allocator_type>::size_type       size_type;
129    typedef typename allocator_traits<allocator_type>::difference_type difference_type;
130
131    typedef /unspecified/ iterator;
132    typedef /unspecified/ const_iterator;
133
134    explicit hash_multimap(size_type n = 193, const hasher& hf = hasher(),
135                           const key_equal& eql = key_equal(),
136                           const allocator_type& a = allocator_type());
137    template <class InputIterator>
138        hash_multimap(InputIterator f, InputIterator l,
139                      size_type n = 193, const hasher& hf = hasher(),
140                      const key_equal& eql = key_equal(),
141                      const allocator_type& a = allocator_type());
142    explicit hash_multimap(const allocator_type&);
143    hash_multimap(const hash_multimap&);
144    ~hash_multimap();
145    hash_multimap& operator=(const hash_multimap&);
146
147    allocator_type get_allocator() const;
148
149    bool      empty() const;
150    size_type size() const;
151    size_type max_size() const;
152
153    iterator       begin();
154    iterator       end();
155    const_iterator begin()  const;
156    const_iterator end()    const;
157
158    iterator insert(const value_type& obj);
159    template <class InputIterator>
160        void insert(InputIterator first, InputIterator last);
161
162    void erase(const_iterator position);
163    size_type erase(const key_type& k);
164    void erase(const_iterator first, const_iterator last);
165    void clear();
166
167    void swap(hash_multimap&);
168
169    hasher hash_funct() const;
170    key_equal key_eq() const;
171
172    iterator       find(const key_type& k);
173    const_iterator find(const key_type& k) const;
174    size_type count(const key_type& k) const;
175    pair<iterator, iterator>             equal_range(const key_type& k);
176    pair<const_iterator, const_iterator> equal_range(const key_type& k) const;
177
178    size_type bucket_count() const;
179    size_type max_bucket_count() const;
180
181    size_type elems_in_bucket(size_type n) const;
182
183    void resize(size_type n);
184};
185
186template <class Key, class T, class Hash, class Pred, class Alloc>
187    void swap(hash_multimap<Key, T, Hash, Pred, Alloc>& x,
188              hash_multimap<Key, T, Hash, Pred, Alloc>& y);
189
190template <class Key, class T, class Hash, class Pred, class Alloc>
191    bool
192    operator==(const hash_multimap<Key, T, Hash, Pred, Alloc>& x,
193               const hash_multimap<Key, T, Hash, Pred, Alloc>& y);
194
195template <class Key, class T, class Hash, class Pred, class Alloc>
196    bool
197    operator!=(const hash_multimap<Key, T, Hash, Pred, Alloc>& x,
198               const hash_multimap<Key, T, Hash, Pred, Alloc>& y);
199
200}  // __gnu_cxx
201
202*/
203
204#include <__assert> // all public C++ headers provide the assertion handler
205#include <__config>
206#include <__hash_table>
207#include <algorithm>
208#include <ext/__hash>
209#include <functional>
210
211#if defined(__DEPRECATED) && __DEPRECATED
212#  if defined(_LIBCPP_WARNING)
213_LIBCPP_WARNING("Use of the header <ext/hash_map> is deprecated.  Migrate to <unordered_map>")
214#  else
215#    warning Use of the header <ext/hash_map> is deprecated.  Migrate to <unordered_map>
216#  endif
217#endif
218
219#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
220#  pragma GCC system_header
221#endif
222
223namespace __gnu_cxx {
224
225template <class _Tp, class _Hash, bool = std::is_empty<_Hash>::value && !std::__libcpp_is_final<_Hash>::value >
226class __hash_map_hasher : private _Hash {
227public:
228  _LIBCPP_HIDE_FROM_ABI __hash_map_hasher() : _Hash() {}
229  _LIBCPP_HIDE_FROM_ABI __hash_map_hasher(const _Hash& __h) : _Hash(__h) {}
230  _LIBCPP_HIDE_FROM_ABI const _Hash& hash_function() const { return *this; }
231  _LIBCPP_HIDE_FROM_ABI size_t operator()(const _Tp& __x) const { return static_cast<const _Hash&>(*this)(__x.first); }
232  _LIBCPP_HIDE_FROM_ABI size_t operator()(const typename _Tp::first_type& __x) const {
233    return static_cast<const _Hash&>(*this)(__x);
234  }
235};
236
237template <class _Tp, class _Hash>
238class __hash_map_hasher<_Tp, _Hash, false> {
239  _Hash __hash_;
240
241public:
242  _LIBCPP_HIDE_FROM_ABI __hash_map_hasher() : __hash_() {}
243  _LIBCPP_HIDE_FROM_ABI __hash_map_hasher(const _Hash& __h) : __hash_(__h) {}
244  _LIBCPP_HIDE_FROM_ABI const _Hash& hash_function() const { return __hash_; }
245  _LIBCPP_HIDE_FROM_ABI size_t operator()(const _Tp& __x) const { return __hash_(__x.first); }
246  _LIBCPP_HIDE_FROM_ABI size_t operator()(const typename _Tp::first_type& __x) const { return __hash_(__x); }
247};
248
249template <class _Tp, class _Pred, bool = std::is_empty<_Pred>::value && !std::__libcpp_is_final<_Pred>::value >
250class __hash_map_equal : private _Pred {
251public:
252  _LIBCPP_HIDE_FROM_ABI __hash_map_equal() : _Pred() {}
253  _LIBCPP_HIDE_FROM_ABI __hash_map_equal(const _Pred& __p) : _Pred(__p) {}
254  _LIBCPP_HIDE_FROM_ABI const _Pred& key_eq() const { return *this; }
255  _LIBCPP_HIDE_FROM_ABI bool operator()(const _Tp& __x, const _Tp& __y) const {
256    return static_cast<const _Pred&>(*this)(__x.first, __y.first);
257  }
258  _LIBCPP_HIDE_FROM_ABI bool operator()(const typename _Tp::first_type& __x, const _Tp& __y) const {
259    return static_cast<const _Pred&>(*this)(__x, __y.first);
260  }
261  _LIBCPP_HIDE_FROM_ABI bool operator()(const _Tp& __x, const typename _Tp::first_type& __y) const {
262    return static_cast<const _Pred&>(*this)(__x.first, __y);
263  }
264  _LIBCPP_HIDE_FROM_ABI bool
265  operator()(const typename _Tp::first_type& __x, const typename _Tp::first_type& __y) const {
266    return static_cast<const _Pred&>(*this)(__x, __y);
267  }
268};
269
270template <class _Tp, class _Pred>
271class __hash_map_equal<_Tp, _Pred, false> {
272  _Pred __pred_;
273
274public:
275  _LIBCPP_HIDE_FROM_ABI __hash_map_equal() : __pred_() {}
276  _LIBCPP_HIDE_FROM_ABI __hash_map_equal(const _Pred& __p) : __pred_(__p) {}
277  _LIBCPP_HIDE_FROM_ABI const _Pred& key_eq() const { return __pred_; }
278  _LIBCPP_HIDE_FROM_ABI bool operator()(const _Tp& __x, const _Tp& __y) const { return __pred_(__x.first, __y.first); }
279  _LIBCPP_HIDE_FROM_ABI bool operator()(const typename _Tp::first_type& __x, const _Tp& __y) const {
280    return __pred_(__x, __y.first);
281  }
282  _LIBCPP_HIDE_FROM_ABI bool operator()(const _Tp& __x, const typename _Tp::first_type& __y) const {
283    return __pred_(__x.first, __y);
284  }
285  _LIBCPP_HIDE_FROM_ABI bool
286  operator()(const typename _Tp::first_type& __x, const typename _Tp::first_type& __y) const {
287    return __pred_(__x, __y);
288  }
289};
290
291template <class _Alloc>
292class __hash_map_node_destructor {
293  typedef _Alloc allocator_type;
294  typedef std::allocator_traits<allocator_type> __alloc_traits;
295  typedef typename __alloc_traits::value_type::__node_value_type value_type;
296
297public:
298  typedef typename __alloc_traits::pointer pointer;
299
300private:
301  typedef typename value_type::first_type first_type;
302  typedef typename value_type::second_type second_type;
303
304  allocator_type& __na_;
305
306public:
307  bool __first_constructed;
308  bool __second_constructed;
309
310  _LIBCPP_HIDE_FROM_ABI __hash_map_node_destructor(__hash_map_node_destructor const&) = default;
311  __hash_map_node_destructor& operator=(const __hash_map_node_destructor&)            = delete;
312
313  _LIBCPP_HIDE_FROM_ABI explicit __hash_map_node_destructor(allocator_type& __na)
314      : __na_(__na), __first_constructed(false), __second_constructed(false) {}
315
316#ifndef _LIBCPP_CXX03_LANG
317  _LIBCPP_HIDE_FROM_ABI __hash_map_node_destructor(std::__hash_node_destructor<allocator_type>&& __x)
318      : __na_(__x.__na_), __first_constructed(__x.__value_constructed), __second_constructed(__x.__value_constructed) {
319    __x.__value_constructed = false;
320  }
321#else  // _LIBCPP_CXX03_LANG
322  _LIBCPP_HIDE_FROM_ABI __hash_map_node_destructor(const std::__hash_node_destructor<allocator_type>& __x)
323      : __na_(__x.__na_), __first_constructed(__x.__value_constructed), __second_constructed(__x.__value_constructed) {
324    const_cast<bool&>(__x.__value_constructed) = false;
325  }
326#endif // _LIBCPP_CXX03_LANG
327
328  _LIBCPP_HIDE_FROM_ABI void operator()(pointer __p) {
329    if (__second_constructed)
330      __alloc_traits::destroy(__na_, std::addressof(__p->__get_value().second));
331    if (__first_constructed)
332      __alloc_traits::destroy(__na_, std::addressof(__p->__get_value().first));
333    if (__p)
334      __alloc_traits::deallocate(__na_, __p, 1);
335  }
336};
337
338template <class _HashIterator>
339class _LIBCPP_TEMPLATE_VIS __hash_map_iterator {
340  _HashIterator __i_;
341
342  typedef const typename _HashIterator::value_type::first_type key_type;
343  typedef typename _HashIterator::value_type::second_type mapped_type;
344
345public:
346  typedef std::forward_iterator_tag iterator_category;
347  typedef std::pair<key_type, mapped_type> value_type;
348  typedef typename _HashIterator::difference_type difference_type;
349  typedef value_type& reference;
350  typedef std::__rebind_pointer_t<typename _HashIterator::pointer, value_type> pointer;
351
352  _LIBCPP_HIDE_FROM_ABI __hash_map_iterator() {}
353
354  _LIBCPP_HIDE_FROM_ABI __hash_map_iterator(_HashIterator __i) : __i_(__i) {}
355
356  _LIBCPP_HIDE_FROM_ABI reference operator*() const { return *operator->(); }
357  _LIBCPP_HIDE_FROM_ABI pointer operator->() const { return (pointer)__i_.operator->(); }
358
359  _LIBCPP_HIDE_FROM_ABI __hash_map_iterator& operator++() {
360    ++__i_;
361    return *this;
362  }
363  _LIBCPP_HIDE_FROM_ABI __hash_map_iterator operator++(int) {
364    __hash_map_iterator __t(*this);
365    ++(*this);
366    return __t;
367  }
368
369  friend _LIBCPP_HIDE_FROM_ABI bool operator==(const __hash_map_iterator& __x, const __hash_map_iterator& __y) {
370    return __x.__i_ == __y.__i_;
371  }
372  friend _LIBCPP_HIDE_FROM_ABI bool operator!=(const __hash_map_iterator& __x, const __hash_map_iterator& __y) {
373    return __x.__i_ != __y.__i_;
374  }
375
376  template <class, class, class, class, class>
377  friend class _LIBCPP_TEMPLATE_VIS hash_map;
378  template <class, class, class, class, class>
379  friend class _LIBCPP_TEMPLATE_VIS hash_multimap;
380  template <class>
381  friend class _LIBCPP_TEMPLATE_VIS __hash_const_iterator;
382  template <class>
383  friend class _LIBCPP_TEMPLATE_VIS __hash_const_local_iterator;
384  template <class>
385  friend class _LIBCPP_TEMPLATE_VIS __hash_map_const_iterator;
386};
387
388template <class _HashIterator>
389class _LIBCPP_TEMPLATE_VIS __hash_map_const_iterator {
390  _HashIterator __i_;
391
392  typedef const typename _HashIterator::value_type::first_type key_type;
393  typedef typename _HashIterator::value_type::second_type mapped_type;
394
395public:
396  typedef std::forward_iterator_tag iterator_category;
397  typedef std::pair<key_type, mapped_type> value_type;
398  typedef typename _HashIterator::difference_type difference_type;
399  typedef const value_type& reference;
400  typedef std::__rebind_pointer_t<typename _HashIterator::pointer, const value_type> pointer;
401
402  _LIBCPP_HIDE_FROM_ABI __hash_map_const_iterator() {}
403
404  _LIBCPP_HIDE_FROM_ABI __hash_map_const_iterator(_HashIterator __i) : __i_(__i) {}
405  _LIBCPP_HIDE_FROM_ABI __hash_map_const_iterator(__hash_map_iterator<typename _HashIterator::__non_const_iterator> __i)
406      : __i_(__i.__i_) {}
407
408  _LIBCPP_HIDE_FROM_ABI reference operator*() const { return *operator->(); }
409  _LIBCPP_HIDE_FROM_ABI pointer operator->() const { return (pointer)__i_.operator->(); }
410
411  _LIBCPP_HIDE_FROM_ABI __hash_map_const_iterator& operator++() {
412    ++__i_;
413    return *this;
414  }
415  _LIBCPP_HIDE_FROM_ABI __hash_map_const_iterator operator++(int) {
416    __hash_map_const_iterator __t(*this);
417    ++(*this);
418    return __t;
419  }
420
421  friend _LIBCPP_HIDE_FROM_ABI bool
422  operator==(const __hash_map_const_iterator& __x, const __hash_map_const_iterator& __y) {
423    return __x.__i_ == __y.__i_;
424  }
425  friend _LIBCPP_HIDE_FROM_ABI bool
426  operator!=(const __hash_map_const_iterator& __x, const __hash_map_const_iterator& __y) {
427    return __x.__i_ != __y.__i_;
428  }
429
430  template <class, class, class, class, class>
431  friend class _LIBCPP_TEMPLATE_VIS hash_map;
432  template <class, class, class, class, class>
433  friend class _LIBCPP_TEMPLATE_VIS hash_multimap;
434  template <class>
435  friend class _LIBCPP_TEMPLATE_VIS __hash_const_iterator;
436  template <class>
437  friend class _LIBCPP_TEMPLATE_VIS __hash_const_local_iterator;
438};
439
440template <class _Key,
441          class _Tp,
442          class _Hash  = hash<_Key>,
443          class _Pred  = std::equal_to<_Key>,
444          class _Alloc = std::allocator<std::pair<const _Key, _Tp> > >
445class _LIBCPP_TEMPLATE_VIS hash_map {
446public:
447  // types
448  typedef _Key key_type;
449  typedef _Tp mapped_type;
450  typedef _Tp data_type;
451  typedef _Hash hasher;
452  typedef _Pred key_equal;
453  typedef _Alloc allocator_type;
454  typedef std::pair<const key_type, mapped_type> value_type;
455  typedef value_type& reference;
456  typedef const value_type& const_reference;
457
458private:
459  typedef std::pair<key_type, mapped_type> __value_type;
460  typedef __hash_map_hasher<__value_type, hasher> __hasher;
461  typedef __hash_map_equal<__value_type, key_equal> __key_equal;
462  typedef std::__rebind_alloc<std::allocator_traits<allocator_type>, __value_type> __allocator_type;
463
464  typedef std::__hash_table<__value_type, __hasher, __key_equal, __allocator_type> __table;
465
466  __table __table_;
467
468  typedef typename __table::__node_pointer __node_pointer;
469  typedef typename __table::__node_const_pointer __node_const_pointer;
470  typedef typename __table::__node_traits __node_traits;
471  typedef typename __table::__node_allocator __node_allocator;
472  typedef typename __table::__node __node;
473  typedef __hash_map_node_destructor<__node_allocator> _Dp;
474  typedef std::unique_ptr<__node, _Dp> __node_holder;
475  typedef std::allocator_traits<allocator_type> __alloc_traits;
476
477public:
478  typedef typename __alloc_traits::pointer pointer;
479  typedef typename __alloc_traits::const_pointer const_pointer;
480  typedef typename __alloc_traits::size_type size_type;
481  typedef typename __alloc_traits::difference_type difference_type;
482
483  typedef __hash_map_iterator<typename __table::iterator> iterator;
484  typedef __hash_map_const_iterator<typename __table::const_iterator> const_iterator;
485
486  _LIBCPP_HIDE_FROM_ABI hash_map() {}
487  explicit _LIBCPP_HIDE_FROM_ABI
488  hash_map(size_type __n, const hasher& __hf = hasher(), const key_equal& __eql = key_equal());
489  _LIBCPP_HIDE_FROM_ABI hash_map(size_type __n, const hasher& __hf, const key_equal& __eql, const allocator_type& __a);
490  template <class _InputIterator>
491  _LIBCPP_HIDE_FROM_ABI hash_map(_InputIterator __first, _InputIterator __last);
492  template <class _InputIterator>
493  _LIBCPP_HIDE_FROM_ABI
494  hash_map(_InputIterator __first,
495           _InputIterator __last,
496           size_type __n,
497           const hasher& __hf     = hasher(),
498           const key_equal& __eql = key_equal());
499  template <class _InputIterator>
500  _LIBCPP_HIDE_FROM_ABI
501  hash_map(_InputIterator __first,
502           _InputIterator __last,
503           size_type __n,
504           const hasher& __hf,
505           const key_equal& __eql,
506           const allocator_type& __a);
507  _LIBCPP_HIDE_FROM_ABI hash_map(const hash_map& __u);
508
509  _LIBCPP_HIDE_FROM_ABI allocator_type get_allocator() const { return allocator_type(__table_.__node_alloc()); }
510
511  _LIBCPP_HIDE_FROM_ABI bool empty() const { return __table_.size() == 0; }
512  _LIBCPP_HIDE_FROM_ABI size_type size() const { return __table_.size(); }
513  _LIBCPP_HIDE_FROM_ABI size_type max_size() const { return __table_.max_size(); }
514
515  _LIBCPP_HIDE_FROM_ABI iterator begin() { return __table_.begin(); }
516  _LIBCPP_HIDE_FROM_ABI iterator end() { return __table_.end(); }
517  _LIBCPP_HIDE_FROM_ABI const_iterator begin() const { return __table_.begin(); }
518  _LIBCPP_HIDE_FROM_ABI const_iterator end() const { return __table_.end(); }
519
520  _LIBCPP_HIDE_FROM_ABI std::pair<iterator, bool> insert(const value_type& __x) {
521    return __table_.__insert_unique(__x);
522  }
523  _LIBCPP_HIDE_FROM_ABI iterator insert(const_iterator, const value_type& __x) { return insert(__x).first; }
524  template <class _InputIterator>
525  _LIBCPP_HIDE_FROM_ABI void insert(_InputIterator __first, _InputIterator __last);
526
527  _LIBCPP_HIDE_FROM_ABI void erase(const_iterator __p) { __table_.erase(__p.__i_); }
528  _LIBCPP_HIDE_FROM_ABI size_type erase(const key_type& __k) { return __table_.__erase_unique(__k); }
529  _LIBCPP_HIDE_FROM_ABI void erase(const_iterator __first, const_iterator __last) {
530    __table_.erase(__first.__i_, __last.__i_);
531  }
532  _LIBCPP_HIDE_FROM_ABI void clear() { __table_.clear(); }
533
534  _LIBCPP_HIDE_FROM_ABI void swap(hash_map& __u) { __table_.swap(__u.__table_); }
535
536  _LIBCPP_HIDE_FROM_ABI hasher hash_funct() const { return __table_.hash_function().hash_function(); }
537  _LIBCPP_HIDE_FROM_ABI key_equal key_eq() const { return __table_.key_eq().key_eq(); }
538
539  _LIBCPP_HIDE_FROM_ABI iterator find(const key_type& __k) { return __table_.find(__k); }
540  _LIBCPP_HIDE_FROM_ABI const_iterator find(const key_type& __k) const { return __table_.find(__k); }
541  _LIBCPP_HIDE_FROM_ABI size_type count(const key_type& __k) const { return __table_.__count_unique(__k); }
542  _LIBCPP_HIDE_FROM_ABI std::pair<iterator, iterator> equal_range(const key_type& __k) {
543    return __table_.__equal_range_unique(__k);
544  }
545  _LIBCPP_HIDE_FROM_ABI std::pair<const_iterator, const_iterator> equal_range(const key_type& __k) const {
546    return __table_.__equal_range_unique(__k);
547  }
548
549  _LIBCPP_HIDE_FROM_ABI mapped_type& operator[](const key_type& __k);
550
551  _LIBCPP_HIDE_FROM_ABI size_type bucket_count() const { return __table_.bucket_count(); }
552  _LIBCPP_HIDE_FROM_ABI size_type max_bucket_count() const { return __table_.max_bucket_count(); }
553
554  _LIBCPP_HIDE_FROM_ABI size_type elems_in_bucket(size_type __n) const { return __table_.bucket_size(__n); }
555
556  _LIBCPP_HIDE_FROM_ABI void resize(size_type __n) { __table_.__rehash_unique(__n); }
557
558private:
559  _LIBCPP_HIDE_FROM_ABI __node_holder __construct_node(const key_type& __k);
560};
561
562template <class _Key, class _Tp, class _Hash, class _Pred, class _Alloc>
563hash_map<_Key, _Tp, _Hash, _Pred, _Alloc>::hash_map(size_type __n, const hasher& __hf, const key_equal& __eql)
564    : __table_(__hf, __eql) {
565  __table_.__rehash_unique(__n);
566}
567
568template <class _Key, class _Tp, class _Hash, class _Pred, class _Alloc>
569hash_map<_Key, _Tp, _Hash, _Pred, _Alloc>::hash_map(
570    size_type __n, const hasher& __hf, const key_equal& __eql, const allocator_type& __a)
571    : __table_(__hf, __eql, __a) {
572  __table_.__rehash_unique(__n);
573}
574
575template <class _Key, class _Tp, class _Hash, class _Pred, class _Alloc>
576template <class _InputIterator>
577hash_map<_Key, _Tp, _Hash, _Pred, _Alloc>::hash_map(_InputIterator __first, _InputIterator __last) {
578  insert(__first, __last);
579}
580
581template <class _Key, class _Tp, class _Hash, class _Pred, class _Alloc>
582template <class _InputIterator>
583hash_map<_Key, _Tp, _Hash, _Pred, _Alloc>::hash_map(
584    _InputIterator __first, _InputIterator __last, size_type __n, const hasher& __hf, const key_equal& __eql)
585    : __table_(__hf, __eql) {
586  __table_.__rehash_unique(__n);
587  insert(__first, __last);
588}
589
590template <class _Key, class _Tp, class _Hash, class _Pred, class _Alloc>
591template <class _InputIterator>
592hash_map<_Key, _Tp, _Hash, _Pred, _Alloc>::hash_map(
593    _InputIterator __first,
594    _InputIterator __last,
595    size_type __n,
596    const hasher& __hf,
597    const key_equal& __eql,
598    const allocator_type& __a)
599    : __table_(__hf, __eql, __a) {
600  __table_.__rehash_unique(__n);
601  insert(__first, __last);
602}
603
604template <class _Key, class _Tp, class _Hash, class _Pred, class _Alloc>
605hash_map<_Key, _Tp, _Hash, _Pred, _Alloc>::hash_map(const hash_map& __u) : __table_(__u.__table_) {
606  __table_.__rehash_unique(__u.bucket_count());
607  insert(__u.begin(), __u.end());
608}
609
610template <class _Key, class _Tp, class _Hash, class _Pred, class _Alloc>
611typename hash_map<_Key, _Tp, _Hash, _Pred, _Alloc>::__node_holder
612hash_map<_Key, _Tp, _Hash, _Pred, _Alloc>::__construct_node(const key_type& __k) {
613  __node_allocator& __na = __table_.__node_alloc();
614  __node_holder __h(__node_traits::allocate(__na, 1), _Dp(__na));
615  __node_traits::construct(__na, std::addressof(__h->__get_value().first), __k);
616  __h.get_deleter().__first_constructed = true;
617  __node_traits::construct(__na, std::addressof(__h->__get_value().second));
618  __h.get_deleter().__second_constructed = true;
619  return __h;
620}
621
622template <class _Key, class _Tp, class _Hash, class _Pred, class _Alloc>
623template <class _InputIterator>
624inline void hash_map<_Key, _Tp, _Hash, _Pred, _Alloc>::insert(_InputIterator __first, _InputIterator __last) {
625  for (; __first != __last; ++__first)
626    __table_.__insert_unique(*__first);
627}
628
629template <class _Key, class _Tp, class _Hash, class _Pred, class _Alloc>
630_Tp& hash_map<_Key, _Tp, _Hash, _Pred, _Alloc>::operator[](const key_type& __k) {
631  iterator __i = find(__k);
632  if (__i != end())
633    return __i->second;
634  __node_holder __h             = __construct_node(__k);
635  std::pair<iterator, bool> __r = __table_.__node_insert_unique(__h.get());
636  __h.release();
637  return __r.first->second;
638}
639
640template <class _Key, class _Tp, class _Hash, class _Pred, class _Alloc>
641inline _LIBCPP_HIDE_FROM_ABI void
642swap(hash_map<_Key, _Tp, _Hash, _Pred, _Alloc>& __x, hash_map<_Key, _Tp, _Hash, _Pred, _Alloc>& __y) {
643  __x.swap(__y);
644}
645
646template <class _Key, class _Tp, class _Hash, class _Pred, class _Alloc>
647_LIBCPP_HIDE_FROM_ABI bool
648operator==(const hash_map<_Key, _Tp, _Hash, _Pred, _Alloc>& __x, const hash_map<_Key, _Tp, _Hash, _Pred, _Alloc>& __y) {
649  if (__x.size() != __y.size())
650    return false;
651  typedef typename hash_map<_Key, _Tp, _Hash, _Pred, _Alloc>::const_iterator const_iterator;
652  for (const_iterator __i = __x.begin(), __ex = __x.end(), __ey = __y.end(); __i != __ex; ++__i) {
653    const_iterator __j = __y.find(__i->first);
654    if (__j == __ey || !(*__i == *__j))
655      return false;
656  }
657  return true;
658}
659
660template <class _Key, class _Tp, class _Hash, class _Pred, class _Alloc>
661inline _LIBCPP_HIDE_FROM_ABI bool
662operator!=(const hash_map<_Key, _Tp, _Hash, _Pred, _Alloc>& __x, const hash_map<_Key, _Tp, _Hash, _Pred, _Alloc>& __y) {
663  return !(__x == __y);
664}
665
666template <class _Key,
667          class _Tp,
668          class _Hash  = hash<_Key>,
669          class _Pred  = std::equal_to<_Key>,
670          class _Alloc = std::allocator<std::pair<const _Key, _Tp> > >
671class _LIBCPP_TEMPLATE_VIS hash_multimap {
672public:
673  // types
674  typedef _Key key_type;
675  typedef _Tp mapped_type;
676  typedef _Tp data_type;
677  typedef _Hash hasher;
678  typedef _Pred key_equal;
679  typedef _Alloc allocator_type;
680  typedef std::pair<const key_type, mapped_type> value_type;
681  typedef value_type& reference;
682  typedef const value_type& const_reference;
683
684private:
685  typedef std::pair<key_type, mapped_type> __value_type;
686  typedef __hash_map_hasher<__value_type, hasher> __hasher;
687  typedef __hash_map_equal<__value_type, key_equal> __key_equal;
688  typedef std::__rebind_alloc<std::allocator_traits<allocator_type>, __value_type> __allocator_type;
689
690  typedef std::__hash_table<__value_type, __hasher, __key_equal, __allocator_type> __table;
691
692  __table __table_;
693
694  typedef typename __table::__node_traits __node_traits;
695  typedef typename __table::__node_allocator __node_allocator;
696  typedef typename __table::__node __node;
697  typedef __hash_map_node_destructor<__node_allocator> _Dp;
698  typedef std::unique_ptr<__node, _Dp> __node_holder;
699  typedef std::allocator_traits<allocator_type> __alloc_traits;
700
701public:
702  typedef typename __alloc_traits::pointer pointer;
703  typedef typename __alloc_traits::const_pointer const_pointer;
704  typedef typename __alloc_traits::size_type size_type;
705  typedef typename __alloc_traits::difference_type difference_type;
706
707  typedef __hash_map_iterator<typename __table::iterator> iterator;
708  typedef __hash_map_const_iterator<typename __table::const_iterator> const_iterator;
709
710  _LIBCPP_HIDE_FROM_ABI hash_multimap() {}
711  explicit _LIBCPP_HIDE_FROM_ABI
712  hash_multimap(size_type __n, const hasher& __hf = hasher(), const key_equal& __eql = key_equal());
713  _LIBCPP_HIDE_FROM_ABI
714  hash_multimap(size_type __n, const hasher& __hf, const key_equal& __eql, const allocator_type& __a);
715  template <class _InputIterator>
716  _LIBCPP_HIDE_FROM_ABI hash_multimap(_InputIterator __first, _InputIterator __last);
717  template <class _InputIterator>
718  _LIBCPP_HIDE_FROM_ABI
719  hash_multimap(_InputIterator __first,
720                _InputIterator __last,
721                size_type __n,
722                const hasher& __hf     = hasher(),
723                const key_equal& __eql = key_equal());
724  template <class _InputIterator>
725  _LIBCPP_HIDE_FROM_ABI hash_multimap(
726      _InputIterator __first,
727      _InputIterator __last,
728      size_type __n,
729      const hasher& __hf,
730      const key_equal& __eql,
731      const allocator_type& __a);
732  _LIBCPP_HIDE_FROM_ABI hash_multimap(const hash_multimap& __u);
733
734  _LIBCPP_HIDE_FROM_ABI allocator_type get_allocator() const { return allocator_type(__table_.__node_alloc()); }
735
736  _LIBCPP_HIDE_FROM_ABI bool empty() const { return __table_.size() == 0; }
737  _LIBCPP_HIDE_FROM_ABI size_type size() const { return __table_.size(); }
738  _LIBCPP_HIDE_FROM_ABI size_type max_size() const { return __table_.max_size(); }
739
740  _LIBCPP_HIDE_FROM_ABI iterator begin() { return __table_.begin(); }
741  _LIBCPP_HIDE_FROM_ABI iterator end() { return __table_.end(); }
742  _LIBCPP_HIDE_FROM_ABI const_iterator begin() const { return __table_.begin(); }
743  _LIBCPP_HIDE_FROM_ABI const_iterator end() const { return __table_.end(); }
744
745  _LIBCPP_HIDE_FROM_ABI iterator insert(const value_type& __x) { return __table_.__insert_multi(__x); }
746  _LIBCPP_HIDE_FROM_ABI iterator insert(const_iterator, const value_type& __x) { return insert(__x); }
747  template <class _InputIterator>
748  _LIBCPP_HIDE_FROM_ABI void insert(_InputIterator __first, _InputIterator __last);
749
750  _LIBCPP_HIDE_FROM_ABI void erase(const_iterator __p) { __table_.erase(__p.__i_); }
751  _LIBCPP_HIDE_FROM_ABI size_type erase(const key_type& __k) { return __table_.__erase_multi(__k); }
752  _LIBCPP_HIDE_FROM_ABI void erase(const_iterator __first, const_iterator __last) {
753    __table_.erase(__first.__i_, __last.__i_);
754  }
755  _LIBCPP_HIDE_FROM_ABI void clear() { __table_.clear(); }
756
757  _LIBCPP_HIDE_FROM_ABI void swap(hash_multimap& __u) { __table_.swap(__u.__table_); }
758
759  _LIBCPP_HIDE_FROM_ABI hasher hash_funct() const { return __table_.hash_function().hash_function(); }
760  _LIBCPP_HIDE_FROM_ABI key_equal key_eq() const { return __table_.key_eq().key_eq(); }
761
762  _LIBCPP_HIDE_FROM_ABI iterator find(const key_type& __k) { return __table_.find(__k); }
763  _LIBCPP_HIDE_FROM_ABI const_iterator find(const key_type& __k) const { return __table_.find(__k); }
764  _LIBCPP_HIDE_FROM_ABI size_type count(const key_type& __k) const { return __table_.__count_multi(__k); }
765  _LIBCPP_HIDE_FROM_ABI std::pair<iterator, iterator> equal_range(const key_type& __k) {
766    return __table_.__equal_range_multi(__k);
767  }
768  _LIBCPP_HIDE_FROM_ABI std::pair<const_iterator, const_iterator> equal_range(const key_type& __k) const {
769    return __table_.__equal_range_multi(__k);
770  }
771
772  _LIBCPP_HIDE_FROM_ABI size_type bucket_count() const { return __table_.bucket_count(); }
773  _LIBCPP_HIDE_FROM_ABI size_type max_bucket_count() const { return __table_.max_bucket_count(); }
774
775  _LIBCPP_HIDE_FROM_ABI size_type elems_in_bucket(size_type __n) const { return __table_.bucket_size(__n); }
776
777  _LIBCPP_HIDE_FROM_ABI void resize(size_type __n) { __table_.__rehash_multi(__n); }
778};
779
780template <class _Key, class _Tp, class _Hash, class _Pred, class _Alloc>
781hash_multimap<_Key, _Tp, _Hash, _Pred, _Alloc>::hash_multimap(size_type __n, const hasher& __hf, const key_equal& __eql)
782    : __table_(__hf, __eql) {
783  __table_.__rehash_multi(__n);
784}
785
786template <class _Key, class _Tp, class _Hash, class _Pred, class _Alloc>
787hash_multimap<_Key, _Tp, _Hash, _Pred, _Alloc>::hash_multimap(
788    size_type __n, const hasher& __hf, const key_equal& __eql, const allocator_type& __a)
789    : __table_(__hf, __eql, __a) {
790  __table_.__rehash_multi(__n);
791}
792
793template <class _Key, class _Tp, class _Hash, class _Pred, class _Alloc>
794template <class _InputIterator>
795hash_multimap<_Key, _Tp, _Hash, _Pred, _Alloc>::hash_multimap(_InputIterator __first, _InputIterator __last) {
796  insert(__first, __last);
797}
798
799template <class _Key, class _Tp, class _Hash, class _Pred, class _Alloc>
800template <class _InputIterator>
801hash_multimap<_Key, _Tp, _Hash, _Pred, _Alloc>::hash_multimap(
802    _InputIterator __first, _InputIterator __last, size_type __n, const hasher& __hf, const key_equal& __eql)
803    : __table_(__hf, __eql) {
804  __table_.__rehash_multi(__n);
805  insert(__first, __last);
806}
807
808template <class _Key, class _Tp, class _Hash, class _Pred, class _Alloc>
809template <class _InputIterator>
810hash_multimap<_Key, _Tp, _Hash, _Pred, _Alloc>::hash_multimap(
811    _InputIterator __first,
812    _InputIterator __last,
813    size_type __n,
814    const hasher& __hf,
815    const key_equal& __eql,
816    const allocator_type& __a)
817    : __table_(__hf, __eql, __a) {
818  __table_.__rehash_multi(__n);
819  insert(__first, __last);
820}
821
822template <class _Key, class _Tp, class _Hash, class _Pred, class _Alloc>
823hash_multimap<_Key, _Tp, _Hash, _Pred, _Alloc>::hash_multimap(const hash_multimap& __u) : __table_(__u.__table_) {
824  __table_.__rehash_multi(__u.bucket_count());
825  insert(__u.begin(), __u.end());
826}
827
828template <class _Key, class _Tp, class _Hash, class _Pred, class _Alloc>
829template <class _InputIterator>
830inline void hash_multimap<_Key, _Tp, _Hash, _Pred, _Alloc>::insert(_InputIterator __first, _InputIterator __last) {
831  for (; __first != __last; ++__first)
832    __table_.__insert_multi(*__first);
833}
834
835template <class _Key, class _Tp, class _Hash, class _Pred, class _Alloc>
836inline _LIBCPP_HIDE_FROM_ABI void
837swap(hash_multimap<_Key, _Tp, _Hash, _Pred, _Alloc>& __x, hash_multimap<_Key, _Tp, _Hash, _Pred, _Alloc>& __y) {
838  __x.swap(__y);
839}
840
841template <class _Key, class _Tp, class _Hash, class _Pred, class _Alloc>
842_LIBCPP_HIDE_FROM_ABI bool operator==(const hash_multimap<_Key, _Tp, _Hash, _Pred, _Alloc>& __x,
843                                      const hash_multimap<_Key, _Tp, _Hash, _Pred, _Alloc>& __y) {
844  if (__x.size() != __y.size())
845    return false;
846  typedef typename hash_multimap<_Key, _Tp, _Hash, _Pred, _Alloc>::const_iterator const_iterator;
847  typedef std::pair<const_iterator, const_iterator> _EqRng;
848  for (const_iterator __i = __x.begin(), __ex = __x.end(); __i != __ex;) {
849    _EqRng __xeq = __x.equal_range(__i->first);
850    _EqRng __yeq = __y.equal_range(__i->first);
851    if (std::distance(__xeq.first, __xeq.second) != std::distance(__yeq.first, __yeq.second) ||
852        !std::is_permutation(__xeq.first, __xeq.second, __yeq.first))
853      return false;
854    __i = __xeq.second;
855  }
856  return true;
857}
858
859template <class _Key, class _Tp, class _Hash, class _Pred, class _Alloc>
860inline _LIBCPP_HIDE_FROM_ABI bool operator!=(const hash_multimap<_Key, _Tp, _Hash, _Pred, _Alloc>& __x,
861                                             const hash_multimap<_Key, _Tp, _Hash, _Pred, _Alloc>& __y) {
862  return !(__x == __y);
863}
864
865} // namespace __gnu_cxx
866
867#if !defined(_LIBCPP_REMOVE_TRANSITIVE_INCLUDES) && _LIBCPP_STD_VER <= 20
868#  include <concepts>
869#  include <iterator>
870#  include <type_traits>
871#endif
872
873#endif // _LIBCPP_HASH_MAP
874