0

I am looking for a solution that lets me store and lookup multiple tuples. Dictionary only provides a simple way to lookup keys but not the values. I understand why (non-uniqueness possibility of values). However, what if each and every entry (keys and values) are strictly unique. Is there a collection that easily lets me store and lookup whatever key I chose and returns the matching field of such key? When looking up values I would necessarily have to specify the key field and the desired lookup field. I target C# 4.0

Example:

Collection<string, int, myEnum> myCollection = new ...

myCollection.Add("abc", 5, myEnum.First);
myCollection.Add("def", 6, myEnum.Second);

myCollection[int, myEnum, 6] = Second (of type myEnum) -> I just made up the way how a key and value field could be specified. Does such collection exist or would I need to roll my own?

I understand I am getting into the concept of tables but would like to avoid using a table structure if at all possible.

Note: The number of tuples will be limited to <20 (so if I rolled my own I would not be overly concerned with having to loop but I look for something more elegant)

Thanks

Arion
  • 31,011
  • 10
  • 70
  • 88
Matt
  • 7,004
  • 11
  • 71
  • 117
  • Jon Skeet posted a class that does what you are looking for here: http://stackoverflow.com/questions/255341/getting-key-of-value-of-a-generic-dictionary – Ruben Apr 23 '12 at 07:07
  • Ruben, great, you beat me to it, just saw it myself but found you posted the link already. Much appreciated, exactly what I was looking for. I am surprised there is no built-in collection for something so trivial. Maybe the use case comes up very rarely because most values are not enforced to be unique but nonetheless I find it valuable to have. – Matt Apr 23 '12 at 07:50

2 Answers2

2

You can just a hashset with a tuple. Like this:

var hs=new HashSet<Tuple<string,int,myEnum>>();

then to add you do this (the add function is O(1)):

hs.Add(new Tuple<string,int,myEnum>("abc", 5, myEnum.First));
hs.Add(new Tuple<string,int,myEnum>("def", 6, myEnum.Second));

to look up the tuple you do this (the contains function is O(1)):

var tupleTolookUp=new Tuple<string,int,myEnum>("abc", 5, myEnum.First);
if(hs.Contains(tupleTolookUp))
{
    //do what ever with the tupleTolookUp
}

In the hashset the Tuples has to be unique. But the good thing is that you do not have to override the Equals and gethashcode. Because the tuple does that internal.

References:

Arion
  • 31,011
  • 10
  • 70
  • 88
  • thanks, interesting idea, however, it does not really solve the question, looking up, for instance, myEnum.First to return "abc" – Matt Apr 23 '12 at 07:22
0

Here's a list of all collections in .NET:
http://msdn.microsoft.com/en-us/library/system.collections.generic.aspx
As you can see, there's no such collection to be found.

I'm also pretty sure there's no standard Data Structure in the common Computer Science literature that describes such a paradigm.

Arion's implementation would provide what you need, but it lacks in performance when searching for Keys/Values, and does not provide uniquity of keys or values, but only of pairs.

Would you could do is implement your Data Structure with two Dictionaries, where the value of one references the key of the other.

Svarog
  • 2,188
  • 15
  • 21
  • I mentioned specifically uniqueness is enforced, and I asked because I am well aware of the standard collections in C#. Your suggestion at the end does not solve the problem because you cannot easily look up the value of the second dictionary to return the matching key of the first dictionary. – Matt Apr 23 '12 at 07:19
  • Yes you can. I might not bern clear enough, but what I suggested is that value in Dictionary A points to key in Dictionary B, while value in Dictionary B points to key of Dictionary A. – Svarog Apr 24 '12 at 07:35
  • I know what you are referring to. I said there is no simple way to lookup the key of Dict A using value "abc" of dict C. Thats why Jon Skeet came up with his solution that I stumbled upon and which Ruben kindly pointed me to. – Matt Apr 25 '12 at 03:42