#Call a std::tuple::get<T> = value for each (T value) in Ts... values

79 messages · Page 1 of 1 (latest)

golden jungle
#
#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);
    }

};
fickle rootBOT
#

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.

agile zealot
#

what are you trying to do here

golden jungle
#

lets see, I'm trying do my implementation of the StructOfArrays data structure

#

and

agile zealot
#

why

golden jungle
#

physics engine

agile zealot
#

no i know

#

im asking why you're making a struct for this / helper

golden jungle
#

I'm so close!

#

huh?

agile zealot
#

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

golden jungle
#

right, im not inserting into an array, im inserting into the StructOfArrays.. it handles that for us and give us back the index

agile zealot
#

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

golden jungle
#

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

agile zealot
#

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

golden jungle
#

well my current way is just unordered maps with UUIDs

agile zealot
#

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

agile zealot
golden jungle
#

whats the profiler

agile zealot
#

well it seems like you're doing this for speed / memory savings ^^

#

I would hope you're not just guessing

golden jungle
#

totally guessing,

agile zealot
#

you shouldn't do that :p

golden jungle
#

someone said it was faster! Andrew Kelley's talk was really convincing

agile zealot
#

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

golden jungle
#

but thats what youre saying

#

!

agile zealot
#

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)

golden jungle
#

but isnt this a well established desgin architecture for HPC like physics enginse

agile zealot
#

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

golden jungle
#

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

agile zealot
#

there is no single truth of what is faster and isn't lel

#

except one thing

golden jungle
#

something about folding? right

agile zealot
#

which is, more work takes more time than doing less work

#

but that's it

golden jungle
#

yes sir

agile zealot
#

my blog post is still in progress 😭

golden jungle
#

😮

agile zealot
#

ugh i need to take the time to properly finish this later

#

anyways

#

one second

#
golden jungle
#

its a build failed no?

agile zealot
#

you should read the error ^^

golden jungle
#

undefined reference to `main'

#

? that one

#

im just a junior

#

trying to learn things

agile zealot
#

well, try to reason about the error

#

what type of error is it for example

#

how would you fix it?

golden jungle
#

you are just missing a main function no?

agile zealot
#

you tell me

#

can't give you all the answers :p

golden jungle
#

I fixed it ?

agile zealot
#

did you fix it

golden jungle
#

added a main function

#

!solved

fickle rootBOT
#

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

magic cloak
#

.