Beispiel #1
0
    // moves the meta info to a new position, uses RefEquals
    public static void UpdateInSorted(this List <Pathfinding.NodeMetaInfo> nodes, Pathfinding.NodeMetaInfo newNode, float oldTotalCost)
    {
        float newTotalCost = newNode.totalCost;

        newNode.totalCost = oldTotalCost;
        int ind = nodes.FindIndexSorted(newNode);

        newNode.totalCost = newTotalCost;

        if (ind > 0 && newNode.totalCost < nodes[ind - 1].totalCost) // need to swap left (shift right)
        {
            while (ind > 0 && newNode.totalCost < nodes[ind - 1].totalCost)
            {
                nodes[ind] = nodes[ind - 1];
                ind--;
            }
            nodes[ind] = newNode;
        }
        else if (ind < nodes.Count - 1 && newNode.totalCost > nodes[ind + 1].totalCost) // need to swap right (shift left)
        {
            while (ind < nodes.Count - 1 && newNode.totalCost > nodes[ind + 1].totalCost)
            {
                nodes[ind] = nodes[ind + 1];
                ind++;
            }
            nodes[ind] = newNode;
        }
    }
Beispiel #2
0
    // binary search for index of an element, uses RefEquals
    public static int FindIndexSorted(this List <Pathfinding.NodeMetaInfo> nodes, Pathfinding.NodeMetaInfo node)
    {
        int left = 0, right = nodes.Count;
        int index = -1;

        while (right > left)
        {
            int center = (left + right) / 2;

            if (nodes[center].totalCost == node.totalCost)
            {
                index = center;
                break;
            }
            else if (nodes[center].totalCost < node.totalCost)
            {
                left = center + 1;
            }
            else
            {
                right = center;
            }
        }

        if (index > -1)
        {
            // we found one of the occurances, now need to go through the range of em and check every one
            // to the right
            int i = index;
            while (i < nodes.Count && nodes[i].totalCost == node.totalCost)
            {
                if (ReferenceEquals(nodes[i++], node))
                {
                    return(i - 1);
                }
            }
            // to the left
            i = index - 1;
            while (i >= 0 && nodes[i].totalCost == node.totalCost)
            {
                if (ReferenceEquals(nodes[i--], node))
                {
                    return(i + 1);
                }
            }
        }
        return(index);
    }
Beispiel #3
0
    // assuming the array is sorted, insert a node retaining sorted order using binary search
    public static void AddSorted(this List <Pathfinding.NodeMetaInfo> nodes, Pathfinding.NodeMetaInfo newNode)
    {
        int left = 0, right = nodes.Count;

        while (right > left)
        {
            int center = (left + right) / 2;
            if (nodes[center].totalCost <= newNode.totalCost)
            {
                left = center + 1;
            }
            else
            {
                right = center;
            }
        }
        nodes.Insert(left, newNode);
    }
Beispiel #4
0
 // using binary search check for node, uses RefEquals
 public static bool ContainsSorted(this List <Pathfinding.NodeMetaInfo> nodes, Pathfinding.NodeMetaInfo node)
 {
     return(nodes.FindIndexSorted(node) != -1);
 }