From 11cbaac33df93e256863bccc35fb720345c7a032 Mon Sep 17 00:00:00 2001 From: LubieKakao1212 Date: Fri, 16 Dec 2022 21:29:30 +0100 Subject: [PATCH 1/2] dictionary extensions --- Collections.meta | 8 +++++ Collections/DictionaryExtensions.cs | 41 ++++++++++++++++++++++++ Collections/DictionaryExtensions.cs.meta | 11 +++++++ 3 files changed, 60 insertions(+) create mode 100644 Collections.meta create mode 100644 Collections/DictionaryExtensions.cs create mode 100644 Collections/DictionaryExtensions.cs.meta 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..f848b45 --- /dev/null +++ b/Collections/DictionaryExtensions.cs @@ -0,0 +1,41 @@ +using System.Collections.Generic; + +namespace NEG.Utils.Collections +{ + public static class DictionaryExtensions + { + /// + /// Adds an element to a dictionary if there was none assigned to specified , otherwise replaces the existing + /// + /// 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 sets it if there was no assoiation then return the associated value + /// + /// + 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: From 90caf11c6c23d7d4f1ff18ba04e76668b31a117e Mon Sep 17 00:00:00 2001 From: LubieKakao1212 Date: Mon, 19 Dec 2022 13:54:07 +0000 Subject: [PATCH 2/2] Applied suggestions --- Collections/DictionaryExtensions.cs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Collections/DictionaryExtensions.cs b/Collections/DictionaryExtensions.cs index f848b45..aae8985 100644 --- a/Collections/DictionaryExtensions.cs +++ b/Collections/DictionaryExtensions.cs @@ -5,7 +5,7 @@ namespace NEG.Utils.Collections public static class DictionaryExtensions { /// - /// Adds an element to a dictionary if there was none assigned to specified , otherwise replaces the existing + /// 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) @@ -24,9 +24,9 @@ namespace NEG.Utils.Collections /// - /// Gets a value from the dictionary under a specified key or sets it if there was no assoiation then return the associated value + /// 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))