Exemplo n.º 1
0
        protected void DownHeap()
        {
            int i = 0;

            while (i <= (Size - 1) / 2)
            {
                AStarPriorityNode <T> node = heap[i];

                AStarPriorityNode <T> biggerChild = GetBiggerChild(GetChildren(i));
                if (biggerChild == null)
                {
                    break;
                }

                if (comparator(biggerChild.Priority, node.Priority))
                {
                    Swap(biggerChild.Index, node.Index);
                    i = node.Index;
                }
                else
                {
                    break;
                }
            }
        }
Exemplo n.º 2
0
 public void Add(AStarPriorityNode <T> node)
 {
     _size++;
     heap.Add(node);
     node.Index = heap.IndexOf(node);
     if (Size >= 2)
     {
         Upheap();
     }
 }
Exemplo n.º 3
0
        public void Add(T item, T parent, float priority)
        {
            _size++;
            AStarPriorityNode <T> newNode = new AStarPriorityNode <T>(item, parent, Size - 1, priority);

            heap.Add(newNode);
            newNode.Index = heap.IndexOf(newNode);
            if (Size >= 2)
            {
                Upheap();
            }
        }
Exemplo n.º 4
0
 public void Reverse()
 {
     for (int i = 0; i < Size; i++)
     {
         if (i >= Size - 1 - i)
         {
             break;
         }
         AStarPriorityNode <T> temp = heap[i];
         heap[i]            = heap[Size - 1 - i];
         heap[Size - 1 - i] = temp;
     }
 }
Exemplo n.º 5
0
        public AStarPriorityNode <T> SearchNode(T item)
        {
            AStarPriorityNode <T> searchResult = null;

            foreach (AStarPriorityNode <T> node in heap)
            {
                if (node.Item.Equals(item))
                {
                    searchResult = node;
                    break;
                }
            }
            return(searchResult);
        }
Exemplo n.º 6
0
        protected AStarPriorityNode <T> GetBiggerChild(List <AStarPriorityNode <T> > children)
        {
            AStarPriorityNode <T> biggerChild = null;

            if (children.Count == 1)
            {
                biggerChild = children[0];
            }
            if (children.Count == 2)
            {
                biggerChild = (comparator(children[0].Priority, children[1].Priority))? children[0] : children[1];
            }
            return(biggerChild);
        }
Exemplo n.º 7
0
        protected void BottomUp()
        {
            for (int i = Size / 2 - 1; i >= 0; i++)
            {
                AStarPriorityNode <T> father = heap[i];

                List <AStarPriorityNode <T> > children    = GetChildren(i);
                AStarPriorityNode <T>         biggerChild = GetBiggerChild(children);


                if (comparator(biggerChild.Priority, father.Priority))
                {
                    Swap(biggerChild.Index, father.Index);
                }
            }
        }
Exemplo n.º 8
0
        protected void Swap(int firstElementIntex, int secondElementIndex)
        {
            if (firstElementIntex < 0 || firstElementIntex >= Size)
            {
                //			Debug.Log("firstElementIntex: "+firstElementIntex+" out of range. Size is: "+Size);
            }
            if (secondElementIndex < 0 || secondElementIndex >= Size)
            {
                //			Debug.Log("secondElementIndex: "+secondElementIndex+" out of range");
            }

            AStarPriorityNode <T> firstElement  = heap[firstElementIntex];
            AStarPriorityNode <T> secondElement = heap[secondElementIndex];

            heap[firstElementIntex]  = secondElement;
            heap[secondElementIndex] = firstElement;

            firstElement.Index  = secondElementIndex;
            secondElement.Index = firstElementIntex;
        }
Exemplo n.º 9
0
        public T Pop()
        {
            AStarPriorityNode <T> highestPriorityElement = heap[0];

            if (Size >= 2)
            {
                heap[0]       = heap[Size - 1];
                heap[0].Index = 0;
                heap.RemoveAt(Size - 1);
            }
            else
            {
                heap.RemoveAt(0);
            }
            Size--;
            if (Size >= 2)
            {
                DownHeap();
            }
            return(highestPriorityElement.Item);
        }
Exemplo n.º 10
0
        protected void Upheap()
        {
            int i = Size - 1;

            while (i >= 0)
            {
                AStarPriorityNode <T> node   = heap[i];
                AStarPriorityNode <T> father = GetFather(node.Index);


                if (comparator(node.Priority, father.Priority))
                {
                    Swap(node.Index, father.Index);
                    i = node.Index;
                }
                else
                {
                    break;
                }
            }
        }