-
Notifications
You must be signed in to change notification settings - Fork 1
/
set.hpp
278 lines (227 loc) · 6.52 KB
/
set.hpp
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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
#ifndef XSG_SET_HPP
# define XSG_SET_HPP
# pragma once
#include "utils.hpp"
#include "mapiterator.hpp"
namespace xsg
{
template <typename Key, class Compare = std::compare_three_way>
class set
{
public:
struct node;
using key_type = Key;
using value_type = Key;
using difference_type = detail::difference_type;
using size_type = detail::size_type;
using reference = value_type&;
using const_reference = value_type const&;
using iterator = mapiterator<node>;
using reverse_iterator = std::reverse_iterator<iterator>;
using const_iterator = mapiterator<node const>;
using const_reverse_iterator = std::reverse_iterator<const_iterator>;
struct node
{
using value_type = set::value_type;
static constinit inline Compare const cmp;
std::uintptr_t l_, r_;
Key const kv_;
explicit node(auto&& ...a)
noexcept(noexcept(Key(std::forward<decltype(a)>(a)...))):
kv_(std::forward<decltype(a)>(a)...)
{
}
//
auto& key() const noexcept { return kv_; }
//
static auto emplace(auto& r, auto&& k)
noexcept(noexcept(new node(std::forward<decltype(k)>(k))))
requires(detail::Comparable<Compare, decltype(k), key_type>)
{
auto const create_node([&](node* const p)
noexcept(noexcept(new node(std::forward<decltype(k)>(k))))
{
auto const q(new node(std::forward<decltype(k)>(k)));
q->l_ = q->r_ = detail::conv(p);
return q;
}
);
return r ? detail::emplace(r, k, create_node) :
std::tuple<node*, node*, bool>(r = create_node({}), {}, true);
}
static auto emplace(auto& r, auto&& ...a)
noexcept(noexcept(
emplace(r, key_type(std::forward<decltype(a)>(a)...))))
requires(std::is_constructible_v<key_type, decltype(a)...>)
{
return emplace(r, key_type(std::forward<decltype(a)>(a)...));
}
};
private:
using this_class = set;
node* root_{};
public:
set() = default;
set(set const& o)
noexcept(noexcept(set(o.begin(), o.end())))
requires(std::is_copy_constructible_v<value_type>):
set(o.begin(), o.end())
{
*this = o;
}
set(set&& o)
noexcept(noexcept(*this = std::move(o)))
{
*this = std::move(o);
}
set(std::input_iterator auto const i, decltype(i) j)
noexcept(noexcept(insert(i, j)))
{
insert(i, j);
}
set(std::initializer_list<value_type> l)
noexcept(noexcept(set(l.begin(), l.end()))):
set(l.begin(), l.end())
{
}
~set() noexcept(noexcept(detail::destroy(root_, {})))
{
detail::destroy(root_, {});
}
# include "common.hpp"
//
auto size() const noexcept { return detail::size(root_, {}); }
//
template <int = 0>
size_type count(auto const& k) const noexcept
requires(detail::Comparable<Compare, decltype(k), key_type>)
{
return bool(detail::find(root_, {}, k));
}
auto count(key_type const k) const noexcept { return count<0>(k); }
//
auto emplace(auto&& ...a)
noexcept(noexcept(node::emplace(root_, std::forward<decltype(a)>(a)...)))
{
auto const [n, p, s](
node::emplace(root_, std::forward<decltype(a)>(a)...)
);
return std::pair(iterator(&root_, n, p), s);
}
//
template <int = 0>
auto equal_range(auto const& k) noexcept
requires(detail::Comparable<Compare, decltype(k), key_type>)
{
auto const [nl, g](detail::equal_range(root_, {}, k));
return std::pair(iterator(&root_, nl), iterator(&root_, g));
}
auto equal_range(key_type const k) noexcept { return equal_range<0>(k); }
template <int = 0>
auto equal_range(auto const& k) const noexcept
requires(detail::Comparable<Compare, decltype(k), key_type>)
{
auto const [nl, g](detail::equal_range(root_, {}, k));
return std::pair(const_iterator(&root_, nl), const_iterator(&root_, g));
}
auto equal_range(key_type const k) const noexcept
{
return equal_range<0>(k);
}
//
template <int = 0>
size_type erase(auto&& k)
noexcept(noexcept(detail::erase(root_, k)))
requires(detail::Comparable<Compare, decltype(k), key_type> &&
!std::convertible_to<decltype(k), const_iterator>)
{
return bool(
std::get<0>(detail::erase(root_, std::forward<decltype(k)>(k)))
);
}
auto erase(key_type const k)
noexcept(noexcept(erase<0>(k)))
requires(detail::Comparable<Compare, decltype(k), key_type>)
{
return erase<0>(k);
}
iterator erase(const_iterator const i)
noexcept(noexcept(
detail::erase(
root_,
const_cast<node*>(i.n_),
const_cast<node*>(i.p_)
)
)
)
{
return {
&root_,
detail::erase(
root_,
const_cast<node*>(i.n_),
const_cast<node*>(i.p_)
)
};
}
//
template <int = 0>
auto insert(auto&& k)
noexcept(noexcept(node::emplace(root_, std::forward<decltype(k)>(k))))
requires(detail::Comparable<Compare, decltype(k), key_type>)
{
auto const [n, p, s](node::emplace(root_, std::forward<decltype(k)>(k)));
return std::pair(iterator(&root_, n, p), s);
}
auto insert(key_type k)
noexcept(noexcept(insert<0>(std::move(k))))
{
return insert<0>(std::move(k));
}
void insert(std::input_iterator auto const i, decltype(i) j)
noexcept(noexcept(emplace(*i)))
{
std::for_each(
i,
j,
[&](auto&& v) noexcept(noexcept(emplace(std::forward<decltype(v)>(v))))
{
emplace(std::forward<decltype(v)>(v));
}
);
}
};
//////////////////////////////////////////////////////////////////////////////
template <int = 0, typename K, class C>
inline auto erase(set<K, C>& c, auto const& k)
noexcept(noexcept(c.erase(K(k))))
requires(!detail::Comparable<C, decltype(k), K>)
{
return c.erase(K(k));
}
template <int = 0, typename K, class C>
inline auto erase(set<K, C>& c, auto const& k)
noexcept(noexcept(c.erase(k)))
requires(detail::Comparable<C, decltype(k), K>)
{
return c.erase(k);
}
template <typename K, class C>
inline auto erase(set<K, C>& c, K const k)
noexcept(noexcept(erase<0>(c, k)))
{
return erase<0>(c, k);
}
template <typename K, class C>
inline auto erase_if(set<K, C>& c, auto pred)
noexcept(noexcept(pred(std::declval<K const&>()), c.erase(c.begin())))
{
typename std::remove_reference_t<decltype(c)>::size_type r{};
for (auto i(c.begin()); i.n(); pred(*i) ? ++r, i = c.erase(i) : ++i);
return r;
}
//////////////////////////////////////////////////////////////////////////////
template <typename K, class C>
inline void swap(set<K, C>& l, decltype(l) r) noexcept { l.swap(r); }
}
#endif // XSG_SET_HPP