Skip to content

Instantly share code, notes, and snippets.

@NikitaChizhov
Created October 31, 2022 13:21
Show Gist options
  • Save NikitaChizhov/0e9cb6277ace5e4cfa4dc385d1a7d568 to your computer and use it in GitHub Desktop.
Save NikitaChizhov/0e9cb6277ace5e4cfa4dc385d1a7d568 to your computer and use it in GitHub Desktop.
public static class Extensions
{
public static TValue GetOrNew<TKey, TValue>(this Dictionary<TKey, TValue> dict, TKey key)
where TKey : notnull
where TValue : new()
{
if (!dict.TryGetValue(key, out var value))
{
dict[key] = value = new();
}
return value;
}
public static void AddOrUpdateDot<TActorId>(this List<Dot<TActorId>> dots, TActorId actorId, int newVersion)
where TActorId : IEquatable<TActorId>
{
var index = -1;
for (var i = 0; i < dots.Count; ++i)
{
if (dots[i].Actor.Equals(actorId))
{
index = i;
break;
}
}
if (index < 0)
{
dots.Add(new Dot<TActorId>(actorId, newVersion));
}
else
{
dots[index] = new Dot<TActorId>(actorId, newVersion);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment