csharp 字典扩展 - GetOrAdd,AddOrUpdate
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了csharp 字典扩展 - GetOrAdd,AddOrUpdate相关的知识,希望对你有一定的参考价值。
/// 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;
}
}
}
/// 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));
}
}
}
以上是关于csharp 字典扩展 - GetOrAdd,AddOrUpdate的主要内容,如果未能解决你的问题,请参考以下文章
dotnet ConcurrentDictionary 的 GetOrAdd 性能比 TryGetValue 加 TryAdd 低
为啥 ConcurrentDictionary.GetOrAdd(key, valueFactory) 允许 valueFactory 被调用两次?