Files
Common-VS2019/SacsStandardBase/Base/Utils/DictionaryUtil.cs
T
2024-11-19 09:48:13 +00:00

129 lines
5.2 KiB
C#

using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Linq;
namespace Sarand.Net.Standard.Base.Utils
{
//=======================================================================
public static class DictionaryUtil
{
//-------------------------------------------------------------------
// C o n t a i n s K e y s
//-------------------------------------------------------------------
public static bool ContainsKeys<TKey, TValue>(this Dictionary<TKey, TValue> dict, IEnumerable<TKey> keys)
{
bool result = false;
keys.ForEachOrBreak((x) => { result = dict.ContainsKey(x); return result; });
return result;
}
//-------------------------------------------------------------------
// G e n e r i c G e t W i t h D e f a u l t
//-------------------------------------------------------------------
/// <summary>Safely get value from Dictionary with default if not defined.</summary>
public static TValue GetValue<TKey, TValue>(this Dictionary<TKey, TValue> dictionary, TKey key, TValue defaultValue)
{
return GetValue(dictionary as IDictionary<TKey, TValue>, key, defaultValue);
}
/// <summary>Safely get value from Dictionary with default if not defined.</summary>
public static TValue GetValue<TKey, TValue>(this IDictionary<TKey, TValue> dictionary, TKey key, TValue defaultValue)
{
if (key == null) return defaultValue;
TValue value;
return dictionary.TryGetValue(key, out value) ? value : defaultValue;
}
//-------------------------------------------------------------------
// G e t V a l u e s
//-------------------------------------------------------------------
public static List<TValue> GetValues<TKey, TValue>(this Dictionary<TKey, TValue> dict)
{
return dict.Select(item => item.Value).ToList();
}
//-------------------------------------------------------------------
// S t r i n g - I n t U t i l i t i e s
//-------------------------------------------------------------------
/// <summary>Increment the count against the specified key by the supplied value, creating the key's entry if needed.</summary>
public static void Increment(this IDictionary<String, int> dictionary, String key, int increment = 1)
{
if (!dictionary.ContainsKey(key))
dictionary[key] = increment;
else
dictionary[key] += increment;
}
//-------------------------------------------------------------------
// A d d T o L i s t V a l u e
//-------------------------------------------------------------------
/// <summary>Add key:object to dictionary where each value is a list of objects.</summary>
public static void Add<TKey, TValue>(this Dictionary<TKey, List<TValue>> dict, TKey key, TValue value)
{
if (!dict.ContainsKey(key))
dict[key] = new List<TValue>();
dict[key].Add(value);
}
/// <summary>Add key:objects to dictionary where each value is a list of objects.</summary>
public static void Add<TKey, TValue>(this Dictionary<TKey, List<TValue>> dict, TKey key, IEnumerable<TValue> values)
{
if (!dict.ContainsKey(key))
dict[key] = new List<TValue>();
dict[key].AddRange(values);
}
//-------------------------------------------------------------------
// A d d R a n g e
//-------------------------------------------------------------------
/// <summary>Add entries from another dictionary.</summary>
public static void AddRange<TKey, TValue>(this Dictionary<TKey, TValue> dict, Dictionary<TKey, TValue> dictToAdd)
{
dictToAdd.ForEach(x => dict[x.Key] = x.Value);
}
//-------------------------------------------------------------------
// Removing
//-------------------------------------------------------------------
/// <summary>
/// Remove items from a dictionary that comply with the specified predicate.
/// <para>- Ref. https://stackoverflow.com/questions/469202/best-way-to-remove-multiple-items-matching-a-predicate-from-a-net-dictionary</para>
/// </summary>
public static void RemoveAll<TKey, TValue>(this Dictionary<TKey, TValue> source,
Predicate<KeyValuePair<TKey, TValue>> predicate)
{
foreach (var pair in source)
if (predicate(pair))
source.Remove(pair.Key);
}
//-------------------------------------------------------------------
// Orderd Dictionary
//-------------------------------------------------------------------
public static DictionaryEntry GetElementAt(this OrderedDictionary dictionary, int index)
{
return dictionary.Cast<DictionaryEntry>().ElementAt(index);
}
public static K GetKeyAt<K>(this OrderedDictionary dictionary, int index)
{
return (K)dictionary.GetElementAt(index).Key;
}
public static V GetValue<K, V>(this OrderedDictionary dictionary, K key)
{
if (dictionary == null || key == null)
return default(V);
try
{
return (V)dictionary.Cast<DictionaryEntry>().AsQueryable().Single(kvp => ((K)kvp.Key).Equals(key)).Value;
}
catch (Exception)
{
return default(V);
}
}
//-------------------------------------------------------------------
}
//=======================================================================
}