swift
Loading...
Searching...
No Matches
range.h
Go to the documentation of this file.
1// SPDX-FileCopyrightText: Copyright (C) 2013 swift Project Community / Contributors
2// SPDX-License-Identifier: GPL-3.0-or-later OR LicenseRef-swift-pilot-client-1
3
5
6#ifndef SWIFT_MISC_RANGE_H
7#define SWIFT_MISC_RANGE_H
8
9#include <algorithm>
10#include <iterator>
11#include <type_traits>
12
13#include <QDebug>
14#include <QtGlobal>
15
16#include "misc/algorithm.h"
17#include "misc/iterator.h"
18#include "misc/predicates.h"
19#include "misc/typetraits.h"
20
21namespace swift::misc
22{
23 template <class>
24 class CRange;
25
31 template <class Derived>
33 {
34 public:
36 template <class F>
37 inline auto transform(F function) const;
38
40 template <class Predicate>
41 inline auto findBy(Predicate p) const;
42
48 template <class K0, class V0, class... KeysValues>
49 inline auto findBy(K0 k0, V0 v0, KeysValues... keysValues) const;
50
53 template <class Predicate>
54 const auto &findFirstBy(Predicate p) const
55 {
56 return findBy(p).front();
57 }
58
61 template <class K, class V>
62 const auto &findFirstBy(K key, V value) const
63 {
64 return findBy(key, value).front();
65 }
66
69 template <class Predicate, class Value>
70 auto findFirstByOrDefault(Predicate p, const Value &def) const
71 {
72 return findBy(p).frontOrDefault(def);
73 }
74
77 template <class Predicate>
78 auto findFirstByOrDefault(Predicate p) const
79 {
80 return findBy(p).frontOrDefault();
81 }
82
85 template <class K, class V, class Value>
86 auto findFirstByOrDefault(K key, V value, const Value &def) const
87 {
88 return findBy(key, value).frontOrDefault(def);
89 }
90
93 template <class T, class K, class V>
94 auto findFirstByOrDefault(K T::*key, V value) const
95 {
96 return findBy(key, value).frontOrDefault();
97 }
98
100 template <class Predicate>
101 bool containsBy(Predicate p) const
102 {
103 return std::any_of(derived().cbegin(), derived().cend(), p);
104 }
105
108 template <class T>
109 bool contains(const T &object) const
110 {
111 return derived().find(object) != derived().cend();
112 }
113
119 template <class K0, class V0, class... KeysValues>
120 bool contains(K0 k0, V0 v0, KeysValues... keysValues) const
121 {
122 return containsBy(swift::misc::predicates::MemberEqual(k0, v0, keysValues...));
123 }
124
126 template <class T, class Predicate>
127 bool equalsBy(const T &other, Predicate c) const
128 {
129 if (equalPointers(&derived(), &other))
130 {
131 return true;
132 }
133 return std::equal(derived().begin(), derived().end(), other.begin(), other.end(), c);
134 }
135
137 template <class T, class Key0, class... Keys>
138 bool equalsByKeys(const T &other, Key0 k0, Keys... keys) const
139 {
140 return equalsBy(other, swift::misc::predicates::EqualsByMembers(k0, keys...));
141 }
142
144 template <class T>
146 {
147 return this->randomElements(1).front();
148 }
149
151 Derived randomElements(int n) const
152 {
153 Derived result;
154 swift::misc::copyRandomElements(derived().begin(), derived().end(), std::inserter(result, result.end()), n);
155 return result;
156 }
157
159 Derived sampleElements(int n) const
160 {
161 Derived result;
162 swift::misc::copySampleElements(derived().begin(), derived().end(), std::inserter(result, result.end()), n);
163 return result;
164 }
165
166 protected:
168 template <typename T, typename U>
169 static bool equalPointers(const T *a, const U *b)
170 {
172 {
173 return a == b;
174 }
175 else
176 {
177 return false;
178 }
179 }
180
181 private:
182 Derived &derived() { return static_cast<Derived &>(*this); }
183 const Derived &derived() const { return static_cast<const Derived &>(*this); }
184 };
185
197 template <class I>
198 class CRange : public CRangeBase<CRange<I>>
199 {
200 public:
203 using value_type = typename std::iterator_traits<I>::value_type;
204 using reference = typename std::iterator_traits<I>::reference;
205 using difference_type = typename std::iterator_traits<I>::difference_type;
209 using iterator = I;
210 using const_iterator = I;
211 using reverse_iterator = std::reverse_iterator<I>;
212 using const_reverse_iterator = std::reverse_iterator<I>;
214
216 CRange(I begin, I end) : m_begin(begin), m_end(end) { check(&begin, &end); }
217
220 const_iterator begin() const { return m_begin; }
221 const_iterator cbegin() const { return m_begin; }
222 const_iterator end() const { return m_end; }
223 const_iterator cend() const { return m_end; }
225
233
236 {
237 static_assert(std::is_same_v<decltype(*rbegin()), decltype(*begin())>);
238 return { rbegin(), rend() };
239 }
240
242 template <class T>
243 operator T() const requires(std::is_convertible_v<value_type, typename T::value_type>)
244 {
245 return to<T>();
246 }
247
250 template <class T>
251 T to() const
252 {
253 T container;
254 std::copy(begin(), end(), Iterators::makeInsertIterator(container));
255 return container;
256 }
257 template <template <class...> class T>
258 auto to() const
259 {
260 return to<T<value_type>>();
261 }
262
263
266 bool empty() const { return begin() == end(); }
267 bool isEmpty() const { return empty(); }
269
271 size_type size() const { return std::distance(begin(), end()); }
272
275 {
276 Q_ASSERT(!empty());
277 return *begin();
278 }
279
282 {
283 static const value_type def {};
284 return empty() ? def : *begin();
285 }
286
288 value_type frontOrDefault(value_type def) const { return empty() ? def : *begin(); }
289
290 private:
291 I m_begin;
292 I m_end;
293
294 void check(...) {}
295 template <class I2, class F>
296 void check(Iterators::ConditionalIterator<I2, F> *begin, Iterators::ConditionalIterator<I2, F> *end)
297 {
298 begin->checkEnd(*end);
299 }
300 };
301
303
306 template <class I>
308 {
309 for (const auto &v : range)
310 {
311 d << v;
312 }
313 return d;
314 }
315 template <class I>
316 QNoDebug operator<<(QNoDebug d, const CRange<I> &)
317 {
318 return d;
319 }
320
321
327 template <class I, class I2>
329 {
330 return { begin, static_cast<I>(end) };
331 }
332
334
337 template <class T>
338 auto makeRange(T &container) -> CRange<decltype(container.begin())>
339 {
340 return { container.begin(), container.end() };
341 }
342 template <class T>
343 auto makeRange(const T &container) -> CRange<decltype(container.begin())>
344 {
345 return { container.begin(), container.end() };
346 }
347
348
352 template <class T>
353 auto makeConstRange(const T &container) -> CRange<decltype(container.cbegin())>
354 {
355 return { container.cbegin(), container.cend() };
356 }
357
359
364 template <class T>
365 auto makeKeysRange(const T &container)
366 {
367 return makeRange(container.keyBegin(), container.keyEnd());
368 }
369 template <class T>
370 void makeKeysRange(T &container)
371 {
372 container.detach(); // http://doc.qt.io/qt-5/containers.html#implicit-sharing-iterator-problem
373 return makeRange(container.keyValueBegin(), container.keyValueEnd());
374 }
375
376
378
385 template <class T>
386 auto makePairsRange(const T &container)
387 {
388 return makeRange(container.keyValueBegin(), container.keyValueEnd());
389 }
390 template <class T>
391 auto makePairsRange(T &container)
392 {
393 container.detach(); // http://doc.qt.io/qt-5/containers.html#implicit-sharing-iterator-problem
394 return makeRange(container.keyValueBegin(), container.keyValueEnd());
395 }
396
397
401 template <class T>
402 void makeKeysRange(const T &&container) = delete;
403
407 template <class T>
408 void makePairsRange(const T &&container) = delete;
409
410 /*
411 * Member functions of CRangeBase template defined out of line, because they depend on CRange etc.
412 */
413 template <class Derived>
414 template <class F>
415 auto CRangeBase<Derived>::transform(F function) const
416 {
417 return makeRange(Iterators::makeTransformIterator(derived().cbegin(), function), derived().cend());
418 }
419
420 template <class Derived>
421 template <class Predicate>
422 auto CRangeBase<Derived>::findBy(Predicate p) const
423 {
424 return makeRange(Iterators::makeConditionalIterator(derived().cbegin(), derived().cend(), p), derived().cend());
425 }
426
427 template <class Derived>
428 template <class K0, class V0, class... KeysValues>
429 auto CRangeBase<Derived>::findBy(K0 k0, V0 v0, KeysValues... keysValues) const
430 {
431 return findBy(swift::misc::predicates::MemberEqual(k0, v0, keysValues...));
432 }
433
434} // namespace swift::misc
435
436#endif // SWIFT_MISC_RANGE_H
Any container class with begin and end iterators can inherit from this CRTP class to gain some useful...
Definition range.h:33
Derived sampleElements(int n) const
Copy n elements from the container, randomly selected but evenly distributed.
Definition range.h:159
bool equalsByKeys(const T &other, Key0 k0, Keys... keys) const
Return true if this container equals another container, considering only the given element members.
Definition range.h:138
auto findFirstByOrDefault(Predicate p) const
Return a copy of the first element for which a given predicate returns true, or a default value if th...
Definition range.h:78
Derived randomElements(int n) const
Copy n elements from the container at random.
Definition range.h:151
bool equalsBy(const T &other, Predicate c) const
Return true if this container equals another container according to the given element equality predic...
Definition range.h:127
const auto & findFirstBy(K key, V value) const
Return a reference to the first element matching some particular key/value pair(s)....
Definition range.h:62
auto findFirstByOrDefault(K key, V value, const Value &def) const
Return a copy of the first element matching some particular key/value pair(s), or a default value if ...
Definition range.h:86
auto findBy(Predicate p) const
Return a copy containing only those elements for which a given predicate returns true.
Definition range.h:422
static bool equalPointers(const T *a, const U *b)
Efficiently compare addresses of two objects. Return false if types are not compatible.
Definition range.h:169
auto findFirstByOrDefault(K T::*key, V value) const
Return a copy of the first element matching some particular key/value pair(s), or a default value if ...
Definition range.h:94
auto findFirstByOrDefault(Predicate p, const Value &def) const
Return a copy of the first element for which a given predicate returns true, or a default value if th...
Definition range.h:70
bool containsBy(Predicate p) const
Return true if there is an element for which a given predicate returns true.
Definition range.h:101
const auto & findFirstBy(Predicate p) const
Return a reference to the first element for which a given predicate returns true. Undefined if there ...
Definition range.h:54
T randomElement() const
Pick one random element.
Definition range.h:145
bool contains(K0 k0, V0 v0, KeysValues... keysValues) const
Return a copy containing only those elements matching some particular key/value pair(s).
Definition range.h:120
bool contains(const T &object) const
Return true if there is an element equal to given object. Uses the most efficient implementation avai...
Definition range.h:109
auto transform(F function) const
Return a new container generated by applying some transformation function to all elements of this one...
Definition range.h:415
A range is a conceptual container which does not contain any elements of its own, but is constructed ...
Definition range.h:199
value_type frontOrDefault(value_type def) const
Returns the element at the beginning of the range, or a default value if the range is empty.
Definition range.h:288
I iterator
STL compatibility.
Definition range.h:209
const value_type & const_reference
STL compatibility.
Definition range.h:206
const_iterator cbegin() const
Begin and end iterators.
Definition range.h:221
typename std::iterator_traits< I >::difference_type difference_type
STL compatibility.
Definition range.h:205
CRange(I begin, I end)
Constructor.
Definition range.h:216
const_reverse_iterator crbegin() const
Reverse begin and end iterators.
Definition range.h:229
const_reverse_iterator crend() const
Reverse begin and end iterators.
Definition range.h:231
value_type key_type
STL compatibility.
Definition range.h:207
CRange< const_reverse_iterator > reverse() const
Create a range from reverse iterators.
Definition range.h:235
difference_type size_type
STL compatibility.
Definition range.h:208
size_type size() const
Returns the number of elements in the range.
Definition range.h:271
T to() const
Explicit conversion to any container of value_type which supports push_back. This will copy elements.
Definition range.h:251
typename std::iterator_traits< I >::reference reference
STL compatibility.
Definition range.h:204
auto to() const
Explicit conversion to any container of value_type which supports push_back. This will copy elements.
Definition range.h:258
bool empty() const
Returns true if the range is empty.
Definition range.h:266
const_iterator end() const
Begin and end iterators.
Definition range.h:222
const_iterator begin() const
Begin and end iterators.
Definition range.h:220
const_reverse_iterator rend() const
Reverse begin and end iterators.
Definition range.h:230
I const_iterator
STL compatibility.
Definition range.h:210
bool isEmpty() const
Returns true if the range is empty.
Definition range.h:267
const_reverse_iterator rbegin() const
Reverse begin and end iterators.
Definition range.h:228
std::reverse_iterator< I > const_reverse_iterator
STL compatibility.
Definition range.h:212
typename std::iterator_traits< I >::value_type value_type
STL compatibility.
Definition range.h:203
const_reference frontOrDefault() const
Returns the element at the beginning of the range, or a default value if the range is empty.
Definition range.h:281
std::reverse_iterator< I > reverse_iterator
STL compatibility.
Definition range.h:211
const_reference front() const
Returns the element at the beginning of the range. Undefined if the range is empty.
Definition range.h:274
const_iterator cend() const
Begin and end iterators.
Definition range.h:223
auto makeTransformIterator(I iterator, F function) -> TransformIterator< I, F >
Construct a TransformIterator of the appropriate type from deduced template function arguments.
Definition iterator.h:353
auto makeInsertIterator(T &container)
Return an insert iterator appropriate to the container type (uses push_back or insert).
Definition iterator.h:88
auto makeConditionalIterator(I iterator, I end, F predicate) -> ConditionalIterator< I, F >
Construct a ConditionalIterator of the appropriate type from deduced template function arguments.
Definition iterator.h:362
auto EqualsByMembers(Ts... vs)
Returns a predicate that returns true if its arguments compare equal to each other,...
Definition predicates.h:96
auto MemberEqual(Ts... vs)
Predicate which tests whether some member functions return some values.
Definition predicates.h:26
Free functions in swift::misc.
T::const_iterator begin(const LockFreeReader< T > &reader)
Non-member begin() and end() for so LockFree containers can be used in ranged for loops.
Definition lockfree.h:258
auto makeKeysRange(const T &container)
Returns a const CRange for iterating over the keys of a Qt associative container.
Definition range.h:365
auto makeRange(I begin, I2 end) -> CRange< I >
Returns a CRange constructed from begin and end iterators of deduced types.
Definition range.h:328
auto makeConstRange(const T &container) -> CRange< decltype(container.cbegin())>
Returns a const CRange constructed from the cbegin and cend iterators of the given container.
Definition range.h:353
T::const_iterator end(const LockFreeReader< T > &reader)
Non-member begin() and end() for so LockFree containers can be used in ranged for loops.
Definition lockfree.h:264
QDebug operator<<(QDebug d, const CRange< I > &range)
Streaming operators for CRange to qDebug.
Definition range.h:307
void copySampleElements(ForwardIt in, ForwardIt end, OutputIt out, int n, Generator &&rng)
Split the range [in,end) into n equal chunks and use the random number generator rng to choose one el...
Definition algorithm.h:89
void copyRandomElements(ForwardIt in, ForwardIt end, OutputIt out, int n, Generator &&rng)
Use the random number generator rng to choose n elements from the range [in,end) and copy them to out...
Definition algorithm.h:63
auto makePairsRange(const T &container)
Returns a const CRange for iterating over the keys and values of a Qt associative container.
Definition range.h:386
Trait which is true if the expression a == b is valid when a and b are instances of T and U.
Definition typetraits.h:164