SliceIterator.hpp
1 // Distributed under the MIT License.
2 // See LICENSE.txt for details.
3 
4 #pragma once
5 
6 #include <array>
7 #include <cstddef>
8 #include <cstdlib>
9 #include <limits>
10 #include <memory>
11 #include <utility>
12 
13 #include "Utilities/Gsl.hpp"
14 
15 /// \cond
16 template <size_t>
17 class Index;
18 /// \endcond
19 
20 /*!
21  * \ingroup DataStructuresGroup
22  * \brief Iterate over a (dim-1)-dimensional slice
23  */
25  public:
26  /*!
27  * @param extents the number of grid points in each dimension
28  * @param fixed_dim the dimension to slice in
29  * @param fixed_index the index of the `fixed_dim` to slice at
30  */
31  template <size_t Dim>
32  SliceIterator(const Index<Dim>& extents, size_t fixed_dim,
33  size_t fixed_index);
34 
35  /// Returns `true` if the iterator is valid
36  explicit operator bool() const noexcept { return volume_offset_ < size_; }
37 
38  /// Step to the next grid point
39  SliceIterator& operator++();
40 
41  /// Offset into a Dim-dimensional DataVector at the current gridpoint.
42  /// Note that the size of the DataVector is assumed to be the product of the
43  /// extents used to construct this SliceIterator
44  size_t volume_offset() const noexcept { return volume_offset_; }
45 
46  /// Offset into a (Dim-1)-dimensional DataVector at the current gridpoint.
47  /// Note that the size of the DataVector is assumed to be the product of the
48  /// extents used to construct this SliceIterator divided by the extent in
49  /// the fixed_dim used to construct this SliceIterator
50  size_t slice_offset() const noexcept { return slice_offset_; }
51 
52  /// Reset the iterator
53  void reset();
54 
55  private:
56  size_t size_ = std::numeric_limits<size_t>::max();
57  size_t stride_ = std::numeric_limits<size_t>::max();
58  size_t stride_count_ = std::numeric_limits<size_t>::max();
59  size_t jump_ = std::numeric_limits<size_t>::max();
60  size_t initial_offset_ = std::numeric_limits<size_t>::max();
61  size_t volume_offset_ = std::numeric_limits<size_t>::max();
62  size_t slice_offset_ = std::numeric_limits<size_t>::max();
63 };
64 
65 /*!
66  * \ingroup DataStructuresGroup
67  * \brief Get the mapping between volume and boundary slice indices
68  *
69  * SliceIterator is used to map between the index of a point on a slice in the
70  * volume data and the index in the corresponding sliced data. Repeatedly
71  * applying the SliceIterator on various components of a tensor becomes very
72  * expensive and so precomputing the index map is sometimes advantageous. This
73  * function computes the index map onto all boundary slices of volume mesh with
74  * extents `extents`.
75  *
76  * The `unique_ptr` is where the volume and slice indices are stored in memory,
77  * the array holds views into the memory buffer. The index of the array is the
78  * fixed dimension, the outer `pair` holds the indices for the lower and upper
79  * side, respectively, while the `pair`s in the `span`s hold the volume and
80  * slice indices, respectively.
81  */
82 template <size_t VolumeDim>
83 auto volume_and_slice_indices(const Index<VolumeDim>& extents) noexcept
87  VolumeDim>>;
size_t volume_offset() const noexcept
Offset into a Dim-dimensional DataVector at the current gridpoint. Note that the size of the DataVect...
Definition: SliceIterator.hpp:44
auto volume_and_slice_indices(const Index< VolumeDim > &extents) noexcept -> std::pair< std::unique_ptr< std::pair< size_t, size_t >[], decltype(&free)>, std::array< std::pair< gsl::span< std::pair< size_t, size_t >>, gsl::span< std::pair< size_t, size_t >>>, VolumeDim >>
Get the mapping between volume and boundary slice indices.
Definition: SliceIterator.cpp:49
void reset()
Reset the iterator.
Definition: SliceIterator.cpp:39
SliceIterator & operator++()
Step to the next grid point.
Definition: SliceIterator.cpp:28
Create a span/view on a range, which is cheap to copy (one pointer).
Definition: Gsl.hpp:291
T max(T... args)
An integer multi-index.
Definition: Index.hpp:28
size_t slice_offset() const noexcept
Offset into a (Dim-1)-dimensional DataVector at the current gridpoint. Note that the size of the Data...
Definition: SliceIterator.hpp:50
Defines functions and classes from the GSL.
SliceIterator(const Index< Dim > &extents, size_t fixed_dim, size_t fixed_index)
Definition: SliceIterator.cpp:17
Iterate over a (dim-1)-dimensional slice.
Definition: SliceIterator.hpp:24