コード例 #1
0
        private void SiftDown(int index)
        {
            PriorityQueueNodeWrapper min = minHeap[index];
            int minIndex = index;

            int leftChildIndex = GetLeftChildIndex(index);

            if (leftChildIndex < Count() && ComparePriorities(minHeap[leftChildIndex], min) < 0)
            {
                min      = minHeap[leftChildIndex];
                minIndex = leftChildIndex;
            }

            int rightChildIndex = GetRightChildIndex(index);

            if (rightChildIndex < Count() && ComparePriorities(minHeap[rightChildIndex], min) < 0)
            {
                minIndex = rightChildIndex;
            }

            if (minIndex != index)
            {
                SwapAndUpdateMap(index, minIndex);
                SiftDown(minIndex);
            }
        }
コード例 #2
0
        private void AttachNodeAtIndex(PriorityQueueNodeWrapper wrapper, int index)
        {
            GrowHeapByOne();

            this.minHeap[index] = wrapper;
            this.nodeUniqueKeyToHeapIndexMap[wrapper.Data.UniqueKey] = index;
        }
コード例 #3
0
        public void Enqueue(PriorityQueueNode node)
        {
            int lastIndex = Count();
            PriorityQueueNodeWrapper wrapper = new PriorityQueueNodeWrapper(node);

            AttachNodeAtIndex(wrapper, lastIndex);
            SiftUp(lastIndex);
        }
コード例 #4
0
        private PriorityQueueNodeWrapper DetachNodeAtIndex(int index)
        {
            PriorityQueueNodeWrapper wrapper = minHeap[index];

            this.nodeUniqueKeyToHeapIndexMap.Remove(wrapper.Data.UniqueKey);
            ShrinkHeapByOne();

            return(wrapper);
        }
コード例 #5
0
        private int ComparePriorities(PriorityQueueNodeWrapper node1, PriorityQueueNodeWrapper node2)
        {
            int result = node1.Priority.CompareTo(node2.Priority);

            if (result == 0)
            {
                result = node1.Sequence.CompareTo(node2.Sequence);
            }

            return(result);
        }
コード例 #6
0
        public override string ToString()
        {
            StringBuilder sb = new StringBuilder();

            for (int i = 0; i < heapCount; i++)
            {
                PriorityQueueNodeWrapper wrapper = minHeap[i];
                sb.AppendLine(string.Format("HeapIndex: {0}  UniqueKey: {1}  Priority: {2}  Sequence: {3}",
                                            i, wrapper.Data.UniqueKey, wrapper.Priority, wrapper.Sequence));
            }
            return(sb.ToString());
        }
コード例 #7
0
        public PriorityQueueNode Dequeue()
        {
            if (minHeap.Count == 0)
            {
                throw new PriorityQueueEmptyException();
            }

            int topIndex = 0;
            PriorityQueueNodeWrapper min = DetachNodeAtIndex(topIndex);

            int lastIndex = Count();
            PriorityQueueNodeWrapper last = DetachNodeAtIndex(lastIndex);

            AttachNodeAtIndex(last, topIndex);

            SiftDown(0);

            return(min);
        }