Files
Common-VS2019/SacsStandardBase/Base/Core/SacsQueueDictionary.cs
T
2022-02-16 11:18:53 +00:00

116 lines
3.3 KiB
C#

using Sarand.Net.Standard.Base.App;
using System;
using System.Collections.Generic;
using System.Text;
namespace Sarand.Net.Standard.Base.Core
{
//=======================================================================
/// <summary>
/// Dictionary with items queued as if in FIFO list.
/// </summary>
/// <typeparam name="K">Type of key</typeparam>
/// <typeparam name="V">Type of value</typeparam>
public class SacsQueueDictionary<K, V>
{
//-------------------------------------------------------------------
// Data memners.
private readonly Queue<K> queue = new Queue<K>();
private readonly Dictionary<K, V> dictionary = new Dictionary<K, V>();
//-------------------------------------------------------------------
// Properties.
public int Count { get { lock (dictionary) return queue.Count; } }
//-------------------------------------------------------------------
/// <summary>
/// Is the KLey defined in the queue dictionary.
/// </summary>
/// <param name="key"></param>
/// <returns>True if found, else false.</returns>
public bool HasValue(K key)
{
lock (dictionary)
{
return dictionary.ContainsKey(key);
}
}
//-------------------------------------------------------------------
/// <summary>
/// Retrieve the value determined by the key.
/// </summary>
/// <param name="key"></param>
/// <returns>Corresponding value if present, else default value.</returns>
public V GetValue(K key)
{
V value = default(V);
lock (dictionary)
{
dictionary.TryGetValue(key, out value);
}
return value;
}
//-------------------------------------------------------------------
public bool SetEntry(K key, V value)
{
if (key == null || value == null) return false;
lock (dictionary)
{
if (dictionary.ContainsKey(key))
{
// Existsing key.
// Replace value in dictionary.
dictionary[key] = value;
return true;
}
// New Key.
// Add to queue and set value in dictionary.
queue.Enqueue(key);
dictionary[key] = value;
return true;
}
if (SacsSystem.DebugMode) Validate();
}
//-------------------------------------------------------------------
/// <summary>
/// Remove the top n entries from the queue dictionary.
/// </summary>
/// <param name="maxEntries"></param>
/// <returns></returns>
public List<KeyValuePair<K, V>> RemoveTopEntries(int maxEntries)
{
List<KeyValuePair<K, V>> entries = new List<KeyValuePair<K, V>>();
lock (dictionary)
{
while (queue.Count > 0 && entries.Count < maxEntries)
{
K key = queue.Dequeue();
V value = dictionary[key];
entries.Add(new KeyValuePair<K, V>(key, value));
dictionary.Remove(key);
}
}
if (SacsSystem.DebugMode) Validate();
return entries;
}
//-------------------------------------------------------------------
// Internal utilities.
//-------------------------------------------------------------------
private void Validate()
{
lock (dictionary)
{
if (queue.Count != dictionary.Count)
throw new NotSupportedException("SacsQueueDictionary.Validate Inconsistent entry counts. Quete: " + queue.Count + ". Dictionary: " + dictionary.Count);
}
}
//-------------------------------------------------------------------
}
//=======================================================================
}