Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Dictionary<TKey, TValue>
- {
- public TValue this[TKey key] {
- get {
- int i = FindEntry(key);
- if (i >= 0) return entries[i].value;
- ThrowHelper.ThrowKeyNotFoundException();
- return default(TValue);
- }
- set {
- Insert(key, value, false);
- }
- }
- public void Add(TKey key, TValue value) {
- Insert(key, value, true);
- }
- private void Insert(TKey key, TValue value, bool add) {
- if( key == null ) {
- ThrowHelper.ThrowArgumentNullException(ExceptionArgument.key);
- }
- if (buckets == null) Initialize(0);
- int hashCode = comparer.GetHashCode(key) & 0x7FFFFFFF;
- int targetBucket = hashCode % buckets.Length;
- #if FEATURE_RANDOMIZED_STRING_HASHING
- int collisionCount = 0;
- #endif
- 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;
- version++;
- return;
- }
- #if FEATURE_RANDOMIZED_STRING_HASHING
- collisionCount++;
- #endif
- }
- int index;
- if (freeCount > 0) {
- index = freeList;
- freeList = entries[index].next;
- freeCount--;
- }
- else {
- if (count == entries.Length)
- {
- Resize();
- targetBucket = hashCode % buckets.Length;
- }
- index = count;
- count++;
- }
- entries[index].hashCode = hashCode;
- entries[index].next = buckets[targetBucket];
- entries[index].key = key;
- entries[index].value = value;
- buckets[targetBucket] = index;
- version++;
- #if FEATURE_RANDOMIZED_STRING_HASHING
- if(collisionCount > HashHelpers.HashCollisionThreshold && HashHelpers.IsWellKnownEqualityComparer(comparer))
- {
- comparer = (IEqualityComparer<TKey>) HashHelpers.GetRandomizedEqualityComparer(comparer);
- Resize(entries.Length, true);
- }
- #endif
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement