Visual Computing Library  devel
Loading...
Searching...
No Matches
parallel_vector_tuple.h
1/*****************************************************************************
2 * VCLib *
3 * Visual Computing Library *
4 * *
5 * Copyright(C) 2021-2025 *
6 * Visual Computing Lab *
7 * ISTI - Italian National Research Council *
8 * *
9 * All rights reserved. *
10 * *
11 * This program is free software; you can redistribute it and/or modify *
12 * it under the terms of the Mozilla Public License Version 2.0 as published *
13 * by the Mozilla Foundation; either version 2 of the License, or *
14 * (at your option) any later version. *
15 * *
16 * This program is distributed in the hope that it will be useful, *
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
19 * Mozilla Public License Version 2.0 *
20 * (https://www.mozilla.org/en-US/MPL/2.0/) for more details. *
21 ****************************************************************************/
22
23#ifndef VCL_SPACE_CORE_PARALLEL_VECTOR_TUPLE_H
24#define VCL_SPACE_CORE_PARALLEL_VECTOR_TUPLE_H
25
26#include <vclib/base.h>
27
28#include <array>
29#include <span>
30
31namespace vcl {
32
55template<typename... Types>
57{
58 using VecTuple = std::tuple<std::vector<Types>...>;
59
60public:
64 static constexpr uint TUPLE_SIZE = std::tuple_size_v<VecTuple>;
65
66private:
67 VecTuple mVecTuple;
68
69 std::array<bool, TUPLE_SIZE> mVecEnabled;
70 std::size_t mSize = 0;
71
72public:
77 {
78 // Initialize all vectors as enabled
79 mVecEnabled.fill(true);
80 }
81
85 static constexpr uint tupleSize() { return TUPLE_SIZE; }
86
96 std::size_t size() const { return mSize; }
97
103 template<uint I>
104 bool isVectorEnabled() const
105 {
106 static_assert(I < TUPLE_SIZE, "Index out of bounds");
107 return mVecEnabled[I];
108 }
109
120 template<uint I>
122 {
123 static_assert(I < TUPLE_SIZE, "Index out of bounds");
124 mVecEnabled[I] = true;
125 std::get<I>(mVecTuple).resize(mSize);
126 }
127
136 template<uint I>
138 {
139 static_assert(I < TUPLE_SIZE, "Index out of bounds");
140 mVecEnabled[I] = false;
141 std::get<I>(mVecTuple).clear();
142 }
143
151 {
152 mVecEnabled.fill(true);
153 if (mSize > 0) {
154 auto func = [this](auto&& vec) {
155 vec.resize(mSize);
156 };
157 applyToAllEnabledVectors<TUPLE_SIZE - 1>(func);
158 }
159 }
160
168 {
169 mVecEnabled.fill(false);
170 auto func = [](auto& vec) {
171 vec.clear();
172 };
173 std::apply(func, mVecTuple);
174 // don't change the size!
175 }
176
186 template<uint I>
187 auto span() const
188 {
189 if (!isVectorEnabled<I>()) {
190 throw std::runtime_error(
191 "Accessing disabled vector at index " + std::to_string(I));
192 }
193 using RT = TypeAtT<I, Types...>;
194 return std::span<const RT>(std::get<I>(mVecTuple));
195 }
196
206 template<uint I>
207 auto span()
208 {
209 if (!isVectorEnabled<I>()) {
210 throw std::runtime_error(
211 "Accessing disabled vector at index " + std::to_string(I));
212 }
213 return std::span(std::get<I>(mVecTuple));
214 }
215
221 void resize(std::size_t size)
222 {
223 if constexpr (TUPLE_SIZE > 0) {
224 auto func = [this, size](auto&& vec) {
225 vec.resize(size);
226 };
227 applyToAllEnabledVectors<TUPLE_SIZE - 1>(func);
228 }
229 mSize = size;
230 }
231
238 void reserve(std::size_t size)
239 {
240 if constexpr (TUPLE_SIZE > 0) {
241 auto func = [this, size](auto&& vec) {
242 vec.reserve(size);
243 };
244 applyToAllEnabledVectors<TUPLE_SIZE - 1>(func);
245 }
246 }
247
252 void clear()
253 {
254 if constexpr (TUPLE_SIZE > 0) {
255 auto func = [](auto& vec) {
256 vec.clear();
257 };
258 applyToAllEnabledVectors<TUPLE_SIZE - 1>(func);
259 }
260 mSize = 0;
261 }
262
263private:
264 template<std::size_t N>
265 void applyToAllEnabledVectors(auto&& func)
266 {
267 if (mVecEnabled[N]) {
268 func(std::get<N>(mVecTuple));
269 }
270 if constexpr (N != 0)
271 applyToAllEnabledVectors<N - 1>(func);
272 }
273};
274
275template<typename... Types>
277 public ParallelVectorTuple<Types...>
278{
279};
280
281} // namespace vcl
282
283#endif // VCL_SPACE_CORE_PARALLEL_VECTOR_TUPLE_H
A class representing a box in N-dimensional space.
Definition box.h:46
A class that holds multiple vectors of different types in a tuple, constraind to the same size.
Definition parallel_vector_tuple.h:57
auto span()
Returns a std::span to the vector at the given index.
Definition parallel_vector_tuple.h:207
void disableAllVectors()
Disables all vectors in the tuple.
Definition parallel_vector_tuple.h:167
void disableVector()
Disables the vector at the given index.
Definition parallel_vector_tuple.h:137
void enableAllVectors()
Enables all vectors in the tuple.
Definition parallel_vector_tuple.h:150
auto span() const
Returns a const std::span to the vector at the given index.
Definition parallel_vector_tuple.h:187
bool isVectorEnabled() const
Returns whether a vector at a given index is enabled.
Definition parallel_vector_tuple.h:104
void clear()
Clears all enabled vectors and resets the size of the parallel vector tuple to zero.
Definition parallel_vector_tuple.h:252
void reserve(std::size_t size)
Reserves space for all enabled vectors to the given size.
Definition parallel_vector_tuple.h:238
ParallelVectorTuple()
Constructs a ParallelVectorTuple with all vectors enabled.
Definition parallel_vector_tuple.h:76
static constexpr uint tupleSize()
The number of vectors in the tuple.
Definition parallel_vector_tuple.h:85
void resize(std::size_t size)
Resizes all enabled vectors to the given size, and sets the current size of the parallel vector tuple...
Definition parallel_vector_tuple.h:221
static constexpr uint TUPLE_SIZE
The number of vectors in the tuple.
Definition parallel_vector_tuple.h:64
void enableVector()
Enables the vector at the given index.
Definition parallel_vector_tuple.h:121
std::size_t size() const
Returns the size of the parallel vector tuple.
Definition parallel_vector_tuple.h:96
typename TypeAt< I, T... >::type TypeAtT
Alias for the type at a given index in a pack of types (variadic templates) or a TypeWrapper.
Definition variadic_templates.h:173
A simple structure that wraps a list of variadic templates, without instantiating anything....
Definition type_wrapper.h:39