Hello There, Guest!
View New Posts  |  View Today's Posts
[C#] Simple PriorityQueue<T> Implementation

  • 0 Vote(s) - 0 Average


11-19-2017, 01:50 AM #1
AceInfinity
Developer
*******
Administrators
Posts: 9,733 Threads:1,026 Joined: Jun 2011 Reputation: 76

Simple PriorityQueue<T> Implementation
Here's a simple generic PriorityQueue<T> class that I created when I needed some of this basic functionality recently.

Code:
internal enum PriorityLevel
{
    Low,
    Medium,
    High
}

internal class PriorityQueue<T> : ICollection, IReadOnlyCollection<T>
{
    private readonly Dictionary<PriorityLevel, Queue<T>> _priorityQueues;

    public int Count
    {
        get
        {
            return _priorityQueues[PriorityLevel.High].Count
                 + _priorityQueues[PriorityLevel.Medium].Count
                 + _priorityQueues[PriorityLevel.Low].Count;
        }
    }
    
    public PriorityQueue()
        : this(64)
    { }
    
    public PriorityQueue(int capacity)
    {
        _priorityQueues = new Dictionary<PriorityLevel, Queue<T>>(3)
        {
            { PriorityLevel.Low,    new Queue<T>(capacity) },
            { PriorityLevel.Medium, new Queue<T>(capacity) },
            { PriorityLevel.High,   new Queue<T>(capacity) }
        };
    }
    
    public void Clear()
    {
        _priorityQueues[PriorityLevel.Low].Clear();
        _priorityQueues[PriorityLevel.Medium].Clear();
        _priorityQueues[PriorityLevel.High].Clear();
    }
    
    public void Enqueue(PriorityLevel priority, T item)
    {
        _priorityQueues[priority].Enqueue(item);
    }

    public T Dequeue()
    {
        if (_priorityQueues[PriorityLevel.High].Count != 0)
            return _priorityQueues[PriorityLevel.High].Dequeue();
        
        if (_priorityQueues[PriorityLevel.Medium].Count != 0)
            return _priorityQueues[PriorityLevel.Medium].Dequeue();
        
        return _priorityQueues[PriorityLevel.Low].Dequeue();
    }

    public T Peek()
    {
        if (_priorityQueues[PriorityLevel.High].Count != 0)
            return _priorityQueues[PriorityLevel.High].Peek();
        
        if (_priorityQueues[PriorityLevel.Medium].Count != 0)
            return _priorityQueues[PriorityLevel.Medium].Peek();
        
        return _priorityQueues[PriorityLevel.Low].Peek();
    }
    
    public IEnumerator<T> GetEnumerator()
    {
        foreach (var item in _priorityQueues[PriorityLevel.High])
            yield return item;
        
        foreach (var item in _priorityQueues[PriorityLevel.Medium])
            yield return item;
        
        foreach (var item in _priorityQueues[PriorityLevel.Low])
            yield return item;
    }

    IEnumerator IEnumerable.GetEnumerator()
    {
        return GetEnumerator();
    }

    public void CopyTo(Array array, int index)
    {
        using (var enumerator = GetEnumerator())
        {
            while (enumerator.MoveNext())
            {
                array.SetValue(enumerator.Current, index++);
            }
        }
    }

    int ICollection.Count { get { return Count; } }

    public object SyncRoot { get { return this; }  }
    public bool IsSynchronized { get { return false; } }
    int IReadOnlyCollection<T>.Count { get { return Count; } }
}

Works pretty decent for what it is. :)


Microsoft MVP .NET Programming - (2012 - Present)
®Crestron DMC-T Certified Automation Programmer

Development Site: aceinfinity.net

 ▲
 ▲ ▲




Forum Jump:


Possibly Related Threads...
Thread Author Replies Views Last Post
   Basic Generic Publisher/Subscriber Implementation AceInfinity 0 468 11-19-2017, 01:56 AM
Last Post: AceInfinity
   Merge Sort Algorithm Implementation AceInfinity 5 4,228 10-11-2013, 08:59 PM
Last Post: AceInfinity
   Fast Notification Manager [Simple Application] AceInfinity 12 6,675 05-24-2012, 01:10 AM
Last Post: AceInfinity
   Simple Question > lewisroy227 15 5,858 10-04-2011, 09:55 PM
Last Post: lewisroy227
  Simple Text & Still Picture RPG (Still In The Making) Ghoster 13 7,973 09-06-2011, 12:23 AM
Last Post: Ghoster


Users browsing this thread: 1 Guest(s)