예제 #1
0
        private void Extend(int newSize)
        {
            var newHeap = new PriorityPair <TPriority, TItem> [newSize];

            heap_.CopyTo(newHeap, 0);
            heap_ = newHeap;
        }
예제 #2
0
        public void Enqueue(TPriority priority, TItem item)
        {
            var pair = new PriorityPair <TPriority, TItem>(priority, item);

            if (heap_.Length == Count)
            {
                Extend(heap_.Length * 2);
            }

            heap_[Count] = pair;
            ++Count;

            int c = Count - 1;

            while (c > 0)
            {
                int p = (c - 1) >> 1;
                if (Compare(heap_[p].Priority, priority) < 0)
                {
                    heap_[c] = heap_[p];
                    c        = p;
                }
                else
                {
                    break;
                }
            }

            heap_[c] = pair;
        }
예제 #3
0
        public PriorityPair <TPriority, TItem> Dequeue()
        {
            PriorityPair <TPriority, TItem> ret = heap_[0];
            int n = Count - 1;

            var item = heap_[n];
            int p    = 0;
            int c    = (p << 1) + 1;

            while (c < n)
            {
                if (c != n - 1 && Compare(heap_[c + 1].Priority, heap_[c].Priority) > 0)
                {
                    ++c;
                }

                if (Compare(item.Priority, heap_[c].Priority) < 0)
                {
                    heap_[p] = heap_[c];
                    p        = c;
                    c        = (p << 1) + 1;
                }
                else
                {
                    break;
                }
            }

            heap_[p] = item;
            Count--;

            return(ret);
        }
예제 #4
0
 public int CompareTo(PriorityPair <TPriority, TItem> target)
 => Priority.CompareTo(target.Priority);