-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_RandAccSet.cpp
More file actions
393 lines (345 loc) · 12.5 KB
/
Copy pathtest_RandAccSet.cpp
File metadata and controls
393 lines (345 loc) · 12.5 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
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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
#include <iostream>
#include <vector>
#include <string>
template<
class Key,
class Comparison = std::less<Key>,
class Allocator = std::allocator<Key>
>
class RandomAccessSet {
private:
typedef std::vector<Key, Allocator> Vector;
public:
typedef Key key_type;
typedef Key value_type;
typedef Comparison key_compare;
typedef Comparison value_compare;
typedef Allocator allocator_type;
typedef typename Allocator::const_reference const_reference;
typedef typename Vector::iterator iterator;
typedef typename Vector::const_iterator const_iterator;
typedef typename Vector::reverse_iterator reverse_iterator;
typedef typename Vector::const_reverse_iterator const_reverse_iterator;
typedef typename Vector::size_type size_type;
typedef typename Vector::difference_type difference_type;
typedef typename Vector::const_pointer const_pointer;
RandomAccessSet(const std::size_t, const Comparison& = Comparison(),
const Allocator& = Allocator());
RandomAccessSet(const Comparison& = Comparison(),
const Allocator& = Allocator());
template <class Iterator>
RandomAccessSet(Iterator, Iterator, const Comparison& = Comparison(),
const Allocator& = Allocator());
const value_type& operator[](const size_type) const;
const_iterator begin() const;
const_iterator end() const;
iterator begin();
iterator end();
const_reverse_iterator rbegin() const;
const_reverse_iterator rend() const;
reverse_iterator rbegin();
reverse_iterator rend();
const_iterator find(const key_type&) const;
iterator find(const key_type&);
bool empty() const;
size_type size() const;
size_type max_size() const;
std::pair<const_iterator, bool> insert(const value_type&);
template <class Iterator>
void insert(Iterator, Iterator);
const_iterator insert(iterator, const value_type&);
void erase(iterator position);
size_type erase(const key_type&);
void erase(iterator, iterator);
void clear();
size_type count(const key_type&) const;
key_compare key_comp() const;
value_compare value_comp() const;
const_iterator lower_bound(const key_type&) const;
const_iterator upper_bound(const key_type&) const;
iterator lower_bound(const key_type&);
iterator upper_bound(const key_type&);
std::pair<const_iterator, const_iterator> equal_range(const key_type&) const;
std::pair<iterator, iterator> equal_range(const key_type&);
allocator_type get_allocator() const;
// TODO: implement C++11 member functions 'emplace' and 'emplace_hint'
private:
std::vector<Key> vector_;
Comparison compare_;
};
template<class Key, class Comparison, class Allocator>
inline
RandomAccessSet<Key, Comparison, Allocator>::RandomAccessSet(
const Comparison& comparison,
const Allocator& allocator
)
: vector_(allocator),
compare_(comparison)
{}
template<class Key, class Comparison, class Allocator>
inline
RandomAccessSet<Key, Comparison, Allocator>::RandomAccessSet(
const std::size_t reserveSize,
const Comparison& comparison,
const Allocator& allocator
)
: vector_(allocator),
compare_(comparison)
{
vector_.reserve(reserveSize);
}
template<class Key, class Comparison, class Allocator>
template <class Iterator>
inline
RandomAccessSet<Key, Comparison, Allocator>::RandomAccessSet(
Iterator beginInput,
Iterator endInput,
const Comparison& comparison,
const Allocator& allocator
)
: vector_(allocator),
compare_(comparison)
{
while(beginInput != endInput) {
insert(*beginInput);
++beginInput;
}
}
template<class Key, class Comparison, class Allocator>
inline const typename RandomAccessSet<Key, Comparison, Allocator>::value_type&
RandomAccessSet<Key, Comparison, Allocator>::operator[](
const typename RandomAccessSet<Key, Comparison, Allocator>::size_type index
) const {
return vector_[index];
}
template<class Key, class Comparison, class Allocator>
inline typename RandomAccessSet<Key, Comparison, Allocator>::const_iterator
RandomAccessSet<Key, Comparison, Allocator>::begin() const {
return vector_.begin();
}
template<class Key, class Comparison, class Allocator>
inline typename RandomAccessSet<Key, Comparison, Allocator>::const_iterator
RandomAccessSet<Key, Comparison, Allocator>::end() const {
return vector_.end();
}
template<class Key, class Comparison, class Allocator>
inline typename RandomAccessSet<Key, Comparison, Allocator>::const_reverse_iterator
RandomAccessSet<Key, Comparison, Allocator>::rbegin() const {
return vector_.rbegin();
}
template<class Key, class Comparison, class Allocator>
inline typename RandomAccessSet<Key, Comparison, Allocator>::const_reverse_iterator
RandomAccessSet<Key, Comparison, Allocator>::rend() const {
return vector_.rend();
}
template<class Key, class Comparison, class Allocator>
inline typename RandomAccessSet<Key, Comparison, Allocator>::iterator
RandomAccessSet<Key, Comparison, Allocator>::begin() {
return vector_.begin();
}
template<class Key, class Comparison, class Allocator>
inline typename RandomAccessSet<Key, Comparison, Allocator>::iterator
RandomAccessSet<Key, Comparison, Allocator>::end() {
return vector_.end();
}
template<class Key, class Comparison, class Allocator>
inline typename RandomAccessSet<Key, Comparison, Allocator>::reverse_iterator
RandomAccessSet<Key, Comparison, Allocator>::rbegin() {
return vector_.rbegin();
}
template<class Key, class Comparison, class Allocator>
inline typename RandomAccessSet<Key, Comparison, Allocator>::reverse_iterator
RandomAccessSet<Key, Comparison, Allocator>::rend() {
return vector_.rend();
}
template<class Key, class Comparison, class Allocator>
inline bool
RandomAccessSet<Key, Comparison, Allocator>::empty() const {
return vector_.empty();
}
template<class Key, class Comparison, class Allocator>
inline typename RandomAccessSet<Key, Comparison, Allocator>::size_type
RandomAccessSet<Key, Comparison, Allocator>::size() const {
return vector_.size();
}
template<class Key, class Comparison, class Allocator>
inline typename RandomAccessSet<Key, Comparison, Allocator>::size_type
RandomAccessSet<Key, Comparison, Allocator>::max_size() const {
return vector_.max_size();
}
template<class Key, class Comparison, class Allocator>
inline std::pair<typename RandomAccessSet<Key, Comparison, Allocator>::const_iterator, bool>
RandomAccessSet<Key, Comparison, Allocator>::insert(
const typename RandomAccessSet<Key, Comparison, Allocator>::value_type& value
) {
bool found(true);
iterator i(lower_bound(static_cast<Key>(value)));
if(i == end() || compare_(static_cast<Key>(value), *i)) {
i = vector_.insert(i, static_cast<Key>(value));
found = false;
}
return std::make_pair(i, !found);
}
template<class Key, class Comparison, class Allocator>
template <class Iterator>
inline void
RandomAccessSet<Key, Comparison, Allocator>::insert(
Iterator first,
Iterator last
) {
for(; first != last; ++first) {
insert(*first);
}
}
// TODO: optimize according to C++11 specification:
// The function optimizes its insertion time if position points to the
// element that will follow the inserted element (or to the end, if it
// would be the last).
//
template<class Key, class Comparison, class Allocator>
inline typename RandomAccessSet<Key, Comparison, Allocator>::const_iterator
RandomAccessSet<Key, Comparison, Allocator>::insert(
typename RandomAccessSet<Key, Comparison, Allocator>::iterator position,
const typename RandomAccessSet<Key, Comparison, Allocator>::value_type& value
) {
std::pair<const_iterator, bool> ret;
ret = insert(value);
return ret.first;
}
template<class Key, class Comparison, class Allocator>
inline void
RandomAccessSet<Key, Comparison, Allocator>::erase(
typename RandomAccessSet<Key, Comparison, Allocator>::iterator position
) {
vector_.erase(position);
}
template<class Key, class Comparison, class Allocator>
inline typename RandomAccessSet<Key, Comparison, Allocator>::size_type
RandomAccessSet<Key, Comparison, Allocator>::erase(
const key_type& x
) {
iterator i = find(x);
if(i != vector_.end()) {
erase(i);
return 1;
}
return 0;
}
template<class Key, class Comparison, class Allocator>
inline void
RandomAccessSet<Key, Comparison, Allocator>::erase(
const typename RandomAccessSet<Key, Comparison, Allocator>::iterator first,
const typename RandomAccessSet<Key, Comparison, Allocator>::iterator last
) {
vector_.erase(first, last);
}
template<class Key, class Comparison, class Allocator>
inline void
RandomAccessSet<Key, Comparison, Allocator>::clear() {
vector_.clear();
}
template<class Key, class Comparison, class Allocator>
inline typename RandomAccessSet<Key, Comparison, Allocator>::key_compare
RandomAccessSet<Key, Comparison, Allocator>::key_comp() const {
return compare_;
}
template<class Key, class Comparison, class Allocator>
inline typename RandomAccessSet<Key, Comparison, Allocator>::value_compare
RandomAccessSet<Key, Comparison, Allocator>::value_comp() const {
return compare_;
}
template<class Key, class Comparison, class Allocator>
inline typename RandomAccessSet<Key, Comparison, Allocator>::const_iterator
RandomAccessSet<Key, Comparison, Allocator>::find(
const key_type& value
) const {
const_iterator i(lower_bound(value));
if(i != end() && compare_(value, *i)) {
i = end();
}
return i;
}
template<class Key, class Comparison, class Allocator>
inline typename RandomAccessSet<Key, Comparison, Allocator>::iterator
RandomAccessSet<Key, Comparison, Allocator>::find(
const key_type& value
) {
iterator i(lower_bound(value));
if(i != end() && compare_(value, *i)) {
i = end();
}
return i;
}
template<class Key, class Comparison, class Allocator>
inline typename RandomAccessSet<Key, Comparison, Allocator>::size_type
RandomAccessSet<Key, Comparison, Allocator>::count(
const key_type& value
) const {
if(find(value) == end()) {
return 0;
}
else {
return 1;
}
}
template<class Key, class Comparison, class Allocator>
inline typename RandomAccessSet<Key, Comparison, Allocator>::const_iterator
RandomAccessSet<Key, Comparison, Allocator>::lower_bound(
const key_type& value
) const {
return std::lower_bound(vector_.begin(), vector_.end(), value, compare_);
}
template<class Key, class Comparison, class Allocator>
inline typename RandomAccessSet<Key, Comparison, Allocator>::iterator
RandomAccessSet<Key, Comparison, Allocator>::lower_bound(
const key_type& value
) {
return std::lower_bound(vector_.begin(), vector_.end(), value, compare_);
}
template<class Key, class Comparison, class Allocator>
inline typename RandomAccessSet<Key, Comparison, Allocator>::const_iterator
RandomAccessSet<Key, Comparison, Allocator>::upper_bound(
const key_type& value
) const {
return std::upper_bound(vector_.begin(), vector_.end(), value, compare_);
}
template<class Key, class Comparison, class Allocator>
inline typename RandomAccessSet<Key, Comparison, Allocator>::iterator
RandomAccessSet<Key, Comparison, Allocator>::upper_bound(
const key_type& value
) {
return std::upper_bound(vector_.begin(), vector_.end(), value, compare_);
}
template<class Key, class Comparison, class Allocator>
inline std::pair<typename RandomAccessSet<Key, Comparison, Allocator>::const_iterator, typename RandomAccessSet<Key, Comparison, Allocator>::const_iterator>
RandomAccessSet<Key, Comparison, Allocator>::equal_range(
const key_type& value
) const {
return std::equal_range(vector_.begin(), vector_.end(), value, compare_);
}
template<class Key, class Comparison, class Allocator>
inline std::pair<typename RandomAccessSet<Key, Comparison, Allocator>::iterator, typename RandomAccessSet<Key, Comparison, Allocator>::iterator>
RandomAccessSet<Key, Comparison, Allocator>::equal_range(
const key_type& value
) {
return std::equal_range(vector_.begin(), vector_.end(), value, compare_);
}
template<class Key, class Comparison, class Allocator>
inline typename RandomAccessSet<Key, Comparison, Allocator>::allocator_type
RandomAccessSet<Key, Comparison, Allocator>::get_allocator() const {
return vector_.get_allocator();
}
#include <set>
using namespace std;
int main()
{
/*RandomAccessSet<int> myset;
for (int i = 100000000; i >=0; --i)
myset.insert(i);
cout << myset.size() << endl;
cout << myset[100] << endl;*/
set<int> myset2;
for(int i = 100000000; i >= 0; --i)
myset2.insert(i);
//cout << myset2[0] << endl;
}