Пример #1
0
        /// <summary>
        /// Swap elements in elementStore at index1 and index2
        /// </summary>
        /// <param name="index1"></param>
        /// <param name="index2"></param>
        private void Swap(int index1, int index2)
        {
            PriorityQueueElement <T> temp = elementStore[index1];

            elementStore[index1] = elementStore[index2];
            elementStore[index2] = temp;
        }
Пример #2
0
        /// <summary>
        /// Inserts a new value in the priority queue
        ///
        /// Step: insert the new value at the end and percolate upwards
        /// </summary>
        /// <param name="item"></param>
        /// <param name="priority"></param>
        public void Insert(T item, int priority)
        {
            if (currentNumOfElement >= elementStore.Length)
            {
                // condition where the priority queue is full
                throw new OverflowException("The priority queue is full");
            }
            elementStore[currentNumOfElement] = new PriorityQueueElement <T>(item, priority);
            int currentIndex = currentNumOfElement;
            int parentIndex  = (int)Math.Floor((currentIndex - 1) / 2.0);

            while (parentIndex >= 0)
            {
                if (elementStore[parentIndex].CompareTo(elementStore[currentIndex]) > 0)
                {
                    Swap(currentIndex, parentIndex);
                }
                currentIndex = parentIndex;
                parentIndex  = (int)Math.Floor((currentIndex - 1) / 2.0);
            }
            currentNumOfElement++;
        }
Пример #3
0
        public int CompareTo(object obj)
        {
            PriorityQueueElement <T> newElem = (PriorityQueueElement <T>)obj;

            return(Priority.CompareTo(newElem.Priority));
        }