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
|
/* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
file Copyright.txt or https://cmake.org/licensing for details. */
#pragma once
#include "cmConfigure.h" // IWYU pragma: keep
#include <algorithm>
#include <functional>
#include <iterator>
namespace RangeIterators {
template <typename Iter, typename UnaryPredicate>
class FilterIterator
{
public:
using iterator_category = std::bidirectional_iterator_tag;
using value_type = typename std::iterator_traits<Iter>::value_type;
using difference_type = typename std::iterator_traits<Iter>::difference_type;
using pointer = typename std::iterator_traits<Iter>::pointer;
using reference = typename std::iterator_traits<Iter>::reference;
FilterIterator(Iter b, Iter e, UnaryPredicate p)
: Cur(std::move(b))
, End(std::move(e))
, Pred(std::move(p))
{
this->SatisfyPredicate();
}
FilterIterator& operator++()
{
++this->Cur;
this->SatisfyPredicate();
return *this;
}
FilterIterator& operator--()
{
do {
--this->Cur;
} while (!this->Pred(*this->Cur));
return *this;
}
bool operator==(FilterIterator const& other) const
{
return this->Cur == other.Cur;
}
bool operator!=(FilterIterator const& other) const
{
return !this->operator==(other);
}
auto operator*() const -> decltype(*std::declval<Iter>())
{
return *this->Cur;
}
private:
void SatisfyPredicate()
{
while (this->Cur != this->End && !this->Pred(*this->Cur)) {
++this->Cur;
}
}
Iter Cur;
Iter End;
UnaryPredicate Pred;
};
template <typename Iter, typename UnaryFunction>
class TransformIterator
{
public:
using iterator_category = std::bidirectional_iterator_tag;
using value_type = typename std::remove_cv<
typename std::remove_reference<decltype(std::declval<UnaryFunction>()(
*std::declval<Iter>()))>::type>::type;
using difference_type = typename std::iterator_traits<Iter>::difference_type;
using pointer = value_type const*;
using reference = value_type const&;
TransformIterator(Iter i, UnaryFunction f)
: Base(std::move(i))
, Func(std::move(f))
{
}
TransformIterator& operator++()
{
++this->Base;
return *this;
}
TransformIterator& operator--()
{
--this->Base;
return *this;
}
bool operator==(TransformIterator const& other) const
{
return this->Base == other.Base;
}
bool operator!=(TransformIterator const& other) const
{
return !this->operator==(other);
}
auto operator*() const
-> decltype(std::declval<UnaryFunction>()(*std::declval<Iter>()))
{
return this->Func(*this->Base);
}
private:
Iter Base;
UnaryFunction Func;
};
} // namespace RangeIterators
template <typename Iter>
class cmRange
{
public:
using const_iterator = Iter;
using value_type = typename std::iterator_traits<Iter>::value_type;
using difference_type = typename std::iterator_traits<Iter>::difference_type;
cmRange(Iter b, Iter e)
: Begin(std::move(b))
, End(std::move(e))
{
}
Iter begin() const { return this->Begin; }
Iter end() const { return this->End; }
bool empty() const { return this->Begin == this->End; }
difference_type size() const
{
return std::distance(this->Begin, this->End);
}
cmRange& advance(difference_type amount) &
{
std::advance(this->Begin, amount);
return *this;
}
cmRange advance(difference_type amount) &&
{
std::advance(this->Begin, amount);
return std::move(*this);
}
cmRange& retreat(difference_type amount) &
{
std::advance(this->End, -amount);
return *this;
}
cmRange retreat(difference_type amount) &&
{
std::advance(this->End, -amount);
return std::move(*this);
}
template <typename UnaryPredicate>
bool all_of(UnaryPredicate p) const
{
return std::all_of(this->Begin, this->End, std::ref(p));
}
template <typename UnaryPredicate>
bool any_of(UnaryPredicate p) const
{
return std::any_of(this->Begin, this->End, std::ref(p));
}
template <typename UnaryPredicate>
bool none_of(UnaryPredicate p) const
{
return std::none_of(this->Begin, this->End, std::ref(p));
}
template <typename UnaryPredicate>
auto filter(UnaryPredicate p) const
-> cmRange<RangeIterators::FilterIterator<Iter, UnaryPredicate>>
{
using It = RangeIterators::FilterIterator<Iter, UnaryPredicate>;
return { It(this->Begin, this->End, p), It(this->End, this->End, p) };
}
template <typename UnaryFunction>
auto transform(UnaryFunction f) const
-> cmRange<RangeIterators::TransformIterator<Iter, UnaryFunction>>
{
using It = RangeIterators::TransformIterator<Iter, UnaryFunction>;
return { It(this->Begin, f), It(this->End, f) };
}
private:
Iter Begin;
Iter End;
};
template <typename Iter1, typename Iter2>
bool operator==(cmRange<Iter1> const& left, cmRange<Iter2> const& right)
{
return left.size() == right.size() &&
std::equal(left.begin(), left.end(), right.begin());
}
template <typename Iter1, typename Iter2>
auto cmMakeRange(Iter1 begin, Iter2 end) -> cmRange<Iter1>
{
return { begin, end };
}
template <typename Range>
auto cmMakeRange(Range const& range) -> cmRange<decltype(range.begin())>
{
return { range.begin(), range.end() };
}
template <typename Range>
auto cmReverseRange(Range const& range) -> cmRange<decltype(range.rbegin())>
{
return { range.rbegin(), range.rend() };
}
|