Skip to content

Instantly share code, notes, and snippets.

@joao-r-reis
Last active October 31, 2020 21:02
Show Gist options
  • Save joao-r-reis/f1b08711494b8f9e5a1921aad8235932 to your computer and use it in GitHub Desktop.
Save joao-r-reis/f1b08711494b8f9e5a1921aad8235932 to your computer and use it in GitHub Desktop.
Insert.cs
private void Insert(TKey key, TValue value, bool add)
{
// these 3 variables are fields of the Dictionary
// int[] buckets;
// Entry[] entries;
// IEqualityComparer<TKey> comparer;
int hashCode = comparer.GetHashCode(key) & 0x7FFFFFFF;
int targetBucket = hashCode % buckets.Length;
for (int i = buckets[targetBucket]; i >= 0; i = entries[i].next)
{
if (entries[i].hashCode == hashCode && comparer.Equals(entries[i].key, key))
{
if (add)
{
ThrowHelper.ThrowArgumentException(ExceptionResource.Argument_AddingDuplicate);
}
entries[i].value = value;
return;
}
}
// removed add logic for simplicity
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment