mirror of
https://github.com/ICDSystems/ICD.Common.Utils.git
synced 2026-01-11 19:44:55 +00:00
feat: Initial commit of PriorityQueue collection
This commit is contained in:
@@ -6,7 +6,8 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
|
||||
|
||||
## [Unreleased]
|
||||
### Added
|
||||
- Adding IcdOrderedDictionary collection
|
||||
- Added IcdOrderedDictionary collection
|
||||
- Added PriorityQueue collection
|
||||
|
||||
## [3.2.0] - 2018-05-09
|
||||
### Added
|
||||
|
||||
140
ICD.Common.Utils.Tests/Collections/PriorityQueueTest.cs
Normal file
140
ICD.Common.Utils.Tests/Collections/PriorityQueueTest.cs
Normal file
@@ -0,0 +1,140 @@
|
||||
using System.Collections.Generic;
|
||||
using ICD.Common.Utils.Collections;
|
||||
using NUnit.Framework;
|
||||
|
||||
namespace ICD.Common.Utils.Tests.Collections
|
||||
{
|
||||
[TestFixture]
|
||||
public sealed class PriorityQueueTest
|
||||
{
|
||||
#region Properties
|
||||
|
||||
[Test]
|
||||
public void CountTest()
|
||||
{
|
||||
PriorityQueue<int> queue = new PriorityQueue<int>();
|
||||
|
||||
Assert.AreEqual(0, queue.Count);
|
||||
|
||||
queue.Enqueue(1);
|
||||
queue.Enqueue(2);
|
||||
queue.Enqueue(3);
|
||||
|
||||
Assert.AreEqual(3, queue.Count);
|
||||
|
||||
queue.Clear();
|
||||
|
||||
Assert.AreEqual(0, queue.Count);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void IsSynchronizedTest()
|
||||
{
|
||||
PriorityQueue<int> queue = new PriorityQueue<int>();
|
||||
|
||||
Assert.IsFalse(queue.IsSynchronized);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void SyncRootTest()
|
||||
{
|
||||
PriorityQueue<int> queue = new PriorityQueue<int>();
|
||||
|
||||
Assert.AreEqual(queue, queue.SyncRoot);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Methods
|
||||
|
||||
[Test]
|
||||
public void ClearTest()
|
||||
{
|
||||
PriorityQueue<int> queue = new PriorityQueue<int>();
|
||||
|
||||
queue.Enqueue(1);
|
||||
queue.Enqueue(2);
|
||||
queue.Enqueue(3);
|
||||
|
||||
Assert.AreEqual(3, queue.Count);
|
||||
|
||||
queue.Clear();
|
||||
|
||||
Assert.AreEqual(0, queue.Count);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void EnqueueTest()
|
||||
{
|
||||
PriorityQueue<int> queue = new PriorityQueue<int>();
|
||||
|
||||
queue.Enqueue(1);
|
||||
queue.Enqueue(2);
|
||||
queue.Enqueue(3);
|
||||
|
||||
Assert.AreEqual(3, queue.Count);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void EnqueuePriorityTest()
|
||||
{
|
||||
PriorityQueue<int> queue = new PriorityQueue<int>();
|
||||
|
||||
queue.Enqueue(1, 3);
|
||||
queue.Enqueue(2, 2);
|
||||
queue.Enqueue(3, 1);
|
||||
|
||||
Assert.AreEqual(3, queue.Count);
|
||||
|
||||
List<int> dequeue = new List<int>
|
||||
{
|
||||
queue.Dequeue(),
|
||||
queue.Dequeue(),
|
||||
queue.Dequeue()
|
||||
};
|
||||
|
||||
Assert.AreEqual(3, dequeue[0]);
|
||||
Assert.AreEqual(2, dequeue[1]);
|
||||
Assert.AreEqual(1, dequeue[2]);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void DequeueTest()
|
||||
{
|
||||
PriorityQueue<int> queue = new PriorityQueue<int>();
|
||||
|
||||
queue.Enqueue(1, 3);
|
||||
queue.Enqueue(2, 2);
|
||||
queue.Enqueue(3, 1);
|
||||
|
||||
Assert.AreEqual(3, queue.Count);
|
||||
|
||||
List<int> dequeue = new List<int>
|
||||
{
|
||||
queue.Dequeue(),
|
||||
queue.Dequeue(),
|
||||
queue.Dequeue()
|
||||
};
|
||||
|
||||
Assert.AreEqual(3, dequeue[0]);
|
||||
Assert.AreEqual(2, dequeue[1]);
|
||||
Assert.AreEqual(1, dequeue[2]);
|
||||
|
||||
Assert.AreEqual(0, queue.Count);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void GetEnumeratorTest()
|
||||
{
|
||||
Assert.Inconclusive();
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void CopyToTest()
|
||||
{
|
||||
Assert.Inconclusive();
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
148
ICD.Common.Utils/Collections/PriorityQueue.cs
Normal file
148
ICD.Common.Utils/Collections/PriorityQueue.cs
Normal file
@@ -0,0 +1,148 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using ICD.Common.Properties;
|
||||
using ICD.Common.Utils.Extensions;
|
||||
|
||||
namespace ICD.Common.Utils.Collections
|
||||
{
|
||||
/// <summary>
|
||||
/// Provides a first-in first-out collection with enhanced insertion features.
|
||||
/// </summary>
|
||||
public sealed class PriorityQueue<T> : IEnumerable<T>, ICollection
|
||||
{
|
||||
private readonly IcdOrderedDictionary<int, List<T>> m_PriorityToQueue;
|
||||
private int m_Count;
|
||||
|
||||
#region Properties
|
||||
|
||||
/// <summary>
|
||||
/// Gets the number of items in the collection.
|
||||
/// </summary>
|
||||
public int Count { get { return m_Count; } }
|
||||
|
||||
/// <summary>
|
||||
/// Is the collection thread safe?
|
||||
/// </summary>
|
||||
public bool IsSynchronized { get { return false; } }
|
||||
|
||||
/// <summary>
|
||||
/// Gets a reference for locking.
|
||||
/// </summary>
|
||||
public object SyncRoot { get { return this; } }
|
||||
|
||||
#endregion
|
||||
|
||||
/// <summary>
|
||||
/// Constructor.
|
||||
/// </summary>
|
||||
public PriorityQueue()
|
||||
{
|
||||
m_PriorityToQueue = new IcdOrderedDictionary<int, List<T>>();
|
||||
}
|
||||
|
||||
#region Methods
|
||||
|
||||
/// <summary>
|
||||
/// Clears the collection.
|
||||
/// </summary>
|
||||
[PublicAPI]
|
||||
public void Clear()
|
||||
{
|
||||
m_PriorityToQueue.Clear();
|
||||
m_Count = 0;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Adds the item to the end of the queue.
|
||||
/// </summary>
|
||||
/// <param name="item"></param>
|
||||
[PublicAPI]
|
||||
public void Enqueue(T item)
|
||||
{
|
||||
Enqueue(item, int.MaxValue);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Adds the item to the queue with the given priority.
|
||||
/// Lower values are dequeued first.
|
||||
/// </summary>
|
||||
/// <param name="item"></param>
|
||||
/// <param name="priority"></param>
|
||||
[PublicAPI]
|
||||
public void Enqueue(T item, int priority)
|
||||
{
|
||||
if (!m_PriorityToQueue.ContainsKey(priority))
|
||||
m_PriorityToQueue.Add(priority, new List<T>());
|
||||
|
||||
m_PriorityToQueue[priority].Add(item);
|
||||
m_Count++;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Dequeues the first item with the lowest priority value.
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
[PublicAPI]
|
||||
public T Dequeue()
|
||||
{
|
||||
KeyValuePair<int, List<T>> kvp;
|
||||
if (!m_PriorityToQueue.TryFirst(out kvp))
|
||||
throw new InvalidOperationException("The queue is empty.");
|
||||
|
||||
int priority = kvp.Key;
|
||||
List<T> queue = kvp.Value;
|
||||
|
||||
T output = queue[0];
|
||||
queue.RemoveAt(0);
|
||||
|
||||
if (queue.Count == 0)
|
||||
m_PriorityToQueue.Remove(priority);
|
||||
|
||||
m_Count--;
|
||||
|
||||
return output;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets an enumerator for the items.
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public IEnumerator<T> GetEnumerator()
|
||||
{
|
||||
return m_PriorityToQueue.Values
|
||||
.SelectMany(v => v)
|
||||
.GetEnumerator();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Copies the collection to the target array.
|
||||
/// </summary>
|
||||
/// <param name="array"></param>
|
||||
/// <param name="index"></param>
|
||||
public void CopyTo(Array array, int index)
|
||||
{
|
||||
foreach (T item in this)
|
||||
{
|
||||
array.SetValue(item, index);
|
||||
index++;
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Private Methods
|
||||
|
||||
/// <summary>
|
||||
/// Gets an enumerator for the items.
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
IEnumerator IEnumerable.GetEnumerator()
|
||||
{
|
||||
return GetEnumerator();
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
@@ -75,6 +75,7 @@
|
||||
<ItemGroup>
|
||||
<Compile Include="Attributes\AbstractIcdAttribute.cs" />
|
||||
<Compile Include="Collections\IcdOrderedDictionary.cs" />
|
||||
<Compile Include="Collections\PriorityQueue.cs" />
|
||||
<Compile Include="Collections\WeakKeyDictionary.cs" />
|
||||
<Compile Include="Comparers\PredicateComparer.cs" />
|
||||
<Compile Include="ConsoleColor.cs" />
|
||||
|
||||
Reference in New Issue
Block a user