143 lines
3.1 KiB
C#
143 lines
3.1 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Text;
|
|
|
|
namespace BitStrap
|
|
{
|
|
public static class DictionaryExtensions
|
|
{
|
|
public struct GCFreeEnumerator<K, V>
|
|
{
|
|
private Dictionary<K, V>.Enumerator enumerator;
|
|
|
|
public KeyValuePair<K, V> Current
|
|
{
|
|
get
|
|
{
|
|
return enumerator.Current;
|
|
}
|
|
}
|
|
|
|
public GCFreeEnumerator(Dictionary<K, V> collection)
|
|
{
|
|
enumerator = collection.GetEnumerator();
|
|
}
|
|
|
|
public GCFreeEnumerator<K, V> GetEnumerator()
|
|
{
|
|
return this;
|
|
}
|
|
|
|
public bool MoveNext()
|
|
{
|
|
return enumerator.MoveNext();
|
|
}
|
|
}
|
|
|
|
public static GCFreeEnumerator<K, V> Each<K, V>(this Dictionary<K, V> collection)
|
|
{
|
|
return new GCFreeEnumerator<K, V>(collection);
|
|
}
|
|
|
|
public static int Count<K, V>(this Dictionary<K, V> collection, Predicate<KeyValuePair<K, V>> predicate)
|
|
{
|
|
if (predicate == null)
|
|
{
|
|
return 0;
|
|
}
|
|
int num = 0;
|
|
Dictionary<K, V>.Enumerator enumerator = collection.GetEnumerator();
|
|
while (enumerator.MoveNext())
|
|
{
|
|
if (predicate(enumerator.Current))
|
|
{
|
|
num++;
|
|
}
|
|
}
|
|
return num;
|
|
}
|
|
|
|
public static bool All<K, V>(this Dictionary<K, V> collection, Predicate<KeyValuePair<K, V>> predicate)
|
|
{
|
|
if (predicate == null)
|
|
{
|
|
return false;
|
|
}
|
|
Dictionary<K, V>.Enumerator enumerator = collection.GetEnumerator();
|
|
while (enumerator.MoveNext())
|
|
{
|
|
if (!predicate(enumerator.Current))
|
|
{
|
|
return false;
|
|
}
|
|
}
|
|
return true;
|
|
}
|
|
|
|
public static bool Any<K, V>(this Dictionary<K, V> collection, Predicate<KeyValuePair<K, V>> predicate)
|
|
{
|
|
if (predicate == null)
|
|
{
|
|
return false;
|
|
}
|
|
Dictionary<K, V>.Enumerator enumerator = collection.GetEnumerator();
|
|
while (enumerator.MoveNext())
|
|
{
|
|
if (predicate(enumerator.Current))
|
|
{
|
|
return true;
|
|
}
|
|
}
|
|
return false;
|
|
}
|
|
|
|
public static KeyValuePair<K, V> FirstOrDefault<K, V>(this Dictionary<K, V> collection)
|
|
{
|
|
Dictionary<K, V>.Enumerator enumerator = collection.GetEnumerator();
|
|
if (enumerator.MoveNext())
|
|
{
|
|
return enumerator.Current;
|
|
}
|
|
return default(KeyValuePair<K, V>);
|
|
}
|
|
|
|
public static KeyValuePair<K, V> FirstOrDefault<K, V>(this Dictionary<K, V> collection, Predicate<KeyValuePair<K, V>> predicate)
|
|
{
|
|
Dictionary<K, V>.Enumerator enumerator = collection.GetEnumerator();
|
|
while (enumerator.MoveNext())
|
|
{
|
|
if (predicate(enumerator.Current))
|
|
{
|
|
return enumerator.Current;
|
|
}
|
|
}
|
|
return default(KeyValuePair<K, V>);
|
|
}
|
|
|
|
public static string ToStringFull<K, V>(this Dictionary<K, V> collection)
|
|
{
|
|
if (collection == null)
|
|
{
|
|
return "null";
|
|
}
|
|
if (collection.Count <= 0)
|
|
{
|
|
return "{}";
|
|
}
|
|
StringBuilder stringBuilder = new StringBuilder();
|
|
stringBuilder.Append("{ ");
|
|
Dictionary<K, V>.Enumerator enumerator = collection.GetEnumerator();
|
|
while (enumerator.MoveNext())
|
|
{
|
|
stringBuilder.Append(enumerator.Current.Key.ToString());
|
|
stringBuilder.Append("=");
|
|
stringBuilder.Append(enumerator.Current.Value.ToString());
|
|
stringBuilder.Append(", ");
|
|
}
|
|
stringBuilder.Remove(stringBuilder.Length - 2, 2);
|
|
stringBuilder.Append(" }");
|
|
return stringBuilder.ToString();
|
|
}
|
|
}
|
|
}
|