MSTL 1.4.0
A Modern C++ Library with extended functionality, web components, and utility libraries
载入中...
搜索中...
未找到
unordered_set.hpp
1#ifndef MSTL_CORE_CONTAINER_UNORDERED_SET_HPP__
2#define MSTL_CORE_CONTAINER_UNORDERED_SET_HPP__
3#include "hashtable.hpp"
5
6template <typename Value, typename HashFcn = hash<Value>, typename EqualKey = equal_to<Value>,
7 typename Alloc = allocator<hashtable_node<Value>>>
8class unordered_set : icollector<unordered_set<Value, HashFcn, EqualKey, Alloc>> {
9#ifdef MSTL_STANDARD_20__
10 static_assert(is_hash_v<HashFcn, Value>, "unordered set requires valid hash function.");
11 static_assert(is_allocator_v<Alloc>, "Alloc type is not a standard allocator type.");
12#endif
13 static_assert(is_same_v<hashtable_node<Value>, typename Alloc::value_type>, "allocator type mismatch.");
14 static_assert(is_object_v<Value>, "unordered set only contains object types.");
15
16 using base_type = hashtable<Value, Value, HashFcn, identity<Value>, EqualKey, Alloc>;
17
18public:
19 using key_type = typename base_type::key_type;
20 using value_type = typename base_type::value_type;
21 using hasher = typename base_type::hasher;
22 using key_equal = typename base_type::key_equal;
23
24 using size_type = typename base_type::size_type;
25 using difference_type = typename base_type::difference_type;
26 using pointer = typename base_type::const_pointer;
27 using const_pointer = typename base_type::const_pointer;
28 using reference = typename base_type::const_reference;
29 using const_reference = typename base_type::const_reference;
30
31 using iterator = typename base_type::const_iterator;
32 using const_iterator = typename base_type::const_iterator;
33
34 using allocator_type = typename base_type::allocator_type;
35
36private:
37 base_type ht_{100};
38
39public:
40 unordered_set() : ht_(100, hasher(), key_equal()) {}
41 explicit unordered_set(size_type n) : ht_(n) {}
42
43 unordered_set(size_type n, const hasher& hf) : ht_(n, hf, key_equal()) {}
44 unordered_set(size_type n, const hasher& hf, const key_equal& eql) : ht_(n, hf, eql) {}
45
46 unordered_set(const unordered_set& ht) : ht_(ht.ht_) {}
47 unordered_set& operator =(const unordered_set& x) = default;
48
49 unordered_set(unordered_set&& x) noexcept(noexcept(ht_.swap(x.ht_))) : ht_(_MSTL forward<base_type>(x.ht_)) {}
50 unordered_set& operator =(unordered_set&& x) noexcept(noexcept(ht_.swap(x.ht_))) {
51 ht_ = _MSTL forward<unordered_set>(x.ht_);
52 return *this;
53 }
54
55 template <typename Iterator>
56 unordered_set(Iterator first, Iterator last) : ht_(100, hasher(), key_equal()) {
57 ht_.insert_unique(first, last);
58 }
59 template <typename Iterator>
60 unordered_set(Iterator first, Iterator list, size_type n) : ht_(n, hasher(), key_equal()) {
61 ht_.insert_unique(first, list);
62 }
63 template <typename Iterator>
64 unordered_set(Iterator first, Iterator last, size_type n, const hasher& hf) : ht_(n, hf, key_equal()) {
65 ht_.insert_unique(first, last);
66 }
67 template <typename Iterator>
68 unordered_set(Iterator first, Iterator last, size_type n, const hasher& hf, const key_equal& eql)
69 : ht_(n, hf, eql) {
70 ht_.insert_unique(first, last);
71 }
72
73 unordered_set(std::initializer_list<value_type> l)
74 : unordered_set(l.begin(), l.end()) {}
75 unordered_set(std::initializer_list<value_type> l, size_type n)
76 : unordered_set(l.begin(), l.end(), n) {}
77 unordered_set(std::initializer_list<value_type> l, size_type n, const hasher& hf)
78 : unordered_set(l.begin(), l.end(), n, hf) {}
79 unordered_set(std::initializer_list<value_type> l, size_type n, const hasher& hf, const key_equal& eql)
80 : unordered_set(l.begin(), l.end(), n, hf, eql) {}
81
82 MSTL_NODISCARD iterator begin() noexcept { return ht_.begin(); }
83 MSTL_NODISCARD iterator end() noexcept { return ht_.end(); }
84 MSTL_NODISCARD const_iterator begin() const noexcept { return ht_.begin(); }
85 MSTL_NODISCARD const_iterator end() const noexcept { return ht_.end(); }
86 MSTL_NODISCARD const_iterator cbegin() const noexcept { return ht_.cbegin(); }
87 MSTL_NODISCARD const_iterator cend() const noexcept { return ht_.cend(); }
88
89 MSTL_NODISCARD size_type size() const noexcept { return ht_.size(); }
90 MSTL_NODISCARD size_type max_size() const noexcept { return ht_.max_size(); }
91 MSTL_NODISCARD bool empty() const noexcept { return ht_.empty(); }
92
93 MSTL_NODISCARD size_type count(const key_type& key) const noexcept(noexcept(ht_.count(key))) {
94 return ht_.count(key);
95 }
96 MSTL_NODISCARD size_type bucket_count() const noexcept { return ht_.bucket_count(); }
97 MSTL_NODISCARD size_type max_bucket_count() const noexcept { return ht_.max_bucket_count(); }
98 MSTL_NODISCARD size_type bucket_size(size_type n) const noexcept { return ht_.bucket_size(n); }
99
100 MSTL_NODISCARD allocator_type get_allocator() const noexcept { return allocator_type(); }
101
102 MSTL_NODISCARD hasher hash_funct() const noexcept(noexcept(ht_.hash_func())) { return ht_.hash_func(); }
103 MSTL_NODISCARD key_equal key_eq() const noexcept(noexcept(ht_.key_eql())) { return ht_.key_eql(); }
104
105 MSTL_NODISCARD float load_factor() const noexcept { return ht_.load_factor(); }
106 MSTL_NODISCARD float max_load_factor() const noexcept { return ht_.max_load_factor(); }
107 void max_load_factor(float new_max) noexcept { ht_.max_load_factor(new_max); }
108
109 void rehash(size_type new_size) { ht_.rehash(new_size); }
110 void reserve(size_type max_count) { ht_.reserve(max_count); }
111
112 template <typename... Args>
113 pair<iterator, bool> emplace(Args&&... args) {
114 return ht_.emplace_unique(_MSTL forward<Args>(args)...);
115 }
116
117 pair<iterator, bool> insert(const value_type& obj) {
118 return ht_.insert_unique(obj);
119 }
120 pair<iterator, bool> insert(value_type&& obj) {
121 return ht_.insert_unique(_MSTL forward<value_type>(obj));
122 }
123 template <typename Iterator>
124 void insert(Iterator first, Iterator last) { ht_.insert_unique(first, last); }
125
126 size_type erase(const key_type& key) noexcept { return ht_.erase(key); }
127 void erase(iterator it) noexcept { ht_.erase(it); }
128 void erase(iterator first, iterator last) noexcept { ht_.erase(first, last); }
129 void clear() noexcept { ht_.clear(); }
130
131 MSTL_NODISCARD iterator find(const key_type& key) { return ht_.find(key); }
132 MSTL_NODISCARD const_iterator find(const key_type& key) const { return ht_.find(key); }
133
134 MSTL_NODISCARD pair<iterator, iterator> equal_range(const key_type& key) { return ht_.equal_range(key); }
135 MSTL_NODISCARD pair<const_iterator, const_iterator> equal_range(const key_type& key) const {
136 return ht_.equal_range(key);
137 }
138
139 void swap(unordered_set& x) noexcept(noexcept(ht_.swap(x.ht_))) { ht_.swap(x.ht_); }
140
141 MSTL_NODISCARD bool operator ==(const unordered_set& rhs) const
142 noexcept(noexcept(ht_ == rhs.ht_)) {
143 return ht_ == rhs.ht_;
144 }
145 MSTL_NODISCARD bool operator <(const unordered_set& rhs) const
146 noexcept(noexcept(ht_ < rhs.ht_)) {
147 return ht_ < rhs.ht_;
148 }
149};
150#if MSTL_SUPPORT_DEDUCTION_GUIDES__
151template <typename Iterator, typename HashFcn = hash<iter_value_t<Iterator>>, typename Compare
152 = equal_to<iter_value_t<Iterator>>, typename Alloc = allocator<iter_value_t<Iterator>>>
153unordered_set(Iterator, Iterator, HashFcn = HashFcn(), Compare = Compare(), Alloc = Alloc())
154-> unordered_set<iter_value_t<Iterator>, HashFcn, Compare, Alloc>;
155
156template <typename Key, typename HashFcn = hash<Key>, typename Compare = equal_to<Key>,
157 typename Alloc = allocator<Key>>
158unordered_set(std::initializer_list<Key>, HashFcn = HashFcn(), Compare = Compare(), Alloc = Alloc())
159-> unordered_set<Key, HashFcn, Compare, Alloc>;
160
161template <typename Iterator, typename Alloc>
162unordered_set(Iterator, Iterator, Alloc)
163-> unordered_set<iter_value_t<Iterator>, hash<iter_value_t<Iterator>>, equal_to<iter_value_t<Iterator>>, Alloc>;
164
165template <typename Iterator, typename HashFcn, typename Alloc>
166unordered_set(Iterator, Iterator, HashFcn, Alloc)
167-> unordered_set<iter_value_t<Iterator>, HashFcn, equal_to<iter_value_t<Iterator>>, Alloc>;
168
169template <typename Key, typename Alloc>
170unordered_set(std::initializer_list<Key>, Alloc)
171-> unordered_set<Key, hash<Key>, equal_to<Key>, Alloc>;
172
173template <typename Key, typename HashFcn, typename Alloc>
174unordered_set(std::initializer_list<Key>, HashFcn, Alloc)
175-> unordered_set<Key, HashFcn, equal_to<Key>, Alloc>;
176#endif
177
179#endif // MSTL_CORE_CONTAINER_UNORDERED_SET_HPP__
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重载
相等比较仿函数
哈希函数的主模板
集合器接口模板