I have data that is essentially tree-like, and to store it/iterate over it I have been using nested dictionaries, eg:
Dictionary<int, Dictionary<string, Dictionary<double>>> data = new ...
to initialize the data structure, I load in the data - imagine it's a List of DataItem objects with fields (int id1, string id2, double value). then I do:
foreach(DataItem item in items)
{
if (!data.ContainsKey(item.id1)) data[item.id1] = new Dictionary<string, Dictionary<double>>();
if(!data[item.id1].ContainsKey(item.id2)) data[item.id1][item.id2] = item.value;
}
then I can iterate through the data by the id1 field, then iterate through each id2 field for that particular id1. is there a better way to implement a data structure like this? I noticed that when I run through that initialization foreach loop, it takes a few seconds, I'm guessing because the ContainsKey lookup isn't constant-time at first, but approaches it? in any case, I feel like there's a more clever way to do this, but I'm not sure the best way. thanks