lundi 7 novembre 2022

Storing vectors of items with one-to-many relationships inside a contiguous buffer

I'd like to store data with one-to-many relationships without redundancies, but can't figure out how..

What I am trying to achieve is marked with /*(0)*/, /*(1)*/, /*(2)*/; Where /*(0)*/ is essentially the contents of m_values concatenated. I'd like to be able to reach the data based on index values ( such as from the std::array ) and from a contiguous std::vector<double> view as well.

What I tried is to have the buffers handled by DataBase, so Item only has a reference, but there are no array of references. Using pointers should be the last resort to go to;

So is there a design ( maybe a pattern? ) that could work here better?

#include <vector>
#include <array>

using DataType = std::vector<double>;
template<size_t N>
class Item{
public:
    Item(DataType key, std::array<DataType, N> values)
    : m_key(key)
    , m_values(values)
    {}    
    DataType& key(){
        return m_key;
    }
    
    DataType& values();/*(0)*/
private:
    DataType m_key;
    std::array<DataType, N> m_values;
};

template<size_t N>
class DataBase{
public:
    DataBase()
    {}
    
    const DataType& key(size_t i){
        return m_content[i].key();
    }

    const DataType& value(size_t i); /*technically also (0)*/
    const std::vector<DataType>& every_key(); /*(1)*/
    const std::vector<DataType>& every_value(); /*(2)*/
    
private:
    std::vector<Item<N>> m_content;
};

Aucun commentaire:

Enregistrer un commentaire