Skip to content

Commit

Permalink
Fixed If GetHashCode is negative
Browse files Browse the repository at this point in the history
  • Loading branch information
Sam Listopad committed Apr 19, 2012
1 parent bdb5940 commit e6da904
Showing 1 changed file with 19 additions and 12 deletions.
31 changes: 19 additions & 12 deletions src/DotLiquid/Util/WeakTable.cs
Expand Up @@ -31,29 +31,36 @@ public WeakTable(int size)
} }
set set
{ {
int i = key.GetHashCode() % _buckets.Length; int i = (int)((uint)key.GetHashCode() % (uint)_buckets.Length);
_buckets[i].Key = key; _buckets[i].Key = key;
_buckets[i].Value = new WeakReference(value); _buckets[i].Value = new WeakReference(value);
} }
} }


public bool TryGetValue(TKey key, out TValue value) public bool TryGetValue(TKey key, out TValue value)
{ {
int i = key.GetHashCode() % _buckets.Length; WeakReference wr;
WeakReference wr; try
if ((wr = _buckets[i].Value) == null || !_buckets[i].Key.Equals(key)) {
{ int i = (int)((uint)key.GetHashCode() % (uint)_buckets.Length);
value = null; if ((wr = _buckets[i].Value) == null || !_buckets[i].Key.Equals(key))
return false; {
} value = null;
value = (TValue) wr.Target; return false;
}
value = (TValue)wr.Target;
}
catch (IndexOutOfRangeException ex)
{
throw new IndexOutOfRangeException("Index Out Of Range, key was " + key.ToString() + ", hashcode was " + key.GetHashCode().ToString(), ex);
}
return wr.IsAlive; return wr.IsAlive;
} }


public void Remove(TKey key) public void Remove(TKey key)
{ {
int i = key.GetHashCode() % _buckets.Length; int i = (int)((uint)key.GetHashCode() % (uint)_buckets.Length);
if (_buckets[i].Key.Equals(key)) if (_buckets[i].Key.Equals(key))
_buckets[i].Value = null; _buckets[i].Value = null;
} }
} }
Expand Down

0 comments on commit e6da904

Please sign in to comment.