Add project files.
This commit is contained in:
@@ -0,0 +1,80 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
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 IDictionary<TKey, TValue> dictionary, TKey key, TValue 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.Add(x.Key, x.Value));
|
||||
}
|
||||
|
||||
//-------------------------------------------------------------------
|
||||
}
|
||||
//=======================================================================
|
||||
}
|
||||
Reference in New Issue
Block a user