reverse lookup in dictionary
-
Is there any data structure avilable out of the box in Framework 3.5, kind of reverse lookup dictionary.
Dictionary< key1, key2 > d = new Dictionary< key1, key2>();
Key2 k2 = d[key1]; // normal
Key1 k1 = d[key2]; // specify the key2 and get key1 -- neededThanks
Regards, Vythees Miles to go before sleep...
-
Is there any data structure avilable out of the box in Framework 3.5, kind of reverse lookup dictionary.
Dictionary< key1, key2 > d = new Dictionary< key1, key2>();
Key2 k2 = d[key1]; // normal
Key1 k1 = d[key2]; // specify the key2 and get key1 -- neededThanks
Regards, Vythees Miles to go before sleep...
There is no reverse lookup in a dictionary because one value can lead to multiple keys, but not the other way round. To get a list of keys that result in a specific value you can use this:
var keys = from key in dic.Keys
where dic[key] == somevalue
select key;regards
-
There is no reverse lookup in a dictionary because one value can lead to multiple keys, but not the other way round. To get a list of keys that result in a specific value you can use this:
var keys = from key in dic.Keys
where dic[key] == somevalue
select key;regards
-
vytheese wrote:
but in my case both are unique.
In this case I would derive a class from IDictionary that will internally hold two dictionaries, one for key->value and one for value->key. Something like:
public class TwoWayDictionary<K, V> : IDictionary<K, V>
Override the methods like Add, Contains etc to allow adding, removing and searching by key and value in both of the dictionaries. regards
-
vytheese wrote:
but in my case both are unique.
In this case I would derive a class from IDictionary that will internally hold two dictionaries, one for key->value and one for value->key. Something like:
public class TwoWayDictionary<K, V> : IDictionary<K, V>
Override the methods like Add, Contains etc to allow adding, removing and searching by key and value in both of the dictionaries. regards
-
The wrapper will ease from access point of view but Again I will end up in performance issue while searching from value point.
Regards, Vythees Miles to go before sleep...
Why? You will be looking up the value as a key in your second dictionary.
-
Why? You will be looking up the value as a key in your second dictionary.
Good, Sorry I failed to notice that. But in any case its only a wrapper for me. What I am looking for is single dictionary with key, values both indexed or hashed. since in my scenario the collection I am expecting is huge and time for process is having little window. so I want to get the feasible data structure that will helps me in both (space/time). But with the time contstraint for my execution I have to trade between space vs time for a intial release. Grazie.
Regards, Vythees Miles to go before sleep...
-
Good, Sorry I failed to notice that. But in any case its only a wrapper for me. What I am looking for is single dictionary with key, values both indexed or hashed. since in my scenario the collection I am expecting is huge and time for process is having little window. so I want to get the feasible data structure that will helps me in both (space/time). But with the time contstraint for my execution I have to trade between space vs time for a intial release. Grazie.
Regards, Vythees Miles to go before sleep...
vytheese wrote:
What I am looking for is single dictionary with key, values both indexed or hashed.
That exactly what a wrapper class with 2 dictionaries will do...
xacc.ide - now with TabsToSpaces support
IronScheme - 1.0 alpha 4a out now (29 May 2008) -
Is there any data structure avilable out of the box in Framework 3.5, kind of reverse lookup dictionary.
Dictionary< key1, key2 > d = new Dictionary< key1, key2>();
Key2 k2 = d[key1]; // normal
Key1 k1 = d[key2]; // specify the key2 and get key1 -- neededThanks
Regards, Vythees Miles to go before sleep...
Yeah, I just use two Dictionaries.