using System; using System.Collections.Generic; namespace Wabbajack { public static class DictionaryExt { public static V TryCreate(this IDictionary dict, K key) where K : notnull where V : new() { return dict.TryCreate(key, () => new V()); } public static V TryCreate(this IDictionary dict, K key, Func create) where K : notnull { if (dict.TryGetValue(key, out var val)) return val; var ret = create(); dict[key] = ret; return ret; } /// /// Adds the given values to the dictionary. If a key already exists, it will throw an exception /// public static void Add(this IDictionary dict, IEnumerable> vals) where K : notnull { foreach (var val in vals) { dict.Add(val); } } /// /// Adds the given values to the dictionary. If a key already exists, it will be replaced /// public static void Set(this IDictionary dict, IEnumerable> vals) where K : notnull { foreach (var val in vals) { dict[val.Key] = val.Value; } } /// /// Clears the dictionary and adds the given values /// public static void SetTo(this IDictionary dict, IEnumerable> vals) where K : notnull { dict.Clear(); dict.Set(vals); } } }