簡體   English   中英

更快地找到列表/數組中的元素

[英]Faster way to find elements in list/array

我正在研究一個有限元程序,其中我的輸入是一個節點列表(ListNodes double[node_index, x, y, z]其中(x,y,z)是笛卡爾系統中節點的坐標),列表elements (ListElements double[element_index, node1_index, node2_index, etc])和每個元素的一些數據(double[element_index])

我必須在有限元模型中的某個給定區域搜索數據,區域由某個邊界[xmin, xmax, ymin, ymax, zmin, zmax] 為了檢索該區域的數據,我首先查找其節點(搜索節點使得(x,y,z)在邊界內)然后查找其元素並最終檢索這些元素的數據。

功能1:檢查節點是否在區域內

private static bool IsBounded(double[] node, double xmin, double ymin, double zmin, double xmax, double ymax, double zmax)
{
    return ((node[1] >= xmin) && (node[1] <= xmax) && (node[2] >= ymin) && (node[2] <= ymax) && (node[3] >= zmin) && (node[3] <= zmax));
}

功能2:檢查所有節點以查找區域內的節點並將其添加到zoneNodes

ListPoint.Where(node => IsBounded(node, xmin, ymin, zmin, xmax, ymax, zmax)).ToList().ForEach(node => zoneNodes.Add(Convert.ToInt32(node[0])));

功能3:在區域內查找元素

// Loop over all elements
for (int j = 0; j < ListElement.Count; j++)
{
    int status = 0;
    for (int i = 1; i < ElementList[j].Count; i++)
    {
        // For each element, check how many nodes are inside the zone
        if (zoneNodes.Contains(Convert.ToInt32(ListElement[j][i])))
        {
            status++;
        }
    }

    // If all the nodes of this element are inside the zone then the element is inside the zone
    if (status == ListElement[j].Count - 1)
    {
        zoneElements.Add(Convert.ToInt32(ElementList[j][0]));
    }
}

功能4:對於區域內的每個元素,我們可以檢索數據

然而,這個過程非常慢。 有沒有辦法改善這個過程以獲得更快的性能?

謝謝,

我不確定這段代碼是你想要的。

請檢查。

減少計算時間的主要方法是使用Dictionary。

Dictionary的時間復雜度通常為O(1)。

public class Sample
{
    // add data to that variables.
    System.Collections.Generic.Dictionary<double, Point> pointPerElement = new System.Collections.Generic.Dictionary<double, Point>();
    System.Collections.Generic.Dictionary<double, Cube> cubePerElement = new System.Collections.Generic.Dictionary<double, Cube>();
    System.Collections.Generic.List<double> elements = new System.Collections.Generic.List<double>();

    private void Calculate()
    {
        foreach (var element in elements)
        {
            if (pointPerElement.ContainsKey(element) == false || cubePerElement.ContainsKey(element) == false)
            {
                continue;
            }
            var point = pointPerElement[element];
            var cube = cubePerElement[element];
            if (cube.IsBouded(point))
            {
                // add point or cube or element to list.
            }
        }
    }
}

private struct Point
{
    public double x;
    public double y;
    public double z;

    public Point(double x, double y, double z)
    {
        this.x = x;
        this.y = y;
        this.z = z;
    }

    public static Point GetVector(Point from, Point to)
    {
        return new Point(to.x - from.x, to.y - from.y, to.z - from.z);
    }
}

private struct Range
{
    public double min;
    public double max;

    public double length;
    public double center;

    public Range(double min, double max)
    {
        System.Diagnostics.Debug.Assert(min < max);
        this.min = min;
        this.max = max;
        this.length = max - min;
        this.center = length * 0.5;
    }
}

private struct Cube
{
    public Range xRange;
    public Range yRange;
    public Range zRange;

    private Point center;

    public Cube(Range xRange, Range yRange, Range zRange)
    {
        this.xRange = xRange;
        this.yRange = yRange;
        this.zRange = zRange;

        this.center = new Point(xRange.center, yRange.center, zRange.center);
    }

    public bool IsBouded(Point point)
    {
        var v = Point.GetVector(point, this.center);
        var doubledV = new Point(v.x * 2, v.y * 2, v.z * 2);
        return doubledV.x <= this.xRange.length
            && doubledV.y <= this.yRange.length
            && doubledV.z <= this.yRange.length;
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM