C# 利用准则寻找匹配向量的有效方法
我有一个字典,其中键由一个向量2定义,我正在尝试执行一个包含匹配Y值的键的函数。(构建图表) 现在我使用两个foreach循环,一个遍历每个条目,另一个查找匹配条件的键C# 利用准则寻找匹配向量的有效方法,c#,search,vector,C#,Search,Vector,我有一个字典,其中键由一个向量2定义,我正在尝试执行一个包含匹配Y值的键的函数。(构建图表) 现在我使用两个foreach循环,一个遍历每个条目,另一个查找匹配条件的键 foreach(KeyValuePair<Vector2, TransportData> entry in transportDictionary) //for every value in dictionary { Vector2 forpos = entry.Key; foreach(KeyVa
foreach(KeyValuePair<Vector2, TransportData> entry in transportDictionary)
//for every value in dictionary
{
Vector2 forpos = entry.Key;
foreach(KeyValuePair<Vector2, TransportData> searchEntry in transportDictionary)
//go through every value in dictionary
{
if(searchEntry.Key.y == forpos.y && searchEntry.Key.x != forpos.x)
//if something is found with matching Y value, at a different X value as to not include itself
{
DoSomething(forpos, searchEntry.key);
//pass the two matched values as arguments
}
}
DoSomethingElse(forpos); //(functions need to be run on every entry individually too)
}
foreach(transportDictionary中的KeyValuePair条目)
//对于字典中的每个值
{
矢量2 forpos=输入键;
foreach(transportDictionary中的KeyValuePair searchEntry)
//查字典里的每一个值
{
if(searchEntry.Key.y==forpos.y&&searchEntry.Key.x!=forpos.x)
//如果发现某物具有匹配的Y值,则在不同的X值处不包括其自身
{
DoSomething(forpos,searchEntry.key);
//将两个匹配的值作为参数传递
}
}
DoSomethingElse(forpos);//(每个条目也需要单独运行函数)
}
它是有效的,但它的效率非常高,我认为这本字典有一千多条词条。对于50个条目的小测试集,此操作已经花费了不可接受的长时间
如何优化此操作?(还是我做了一些根本错误的事情?)
如果它有助于找到方法,则此应用程序中每个矢量2的x和y坐标将始终为整数
--编辑--
无论如何,我需要在每个条目上运行一个函数,因此不必对起始字典进行子集设置。一个想法是,首先将transportDictionary筛选为至少有一个匹配的
Key.Y
,然后只处理一个键列表(因为这似乎就是您所需要的)
然后,您还可以将第二个foreach
更改为仅与具有Y
匹配项的键进行比较,这样您就不会遍历每个循环中的所有键:
最后,您还可以在运行过程中删除刚刚处理的所有项,这样您就不会对它们进行多次迭代(当然,我不知道DoSomething()
有什么作用……如果您需要对匹配项进行多次迭代,那么这将不起作用):
列出haveAmatch=transportDictionary的所有密钥。其中(当前=>
transportDictionary.Count(其他=>current.Key.Y==other.Key.Y)>1)
.Select(item=>item.Key)
.ToList();
while(allkeyshaveamatch.Any())
{
//拿到第一把钥匙
var currentKey=allkeyshaveamatch.First();
//获取所有匹配的密钥
var matchingKeys=具有匹配的所有键
.Skip(1)
.Where(candidateKey=>candidateKey.Y==currentKey.Y)
.选择(匹配=>match)
.ToList();
//对每一场比赛都做些什么
foreach(匹配键中的var matchingKey)
{
DoSomething(当前键、匹配键);
}
//删除我们刚刚处理的密钥
所有具有匹配的键。移除(当前键);
}
似乎您正在使用=
匹配某些数据。这就是字典的用途。为什么不使用TryGetValue
和其他O(1)方法?顺便说一句,性能问题似乎是更好的地方。我以前从未使用过Linq(一周前刚开始编程),我花了一段时间才理解语法,但我还是成功了。然而,我做了一些基准测试,它实际上比我最初的蛮力方法稍微慢一点。对于非常大的数据集(我只测试了200个条目),它可能会更快,但我有另一个想法,即创建一个反向字典,并在其中查找匹配值。我真的很感激你如何格式化和命名你的建议代码,使它更容易理解。
List<Vector2> allKeysThatHaveAMatch = transportDictionary.Where(current =>
transportDictionary.Count(other => current.Key.Y == other.Key.Y) > 1)
.Select(item => item.Key)
.ToList();
while (allKeysThatHaveAMatch.Any())
{
// Get the first key
var currentKey = allKeysThatHaveAMatch.First();
// Get all matching keys
var matchingKeys = allKeysThatHaveAMatch
.Skip(1)
.Where(candidateKey => candidateKey.Y == currentKey.Y)
.Select(match => match)
.ToList();
// Do Something with each match
foreach (var matchingKey in matchingKeys)
{
DoSomething(currentKey, matchingKey);
}
// Remove the key we just processed
allKeysThatHaveAMatch.Remove(currentKey);
}