147 lines
3.4 KiB
C#
147 lines
3.4 KiB
C#
using System.Collections.Generic;
|
|
using JD.Kernel.StaticHelpers;
|
|
|
|
namespace JD.Collections
|
|
{
|
|
/// <summary>
|
|
/// Counts the occurrences of key.
|
|
/// </summary>
|
|
public class OccurencesCounter<TKey>
|
|
{
|
|
#region Variables
|
|
|
|
private readonly Dictionary<TKey, int> m_counts = new Dictionary<TKey, int>();
|
|
|
|
#endregion
|
|
|
|
#region Properties
|
|
|
|
public ICollection<TKey> Keys
|
|
{
|
|
get { return m_counts.Keys; }
|
|
}
|
|
|
|
/// <summary>
|
|
/// Enumerates all the items (in key) and the count of occurrences (the value).
|
|
/// </summary>
|
|
public ICollection<KeyValuePair<TKey, int>> Occurences
|
|
{
|
|
get { return m_counts; }
|
|
}
|
|
|
|
public bool IsEmpty
|
|
{
|
|
get { return m_counts.Count == 0; }
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region Methods
|
|
|
|
public int OccurenceOf(TKey key)
|
|
{
|
|
Throw.IfNull(key, "key");
|
|
|
|
int count;
|
|
m_counts.TryGetValue(key, out count);
|
|
return count;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Increments the occurence for the given <paramref name="key"/>. Returns 1 if it's the first time the key is referenced.
|
|
/// </summary>
|
|
/// <param name="key"></param>
|
|
/// <returns></returns>
|
|
public int Increment(TKey key)
|
|
{
|
|
Throw.IfNull(key, "key");
|
|
|
|
return Increment(key, 1);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Increments the occurence for the given <paramref name="key"/>.
|
|
/// </summary>
|
|
public int Increment(TKey key, int increment)
|
|
{
|
|
Throw.IfNull(key, "key");
|
|
|
|
int count;
|
|
if (!m_counts.TryGetValue(key, out count))
|
|
{
|
|
count = 0;
|
|
}
|
|
|
|
count += increment;
|
|
SetValue(key, count);
|
|
|
|
return count;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Decrements the occurence for the given <paramref name="key"/>. Returns 0 if it's the last time the key is referenced.
|
|
/// </summary>
|
|
/// <param name="key"></param>
|
|
/// <returns></returns>
|
|
public int Decrement(TKey key)
|
|
{
|
|
Throw.IfNull(key, "key");
|
|
|
|
int count;
|
|
if (!m_counts.TryGetValue(key, out count))
|
|
{
|
|
count = 0;
|
|
}
|
|
|
|
count--;
|
|
SetValue(key, count);
|
|
|
|
return count;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Whether a key in the given collection has occurred at least one time.
|
|
/// </summary>
|
|
/// <param name="keys"></param>
|
|
/// <returns></returns>
|
|
public bool OccurenceOfAny(params TKey[] keys)
|
|
{
|
|
Throw.IfNull(keys, "keys");
|
|
|
|
foreach (TKey currentKey in keys)
|
|
{
|
|
if (OccurenceOf(currentKey) > 0)
|
|
{
|
|
return true;
|
|
}
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Clear.
|
|
/// </summary>
|
|
public void Clear()
|
|
{
|
|
m_counts.Clear();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Sets the occurence count directly.
|
|
/// </summary>
|
|
public void SetValue(TKey key, int count)
|
|
{
|
|
if (count == 0)
|
|
{
|
|
m_counts.Remove(key);
|
|
}
|
|
else
|
|
{
|
|
m_counts[key] = count;
|
|
}
|
|
}
|
|
|
|
#endregion
|
|
}
|
|
}
|