What is the most efficient way to create a lookup table in C #

What is the most efficient way to view a lookup table in C #

I have a lookup table. Similar to

0 "Thing 1"
1 "Thing 2"
2 "Reserved"
3 "Reserved"
4 "Reserved"
5 "Not a Thing"

So, if someone wants to "Thing 1" or "Thing 2", they pass at 0 or 1. But they can pass in another. I have 256 of these things and maybe 200 of them are reserved.

So, what do you most effectively want to customize?

  • An array string or dictionary variable that receives all values. And then take an integer and return the value at that place.

One problem with this solution is all the Reserved values. I do not want to create these redundant "reserved" values. Or else, I may have an if statement in all the different places that are "reserved", but now they can be only 2-3, maybe 2-3, 40-55 and all different places in the byte. This if statement will be uncontrollably fast.

  • My other option that I was thinking about was a switch statement. And I would have all 50-digit values ​​and would fail by default for the reserved values.

I am wondering if this is a lot more processing than creating a string array or dictionary and just returning the appropriate value.

  • Something else? Is there any other way to consider?
+5
9

, O (1), Dictionary (TKey, TValue) -.

var things = new Dictionary<int, string>();
things[0]="Thing 1";
things[1]="Thing 2";
things[4711]="Carmen Sandiego";
+10

( ) , ():

var myDictionary = new Dictionary<int, string>();
myDictionary.Add(0, "Value 1");
myDictionary.Add(200, "Another value");
// and so on

, , , (string [200]) / null.

var myArray = new string[200];
myArray[0] = "Value 1";
myArray[2] = "Another value";
//myArray[1] is null
+3

# . , , . ; , , .

- , (.. , ), . (, , NULL ) , , , .

- HashSet<int> , , , :

public class LookupTable
{
   public readonly Dictionary<int, string> Table { get; }
   public readonly HashSet<int> ReservedKeys { get; }

   public LookupTable()
   {
      Table = new Dictionary<int, string>();
      ReservedKeys = new HashSet<int>();
   }

   public string Lookup(int key)
   {
      return (ReservedKeys.Contains(key))
         ? null
         : Table[key];
   }
}

, - - Lookup null, , , , - Table.Values .

+3

Dictionary ( ) / , .

MSDN:

, O (1), < (Of < (TKey, TValue > ) > ) -.

"" , , /. , , , "" /, - .

, , Sparse Matrix.

0

, . . . , , . ?

null .

, , , .

, , , , .

0

var dic = new Dictionary<int, string>();

:

string GetDescription(int val)
{
     if(0 <= val && val < 256)
        if(!dic.Contains(val))
           return "Reserved";
        return dic[val];
    throw new ApplicationException("Value must be between 0 and 255");
}
0

. . - O (n), .

2- , ,

0

, . 256 , 0..255, ? , 256 .

, . "" , " " 50 , " ", , . , , .

? , , , .

On the other hand, I suppose you have proven that this search is your bottleneck, either by profiling or using stacks. If less than 10% of the time was spent in this request, when it is slow, then this is not your bottleneck so that you can do what is easiest to encode.

0
source

All Articles