-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
Signed-off-by: Serhii A. Hrytsenko <[email protected]>
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,36 +1,32 @@ | ||
namespace HomeInventory.Core; | ||
using System.Runtime.InteropServices; | ||
|
||
namespace HomeInventory.Core; | ||
|
||
public static class DictionaryExtensions | ||
{ | ||
public static TResult GetOrAdd<TKey, TValue, TResult>(this IDictionary<TKey, TValue> dictionary, TKey key, Func<TKey, TResult> createValueFunc) | ||
where TKey : notnull | ||
where TResult : TValue => | ||
dictionary.TryGetValue(key, out var value) | ||
? (TResult)value! | ||
: dictionary.Add(key, createValueFunc); | ||
|
||
public static async ValueTask<TResult> GetOrAddAsync<TKey, TValue, TResult>(this IDictionary<TKey, TValue> dictionary, TKey key, Func<TKey, Task<TResult>> createValueFunc) | ||
where TKey : notnull | ||
where TResult : TValue => | ||
dictionary.TryGetValue(key, out var value) | ||
? (TResult)value! | ||
: await dictionary.AddAsync(key, createValueFunc); | ||
|
||
private static TResult Add<TKey, TValue, TResult>(this IDictionary<TKey, TValue> dictionary, TKey key, Func<TKey, TResult> createValueFunc) | ||
public static TResult GetOrAdd<TKey, TValue, TResult>(this Dictionary<TKey, TValue> dictionary, TKey key, Func<TKey, TResult> createValueFunc) | ||
where TKey : notnull | ||
where TResult : TValue | ||
{ | ||
var newValue = createValueFunc(key); | ||
dictionary.Add(key, newValue); | ||
return newValue; | ||
ref var val = ref CollectionsMarshal.GetValueRefOrAddDefault(dictionary, key, out var exists); | ||
if (!exists) | ||
{ | ||
val = createValueFunc(key); | ||
} | ||
|
||
return (TResult)val!; | ||
} | ||
|
||
private static async Task<TResult> AddAsync<TKey, TValue, TResult>(this IDictionary<TKey, TValue> dictionary, TKey key, Func<TKey, Task<TResult>> createValueFunc) | ||
public static async ValueTask<TResult> GetOrAddAsync<TKey, TValue, TResult>(this Dictionary<TKey, TValue> dictionary, TKey key, Func<TKey, Task<TResult>> createValueFunc) | ||
where TKey : notnull | ||
where TResult : TValue | ||
{ | ||
var newValue = await createValueFunc(key); | ||
dictionary.Add(key, newValue); | ||
return newValue; | ||
ref var val = ref CollectionsMarshal.GetValueRefOrAddDefault(dictionary, key, out var exists); | ||
if (!exists) | ||
{ | ||
val = await createValueFunc(key); | ||
Check failure on line 27 in src/HomeInventory/HomeInventory.Core/DictionaryExtensions.cs GitHub Actions / Build .NET
Check failure on line 27 in src/HomeInventory/HomeInventory.Core/DictionaryExtensions.cs GitHub Actions / Build .NET
Check failure on line 27 in src/HomeInventory/HomeInventory.Core/DictionaryExtensions.cs GitHub Actions / Build .NET
Check failure on line 27 in src/HomeInventory/HomeInventory.Core/DictionaryExtensions.cs GitHub Actions / Build .NET
Check failure on line 27 in src/HomeInventory/HomeInventory.Core/DictionaryExtensions.cs GitHub Actions / Analyze (csharp)
Check failure on line 27 in src/HomeInventory/HomeInventory.Core/DictionaryExtensions.cs GitHub Actions / Analyze (csharp)
Check failure on line 27 in src/HomeInventory/HomeInventory.Core/DictionaryExtensions.cs GitHub Actions / Analyze (csharp)
|
||
} | ||
|
||
return (TResult)val!; | ||
Check failure on line 30 in src/HomeInventory/HomeInventory.Core/DictionaryExtensions.cs GitHub Actions / Build .NET
Check failure on line 30 in src/HomeInventory/HomeInventory.Core/DictionaryExtensions.cs GitHub Actions / Build .NET
Check failure on line 30 in src/HomeInventory/HomeInventory.Core/DictionaryExtensions.cs GitHub Actions / Build .NET
Check failure on line 30 in src/HomeInventory/HomeInventory.Core/DictionaryExtensions.cs GitHub Actions / Build .NET
Check failure on line 30 in src/HomeInventory/HomeInventory.Core/DictionaryExtensions.cs GitHub Actions / Analyze (csharp)
Check failure on line 30 in src/HomeInventory/HomeInventory.Core/DictionaryExtensions.cs GitHub Actions / Analyze (csharp)
Check failure on line 30 in src/HomeInventory/HomeInventory.Core/DictionaryExtensions.cs GitHub Actions / Analyze (csharp)
|
||
} | ||
} |