-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathfacade.hh
255 lines (194 loc) · 5.69 KB
/
facade.hh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
// Copyright (c) 2022 Mikael Simonsson <https://mikaelsimonsson.com>.
// SPDX-License-Identifier: BSL-1.0
// # Facade (`std::` container wrapper)
// Wrapper around `std::set` or `std::unordered_set`, see [set/sorted.hh](sorted.hh) and
// [set/unsorted.hh](unsorted.hh).
#pragma once
#include "snn-core/math/common.hh"
#include "snn-core/range/bidirectional.hh"
#include "snn-core/range/forward.hh"
namespace snn::set
{
// ## Classes
// ### facade
template <typename Set>
class facade final
{
public:
// #### Types
using key_type = typename Set::key_type;
using value_type = typename Set::value_type;
using iterator = typename Set::iterator;
using const_iterator = typename Set::const_iterator;
private:
// #### Classes
class insert_result final
{
public:
using pair_type = std::pair<facade::iterator, bool>;
explicit insert_result(pair_type&& p)
: p_{std::move(p)}
{
}
explicit operator bool() const noexcept
{
return was_inserted();
}
[[nodiscard]] auto iterator() const
{
return p_.first;
}
[[nodiscard]] const auto& key() const noexcept
{
return *p_.first;
}
[[nodiscard]] bool was_inserted() const noexcept
{
return p_.second;
}
private:
pair_type p_;
};
public:
// #### Default constructor
facade() = default;
// #### Explicit constructors
// Generic container interface.
explicit facade(container::reserve_t, const usize capacity)
{
reserve_if_supported_(capacity);
}
// #### Converting constructors
facade(init_list<value_type> init)
: set_{init}
{
}
// #### Explicit conversion operators
explicit operator bool() const noexcept
{
return !is_empty();
}
// #### Iterators
[[nodiscard]] decltype(auto) begin() noexcept
{
return set_.begin();
}
[[nodiscard]] decltype(auto) begin() const noexcept
{
return set_.cbegin();
}
[[nodiscard]] decltype(auto) cbegin() const noexcept
{
return set_.cbegin();
}
[[nodiscard]] decltype(auto) end() noexcept
{
return set_.end();
}
[[nodiscard]] decltype(auto) end() const noexcept
{
return set_.cend();
}
[[nodiscard]] decltype(auto) cend() const noexcept
{
return set_.cend();
}
// #### Count
[[nodiscard]] usize count() const noexcept
{
return set_.size();
}
[[nodiscard]] bool is_empty() const noexcept
{
return set_.empty();
}
// #### Capacity
// Generic container interface.
void reserve(const usize capacity)
{
reserve_if_supported_(capacity);
}
void reserve_append(const usize capacity_increase)
{
reserve_if_supported_(math::add_with_saturation(set_.size(), capacity_increase));
}
// #### Insert
template <typename K>
insert_result insert(K&& key)
{
return insert_result{set_.emplace(std::forward<K>(key))};
}
template <typename... Args>
insert_result insert_inplace(Args&&... args)
{
return insert_result{set_.emplace(std::forward<Args>(args)...)};
}
// #### Append
// Generic container interface.
template <typename... Args>
void append_inplace(Args&&... args)
{
set_.emplace(std::forward<Args>(args)...);
}
// #### Lookup
template <typename K>
[[nodiscard]] bool contains(const K& key) const
{
return set_.find(key) != set_.cend();
}
// #### Modifiers
void clear() noexcept
{
set_.clear();
}
template <typename K>
bool remove(const K& key)
{
// Don't use `erase(key)`, it will always convert to `key_type`, `find(key)` will not
// under optimal conditions.
const auto it = set_.find(key);
if (it != set_.end())
{
set_.erase(it);
return true;
}
return false;
}
void swap(facade& other) noexcept
{
if (this != &other)
{
set_.swap(other.set_);
}
}
// #### Range
[[nodiscard]] auto range() const noexcept
{
return range_(typename std::iterator_traits<const_iterator>::iterator_category{});
}
private:
Set set_{};
auto range_(std::forward_iterator_tag) const noexcept
{
return range::forward{meta::iterators, set_.cbegin(), set_.cend()};
}
auto range_(std::bidirectional_iterator_tag) const noexcept
{
return range::bidirectional{meta::iterators, set_.cbegin(), set_.cend()};
}
void reserve_if_supported_(const usize capacity)
{
if constexpr (requires { set_.reserve(usize{}); })
{
set_.reserve(capacity);
}
}
};
// ## Functions
// ### swap
template <typename Set>
void swap(facade<Set>& a, facade<Set>& b) noexcept
{
a.swap(b);
}
}