Пример #1
0
        public HashTable(int initialCapacity)
        {
            if (initialCapacity < 1)
            {
                throw new ArgumentOutOfRangeException(nameof(initialCapacity));
            }
            _array = new HashTableArray <TKey, TValue>(initialCapacity);

            // when the count exeeds this value, the next Addd will cause the array to grow
            _maxItemsAtCurrentSize = (int)(initialCapacity * _fillfactor) + 1;
        }
Пример #2
0
        public void Add(TKey key, TValue value)
        {
            // if we are at capacity, the array needs to grow
            if (_count >= _maxItemsAtCurrentSize)
            {
                // allocate a larger array
                HashTableArray <TKey, TValue> largeArray = new HashTableArray <TKey, TValue>(_array.Capacity * 2);
                // and re-add each item to the new array
                foreach (HashTableNodePair <TKey, TValue> node in _array.Items)
                {
                    largeArray.Add(node.Key, node.Value);
                }
                // the larger array is now the hash table storage
                _array = largeArray;
                // update the new max items cached value
                _maxItemsAtCurrentSize = (int)(_array.Capacity * _fillfactor) + 1;
            }

            _array.Add(key, value);
            _count++;
        }