forked from parallel101/stl1weekend
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSet.hpp
251 lines (198 loc) · 7.85 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
#pragma once
#include <cstddef>
#include <functional>
#include <memory>
#include <utility>
#include "_RbTree.hpp"
#include "_Common.hpp"
template <class _Tp, class _Compare = std::less<_Tp>,
class _Alloc = std::allocator<_Tp>>
struct Set : _RbTreeImpl<_Tp const, _Compare, _Alloc> {
using typename _RbTreeImpl<_Tp const, _Compare, _Alloc>::const_iterator;
using typename _RbTreeImpl<_Tp const, _Compare, _Alloc>::node_type;
using iterator = const_iterator;
using value_type = _Tp;
using size_type = std::size_t;
using difference_type = std::ptrdiff_t;
Set() = default;
explicit Set(_Compare __comp)
: _RbTreeImpl<_Tp const, _Compare, _Alloc>(__comp) {}
Set(Set &&) = default;
Set &operator=(Set &&) = default;
Set(Set const &__that) : _RbTreeImpl<_Tp const, _Compare, _Alloc>() {
this->_M_single_insert(__that.begin(), __that.end());
}
Set &operator=(Set const &__that) {
if (&__that != this) {
this->assign(__that.begin(), __that.end());
}
return *this;
}
Set &operator=(std::initializer_list<_Tp> __ilist) {
this->assign(__ilist);
return *this;
}
void assign(std::initializer_list<_Tp> __ilist) {
this->clear();
this->_M_single_insert(__ilist.begin(), __ilist.end());
}
_Compare value_comp() const noexcept {
return this->_M_comp;
}
template <class _Tv, _LIBPENGCXX_REQUIRES_TRANSPARENT_COMPARE(_Compare, _Tv, _Tp)>
const_iterator find(_Tp &&__value) const noexcept {
return this->_M_find(__value);
}
const_iterator find(_Tp const &__value) const noexcept {
return this->_M_find(__value);
}
std::pair<iterator, bool> insert(_Tp &&__value) {
return this->_M_single_emplace(std::move(__value));
}
std::pair<iterator, bool> insert(_Tp const &__value) {
return this->_M_single_emplace(__value);
}
template <class... _Ts>
std::pair<iterator, bool> emplace(_Ts &&...__value) {
return this->_M_single_emplace(std::forward<_Ts>(__value)...);
}
template <_LIBPENGCXX_REQUIRES_ITERATOR_CATEGORY(std::input_iterator,
_InputIt)>
void insert(_InputIt __first, _InputIt __last) {
return this->_M_single_insert(__first, __last);
}
using _RbTreeImpl<_Tp const, _Compare, _Alloc>::assign;
template <_LIBPENGCXX_REQUIRES_ITERATOR_CATEGORY(std::input_iterator,
_InputIt)>
void assign(_InputIt __first, _InputIt __last) {
this->clear();
return this->_M_single_insert(__first, __last);
}
using _RbTreeImpl<_Tp const, _Compare, _Alloc>::erase;
template <class _Tv, _LIBPENGCXX_REQUIRES_TRANSPARENT_COMPARE(_Compare, _Tv, _Tp)>
std::size_t erase(_Tv &&__value) {
return this->_M_single_erase(__value);
}
std::size_t erase(_Tp const &__value) {
return this->_M_single_erase(__value);
}
template <class _Tv, _LIBPENGCXX_REQUIRES_TRANSPARENT_COMPARE(_Compare, _Tv, _Tp)>
std::size_t count(_Tv &&__value) const noexcept {
return this->_M_contains(__value) ? 1 : 0;
}
std::size_t count(_Tp const &__value) const noexcept {
return this->_M_contains(__value) ? 1 : 0;
}
template <class _Tv, _LIBPENGCXX_REQUIRES_TRANSPARENT_COMPARE(_Compare, _Tv, _Tp)>
bool contains(_Tv &&__value) const noexcept {
return this->_M_contains(__value);
}
bool contains(_Tp const &__value) const noexcept {
return this->_M_contains(__value);
}
template <class _Tv, _LIBPENGCXX_REQUIRES_TRANSPARENT_COMPARE(_Compare, _Tv, _Tp)>
node_type extract(_Tv &&__value) {
iterator __it = this->_M_find(__value);
return __it != this->end() ? this->extract(__it) : node_type();
}
node_type extract(_Tp const &__value) {
iterator __it = this->_M_find(__value);
return __it != this->end() ? this->extract(__it) : node_type();
}
};
template <class _Tp, class _Compare = std::less<_Tp>,
class _Alloc = std::allocator<_Tp>>
struct MultiSet : _RbTreeImpl<_Tp const, _Compare, _Alloc> {
using typename _RbTreeImpl<_Tp const, _Compare, _Alloc>::const_iterator;
using typename _RbTreeImpl<_Tp const, _Compare, _Alloc>::node_type;
using iterator = const_iterator;
using value_type = _Tp;
using size_type = std::size_t;
using difference_type = std::ptrdiff_t;
MultiSet() = default;
explicit MultiSet(_Compare __comp)
: _RbTreeImpl<_Tp const, _Compare, _Alloc>(__comp) {}
MultiSet(MultiSet &&) = default;
MultiSet &operator=(MultiSet &&) = default;
MultiSet(MultiSet const &__that)
: _RbTreeImpl<_Tp const, _Compare, _Alloc>() {
this->_M_multi_insert(__that.begin(), __that.end());
}
MultiSet &operator=(MultiSet const &__that) {
if (&__that != this) {
this->assign(__that.begin(), __that.end());
}
return *this;
}
MultiSet &operator=(std::initializer_list<_Tp> __ilist) {
this->assign(__ilist);
return *this;
}
void assign(std::initializer_list<_Tp> __ilist) {
this->clear();
this->_M_multi_insert(__ilist.begin(), __ilist.end());
}
_Compare value_comp() const noexcept {
return this->_M_comp;
}
template <class _Tv, _LIBPENGCXX_REQUIRES_TRANSPARENT_COMPARE(_Compare, _Tv, _Tp)>
const_iterator find(_Tp &&__value) const noexcept {
return this->_M_find(__value);
}
const_iterator find(_Tp const &__value) const noexcept {
return this->_M_find(__value);
}
iterator insert(_Tp &&__value) {
return this->_M_multi_emplace(std::move(__value));
}
iterator insert(_Tp const &__value) {
return this->_M_multi_emplace(__value);
}
template <class... _Ts>
iterator emplace(_Ts &&...__value) {
return this->_M_multi_emplace(std::forward<_Ts>(__value)...);
}
template <_LIBPENGCXX_REQUIRES_ITERATOR_CATEGORY(std::input_iterator,
_InputIt)>
void insert(_InputIt __first, _InputIt __last) {
return this->_M_multi_insert(__first, __last);
}
using _RbTreeImpl<_Tp const, _Compare, _Alloc>::assign;
template <_LIBPENGCXX_REQUIRES_ITERATOR_CATEGORY(std::input_iterator,
_InputIt)>
void assign(_InputIt __first, _InputIt __last) {
this->clear();
return this->_M_multi_insert(__first, __last);
}
using _RbTreeImpl<_Tp const, _Compare, _Alloc>::erase;
template <class _Tv, _LIBPENGCXX_REQUIRES_TRANSPARENT_COMPARE(_Compare, _Tv, _Tp)>
std::size_t erase(_Tv &&__value) {
return this->_M_multi_erase(__value);
}
std::size_t erase(_Tp const &__value) {
return this->_M_multi_erase(__value);
}
template <class _Tv, _LIBPENGCXX_REQUIRES_TRANSPARENT_COMPARE(_Compare, _Tv, _Tp)>
std::size_t count(_Tv &&__value) const noexcept {
return this->_M_multi_count(__value);
}
std::size_t count(_Tp const &__value) const noexcept {
return this->_M_multi_count(__value);
}
template <class _Tv, _LIBPENGCXX_REQUIRES_TRANSPARENT_COMPARE(_Compare, _Tv, _Tp)>
bool contains(_Tv &&__value) const noexcept {
return this->_M_contains(__value);
}
bool contains(_Tp const &__value) const noexcept {
return this->_M_contains(__value);
}
template <class _Tv, _LIBPENGCXX_REQUIRES_TRANSPARENT_COMPARE(_Compare, _Tv, _Tp)>
node_type extract(_Tv &&__value) {
iterator __it = this->_M_find(__value);
return __it != this->end() ? this->extract(__it) : node_type();
}
node_type extract(_Tp const &__value) {
iterator __it = this->_M_find(__value);
return __it != this->end() ? this->extract(__it) : node_type();
}
};