-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcommon.hpp
More file actions
211 lines (173 loc) · 4.79 KB
/
common.hpp
File metadata and controls
211 lines (173 loc) · 4.79 KB
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
// iterators
iterator begin() noexcept
{
return root_ ?
iterator(&root_, detail::first_node(root_, {})) :
iterator(&root_);
}
iterator end() noexcept { return iterator(&root_); }
// const iterators
const_iterator begin() const noexcept
{
return root_ ?
const_iterator(&root_, detail::first_node(root_, {})) :
const_iterator(&root_);
}
const_iterator end() const noexcept { return const_iterator(&root_); }
auto cbegin() const noexcept { return begin(); }
auto cend() const noexcept { return end(); }
// reverse iterators
reverse_iterator rbegin() noexcept
{
return reverse_iterator(iterator(&root_));
}
reverse_iterator rend() noexcept
{
return root_ ?
reverse_iterator(iterator(&root_, detail::first_node(root_, {}))) :
reverse_iterator(iterator(&root_));
}
// const reverse iterators
const_reverse_iterator rbegin() const noexcept
{
return const_reverse_iterator(const_iterator(&root_));
}
const_reverse_iterator rend() const noexcept
{
return root_ ?
const_reverse_iterator(
const_iterator(&root_, detail::first_node(root_, {}))
) :
const_reverse_iterator(const_iterator(&root_));
}
auto crbegin() const noexcept { return rbegin(); }
auto crend() const noexcept { return rend(); }
// self-assign neglected
auto& operator=(this_class const& o)
noexcept(noexcept(clear(), insert(o.begin(), o.end())))
requires(std::is_copy_constructible_v<value_type>)
{
if (this != &o) clear(), insert(o.begin(), o.end());
return *this;
}
auto& operator=(this_class&& o)
noexcept(noexcept(detail::destroy(root_, {})))
{
detail::destroy(root_, {});
detail::assign(root_, o.root_)(o.root_, nullptr);
return *this;
}
auto& operator=(std::initializer_list<value_type> const l)
noexcept(noexcept(clear(), insert(l.begin(), l.end())))
{
clear(); insert(l.begin(), l.end());
return *this;
}
//
friend bool operator==(this_class const& l, this_class const& r)
noexcept(noexcept(std::equal(l.begin(), l.end(), r.begin(), r.end())))
{
return std::equal(l.begin(), l.end(), r.begin(), r.end());
}
friend auto operator<=>(this_class const& l, this_class const& r)
noexcept(noexcept(
std::lexicographical_compare_three_way(
l.begin(), l.end(),
r.begin(), r.end()
)
)
)
{
return std::lexicographical_compare_three_way(
l.begin(), l.end(),
r.begin(), r.end()
);
}
//
auto root() const noexcept { return root_; }
//
static constexpr size_type max_size() noexcept
{
return ~size_type{} / sizeof(node*);
}
void clear() noexcept(noexcept(detail::destroy(root_, {})))
{
detail::destroy(root_, {}); root_ = {};
}
bool empty() const noexcept { return !root_; }
void swap(this_class& o) noexcept
{
detail::assign(root_, o.root_)(o.root_, root_);
}
//
template <int = 0>
bool contains(auto const& k) const noexcept
requires(detail::Comparable<Compare, key_type, decltype(k)>)
{
return std::get<0>(detail::find(root_, {}, k));
}
auto contains(key_type const k) const noexcept { return contains<0>(k); }
//
iterator erase(const_iterator a, const_iterator const b)
noexcept(noexcept(erase(a)))
{
while (a != b) { a = erase(a); } return {&root_, a.n(), a.p()};
}
//
template <int = 0>
iterator find(auto const& k) noexcept
requires(detail::Comparable<Compare, key_type, decltype(k)>)
{
return {&root_, detail::find(root_, {}, k)};
}
auto find(key_type const k) noexcept { return find<0>(k); }
template <int = 0>
const_iterator find(auto const& k) const noexcept
requires(detail::Comparable<Compare, key_type, decltype(k)>)
{
return {&root_, detail::find(root_, {}, k)};
}
auto find(key_type const k) const noexcept { return find<0>(k); }
//
void insert(std::initializer_list<value_type> const l)
noexcept(noexcept(insert(l.begin(), l.end())))
requires(std::is_copy_constructible_v<value_type>)
{
insert(l.begin(), l.end());
}
//
template <int = 0>
iterator lower_bound(auto const& k) noexcept
requires(detail::Comparable<Compare, decltype(k), key_type>)
{
return std::get<0>(equal_range(k));
}
auto lower_bound(key_type const k) noexcept { return lower_bound<0>(k); }
template <int = 0>
const_iterator lower_bound(auto const& k) const noexcept
requires(detail::Comparable<Compare, decltype(k), key_type>)
{
return std::get<0>(equal_range(k));
}
auto lower_bound(key_type const k) const noexcept
{
return lower_bound<0>(k);
}
//
template <int = 0>
iterator upper_bound(auto const& k) noexcept
requires(detail::Comparable<Compare, decltype(k), key_type>)
{
return std::get<1>(equal_range(k));
}
auto upper_bound(key_type const k) noexcept { return upper_bound<0>(k); }
template <int = 0>
const_iterator upper_bound(auto const& k) const noexcept
requires(detail::Comparable<Compare, decltype(k), key_type>)
{
return std::get<1>(equal_range(k));
}
auto upper_bound(key_type const k) const noexcept
{
return upper_bound<0>(k);
}