NexusForce 1.0.0
A rigorously engineered full-stack C++ backend library.
载入中...
搜索中...
未找到
flat_unordered_map.hpp
浏览该文件的文档.
1#ifndef NEFORCE_CORE_CONTAINER_FLAT_UNORDERED_MAP_HPP__
2#define NEFORCE_CORE_CONTAINER_FLAT_UNORDERED_MAP_HPP__
3
13
15NEFORCE_BEGIN_NAMESPACE__
16
22
39template <typename Key, typename T, typename HashFcn = hash<Key>, typename EqualKey = equal_to<Key>,
40 typename Alloc = allocator<pair<Key, T>>>
41class flat_unordered_map : public icollector<flat_unordered_map<Key, T, HashFcn, EqualKey, Alloc>> {
42 static_assert(is_hash_v<HashFcn, Key>, "flat_unordered_map requires valid hash function.");
43 static_assert(is_allocator_v<Alloc>, "Alloc type is not a standard allocator type.");
44 static_assert(is_object_v<Key>, "flat_unordered_map only contains object types.");
45
46 using base_type = flat_hashtable<pair<Key, T>, Key, HashFcn, select1st<pair<Key, T>>, EqualKey,
47 Alloc>;
48
49public:
50 using key_type = typename base_type::key_type;
51 using mapped_type = T;
53 using hasher = typename base_type::hasher;
55
59 using const_pointer = const value_type*;
61 using const_reference = const value_type&;
62 using iterator = typename base_type::iterator;
65
66private:
67 base_type ht_;
68
69public:
75 flat_unordered_map() = default;
76
81 explicit flat_unordered_map(const size_type n) :
82 ht_(n) {}
83
89 flat_unordered_map(const size_type n, const hasher& hf) :
90 ht_(n, hf) {}
91
98 flat_unordered_map(const size_type n, const hasher& hf, const key_equal& eql) :
99 ht_(n, hf, eql) {}
100
106 ht_(other.ht_) {}
107
114 ht_ = other.ht_;
115 return *this;
116 }
117
123 ht_(_NEFORCE move(other.ht_)) {}
124
131 ht_ = _NEFORCE move(other.ht_);
132 return *this;
133 }
134
141 template <typename Iterator>
142 flat_unordered_map(Iterator first, Iterator last) :
143 ht_() {
144 ht_.insert_unique(first, last);
145 }
146
154 template <typename Iterator>
155 flat_unordered_map(Iterator first, Iterator last, const size_type n) :
156 ht_(n) {
157 ht_.insert_unique(first, last);
158 }
159
168 template <typename Iterator>
169 flat_unordered_map(Iterator first, Iterator last, const size_type n, const hasher& hf) :
170 ht_(n, hf) {
171 ht_.insert_unique(first, last);
172 }
173
183 template <typename Iterator>
184 flat_unordered_map(Iterator first, Iterator last, const size_type n, const hasher& hf, const key_equal& eql) :
185 ht_(n, hf, eql) {
186 ht_.insert_unique(first, last);
187 }
188
193 flat_unordered_map(std::initializer_list<value_type> ilist) :
194 flat_unordered_map(ilist.begin(), ilist.end()) {}
195
201 flat_unordered_map(std::initializer_list<value_type> ilist, const size_type n) :
202 flat_unordered_map(ilist.begin(), ilist.end(), n) {}
203
210 flat_unordered_map(std::initializer_list<value_type> ilist, const size_type n, const hasher& hf) :
211 flat_unordered_map(ilist.begin(), ilist.end(), n, hf) {}
212
220 flat_unordered_map(std::initializer_list<value_type> ilist, const size_type n, const hasher& hf,
221 const key_equal& eql) :
222 flat_unordered_map(ilist.begin(), ilist.end(), n, hf, eql) {}
223
228 NEFORCE_NODISCARD iterator begin() noexcept { return ht_.begin(); }
229
234 NEFORCE_NODISCARD iterator end() noexcept { return ht_.end(); }
235
240 NEFORCE_NODISCARD const_iterator begin() const noexcept { return ht_.begin(); }
241
246 NEFORCE_NODISCARD const_iterator end() const noexcept { return ht_.end(); }
247
252 NEFORCE_NODISCARD const_iterator cbegin() const noexcept { return ht_.cbegin(); }
253
258 NEFORCE_NODISCARD const_iterator cend() const noexcept { return ht_.cend(); }
259
264 NEFORCE_NODISCARD size_type size() const noexcept { return ht_.size(); }
265
270 NEFORCE_NODISCARD size_type max_size() const noexcept { return ht_.max_size(); }
271
276 NEFORCE_NODISCARD bool empty() const noexcept { return ht_.empty(); }
277
282 NEFORCE_NODISCARD size_type capacity() const noexcept { return ht_.capacity(); }
283
289 NEFORCE_NODISCARD size_type count(const key_type& key) const noexcept(noexcept(ht_.count(key))) {
290 return ht_.count(key);
291 }
292
298 NEFORCE_NODISCARD bool contains(const key_type& key) const noexcept(noexcept(ht_.contains(key))) {
299 return ht_.contains(key);
300 }
301
306 NEFORCE_NODISCARD hasher hash_function() const noexcept(noexcept(ht_.hash_function())) {
307 return ht_.hash_function();
308 }
309
314 NEFORCE_NODISCARD key_equal key_eql() const noexcept(noexcept(ht_.key_eql())) { return ht_.key_eql(); }
315
320 NEFORCE_NODISCARD float load_factor() const noexcept { return ht_.load_factor(); }
321
326 NEFORCE_NODISCARD float max_load_factor() const noexcept { return ht_.max_load_factor(); }
327
332 void max_load_factor(const float lf) noexcept { ht_.max_load_factor(lf); }
333
338 void rehash(const size_type n) { ht_.rehash(n); }
339
346 void reserve(const size_type n) { ht_.reserve(n); }
347
354 template <typename... Args>
356 return ht_.emplace_unique(_NEFORCE forward<Args>(args)...);
357 }
358
364 pair<iterator, bool> insert(const value_type& value) { return ht_.insert_unique(value); }
365
371 pair<iterator, bool> insert(value_type&& value) { return ht_.insert_unique(_NEFORCE move(value)); }
372
379 template <typename Iterator>
380 void insert(Iterator first, Iterator last) {
381 ht_.insert_unique(first, last);
382 }
383
388 void insert(std::initializer_list<value_type> ilist) { ht_.insert_unique(ilist); }
389
395 size_type erase(const key_type& key) noexcept { return ht_.erase(key); }
396
402 iterator erase(const iterator position) noexcept { return ht_.erase(position); }
403
410 iterator erase(const iterator first, const iterator last) noexcept { return ht_.erase(first, last); }
411
417 const_iterator erase(const const_iterator position) noexcept { return ht_.erase(position); }
418
425 const_iterator erase(const const_iterator first, const const_iterator last) noexcept {
426 return ht_.erase(first, last);
427 }
428
432 void clear() noexcept { ht_.clear(); }
433
439 NEFORCE_NODISCARD iterator find(const key_type& key) { return ht_.find(key); }
440
446 NEFORCE_NODISCARD const_iterator find(const key_type& key) const { return ht_.find(key); }
447
453 NEFORCE_NODISCARD pair<iterator, iterator> equal_range(const key_type& key) { return ht_.equal_range(key); }
454
460 NEFORCE_NODISCARD pair<const_iterator, const_iterator> equal_range(const key_type& key) const {
461 return ht_.equal_range(key);
462 }
463
471 NEFORCE_NODISCARD T& operator[](const key_type& key) {
472 auto iter = ht_.find(key);
473 if (iter == ht_.end()) {
474 iter = ht_.emplace_unique(key, T()).first;
475 }
476 return iter->second;
477 }
478
485 NEFORCE_NODISCARD const T& at(const key_type& key) const {
486 auto iter = ht_.find(key);
487 if (iter == end()) {
488 NEFORCE_THROW_EXCEPTION(iterator_exception("flat_unordered_map key-value does not exist"));
489 }
490 return iter->second;
491 }
492
499 NEFORCE_NODISCARD T& at(const key_type& key) {
500 auto iter = ht_.find(key);
501 if (iter == end()) {
502 NEFORCE_THROW_EXCEPTION(iterator_exception("flat_unordered_map key-value does not exist"));
503 }
504 return iter->second;
505 }
506
511 void swap(flat_unordered_map& other) noexcept(is_nothrow_swappable_v<base_type>) { ht_.swap(other.ht_); }
512
518 NEFORCE_NODISCARD bool equal_to(const flat_unordered_map& rhs) const noexcept(noexcept(ht_ == rhs.ht_)) {
519 return ht_ == rhs.ht_;
520 }
521
527 NEFORCE_NODISCARD bool less_than(const flat_unordered_map& rhs) const noexcept(noexcept(ht_ < rhs.ht_)) {
528 return ht_ < rhs.ht_;
529 }
530};
531
532#ifdef NEFORCE_STANDARD_17
533template <typename Iterator, typename HashFcn = hash<iter_map_key_t<Iterator>>,
534 typename Compare = equal_to<iter_map_key_t<Iterator>>, typename Alloc>
535flat_unordered_map(Iterator, Iterator, HashFcn = HashFcn(), Compare = Compare(), Alloc = Alloc())
536 -> flat_unordered_map<iter_map_key_t<Iterator>, iter_map_value_t<Iterator>, HashFcn, Compare, Alloc>;
537
538template <typename Key, typename T, typename HashFcn = hash<Key>, typename Compare = equal_to<Key>,
539 typename Alloc = allocator<pair<Key, T>>>
540flat_unordered_map(std::initializer_list<pair<Key, T>>, HashFcn = HashFcn(), Compare = Compare(), Alloc = Alloc())
541 -> flat_unordered_map<Key, T, HashFcn, Compare, Alloc>;
542
543template <typename Iterator, typename Alloc>
544flat_unordered_map(Iterator, Iterator, Alloc)
545 -> flat_unordered_map<iter_map_key_t<Iterator>, iter_map_value_t<Iterator>, hash<iter_map_key_t<Iterator>>,
546 equal_to<iter_map_key_t<Iterator>>, Alloc>;
547
548template <typename Iterator, typename HashFcn, typename Alloc>
549flat_unordered_map(Iterator, Iterator, HashFcn, Alloc)
550 -> flat_unordered_map<iter_map_key_t<Iterator>, iter_map_value_t<Iterator>, HashFcn,
551 equal_to<iter_map_key_t<Iterator>>, Alloc>;
552
553template <typename Key, typename T, typename Alloc>
554flat_unordered_map(std::initializer_list<pair<Key, T>>, Alloc)
555 -> flat_unordered_map<Key, T, hash<Key>, equal_to<Key>, Alloc>;
556
557template <typename Key, typename T, typename HashFcn, typename Alloc>
558flat_unordered_map(std::initializer_list<pair<Key, T>>, HashFcn, Alloc)
559 -> flat_unordered_map<Key, T, HashFcn, equal_to<Key>, Alloc>;
560#endif
561 // Container
563
564NEFORCE_END_NAMESPACE__
565#endif // NEFORCE_CORE_CONTAINER_FLAT_UNORDERED_MAP_HPP__
pair< const_iterator, const_iterator > equal_range(const key_type &key) const
获取等于指定键的常量元素范围
bool equal_to(const flat_unordered_map &rhs) const noexcept(noexcept(ht_==rhs.ht_))
相等比较操作符
flat_unordered_map(std::initializer_list< value_type > ilist, const size_type n, const hasher &hf)
初始化列表构造函数,指定初始容量和哈希函数
flat_unordered_map(std::initializer_list< value_type > ilist, const size_type n)
初始化列表构造函数,指定初始容量
bool empty() const noexcept
检查是否为空
bool less_than(const flat_unordered_map &rhs) const noexcept(noexcept(ht_< rhs.ht_))
小于比较操作符
pair< iterator, bool > insert(const value_type &value)
插入元素(拷贝版本)
void insert(std::initializer_list< value_type > ilist)
初始化列表插入元素
const_iterator cbegin() const noexcept
获取常量起始迭代器
size_type size() const noexcept
获取元素数量
hasher hash_function() const noexcept(noexcept(ht_.hash_function()))
获取哈希函数对象
void rehash(const size_type n)
重新哈希,调整容量
const_iterator end() const noexcept
获取常量结束迭代器
flat_unordered_map(const size_type n)
构造函数,指定初始容量
pair< iterator, bool > insert(value_type &&value)
移动插入元素
size_type max_size() const noexcept
获取最大可能大小
const_iterator erase(const const_iterator position) noexcept
删除指定位置的常量元素
float max_load_factor() const noexcept
获取最大负载因子
flat_unordered_map(flat_unordered_map &&other) noexcept(is_nothrow_move_constructible_v< base_type >)
移动构造函数
flat_unordered_map & operator=(const flat_unordered_map &other)
拷贝赋值运算符
iterator begin() noexcept
获取起始迭代器
void clear() noexcept
清空容器
flat_unordered_map(const flat_unordered_map &other)
拷贝构造函数
iterator end() noexcept
获取结束迭代器
const T & at(const key_type &key) const
带边界检查的常量访问
const_iterator erase(const const_iterator first, const const_iterator last) noexcept
删除指定范围内的常量元素
typename base_type::difference_type difference_type
差值类型
const value_type & const_reference
常量引用类型
typename base_type::hasher hasher
哈希函数类型
flat_unordered_map(Iterator first, Iterator last, const size_type n)
范围构造函数,指定初始容量
typename base_type::const_iterator const_iterator
常量迭代器类型
pair< iterator, bool > emplace(Args &&... args)
在容器中就地构造元素
iterator erase(const iterator first, const iterator last) noexcept
删除指定范围内的元素
bool contains(const key_type &key) const noexcept(noexcept(ht_.contains(key)))
检查是否包含指定键
flat_unordered_map(Iterator first, Iterator last)
范围构造函数
T & operator[](const key_type &key)
下标访问操作符
size_type count(const key_type &key) const noexcept(noexcept(ht_.count(key)))
统计具有指定键的元素数量
void reserve(const size_type n)
预留空间
flat_unordered_map & operator=(flat_unordered_map &&other) noexcept(is_nothrow_move_assignable_v< base_type >)
移动赋值运算符
float load_factor() const noexcept
获取当前负载因子
typename base_type::iterator iterator
迭代器类型
iterator erase(const iterator position) noexcept
删除指定位置的元素
T & at(const key_type &key)
带边界检查的访问
iterator find(const key_type &key)
查找具有指定键的元素
void insert(Iterator first, Iterator last)
范围插入元素
void swap(flat_unordered_map &other) noexcept(is_nothrow_swappable_v< base_type >)
交换两个容器
const_iterator find(const key_type &key) const
查找具有指定键的常量元素
const_iterator cend() const noexcept
获取常量结束迭代器
typename base_type::key_type key_type
键类型
flat_unordered_map(std::initializer_list< value_type > ilist, const size_type n, const hasher &hf, const key_equal &eql)
初始化列表构造函数,指定初始容量、哈希函数和键相等比较函数
const_iterator begin() const noexcept
获取常量起始迭代器
typename base_type::size_type size_type
大小类型
key_equal key_eql() const noexcept(noexcept(ht_.key_eql()))
获取键相等比较函数对象
flat_unordered_map(const size_type n, const hasher &hf, const key_equal &eql)
构造函数,指定初始容量、哈希函数和键相等比较函数
size_type erase(const key_type &key) noexcept
删除所有具有指定键的元素
flat_unordered_map(Iterator first, Iterator last, const size_type n, const hasher &hf)
范围构造函数,指定初始容量和哈希函数
pair< iterator, iterator > equal_range(const key_type &key)
获取等于指定键的元素范围
typename base_type::key_equal key_equal
键相等比较函数类型
typename base_type::allocator_type allocator_type
分配器类型
void max_load_factor(const float lf) noexcept
设置最大负载因子
flat_unordered_map(std::initializer_list< value_type > ilist)
初始化列表构造函数
size_type capacity() const noexcept
获取容量
flat_unordered_map()=default
默认构造函数
flat_unordered_map(const size_type n, const hasher &hf)
构造函数,指定初始容量和哈希函数
flat_unordered_map(Iterator first, Iterator last, const size_type n, const hasher &hf, const key_equal &eql)
范围构造函数,指定初始容量、哈希函数和键相等比较函数
const value_type * const_pointer
常量指针类型
平坦哈希表容器
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的便捷变量模板
集合器接口模板
指针或迭代器行为异常
存储两个值的元组对
选择pair的第一个元素