简体   繁体   English

简单的查找功能

[英]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> . 我建议使用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): 通常 ,最有效的方法是将数据放入Dictionary或类似的容器中( SortedDictionarySortedListDictionary有很小的差异,在某些情况下更适合):

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. 如果您将要查找5种类型 (即问题的大小非常小),那么像您这样的直接列表可能比关联容器更快。 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. 但是如果列表很大并且您需要经常查找数据,那么最好在其他答案中注意使用Dictionary

Use a Dictionary instead of a List: 使用Dictionary而不是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. 当您需要按键查找时,字典比列表更快。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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