Пример #1
0
        private void Initialize(int capacity)
        {
            var expanded = PrimeHelpers.ExpandPrime(capacity);

            _rehash = (int)(expanded * 0.75f);
            _keys   = new Type[expanded];
            _values = new TValue[expanded];
        }
Пример #2
0
        private void Expand(int capacity)
        {
            int newCapacity = PrimeHelpers.ExpandPrime(capacity);

            var expandedKeys   = new Type[newCapacity];
            var expandedValues = new TValue[newCapacity];

            int rehashed = 0;

            for (int i = 0; i < capacity; i++)
            {
                var existingKey = _keys[i];

                if (existingKey != null)
                {
                    var hash = RuntimeHelpers.GetHashCode(existingKey);

                    int newPosition = (hash & 0x7FFFFFFF) % newCapacity;
                    while (expandedKeys[newPosition] != null)
                    {
                        newPosition++;
                        if (newPosition == newCapacity)
                        {
                            newPosition = 0;
                        }
                    }

                    expandedKeys[newPosition]   = existingKey;
                    expandedValues[newPosition] = _values[i];

                    rehashed++;
                    if (rehashed == _count)
                    {
                        break;
                    }
                }
            }

            _rehash = (int)(expandedKeys.Length * 0.75f);
            _keys   = expandedKeys;
            _values = expandedValues;
        }