mirror of
https://github.com/ICDSystems/ICD.Common.Utils.git
synced 2026-02-17 05:35:07 +00:00
Merge branch 'Collections' of Common/Utils into dev
This commit is contained in:
@@ -5,6 +5,9 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
|
|||||||
and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).
|
and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).
|
||||||
|
|
||||||
## [Unreleased]
|
## [Unreleased]
|
||||||
|
### Added
|
||||||
|
- Added IcdOrderedDictionary collection
|
||||||
|
- Added PriorityQueue collection
|
||||||
|
|
||||||
## [3.2.0] - 2018-05-09
|
## [3.2.0] - 2018-05-09
|
||||||
### Added
|
### Added
|
||||||
|
|||||||
126
ICD.Common.Utils.Tests/Collections/IcdOrderedDictionaryTest.cs
Normal file
126
ICD.Common.Utils.Tests/Collections/IcdOrderedDictionaryTest.cs
Normal file
@@ -0,0 +1,126 @@
|
|||||||
|
using System.Linq;
|
||||||
|
using ICD.Common.Utils.Collections;
|
||||||
|
using NUnit.Framework;
|
||||||
|
|
||||||
|
namespace ICD.Common.Utils.Tests.Collections
|
||||||
|
{
|
||||||
|
[TestFixture]
|
||||||
|
public sealed class IcdOrderedDictionaryTest
|
||||||
|
{
|
||||||
|
#region Properties
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void CountTest()
|
||||||
|
{
|
||||||
|
IcdOrderedDictionary<int, int> dict = new IcdOrderedDictionary<int, int>
|
||||||
|
{
|
||||||
|
{0, 0},
|
||||||
|
{1, 10},
|
||||||
|
{2, 20}
|
||||||
|
};
|
||||||
|
|
||||||
|
Assert.AreEqual(3, dict.Count);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void IsReadOnlyTest()
|
||||||
|
{
|
||||||
|
IcdOrderedDictionary<int, int> dict = new IcdOrderedDictionary<int, int>();
|
||||||
|
|
||||||
|
Assert.IsFalse(dict.IsReadOnly);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void KeysTest()
|
||||||
|
{
|
||||||
|
IcdOrderedDictionary<int, int> dict = new IcdOrderedDictionary<int, int>
|
||||||
|
{
|
||||||
|
{0, 0},
|
||||||
|
{1, 10},
|
||||||
|
{-1, -10}
|
||||||
|
};
|
||||||
|
|
||||||
|
int[] keys = dict.Keys.ToArray();
|
||||||
|
|
||||||
|
Assert.AreEqual(3, keys.Length);
|
||||||
|
Assert.AreEqual(-1, keys[0]);
|
||||||
|
Assert.AreEqual(0, keys[1]);
|
||||||
|
Assert.AreEqual(1, keys[2]);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void ValuesTest()
|
||||||
|
{
|
||||||
|
IcdOrderedDictionary<int, int> dict = new IcdOrderedDictionary<int, int>
|
||||||
|
{
|
||||||
|
{0, 0},
|
||||||
|
{1, 10},
|
||||||
|
{-1, -10}
|
||||||
|
};
|
||||||
|
|
||||||
|
int[] values = dict.Values.ToArray();
|
||||||
|
|
||||||
|
Assert.AreEqual(3, values.Length);
|
||||||
|
Assert.AreEqual(-10, values[0]);
|
||||||
|
Assert.AreEqual(0, values[1]);
|
||||||
|
Assert.AreEqual(10, values[2]);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void IndexerTest()
|
||||||
|
{
|
||||||
|
IcdOrderedDictionary<int, int> dict = new IcdOrderedDictionary<int, int>
|
||||||
|
{
|
||||||
|
{0, 0},
|
||||||
|
{1, 10},
|
||||||
|
{-1, -10}
|
||||||
|
};
|
||||||
|
|
||||||
|
Assert.AreEqual(0, dict[0]);
|
||||||
|
Assert.AreEqual(10, dict[1]);
|
||||||
|
Assert.AreEqual(-10, dict[-1]);
|
||||||
|
}
|
||||||
|
|
||||||
|
#endregion
|
||||||
|
|
||||||
|
#region Methods
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void GetEnumeratorTest()
|
||||||
|
{
|
||||||
|
Assert.Inconclusive();
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void AddTest()
|
||||||
|
{
|
||||||
|
Assert.Inconclusive();
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void ClearTest()
|
||||||
|
{
|
||||||
|
Assert.Inconclusive();
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void ContainsKeyTest()
|
||||||
|
{
|
||||||
|
Assert.Inconclusive();
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void RemoveTest()
|
||||||
|
{
|
||||||
|
Assert.Inconclusive();
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void TryGetValueTest()
|
||||||
|
{
|
||||||
|
Assert.Inconclusive();
|
||||||
|
}
|
||||||
|
|
||||||
|
#endregion
|
||||||
|
}
|
||||||
|
}
|
||||||
185
ICD.Common.Utils.Tests/Collections/PriorityQueueTest.cs
Normal file
185
ICD.Common.Utils.Tests/Collections/PriorityQueueTest.cs
Normal file
@@ -0,0 +1,185 @@
|
|||||||
|
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 EnqueueFirstTest()
|
||||||
|
{
|
||||||
|
PriorityQueue<int> queue = new PriorityQueue<int>();
|
||||||
|
|
||||||
|
queue.Enqueue(1, int.MaxValue);
|
||||||
|
queue.Enqueue(2, int.MinValue);
|
||||||
|
queue.EnqueueFirst(3);
|
||||||
|
|
||||||
|
Assert.AreEqual(3, queue.Count);
|
||||||
|
|
||||||
|
Assert.AreEqual(3, queue.Dequeue());
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void EnqueueRemoveTest()
|
||||||
|
{
|
||||||
|
PriorityQueue<int> queue = new PriorityQueue<int>();
|
||||||
|
|
||||||
|
queue.Enqueue(1);
|
||||||
|
queue.Enqueue(2);
|
||||||
|
queue.Enqueue(3);
|
||||||
|
|
||||||
|
queue.EnqueueRemove(4, i => i == 2);
|
||||||
|
|
||||||
|
Assert.AreEqual(3, queue.Count);
|
||||||
|
|
||||||
|
List<int> dequeue = new List<int>
|
||||||
|
{
|
||||||
|
queue.Dequeue(),
|
||||||
|
queue.Dequeue(),
|
||||||
|
queue.Dequeue()
|
||||||
|
};
|
||||||
|
|
||||||
|
Assert.AreEqual(1, dequeue[0]);
|
||||||
|
Assert.AreEqual(4, dequeue[1]);
|
||||||
|
Assert.AreEqual(3, dequeue[2]);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void EnqueueRemovePriorityTest()
|
||||||
|
{
|
||||||
|
Assert.Inconclusive();
|
||||||
|
}
|
||||||
|
|
||||||
|
[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
|
||||||
|
}
|
||||||
|
}
|
||||||
159
ICD.Common.Utils/Collections/IcdOrderedDictionary.cs
Normal file
159
ICD.Common.Utils/Collections/IcdOrderedDictionary.cs
Normal file
@@ -0,0 +1,159 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using ICD.Common.Utils.Extensions;
|
||||||
|
|
||||||
|
namespace ICD.Common.Utils.Collections
|
||||||
|
{
|
||||||
|
public sealed class IcdOrderedDictionary<TKey, TValue> : IDictionary<TKey, TValue>
|
||||||
|
{
|
||||||
|
private readonly List<TKey> m_OrderedKeys;
|
||||||
|
private readonly Dictionary<TKey, TValue> m_Dictionary;
|
||||||
|
private readonly IComparer<TKey> m_Comparer;
|
||||||
|
|
||||||
|
#region Properties
|
||||||
|
|
||||||
|
public int Count { get { return m_Dictionary.Count; } }
|
||||||
|
|
||||||
|
public bool IsReadOnly { get { return false; } }
|
||||||
|
|
||||||
|
public ICollection<TKey> Keys { get { return m_OrderedKeys; } }
|
||||||
|
|
||||||
|
public ICollection<TValue> Values
|
||||||
|
{
|
||||||
|
get
|
||||||
|
{
|
||||||
|
return m_OrderedKeys.Select(k => m_Dictionary[k])
|
||||||
|
.ToArray(Count);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public TValue this[TKey key]
|
||||||
|
{
|
||||||
|
get { return m_Dictionary[key]; }
|
||||||
|
set
|
||||||
|
{
|
||||||
|
// ReSharper disable once CompareNonConstrainedGenericWithNull
|
||||||
|
if (key == null)
|
||||||
|
throw new ArgumentNullException("key");
|
||||||
|
|
||||||
|
if (!ContainsKey(key))
|
||||||
|
m_OrderedKeys.AddSorted(key, m_Comparer);
|
||||||
|
|
||||||
|
m_Dictionary[key] = value;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#endregion
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Constructor.
|
||||||
|
/// </summary>
|
||||||
|
public IcdOrderedDictionary()
|
||||||
|
: this(Comparer<TKey>.Default)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Constructor.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="comparer"></param>
|
||||||
|
public IcdOrderedDictionary(IComparer<TKey> comparer)
|
||||||
|
{
|
||||||
|
if (comparer == null)
|
||||||
|
throw new ArgumentNullException("comparer");
|
||||||
|
|
||||||
|
m_Comparer = comparer;
|
||||||
|
m_OrderedKeys = new List<TKey>();
|
||||||
|
m_Dictionary = new Dictionary<TKey, TValue>();
|
||||||
|
}
|
||||||
|
|
||||||
|
#region Methods
|
||||||
|
|
||||||
|
public IEnumerator<KeyValuePair<TKey, TValue>> GetEnumerator()
|
||||||
|
{
|
||||||
|
return m_OrderedKeys.Select(k => new KeyValuePair<TKey, TValue>(k, m_Dictionary[k]))
|
||||||
|
.GetEnumerator();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Add(TKey key, TValue value)
|
||||||
|
{
|
||||||
|
// ReSharper disable once CompareNonConstrainedGenericWithNull
|
||||||
|
if (key == null)
|
||||||
|
throw new ArgumentNullException("key");
|
||||||
|
|
||||||
|
if (m_Dictionary.ContainsKey(key))
|
||||||
|
throw new ArgumentException("An item with the same key has already been added.", "key");
|
||||||
|
|
||||||
|
this[key] = value;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Clear()
|
||||||
|
{
|
||||||
|
m_OrderedKeys.Clear();
|
||||||
|
m_Dictionary.Clear();
|
||||||
|
}
|
||||||
|
|
||||||
|
public bool ContainsKey(TKey key)
|
||||||
|
{
|
||||||
|
return m_Dictionary.ContainsKey(key);
|
||||||
|
}
|
||||||
|
|
||||||
|
public bool Remove(TKey key)
|
||||||
|
{
|
||||||
|
// ReSharper disable once CompareNonConstrainedGenericWithNull
|
||||||
|
if (key == null)
|
||||||
|
throw new ArgumentNullException("key");
|
||||||
|
|
||||||
|
if (!m_Dictionary.Remove(key))
|
||||||
|
return false;
|
||||||
|
|
||||||
|
m_OrderedKeys.Remove(key);
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
public bool TryGetValue(TKey key, out TValue value)
|
||||||
|
{
|
||||||
|
return m_Dictionary.TryGetValue(key, out value);
|
||||||
|
}
|
||||||
|
|
||||||
|
#endregion
|
||||||
|
|
||||||
|
#region Private Methods
|
||||||
|
|
||||||
|
IEnumerator IEnumerable.GetEnumerator()
|
||||||
|
{
|
||||||
|
return GetEnumerator();
|
||||||
|
}
|
||||||
|
|
||||||
|
void ICollection<KeyValuePair<TKey, TValue>>.Add(KeyValuePair<TKey, TValue> item)
|
||||||
|
{
|
||||||
|
Add(item.Key, item.Value);
|
||||||
|
}
|
||||||
|
|
||||||
|
bool ICollection<KeyValuePair<TKey, TValue>>.Contains(KeyValuePair<TKey, TValue> item)
|
||||||
|
{
|
||||||
|
TValue value;
|
||||||
|
return TryGetValue(item.Key, out value) &&
|
||||||
|
EqualityComparer<TValue>.Default.Equals(value, item.Value);
|
||||||
|
}
|
||||||
|
|
||||||
|
void ICollection<KeyValuePair<TKey, TValue>>.CopyTo(KeyValuePair<TKey, TValue>[] array, int index)
|
||||||
|
{
|
||||||
|
foreach (KeyValuePair<TKey, TValue> kvp in this)
|
||||||
|
{
|
||||||
|
array.SetValue(kvp, index);
|
||||||
|
index++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
bool ICollection<KeyValuePair<TKey, TValue>>.Remove(KeyValuePair<TKey, TValue> item)
|
||||||
|
{
|
||||||
|
return (this as ICollection<KeyValuePair<TKey, TValue>>).Contains(item) && Remove(item.Key);
|
||||||
|
}
|
||||||
|
|
||||||
|
#endregion
|
||||||
|
}
|
||||||
|
}
|
||||||
244
ICD.Common.Utils/Collections/PriorityQueue.cs
Normal file
244
ICD.Common.Utils/Collections/PriorityQueue.cs
Normal file
@@ -0,0 +1,244 @@
|
|||||||
|
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>
|
||||||
|
/// Enqueues the item at the beginning of the queue.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="item"></param>
|
||||||
|
[PublicAPI]
|
||||||
|
public void EnqueueFirst(T item)
|
||||||
|
{
|
||||||
|
const int priority = int.MinValue;
|
||||||
|
|
||||||
|
if (!m_PriorityToQueue.ContainsKey(priority))
|
||||||
|
m_PriorityToQueue.Add(priority, new List<T>());
|
||||||
|
|
||||||
|
m_PriorityToQueue[priority].Insert(0, item);
|
||||||
|
m_Count++;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Removes any items in the queue matching the predicate.
|
||||||
|
/// Inserts the given item in the position of the first removed item, or at the end of the queue.
|
||||||
|
/// This is useful for reducing duplication, or replacing items with something more pertinant.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="item"></param>
|
||||||
|
/// <param name="remove"></param>
|
||||||
|
[PublicAPI]
|
||||||
|
public void EnqueueRemove(T item, Func<T, bool> remove)
|
||||||
|
{
|
||||||
|
if (remove == null)
|
||||||
|
throw new ArgumentNullException("remove");
|
||||||
|
|
||||||
|
EnqueueRemove(item, remove, int.MaxValue);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Removes any items in the queue matching the predicate.
|
||||||
|
/// Inserts the given item in the position of the first removed item, or at the end of the queue.
|
||||||
|
/// This is useful for reducing duplication, or replacing items with something more pertinant.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="item"></param>
|
||||||
|
/// <param name="remove"></param>
|
||||||
|
/// <param name="priority"></param>
|
||||||
|
[PublicAPI]
|
||||||
|
public void EnqueueRemove(T item, Func<T, bool> remove, int priority)
|
||||||
|
{
|
||||||
|
if (remove == null)
|
||||||
|
throw new ArgumentNullException("remove");
|
||||||
|
|
||||||
|
bool inserted = false;
|
||||||
|
|
||||||
|
foreach (KeyValuePair<int, List<T>> kvp in m_PriorityToQueue)
|
||||||
|
{
|
||||||
|
int[] removeIndices =
|
||||||
|
kvp.Value
|
||||||
|
.FindIndices(v => remove(v))
|
||||||
|
.Reverse()
|
||||||
|
.ToArray();
|
||||||
|
|
||||||
|
if (removeIndices.Length == 0)
|
||||||
|
continue;
|
||||||
|
|
||||||
|
foreach (int removeIndex in removeIndices)
|
||||||
|
{
|
||||||
|
kvp.Value.RemoveAt(removeIndex);
|
||||||
|
m_Count--;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!inserted)
|
||||||
|
{
|
||||||
|
int insertIndex = removeIndices[0];
|
||||||
|
kvp.Value.Insert(insertIndex, item);
|
||||||
|
m_Count++;
|
||||||
|
|
||||||
|
inserted = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!inserted)
|
||||||
|
Enqueue(item, priority);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Dequeues the first item with the lowest priority value.
|
||||||
|
/// </summary>
|
||||||
|
/// <returns></returns>
|
||||||
|
[PublicAPI]
|
||||||
|
public T Dequeue()
|
||||||
|
{
|
||||||
|
T output;
|
||||||
|
if (TryDequeue(out output))
|
||||||
|
return output;
|
||||||
|
|
||||||
|
throw new InvalidOperationException("The queue is empty.");
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Attempts to dequeue an item from the queue.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="output"></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
[PublicAPI]
|
||||||
|
public bool TryDequeue(out T output)
|
||||||
|
{
|
||||||
|
output = default(T);
|
||||||
|
|
||||||
|
KeyValuePair<int, List<T>> kvp;
|
||||||
|
if (!m_PriorityToQueue.TryFirst(out kvp))
|
||||||
|
return false;
|
||||||
|
|
||||||
|
int priority = kvp.Key;
|
||||||
|
List<T> queue = kvp.Value;
|
||||||
|
|
||||||
|
output = queue[0];
|
||||||
|
queue.RemoveAt(0);
|
||||||
|
|
||||||
|
if (queue.Count == 0)
|
||||||
|
m_PriorityToQueue.Remove(priority);
|
||||||
|
|
||||||
|
m_Count--;
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <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
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -74,6 +74,8 @@
|
|||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<Compile Include="Attributes\AbstractIcdAttribute.cs" />
|
<Compile Include="Attributes\AbstractIcdAttribute.cs" />
|
||||||
|
<Compile Include="Collections\IcdOrderedDictionary.cs" />
|
||||||
|
<Compile Include="Collections\PriorityQueue.cs" />
|
||||||
<Compile Include="Collections\WeakKeyDictionary.cs" />
|
<Compile Include="Collections\WeakKeyDictionary.cs" />
|
||||||
<Compile Include="Comparers\PredicateComparer.cs" />
|
<Compile Include="Comparers\PredicateComparer.cs" />
|
||||||
<Compile Include="ConsoleColor.cs" />
|
<Compile Include="ConsoleColor.cs" />
|
||||||
|
|||||||
Reference in New Issue
Block a user