From 3573c3f92e3d1f39069c23352a969a63189abbe4 Mon Sep 17 00:00:00 2001 From: Claude Date: Sat, 26 Sep 2026 16:25:08 +0000 Subject: [PATCH] fix: keep the stored key when ContiguousMap's indexer updates a value [patch] The indexer setter rebuilt the entry from the caller's key, so with a normalising comparer an update through map["APPLE"] rewrote the key that Keys, enumeration and AsSpan() report, while keyToIndex still held the original. Keep the stored key and replace only the value, as Dictionary, InsertionOrderMap and OrderedMap do. Fixes #69 Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01YXTjHR3MfJpmxt8LEwNkjm --- Containers.Test/ContiguousMapTests.cs | 16 ++++++++++++++++ Containers/ContiguousMap.cs | 4 ++-- 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/Containers.Test/ContiguousMapTests.cs b/Containers.Test/ContiguousMapTests.cs index 5a0659b..42f20f2 100644 --- a/Containers.Test/ContiguousMapTests.cs +++ b/Containers.Test/ContiguousMapTests.cs @@ -157,6 +157,22 @@ public void Indexer_Set_UpdatesValue() Assert.AreEqual(1, map.Count); // Count should not change } + [TestMethod] + public void Indexer_SetExistingKeyWithDifferentSpelling_KeepsOriginalKey() + { + // Arrange + ContiguousMap map = new(StringComparer.OrdinalIgnoreCase) { ["Apple"] = 1 }; + + // Act + map["APPLE"] = 2; + + // Assert + Assert.AreSequenceEqual(["Apple"], map.Keys); + Assert.AreEqual("Apple", map.AsSpan()[0].Key); + Assert.AreEqual(new KeyValuePair("Apple", 2), map.Single()); + Assert.AreEqual(2, map["apple"]); + } + [TestMethod] public void Indexer_SetNewKey_AddsKeyValuePair() { diff --git a/Containers/ContiguousMap.cs b/Containers/ContiguousMap.cs index b72142a..299d367 100644 --- a/Containers/ContiguousMap.cs +++ b/Containers/ContiguousMap.cs @@ -174,8 +174,8 @@ public TValue this[TKey key] if (keyToIndex.TryGetValue(key, out int index)) { - // Key exists, update the value - items[index] = new Entry(key, value); + // Key exists, update the value and keep the key that was stored first + items[index] = new Entry(items[index].Key, value); } else {