5

Ended up with this awful data structure:

List<KeyValuePair<string, KeyValuePair<string, string>>>

It's not likely to get huge (<1K I estimate) and I am gonna iterate this list over and over.

Anyone can think of some better alternative with built-in types?

JohnIdol
  • 48,899
  • 61
  • 158
  • 242
  • 1
    answers to this question might be helpful http://stackoverflow.com/questions/101825/whats-the-best-way-of-using-a-pair-triple-etc-of-values-as-one-value-in-c – Timothy Carter Jun 24 '09 at 16:48

2 Answers2

10
struct MrStruct
{
   public string Key1,
   public string Key2,
   public string Value1
}


List<MrStruct>;

This is assuming that you are accessing the list sequentially as you did say iterate over. Potentially, other data structures could be faster for searching.

kemiller2002
  • 113,795
  • 27
  • 197
  • 251
  • plain and simple- I like it - would this perform better than what I have querying on key1? – JohnIdol Jun 24 '09 at 16:50
  • If key1 is unique you could consider SortedList, otherwise you could sort your List<> based on MrStruct.Key1 for performance. – user7116 Jun 24 '09 at 16:55
  • What type of querying are you doing? If the Key1 is unique, using a dictionary would be much, much faster... – Reed Copsey Jun 24 '09 at 16:56
  • Depends on a couple of different factors, you could sort the keys by Key1 and assuming that Key1 is unique you can do a binary search on it which gives you an approximate o(log n) search time. Since yours is also in a list and not something like a hashtable, I think that would be about the fastest search your could perform. – kemiller2002 Jun 24 '09 at 16:56
  • Key1 is not unique (otherwise would have picked a dictionary) – JohnIdol Jun 24 '09 at 17:04
  • A dictionary can be faster depending on the scenario. Creating a dictionary might have a higher set up time than the list, and if you are only doing a handful of searches against it and still plan on iterating the entire list, this structure would probably ultimately work against you. – kemiller2002 Jun 24 '09 at 17:05
  • @JohnIdol: See my revision for a Dictionary approach with non-unique keys. It's kind of ugly, but it'd make finding all of the elements given a key1 value very fast - much faster than any other option listed. If you're doing many searches, this can help. – Reed Copsey Jun 24 '09 at 17:11
  • I think you'd have to test that, because you are going to pay the setup time to create the dictionary, and it will be much more difficult to pull all the values out if you need a complete list. – kemiller2002 Jun 24 '09 at 17:24
  • Kevin, any reason for using a struct? I know that [KeyValuePair](https://msdn.microsoft.com/en-us/library/5tbh8a42%28v=vs.110%29.aspx) is a struct but this does have [a performance hit](http://www.dotnetperls.com/tuple-keyvaluepair). – Ben Jun 07 '15 at 14:19
  • It all depends on what you want to do with it. The performance hit is negligible, and in the big scope of things class vs. struct should almost never be compared for a performance optimization. Really what should be asked is "do I need it to be pass by reference or pass by value?" – kemiller2002 Jun 07 '15 at 14:58
10

The best option would be to wrap your own Tuple class, sort of like the one shipping in .NET 4.0.

Then you could have a single:

List<Tuple<string,string,string>>

This is easy enough to write in .NET 2.0 - it's basically just a triplet of values, instead of having 2 in a KeyValuePair. There is no built-in equivelent for a triplet of values in .NET 2.0, though.


Edit:

After reading your comment about querying in another post, I thought I'd mention this as well -

Even if you don't have unique values in key1, you could dramatically speed up any type of query/search by using:

Dictionary<string, List<KeyValuePair<string,string>>>

Then, instead of storing a single KeyValuePair, you could look up the list of them via the key in the first element. This would be much, much faster if you needed to find all of the elements with a given first key...

Reed Copsey
  • 554,122
  • 78
  • 1,158
  • 1,373
  • I like the dictionary version - I came up with some thing like that as well (as a sort of group by approach) - I am probably gonna be searching that dictionary many times on that string that is now key – JohnIdol Jun 24 '09 at 17:22