28template <std::
integral T>
30 return ((step == T{0} && beg != end) ||
31 (beg < end && step <= T{0}) ||
32 (beg > end && step >= T{0}));
70template <std::
integral T>
71constexpr size_t distance(T beg, T end, T step) {
72 if constexpr (std::is_unsigned_v<T>) {
74 ?
static_cast<size_t>((end - beg + step - T{1}) / step)
77 return static_cast<size_t>(
78 std::max(T{0}, (end - beg + step + (step > T{0} ? T{-1} : T{1})) / step)
187template <std::
integral T,
size_t N = 1>
200 static constexpr size_t rank = N;
251 template <
typename... Ranges>
252 requires (
sizeof...(Ranges) == N) &&
255 : _dims{ ranges.
dim(0)... } {}
273 explicit IndexRanges(
const std::array<std::tuple<T, T, T>, N>& dims) : _dims{dims} {}
297 const std::tuple<T, T, T>&
dim(
size_t d)
const {
return _dims[d]; }
330 std::tuple<T, T, T>&
dim(
size_t d) {
return _dims[d]; }
346 T
begin() const requires (N == 1) {
return std::get<0>(_dims[0]); }
358 T
end() const requires (N == 1) {
return std::get<1>(_dims[0]); }
370 T
step_size() const requires (N == 1) {
return std::get<2>(_dims[0]); }
525 std::array<std::tuple<T, T, T>, N> _dims;
532template <std::
integral T,
size_t N>
539template <std::
integral T,
size_t N>
542 std::get<0>(_dims[0]) = new_begin;
546template <std::
integral T,
size_t N>
549 std::get<1>(_dims[0]) = new_end;
553template <std::
integral T,
size_t N>
556 std::get<2>(_dims[0]) = new_step_size;
560template <std::
integral T,
size_t N>
563 auto [beg,
end, step] = _dims[0];
565 static_cast<T
>(part_beg) * step + beg,
566 static_cast<T
>(part_end) * step + beg,
571template <std::
integral T,
size_t N>
573 if constexpr (N == 1) {
580 auto compute = [
this]<
size_t D>(
auto& self,
size_t total) ->
size_t {
581 if constexpr (D == N) {
584 size_t s = this->
size(D);
585 if (s == 0)
return D == 0 ? 0 : total;
586 return self.template operator()<D + 1>(self, total * s);
589 return compute.template operator()<0>(compute, 1);
611template <std::
integral T>
633template <
typename T,
size_t N>
class to create an N-dimensional index range of integral indices
Definition iterator.hpp:188
IndexRanges & reset(T beg, T end, T step_size)
updates the range with a new starting index, ending index, and step size (only available when N == 1)
Definition iterator.hpp:534
T end() const
Definition iterator.hpp:358
const std::tuple< T, T, T > & dim(size_t d) const
Definition iterator.hpp:297
IndexRanges & end(T new_end)
updates the ending index of the range (only available when N == 1)
Definition iterator.hpp:548
std::tuple< T, T, T > & dim(size_t d)
returns the (begin, end, step) tuple for dimension d (mutable)
Definition iterator.hpp:330
IndexRanges unravel(size_t part_beg, size_t part_end) const
maps a contiguous index partition back to the corresponding subrange (only available when N == 1)
Definition iterator.hpp:562
IndexRanges & step_size(T new_step_size)
updates the step size of the range (only available when N == 1)
Definition iterator.hpp:555
static constexpr size_t rank
Definition iterator.hpp:200
IndexRanges()=default
constructs an index range without initialization
size_t size(size_t d) const
returns the number of iterations along dimension d
Definition iterator.hpp:491
IndexRanges(Ranges &&... ranges)
constructs an N-D index range from N 1D ranges
Definition iterator.hpp:254
size_t size() const
returns the number of active flat iterations
Definition iterator.hpp:572
T index_type
alias for the index type
Definition iterator.hpp:195
IndexRanges(const std::array< std::tuple< T, T, T >, N > &dims)
constructs an index range from an array of (begin, end, step) tuples
Definition iterator.hpp:273
IndexRanges(T beg, T end, T step_size)
constructs a 1D index range (only available when N == 1)
Definition iterator.hpp:226
IndexRanges & begin(T new_begin)
updates the starting index of the range (only available when N == 1)
Definition iterator.hpp:541
T begin() const
queries the starting index of the range (only available when N == 1)
Definition iterator.hpp:346
T step_size() const
Definition iterator.hpp:370
concept to check if a type is a tf::IndexRanges, regardless of dimensionality
Definition iterator.hpp:645
taskflow namespace
Definition small_vector.hpp:20
IndexRanges< T, 1 > IndexRange
alias for the common 1D case of tf::IndexRanges
Definition iterator.hpp:612
constexpr bool is_index_ranges_v
base type trait to detect if a type is a tf::IndexRanges
Definition iterator.hpp:623
constexpr size_t distance(T beg, T end, T step)
calculates the number of iterations in the given index range
Definition iterator.hpp:71
constexpr bool is_index_range_invalid(T beg, T end, T step)
checks if the given index range is invalid
Definition iterator.hpp:29