private void AddExistingValues(ImmutableHashTable <TKey, TValue> previous) { foreach (ImmutableHashTree <TKey, TValue> bucket in previous.Buckets) { foreach (var keyValue in bucket.InOrder()) { int hashCode = keyValue.Key.GetHashCode(); int bucketIndex = hashCode & (this.Divisor - 1); this.Buckets[bucketIndex] = this.Buckets[bucketIndex].Add(keyValue.Key, keyValue.Value); } } }
public void Setup() { dictionary.Add(typeof(Class1), () => new Class1()); dictionary.Add(typeof(Class2), () => new Class2()); dictionary.Add(typeof(Class3), () => new Class3()); dictionary.Add(typeof(Class4), () => new Class4()); dictionary.Add(typeof(Class5), () => new Class5()); immutableHashTable = immutableHashTable.Add(typeof(Class1), () => new Class1()); immutableHashTable = immutableHashTable.Add(typeof(Class2), () => new Class2()); immutableHashTable = immutableHashTable.Add(typeof(Class3), () => new Class3()); immutableHashTable = immutableHashTable.Add(typeof(Class4), () => new Class4()); immutableHashTable = immutableHashTable.Add(typeof(Class5), () => new Class5()); }
/// <summary> /// Initializes a new instance of the <see cref="ImmutableHashTable{TKey,TValue}"/> class. /// </summary> /// <param name="previous">The "previous" hash table that contains already existing values.</param> /// <param name="key">The key to be associated with the value.</param> /// <param name="value">The value to be added to the tree.</param> internal ImmutableHashTable(ImmutableHashTable <TKey, TValue> previous, TKey key, TValue value) { this.Count = previous.Count + 1; if (previous.Count >= previous.Divisor) { this.Divisor = previous.Divisor * 2; this.Buckets = new ImmutableHashTree <TKey, TValue> [this.Divisor]; InitializeBuckets(0, this.Divisor); this.AddExistingValues(previous); } else { this.Divisor = previous.Divisor; this.Buckets = new ImmutableHashTree <TKey, TValue> [this.Divisor]; Array.Copy(previous.Buckets, this.Buckets, previous.Divisor); } var hashCode = key.GetHashCode(); var bucketIndex = hashCode & (this.Divisor - 1); this.Buckets[bucketIndex] = this.Buckets[bucketIndex].Add(key, value); }