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#include <stdexcept>
211#include <type_traits>
212
213#if defined(__DEPRECATED) && __DEPRECATED
214#if defined(_LIBCPP_WARNING)
215    _LIBCPP_WARNING("Use of the header <ext/hash_map> is deprecated.  Migrate to <unordered_map>")
216#else
217#   warning Use of the header <ext/hash_map> is deprecated.  Migrate to <unordered_map>
218#endif
219#endif
220
221#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
222#  pragma GCC system_header
223#endif
224
225namespace __gnu_cxx {
226
227template <class _Tp, class _Hash,
228          bool = std::is_empty<_Hash>::value && !std::__libcpp_is_final<_Hash>::value
229        >
230class __hash_map_hasher
231    : private _Hash
232{
233public:
234    _LIBCPP_INLINE_VISIBILITY __hash_map_hasher() : _Hash() {}
235    _LIBCPP_INLINE_VISIBILITY __hash_map_hasher(const _Hash& __h) : _Hash(__h) {}
236    _LIBCPP_INLINE_VISIBILITY const _Hash& hash_function() const {return *this;}
237    _LIBCPP_INLINE_VISIBILITY
238    size_t operator()(const _Tp& __x) const
239        {return static_cast<const _Hash&>(*this)(__x.first);}
240    _LIBCPP_INLINE_VISIBILITY
241    size_t operator()(const typename _Tp::first_type& __x) const
242        {return static_cast<const _Hash&>(*this)(__x);}
243};
244
245template <class _Tp, class _Hash>
246class __hash_map_hasher<_Tp, _Hash, false>
247{
248    _Hash __hash_;
249public:
250    _LIBCPP_INLINE_VISIBILITY __hash_map_hasher() : __hash_() {}
251    _LIBCPP_INLINE_VISIBILITY __hash_map_hasher(const _Hash& __h) : __hash_(__h) {}
252    _LIBCPP_INLINE_VISIBILITY const _Hash& hash_function() const {return __hash_;}
253    _LIBCPP_INLINE_VISIBILITY
254    size_t operator()(const _Tp& __x) const
255        {return __hash_(__x.first);}
256    _LIBCPP_INLINE_VISIBILITY
257    size_t operator()(const typename _Tp::first_type& __x) const
258        {return __hash_(__x);}
259};
260
261template <class _Tp, class _Pred,
262          bool = std::is_empty<_Pred>::value && !std::__libcpp_is_final<_Pred>::value
263         >
264class __hash_map_equal
265    : private _Pred
266{
267public:
268    _LIBCPP_INLINE_VISIBILITY __hash_map_equal() : _Pred() {}
269    _LIBCPP_INLINE_VISIBILITY __hash_map_equal(const _Pred& __p) : _Pred(__p) {}
270    _LIBCPP_INLINE_VISIBILITY const _Pred& key_eq() const {return *this;}
271    _LIBCPP_INLINE_VISIBILITY
272    bool operator()(const _Tp& __x, const _Tp& __y) const
273        {return static_cast<const _Pred&>(*this)(__x.first, __y.first);}
274    _LIBCPP_INLINE_VISIBILITY
275    bool operator()(const typename _Tp::first_type& __x, const _Tp& __y) const
276        {return static_cast<const _Pred&>(*this)(__x, __y.first);}
277    _LIBCPP_INLINE_VISIBILITY
278    bool operator()(const _Tp& __x, const typename _Tp::first_type& __y) const
279        {return static_cast<const _Pred&>(*this)(__x.first, __y);}
280    _LIBCPP_INLINE_VISIBILITY
281    bool operator()(const typename _Tp::first_type& __x,
282                    const typename _Tp::first_type& __y) const
283        {return static_cast<const _Pred&>(*this)(__x, __y);}
284};
285
286template <class _Tp, class _Pred>
287class __hash_map_equal<_Tp, _Pred, false>
288{
289    _Pred __pred_;
290public:
291    _LIBCPP_INLINE_VISIBILITY __hash_map_equal() : __pred_() {}
292    _LIBCPP_INLINE_VISIBILITY __hash_map_equal(const _Pred& __p) : __pred_(__p) {}
293    _LIBCPP_INLINE_VISIBILITY const _Pred& key_eq() const {return __pred_;}
294    _LIBCPP_INLINE_VISIBILITY
295    bool operator()(const _Tp& __x, const _Tp& __y) const
296        {return __pred_(__x.first, __y.first);}
297    _LIBCPP_INLINE_VISIBILITY
298    bool operator()(const typename _Tp::first_type& __x, const _Tp& __y) const
299        {return __pred_(__x, __y.first);}
300    _LIBCPP_INLINE_VISIBILITY
301    bool operator()(const _Tp& __x, const typename _Tp::first_type& __y) const
302        {return __pred_(__x.first, __y);}
303    _LIBCPP_INLINE_VISIBILITY
304    bool operator()(const typename _Tp::first_type& __x,
305                    const typename _Tp::first_type& __y) const
306        {return __pred_(__x, __y);}
307};
308
309template <class _Alloc>
310class __hash_map_node_destructor
311{
312    typedef _Alloc                              allocator_type;
313    typedef std::allocator_traits<allocator_type>    __alloc_traits;
314    typedef typename __alloc_traits::value_type::__node_value_type value_type;
315public:
316    typedef typename __alloc_traits::pointer    pointer;
317private:
318    typedef typename value_type::first_type     first_type;
319    typedef typename value_type::second_type    second_type;
320
321    allocator_type& __na_;
322
323public:
324    bool __first_constructed;
325    bool __second_constructed;
326
327    __hash_map_node_destructor(__hash_map_node_destructor const&) = default;
328    __hash_map_node_destructor& operator=(const __hash_map_node_destructor&) = delete;
329
330    _LIBCPP_INLINE_VISIBILITY
331    explicit __hash_map_node_destructor(allocator_type& __na)
332        : __na_(__na),
333          __first_constructed(false),
334          __second_constructed(false)
335        {}
336
337#ifndef _LIBCPP_CXX03_LANG
338    _LIBCPP_INLINE_VISIBILITY
339    __hash_map_node_destructor(std::__hash_node_destructor<allocator_type>&& __x)
340        : __na_(__x.__na_),
341          __first_constructed(__x.__value_constructed),
342          __second_constructed(__x.__value_constructed)
343        {
344            __x.__value_constructed = false;
345        }
346#else  // _LIBCPP_CXX03_LANG
347    _LIBCPP_INLINE_VISIBILITY
348    __hash_map_node_destructor(const std::__hash_node_destructor<allocator_type>& __x)
349        : __na_(__x.__na_),
350          __first_constructed(__x.__value_constructed),
351          __second_constructed(__x.__value_constructed)
352        {
353            const_cast<bool&>(__x.__value_constructed) = false;
354        }
355#endif // _LIBCPP_CXX03_LANG
356
357    _LIBCPP_INLINE_VISIBILITY
358    void operator()(pointer __p)
359    {
360        if (__second_constructed)
361            __alloc_traits::destroy(__na_, _VSTD::addressof(__p->__value_.second));
362        if (__first_constructed)
363            __alloc_traits::destroy(__na_, _VSTD::addressof(__p->__value_.first));
364        if (__p)
365            __alloc_traits::deallocate(__na_, __p, 1);
366    }
367};
368
369template <class _HashIterator>
370class _LIBCPP_TEMPLATE_VIS __hash_map_iterator
371{
372    _HashIterator __i_;
373
374    typedef const typename _HashIterator::value_type::first_type key_type;
375    typedef typename _HashIterator::value_type::second_type      mapped_type;
376public:
377    typedef std::forward_iterator_tag                            iterator_category;
378    typedef std::pair<key_type, mapped_type>                     value_type;
379    typedef typename _HashIterator::difference_type              difference_type;
380    typedef value_type&                                          reference;
381    typedef typename std::__rebind_pointer<typename _HashIterator::pointer, value_type>::type
382        pointer;
383
384    _LIBCPP_INLINE_VISIBILITY __hash_map_iterator() {}
385
386    _LIBCPP_INLINE_VISIBILITY __hash_map_iterator(_HashIterator __i) : __i_(__i) {}
387
388    _LIBCPP_INLINE_VISIBILITY reference operator*() const {return *operator->();}
389    _LIBCPP_INLINE_VISIBILITY pointer operator->() const {return (pointer)__i_.operator->();}
390
391    _LIBCPP_INLINE_VISIBILITY __hash_map_iterator& operator++() {++__i_; return *this;}
392    _LIBCPP_INLINE_VISIBILITY
393    __hash_map_iterator operator++(int)
394    {
395        __hash_map_iterator __t(*this);
396        ++(*this);
397        return __t;
398    }
399
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_;}
406
407    template <class, class, class, class, class> friend class _LIBCPP_TEMPLATE_VIS hash_map;
408    template <class, class, class, class, class> friend class _LIBCPP_TEMPLATE_VIS hash_multimap;
409    template <class> friend class _LIBCPP_TEMPLATE_VIS __hash_const_iterator;
410    template <class> friend class _LIBCPP_TEMPLATE_VIS __hash_const_local_iterator;
411    template <class> friend class _LIBCPP_TEMPLATE_VIS __hash_map_const_iterator;
412};
413
414template <class _HashIterator>
415class _LIBCPP_TEMPLATE_VIS __hash_map_const_iterator
416{
417    _HashIterator __i_;
418
419    typedef const typename _HashIterator::value_type::first_type key_type;
420    typedef typename _HashIterator::value_type::second_type      mapped_type;
421public:
422    typedef std::forward_iterator_tag                            iterator_category;
423    typedef std::pair<key_type, mapped_type>                     value_type;
424    typedef typename _HashIterator::difference_type              difference_type;
425    typedef const value_type&                                    reference;
426    typedef typename std::__rebind_pointer<typename _HashIterator::pointer, const value_type>::type
427        pointer;
428
429    _LIBCPP_INLINE_VISIBILITY __hash_map_const_iterator() {}
430
431    _LIBCPP_INLINE_VISIBILITY
432    __hash_map_const_iterator(_HashIterator __i) : __i_(__i) {}
433    _LIBCPP_INLINE_VISIBILITY
434    __hash_map_const_iterator(
435            __hash_map_iterator<typename _HashIterator::__non_const_iterator> __i)
436                : __i_(__i.__i_) {}
437
438    _LIBCPP_INLINE_VISIBILITY
439    reference operator*() const {return *operator->();}
440    _LIBCPP_INLINE_VISIBILITY
441    pointer operator->() const {return (pointer)__i_.operator->();}
442
443    _LIBCPP_INLINE_VISIBILITY
444    __hash_map_const_iterator& operator++() {++__i_; return *this;}
445    _LIBCPP_INLINE_VISIBILITY
446    __hash_map_const_iterator operator++(int)
447    {
448        __hash_map_const_iterator __t(*this);
449        ++(*this);
450        return __t;
451    }
452
453    friend _LIBCPP_INLINE_VISIBILITY
454    bool operator==(const __hash_map_const_iterator& __x, const __hash_map_const_iterator& __y)
455        {return __x.__i_ == __y.__i_;}
456    friend _LIBCPP_INLINE_VISIBILITY
457    bool operator!=(const __hash_map_const_iterator& __x, const __hash_map_const_iterator& __y)
458        {return __x.__i_ != __y.__i_;}
459
460    template <class, class, class, class, class> friend class _LIBCPP_TEMPLATE_VIS hash_map;
461    template <class, class, class, class, class> friend class _LIBCPP_TEMPLATE_VIS hash_multimap;
462    template <class> friend class _LIBCPP_TEMPLATE_VIS __hash_const_iterator;
463    template <class> friend class _LIBCPP_TEMPLATE_VIS __hash_const_local_iterator;
464};
465
466template <class _Key, class _Tp, class _Hash = hash<_Key>, class _Pred = std::equal_to<_Key>,
467          class _Alloc = std::allocator<std::pair<const _Key, _Tp> > >
468class _LIBCPP_TEMPLATE_VIS hash_map
469{
470public:
471    // types
472    typedef _Key                                           key_type;
473    typedef _Tp                                            mapped_type;
474    typedef _Tp                                            data_type;
475    typedef _Hash                                          hasher;
476    typedef _Pred                                          key_equal;
477    typedef _Alloc                                         allocator_type;
478    typedef std::pair<const key_type, mapped_type>         value_type;
479    typedef value_type&                                    reference;
480    typedef const value_type&                              const_reference;
481
482private:
483    typedef std::pair<key_type, mapped_type>                    __value_type;
484    typedef __hash_map_hasher<__value_type, hasher>   __hasher;
485    typedef __hash_map_equal<__value_type, key_equal> __key_equal;
486    typedef typename std::__rebind_alloc_helper<
487        std::allocator_traits<allocator_type>, __value_type>::type __allocator_type;
488
489    typedef std::__hash_table<__value_type, __hasher,
490                         __key_equal,  __allocator_type>   __table;
491
492    __table __table_;
493
494    typedef typename __table::__node_pointer               __node_pointer;
495    typedef typename __table::__node_const_pointer         __node_const_pointer;
496    typedef typename __table::__node_traits                __node_traits;
497    typedef typename __table::__node_allocator             __node_allocator;
498    typedef typename __table::__node                       __node;
499    typedef __hash_map_node_destructor<__node_allocator>   _Dp;
500    typedef std::unique_ptr<__node, _Dp>                   __node_holder;
501    typedef std::allocator_traits<allocator_type>          __alloc_traits;
502public:
503    typedef typename __alloc_traits::pointer         pointer;
504    typedef typename __alloc_traits::const_pointer   const_pointer;
505    typedef typename __alloc_traits::size_type       size_type;
506    typedef typename __alloc_traits::difference_type difference_type;
507
508    typedef __hash_map_iterator<typename __table::iterator>       iterator;
509    typedef __hash_map_const_iterator<typename __table::const_iterator> const_iterator;
510
511    _LIBCPP_INLINE_VISIBILITY hash_map() { }
512    explicit hash_map(size_type __n, const hasher& __hf = hasher(),
513                           const key_equal& __eql = key_equal());
514    hash_map(size_type __n, const hasher& __hf,
515                  const key_equal& __eql,
516                  const allocator_type& __a);
517    template <class _InputIterator>
518        hash_map(_InputIterator __first, _InputIterator __last);
519    template <class _InputIterator>
520        hash_map(_InputIterator __first, _InputIterator __last,
521                      size_type __n, const hasher& __hf = hasher(),
522                      const key_equal& __eql = key_equal());
523    template <class _InputIterator>
524        hash_map(_InputIterator __first, _InputIterator __last,
525                      size_type __n, const hasher& __hf,
526                      const key_equal& __eql,
527                      const allocator_type& __a);
528    hash_map(const hash_map& __u);
529
530    _LIBCPP_INLINE_VISIBILITY
531    allocator_type get_allocator() const
532        {return allocator_type(__table_.__node_alloc());}
533
534    _LIBCPP_INLINE_VISIBILITY
535    bool      empty() const {return __table_.size() == 0;}
536    _LIBCPP_INLINE_VISIBILITY
537    size_type size() const  {return __table_.size();}
538    _LIBCPP_INLINE_VISIBILITY
539    size_type max_size() const {return __table_.max_size();}
540
541    _LIBCPP_INLINE_VISIBILITY
542    iterator       begin()        {return __table_.begin();}
543    _LIBCPP_INLINE_VISIBILITY
544    iterator       end()          {return __table_.end();}
545    _LIBCPP_INLINE_VISIBILITY
546    const_iterator begin()  const {return __table_.begin();}
547    _LIBCPP_INLINE_VISIBILITY
548    const_iterator end()    const {return __table_.end();}
549
550    _LIBCPP_INLINE_VISIBILITY
551    std::pair<iterator, bool> insert(const value_type& __x)
552        {return __table_.__insert_unique(__x);}
553    _LIBCPP_INLINE_VISIBILITY
554    iterator insert(const_iterator, const value_type& __x) {return insert(__x).first;}
555    template <class _InputIterator>
556        _LIBCPP_INLINE_VISIBILITY
557        void insert(_InputIterator __first, _InputIterator __last);
558
559    _LIBCPP_INLINE_VISIBILITY
560    void erase(const_iterator __p) {__table_.erase(__p.__i_);}
561    _LIBCPP_INLINE_VISIBILITY
562    size_type erase(const key_type& __k) {return __table_.__erase_unique(__k);}
563    _LIBCPP_INLINE_VISIBILITY
564    void erase(const_iterator __first, const_iterator __last)
565        {__table_.erase(__first.__i_, __last.__i_);}
566    _LIBCPP_INLINE_VISIBILITY
567    void clear() {__table_.clear();}
568
569    _LIBCPP_INLINE_VISIBILITY
570    void swap(hash_map& __u) {__table_.swap(__u.__table_);}
571
572    _LIBCPP_INLINE_VISIBILITY
573    hasher hash_funct() const
574        {return __table_.hash_function().hash_function();}
575    _LIBCPP_INLINE_VISIBILITY
576    key_equal key_eq() const
577        {return __table_.key_eq().key_eq();}
578
579    _LIBCPP_INLINE_VISIBILITY
580    iterator       find(const key_type& __k)       {return __table_.find(__k);}
581    _LIBCPP_INLINE_VISIBILITY
582    const_iterator find(const key_type& __k) const {return __table_.find(__k);}
583    _LIBCPP_INLINE_VISIBILITY
584    size_type count(const key_type& __k) const {return __table_.__count_unique(__k);}
585    _LIBCPP_INLINE_VISIBILITY
586    std::pair<iterator, iterator>             equal_range(const key_type& __k)
587        {return __table_.__equal_range_unique(__k);}
588    _LIBCPP_INLINE_VISIBILITY
589    std::pair<const_iterator, const_iterator> equal_range(const key_type& __k) const
590        {return __table_.__equal_range_unique(__k);}
591
592    mapped_type& operator[](const key_type& __k);
593
594    _LIBCPP_INLINE_VISIBILITY
595    size_type bucket_count() const {return __table_.bucket_count();}
596    _LIBCPP_INLINE_VISIBILITY
597    size_type max_bucket_count() const {return __table_.max_bucket_count();}
598
599    _LIBCPP_INLINE_VISIBILITY
600    size_type elems_in_bucket(size_type __n) const
601        {return __table_.bucket_size(__n);}
602
603    _LIBCPP_INLINE_VISIBILITY
604    void resize(size_type __n) {__table_.rehash(__n);}
605
606private:
607    __node_holder __construct_node(const key_type& __k);
608};
609
610template <class _Key, class _Tp, class _Hash, class _Pred, class _Alloc>
611hash_map<_Key, _Tp, _Hash, _Pred, _Alloc>::hash_map(
612        size_type __n, const hasher& __hf, const key_equal& __eql)
613    : __table_(__hf, __eql)
614{
615    __table_.rehash(__n);
616}
617
618template <class _Key, class _Tp, class _Hash, class _Pred, class _Alloc>
619hash_map<_Key, _Tp, _Hash, _Pred, _Alloc>::hash_map(
620        size_type __n, const hasher& __hf, const key_equal& __eql,
621        const allocator_type& __a)
622    : __table_(__hf, __eql, __a)
623{
624    __table_.rehash(__n);
625}
626
627template <class _Key, class _Tp, class _Hash, class _Pred, class _Alloc>
628template <class _InputIterator>
629hash_map<_Key, _Tp, _Hash, _Pred, _Alloc>::hash_map(
630        _InputIterator __first, _InputIterator __last)
631{
632    insert(__first, __last);
633}
634
635template <class _Key, class _Tp, class _Hash, class _Pred, class _Alloc>
636template <class _InputIterator>
637hash_map<_Key, _Tp, _Hash, _Pred, _Alloc>::hash_map(
638        _InputIterator __first, _InputIterator __last, size_type __n,
639        const hasher& __hf, const key_equal& __eql)
640    : __table_(__hf, __eql)
641{
642    __table_.rehash(__n);
643    insert(__first, __last);
644}
645
646template <class _Key, class _Tp, class _Hash, class _Pred, class _Alloc>
647template <class _InputIterator>
648hash_map<_Key, _Tp, _Hash, _Pred, _Alloc>::hash_map(
649        _InputIterator __first, _InputIterator __last, size_type __n,
650        const hasher& __hf, const key_equal& __eql, const allocator_type& __a)
651    : __table_(__hf, __eql, __a)
652{
653    __table_.rehash(__n);
654    insert(__first, __last);
655}
656
657template <class _Key, class _Tp, class _Hash, class _Pred, class _Alloc>
658hash_map<_Key, _Tp, _Hash, _Pred, _Alloc>::hash_map(
659        const hash_map& __u)
660    : __table_(__u.__table_)
661{
662    __table_.rehash(__u.bucket_count());
663    insert(__u.begin(), __u.end());
664}
665
666template <class _Key, class _Tp, class _Hash, class _Pred, class _Alloc>
667typename hash_map<_Key, _Tp, _Hash, _Pred, _Alloc>::__node_holder
668hash_map<_Key, _Tp, _Hash, _Pred, _Alloc>::__construct_node(const key_type& __k)
669{
670    __node_allocator& __na = __table_.__node_alloc();
671    __node_holder __h(__node_traits::allocate(__na, 1), _Dp(__na));
672    __node_traits::construct(__na, _VSTD::addressof(__h->__value_.first), __k);
673    __h.get_deleter().__first_constructed = true;
674    __node_traits::construct(__na, _VSTD::addressof(__h->__value_.second));
675    __h.get_deleter().__second_constructed = true;
676    return __h;
677}
678
679template <class _Key, class _Tp, class _Hash, class _Pred, class _Alloc>
680template <class _InputIterator>
681inline
682void
683hash_map<_Key, _Tp, _Hash, _Pred, _Alloc>::insert(_InputIterator __first,
684                                                       _InputIterator __last)
685{
686    for (; __first != __last; ++__first)
687        __table_.__insert_unique(*__first);
688}
689
690template <class _Key, class _Tp, class _Hash, class _Pred, class _Alloc>
691_Tp&
692hash_map<_Key, _Tp, _Hash, _Pred, _Alloc>::operator[](const key_type& __k)
693{
694    iterator __i = find(__k);
695    if (__i != end())
696        return __i->second;
697    __node_holder __h = __construct_node(__k);
698    std::pair<iterator, bool> __r = __table_.__node_insert_unique(__h.get());
699    __h.release();
700    return __r.first->second;
701}
702
703template <class _Key, class _Tp, class _Hash, class _Pred, class _Alloc>
704inline _LIBCPP_INLINE_VISIBILITY
705void
706swap(hash_map<_Key, _Tp, _Hash, _Pred, _Alloc>& __x,
707     hash_map<_Key, _Tp, _Hash, _Pred, _Alloc>& __y)
708{
709    __x.swap(__y);
710}
711
712template <class _Key, class _Tp, class _Hash, class _Pred, class _Alloc>
713bool
714operator==(const hash_map<_Key, _Tp, _Hash, _Pred, _Alloc>& __x,
715           const hash_map<_Key, _Tp, _Hash, _Pred, _Alloc>& __y)
716{
717    if (__x.size() != __y.size())
718        return false;
719    typedef typename hash_map<_Key, _Tp, _Hash, _Pred, _Alloc>::const_iterator
720                                                                 const_iterator;
721    for (const_iterator __i = __x.begin(), __ex = __x.end(), __ey = __y.end();
722            __i != __ex; ++__i)
723    {
724        const_iterator __j = __y.find(__i->first);
725        if (__j == __ey || !(*__i == *__j))
726            return false;
727    }
728    return true;
729}
730
731template <class _Key, class _Tp, class _Hash, class _Pred, class _Alloc>
732inline _LIBCPP_INLINE_VISIBILITY
733bool
734operator!=(const hash_map<_Key, _Tp, _Hash, _Pred, _Alloc>& __x,
735           const hash_map<_Key, _Tp, _Hash, _Pred, _Alloc>& __y)
736{
737    return !(__x == __y);
738}
739
740template <class _Key, class _Tp, class _Hash = hash<_Key>, class _Pred = std::equal_to<_Key>,
741          class _Alloc = std::allocator<std::pair<const _Key, _Tp> > >
742class _LIBCPP_TEMPLATE_VIS hash_multimap
743{
744public:
745    // types
746    typedef _Key                                           key_type;
747    typedef _Tp                                            mapped_type;
748    typedef _Tp                                            data_type;
749    typedef _Hash                                          hasher;
750    typedef _Pred                                          key_equal;
751    typedef _Alloc                                         allocator_type;
752    typedef std::pair<const key_type, mapped_type>         value_type;
753    typedef value_type&                                    reference;
754    typedef const value_type&                              const_reference;
755
756private:
757    typedef std::pair<key_type, mapped_type>               __value_type;
758    typedef __hash_map_hasher<__value_type, hasher>   __hasher;
759    typedef __hash_map_equal<__value_type, key_equal> __key_equal;
760    typedef typename std::__rebind_alloc_helper<std::allocator_traits<allocator_type>, __value_type>::type __allocator_type;
761
762    typedef std::__hash_table<__value_type, __hasher,
763                         __key_equal,  __allocator_type>   __table;
764
765    __table __table_;
766
767    typedef typename __table::__node_traits                __node_traits;
768    typedef typename __table::__node_allocator             __node_allocator;
769    typedef typename __table::__node                       __node;
770    typedef __hash_map_node_destructor<__node_allocator>   _Dp;
771    typedef std::unique_ptr<__node, _Dp>                         __node_holder;
772    typedef std::allocator_traits<allocator_type>               __alloc_traits;
773public:
774    typedef typename __alloc_traits::pointer         pointer;
775    typedef typename __alloc_traits::const_pointer   const_pointer;
776    typedef typename __alloc_traits::size_type       size_type;
777    typedef typename __alloc_traits::difference_type difference_type;
778
779    typedef __hash_map_iterator<typename __table::iterator>       iterator;
780    typedef __hash_map_const_iterator<typename __table::const_iterator> const_iterator;
781
782    _LIBCPP_INLINE_VISIBILITY
783    hash_multimap() { }
784    explicit hash_multimap(size_type __n, const hasher& __hf = hasher(),
785                                const key_equal& __eql = key_equal());
786    hash_multimap(size_type __n, const hasher& __hf,
787                                const key_equal& __eql,
788                                const allocator_type& __a);
789    template <class _InputIterator>
790        hash_multimap(_InputIterator __first, _InputIterator __last);
791    template <class _InputIterator>
792        hash_multimap(_InputIterator __first, _InputIterator __last,
793                      size_type __n, const hasher& __hf = hasher(),
794                      const key_equal& __eql = key_equal());
795    template <class _InputIterator>
796        hash_multimap(_InputIterator __first, _InputIterator __last,
797                      size_type __n, const hasher& __hf,
798                      const key_equal& __eql,
799                      const allocator_type& __a);
800    hash_multimap(const hash_multimap& __u);
801
802    _LIBCPP_INLINE_VISIBILITY
803    allocator_type get_allocator() const
804        {return allocator_type(__table_.__node_alloc());}
805
806    _LIBCPP_INLINE_VISIBILITY
807    bool      empty() const {return __table_.size() == 0;}
808    _LIBCPP_INLINE_VISIBILITY
809    size_type size() const  {return __table_.size();}
810    _LIBCPP_INLINE_VISIBILITY
811    size_type max_size() const {return __table_.max_size();}
812
813    _LIBCPP_INLINE_VISIBILITY
814    iterator       begin()        {return __table_.begin();}
815    _LIBCPP_INLINE_VISIBILITY
816    iterator       end()          {return __table_.end();}
817    _LIBCPP_INLINE_VISIBILITY
818    const_iterator begin()  const {return __table_.begin();}
819    _LIBCPP_INLINE_VISIBILITY
820    const_iterator end()    const {return __table_.end();}
821
822    _LIBCPP_INLINE_VISIBILITY
823    iterator insert(const value_type& __x) {return __table_.__insert_multi(__x);}
824    _LIBCPP_INLINE_VISIBILITY
825    iterator insert(const_iterator, const value_type& __x) {return insert(__x);}
826    template <class _InputIterator>
827        _LIBCPP_INLINE_VISIBILITY
828        void insert(_InputIterator __first, _InputIterator __last);
829
830    _LIBCPP_INLINE_VISIBILITY
831    void erase(const_iterator __p) {__table_.erase(__p.__i_);}
832    _LIBCPP_INLINE_VISIBILITY
833    size_type erase(const key_type& __k) {return __table_.__erase_multi(__k);}
834    _LIBCPP_INLINE_VISIBILITY
835    void erase(const_iterator __first, const_iterator __last)
836        {__table_.erase(__first.__i_, __last.__i_);}
837    _LIBCPP_INLINE_VISIBILITY
838    void clear() {__table_.clear();}
839
840    _LIBCPP_INLINE_VISIBILITY
841    void swap(hash_multimap& __u) {__table_.swap(__u.__table_);}
842
843    _LIBCPP_INLINE_VISIBILITY
844    hasher hash_funct() const
845        {return __table_.hash_function().hash_function();}
846    _LIBCPP_INLINE_VISIBILITY
847    key_equal key_eq() const
848        {return __table_.key_eq().key_eq();}
849
850    _LIBCPP_INLINE_VISIBILITY
851    iterator       find(const key_type& __k)       {return __table_.find(__k);}
852    _LIBCPP_INLINE_VISIBILITY
853    const_iterator find(const key_type& __k) const {return __table_.find(__k);}
854    _LIBCPP_INLINE_VISIBILITY
855    size_type count(const key_type& __k) const {return __table_.__count_multi(__k);}
856    _LIBCPP_INLINE_VISIBILITY
857    std::pair<iterator, iterator>             equal_range(const key_type& __k)
858        {return __table_.__equal_range_multi(__k);}
859    _LIBCPP_INLINE_VISIBILITY
860    std::pair<const_iterator, const_iterator> equal_range(const key_type& __k) const
861        {return __table_.__equal_range_multi(__k);}
862
863    _LIBCPP_INLINE_VISIBILITY
864    size_type bucket_count() const {return __table_.bucket_count();}
865    _LIBCPP_INLINE_VISIBILITY
866    size_type max_bucket_count() const {return __table_.max_bucket_count();}
867
868    _LIBCPP_INLINE_VISIBILITY
869    size_type elems_in_bucket(size_type __n) const
870        {return __table_.bucket_size(__n);}
871
872    _LIBCPP_INLINE_VISIBILITY
873    void resize(size_type __n) {__table_.rehash(__n);}
874};
875
876template <class _Key, class _Tp, class _Hash, class _Pred, class _Alloc>
877hash_multimap<_Key, _Tp, _Hash, _Pred, _Alloc>::hash_multimap(
878        size_type __n, const hasher& __hf, const key_equal& __eql)
879    : __table_(__hf, __eql)
880{
881    __table_.rehash(__n);
882}
883
884template <class _Key, class _Tp, class _Hash, class _Pred, class _Alloc>
885hash_multimap<_Key, _Tp, _Hash, _Pred, _Alloc>::hash_multimap(
886        size_type __n, const hasher& __hf, const key_equal& __eql,
887        const allocator_type& __a)
888    : __table_(__hf, __eql, __a)
889{
890    __table_.rehash(__n);
891}
892
893template <class _Key, class _Tp, class _Hash, class _Pred, class _Alloc>
894template <class _InputIterator>
895hash_multimap<_Key, _Tp, _Hash, _Pred, _Alloc>::hash_multimap(
896        _InputIterator __first, _InputIterator __last)
897{
898    insert(__first, __last);
899}
900
901template <class _Key, class _Tp, class _Hash, class _Pred, class _Alloc>
902template <class _InputIterator>
903hash_multimap<_Key, _Tp, _Hash, _Pred, _Alloc>::hash_multimap(
904        _InputIterator __first, _InputIterator __last, size_type __n,
905        const hasher& __hf, const key_equal& __eql)
906    : __table_(__hf, __eql)
907{
908    __table_.rehash(__n);
909    insert(__first, __last);
910}
911
912template <class _Key, class _Tp, class _Hash, class _Pred, class _Alloc>
913template <class _InputIterator>
914hash_multimap<_Key, _Tp, _Hash, _Pred, _Alloc>::hash_multimap(
915        _InputIterator __first, _InputIterator __last, size_type __n,
916        const hasher& __hf, const key_equal& __eql, const allocator_type& __a)
917    : __table_(__hf, __eql, __a)
918{
919    __table_.rehash(__n);
920    insert(__first, __last);
921}
922
923template <class _Key, class _Tp, class _Hash, class _Pred, class _Alloc>
924hash_multimap<_Key, _Tp, _Hash, _Pred, _Alloc>::hash_multimap(
925        const hash_multimap& __u)
926    : __table_(__u.__table_)
927{
928    __table_.rehash(__u.bucket_count());
929    insert(__u.begin(), __u.end());
930}
931
932template <class _Key, class _Tp, class _Hash, class _Pred, class _Alloc>
933template <class _InputIterator>
934inline
935void
936hash_multimap<_Key, _Tp, _Hash, _Pred, _Alloc>::insert(_InputIterator __first,
937                                                            _InputIterator __last)
938{
939    for (; __first != __last; ++__first)
940        __table_.__insert_multi(*__first);
941}
942
943template <class _Key, class _Tp, class _Hash, class _Pred, class _Alloc>
944inline _LIBCPP_INLINE_VISIBILITY
945void
946swap(hash_multimap<_Key, _Tp, _Hash, _Pred, _Alloc>& __x,
947     hash_multimap<_Key, _Tp, _Hash, _Pred, _Alloc>& __y)
948{
949    __x.swap(__y);
950}
951
952template <class _Key, class _Tp, class _Hash, class _Pred, class _Alloc>
953bool
954operator==(const hash_multimap<_Key, _Tp, _Hash, _Pred, _Alloc>& __x,
955           const hash_multimap<_Key, _Tp, _Hash, _Pred, _Alloc>& __y)
956{
957    if (__x.size() != __y.size())
958        return false;
959    typedef typename hash_multimap<_Key, _Tp, _Hash, _Pred, _Alloc>::const_iterator
960                                                                 const_iterator;
961    typedef std::pair<const_iterator, const_iterator> _EqRng;
962    for (const_iterator __i = __x.begin(), __ex = __x.end(); __i != __ex;)
963    {
964        _EqRng __xeq = __x.equal_range(__i->first);
965        _EqRng __yeq = __y.equal_range(__i->first);
966        if (_VSTD::distance(__xeq.first, __xeq.second) !=
967            _VSTD::distance(__yeq.first, __yeq.second) ||
968                  !_VSTD::is_permutation(__xeq.first, __xeq.second, __yeq.first))
969            return false;
970        __i = __xeq.second;
971    }
972    return true;
973}
974
975template <class _Key, class _Tp, class _Hash, class _Pred, class _Alloc>
976inline _LIBCPP_INLINE_VISIBILITY
977bool
978operator!=(const hash_multimap<_Key, _Tp, _Hash, _Pred, _Alloc>& __x,
979           const hash_multimap<_Key, _Tp, _Hash, _Pred, _Alloc>& __y)
980{
981    return !(__x == __y);
982}
983
984} // namespace __gnu_cxx
985
986#endif // _LIBCPP_HASH_MAP
987