Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix InlineDictionary Behavior for Single Item Reassignment #17370

Merged
merged 6 commits into from
Oct 31, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion src/Avalonia.Base/Utilities/SmallDictionary.cs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,15 @@ void SetCore(TKey key, TValue value, bool overwrite)
}
else
{
// We have a single element, upgrade to array
// We have a single element, check if we should update the value.
if((TKey)_data == key && overwrite)
lindexi marked this conversation as resolved.
Show resolved Hide resolved
{
_data = key;
lindexi marked this conversation as resolved.
Show resolved Hide resolved
_value = value;

return;
}
// If we do not replace it, upgrade to array.
arr = new KeyValuePair[6];
arr[0] = new KeyValuePair((TKey)_data, _value);
arr[1] = new KeyValuePair(key, value);
Expand Down
11 changes: 11 additions & 0 deletions tests/Avalonia.Base.UnitTests/Utilities/InlineDictionaryTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,17 @@ namespace Avalonia.Base.UnitTests.Utilities;

public class InlineDictionaryTests
{
[Fact]
public void Set_Twice_With_Internal_Array_Works()
lindexi marked this conversation as resolved.
Show resolved Hide resolved
{
var dic = new InlineDictionary<string, int>();
dic["foo"] = 1;
Assert.Equal(1, dic["foo"]);

dic["foo"] = 2;
Assert.Equal(2, dic["foo"]);
}

[Fact]
public void Enumeration_After_Add_With_Internal_Array_Works()
{
Expand Down
Loading