views:

667

answers:

5

I want to build 2-dimentional collection where i need unique combination of key value pairs. For example Domain "Company" (Id: 1) can have MachineName "Machine1" and "Machine2", but cannot add another MachineName "Machine1" again. Another Domain "Corporate" (Id:2) can have another machineName "Machine1".

here my collection will be like this 1-Machine1, 1-Machine2, 2-Machine1. Adding 1-Machine1 or 2-Machine1 should be invalid entry. Please suggest datatype or approach for this. I cannot use Dict> datatype, because it may hamper performance if size grows.

+1  A: 

I'm sorry, but from your description it still sounds like a Dictionary implementation would be a good fit.

If and when the performance of the application suffers due to the speed of the dictionary, then you can revisit the problem and roll your own specifically tailored solution.

Coxy
No. I can't .If I want to use dictionary,then I have to keep DomainId as Key and List of MachineName as value.For every value addition I have to check first key exists, if not add key. If key exists then check if value exist if no then add value to list, else skip addition. Relatively look might be like like check if key exists and if yes then get check if value exists in list then continue program execution.
mandar
I don't think you do have to do those checks. I _think_ with a dictionary, you can just blindly reference `myDomains["domain1"]`. If the `domain1` entry is already there, pointing to that entry. If it's not there, it'll create it.
Damovisa
A: 

Do you need to be able to look up the list of domains with given machine name efficiently? Otherwise a Hashtable<String, HashSet<String>> seems like a good fit.

There also seems to be something called NameValueCollection which might be a good fit if you change the defaults so that it isn't case- or culture-sensitive.

Tuure Laurinolli
I m using C# 2.0.So cannot use HashSet
mandar
@mandar - That's a shame... Any particular reason you can't target 3.5?
Damovisa
i m sorry, but it is legacy application, developed nearly 4-5 years ago.
mandar
@mandar, What about NameValueCollection? From a cursoru glance at documentation it appears to be approximately the same thing?
Tuure Laurinolli
A: 

You didn't state this as a requirement, but my guess is that you also need to be able to query the data structure for all of the machines for a specific "domain". Ex. list the machines belonging to Company 1. This is the only reason I can think of where the performance of using a Dictionary might be unacceptable (since you would have to traverse the entire list to find all of the matching entries).

In that case you might consider representing the data as a tree.

Edit:

Based on your comment above, you could just concatenate your keys as a string and use a HashSet to check if you've already stored that key.

Michael Petito
A: 

You could do something like this:

Dictionary<String, List<String>> mapping = new Dictionary<string, List<string>>();
mapping.Add("1",new List<string>());
mapping["1"].Add("Machine1");
mapping["1"].Add("Machine2");

This will give you a one to many mapping between domain and machines.

or the NameValueCollection class would do the same.

Nathan W
This won't stop you from doing `mapping["1"].Add("Machine1")` again though... I believe machines need to be unique.
Damovisa
true, you would have to do checks.
Nathan W
A: 

So you need some kind of collection with a unique key, and each item within this collection is unique.

So really, you're talking about a dictionary where the value within the dictionary is a unique collection.

Assuming you're only talking about strings, I'd be using something like:

Dictionary<string, HashSet<string>>

Someone correct me if I'm wrong, but I think the advantage of using these generic structures is you can (right off the bat), do this:

Dictionary<string, HashSet<string>> domains = new Dictionary<string, HashSet<string>>();
domains["Domain1"].Add("Machine1");
Damovisa
Yes u r right: "value within the dictionary is a unique collection".But is there any alternative to start without Dictionary. I don't mind "Key" (domainId in this case) getting duplicated. Because I just have add or clear or iterate through combination. May be some datatype ensuring unique Key-Value combination.
mandar
I'm still not sure why having a dictionary is a problem. It's a very fast structure for retrieving objects based on a key. Even if the list of domains gets very very large, you'll still be able to access its list of machines in about O(1) time.
Damovisa