Skip to content

Instantly share code, notes, and snippets.

@droyad
Created August 21, 2012 06:52
Show Gist options
  • Save droyad/3412797 to your computer and use it in GitHub Desktop.
Save droyad/3412797 to your computer and use it in GitHub Desktop.
private struct Entry
{
public int hashCode;
public int next;
public TKey key;
public TValue value;
}
private Dictionary<TKey, TValue>.Entry[] entries;
private int[] buckets;
public bool TryGetValue(TKey key, out TValue value)
{
int entry = this.FindEntry(key);
if (entry >= 0)
{
value = this.entries[entry].value;
return true;
}
else
{
value = default (TValue);
return false;
}
}
private int FindEntry(TKey key)
{
if ((object) key == null)
ThrowHelper.ThrowArgumentNullException(ExceptionArgument.key);
if (this.buckets != null)
{
int num = this.comparer.GetHashCode(key) & int.MaxValue;
for (int index = this.buckets[num % this.buckets.Length]; index >= 0; index = this.entries[index].next)
{
if (this.entries[index].hashCode == num && this.comparer.Equals(this.entries[index].key, key))
return index;
}
}
return -1;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment