Reputation:
I have a dictionary where the key is a string; while the values are a list of floats.
key = "Sensor1";
values = new List<float>{0.4, 0.5, 0.2, 0.4};
Is this the best data structure that I can use for my case? My concern is related to the time spent parsing the list of values (I will parse this often, both reading it than for write), but I don't see another way, beside a dictionary with the list in it for the values.
40 sensors read values 10 times a second; I read all these values and save in the dictionary the list with the values, and the key. There are cases where the values in the list for each sensor has to be replaced. The whole process is quite complex; this is the quickest way to summarize it; hopefully is clear.
Upvotes: 1
Views: 412
Reputation: 37918
Not sure what you mean by "parsing the list of values" but probably you're looking for Lookup.
It is similar to Dictionary<TKey, Collection<TElement>>
. You can group items by key and access collection by this key
Upvotes: 0
Reputation: 1625
For the type of data that you are describing you need to use some kind of hash map, there is multiple ways of implementing a hash map (like c# dictionary), for example in each key you can save the value as a tree(binary , red/black,..), a linked list, sorted array and so on.. also need to pick a hash function that needs to be very fast. I would trust microsoft for implementing dictionary in the best way with suitible performences.
Upvotes: 0
Reputation: 726499
If your list represents related values that share some common behavior, you may benefit from encapsulating this list in a class:
class SensorData {
public IList<float> Values {get;}
public SensorData(IEnumerable<float> values) {
Values = values.ToList();
}
// Add some useful methods that operate on Values here
...
}
Now instead of making
IDictionary<string,List<float>>
you would make
IDictionary<string,SensorData>
which could potentially provide for better readability of your code.
Upvotes: 0
Reputation: 1543
It is generally Dictionary
that is used mostly for such tasks if your data structure allows it, it is extremely fast since notation is O(1).
I would suggest looking into HashSet
, it might be more efficient for your case.
Comparison between Dictionary
, List
and HashSet
could be found here.
Upvotes: 1