6 #ifndef SWIFT_MISC_RANGE_H
7 #define SWIFT_MISC_RANGE_H
11 #include <type_traits>
31 template <
class Derived>
40 template <
class Predicate>
41 inline auto findBy(Predicate p)
const;
48 template <
class K0,
class V0,
class... KeysValues>
49 inline auto findBy(K0 k0, V0 v0, KeysValues... keysValues)
const;
53 template <
class Predicate>
61 template <
class K,
class V>
64 return findBy(key, value).front();
69 template <
class Predicate,
class Value>
72 return findBy(p).frontOrDefault(def);
77 template <
class Predicate>
80 return findBy(p).frontOrDefault();
85 template <
class K,
class V,
class Value>
88 return findBy(key, value).frontOrDefault(def);
93 template <
class T,
class K,
class V>
96 return findBy(key, value).frontOrDefault();
100 template <
class Predicate>
103 return std::any_of(derived().cbegin(), derived().cend(), p);
111 return derived().find(
object) != derived().cend();
119 template <
class K0,
class V0,
class... KeysValues>
120 bool contains(K0 k0, V0 v0, KeysValues... keysValues)
const
126 template <
class T,
class Predicate>
130 return std::equal(derived().
begin(), derived().
end(), other.begin(), other.end(), c);
134 template <
class T,
class Key0,
class... Keys>
165 template <
typename T,
typename U>
169 else {
return false; }
173 Derived &derived() {
return static_cast<Derived &
>(*this); }
174 const Derived &derived()
const {
return static_cast<const Derived &
>(*this); }
194 typedef typename std::iterator_traits<I>::value_type
value_type;
195 typedef typename std::iterator_traits<I>::reference
reference;
228 static_assert(std::is_same_v<decltype(*
rbegin()), decltype(*
begin())>,
"");
233 template <
class T,
class = std::enable_if_t<std::is_convertible_v<value_type,
typename T::value_type>>>
248 template <
template <
class...>
class T>
251 return to<T<value_type>>();
286 template <
class I2,
class F>
287 void check(Iterators::ConditionalIterator<I2, F> *
begin, Iterators::ConditionalIterator<I2, F> *
end)
300 for (
const auto &v : range) { d << v; }
315 template <
class I,
class I2>
318 return {
begin,
static_cast<I
>(
end) };
328 return { container.
begin(), container.end() };
333 return { container.
begin(), container.end() };
343 return { container.
cbegin(), container.cend() };
355 return makeRange(container.keyBegin(), container.keyEnd());
361 return makeRange(container.keyValueBegin(), container.keyValueEnd());
376 return makeRange(container.keyValueBegin(), container.keyValueEnd());
382 return makeRange(container.keyValueBegin(), container.keyValueEnd());
401 template <
class Derived>
408 template <
class Derived>
409 template <
class Predicate>
415 template <
class Derived>
416 template <
class K0,
class V0,
class... KeysValues>
Any container class with begin and end iterators can inherit from this CRTP class to gain some useful...
Derived sampleElements(int n) const
Copy n elements from the container, randomly selected but evenly distributed.
bool equalsByKeys(const T &other, Key0 k0, Keys... keys) const
Return true if this container equals another container, considering only the given element members.
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...
Derived randomElements(int n) const
Copy n elements from the container at random.
bool equalsBy(const T &other, Predicate c) const
Return true if this container equals another container according to the given element equality predic...
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 ...
auto findBy(Predicate p) const
Return a copy containing only those elements for which a given predicate returns true.
static bool equalPointers(const T *a, const U *b)
Efficiently compare addresses of two objects. Return false if types are not compatible.
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 ...
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...
bool containsBy(Predicate p) const
Return true if there is an element for which a given predicate returns true.
const auto & findFirstBy(Predicate p) const
Return a reference to the first element for which a given predicate returns true. Undefined if there ...
T randomElement() const
Pick one random element.
bool contains(K0 k0, V0 v0, KeysValues... keysValues) const
Return a copy containing only those elements matching some particular key/value pair(s).
const auto & findFirstBy(K key, V value) const
Return a reference to the first element matching some particular key/value pair(s)....
bool contains(const T &object) const
Return true if there is an element equal to given object. Uses the most efficient implementation avai...
auto transform(F function) const
Return a new container generated by applying some transformation function to all elements of this one...
auto findBy(K0 k0, V0 v0, KeysValues... keysValues) const
Return a copy containing only those elements matching some particular key/value pair(s).
A range is a conceptual container which does not contain any elements of its own, but is constructed ...
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.
std::iterator_traits< I >::value_type value_type
STL compatibility.
std::reverse_iterator< I > const_reverse_iterator
STL compatibility.
const_iterator cbegin() const
Begin and end iterators.
I iterator
STL compatibility.
CRange< const_reverse_iterator > reverse() const
Create a range from reverse iterators.
CRange(I begin, I end)
Constructor.
const_reverse_iterator crbegin() const
Reverse begin and end iterators.
const_reverse_iterator crend() const
Reverse begin and end iterators.
I const_iterator
STL compatibility.
std::reverse_iterator< I > reverse_iterator
STL compatibility.
difference_type size_type
STL compatibility.
size_type size() const
Returns the number of elements in the range.
T to() const
Explicit conversion to any container of value_type which supports push_back. This will copy elements.
auto to() const
Explicit conversion to any container of value_type which supports push_back. This will copy elements.
bool empty() const
Returns true if the range is empty.
const_iterator end() const
Begin and end iterators.
const_iterator begin() const
Begin and end iterators.
value_type key_type
STL compatibility.
const_reverse_iterator rend() const
Reverse begin and end iterators.
std::iterator_traits< I >::reference reference
STL compatibility.
bool isEmpty() const
Returns true if the range is empty.
const_reverse_iterator rbegin() const
Reverse begin and end iterators.
std::iterator_traits< I >::difference_type difference_type
STL compatibility.
const value_type & const_reference
STL compatibility.
const_reference frontOrDefault() const
Returns the element at the beginning of the range, or a default value if the range is empty.
const_reference front() const
Returns the element at the beginning of the range. Undefined if the range is empty.
const_iterator cend() const
Begin and end iterators.
auto makeTransformIterator(I iterator, F function) -> TransformIterator< I, F >
Construct a TransformIterator of the appropriate type from deduced template function arguments.
auto makeInsertIterator(T &container)
Return an insert iterator appropriate to the container type (uses push_back or insert).
auto makeConditionalIterator(I iterator, I end, F predicate) -> ConditionalIterator< I, F >
Construct a ConditionalIterator of the appropriate type from deduced template function arguments.
auto EqualsByMembers(Ts... vs)
Returns a predicate that returns true if its arguments compare equal to each other,...
auto MemberEqual(Ts... vs)
Predicate which tests whether some member functions return some values.
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.
auto makeKeysRange(const T &container)
Returns a const CRange for iterating over the keys of a Qt associative container.
auto makeRange(I begin, I2 end) -> CRange< I >
Returns a CRange constructed from begin and end iterators of deduced types.
auto makeConstRange(const T &container) -> CRange< decltype(container.cbegin())>
Returns a const CRange constructed from the cbegin and cend iterators of the given container.
T::const_iterator end(const LockFreeReader< T > &reader)
Non-member begin() and end() for so LockFree containers can be used in ranged for loops.
QDebug operator<<(QDebug d, const CRange< I > &range)
Streaming operators for CRange to qDebug.
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...
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...
auto makePairsRange(const T &container)
Returns a const CRange for iterating over the keys and values of a Qt associative container.
Trait which is true if the expression a == b is valid when a and b are instances of T and U.