Last active
April 19, 2017 02:03
-
-
Save pmunin/28d0ba1acce677736c5e to your computer and use it in GitHub Desktop.
Dictionary Extensions - GetOrAdd, AddOrUpdate
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// Latest version is here: https://gist.github.com/pmunin/28d0ba1acce677736c5e | |
using System; | |
using System.Collections; | |
using System.Collections.Concurrent; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Text; | |
using System.Threading.Tasks; | |
namespace DictionaryUtils | |
{ | |
public static partial class DictionaryAddOrUpdateExtensions | |
{ | |
static TValue AddOrUpdate<TKey, TValue>(IDictionary<TKey, TValue> dict, TKey key, TValue addValue, Func<TKey, TValue, TValue> updateValueFactoryExt = null) | |
{ | |
if (updateValueFactoryExt == null) | |
updateValueFactoryExt = (k, v) => addValue; | |
if (dict is ConcurrentDictionary<TKey, TValue> concDict) | |
return concDict.AddOrUpdate(key, addValue, updateValueFactoryExt); | |
if (dict.TryGetValue(key, out var res)) | |
dict[key] = res = updateValueFactoryExt(key, res); | |
else | |
dict[key] = res = addValue; | |
return res; | |
} | |
static TValue AddOrUpdate<TKey, TValue>(IDictionary<TKey, TValue> dict, TKey key, Func<TKey, TValue> addValueFactory, Func<TKey, TValue, TValue> updateValueFactoryExt = null) | |
{ | |
if (updateValueFactoryExt == null) | |
updateValueFactoryExt = (k, v) => addValueFactory(k); | |
if (dict is ConcurrentDictionary<TKey, TValue> concDict) | |
return concDict.AddOrUpdate(key, addValueFactory, updateValueFactoryExt); | |
if (dict.TryGetValue(key, out var res)) | |
dict[key] = res = updateValueFactoryExt(key, res); | |
else | |
dict[key] = res = addValueFactory(key); | |
return res; | |
} | |
public static TValue AddOrUpdate<TKey, TValue, TResult>(this IDictionary<TKey, TValue> dict, TKey key, Func<TKey, TResult> addValueFactory, Func<TKey, TResult, TResult> updateValueFactoryExt) | |
where TResult : TValue | |
{ | |
return AddOrUpdate<TKey, TValue>(dict, key, k => addValueFactory(k), (k, oldVal) => updateValueFactoryExt(k, (TResult)oldVal)); | |
} | |
public static TValue AddOrUpdate<TKey, TValue, TResult>(this IDictionary<TKey, TValue> dict, TKey key, TResult addValue, Func<TKey, TResult, TResult> updateValueFactoryExt) | |
where TResult : TValue | |
{ | |
return AddOrUpdate<TKey, TValue>(dict, key, addValue, (k, oldVal) => updateValueFactoryExt(k, (TResult)oldVal)); | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// Latest version is here: https://gist.github.com/pmunin/28d0ba1acce677736c5e | |
using System; | |
using System.Collections; | |
using System.Collections.Concurrent; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Text; | |
using System.Threading.Tasks; | |
namespace DictionaryUtils | |
{ | |
public static partial class DictionaryGetOrAddExtensions | |
{ | |
static TValue GetOrAdd<TKey, TValue>(IDictionary<TKey, TValue> dictionary, TKey key, Func<TKey, TValue> createValueIfNotExist) | |
{ | |
if(dictionary is ConcurrentDictionary<TKey,TValue> concurrentDictionary) | |
return concurrentDictionary.GetOrAdd(key, valueFactory: createValueIfNotExist); | |
if (!dictionary.TryGetValue(key, out var res)) | |
dictionary.Add(key, res = createValueIfNotExist(key)); | |
return res; | |
//System.Collections.Concurrent.ConcurrentDictionary<,> | |
} | |
public static TResult GetOrAdd<TKey, TValue, TResult>(this IDictionary<TKey, TValue> dictionary, TKey key, Func<TKey, TResult> createResultIfNotExistAs) | |
where TResult:TValue | |
{ | |
var res = GetOrAdd<TKey,TValue>(dictionary, key, createValueIfNotExist:k=>createResultIfNotExistAs(k)); | |
return (TResult)res; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment