NexusForce 1.0.0
A rigorously engineered full-stack C++ backend library.
载入中...
搜索中...
未找到
sparse_multimap.hpp
浏览该文件的文档.
1#ifndef NEFORCE_CORE_CONTAINER_SPARSE_MULTIMAP_HPP__
2#define NEFORCE_CORE_CONTAINER_SPARSE_MULTIMAP_HPP__
3
18
20NEFORCE_BEGIN_NAMESPACE__
21
27
40template <typename Key, typename T, typename Compare = less<Key>, typename Alloc = allocator<pair<Key, T>>>
41class sparse_multimap : public icollector<sparse_multimap<Key, T, Compare, Alloc>> {
42 static_assert(is_allocator_v<Alloc>, "Alloc type is not a standard allocator type.");
43 static_assert(is_same_v<pair<Key, T>, typename Alloc::value_type>, "allocator type mismatch.");
44 static_assert(is_object_v<T>, "sparse_multimap only contains object types.");
45
46public:
47 using key_type = Key;
48 using data_type = T;
49 using mapped_type = T;
51 using key_compare = Compare;
52
59 struct value_compare {
60 private:
61 Compare comp_;
62 friend class sparse_multimap;
63
64 public:
65 explicit value_compare(Compare comp) :
66 comp_(comp) {}
67
74 bool operator()(const value_type& lhs, const value_type& rhs) const noexcept {
75 return comp_(lhs.first, rhs.first);
76 }
77 };
78
79private:
80 using base_type = sparse_vector<Key, pair<Key, T>, select1st<pair<Key, T>>, Compare, Alloc>;
81
82public:
85 using pointer = typename base_type::pointer;
89 using iterator = typename base_type::iterator;
94
95private:
96 base_type data_;
97
98public:
105 data_(Compare()) {}
106
111 explicit sparse_multimap(const key_compare& comp) :
112 data_(comp) {}
113
119 data_(other.data_) {}
120
127 data_ = other.data_;
128 return *this;
129 }
130
136 data_(_NEFORCE move(other.data_)) {}
137
144 data_ = _NEFORCE move(other.data_);
145 return *this;
146 }
147
154 template <typename Iterator>
155 sparse_multimap(Iterator first, Iterator last) :
156 data_(Compare()) {
157 data_.insert_equal(first, last);
158 }
159
167 template <typename Iterator>
168 sparse_multimap(Iterator first, Iterator last, const key_compare& comp) :
169 data_(comp) {
170 data_.insert_equal(first, last);
171 }
172
177 sparse_multimap(std::initializer_list<value_type> ilist) :
178 sparse_multimap(ilist.begin(), ilist.end()) {}
179
185 sparse_multimap(std::initializer_list<value_type> ilist, const key_compare& comp) :
186 sparse_multimap(ilist.begin(), ilist.end(), comp) {}
187
193 sparse_multimap& operator=(std::initializer_list<value_type> ilist) {
194 clear();
195 insert(ilist.begin(), ilist.end());
196 return *this;
197 }
198
202 ~sparse_multimap() = default;
203
208 NEFORCE_NODISCARD iterator begin() noexcept { return data_.begin(); }
209
214 NEFORCE_NODISCARD iterator end() noexcept { return data_.end(); }
215
220 NEFORCE_NODISCARD const_iterator begin() const noexcept { return data_.cbegin(); }
221
226 NEFORCE_NODISCARD const_iterator end() const noexcept { return data_.cend(); }
227
232 NEFORCE_NODISCARD const_iterator cbegin() const noexcept { return data_.cbegin(); }
233
238 NEFORCE_NODISCARD const_iterator cend() const noexcept { return data_.cend(); }
239
244 NEFORCE_NODISCARD reverse_iterator rbegin() noexcept { return data_.rbegin(); }
245
250 NEFORCE_NODISCARD reverse_iterator rend() noexcept { return data_.rend(); }
251
256 NEFORCE_NODISCARD const_reverse_iterator rbegin() const noexcept { return data_.rbegin(); }
257
262 NEFORCE_NODISCARD const_reverse_iterator rend() const noexcept { return data_.rend(); }
263
268 NEFORCE_NODISCARD const_reverse_iterator crbegin() const noexcept { return data_.crbegin(); }
269
274 NEFORCE_NODISCARD const_reverse_iterator crend() const noexcept { return data_.crend(); }
275
280 NEFORCE_NODISCARD size_type size() const noexcept { return data_.size(); }
281
286 NEFORCE_NODISCARD size_type max_size() const noexcept { return data_.max_size(); }
287
292 NEFORCE_NODISCARD bool empty() const noexcept { return data_.empty(); }
293
298 NEFORCE_NODISCARD key_compare key_comp() const noexcept { return data_.key_compare(); }
299
304 NEFORCE_NODISCARD value_compare value_comp() const noexcept { return value_compare(data_.key_compare()); }
305
312 template <typename... Args>
313 iterator emplace(Args&&... args) {
314 return data_.emplace_equal(_NEFORCE forward<Args>(args)...);
315 }
316
322 iterator insert(const value_type& value) { return data_.insert_equal(value); }
323
329 iterator insert(value_type&& value) { return data_.insert_equal(_NEFORCE move(value)); }
330
338 template <typename... Args>
339 iterator emplace_hint(iterator position, Args&&... args) {
340 return data_.emplace_equal_hint(position, _NEFORCE forward<Args>(args)...);
341 }
342
349 iterator insert(iterator position, const value_type& value) { return data_.insert_equal(position, value); }
350
357 iterator insert(iterator position, value_type&& value) {
358 return data_.insert_equal(position, _NEFORCE move(value));
359 }
360
367 template <typename Iterator>
368 void insert(Iterator first, Iterator last) {
369 data_.insert_equal(first, last);
370 }
371
376 void erase(iterator position) noexcept(noexcept(data_.erase(position))) { data_.erase(position); }
377
383 size_type erase(const key_type& key) noexcept(noexcept(data_.erase(key))) { return data_.erase(key); }
384
390 void erase(iterator first, iterator last) noexcept(noexcept(data_.erase(first, last))) { data_.erase(first, last); }
391
395 void clear() noexcept(noexcept(data_.clear())) { data_.clear(); }
396
402 NEFORCE_NODISCARD iterator find(const key_type& key) { return data_.find(key); }
403
409 NEFORCE_NODISCARD const_iterator find(const key_type& key) const { return data_.find(key); }
410
416 NEFORCE_NODISCARD size_type count(const key_type& key) const { return data_.count(key); }
417
423 NEFORCE_NODISCARD iterator lower_bound(const key_type& key) { return data_.lower_bound(key); }
424
430 NEFORCE_NODISCARD const_iterator lower_bound(const key_type& key) const { return data_.lower_bound(key); }
431
437 NEFORCE_NODISCARD iterator upper_bound(const key_type& key) { return data_.upper_bound(key); }
438
444 NEFORCE_NODISCARD const_iterator upper_bound(const key_type& key) const { return data_.upper_bound(key); }
445
451 NEFORCE_NODISCARD pair<iterator, iterator> equal_range(const key_type& key) { return data_.equal_range(key); }
452
458 NEFORCE_NODISCARD pair<const_iterator, const_iterator> equal_range(const key_type& key) const {
459 return data_.equal_range(key);
460 }
461
466 void reserve(size_type n) { data_.reserve(n); }
467
472 NEFORCE_NODISCARD size_type capacity() const noexcept { return data_.capacity(); }
473
477 void shrink_to_fit() { data_.shrink_to_fit(); }
478
483 void swap(sparse_multimap& other) noexcept(is_nothrow_swappable_v<base_type>) { data_.swap(other.data_); }
484
490 NEFORCE_NODISCARD bool equal_to(const sparse_multimap& rhs) const noexcept(noexcept(data_ == rhs.data_)) {
491 return data_ == rhs.data_;
492 }
493
499 NEFORCE_NODISCARD bool less_than(const sparse_multimap& rhs) const noexcept(noexcept(data_ < rhs.data_)) {
500 return data_ < rhs.data_;
501 }
502};
503
504#ifdef NEFORCE_STANDARD_17
505template <typename Iterator, typename Compare,
506 typename Alloc = allocator<pair<iter_map_key_t<Iterator>, iter_map_value_t<Iterator>>>>
507sparse_multimap(Iterator, Iterator, Compare = Compare(), Alloc = Alloc())
508 -> sparse_multimap<iter_map_key_t<Iterator>, iter_map_value_t<Iterator>, Compare, Alloc>;
509
510template <typename Key, typename T, typename Compare = less<Key>, typename Alloc = allocator<pair<Key, T>>>
511sparse_multimap(std::initializer_list<pair<Key, T>>, Compare = Compare(), Alloc = Alloc())
512 -> sparse_multimap<Key, T, Compare, Alloc>;
513
514template <typename Iterator, typename Alloc>
515sparse_multimap(Iterator, Iterator, Alloc)
516 -> sparse_multimap<iter_map_key_t<Iterator>, iter_map_value_t<Iterator>, less<iter_map_key_t<Iterator>>, Alloc>;
517
518template <typename Key, typename T, typename Alloc>
519sparse_multimap(std::initializer_list<pair<Key, T>>, Alloc) -> sparse_multimap<Key, T, less<Key>, Alloc>;
520#endif
521 // Container
523
524NEFORCE_END_NAMESPACE__
525#endif // NEFORCE_CORE_CONTAINER_SPARSE_MULTIMAP_HPP__
iterator insert(iterator position, value_type &&value)
在提示位置附近移动插入元素
void reserve(size_type n)
预留容量
typename base_type::const_iterator const_iterator
常量迭代器类型
iterator end() noexcept
获取结束迭代器
iterator emplace(Args &&... args)
在sparse_multimap中就地构造元素
const_iterator cbegin() const noexcept
获取常量起始迭代器
typename base_type::const_reference const_reference
常量引用类型
bool less_than(const sparse_multimap &rhs) const noexcept(noexcept(data_< rhs.data_))
小于比较操作符
sparse_multimap(std::initializer_list< value_type > ilist)
初始化列表构造函数
size_type capacity() const noexcept
获取当前容量
const_reverse_iterator crend() const noexcept
获取常量反向结束迭代器
const_iterator lower_bound(const key_type &key) const
获取第一个不小于指定键的常量元素位置
typename base_type::reverse_iterator reverse_iterator
反向迭代器类型
typename base_type::pointer pointer
指针类型
iterator emplace_hint(iterator position, Args &&... args)
在提示位置附近就地构造元素
pair< const_iterator, const_iterator > equal_range(const key_type &key) const
获取等于指定键的常量元素范围
sparse_multimap(Iterator first, Iterator last, const key_compare &comp)
范围构造函数,指定比较函数
pair< iterator, iterator > equal_range(const key_type &key)
获取等于指定键的元素范围
sparse_multimap(const key_compare &comp)
构造函数,指定比较函数
size_type erase(const key_type &key) noexcept(noexcept(data_.erase(key)))
删除所有具有指定键的元素
sparse_multimap(sparse_multimap &&other) noexcept(is_nothrow_move_constructible_v< base_type >)
移动构造函数
const_iterator end() const noexcept
获取常量结束迭代器
iterator find(const key_type &key)
查找具有指定键的元素
void erase(iterator first, iterator last) noexcept(noexcept(data_.erase(first, last)))
删除指定范围内的元素
const_iterator find(const key_type &key) const
查找具有指定键的常量元素
void shrink_to_fit()
收缩容量以适应实际大小
typename base_type::const_reverse_iterator const_reverse_iterator
常量反向迭代器类型
key_compare key_comp() const noexcept
获取键比较函数对象
const_iterator begin() const noexcept
获取常量起始迭代器
sparse_multimap(Iterator first, Iterator last)
范围构造函数
size_type count(const key_type &key) const
统计具有指定键的元素数量
bool empty() const noexcept
检查是否为空
typename base_type::iterator iterator
迭代器类型
reverse_iterator rend() noexcept
获取反向结束迭代器
sparse_multimap & operator=(const sparse_multimap &other)
拷贝赋值运算符
iterator upper_bound(const key_type &key)
获取第一个大于指定键的元素位置
~sparse_multimap()=default
析构函数
void clear() noexcept(noexcept(data_.clear()))
清空sparse_multimap
size_type max_size() const noexcept
获取最大可能大小
const_iterator upper_bound(const key_type &key) const
获取第一个大于指定键的常量元素位置
sparse_multimap(std::initializer_list< value_type > ilist, const key_compare &comp)
初始化列表构造函数,指定比较函数
iterator begin() noexcept
获取起始迭代器
const_reverse_iterator rend() const noexcept
获取常量反向结束迭代器
void erase(iterator position) noexcept(noexcept(data_.erase(position)))
删除指定位置的元素
sparse_multimap(const sparse_multimap &other)
拷贝构造函数
void swap(sparse_multimap &other) noexcept(is_nothrow_swappable_v< base_type >)
交换两个sparse_multimap的内容
reverse_iterator rbegin() noexcept
获取反向起始迭代器
const_reverse_iterator crbegin() const noexcept
获取常量反向起始迭代器
size_type size() const noexcept
获取元素数量
typename base_type::reference reference
引用类型
const_reverse_iterator rbegin() const noexcept
获取常量反向起始迭代器
void insert(Iterator first, Iterator last)
范围插入元素
typename base_type::size_type size_type
大小类型
typename base_type::const_pointer const_pointer
常量指针类型
typename base_type::difference_type difference_type
差值类型
sparse_multimap & operator=(std::initializer_list< value_type > ilist)
初始化列表赋值运算符
Compare key_compare
键比较函数类型
iterator lower_bound(const key_type &key)
获取第一个不小于指定键的元素位置
typename base_type::allocator_type allocator_type
分配器类型
bool equal_to(const sparse_multimap &rhs) const noexcept(noexcept(data_==rhs.data_))
相等比较操作符
const_iterator cend() const noexcept
获取常量结束迭代器
iterator insert(iterator position, const value_type &value)
在提示位置附近拷贝插入元素
iterator insert(value_type &&value)
移动插入元素
value_compare value_comp() const noexcept
获取值比较函数对象
sparse_multimap & operator=(sparse_multimap &&other) noexcept(is_nothrow_move_assignable_v< base_type >)
移动赋值运算符
pair< Key, T > value_type
值类型
iterator insert(const value_type &value)
拷贝插入元素
constexpr T && forward(remove_reference_t< T > &x) noexcept
完美转发左值
constexpr bool is_object_v
is_object的便捷变量模板
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的便捷变量模板
constexpr bool is_same_v
is_same的便捷变量模板
稀疏向量容器
集合器接口模板
存储两个值的元组对
选择pair的第一个元素
bool operator()(const value_type &lhs, const value_type &rhs) const noexcept
比较两个键值对