0

I have the below sequence of data to be stored into C++ container.

Type1 -> Channel 0 -> Data1 -> Value
                   -> Data2 -> Value
                   -> Data3 -> Value
         Channel 1 -> Data1 -> Value
                   -> Data2 -> Value
                   -> Data3 -> Value
         Channel 2 -> Data1 -> Value
                   -> Data2 -> Value
                   -> Data3 -> Value
Type2 -> Channel 0 -> Data1 -> Value
                   -> Data2 -> Value
Type3 -> Channel 0 -> Data1 -> Value
                   -> Data2 -> Value
         Channel 1 -> Data1 -> Value
                   -> Data2 -> Value

What is the best way to store in a container. We use C++14.

Thanks in advance !!

I have tried the below map:

std::map<std::string,std::map<int,std::map<std::string, int>>> DataMap;

Is there any optimized way to do this?

Yakk - Adam Nevraumont
  • 262,606
  • 27
  • 330
  • 524
Monika
  • 1
  • 2
  • Have you looked into multidimensional indexing? E.g. kd-tree, quadtree, R-Tree... It requires though that you turn "Type" / "Channel" into numerical values. WHether htese are youse depends a lot on how you want to access the data, see next comment. – TilmannZ Feb 15 '23 at 10:36
  • As suggested by @Paglo_Yaggi, please explain how you want to query the data. Do you typically have the Type and want to get all channels for it? Or you have a Channel and a Type and want to get the Value? Or do you sometimes have only channel ids and not types? – TilmannZ Feb 15 '23 at 10:38

1 Answers1

0

Well, 'the best' depends on the situation, you didn't explained your problem nor the characteristics of your data. But your approach tends to have a big footprint among other performance issues.

I suggest to use a std::multimap, even better std::unordered_multimap if you don't need the elements ordered by 'Type'. Then using lower_bound or equal_range you will access all data for each type. For the data itself you could use an struct with the channel/dataname/value. Again you didn't explained the characteristics of your problem, but assuming DataX is some kind of property I would use an outside string/int mapping to reduce the footprint.

Example assuming a problem where Type are many and data per type are few:

struct Data
{
  int channel;
  std::string dataId; //recommended mapping outside
  int value;
};
std::unordered_map<std::string,Data> DataMap;

... with outside mapping

std::vector<std::string> SimpleDictionary;
struct Data
{
  int channel;
  int dataId; //dataId is the index in the SimpleDictionary
  int value;
};
std::unordered_map<std::string,Data> DataMap;
Pablo Yaggi
  • 1,061
  • 5
  • 14
  • Also using a single `std::(unordered_)map` with a `std::touple` key might be an option. As in `std::(unordered_)map< ,int>`. This will require an implementation for hashing these touples as shown in [this SO question](https://stackoverflow.com/questions/11408934/using-a-stdtuple-as-key-for-stdunordered-map). – Ignacio Gaviglio Feb 14 '23 at 15:37