C
C#15mo ago
__dil__

❔ Entry API for dictionaries?

Rust has this nifty API called Entry which allows one to write code such as:
map.entry(c)
.and_modify(|count_of_c| { *count_of_c += 1 })
.or_insert(1);
map.entry(c)
.and_modify(|count_of_c| { *count_of_c += 1 })
.or_insert(1);
So, you can modify a value in a hashmap (dictionary) if the key exists, or insert a new entry if the key does not exist. Is there something similar in C#?
19 Replies
mtreit
mtreit15mo ago
I don't think so.
__dil__
__dil__OP15mo ago
What would the equivalent idiomatic C# code look like?
mtreit
mtreit15mo ago
If I understand the code correctly, something like this?
if (!dict.TryGetValue(key, out var val))
{
dict.Add(key, 0);
}

dict[key] += 1;
if (!dict.TryGetValue(key, out var val))
{
dict.Add(key, 0);
}

dict[key] += 1;
Actually since the value isn't used, something like this:
if (!dict.ContainsKey(key))
{
dict.Add(key, 0);
}

dict[key] += 1;
if (!dict.ContainsKey(key))
{
dict.Add(key, 0);
}

dict[key] += 1;
Pobiega
Pobiega15mo ago
I very often end up with extension methods like IncrementOrInsert for numeric dictionaries
Sossenbinder
Sossenbinder15mo ago
I think there's only a native AddOrUpdate for ConcurrentDictionaries But not for regular Dictionaries IIRC The closest thing I have in mind would be a TryAdd to have contains & add in one statement, but it does not allow modification of an existing entry
mtreit
mtreit15mo ago
Just out of curiosity I tried both: As usual, the penalty for using the thread-safe version is pretty high.
No description
Sossenbinder
Sossenbinder15mo ago
I wonder why there is no out of the box AddOrUpdate for the regular Dictionary, it's a welcome QoL addition and I think everyone would be aware that it is not an atomic threadsafe operation catthinking
mtreit
mtreit15mo ago
GitHub
API Proposal: Add AddOrUpdate to Dictionary · Issue #26848 · dotnet...
Add the AddOrUpdate method that is on ConcurrentDictionary to Dictionary. This would improve performance in use cases such as counting keys. K-Nucleotide in the benchmarksgame does this and has an ...
mtreit
mtreit15mo ago
This issue talks about DictionarySlim. I've never heard of that.
Sossenbinder
Sossenbinder15mo ago
Me neither
mtreit
mtreit15mo ago
Doesn't seem like anything that ever actually shipped
Sossenbinder
Sossenbinder15mo ago
Can't find anything about that on source dot net as well
viktor11k
viktor11k15mo ago
GitHub
corefxlab/src/Microsoft.Experimental.Collections/Microsoft/Collecti...
This repo is for experimentation and exploring new ideas that may or may not make it into the main corefx repo. - dotnet/corefxlab
mtreit
mtreit15mo ago
Since it's archived, feels like it didn't pan out.
reflectronic
reflectronic15mo ago
reflectronic
reflectronic15mo ago
which you can use to efficiently implement an AddOrUpdate method yourself it is "unsafe" so you do need to be mindful when using it
reflectronic
reflectronic15mo ago
there are some related issues https://github.com/dotnet/runtime/issues/15059 and there seems to be little appetite to introduce convenience methods for these cases
GitHub
Propose Adding a GetOrAdd(TKey key, Func valueFactory) to Dictionar...
On ConcurrentDictionary<TKey, TValue> there is the very useful function TValue GetOrAdd(TKey key, Func<TKey, TValie> valueFactory). The pattern that the method represents is one that is...
Sossenbinder
Sossenbinder15mo ago
Ohh right I remember that CollectionsMarshal did receive that, indeed Thanks for reminding me too, I completely forgot
Accord
Accord15mo ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server