Esempio n. 1
0
        private Table(Table <TKey, TValue> origin, TKey key, TValue value)
        {
            int newBucketIndex;

            Count = origin.Count + 1;
            if (origin.Count > origin.Divisor)
            {
                Divisor = (origin.Divisor + 1) * 4 - 1;
                Buckets = CoreExtensions.CreateArray(Divisor + 1, EmptyBucket);
                var originBuckets = origin.Buckets;
                for (var originBucketIndex = 0; originBucketIndex < originBuckets.Length; originBucketIndex++)
                {
                    var originBucket = originBuckets[originBucketIndex];
                    for (var index = 0; index < originBucket.KeyValues.Length; index++)
                    {
                        var keyValue = originBucket.KeyValues[index];
                        newBucketIndex          = keyValue.Key.GetHashCode() & Divisor;
                        Buckets[newBucketIndex] = Buckets[newBucketIndex].Add(keyValue);
                    }
                }
            }
            else
            {
                Divisor = origin.Divisor;
                Buckets = origin.Buckets.Copy();
            }

            newBucketIndex          = key.GetHashCode() & Divisor;
            Buckets[newBucketIndex] = Buckets[newBucketIndex].Add(new KeyValue(key, value));
        }
Esempio n. 2
0
        public Table <TKey, TValue> Remove(TKey key, out bool removed)
        {
            removed = false;
            var newBuckets  = CoreExtensions.CreateArray(Divisor + 1, EmptyBucket);
            var hashCode    = key.GetHashCode();
            var bucketIndex = hashCode & Divisor;

            for (var curBucketIndex = 0; curBucketIndex < Buckets.Length; curBucketIndex++)
            {
                var bucket = Buckets[curBucketIndex];
                if (curBucketIndex != bucketIndex)
                {
                    newBuckets[curBucketIndex] = bucket.Copy();
                    continue;
                }

                // Bucket to remove an element
                for (var index = 0; index < bucket.KeyValues.Length; index++)
                {
                    var keyValue = bucket.KeyValues[index];
                    // Remove the element
                    if (keyValue.Key.GetHashCode() == hashCode && (ReferenceEquals(keyValue.Key, key) || Equals(keyValue.Key, key)))
                    {
                        newBuckets[bucketIndex] = bucket.Remove(index);
                        removed = true;
                    }
                }
            }

            return(new Table <TKey, TValue>(newBuckets, Divisor, removed ? Count - 1: Count));
        }
Esempio n. 3
0
 private Table(int size)
     : this(
         CoreExtensions.CreateArray(size + 1, EmptyBucket),
         size,
         0)
 {
 }
Esempio n. 4
0
 private Bucket(int count)
 {
     KeyValues = CoreExtensions.CreateArray <KeyValue>(count);
 }