/
associate_iterator.h
388 lines (324 loc) · 10.9 KB
/
associate_iterator.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
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
/*
* Copyright (c) 2016-2021 Morwenn
* SPDX-License-Identifier: MIT
*/
#ifndef CPPSORT_DETAIL_ASSOCIATE_ITERATOR_H_
#define CPPSORT_DETAIL_ASSOCIATE_ITERATOR_H_
////////////////////////////////////////////////////////////
// Headers
////////////////////////////////////////////////////////////
#include <type_traits>
#include <utility>
#include <cpp-sort/utility/iter_move.h>
#include "attributes.h"
#include "iterator_traits.h"
namespace cppsort
{
namespace detail
{
//
// This header contains utility classes used when an adapter
// needs to associate a value to every element of a collection
// and uses this value to sort the collection
//
// The iterator will be associated to a value in association,
// and the utility class associate_iterator will make sure that
// the changes are propagated to the original collection
//
// The class associated_value is used when an element is moved
// out of the original collection, to make sure that it is still
// associated to the additional data
//
template<typename Iterator, typename Data>
struct association;
template<typename Value, typename Data>
struct associated_value;
template<typename Iterator, typename Data>
struct association
{
// Public types
using iterator_type = Iterator;
using data_type = Data;
// Public members
Iterator it;
Data data;
// Can't copy it, can't move-construct it either
association(const association&) = delete;
association(association&&) = delete;
association& operator=(const association&) = delete;
association(Iterator it, const Data& data):
it(std::move(it)),
data(data)
{}
association(Iterator it, Data&& data):
it(std::move(it)),
data(std::move(data))
{}
auto operator=(association&& other) noexcept
-> association&
{
*it = std::move(*other.it);
data = std::move(other.data);
return *this;
}
auto operator=(associated_value<value_type_t<Iterator>, Data>&& other)
-> association&
{
*it = std::move(other.value);
data = std::move(other.data);
return *this;
}
CPPSORT_ATTRIBUTE_NODISCARD
auto get()
-> decltype(*it)
{
return *it;
}
CPPSORT_ATTRIBUTE_NODISCARD
auto get() const
-> decltype(*it)
{
return *it;
}
};
template<typename Iterator, typename Data>
auto swap(association<Iterator, Data>& lhs, association<Iterator, Data>& rhs)
-> void
{
using std::swap;
using utility::iter_swap;
iter_swap(lhs.it, rhs.it);
swap(lhs.data, rhs.data);
}
template<typename Value, typename Data>
struct associated_value
{
// Public members
Value value;
Data data;
// Can't copy it, can't move-construct it either
associated_value(const associated_value&) = delete;
associated_value& operator=(const associated_value&) = delete;
associated_value(Value&& value, Data&& data):
value(std::move(value)),
data(std::move(data))
{}
associated_value(associated_value&& other):
value(std::move(other.value)),
data(std::move(other.data))
{}
auto operator=(associated_value&& other)
-> associated_value&
{
value = std::move(other.value);
data = std::move(other.data);
return *this;
}
CPPSORT_ATTRIBUTE_NODISCARD
auto get()
-> Value&
{
return value;
}
CPPSORT_ATTRIBUTE_NODISCARD
auto get() const
-> const Value&
{
return value;
}
// Silence GCC -Winline warning
~associated_value() noexcept {}
};
////////////////////////////////////////////////////////////
// Iterator used to sort groups
template<typename Iterator>
class associate_iterator
{
public:
////////////////////////////////////////////////////////////
// Public types
using iterator_category = std::random_access_iterator_tag;
using iterator_type = Iterator;
using value_type = value_type_t<Iterator>;
using difference_type = difference_type_t<Iterator>;
using pointer = pointer_t<Iterator>;
using reference = reference_t<Iterator>;
////////////////////////////////////////////////////////////
// Constructors
associate_iterator() = default;
explicit associate_iterator(Iterator it):
_it(std::move(it))
{}
////////////////////////////////////////////////////////////
// Members access
CPPSORT_ATTRIBUTE_NODISCARD
auto base() const
-> iterator_type
{
return _it;
}
////////////////////////////////////////////////////////////
// Element access
CPPSORT_ATTRIBUTE_NODISCARD
auto operator*() const
-> decltype(*base())
{
return *_it;
}
CPPSORT_ATTRIBUTE_NODISCARD
auto operator->() const
-> pointer
{
return &(operator*());
}
////////////////////////////////////////////////////////////
// Increment/decrement operators
auto operator++()
-> associate_iterator&
{
++_it;
return *this;
}
auto operator++(int)
-> associate_iterator
{
auto tmp = *this;
operator++();
return tmp;
}
auto operator--()
-> associate_iterator&
{
--_it;
return *this;
}
auto operator--(int)
-> associate_iterator
{
auto tmp = *this;
operator--();
return tmp;
}
auto operator+=(difference_type increment)
-> associate_iterator&
{
_it += increment;
return *this;
}
auto operator-=(difference_type increment)
-> associate_iterator&
{
_it -= increment;
return *this;
}
////////////////////////////////////////////////////////////
// Elements access operators
CPPSORT_ATTRIBUTE_NODISCARD
auto operator[](difference_type pos) const
-> decltype(base()[pos])
{
return _it[pos];
}
////////////////////////////////////////////////////////////
// Comparison operators
CPPSORT_ATTRIBUTE_NODISCARD
friend auto operator==(const associate_iterator& lhs, const associate_iterator& rhs)
-> bool
{
return lhs.base() == rhs.base();
}
CPPSORT_ATTRIBUTE_NODISCARD
friend auto operator!=(const associate_iterator& lhs, const associate_iterator& rhs)
-> bool
{
return lhs.base() != rhs.base();
}
////////////////////////////////////////////////////////////
// Relational operators
CPPSORT_ATTRIBUTE_NODISCARD
friend auto operator<(const associate_iterator& lhs, const associate_iterator& rhs)
-> bool
{
return lhs.base() < rhs.base();
}
CPPSORT_ATTRIBUTE_NODISCARD
friend auto operator<=(const associate_iterator& lhs, const associate_iterator& rhs)
-> bool
{
return lhs.base() <= rhs.base();
}
CPPSORT_ATTRIBUTE_NODISCARD
friend auto operator>(const associate_iterator& lhs, const associate_iterator& rhs)
-> bool
{
return lhs.base() > rhs.base();
}
CPPSORT_ATTRIBUTE_NODISCARD
friend auto operator>=(const associate_iterator& lhs, const associate_iterator& rhs)
-> bool
{
return lhs.base() >= rhs.base();
}
////////////////////////////////////////////////////////////
// Arithmetic operators
CPPSORT_ATTRIBUTE_NODISCARD
friend auto operator+(associate_iterator it, difference_type size)
-> associate_iterator
{
it += size;
return it;
}
CPPSORT_ATTRIBUTE_NODISCARD
friend auto operator+(difference_type size, associate_iterator it)
-> associate_iterator
{
it += size;
return it;
}
CPPSORT_ATTRIBUTE_NODISCARD
friend auto operator-(associate_iterator it, difference_type size)
-> associate_iterator
{
it -= size;
return it;
}
CPPSORT_ATTRIBUTE_NODISCARD
friend auto operator-(const associate_iterator& lhs, const associate_iterator& rhs)
-> difference_type
{
return lhs.base() - rhs.base();
}
////////////////////////////////////////////////////////////
// iter_move/iter_swap
friend auto iter_swap(associate_iterator lhs, associate_iterator rhs)
-> void
{
using utility::iter_swap;
iter_swap(lhs.base(), rhs.base());
}
CPPSORT_ATTRIBUTE_NODISCARD
friend auto iter_move(associate_iterator it)
-> associated_value<
value_type_t<typename value_type_t<Iterator>::iterator_type>,
typename value_type_t<Iterator>::data_type
>
{
return {
std::move(*(it->it)),
std::move(it->data)
};
}
private:
Iterator _it;
};
////////////////////////////////////////////////////////////
// Construction function
template<typename Iterator>
CPPSORT_ATTRIBUTE_NODISCARD
auto make_associate_iterator(Iterator it)
-> associate_iterator<Iterator>
{
return associate_iterator<Iterator>(std::move(it));
}
}}
#endif // CPPSORT_DETAIL_ASSOCIATE_ITERATOR_H_