#include <array>
#include <tuple>
#include <stack>
#include <stdexcept>
template <size_t N, typename... Ts>
class StructOfArrays
{
private:
std::stack<size_t> free_handles_;
std::tuple<std::array<Ts, N>...> arrays_;
public:
StructOfArrays()
{
for (std::size_t i = 0; i < N; i++)
{
free_handles_.push(N - 1 - i); // Push in reverse order
}
}
template <typename T>
T& GetValue(size_t handle) const
{
return std::get<std::array<T, N>>(arrays_)[handle];
}
size_t Insert(const Ts&... values)
{
if (free_handles_.empty())
{
throw std::runtime_error("StructOfArrays is full!");
}
size_t handle = free_handles_.top();
free_handles_.pop();
std::get<std::array<T, N>>...(arrays_)[handle] = Ts... values; // this line is the issue here that I need help with
}
template <typename T>
void Remove(size_t handle)
{
// .. something
free_handles_.push(handle);
}
};
#Call a std::tuple::get<T> = value for each (T value) in Ts... values
79 messages · Page 1 of 1 (latest)
When your question is answered use !solved to mark the question as resolved.
Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question use !howto ask.
what are you trying to do here
lets see, I'm trying do my implementation of the StructOfArrays data structure
and
why
physics engine
this is one of those things that ime just make things harder to work with / reason about over enough time
like, you're getting rid of array semantics here for example
there is no such thing as inserting into an array in C++
the value just exists
right, im not inserting into an array, im inserting into the StructOfArrays.. it handles that for us and give us back the index
yes
and that's arguably not array
that's a vector
but it's besides the point, what i'm trying to get across is that, this is not an abstraction that is "worth it" to do
why is that
dont i get to squash all my data of the same type together to save memory, and then it takes advantage of SIMD caching or something something CPU things
SoA vs AoS is a "weird" concept to reason about
because we get into cpu caching and prefetching / memory access patterns
and what you're doing here could be hurting it, im not too sure what code it compiles to
well my current way is just unordered maps with UUIDs
like, it's not much harder to just write
struct Objects {
std::array<Position, 256> positions;
/// ...
};```
and that's going to be infinitely more clearer about intent
what's the profiler say?
whats the profiler
well it seems like you're doing this for speed / memory savings ^^
I would hope you're not just guessing
totally guessing,
you shouldn't do that :p
someone said it was faster! Andrew Kelley's talk was really convincing
dont listen to other people when it comes to performance saying X is faster than Y
i don't care if god himself is saying it
there is never a single X > Y that exists
im not talking about performance ^^
i'm talking about code maintiability / expressiveness (was, technically, but i digress)
but isnt this a well established desgin architecture for HPC like physics enginse
you know what's well established in HPC?
no one knows anything and no one can guess how a system is going to interact
so you use a profiler to figure out what is slow and what isn't
is there some level of common knowledge of "this is probably faster" yes, but it's not useful for you to act that way
meow meow alright.... I promise not to use it if you show me how to fix it, cause im just interested in how to unpack it properly at this point
something about folding? right
yes sir
my blog post is still in progress 😭
😮
ugh i need to take the time to properly finish this later
anyways
one second
https://godbolt.org/z/4Y86hhMfG @golden jungle
its a build failed no?
you should read the error ^^
undefined reference to `main'
? that one
im just a junior
trying to learn things
well, try to reason about the error
what type of error is it for example
how would you fix it?
you are just missing a main function no?
did you fix it
Thank you and let us know if you have any more questions!
This thread is now set to auto-hide after an hour of inactivity
.