From 38b1f648cee24aa38a763e08fba6dfa69ffcd7c8 Mon Sep 17 00:00:00 2001 From: Chomp Date: Wed, 16 Jul 2025 10:13:33 +0100 Subject: [PATCH] Added dictionary extensions --- .../Extensions/DictionaryExtensions.cs | 37 +++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 Libraries/SPTarkov.Server.Core/Extensions/DictionaryExtensions.cs diff --git a/Libraries/SPTarkov.Server.Core/Extensions/DictionaryExtensions.cs b/Libraries/SPTarkov.Server.Core/Extensions/DictionaryExtensions.cs new file mode 100644 index 00000000..5c639e5a --- /dev/null +++ b/Libraries/SPTarkov.Server.Core/Extensions/DictionaryExtensions.cs @@ -0,0 +1,37 @@ +namespace SPTarkov.Server.Core.Extensions +{ + public static class DictionaryExtensions + { + /// + /// Add a value by key to a dictionary, if the key doesn't exist, create it + /// + /// Dictionary key type + /// Dictionary to add/update + /// Key to update by + /// Value to add to key + public static void AddOrUpdate(this IDictionary dict, T key, double value) + where T : notnull + { + if (!dict.TryAdd(key, value)) + { + dict[key] += value; + } + } + + /// + /// Add a value by key to a dictionary, if the key doesn't exist, create it + /// + /// Dictionary key type + /// Dictionary to add/update + /// Key to update by + /// Value to add to key + public static void AddOrUpdate(this IDictionary dict, T key, int value) + where T : notnull + { + if (!dict.TryAdd(key, value)) + { + dict[key] += value; + } + } + } +}