Ejemplo n.º 1
0
        /// <summary>
        /// If the queue is empty we do just return null
        ///
        /// Operation: O(log n)
        /// </summary>
        public T Dequeue()
        {
            if (IsEmpty())
            {
                return(default(T));
            }

            KeyValueEntry <int, T> result = _queue[0];

            _queue[0] = _queue[Count - 1];
            RemovePosition(result.Value);
            UpdatePosition(_queue[0].Value, 0);
            _queue[Count - 1] = null;
            Count--;
            BubbleDown(0);

            return(result.Value);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Operation: O(log n)
        ///
        /// InvalidOperationException: If we add an element which is alread in the Queue
        /// </summary>
        public void Enqueue(T element, int priority)
        {
            if (Contains(element))
            {
                throw new InvalidOperationException("Cannot add an element which is already in the queue");
            }

            if (Count == _queue.Length)
            {
                KeyValueEntry <int, T>[] oldQueue = _queue;
                _queue = new KeyValueEntry <int, T> [oldQueue.Length * 3 / 2 + 1];
                Array.Copy(oldQueue, _queue, oldQueue.Length);
            }

            _queue[Count] = new KeyValueEntry <int, T>(priority, element);
            UpdatePosition(element, Count);
            Count++;

            BubbleUp(Count - 1);
        }