6 #ifndef SWIFT_MISC_ITERATOR_H
7 #define SWIFT_MISC_ITERATOR_H
11 #include <type_traits>
52 template <
typename T, std::enable_if_t<!std::is_convertible_v<T, OutputIterator>,
int> = 0>
55 m_func(std::forward<T>(value));
92 return makeOutputIterator([&container](
auto &&v) { container.push_back(std::forward<decltype(v)>(v)); });
96 return makeOutputIterator([&container](
auto &&v) { container.insert(std::forward<decltype(v)>(v)); });
106 template <
class I,
class F>
114 std::decay_t<decltype(std::declval<F>()(std::declval<typename std::iterator_traits<I>::value_type>()))>;
116 using reference =
typename std::iterator_traits<I>::reference;
121 decltype(std::declval<F>()(std::declval<
typename std::iterator_traits<I>::value_type>()));
125 struct PointerWrapper
127 PointerWrapper(std::decay_t<undecayed_type> *obj) : m_obj(std::move(*obj)) {}
128 std::decay_t<undecayed_type>
const *
operator->()
const {
return &m_obj; }
129 std::decay_t<undecayed_type>
operator*() const & {
return m_obj; }
130 std::decay_t<undecayed_type>
operator*() && {
return std::move(m_obj); }
133 const std::decay_t<undecayed_type> m_obj;
137 using pointer =
typename std::conditional<std::is_reference_v<undecayed_type>,
138 std::remove_reference_t<undecayed_type> *, PointerWrapper>::type;
166 Q_ASSERT(m_function);
167 return (*m_function)(*m_iterator);
175 Q_ASSERT(m_function);
176 auto &&obj = (*m_function)(*m_iterator);
201 template <
class I,
class F>
208 using value_type =
typename std::iterator_traits<I>::value_type;
210 using pointer =
typename std::iterator_traits<I>::pointer;
211 using reference =
typename std::iterator_traits<I>::reference;
217 while (m_iterator != m_end && !(*m_predicate)(*m_iterator)) { ++m_iterator; }
228 Q_ASSERT(m_predicate);
232 while (m_iterator != m_end && !(*m_predicate)(*m_iterator));
249 typename std::iterator_traits<I>::reference
operator*() {
return *m_iterator; }
264 Q_ASSERT(m_end == other.m_end && m_end == other.m_iterator);
284 using value_type =
typename std::iterator_traits<I>::value_type;
286 using pointer =
typename std::iterator_traits<I>::pointer;
287 using reference =
typename std::iterator_traits<I>::reference;
293 Q_ASSERT(m_iterators.size() % 2 == 0);
294 while (!m_iterators.empty() && m_iterators[0] == m_iterators[1]) { m_iterators.remove(0, 2); }
306 while (!m_iterators.empty() && m_iterators[0] == m_iterators[1]) { m_iterators.remove(0, 2); }
323 typename std::iterator_traits<I>::reference
operator*() {
return *(m_iterators[0]); }
336 QVector<I> m_iterators;
342 template <
class I,
class F>
345 return { iterator,
function };
351 template <
class I,
class F>
354 return { iterator,
end, predicate };
363 return { std::move(iterators) };
Iterator wrapper which concatenates zero or more pairs of begin and end iterators.
ConcatIterator(I end)
Implicit conversion from an end iterator.
typename std::iterator_traits< I >::difference_type difference_type
Types.
typename std::iterator_traits< I >::value_type value_type
Types.
bool operator<=(const ConcatIterator &other) const
Comparison operators.
bool operator>=(const ConcatIterator &other) const
Comparison operators.
typename std::iterator_traits< I >::pointer pointer
Types.
bool operator!=(const ConcatIterator &other) const
Comparison operators.
bool operator<(const ConcatIterator &other) const
Comparison operators.
ConcatIterator(QVector< I > iterators)
Constructor.
std::forward_iterator_tag iterator_category
Types.
std::iterator_traits< I >::reference operator*()
Dereference operator, returns the object referenced by the iterator. Undefined if iterator is at the ...
ConcatIterator & operator++()
Advance to the next element. Undefined if iterator is at the end.
typename std::iterator_traits< I >::reference reference
Types.
I operator->()
Indirection operator, returns the underlying iterator. Undefined if iterator is at the end.
bool operator>(const ConcatIterator &other) const
Comparison operators.
ConcatIterator operator++(int)
Advance to the next element. Undefined if iterator is at the end.
bool operator==(const ConcatIterator &other) const
Comparison operators.
Iterator wrapper which skips over any elements which do not satisfy a given condition predicate.
ConditionalIterator operator++(int)
Advance the iterator to the next element which matches the predicate, or the end if there are none re...
ConditionalIterator & operator++()
Advance the iterator to the next element which matches the predicate, or the end if there are none re...
ConditionalIterator(I end)
Implicit conversion from an end iterator.
typename std::iterator_traits< I >::difference_type difference_type
Types.
typename std::iterator_traits< I >::pointer pointer
Types.
typename std::iterator_traits< I >::reference reference
Types.
typename std::iterator_traits< I >::value_type value_type
Types.
std::iterator_traits< I >::reference operator*()
Dereference operator, returns the object referenced by the iterator. Undefined if iterator is at the ...
bool operator>(const ConditionalIterator &other) const
Comparison operators.
bool operator<(const ConditionalIterator &other) const
Comparison operators.
bool operator>=(const ConditionalIterator &other) const
Comparison operators.
ConditionalIterator(I iterator, I end, F predicate)
Constructor.
bool operator<=(const ConditionalIterator &other) const
Comparison operators.
bool operator!=(const ConditionalIterator &other) const
Comparison operators.
I operator->()
Indirection operator, returns the underlying iterator. Undefined if iterator is at the end.
std::forward_iterator_tag iterator_category
Types.
bool operator==(const ConditionalIterator &other) const
Comparison operators.
Configurable output iterator using a provided functor to do the insertion.
OutputIterator(const F &func)
Constructor.
OutputIterator & operator*()
Dereference (no-op)
~OutputIterator()=default
Destructor.
OutputIterator & operator=(T &&value)
Assignment operator performs the output.
std::output_iterator_tag iterator_category
Types.
OutputIterator(const OutputIterator &other)
Constructor.
OutputIterator(F &&func)
Constructor.
void difference_type
Types.
OutputIterator operator++(int)
Advance the iterator (no-op)
OutputIterator & operator=(const OutputIterator &other)
Copy assignment operator.
OutputIterator & operator++()
Advance the iterator (no-op)
Iterator classes for the containers.
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 makeOutputIterator(F &&func)
Return an output iterator of type deduced from the argument.
auto makeConditionalIterator(I iterator, I end, F predicate) -> ConditionalIterator< I, F >
Construct a ConditionalIterator of the appropriate type from deduced template function arguments.
auto makeConcatIterator(QVector< I > iterators) -> ConcatIterator< I >
Construct a ConcatIterator of the appropriate type from deduced template function arguments.
T::const_iterator end(const LockFreeReader< T > &reader)
Non-member begin() and end() for so LockFree containers can be used in ranged for loops.
Trait which is true if the expression a.push_back(v) is valid when a and v are instances of T and T::...