IndexIterator.hpp
Go to the documentation of this file.
1 // Distributed under the MIT License.
2 // See LICENSE.txt for details.
3 
4 /// \file
5 /// Defines IndexIterator.
6 
7 #pragma once
8 
9 #include <cstddef>
10 
11 #include "DataStructures/Index.hpp"
12 
13 /// \ingroup DataStructuresGroup
14 /// IndexIterator iterates over a unique set of Index.
15 ///
16 /// \example
17 /// \snippet Test_IndexIterator.cpp index_iterator_example
18 ///
19 /// Each integer of the Index will vary from 0 to extents[d] - 1,
20 /// with the lowest dimension varying the fastest.
21 template <std::size_t Dim>
23  public:
24  /// Construct from an Index
25  explicit IndexIterator(Index<Dim> extents);
26  /// It does not make sense to default construct an IndexIterator
27  IndexIterator() = delete;
28  /// \cond HIDDEN_SYMBOLS
29  ~IndexIterator() = default;
30  // @{
31  /// No copy or move semantics
32  IndexIterator(const IndexIterator<Dim>&) = delete;
34  IndexIterator<Dim>& operator=(const IndexIterator<Dim>&) = delete;
35  IndexIterator<Dim>& operator=(IndexIterator<Dim>&&) = delete;
36  // @}
37  /// \endcond
38 
39  /// Returns false if the end of the Index iteration is reached
40  explicit operator bool() const noexcept { return valid_; }
41 
42  /// Advance to next Index.
44 
45  const Index<Dim>& operator*() const noexcept { return index_; }
46  const Index<Dim>* operator->() const noexcept { return &index_; }
47 
48  /// Returns an index representing the (i, j, ...)th values that the iterator
49  /// currently represents
50  const Index<Dim>& operator()() const noexcept { return index_; }
51 
52  /// Get the collapsed index into a 1D array of the data corresponding to the
53  /// current Index of the IndexIterator. Note that the first dimension of the
54  /// Index varies fastest when computing the collapsed index.
55  size_t collapsed_index() const noexcept { return collapsed_index_; }
56 
57  private:
58  const Index<Dim> extents_{};
59  Index<Dim> index_{};
60  size_t collapsed_index_{0};
61  bool valid_{false};
62 };
IndexIterator iterates over a unique set of Index.
Definition: IndexIterator.hpp:22
const Index< Dim > & operator()() const noexcept
Returns an index representing the (i, j, ...)th values that the iterator currently represents...
Definition: IndexIterator.hpp:50
Defines class template Index.
size_t collapsed_index() const noexcept
Get the collapsed index into a 1D array of the data corresponding to the current Index of the IndexIt...
Definition: IndexIterator.hpp:55
IndexIterator()=delete
It does not make sense to default construct an IndexIterator.
An integer multi-index.
Definition: Index.hpp:28
IndexIterator< Dim > & operator++()
Advance to next Index.
Definition: IndexIterator.cpp:16