Skip to content

Commit

Permalink
style: cleanup DictionaryExtensions
Browse files Browse the repository at this point in the history
* Explicit type is used where type is not evident.
* Conditions are inlined with a ternary and the return value is used directly.
* System.Runtime.InteropServices is only imported for .NET >= 6.0
  • Loading branch information
oliverbooth committed Apr 1, 2023
1 parent b794352 commit d29663f
Showing 1 changed file with 14 additions and 30 deletions.
44 changes: 14 additions & 30 deletions X10D/src/Collections/DictionaryExtensions.cs
@@ -1,5 +1,7 @@
using System.Diagnostics.Contracts;
#if NET6_0_OR_GREATER
using System.Runtime.InteropServices;
#endif
using System.Web;

namespace X10D.Collections;
Expand Down Expand Up @@ -65,7 +67,7 @@ public static class DictionaryExtensions
#else
if (dictionary.TryGetValue(key, out TValue? old))
{
var updated = updateValueFactory(key, old);
TValue updated = updateValueFactory(key, old);
dictionary[key] = updated;

return updated;
Expand Down Expand Up @@ -119,7 +121,7 @@ public static class DictionaryExtensions

if (dictionary.TryGetValue(key, out TValue? old))
{
var updated = updateValueFactory(key, old);
TValue updated = updateValueFactory(key, old);
dictionary[key] = updated;

return updated;
Expand Down Expand Up @@ -180,26 +182,17 @@ public static class DictionaryExtensions

#if NET6_0_OR_GREATER
ref TValue? value = ref CollectionsMarshal.GetValueRefOrAddDefault(dictionary, key, out bool exists);
if (exists)
{
value = updateValueFactory(key, value!);
}
else
{
value = addValueFactory(key);
}

return value;
return exists ? updateValueFactory(key, value!) : addValueFactory(key);
#else
if (dictionary.TryGetValue(key, out TValue? old))
{
var updated = updateValueFactory(key, old);
TValue updated = updateValueFactory(key, old);
dictionary[key] = updated;

return updated;
}

var add = addValueFactory(key);
TValue add = addValueFactory(key);
dictionary.Add(key, add);

return add;
Expand Down Expand Up @@ -257,13 +250,13 @@ public static class DictionaryExtensions

if (dictionary.TryGetValue(key, out TValue? old))
{
var updated = updateValueFactory(key, old);
TValue updated = updateValueFactory(key, old);
dictionary[key] = updated;

return updated;
}

var add = addValueFactory(key);
TValue add = addValueFactory(key);
dictionary.Add(key, add);

return add;
Expand Down Expand Up @@ -326,26 +319,17 @@ public static class DictionaryExtensions

#if NET6_0_OR_GREATER
ref TValue? value = ref CollectionsMarshal.GetValueRefOrAddDefault(dictionary, key, out bool exists);
if (exists)
{
value = updateValueFactory(key, value!, factoryArgument);
}
else
{
value = addValueFactory(key, factoryArgument);
}

return value;
return exists ? updateValueFactory(key, value!, factoryArgument) : addValueFactory(key, factoryArgument);
#else
if (dictionary.TryGetValue(key, out TValue? old))
{
var updated = updateValueFactory(key, old, factoryArgument);
TValue updated = updateValueFactory(key, old, factoryArgument);
dictionary[key] = updated;

return updated;
}

var add = addValueFactory(key, factoryArgument);
TValue add = addValueFactory(key, factoryArgument);
dictionary.Add(key, add);

return add;
Expand Down Expand Up @@ -409,13 +393,13 @@ public static class DictionaryExtensions

if (dictionary.TryGetValue(key, out TValue? old))
{
var updated = updateValueFactory(key, old, factoryArgument);
TValue updated = updateValueFactory(key, old, factoryArgument);
dictionary[key] = updated;

return updated;
}

var add = addValueFactory(key, factoryArgument);
TValue add = addValueFactory(key, factoryArgument);
dictionary.Add(key, add);

return add;
Expand Down

0 comments on commit d29663f

Please sign in to comment.