| Taskflow: A General-purpose Task-parallel Programming System

Loading...
Searching...
No Matches
iterator.hpp
1#pragma once
2
3#include <concepts>
4#include <cstddef>
5#include <type_traits>
6
7 namespace tf {
8
27 template <std::integral B, std::integral E, std::integral S>
28 constexpr bool is_index_range_invalid(B beg, E end, S step) {
29 return ((step == 0 && beg != end) ||
30 (beg < end && step <= 0) || // positive range
31 (beg > end && step >= 0)); // negative range
32}
33
72 template <std::integral B, std::integral E, std::integral S>
73 constexpr size_t distance(B beg, E end, S step) {
74 return (end - beg + step + (step > 0 ? -1 : 1)) / step;
75}
76
77// ----------------------------------------------------------------------------
78// IndexRange
79// ----------------------------------------------------------------------------
80
116 template <typename T>
117 class IndexRange {
118
119 static_assert(std::is_integral_v<T>, "index type must be integral");
120
121 public:
122
126 using index_type = T;
127
131 IndexRange() = default;
132
139 explicit IndexRange(T beg, T end, T step_size)
140 : _beg{beg}, _end{end}, _step_size{step_size} {}
141
145 T begin() const { return _beg; }
146
150 T end() const { return _end; }
151
155 T step_size() const { return _step_size; }
156
160 IndexRange<T>& reset(T begin, T end, T step_size) {
161 _beg = begin;
162 _end = end;
163 _step_size = step_size;
164 return *this;
165 }
166
170 IndexRange<T>& begin(T new_begin) { _beg = new_begin; return *this; }
171
175 IndexRange<T>& end(T new_end) { _end = new_end; return *this; }
176
180 IndexRange<T>& step_size(T new_step_size) { _step_size = new_step_size; return *this; }
181
194 size_t size() const { return distance(_beg, _end, _step_size); }
195
214 IndexRange discrete_domain(size_t part_beg, size_t part_end) const {
215 return IndexRange(
216 static_cast<T>(part_beg) * _step_size + _beg,
217 static_cast<T>(part_end) * _step_size + _beg,
218 _step_size
219 );
220 }
221
222 private:
223
224 T _beg;
225 T _end;
226 T _step_size;
227
228};
229
230// ----------------------------------------------------------------------------
231
232
233
234
235
236} // end of namespace tf -----------------------------------------------------
tf::IndexRange::size
size_t size() const
queries the number of elements in the range
Definition iterator.hpp:194
tf::IndexRange::end
T end() const
queries the ending index of the range
Definition iterator.hpp:150
tf::IndexRange::begin
T begin() const
queries the starting index of the range
Definition iterator.hpp:145
tf::IndexRange::index_type
T index_type
alias for the index type used in the range
Definition iterator.hpp:126
tf::IndexRange::end
IndexRange< T > & end(T new_end)
updates the ending index of the range
Definition iterator.hpp:175
tf::IndexRange::reset
IndexRange< T > & reset(T begin, T end, T step_size)
updates the range with the new starting index, ending index, and step size
Definition iterator.hpp:160
tf::IndexRange::step_size
IndexRange< T > & step_size(T new_step_size)
updates the step size of the range
Definition iterator.hpp:180
tf::IndexRange::step_size
T step_size() const
queries the step size of the range
Definition iterator.hpp:155
tf::IndexRange::IndexRange
IndexRange()=default
constructs an index range object without any initialization
tf::IndexRange::IndexRange
IndexRange(T beg, T end, T step_size)
constructs an IndexRange object
Definition iterator.hpp:139
tf::IndexRange::discrete_domain
IndexRange discrete_domain(size_t part_beg, size_t part_end) const
returns a range from the given discrete domain
Definition iterator.hpp:214
tf::IndexRange::begin
IndexRange< T > & begin(T new_begin)
updates the starting index of the range
Definition iterator.hpp:170
tf
taskflow namespace
Definition small_vector.hpp:20
tf::distance
constexpr size_t distance(B beg, E end, S step)
calculates the number of iterations in the given index range
Definition iterator.hpp:73
tf::is_index_range_invalid
constexpr bool is_index_range_invalid(B beg, E end, S step)
checks if the given index range is invalid
Definition iterator.hpp:28