diff --git a/Collections.meta b/Collections.meta new file mode 100644 index 0000000..d087cf9 --- /dev/null +++ b/Collections.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: 9b25b74f42726e94585fe6ae4b9dd947 +folderAsset: yes +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Collections/DictionaryExtensions.cs b/Collections/DictionaryExtensions.cs new file mode 100644 index 0000000..aae8985 --- /dev/null +++ b/Collections/DictionaryExtensions.cs @@ -0,0 +1,41 @@ +using System.Collections.Generic; + +namespace NEG.Utils.Collections +{ + public static class DictionaryExtensions + { + /// + /// Adds given value to a dictionary if there was no element at given , replaces element with otherwise. + /// + /// true if element was added, false if it was replaced + public static bool AddOrUpdate(this Dictionary dict, K key, V value) + { + if (dict.ContainsKey(key)) + { + dict[key] = value; + return false; + } + else + { + dict.Add(key, value); + return true; + } + } + + + /// + /// Gets a value from the dictionary under a specified key or adds it if did not exist and returns . + /// + /// value under a given if it exists, otherwise + public static V GetOrSetToDefault(this Dictionary dict, K key, V defaultValue) + { + if (dict.TryGetValue(key, out V value)) + { + return value; + } + dict.Add(key, defaultValue); + + return defaultValue; + } + } +} diff --git a/Collections/DictionaryExtensions.cs.meta b/Collections/DictionaryExtensions.cs.meta new file mode 100644 index 0000000..d6cb872 --- /dev/null +++ b/Collections/DictionaryExtensions.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: a2c99d7a9a2f8184f8b8e94c01723ec6 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: