Beispiel #1
0
        public void Push(T itemToAdd, int priority)
        {
            var item = new HeapItem <T> (itemToAdd, itemCount);

            items[itemCount]      = item;
            priorities[itemCount] = priority;
            itemCount++;
        }
Beispiel #2
0
        public HeapItem <T> PushAndGet(T itemToAdd, int priority)
        {
            var item = new HeapItem <T> (itemToAdd, itemCount);

            items[itemCount]      = item;
            priorities[itemCount] = priority;
            itemCount++;

            return(item);
        }
Beispiel #3
0
        private void SortUp(HeapItem <T> item)
        {
            var ownPriority = priorities[item.Index];

            while (true)
            {
                var parentIndex    = GetParentIndex(item.Index);
                var parentPriority = priorities[parentIndex];

                if (ownPriority > parentPriority)
                {
                    Swap(item.Index, parentIndex);
                }
                else
                {
                    break;
                }
            }
        }
Beispiel #4
0
        private void SortDown(HeapItem <T> item)
        {
            while (true)
            {
                var itemIndex       = item.Index;
                var leftChildIndex  = itemIndex * 2 + 1;
                var rightChildIndex = itemIndex * 2 + 2;


                if (leftChildIndex < itemCount)
                {
                    var swapIndex = leftChildIndex;

                    if (rightChildIndex < itemCount)
                    {
                        var leftPriority  = priorities[leftChildIndex];
                        var rightPriority = priorities[rightChildIndex];

                        if (leftPriority < rightPriority)
                        {
                            swapIndex = rightChildIndex;
                        }
                    }

                    if (priorities[itemIndex] < priorities[swapIndex])
                    {
                        Swap(item.Index, swapIndex);
                    }
                    else
                    {
                        return;
                    }
                }
                else
                {
                    return;
                }
            }
        }
Beispiel #5
0
 public bool Contains(HeapItem <T> item)
 {
     return(Equals(item, items[item.Index]));
 }
Beispiel #6
0
 public void UpdateItem(HeapItem <T> item, int priority)
 {
     priorities[item.Index] = priority;
     SortUp(item);
 }
Beispiel #7
0
 public int CompareTo(HeapItem <T> other)
 {
     return(Item.CompareTo(other.Item));
 }