c# – 在大清单中找到彼此的近点
发布时间:2020-12-15 20:59:54 所属栏目:百科 来源:网络整理
导读:我有一个结构节点的大列表(如300000个元素),其中Node被定义为 public struct Node{ public int nid; public double x,y,z;} 并且我想知道哪些节点彼此接近一定量(在代码中称为间隙),以便组合在一起的元素. 请注意,对于给定的节点分布,每个节点仅属于一个集合
我有一个结构节点的大列表(如300000个元素),其中Node被定义为
public struct Node { public int nid; public double x,y,z; } 并且我想知道哪些节点彼此接近一定量(在代码中称为间隙),以便组合在一起的元素. 我试着用这种方式,循环每个节点并使用Linq查询和三维空间中两点之间的距离的经典公式寻找它的邻居,但它确实很慢. 有什么建议吗?提前致谢. List<Node> nList; [...] for (int i = 0; i < nList.Count; i++) // cycles through all nodes in list { Node nodo_old = nList[i]; // current node List<Node> nset = nList.Where(n => (Math.Sqrt(Math.Pow((n.x - nodo_old.x),2) + Math.Pow((n.y - nodo_old.y),2) + Math.Pow((n.z - nodo_old.z),2)) <= gap)).ToList(); [...] // now nset contains a list of neighbour nodes } 解决方法
此算法删除已检查的节点以减少处理时间.我没有尝试你的问题,但我相信它会帮助你,因为我在另一个场景测试它,它的工作完美.
// to hold sets of neighbour nodes Dictionary<int,List<Node>> relatedCollectionsDictionary = new Dictionary<int,List<Node>>(); int index = 0; List<Node> nList; while (nList.Any()) { var relatedCollection = nList.Where(n => (Math.Sqrt(Math.Pow((n.x - nList.First().x),2) + Math.Pow((n.y - nList.First().y),2) + Math.Pow((n.z - nList.First().z),2)) <= gap)); List<Node> relatedCollectionList = relatedCollection.ToList(); List<Node> relatedCollectionListForDictionary = relatedCollection.ToList(); relatedCollectionsDictionary.Add(index++,relatedCollectionListForDictionary); while (relatedCollectionList.Any()) { nList.Remove(relatedCollectionList.First()); relatedCollectionList.RemoveAt(0); } } 我们的想法是,您每次都要进行大量处理并遍历所有节点.但是使用这种情况,您不会多次遍历任何项目. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |