NexusForce 1.0.0
A rigorously engineered full-stack C++ backend library.
载入中...
搜索中...
未找到
flat_unordered_set.hpp
浏览该文件的文档.
1#ifndef NEFORCE_CORE_CONTAINER_FLAT_UNORDERED_SET_HPP__
2#define NEFORCE_CORE_CONTAINER_FLAT_UNORDERED_SET_HPP__
3
13
15NEFORCE_BEGIN_NAMESPACE__
16
22
35template <typename Value, typename HashFcn = hash<Value>, typename EqualKey = equal_to<Value>,
36 typename Alloc = allocator<Value>>
37class flat_unordered_set : public icollector<flat_unordered_set<Value, HashFcn, EqualKey, Alloc>> {
38 static_assert(is_hash_v<HashFcn, Value>, "flat_unordered_set requires valid hash function.");
39 static_assert(is_allocator_v<Alloc>, "Alloc type is not a standard allocator type.");
40 static_assert(is_object_v<Value>, "flat_unordered_set only contains object types.");
41
42private:
43 using base_type = flat_hashtable<Value, Value, HashFcn, identity<Value>, EqualKey, Alloc>;
44
45public:
46 using key_type = typename base_type::key_type;
48 using hasher = typename base_type::hasher;
50
57 using iterator = typename base_type::iterator;
60
61private:
62 base_type ht_;
63
64public:
70 flat_unordered_set() = default;
71
76 explicit flat_unordered_set(const size_type n) :
77 ht_(n) {}
78
84 flat_unordered_set(const size_type n, const hasher& hf) :
85 ht_(n, hf) {}
86
93 flat_unordered_set(const size_type n, const hasher& hf, const key_equal& eql) :
94 ht_(n, hf, eql) {}
95
101 ht_(other.ht_) {}
102
109 ht_ = other.ht_;
110 return *this;
111 }
112
118 ht_(_NEFORCE move(other.ht_)) {}
119
126 ht_ = _NEFORCE move(other.ht_);
127 return *this;
128 }
129
136 template <typename Iterator>
137 flat_unordered_set(Iterator first, Iterator last) :
138 ht_() {
139 ht_.insert_unique(first, last);
140 }
141
149 template <typename Iterator>
150 flat_unordered_set(Iterator first, Iterator last, const size_type n) :
151 ht_(n) {
152 ht_.insert_unique(first, last);
153 }
154
163 template <typename Iterator>
164 flat_unordered_set(Iterator first, Iterator last, const size_type n, const hasher& hf) :
165 ht_(n, hf) {
166 ht_.insert_unique(first, last);
167 }
168
178 template <typename Iterator>
179 flat_unordered_set(Iterator first, Iterator last, const size_type n, const hasher& hf, const key_equal& eql) :
180 ht_(n, hf, eql) {
181 ht_.insert_unique(first, last);
182 }
183
188 flat_unordered_set(std::initializer_list<value_type> ilist) :
189 flat_unordered_set(ilist.begin(), ilist.end()) {}
190
196 flat_unordered_set(std::initializer_list<value_type> ilist, const size_type n) :
197 flat_unordered_set(ilist.begin(), ilist.end(), n) {}
198
205 flat_unordered_set(std::initializer_list<value_type> ilist, const size_type n, const hasher& hf) :
206 flat_unordered_set(ilist.begin(), ilist.end(), n, hf) {}
207
215 flat_unordered_set(std::initializer_list<value_type> ilist, const size_type n, const hasher& hf,
216 const key_equal& eql) :
217 flat_unordered_set(ilist.begin(), ilist.end(), n, hf, eql) {}
218
223 NEFORCE_NODISCARD iterator begin() noexcept { return ht_.begin(); }
224
229 NEFORCE_NODISCARD iterator end() noexcept { return ht_.end(); }
230
235 NEFORCE_NODISCARD const_iterator begin() const noexcept { return ht_.begin(); }
236
241 NEFORCE_NODISCARD const_iterator end() const noexcept { return ht_.end(); }
242
247 NEFORCE_NODISCARD const_iterator cbegin() const noexcept { return ht_.cbegin(); }
248
253 NEFORCE_NODISCARD const_iterator cend() const noexcept { return ht_.cend(); }
254
259 NEFORCE_NODISCARD size_type size() const noexcept { return ht_.size(); }
260
265 NEFORCE_NODISCARD size_type max_size() const noexcept { return ht_.max_size(); }
266
271 NEFORCE_NODISCARD bool empty() const noexcept { return ht_.empty(); }
272
277 NEFORCE_NODISCARD size_type capacity() const noexcept { return ht_.capacity(); }
278
284 NEFORCE_NODISCARD size_type count(const key_type& key) const noexcept(noexcept(ht_.count(key))) {
285 return ht_.count(key);
286 }
287
293 NEFORCE_NODISCARD bool contains(const key_type& key) const noexcept(noexcept(ht_.contains(key))) {
294 return ht_.contains(key);
295 }
296
301 NEFORCE_NODISCARD hasher hash_function() const noexcept(noexcept(ht_.hash_function())) {
302 return ht_.hash_function();
303 }
304
309 NEFORCE_NODISCARD key_equal key_eql() const noexcept(noexcept(ht_.key_eql())) { return ht_.key_eql(); }
310
315 NEFORCE_NODISCARD float load_factor() const noexcept { return ht_.load_factor(); }
316
321 NEFORCE_NODISCARD float max_load_factor() const noexcept { return ht_.max_load_factor(); }
322
327 void max_load_factor(const float lf) noexcept { ht_.max_load_factor(lf); }
328
333 void rehash(const size_type n) { ht_.rehash(n); }
334
341 void reserve(const size_type n) { ht_.reserve(n); }
342
349 template <typename... Args>
351 return ht_.emplace_unique(_NEFORCE forward<Args>(args)...);
352 }
353
359 pair<iterator, bool> insert(const value_type& value) { return ht_.insert_unique(value); }
360
366 pair<iterator, bool> insert(value_type&& value) { return ht_.insert_unique(_NEFORCE move(value)); }
367
374 template <typename Iterator>
375 void insert(Iterator first, Iterator last) {
376 ht_.insert_unique(first, last);
377 }
378
383 void insert(std::initializer_list<value_type> ilist) { ht_.insert_unique(ilist); }
384
390 size_type erase(const key_type& key) noexcept { return ht_.erase(key); }
391
397 iterator erase(const iterator position) noexcept { return ht_.erase(position); }
398
405 iterator erase(const iterator first, const iterator last) noexcept { return ht_.erase(first, last); }
406
412 const_iterator erase(const const_iterator position) noexcept { return ht_.erase(position); }
413
420 const_iterator erase(const const_iterator first, const const_iterator last) noexcept {
421 return ht_.erase(first, last);
422 }
423
427 void clear() noexcept { ht_.clear(); }
428
434 NEFORCE_NODISCARD iterator find(const key_type& key) { return ht_.find(key); }
435
441 NEFORCE_NODISCARD const_iterator find(const key_type& key) const { return ht_.find(key); }
442
448 NEFORCE_NODISCARD pair<iterator, iterator> equal_range(const key_type& key) { return ht_.equal_range(key); }
449
455 NEFORCE_NODISCARD pair<const_iterator, const_iterator> equal_range(const key_type& key) const {
456 return ht_.equal_range(key);
457 }
458
463 void swap(flat_unordered_set& other) noexcept(is_nothrow_swappable_v<base_type>) { ht_.swap(other.ht_); }
464
470 NEFORCE_NODISCARD bool equal_to(const flat_unordered_set& rhs) const noexcept(noexcept(ht_ == rhs.ht_)) {
471 return ht_ == rhs.ht_;
472 }
473
479 NEFORCE_NODISCARD bool less_than(const flat_unordered_set& rhs) const noexcept(noexcept(ht_ < rhs.ht_)) {
480 return ht_ < rhs.ht_;
481 }
482};
483
484#ifdef NEFORCE_STANDARD_17
485template <typename Iterator, typename HashFcn = hash<iter_value_t<Iterator>>,
486 typename Compare = equal_to<iter_value_t<Iterator>>, typename Alloc = allocator<iter_value_t<Iterator>>>
487flat_unordered_set(Iterator, Iterator, HashFcn = HashFcn(), Compare = Compare(), Alloc = Alloc())
488 -> flat_unordered_set<iter_value_t<Iterator>, HashFcn, Compare, Alloc>;
489
490template <typename Key, typename HashFcn = hash<Key>, typename Compare = equal_to<Key>, typename Alloc = allocator<Key>>
491flat_unordered_set(std::initializer_list<Key>, HashFcn = HashFcn(), Compare = Compare(), Alloc = Alloc())
492 -> flat_unordered_set<Key, HashFcn, Compare, Alloc>;
493
494template <typename Iterator, typename Alloc>
495flat_unordered_set(Iterator, Iterator, Alloc)
496 -> flat_unordered_set<iter_value_t<Iterator>, hash<iter_value_t<Iterator>>, equal_to<iter_value_t<Iterator>>,
497 Alloc>;
498
499template <typename Iterator, typename HashFcn, typename Alloc>
500flat_unordered_set(Iterator, Iterator, HashFcn, Alloc)
501 -> flat_unordered_set<iter_value_t<Iterator>, HashFcn, equal_to<iter_value_t<Iterator>>, Alloc>;
502
503template <typename Key, typename Alloc>
504flat_unordered_set(std::initializer_list<Key>, Alloc) -> flat_unordered_set<Key, hash<Key>, equal_to<Key>, Alloc>;
505
506template <typename Key, typename HashFcn, typename Alloc>
507flat_unordered_set(std::initializer_list<Key>, HashFcn, Alloc)
508 -> flat_unordered_set<Key, HashFcn, equal_to<Key>, Alloc>;
509#endif
510 // Container
512
513NEFORCE_END_NAMESPACE__
514#endif // NEFORCE_CORE_CONTAINER_FLAT_UNORDERED_SET_HPP__
flat_unordered_set()=default
默认构造函数
typename base_type::difference_type difference_type
差值类型
typename base_type::key_type key_type
键类型
const_iterator end() const noexcept
获取常量结束迭代器
flat_unordered_set(std::initializer_list< value_type > ilist, const size_type n)
初始化列表构造函数,指定初始容量
hasher hash_function() const noexcept(noexcept(ht_.hash_function()))
获取哈希函数对象
bool less_than(const flat_unordered_set &rhs) const noexcept(noexcept(ht_< rhs.ht_))
小于比较操作符
flat_unordered_set(std::initializer_list< value_type > ilist, const size_type n, const hasher &hf)
初始化列表构造函数,指定初始容量和哈希函数
typename base_type::value_type value_type
值类型
flat_unordered_set(Iterator first, Iterator last, const size_type n, const hasher &hf, const key_equal &eql)
范围构造函数,指定初始容量、哈希函数和键相等比较函数
size_type size() const noexcept
获取元素数量
void rehash(const size_type n)
重新哈希,调整容量
typename base_type::allocator_type allocator_type
分配器类型
iterator erase(const iterator first, const iterator last) noexcept
删除指定范围内的元素
pair< iterator, bool > emplace(Args &&... args)
在容器中就地构造元素
typename base_type::key_equal key_equal
键相等比较函数类型
flat_unordered_set & operator=(flat_unordered_set &&other) noexcept(is_nothrow_move_assignable_v< base_type >)
移动赋值运算符
void swap(flat_unordered_set &other) noexcept(is_nothrow_swappable_v< base_type >)
交换两个容器
bool equal_to(const flat_unordered_set &rhs) const noexcept(noexcept(ht_==rhs.ht_))
相等比较操作符
pair< const_iterator, const_iterator > equal_range(const key_type &key) const
获取等于指定键的常量元素范围
flat_unordered_set(const size_type n, const hasher &hf)
构造函数,指定初始容量和哈希函数
flat_unordered_set(Iterator first, Iterator last, const size_type n)
范围构造函数,指定初始容量
flat_unordered_set(std::initializer_list< value_type > ilist)
初始化列表构造函数
typename base_type::const_reference reference
引用类型(键即值,不可修改)
bool empty() const noexcept
检查是否为空
size_type capacity() const noexcept
获取容量
iterator find(const key_type &key)
查找具有指定键的元素
iterator begin() noexcept
获取起始迭代器
flat_unordered_set(const size_type n)
构造函数,指定初始容量
void insert(Iterator first, Iterator last)
范围插入元素
void insert(std::initializer_list< value_type > ilist)
初始化列表插入元素
iterator end() noexcept
获取结束迭代器
typename base_type::const_pointer const_pointer
常量指针类型
size_type max_size() const noexcept
获取最大可能大小
size_type erase(const key_type &key) noexcept
删除所有具有指定键的元素
float max_load_factor() const noexcept
获取最大负载因子
flat_unordered_set(Iterator first, Iterator last)
范围构造函数
typename base_type::const_pointer pointer
指针类型(键即值,不可修改)
const_iterator begin() const noexcept
获取常量起始迭代器
const_iterator erase(const const_iterator first, const const_iterator last) noexcept
删除指定范围内的常量元素
float load_factor() const noexcept
获取当前负载因子
const_iterator cend() const noexcept
获取常量结束迭代器
flat_unordered_set(const flat_unordered_set &other)
拷贝构造函数
flat_unordered_set(Iterator first, Iterator last, const size_type n, const hasher &hf)
范围构造函数,指定初始容量和哈希函数
void clear() noexcept
清空容器
typename base_type::hasher hasher
哈希函数类型
flat_unordered_set(std::initializer_list< value_type > ilist, const size_type n, const hasher &hf, const key_equal &eql)
初始化列表构造函数,指定初始容量、哈希函数和键相等比较函数
const_iterator find(const key_type &key) const
查找具有指定键的常量元素
key_equal key_eql() const noexcept(noexcept(ht_.key_eql()))
获取键相等比较函数对象
iterator erase(const iterator position) noexcept
删除指定位置的元素
pair< iterator, bool > insert(value_type &&value)
移动插入元素
typename base_type::const_reference const_reference
常量引用类型
typename base_type::iterator iterator
迭代器类型
pair< iterator, iterator > equal_range(const key_type &key)
获取等于指定键的元素范围
typename base_type::const_iterator const_iterator
常量迭代器类型
void reserve(const size_type n)
预留空间
pair< iterator, bool > insert(const value_type &value)
插入元素(拷贝版本)
flat_unordered_set(const size_type n, const hasher &hf, const key_equal &eql)
构造函数,指定初始容量、哈希函数和键相等比较函数
void max_load_factor(const float lf) noexcept
设置最大负载因子
bool contains(const key_type &key) const noexcept(noexcept(ht_.contains(key)))
检查是否包含指定键
flat_unordered_set & operator=(const flat_unordered_set &other)
拷贝赋值运算符
flat_unordered_set(flat_unordered_set &&other) noexcept(is_nothrow_move_constructible_v< base_type >)
移动构造函数
size_type count(const key_type &key) const noexcept(noexcept(ht_.count(key)))
统计具有指定键的元素数量
const_iterator cbegin() const noexcept
获取常量起始迭代器
typename base_type::size_type size_type
大小类型
const_iterator erase(const const_iterator position) noexcept
删除指定位置的常量元素
平坦哈希表容器
constexpr T && forward(remove_reference_t< T > &x) noexcept
完美转发左值
constexpr bool is_object_v
is_object的便捷变量模板
constexpr bool is_hash_v
is_hash的便捷变量模板
constexpr Iterator2 move(Iterator1 first, Iterator1 last, Iterator2 result) noexcept(noexcept(inner::__move_aux(first, last, result)))
移动范围元素
constexpr bool is_nothrow_swappable_v
is_nothrow_swappable的便捷变量模板
constexpr bool is_allocator_v
is_allocator的便捷变量模板
constexpr bool is_nothrow_move_assignable_v
is_nothrow_move_assignable的便捷变量模板
constexpr bool is_nothrow_move_constructible_v
is_nothrow_move_constructible的便捷变量模板
集合器接口模板
存储两个值的元组对