Vulpes  0.1.0
Versatile and parallel compUtational Library for high-order discontinuous Polytopal Element Simulations
Loading...
Searching...
No Matches
index_tracker.hpp
Go to the documentation of this file.
1// ----------------------------------------------------------------------
2//
3// Copyright (C) 2024 - 2026 by the Vulpes authors.
4// This file is part of Vulpes.
5//
6// Vulpes is free software; you can redistribute it and/or modify
7// it under the terms of the GNU General Public License as published by
8// the Free Software Foundation, either version 3 of the License, or
9// (at your option) any later version.
10//
11// Vulpes is distributed in the hope that it will be useful,
12// but WITHOUT ANY WARRANTY; without even the implied warranty of
13// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14// General Public License for more details.
15//
16// You should have received a copy of the GNU General Public License
17// along with Vulpes. If not, see <http://www.gnu.org/licenses/>.
18//
19// ----------------------------------------------------------------------
20
21// Author: Matteo Caldana <matteo.caldana@polimi.it>
22
23#ifndef VULPES_DATASTRUCTURE_INDEX_TRACKER_HPP_
24#define VULPES_DATASTRUCTURE_INDEX_TRACKER_HPP_
25
26#include <algorithm>
27#include <cassert>
28#include <cstddef>
29#include <cstdint>
30#include <type_traits>
31#include <vector>
32
33namespace vulpes {
34namespace datastructure {
35
36// linked list approach shows a reduction of computational cost of ~20% when
37// using agglomeration methods based on recursive bisection
38#ifndef INDEX_TRACKER_AS_VECTOR
39
46template <typename IndexType>
48 public:
52 static constexpr IndexType npos = static_cast<IndexType>(-1);
53
59 class Reference {
60 public:
61 Reference(IndexTracker& tracker, IndexType idx);
62
64 operator IndexType() const;
65
67 Reference& operator=(IndexType new_v);
68
70 Reference& operator=(const Reference& other);
71
72 private:
74 IndexType m_idx;
75 };
76
83 template <typename U>
84 explicit IndexTracker(const std::vector<U>& vec) {
85 static_assert(
86 std::is_integral<U>::value, "Constructor requires integral vector");
87 m_N = vec.size();
88 m_V = m_N;
89 m_data.reserve(m_N);
90 for (U v : vec) {
91 assert(v >= 0);
92 m_data.push_back(static_cast<IndexType>(v));
93 m_V = std::max(m_V, static_cast<IndexType>(v) + 1);
94 }
95
96 m_head.assign(m_V, npos);
97 m_next.assign(m_N, npos);
98 m_prev.assign(m_N, npos);
99
100 for (IndexType i = 0; i < m_N; ++i) {
101 IndexType v = m_data[i];
102 assert(v < m_V);
103 m_next[i] = m_head[v];
104 m_prev[i] = npos;
105 if (m_head[v] != npos) {
106 m_prev[m_head[v]] = i;
107 }
108 m_head[v] = i;
109 }
110 }
111
113 Reference operator[](IndexType i);
114
116 IndexType operator[](IndexType i) const;
117
125 std::vector<IndexType> indicesOf(IndexType v, bool sorted = true) const;
126
133 template <typename F>
134 void forEachIndex(IndexType v, F f) const {
135 if (v >= m_V) {
136 return;
137 }
138 for (IndexType i = m_head[v]; i != npos; i = m_next[i]) {
139 f(i);
140 }
141 }
142
144 IndexType size() const { return m_N; }
145
147 const std::vector<IndexType>& getValues() const { return m_data; };
148
149 private:
150 friend class Reference;
151
152 void updateIndex(IndexType i, IndexType new_v);
153
154 std::vector<IndexType> m_data;
155 IndexType m_N{};
156 IndexType m_V{};
157 std::vector<IndexType> m_head;
158 std::vector<IndexType> m_next;
159 std::vector<IndexType> m_prev;
160};
161
162#else
163
164template <typename IndexType>
165class IndexTracker {
166 public:
167 template <typename U>
168 explicit IndexTracker(const std::vector<U>& data) {
169 if constexpr (std::is_same_v<U, IndexType>) {
170 m_data = data;
171 } else {
172 m_data.reserve(data.size());
173 for (U u : data) {
174 m_data.push_back(static_cast<IndexType>(u));
175 }
176 }
177 }
178
179 const auto& operator[](IndexType i) const { return m_data[i]; };
180 auto& operator[](IndexType i) { return m_data[i]; };
181
182 const auto& getValues() const { return m_data; }
183
184 std::vector<IndexType> indicesOf(IndexType v) const {
185 std::vector<IndexType> result;
186 for (IndexType ocid = 0; ocid < m_data.size(); ++ocid) {
187 if (m_data[ocid] == v) {
188 result.push_back(ocid);
189 }
190 }
191 return result;
192 }
193
194 const auto size() const { return m_data.size(); }
195
196 protected:
197 std::vector<IndexType> m_data;
198};
199
200#endif
201
202extern template class IndexTracker<int32_t>;
203extern template class IndexTracker<int64_t>;
204extern template class IndexTracker<uint32_t>;
205extern template class IndexTracker<uint64_t>;
206
207} // namespace datastructure
208} // namespace vulpes
209
210#endif // VULPES_DATASTRUCTURE_INDEX_TRACKER_HPP_
IndexType m_idx
Definition index_tracker.hpp:74
Reference(IndexTracker &tracker, IndexType idx)
Definition index_tracker.cpp:34
IndexTracker & m_tracker
Definition index_tracker.hpp:73
Reference & operator=(IndexType new_v)
Assignment from new value triggers internal update logic.
Definition index_tracker.cpp:45
A class that uses a linked list to keep track of all the indices of a vector that are equal to a cert...
Definition index_tracker.hpp:47
std::vector< index_t > m_data
Definition index_tracker.hpp:154
std::vector< index_t > m_head
Definition index_tracker.hpp:157
IndexType size() const
Returns the number of tracked elements.
Definition index_tracker.hpp:144
index_t m_V
Definition index_tracker.hpp:156
void updateIndex(IndexType i, IndexType new_v)
Definition index_tracker.cpp:90
static constexpr index_t npos
Definition index_tracker.hpp:52
Reference operator[](IndexType i)
Returns a proxy reference to an element at index i.
Definition index_tracker.cpp:57
std::vector< index_t > m_next
Definition index_tracker.hpp:158
std::vector< IndexType > indicesOf(IndexType v, bool sorted=true) const
Returns a vector of all indices whose value equals v.
Definition index_tracker.cpp:70
index_t m_N
Definition index_tracker.hpp:155
IndexType operator[](IndexType i) const
Const access to element at index i.
Definition index_tracker.cpp:64
friend class Reference
Definition index_tracker.hpp:150
const std::vector< IndexType > & getValues() const
Returns a const reference to the underlying data.
Definition index_tracker.hpp:147
void forEachIndex(IndexType v, F f) const
Applies a callback to each index that maps to the given value.
Definition index_tracker.hpp:134
std::vector< index_t > m_prev
Definition index_tracker.hpp:159
IndexTracker(const std::vector< U > &vec)
Constructor from any integral vector. Values are cast to IndexType.
Definition index_tracker.hpp:84
Definition crs_map.hpp:36
Definition cell_manager.hpp:38