-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathset.h
316 lines (252 loc) · 10.4 KB
/
set.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
309
310
311
312
313
314
315
/* THOR - THOR Template Library
* Joshua M. Kriegshauser
*
* set.h
*
* This file defines (mostly) STL-compatible set and multiset containers.
*
* Extensions/Changes to set and multiset:
* - The insert(pos, value_type) functions that support an insert hint are not implemented.
* - The value_comp() functions are not implemented.
*/
#ifndef THOR_SET_H
#define THOR_SET_H
#pragma once
#ifndef THOR_TREE_H
#include "tree.h"
#endif
#ifndef THOR_FUNCTION_H
#include "function.h"
#endif
#ifndef THOR_SORT_H
#include "sort.h"
#endif
namespace thor
{
// thor::set
template <class Key, class Compare = less<Key> > class set
{
typedef red_black_tree<Key, Key, identity<Key>, Compare> tree_type;
typedef typename tree_type::iterator mutable_iterator;
mutable_iterator make_mutable(typename tree_type::const_iterator pos) const { return *(mutable_iterator*)&pos; }
tree_type m_tree;
public:
typedef Key key_type;
typedef Key value_type;
typedef Compare key_compare;
typedef typename tree_type::pointer pointer;
typedef typename tree_type::reference reference;
typedef typename tree_type::const_pointer const_pointer;
typedef typename tree_type::const_reference const_reference;
typedef typename tree_type::size_type size_type;
typedef typename tree_type::difference_type difference_type;
// iterator and const_iterator are the same since the value can never be modified.
typedef typename tree_type::const_iterator iterator;
typedef typename tree_type::const_iterator const_iterator;
typedef typename tree_type::const_reverse_iterator reverse_iterator;
typedef typename tree_type::const_reverse_iterator const_reverse_iterator;
// constructors
set()
{}
set(const key_compare& comp) :
m_tree(comp)
{}
template <class InputIterator> set(InputIterator first, InputIterator last)
{
m_tree.insert_unique(first, last);
}
template <class InputIterator> set(InputIterator first, InputIterator last, const key_compare& comp) :
m_tree(comp)
{
m_tree.insert_unique(first, last);
}
set(const set& S) :
m_tree(S.m_tree)
{}
~set()
{}
set& operator = (const set& S)
{
m_tree = S.m_tree;
return *this;
}
// iteration
iterator begin() const { return m_tree.begin(); }
iterator end() const { return m_tree.end(); }
reverse_iterator rbegin() const { return m_tree.rbegin(); }
reverse_iterator rend() const { return m_tree.rend(); }
// size
bool empty() const { return m_tree.empty(); }
size_type size() const { return m_tree.size(); }
size_type max_size() const { return m_tree.max_size(); }
const key_compare& key_comp() const { return m_tree.key_comp(); }
void swap(set& m) { m_tree.swap(m.m_tree); }
// insertion
pair<iterator, bool> insert(const value_type& v) { return m_tree.insert_unique(v); }
template <class InputIterator> void insert(InputIterator first, InputIterator last) { m_tree.insert_unique(first, last); }
// erasing
void erase(iterator pos) { m_tree.erase(make_mutable(pos)); }
size_type erase(const key_type& k)
{
iterator i(find(k));
if (i != end())
{
m_tree.erase(make_mutable(i));
return 1;
}
return 0;
}
void erase(iterator first, iterator last) { m_tree.erase(make_mutable(first), make_mutable(last)); }
void clear() { m_tree.clear(); }
// searching
iterator find(const key_type& k) const { return m_tree.find(k); }
size_type count(const key_type& k) const { return find(k) == end() ? 0 : 1; }
iterator lower_bound(const key_type& k) const { return m_tree.lower_bound(k); }
iterator upper_bound(const key_type& k) const { return m_tree.upper_bound(k); }
pair<iterator,iterator> equal_range(const key_type& k) const { return m_tree.equal_range(k); }
};
// thor::multiset
template <class Key, class Compare = less<Key> > class multiset
{
typedef red_black_tree<Key, Key, identity<Key>, Compare> tree_type;
typedef typename tree_type::iterator mutable_iterator;
mutable_iterator make_mutable(typename tree_type::const_iterator pos) const { return *(mutable_iterator*)&pos; }
tree_type m_tree;
public:
typedef Key key_type;
typedef Key value_type;
typedef Compare key_compare;
typedef typename tree_type::pointer pointer;
typedef typename tree_type::reference reference;
typedef typename tree_type::const_pointer const_pointer;
typedef typename tree_type::const_reference const_reference;
typedef typename tree_type::size_type size_type;
typedef typename tree_type::difference_type difference_type;
// iterator and const_iterator are the same since the value can never be modified.
typedef typename tree_type::const_iterator iterator;
typedef typename tree_type::const_iterator const_iterator;
typedef typename tree_type::const_reverse_iterator reverse_iterator;
typedef typename tree_type::const_reverse_iterator const_reverse_iterator;
// constructors
multiset()
{}
multiset(const key_compare& comp) :
m_tree(comp)
{}
template <class InputIterator> multiset(InputIterator first, InputIterator last)
{
m_tree.insert_equal(first, last);
}
template <class InputIterator> multiset(InputIterator first, InputIterator last, const key_compare& comp) :
m_tree(comp)
{
m_tree.insert_equal(first, last);
}
multiset(const multiset& m) :
m_tree(m.m_tree)
{}
~multiset()
{}
multiset& operator = (const multiset& m)
{
m_tree = m.m_tree;
return *this;
}
// iteration
iterator begin() const { return m_tree.begin(); }
iterator end() const { return m_tree.end(); }
reverse_iterator rbegin() const { return m_tree.rbegin(); }
reverse_iterator rend() const { return m_tree.rend(); }
// size
bool empty() const { return m_tree.empty(); }
size_type size() const { return m_tree.size(); }
size_type max_size() const { return m_tree.max_size(); }
const key_compare& key_comp() const { return m_tree.key_comp(); }
void swap(multiset& m) { m_tree.swap(m.m_tree); }
// insertion
iterator insert(const value_type& v) { return m_tree.insert_equal(v); }
template <class InputIterator> void insert(InputIterator f, InputIterator l) { m_tree.insert_equal(f, l); }
// erasing
void erase(iterator pos) { m_tree.erase(make_mutable(pos)); }
size_type erase(const key_type& k) { return m_tree.erase(k); }
void erase(iterator first, iterator last) { m_tree.erase(make_mutable(first), make_mutable(last)); }
void clear() { m_tree.clear(); }
// searching
iterator find(const key_type& k) const { return m_tree.find(k); }
size_type count(const key_type& k) const { return m_tree.count(k); }
iterator lower_bound(const key_type& k) const { return m_tree.lower_bound(k); }
iterator upper_bound(const key_type& k) const { return m_tree.upper_bound(k); }
pair<iterator,iterator> equal_range(const key_type& k) const { return m_tree.equal_range(k); }
};
// Swap specialization
template <class Key, class Compare> void swap(set<Key, Compare>& lhs, set<Key, Compare>& rhs)
{
lhs.swap(rhs);
}
template <class Key, class Compare> void swap(multiset<Key, Compare>& lhs, multiset<Key, Compare>& rhs)
{
lhs.swap(rhs);
}
} // namespace thor
// Global operators
template <class Key, class Compare>
bool operator == (const thor::set<Key,Compare>& lhs, const thor::set<Key,Compare>& rhs)
{
return lhs.size() == rhs.size() && thor::equal(lhs.begin(), lhs.end(), rhs.begin());
}
template <class Key, class Compare>
bool operator < (const thor::set<Key,Compare>& lhs, const thor::set<Key,Compare>& rhs)
{
return thor::lexicographical_compare(lhs.begin(), lhs.end(), rhs.begin(), rhs.end());
}
template <class Key, class Compare>
bool operator != (const thor::set<Key,Compare>& lhs, const thor::set<Key,Compare>& rhs)
{
return !(lhs == rhs);
}
template <class Key, class Compare>
bool operator > (const thor::set<Key,Compare>& lhs, const thor::set<Key,Compare>& rhs)
{
return thor::lexicographical_compare(lhs.begin(), lhs.end(), rhs.begin(), rhs.end(), thor::greater<typename thor::set<Key,Compare>::value_type>());
}
template <class Key, class Compare>
bool operator <= (const thor::set<Key,Compare>& lhs, const thor::set<Key,Compare>& rhs)
{
return !(lhs > rhs);
}
template <class Key, class Compare>
bool operator >= (const thor::set<Key,Compare>& lhs, const thor::set<Key,Compare>& rhs)
{
return !(lhs < rhs);
}
template <class Key, class Compare>
bool operator == (const thor::multiset<Key,Compare>& lhs, const thor::multiset<Key,Compare>& rhs)
{
return lhs.size() == rhs.size() && thor::equal(lhs.begin(), lhs.end(), rhs.begin());
}
template <class Key, class Compare>
bool operator < (const thor::multiset<Key,Compare>& lhs, const thor::multiset<Key,Compare>& rhs)
{
return thor::lexicographical_compare(lhs.begin(), lhs.end(), rhs.begin(), rhs.end());
}
template <class Key, class Compare>
bool operator != (const thor::multiset<Key,Compare>& lhs, const thor::multiset<Key,Compare>& rhs)
{
return !(lhs == rhs);
}
template <class Key, class Compare>
bool operator > (const thor::multiset<Key,Compare>& lhs, const thor::multiset<Key,Compare>& rhs)
{
return thor::lexicographical_compare(lhs.begin(), lhs.end(), rhs.begin(), rhs.end(), thor::greater<typename thor::multiset<Key,Compare>::value_type>());
}
template <class Key, class Compare>
bool operator <= (const thor::multiset<Key,Compare>& lhs, const thor::multiset<Key,Compare>& rhs)
{
return !(lhs > rhs);
}
template <class Key, class Compare>
bool operator >= (const thor::multiset<Key,Compare>& lhs, const thor::multiset<Key,Compare>& rhs)
{
return !(lhs < rhs);
}
#endif