Exemplo n.º 1
0
 public PriorityQueue(IEnumerable <T> contents)
 {
     Comparer = Comparer <T> .Default.Compare;
     _Buffer  = contents.ToArray();
     _Count   = _Buffer.Length;
     HeapQueue <T> .Heapify(_Buffer, _Count, Comparer);
 }
Exemplo n.º 2
0
        public T Dequeue()
        {
            if (_Count <= 0)
            {
                throw new InvalidOperationException("The queue is empty.");
            }

            _Count -= 1;
            T result = _Buffer[0];

            if (_Count > 0)
            {
                _Buffer[0]      = _Buffer[_Count];
                _Buffer[_Count] = default(T);
                HeapQueue <T> .SiftUp(_Buffer, 0, _Count, Comparer);
            }

            return(result);
        }
Exemplo n.º 3
0
 public void Enqueue(T value)
 {
     Grow(1);
     _Buffer[_Count - 1] = value;
     HeapQueue <T> .SiftDown(_Buffer, 0, _Count - 1, Comparer);
 }