32#ifndef PFC_MULTI_INDEX_HPP
33#define PFC_MULTI_INDEX_HPP
53 const std::array<int, D> m_begin;
54 const std::array<int, D> m_size;
55 const std::array<int, D> m_end;
56 const size_t m_linear_begin;
60 const size_t m_linear_size;
62 std::array<int, D> calculate_end(std::array<int, D>
begin,
63 std::array<int, D> size) {
64 std::array<int, D>
end;
78 std::array<int, D> offset = std::array<int, D>())
79 : m_begin(offset), m_size(size), m_end(calculate_end(m_begin, m_size)),
81 m_linear_size(m_linear_end - m_linear_begin + 1) {}
83 const std::array<int, D> &get_begin()
const {
return m_begin; }
84 const std::array<int, D> &get_size()
const {
return m_size; }
85 const std::array<int, D> &get_end()
const {
return m_end; }
86 const size_t &get_linear_begin()
const {
return m_linear_begin; }
87 const size_t &get_linear_size()
const {
return m_linear_size; }
88 const size_t &get_linear_end()
const {
return m_linear_end; }
97 size_t to_linear(
const std::array<int, D> &indices)
const {
100 for (
size_t i = 1;
i < D; ++
i) {
116 std::array<int, D> indices;
117 for (
size_t i = 0;
i < D; ++
i) {
118 indices[
i] = (
idx % m_size[
i]) + m_begin[
i];
130 bool inbounds(
const std::array<int, D> &indices)
const {
131 for (
size_t i = 0;
i < D; ++
i) {
132 if (indices[
i] < m_begin[
i] || indices[
i] > m_end[
i]) {
147 os <<
"MultiIndex set with " << D <<
" dimensions (indices from {";
148 for (
size_t i = 0;
i < D - 1;
i++)
os << index.m_begin[
i] <<
",";
149 os << index.m_begin[D - 1] <<
"} to {";
150 for (
size_t i = 0;
i < D - 1;
i++)
os << index.m_end[
i] <<
",";
151 os << index.m_end[D - 1] <<
"}, size {";
152 for (
size_t i = 0;
i < D - 1;
i++)
os << index.
m_size[
i] <<
",";
153 os << index.
m_size[D - 1] <<
"}, linear size " << index.m_linear_size <<
")";
162 std::array<int, D> m_indices;
164 size_t m_linear_index;
176 m_linear_index = m_multi_index.
to_linear(indices);
206 for (
size_t i = 0;
i < D;
i++) {
208 if (m_indices[
i] > m_multi_index.m_end[
i]) {
209 m_indices[
i] = m_multi_index.m_begin[
i];
241 return Iterator(m_linear_index -
n, m_multi_index);
251 return m_linear_index !=
other.m_linear_index;
261 return m_linear_index ==
other.m_linear_index;
269 operator size_t()
const {
return m_linear_index; }
277 operator std::array<int, D>()
const {
return m_indices; }
296 for (
size_t i = 0;
i < D - 1;
i++)
os <<
it.m_indices[
i] <<
", ";
297 os <<
it.m_indices[D - 1] <<
"}";
302 friend class Iterator;
Iterator class for iterating over multi-dimensional indices.
Definition multi_index.hpp:160
bool operator!=(const Iterator &other) const
Compares two iterators for inequality.
Definition multi_index.hpp:250
size_t get_linear_index() const
Returns the current linear index.
Definition multi_index.hpp:284
Iterator operator++(int)
Advances the iterator to the next position (post-increment).
Definition multi_index.hpp:222
Iterator operator-(int n) const
Subtraction operator for the Iterator class.
Definition multi_index.hpp:240
Iterator & operator++()
Advances the iterator to the next position.
Definition multi_index.hpp:204
Iterator(std::array< int, D > indices, const MultiIndex &multi_index)
Constructs an Iterator object with the specified indices and MultiIndex object.
Definition multi_index.hpp:174
Iterator(int linear_index, const MultiIndex &multi_index)
Constructs an Iterator object with the specified linear index and MultiIndex object.
Definition multi_index.hpp:186
std::array< int, D > & operator*()
Dereferences the iterator to obtain the current multi-dimensional indices.
Definition multi_index.hpp:197
bool operator==(const Iterator &other) const
Compares two iterators for equality.
Definition multi_index.hpp:260
friend std::ostream & operator<<(std::ostream &os, const Iterator &it)
Outputs the current multi-dimensional indices to the specified output stream.
Definition multi_index.hpp:294
MultiIndex class for iterating over multi-dimensional indices.
Definition multi_index.hpp:51
bool inbounds(const std::array< int, D > &indices) const
Checks whether given indices are in bounds or not.
Definition multi_index.hpp:130
size_t to_linear(const std::array< int, D > &indices) const
Converts a multi-dimensional index to its corresponding linear index.
Definition multi_index.hpp:97
Iterator end()
Returns an iterator pointing to the end of the range.
Definition multi_index.hpp:316
std::array< int, D > to_multi(size_t idx) const
Converts a linear index to its corresponding multi-dimensional indices.
Definition multi_index.hpp:115
Iterator from(std::array< int, D > from)
Returns an iterator starting from the specified multi-dimensional indices.
Definition multi_index.hpp:339
Iterator begin()
Returns an iterator pointing to the beginning of the range.
Definition multi_index.hpp:309
Iterator end() const
Returns a const iterator pointing to the end of the range.
Definition multi_index.hpp:330
friend std::ostream & operator<<(std::ostream &os, const MultiIndex< D > &index)
Outputs the index to the specified output stream.
Definition multi_index.hpp:146
Iterator begin() const
Returns a const iterator pointing to the beginning of the range.
Definition multi_index.hpp:323
Iterator from(size_t from)
Returns an iterator starting from the specified linear index.
Definition multi_index.hpp:352
MultiIndex(std::array< int, D > size, std::array< int, D > offset=std::array< int, D >())
Constructs a MultiIndex object with the specified offset and size in each dimension.
Definition multi_index.hpp:77
Represents the global simulation domain (the "world").
Definition world.hpp:91
const Int3 m_size
Grid dimensions: {nx, ny, nz}.
Definition world.hpp:94