-
Notifications
You must be signed in to change notification settings - Fork 170
/
Copy pathKDStlContainerAdaptor.h
434 lines (382 loc) · 11 KB
/
KDStlContainerAdaptor.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
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
/****************************************************************************
** MIT License
**
** Copyright (C) 2021 Klarälvdalens Datakonsult AB, a KDAB Group company, [email protected]
** Author: Giuseppe D'Angelo <[email protected]>
**
** This file is part of KDToolBox (https://github.com/KDAB/KDToolBox).
**
** Permission is hereby granted, free of charge, to any person obtaining a copy
** of this software and associated documentation files (the "Software"), to deal
** in the Software without restriction, including without limitation the rights
** to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
** copies of the Software, ** and to permit persons to whom the Software is
** furnished to do so, subject to the following conditions:
**
** The above copyright notice and this permission notice (including the next paragraph)
** shall be included in all copies or substantial portions of the Software.
**
** THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
** IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
** FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
** AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
** LIABILITY, WHETHER IN AN ACTION OF ** CONTRACT, TORT OR OTHERWISE,
** ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
** DEALINGS IN THE SOFTWARE.
****************************************************************************/
// SPDX-License-Identifier: MIT
#ifndef KDTOOLBOX_STLCONTAINERADAPTOR_H
#define KDTOOLBOX_STLCONTAINERADAPTOR_H
#pragma once
#include <vector>
#include <algorithm>
#include <iterator>
namespace KDToolBox {
namespace StlContainerAdaptor {
template<typename T, typename... Args>
struct StdVectorAdaptor : std::vector<T, Args...>
{
using base_container = std::vector<T, Args...>;
using base_size_type = typename base_container::size_type;
using size_type = int;
using value_type = typename base_container::value_type;
// using ConstIterator = typename base_container::const_iterator;
// Construction / RO5
using base_container::base_container;
StdVectorAdaptor() = default; // work around broken compilers in C++17 mode
explicit StdVectorAdaptor(size_type count)
: base_container(base_size_type(count))
{
}
StdVectorAdaptor(size_type count, const value_type &v)
: base_container(base_size_type(count), v)
{
}
// It's a lot of churn to fix all cases
//[[deprecated("Use clone() instead; copies of non-Qt containers are not cheap")]]
StdVectorAdaptor(const StdVectorAdaptor &) = default;
//[[deprecated("Use assignFrom() instead; copies of non-Qt containers are not cheap")]]
StdVectorAdaptor &operator=(const StdVectorAdaptor &) = default;
StdVectorAdaptor(StdVectorAdaptor &&) = default;
StdVectorAdaptor &operator=(StdVectorAdaptor &&) = default;
~StdVectorAdaptor() = default;
StdVectorAdaptor clone() const
{
return *this;
}
StdVectorAdaptor &assignFrom(const StdVectorAdaptor &other)
{
return *this = other;
}
StdVectorAdaptor &assignFrom(StdVectorAdaptor &&other)
{
return *this = std::move(other);
}
// Iterators
decltype(auto) constBegin() const
{
return this->cbegin();
}
decltype(auto) constEnd() const
{
return this->cend();
}
// Data access
decltype(auto) constData() const
{
return this->data();
}
decltype(auto) at(size_type i) const
{
return base_container::operator[](base_size_type(i));
}
decltype(auto) operator[](size_type i)
{
return base_container::operator[](base_size_type(i));
}
decltype(auto) operator[](size_type i) const
{
return base_container::operator[](base_size_type(i));
}
auto value(size_type i, const T &defaultValue) const
{
if (i >= 0 && i < size())
return at(i);
return defaultValue;
}
auto value(size_type i) const
{
if (i >= 0 && i < size())
return at(i);
return value_type();
}
decltype(auto) first()
{
return this->front();
}
decltype(auto) first() const
{
return this->front();
}
decltype(auto) constFirst() const
{
return this->front();
}
decltype(auto) last()
{
return this->back();
}
decltype(auto) last() const
{
return this->back();
}
decltype(auto) constLast() const
{
return this->back();
}
// Size and capacity
decltype(auto) isEmpty() const
{
return this->empty();
}
decltype(auto) size() const
{
return size_type(base_container::size());
}
decltype(auto) count() const
{
return size();
}
decltype(auto) length() const
{
return size();
}
decltype(auto) capacity() const
{
return size_type(base_container::capacity());
}
void reserve(size_type s)
{
base_container::reserve(base_size_type(s));
}
void squeeze()
{
this->shrink_to_fit();
}
// Insertion
void append(const value_type &v)
{
this->push_back(v);
}
void append(value_type &&v)
{
this->push_back(std::move(v));
}
void append(const StdVectorAdaptor &other)
{
if (this != &other) {
this->insert(this->end(), other.begin(), other.end());
} else {
this->reserve(2 * size());
std::copy(this->begin(), this->end(), std::back_inserter(*this));
}
}
void prepend(const value_type &v)
{
this->insert(this->begin(), v);
}
void prepend(value_type &&v)
{
this->insert(this->begin(), std::move(v));
}
using base_container::insert;
decltype(auto) insert(size_type position, const value_type &v)
{
return this->insert(this->begin() + position, v);
}
decltype(auto) insert(size_type position, value_type &&v)
{
return this->insert(this->begin() + position, std::move(v));
}
// Removal
void removeFirst()
{
this->erase(this->begin());
}
void removeLast()
{
this->pop_back();
}
void remove(size_type position)
{
this->erase(this->begin() + position);
}
void remove(size_type position, size_type count)
{
const auto b = this->begin();
this->erase(b + position, b + position + count);
}
void removeAt(size_type position)
{
this->erase(this->begin() + position);
}
template<typename AT>
decltype(auto) removeAll(const AT &v)
{
const auto b = this->begin();
const auto e = this->end();
const auto i = std::remove(b, e, v);
const auto result = size_type(e - i);
this->erase(i, e);
return result;
}
template<typename AT>
bool removeOne(const AT &v)
{
const auto b = this->begin();
const auto e = this->end();
const auto i = std::find(b, e, v);
if (i == e)
return false;
this->erase(i);
return true;
}
decltype(auto) takeAt(size_type i)
{
const auto it = this->begin() + i;
const auto result = std::move(*it);
this->erase(it);
return result;
}
decltype(auto) takeLast()
{
return takeAt(size() - 1);
}
decltype(auto) takeFirst()
{
return takeAt(0);
}
// Search
template<typename AT>
bool contains(const AT &v) const
{
return indexOf(v) >= 0;
}
template<typename AT>
size_type indexOf(const AT &v, size_type from = 0) const
{
const auto s = size();
if (from < 0)
from = (std::max)(from + s, 0);
if (from < s) {
const auto b = this->begin();
const auto e = this->end();
const auto i = std::find(b + from, e, v);
if (i != e)
return size_type(i - b);
}
return size_type(-1);
}
template<typename AT>
size_type lastIndexOf(const AT &v, size_type from = -1) const
{
const auto s = size();
if (from < 0)
from += s;
else if (from >= s)
from = s - 1;
if (from >= 0) {
const auto b = this->begin();
const auto revB = std::make_reverse_iterator(b + from + 1);
const auto revE = std::make_reverse_iterator(b);
const auto i = std::find(revB, revE, v);
if (i != revE)
return size_type(i.base() - b) - 1;
}
return size_type(-1);
}
template<typename AT>
bool startsWith(const AT &v) const
{
return this->front() == v;
}
template<typename AT>
bool endsWith(const AT &v) const
{
return this->back() == v;
}
// Miscellanea
StdVectorAdaptor &fill(const value_type &v, size_type i = -1)
{
if (i < 0)
i = size();
this->assign(base_size_type(i), v);
return *this;
}
StdVectorAdaptor mid(size_type pos, size_type len = -1)
{
const auto s = size();
if (len < 0)
len = s;
len = (std::min)(len, s - pos);
const auto b = this->begin() + pos;
const auto e = b + len;
return StdVectorAdaptor(b, e);
}
void move(size_type from, size_type to)
{
const auto b = this->begin();
if (from < to)
std::rotate(b + from, b + from + 1, b + to + 1);
else
std::rotate(b + to, b + from, b + from + 1);
}
void replace(size_type pos, const value_type &v)
{
at(pos) = v;
}
void swapItemsAt(size_type i, size_type j)
{
qSwap(at(i), at(j));
}
// Misc operators
template<typename AT>
decltype(auto) operator<<(const typename StdVectorAdaptor<AT>::value_type &v)
{
push_back(v);
return *this;
}
template<typename AT>
decltype(auto) operator<<(typename StdVectorAdaptor<AT>::value_type &&v)
{
push_back(std::move(v));
return *this;
}
template<typename AT>
decltype(auto) operator<<(const StdVectorAdaptor<AT> &rhs)
{
append(rhs);
return *this;
}
template<typename AT>
decltype(auto) operator+(const StdVectorAdaptor<AT> &rhs)
{
StdVectorAdaptor<AT> result;
result.reserve(size() + rhs.size());
result.insert(result.end(), base_container::begin(), base_container::end());
result.insert(result.end(), rhs.begin(), rhs.end());
return result;
}
decltype(auto) operator+=(const StdVectorAdaptor &other)
{
append(other);
return *this;
}
};
} // namespace StlContainerAdaptor
} // namespace KDToolBox
namespace KDDockWidgets {
template<typename T, typename... Args>
using Vector = KDToolBox::StlContainerAdaptor::StdVectorAdaptor<T, Args...>;
}
#endif // KDTOOLBOX_STLCONTAINERADAPTOR_H