简体   繁体   中英

Simple lookup function

I've got a simple struct which I want to use as a lookup table:

public struct TileTypeSize
{
    public string type;
    public Size size;

    public TileTypeSize(string typeIn, Size sizeIn)
    {
        type = typeIn;
        size = sizeIn;
    }
}

I populate this thusly:

        tileTypeSizeList.Add(new TileTypeSize("W",rectangleSizeWall));
        tileTypeSizeList.Add(new TileTypeSize("p",rectangleSizePill));
        tileTypeSizeList.Add(new TileTypeSize("P",rectangleSizePowerPill));
        tileTypeSizeList.Add(new TileTypeSize("_",rectangleSizeWall));
        tileTypeSizeList.Add(new TileTypeSize("=",rectangleSizeWall));

What is the most efficient way to look up the size for a given type?

Thanks in advance!

If you know there will be one and only one match in the collection, then you can use:

var size = tileTypeSizeList.Single(t => t.type == someType).size;

If not, you'll have to be a little more clever to properly handle the cases where no match is found:

Size size;
var match = 
    tileTypeSizeList
        .Cast<TileTypeSize?>().FirstOrDefault(t => t.type == someType);
if(match != null) size = match.size;

Keep in mind, though, that there are better ways to store this information if that is the only data in the struct. I would suggest a Dictionary<string, Size> .

In general , the most efficient way would be to put your data into a Dictionary or similar container instead ( SortedDictionary and SortedList have small differences from Dictionary and are an even better fit in certain cases):

var dict = new Dictionary<string, Size>
{
     { "W", rectangleSizeWall },
     // etc
}

And then:

var size = dict["W"];

You can of course still iterate sequentially over the values in the dictionary if there is reason to do so.

If 5 types is all you are going to be looking up (ie the size of the problem is ridiculously small) then a straight list like you have would likely be faster than an associative container. So:

var tileStruct = tileTypeSizeList.FirstOrDefault(s => s.type == "W");
if (tileStruct.type == "") {
    // not found
}
else {
    var size = tileStruct.size;
}

You may remove the "if found" check if you are sure that you will never have a search miss.

var type = tileTypeSizeList.FirstOrDefault(t => t.type == someType);
if(type==null) throw new NotFoundException();
return type.size;

But if the list is big and you need to lookup data really often you better use Dictionary as noticed in other answers.

Use a Dictionary instead of a List:

Dictionary<string, TileTypeSize> tileTypeSizeDictionary = Dictionary<string, TileTypeSize>();
tileTypeSizeDictionary.Add("W", new TileTypeSize("W",rectangleSizeWall));
...

You lookup your elements with:

  TileTypeSize rectangleSizeWall = tileTypeSizeDictionary["W"];

A dictionary is faster than a list when you need to lookup by key.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM