index_range.h Source File

CPP API: index_range.h Source File
index_range.h
Go to the documentation of this file.
1 /*
2 * Copyright (C) 2020-2026 MEmilio
3 *
4 * Authors: René Schmieding
5 *
6 * Contact: Martin J. Kuehn <Martin.Kuehn@DLR.de>
7 *
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
11 *
12 * http://www.apache.org/licenses/LICENSE-2.0
13 *
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 */
20 #ifndef MIO_UTILS_INDEX_RANGE_H_
21 #define MIO_UTILS_INDEX_RANGE_H_
22 
23 #include "memilio/utils/index.h"
24 #include "memilio/utils/stl_util.h"
25 
26 namespace mio
27 {
28 
33 template <class MultiIndex>
35 {
36 public:
37  using iterator_category = std::forward_iterator_tag;
38  using value_type = MultiIndex;
40  using pointer = void;
41  using difference_type = std::ptrdiff_t;
42 
48  : m_index(MultiIndex::Zero())
49  , m_dims(MultiIndex::Zero())
50  {
51  }
52 
59  : m_index(std::move(index))
60  , m_dims(std::move(dimensions))
61  {
62  }
63 
66  {
67  return m_index;
68  }
69 
72  {
73  if constexpr (MultiIndex::size > 0) {
75  }
76  return *this;
77  }
78 
81  {
82  auto retval = *this;
83  ++(*this);
84  return retval;
85  }
86 
88  bool operator==(const MultiIndexIterator& other) const
89  {
90  return m_index == other.m_index;
91  }
92 
94  bool operator!=(const MultiIndexIterator& other) const
95  {
96  return !(*this == other);
97  }
98 
99 private:
104  template <size_t I = MultiIndex::size - 1>
105  inline void increment_index()
106  {
107  assert(mio::get<I>(m_dims).get() > 0 && "All dimensions must be positive.");
108  assert(mio::get<I>(m_index) < mio::get<I>(m_dims) && "Index out of bounds.");
109 
110  if constexpr (I > 0) {
111  // increment first, then do a carry-over if necessary
112  ++mio::get<I>(m_index);
113  if (mio::get<I>(m_index) == mio::get<I>(m_dims)) {
114  mio::get<I>(m_index) = mio::get<I>(m_index).Zero();
115  increment_index<I - 1>();
116  }
117  }
118  else {
119  ++mio::get<0>(m_index);
120  // no carry check for the most significant index
121  }
122  }
125 };
126 
134 template <class... Categories>
136 {
137  return {Index<Categories...>::Zero(), std::move(dimensions)};
138 }
139 
147 template <class... Categories>
149 {
150  // set end to the first invalid index that is reached by increments of 1,
151  // i.e. 0 everywhere except for the most significant index (position 0),
152  // which is set to its dimension
154  if constexpr (Index<Categories...>::size > 0) {
155  mio::get<0>(end) = mio::get<0>(dimensions);
156  }
157  return {end, dimensions};
158 }
159 
167 template <class... Categories>
169 {
170  return {begin(dimensions), end(dimensions)};
171 }
172 
173 } // namespace mio
174 
175 #endif // MIO_UTILS_INDEX_RANGE_H_
An Index with more than one template parameter combines several Index objects.
Definition: index.h:181
static constexpr Index Zero()
Construct an Index filled with zeroes.
Definition: index.h:187
A Range that can be used to iterate over a MultiIndex.
Definition: index_range.h:35
MultiIndexIterator & operator++()
Pre-increment operator.
Definition: index_range.h:71
void increment_index()
Implementation of ++.
Definition: index_range.h:105
value_type reference
Definition: index_range.h:39
std::ptrdiff_t difference_type
Definition: index_range.h:41
bool operator==(const MultiIndexIterator &other) const
Equality operator.
Definition: index_range.h:88
MultiIndexIterator operator++(int)
Post-increment operator.
Definition: index_range.h:80
MultiIndexIterator()
Default constructed MultiIndexIterator.
Definition: index_range.h:47
value_type m_index
Index used for iteration.
Definition: index_range.h:123
MultiIndex value_type
Definition: index_range.h:38
reference operator*() const
Dereference operator.
Definition: index_range.h:65
value_type m_dims
Copy of range dimensions.
Definition: index_range.h:124
std::forward_iterator_tag iterator_category
Definition: index_range.h:37
bool operator!=(const MultiIndexIterator &other) const
Inequality operator.
Definition: index_range.h:94
void pointer
Definition: index_range.h:40
MultiIndexIterator(value_type index, reference dimensions)
Iterator for MultiIndices.
Definition: index_range.h:58
A range of items defined by two iterators.
Definition: stl_util.h:99
int size(Comm comm)
Return the size of the given communicator.
Definition: miompi.cpp:75
A collection of classes to simplify handling of matrix shapes in meta programming.
Definition: models/abm/analyze_result.h:30
requires details::IsElementReference< M > RowMajorIterator< M, false > end(M &m)
create a non-const end iterator for the matrix m.
Definition: eigen_util.h:449
Range< MultiIndexIterator< Index< Categories... > > > make_index_range(Index< Categories... > dimensions)
Construct a range that can be used to iterate over all MultiIndices in the given dimensions.
Definition: index_range.h:168
requires details::IsElementReference< M > RowMajorIterator< M, false > begin(M &m)
create a non-const iterator to first element of the matrix m.
Definition: eigen_util.h:421
constexpr std::tuple_element< I, std::tuple< Index< CategoryTags >... > >::type & get(Index< CategoryTags... > &i) noexcept
Retrieves the Index (by reference) at the Ith position of a MultiIndex.
Definition: index.h:294
Definition: io.h:94