-
Notifications
You must be signed in to change notification settings - Fork 70
/
OrderedSet.h
308 lines (288 loc) · 13.4 KB
/
OrderedSet.h
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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
/* -*- mode: C++; c-basic-offset: 4; tab-width: 4 -*-
*
* Copyright (c) 2021 Apple Inc. All rights reserved.
*
* @APPLE_LICENSE_HEADER_START@
*
* This file contains Original Code and/or Modifications of Original Code
* as defined in and that are subject to the Apple Public Source License
* Version 2.0 (the 'License'). You may not use this file except in
* compliance with the License. Please obtain a copy of the License at
* http://www.opensource.apple.com/apsl/ and read it before using this
* file.
*
* The Original Code and all software distributed under the License are
* distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
* EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
* INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
* Please see the License for the specific language governing rights and
* limitations under the License.
*
* @APPLE_LICENSE_HEADER_END@
*/
#ifndef LSL_OrderedSet_h
#define LSL_OrderedSet_h
#include "BTree.h"
namespace lsl {
struct ConstCharStarCompare {
bool operator() (const char* x, const char *y) const {
for (size_t i = 0;; ++i) {
if (x[i] < y[i]) {
return true;
} else if (x[i] > y[i]) {
return false;
} else if (y[i] == 0) {
return false;
} else if (x[i] == 0) {
return true;
}
}
}
};
template<typename T, class C=std::less<T>>
struct TRIVIAL_ABI OrderedSet {
using key_type = T;
using value_type = T;
using key_compare = C;
using value_compare = C;
using difference_type = std::ptrdiff_t;
using reference = value_type&;
using pointer = value_type*;
using size_type = std::size_t;
struct const_iterator {
using iterator_category = std::bidirectional_iterator_tag;
using value_type = T;
using difference_type = std::ptrdiff_t;
using pointer = value_type*;
using reference = value_type&;
const_iterator(const const_iterator& other) : _i(other._i) {}
const_iterator(const_iterator&& other) : _i(other._i) {
swap(other);
}
const_iterator& operator=(const const_iterator& other) {
auto tmp = other;
swap(tmp);
return *this;
}
const_iterator& operator=(const_iterator&& other) {
swap(other);
return *this;
}
reference operator*() {
return *_i;
}
pointer operator->() {
// So this is gross. std::map avoid this by never moving
return (pointer)&*_i;
}
const_iterator& operator++() {
++_i;
return *this;
}
const_iterator operator++(int) {
auto tmp = *this;
++*this;
return tmp;
}
const_iterator& operator--() {
--_i;
return *this;
}
const_iterator operator--(int) const {
auto result = *this;
--*this;
return result;
}
std::strong_ordering operator<=>(const const_iterator& other) const = default;
const_iterator(typename BTree<T,C,false>::const_iterator I) : _i(I) {}
friend void swap(const_iterator& x, const_iterator& y) {
x.swap(y);
}
private:
friend struct OrderedSet;
void swap(const_iterator& other) {
using std::swap;
if (this == &other) { return; }
swap(_i, other._i);
}
typename BTree<T,C,false>::const_iterator _i;
};
using iterator = const_iterator;
const_iterator cbegin() const { return _btree.begin(); }
const_iterator cend() const { return _btree.end();}
const_iterator begin() const { return cbegin(); }
const_iterator end() const { return cend(); }
iterator begin() { return std::as_const(*this).begin(); }
iterator end() { return std::as_const(*this).end(); }
iterator insert(const_iterator hint, const value_type& key) { return _btree.insert(hint._i, key).first; }
iterator insert(const_iterator hint, value_type&& key) { return _btree.insert(hint._i, std::move(key)).first; }
std::pair<iterator,bool> insert(const value_type& key) { return _btree.insert(key); }
std::pair<iterator,bool> insert(value_type&& key) { return _btree.insert(std::move(key)); }
const_iterator find(const key_type& key) const { return _btree.find(key); }
iterator find(const value_type& key) { return iterator(std::as_const(*this).find(key)); }
const_iterator lower_bound(const value_type& key) const { return _btree.lower_bound(key); }
iterator lower_bound(const value_type& key) { return iterator(std::as_const(*this).lower_bound(key)); }
iterator erase(iterator i) { return _btree.erase(i._i); }
size_type erase(const value_type& key) { return _btree.erase(key); }
size_type size() const { return _btree.size(); }
bool empty() const { return _btree.empty(); }
void clear() { return _btree.clear(); }
size_type count(const value_type& key) const { return _btree.count(); }
OrderedSet() = delete;
//OrderedSet(const OrderedSet& other); //PRIVATE
explicit OrderedSet(Allocator& allocator) : _btree(allocator) {}
explicit OrderedSet(value_compare comp, Allocator& allocator) : _btree(comp, allocator) {}
template< class InputIt1, class InputIt2>
OrderedSet( InputIt1 first, InputIt2 last, value_compare comp, Allocator& allocator) : _btree(first, last, comp, allocator) {}
template< class InputIt1, class InputIt2>
OrderedSet( InputIt1 first, InputIt2 last, Allocator& allocator) : _btree(first, last, value_compare(C()), allocator) {}
OrderedSet(const OrderedSet& other, Allocator& allocator) : _btree(other._btree, allocator) {}
OrderedSet(OrderedSet&& other) {
swap(other);
}
OrderedSet& operator=(const OrderedSet& other) {
auto tmp = other;
swap(tmp);
return *this;
}
OrderedSet& operator=(OrderedSet&& other) {
swap(other);
return *this;
}
friend void swap(OrderedSet& x, OrderedSet& y) {
x.swap(y);
}
private:
OrderedSet(const OrderedSet& other) : _btree(other._btree) {}
void swap(OrderedSet& other) {
using std::swap;
if (this == &other) { return; }
swap(_btree, other._btree);
}
BTree<T,C, false> _btree;
};
template<typename T, class C=std::less<T>>
struct TRIVIAL_ABI OrderedMultiSet {
using key_type = T;
using value_type = T;
using key_compare = C;
using value_compare = C;
using difference_type = std::ptrdiff_t;
using reference = value_type&;
using pointer = value_type*;
using size_type = std::size_t;
struct const_iterator {
using iterator_category = std::bidirectional_iterator_tag;
using value_type = T;
using difference_type = std::ptrdiff_t;
using pointer = value_type*;
using reference = value_type&;
const_iterator(const const_iterator& other) : _i(other._i) {}
const_iterator(const_iterator&& other) : _i(other._i) {
swap(other);
}
const_iterator& operator=(const const_iterator& other) {
auto tmp = other;
swap(tmp);
return *this;
}
const_iterator& operator=(const_iterator&& other) {
swap(other);
return *this;
}
reference operator*() {
return *_i;
}
pointer operator->() {
// So this is gross. std::map avoid this by never moving
return (pointer)&*_i;
}
const_iterator& operator++() {
++_i;
return *this;
}
const_iterator operator++(int) {
auto tmp = *this;
++*this;
return tmp;
}
const_iterator& operator--() {
--_i;
return *this;
}
const_iterator operator--(int) const {
auto result = *this;
--*this;
return result;
}
std::strong_ordering operator<=>(const const_iterator& other) const = default;
const_iterator(typename BTree<T,C,true>::const_iterator I) : _i(I) {}
friend void swap(const_iterator& x, const_iterator& y) {
x.swap(y);
}
private:
friend struct OrderedMultiSet;
void swap(const_iterator& other) {
using std::swap;
if (this == &other) { return; }
swap(_i, other._i);
}
typename BTree<T,C,true>::const_iterator _i;
};
using iterator = const_iterator;
const_iterator cbegin() const { return _btree.begin(); }
const_iterator cend() const { return _btree.end();}
const_iterator begin() const { return cbegin(); }
const_iterator end() const { return cend(); }
iterator begin() { return std::as_const(*this).begin(); }
iterator end() { return std::as_const(*this).end(); }
iterator insert(const_iterator hint, const value_type& key) { return _btree.insert(hint._i, key).first; }
iterator insert(const value_type& key) { return _btree.insert(key).first; }
iterator insert(const_iterator hint, value_type&& key) { return _btree.insert(hint._i, std::move(key)).first; }
iterator insert(value_type&& key) { return _btree.insert(std::move(key)).first; }
const_iterator find(const key_type& key) const { return _btree.find(key); }
iterator find(const value_type& key) { return iterator(std::as_const(*this).find(key)); }
const_iterator lower_bound(const value_type& key) const { return _btree.lower_bound(key); }
iterator lower_bound(const value_type& key) { return iterator(std::as_const(*this).lower_bound(key)); }
iterator erase(iterator i) { return _btree.erase(i._i); }
size_type erase(const value_type& key) { return _btree.erase(key); }
size_type size() const { return _btree.size(); }
bool empty() const { return _btree.empty(); }
void clear() { return _btree.clear(); }
size_type count(const value_type& key) const { return _btree.count(key); }
OrderedMultiSet() = delete;
//OrderedMultiSet(const OrderedMultiSet&) // PRIVATE
explicit OrderedMultiSet(Allocator& allocator) : _btree(allocator) {}
explicit OrderedMultiSet(value_compare comp, Allocator& allocator) : _btree(comp, allocator) {}
template< class InputIt1, class InputIt2>
OrderedMultiSet( InputIt1 first, InputIt2 last, value_compare comp, Allocator& allocator) : _btree(first, last, comp, allocator) {}
template< class InputIt1, class InputIt2>
OrderedMultiSet( InputIt1 first, InputIt2 last, Allocator& allocator) : _btree(first, last, value_compare(C()), allocator) {}
OrderedMultiSet(const OrderedMultiSet& other, Allocator& allocator) : _btree(other, allocator) {}
OrderedMultiSet(OrderedMultiSet&& other) {
swap(other);
}
OrderedMultiSet& operator=(const OrderedMultiSet& other) {
auto tmp = other;
swap(tmp);
return *this;
}
OrderedMultiSet& operator=(OrderedMultiSet&& other) {
swap(other);
return *this;
}
friend void swap(OrderedMultiSet& x, OrderedMultiSet& y) {
x.swap(y);
}
private:
OrderedMultiSet(const OrderedMultiSet& other) : _btree(other._btree) {}
void swap(OrderedMultiSet& other) {
using std::swap;
if (this == &other) { return; }
swap(_btree, other._btree);
}
BTree<T,C, true> _btree;
};
};
#endif /* LSL_OrderedSet_h */