resolve cyclic dependency with zstd
[platform/upstream/cmake.git] / Source / cmRange.h
1 /* Distributed under the OSI-approved BSD 3-Clause License.  See accompanying
2    file Copyright.txt or https://cmake.org/licensing for details.  */
3 #pragma once
4
5 #include "cmConfigure.h" // IWYU pragma: keep
6
7 #include <algorithm>
8 #include <functional>
9 #include <iterator>
10
11 namespace RangeIterators {
12
13 template <typename Iter, typename UnaryPredicate>
14 class FilterIterator
15 {
16 public:
17   using iterator_category = std::bidirectional_iterator_tag;
18   using value_type = typename std::iterator_traits<Iter>::value_type;
19   using difference_type = typename std::iterator_traits<Iter>::difference_type;
20   using pointer = typename std::iterator_traits<Iter>::pointer;
21   using reference = typename std::iterator_traits<Iter>::reference;
22
23   FilterIterator(Iter b, Iter e, UnaryPredicate p)
24     : Cur(std::move(b))
25     , End(std::move(e))
26     , Pred(std::move(p))
27   {
28     this->SatisfyPredicate();
29   }
30
31   FilterIterator& operator++()
32   {
33     ++this->Cur;
34     this->SatisfyPredicate();
35     return *this;
36   }
37
38   FilterIterator& operator--()
39   {
40     do {
41       --this->Cur;
42     } while (!this->Pred(*this->Cur));
43     return *this;
44   }
45
46   bool operator==(FilterIterator const& other) const
47   {
48     return this->Cur == other.Cur;
49   }
50
51   bool operator!=(FilterIterator const& other) const
52   {
53     return !this->operator==(other);
54   }
55
56   auto operator*() const -> decltype(*std::declval<Iter>())
57   {
58     return *this->Cur;
59   }
60
61 private:
62   void SatisfyPredicate()
63   {
64     while (this->Cur != this->End && !this->Pred(*this->Cur)) {
65       ++this->Cur;
66     }
67   }
68
69   Iter Cur;
70   Iter End;
71   UnaryPredicate Pred;
72 };
73
74 template <typename Iter, typename UnaryFunction>
75 class TransformIterator
76 {
77 public:
78   using iterator_category = std::bidirectional_iterator_tag;
79   using value_type =
80     typename std::remove_cv<typename std::remove_reference<decltype(
81       std::declval<UnaryFunction>()(*std::declval<Iter>()))>::type>::type;
82   using difference_type = typename std::iterator_traits<Iter>::difference_type;
83   using pointer = value_type const*;
84   using reference = value_type const&;
85
86   TransformIterator(Iter i, UnaryFunction f)
87     : Base(std::move(i))
88     , Func(std::move(f))
89   {
90   }
91
92   TransformIterator& operator++()
93   {
94     ++this->Base;
95     return *this;
96   }
97
98   TransformIterator& operator--()
99   {
100     --this->Base;
101     return *this;
102   }
103
104   bool operator==(TransformIterator const& other) const
105   {
106     return this->Base == other.Base;
107   }
108
109   bool operator!=(TransformIterator const& other) const
110   {
111     return !this->operator==(other);
112   }
113
114   auto operator*() const
115     -> decltype(std::declval<UnaryFunction>()(*std::declval<Iter>()))
116   {
117     return this->Func(*this->Base);
118   }
119
120 private:
121   Iter Base;
122   UnaryFunction Func;
123 };
124
125 } // namespace RangeIterators
126
127 template <typename Iter>
128 class cmRange
129 {
130 public:
131   using const_iterator = Iter;
132   using value_type = typename std::iterator_traits<Iter>::value_type;
133   using difference_type = typename std::iterator_traits<Iter>::difference_type;
134
135   cmRange(Iter b, Iter e)
136     : Begin(std::move(b))
137     , End(std::move(e))
138   {
139   }
140
141   Iter begin() const { return this->Begin; }
142   Iter end() const { return this->End; }
143   bool empty() const { return this->Begin == this->End; }
144
145   difference_type size() const
146   {
147     return std::distance(this->Begin, this->End);
148   }
149
150   cmRange& advance(difference_type amount) &
151   {
152     std::advance(this->Begin, amount);
153     return *this;
154   }
155
156   cmRange advance(difference_type amount) &&
157   {
158     std::advance(this->Begin, amount);
159     return std::move(*this);
160   }
161
162   cmRange& retreat(difference_type amount) &
163   {
164     std::advance(this->End, -amount);
165     return *this;
166   }
167
168   cmRange retreat(difference_type amount) &&
169   {
170     std::advance(this->End, -amount);
171     return std::move(*this);
172   }
173
174   template <typename UnaryPredicate>
175   bool all_of(UnaryPredicate p) const
176   {
177     return std::all_of(this->Begin, this->End, std::ref(p));
178   }
179
180   template <typename UnaryPredicate>
181   bool any_of(UnaryPredicate p) const
182   {
183     return std::any_of(this->Begin, this->End, std::ref(p));
184   }
185
186   template <typename UnaryPredicate>
187   bool none_of(UnaryPredicate p) const
188   {
189     return std::none_of(this->Begin, this->End, std::ref(p));
190   }
191
192   template <typename UnaryPredicate>
193   auto filter(UnaryPredicate p) const
194     -> cmRange<RangeIterators::FilterIterator<Iter, UnaryPredicate>>
195   {
196     using It = RangeIterators::FilterIterator<Iter, UnaryPredicate>;
197     return { It(this->Begin, this->End, p), It(this->End, this->End, p) };
198   }
199
200   template <typename UnaryFunction>
201   auto transform(UnaryFunction f) const
202     -> cmRange<RangeIterators::TransformIterator<Iter, UnaryFunction>>
203   {
204     using It = RangeIterators::TransformIterator<Iter, UnaryFunction>;
205     return { It(this->Begin, f), It(this->End, f) };
206   }
207
208 private:
209   Iter Begin;
210   Iter End;
211 };
212
213 template <typename Iter1, typename Iter2>
214 bool operator==(cmRange<Iter1> const& left, cmRange<Iter2> const& right)
215 {
216   return left.size() == right.size() &&
217     std::equal(left.begin(), left.end(), right.begin());
218 }
219
220 template <typename Iter1, typename Iter2>
221 auto cmMakeRange(Iter1 begin, Iter2 end) -> cmRange<Iter1>
222 {
223   return { begin, end };
224 }
225
226 template <typename Range>
227 auto cmMakeRange(Range const& range) -> cmRange<decltype(range.begin())>
228 {
229   return { range.begin(), range.end() };
230 }
231
232 template <typename Range>
233 auto cmReverseRange(Range const& range) -> cmRange<decltype(range.rbegin())>
234 {
235   return { range.rbegin(), range.rend() };
236 }