From 47685a021f36938108d438402e6af6d4da86be0a Mon Sep 17 00:00:00 2001 From: WinCPP Date: Sun, 9 Apr 2017 12:08:24 +0530 Subject: [PATCH 1/2] CoreFx #15622 New overload Dictionary.Remove(K, out V) --- .../System/Collections/Generic/Dictionary.cs | 43 +++++++++++++++++++ 1 file changed, 43 insertions(+) diff --git a/src/System.Private.CoreLib/src/System/Collections/Generic/Dictionary.cs b/src/System.Private.CoreLib/src/System/Collections/Generic/Dictionary.cs index d6c8d55ee2e..736d5f2e30c 100644 --- a/src/System.Private.CoreLib/src/System/Collections/Generic/Dictionary.cs +++ b/src/System.Private.CoreLib/src/System/Collections/Generic/Dictionary.cs @@ -575,6 +575,49 @@ public bool Remove(TKey key) return false; } + public bool Remove(TKey key, out TValue value) + { + if (key == null) + { + value = default(TValue); + throw new ArgumentNullException(nameof(key)); + } + + if (buckets != null) + { + int hashCode = comparer.GetHashCode(key) & 0x7FFFFFFF; + int bucket = hashCode % buckets.Length; + int last = -1; + for (int i = buckets[bucket]; i >= 0; last = i, i = entries[i].next) + { + if (entries[i].hashCode == hashCode && comparer.Equals(entries[i].key, key)) + { + if (last < 0) + { + buckets[bucket] = entries[i].next; + } + else + { + entries[last].next = entries[i].next; + } + + value = entries[i].value; + + entries[i].hashCode = -1; + entries[i].next = freeList; + entries[i].key = default(TKey); + entries[i].value = default(TValue); + freeList = i; + freeCount++; + version++; + return true; + } + } + } + value = default(TValue); + return false; + } + public bool TryGetValue(TKey key, out TValue value) { int i = FindEntry(key); From d12c1a939324016036061585c3332d573d83290d Mon Sep 17 00:00:00 2001 From: WinCPP Date: Wed, 12 Apr 2017 07:17:37 +0530 Subject: [PATCH 2/2] CoreFx #15622 Perf comment, remove default assign --- .../src/System/Collections/Generic/Dictionary.cs | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/System.Private.CoreLib/src/System/Collections/Generic/Dictionary.cs b/src/System.Private.CoreLib/src/System/Collections/Generic/Dictionary.cs index 736d5f2e30c..9ddadcd1165 100644 --- a/src/System.Private.CoreLib/src/System/Collections/Generic/Dictionary.cs +++ b/src/System.Private.CoreLib/src/System/Collections/Generic/Dictionary.cs @@ -537,6 +537,9 @@ private void Resize(int newSize, bool forceNewHashCodes) entries = newEntries; } + // The overload Remove(TKey key, out TValue value) is a copy of this method with one additional + // statement to copy the value for entry being removed into the output parameter. + // Code has been intentionally duplicated for performance reasons. public bool Remove(TKey key) { if (key == null) @@ -575,11 +578,13 @@ public bool Remove(TKey key) return false; } + // This overload is a copy of the overload Remove(TKey key) with one additional + // statement to copy the value for entry being removed into the output parameter. + // Code has been intentionally duplicated for performance reasons. public bool Remove(TKey key, out TValue value) { if (key == null) { - value = default(TValue); throw new ArgumentNullException(nameof(key)); }