1#ifndef MSTL_CORE_CONTAINER_UNORDERED_MULTISET_HPP__
2#define MSTL_CORE_CONTAINER_UNORDERED_MULTISET_HPP__
3#include "hashtable.hpp"
6template <
typename Value,
typename HashFcn = hash<Value>,
typename EqualKey = equal_to<Value>,
7 typename Alloc = allocator<hashtable_node<Value>>>
8class unordered_multiset :
icollector<unordered_multiset<Value, HashFcn, EqualKey, Alloc>> {
9#ifdef MSTL_STANDARD_20__
10 static_assert(is_hash_v<HashFcn, Value>,
"unordered multiset requires valid hash function.");
11 static_assert(is_allocator_v<Alloc>,
"Alloc type is not a standard allocator type.");
13 static_assert(is_same_v<hashtable_node<Value>,
typename Alloc::value_type>,
14 "allocator type mismatch.");
15 static_assert(is_object_v<Value>,
"unordered multiset only contains object types.");
17 using base_type = hashtable<Value, Value, HashFcn, identity<Value>, EqualKey, Alloc>;
20 using key_type =
typename base_type::key_type;
21 using value_type =
typename base_type::value_type;
22 using hasher =
typename base_type::hasher;
23 using key_equal =
typename base_type::key_equal;
25 using size_type =
typename base_type::size_type;
26 using difference_type =
typename base_type::difference_type;
27 using pointer =
typename base_type::const_pointer;
28 using const_pointer =
typename base_type::const_pointer;
29 using reference =
typename base_type::const_reference;
30 using const_reference =
typename base_type::const_reference;
32 using iterator =
typename base_type::const_iterator;
33 using const_iterator =
typename base_type::const_iterator;
35 using allocator_type =
typename base_type::allocator_type;
41 unordered_multiset() : ht_(100, hasher(), key_equal()) {}
42 explicit unordered_multiset(size_type n) : ht_(n) {}
44 unordered_multiset(size_type n,
const hasher& hf) : ht_(n, hf, key_equal()) {}
45 unordered_multiset(size_type n,
const hasher& hf,
const key_equal& eql) : ht_(n, hf, eql) {}
47 unordered_multiset(
const unordered_multiset& ht) : ht_(ht.ht_) {}
48 unordered_multiset& operator =(
const unordered_multiset& x) =
default;
50 unordered_multiset(unordered_multiset&& x)
noexcept(
noexcept(ht_.swap(x.ht_))) : ht_(
_MSTL forward<base_type>(x.ht_)) {}
51 unordered_multiset& operator =(unordered_multiset&& x)
noexcept(
noexcept(ht_.swap(x.ht_))) {
56 template <
typename Iterator>
57 unordered_multiset(Iterator first, Iterator last) : ht_(100, hasher(), key_equal()) {
58 ht_.insert_equal(first, last);
60 template <
typename Iterator>
61 unordered_multiset(Iterator first, Iterator list, size_type n) : ht_(n, hasher(), key_equal()) {
62 ht_.insert_equal(first, list);
64 template <
typename Iterator>
65 unordered_multiset(Iterator first, Iterator last, size_type n,
const hasher& hf) : ht_(n, hf, key_equal()) {
66 ht_.insert_equal(first, last);
68 template <
typename Iterator>
69 unordered_multiset(Iterator first, Iterator last, size_type n,
const hasher& hf,
const key_equal& eql)
71 ht_.insert_equal(first, last);
74 unordered_multiset(std::initializer_list<value_type> l)
75 : unordered_multiset(l.begin(), l.end()) {}
76 unordered_multiset(std::initializer_list<value_type> l, size_type n)
77 : unordered_multiset(l.begin(), l.end(), n) {}
78 unordered_multiset(std::initializer_list<value_type> l, size_type n,
const hasher& hf)
79 : unordered_multiset(l.begin(), l.end(), n, hf) {}
80 unordered_multiset(std::initializer_list<value_type> l, size_type n,
const hasher& hf,
const key_equal& eql)
81 : unordered_multiset(l.begin(), l.end(), n, hf, eql) {}
83 MSTL_NODISCARD iterator begin() noexcept {
return ht_.begin(); }
84 MSTL_NODISCARD iterator end() noexcept {
return ht_.end(); }
85 MSTL_NODISCARD const_iterator begin() const noexcept {
return ht_.begin(); }
86 MSTL_NODISCARD const_iterator end() const noexcept {
return ht_.end(); }
87 MSTL_NODISCARD const_iterator cbegin() const noexcept {
return ht_.cbegin(); }
88 MSTL_NODISCARD const_iterator cend() const noexcept {
return ht_.cend(); }
90 MSTL_NODISCARD size_type size() const noexcept {
return ht_.size(); }
91 MSTL_NODISCARD size_type max_size() const noexcept {
return ht_.max_size(); }
92 MSTL_NODISCARD
bool empty() const noexcept {
return ht_.empty(); }
94 MSTL_NODISCARD size_type count(
const key_type& key)
const noexcept(
noexcept(ht_.count(key))) {
95 return ht_.count(key);
97 MSTL_NODISCARD size_type bucket_count() const noexcept {
return ht_.bucket_count(); }
98 MSTL_NODISCARD size_type max_bucket_count() const noexcept {
return ht_.max_bucket_count(); }
99 MSTL_NODISCARD size_type bucket_size(size_type n)
const noexcept {
return ht_.bucket_size(n); }
101 MSTL_NODISCARD allocator_type get_allocator() const noexcept {
return allocator_type(); }
103 MSTL_NODISCARD hasher hash_funct() const noexcept(noexcept(ht_.hash_func())) {
return ht_.hash_func(); }
104 MSTL_NODISCARD key_equal key_eq() const noexcept(noexcept(ht_.key_eql())) {
return ht_.key_eql(); }
106 MSTL_NODISCARD
float load_factor() const noexcept {
return ht_.load_factor(); }
107 MSTL_NODISCARD
float max_load_factor() const noexcept {
return ht_.max_load_factor(); }
108 void max_load_factor(
float new_max)
noexcept { ht_.max_load_factor(new_max); }
110 void rehash(size_type new_size) { ht_.rehash(new_size); }
111 void reserve(size_type max_count) { ht_.reserve(max_count); }
113 template <
typename... Args>
114 iterator emplace(Args&&... args) {
118 iterator insert(
const value_type& obj) {
119 return ht_.insert_equal(obj);
121 iterator insert(value_type&& obj) {
124 template <
typename Iterator>
125 void insert(Iterator first, Iterator last) { ht_.insert_equal(first, last); }
127 size_type erase(
const key_type& key)
noexcept {
return ht_.erase(key); }
128 void erase(iterator it)
noexcept { ht_.erase(it); }
129 void erase(iterator first, iterator last)
noexcept { ht_.erase(first, last); }
130 void clear() noexcept { ht_.clear(); }
132 void swap(unordered_multiset& x)
noexcept(
noexcept(ht_.swap(x.ht_))) { ht_.swap(x.ht_); }
134 MSTL_NODISCARD iterator find(
const key_type& key) {
return ht_.find(key); }
135 MSTL_NODISCARD const_iterator find(
const key_type& key)
const {
return ht_.find(key); }
137 MSTL_NODISCARD pair<iterator, iterator> equal_range(
const key_type& key) {
return ht_.equal_range(key); }
138 MSTL_NODISCARD pair<const_iterator, const_iterator> equal_range(
const key_type& key)
const {
139 return ht_.equal_range(key);
142 MSTL_NODISCARD
bool operator ==(
const unordered_multiset& rhs)
const
143 noexcept(
noexcept(ht_ == rhs.ht_)) {
144 return ht_ == rhs.ht_;
146 MSTL_NODISCARD
bool operator <(
const unordered_multiset& rhs)
const
147 noexcept(
noexcept(ht_ < rhs.ht_)) {
148 return ht_ < rhs.ht_;
151#if MSTL_SUPPORT_DEDUCTION_GUIDES__
152template <
typename Iterator,
typename HashFcn = hash<iter_value_t<Iterator>>,
typename Compare
153 = equal_to<iter_value_t<Iterator>>,
typename Alloc = allocator<iter_value_t<Iterator>>>
154unordered_multiset(Iterator, Iterator, HashFcn = HashFcn(), Compare = Compare(), Alloc = Alloc())
155-> unordered_multiset<iter_value_t<Iterator>, HashFcn, Compare, Alloc>;
157template <
typename Value,
typename HashFcn = hash<Value>,
typename Compare = equal_to<Value>,
158 typename Alloc = allocator<Value>>
159unordered_multiset(std::initializer_list<Value>, HashFcn = HashFcn(), Compare = Compare(), Alloc = Alloc())
160-> unordered_multiset<Value, HashFcn, Compare, Alloc>;
162template <
typename Iterator,
typename Alloc>
163unordered_multiset(Iterator, Iterator, Alloc)
166template <
typename Iterator,
typename HashFcn,
typename Alloc>
167unordered_multiset(Iterator, Iterator, HashFcn, Alloc)
170template <
typename Value,
typename Alloc>
171unordered_multiset(std::initializer_list<Value>, Alloc)
174template <
typename Value,
typename HashFcn,
typename Alloc>
175unordered_multiset(std::initializer_list<Value>, HashFcn, Alloc)
176-> unordered_multiset<Value, HashFcn, equal_to<Value>, Alloc>;
MSTL_NODISCARD constexpr T && forward(remove_reference_t< T > &x) noexcept
完美转发左值
#define _MSTL
全局命名空间MSTL前缀
#define MSTL_END_NAMESPACE__
结束全局命名空间MSTL
#define MSTL_BEGIN_NAMESPACE__
开始全局命名空间MSTL
void swap()=delete
删除无参数的swap重载