Permalink
Browse files

IDictionary interface with Remove(TKey, out TValue) method

  • Loading branch information...
JeffreyZhao committed Jan 6, 2013
1 parent 24f347d commit 15864550a339abdebba9b507afddb23d1caf9cb9
Showing with 19 additions and 4 deletions.
  1. +12 −4 src/Tmc/HashDictionary.cs
  2. +6 −0 src/Tmc/IDictionary.cs
  3. +1 −0 src/Tmc/Tmc.csproj
View
@@ -65,15 +65,15 @@ private struct Entry {
get { return _keys ?? (_keys = new KeyCollection(this)); }
}
- ICollection<TKey> IDictionary<TKey, TValue>.Keys {
+ ICollection<TKey> System.Collections.Generic.IDictionary<TKey, TValue>.Keys {
get { return Keys; }
}
public ValueCollection Values {
get { return _values ?? (_values = new ValueCollection(this)); }
}
- ICollection<TValue> IDictionary<TKey, TValue>.Values {
+ ICollection<TValue> System.Collections.Generic.IDictionary<TKey, TValue>.Values {
get { return Values; }
}
@@ -298,9 +298,13 @@ private struct Entry {
}
public bool Remove(TKey key) {
+ TValue value;
+ return Remove(key, out value);
+ }
+
+ public bool Remove(TKey key, out TValue value) {
// ReSharper disable CompareNonConstrainedGenericWithNull
- if (key == null)
- {
+ if (key == null) {
throw new ArgumentNullException("key");
}
// ReSharper restore CompareNonConstrainedGenericWithNull
@@ -309,6 +313,7 @@ private struct Entry {
var hashCode = _comparer.GetHashCode(key) & 0x7FFFFFFF;
var bucket = hashCode % _buckets.Length;
var last = -1;
+
for (var 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) {
@@ -318,6 +323,8 @@ private struct Entry {
_entries[last].Next = _entries[i].Next;
}
+ value = _entries[i].Value;
+
_entries[i].HashCode = -1;
_entries[i].Next = _freeList;
_entries[i].Key = default(TKey);
@@ -331,6 +338,7 @@ private struct Entry {
}
}
+ value = default(TValue);
return false;
}
View
@@ -0,0 +1,6 @@
+namespace Tmc
+{
+ public interface IDictionary<TKey, TValue> : System.Collections.Generic.IDictionary<TKey, TValue> {
+ bool Remove(TKey key, out TValue value);
+ }
+}
View
@@ -41,6 +41,7 @@
<Compile Include="ErrorMessages.cs" />
<Compile Include="HashedLinkedList.cs" />
<Compile Include="HashHelpers.cs" />
+ <Compile Include="IDictionary.cs" />
<Compile Include="NullableKey.cs" />
<Compile Include="NullableKeyComparer.cs" />
<Compile Include="NullableKeyEqualityComparer.cs" />

0 comments on commit 1586455

Please sign in to comment.