private void EnsureCapacity(long min) { if ((m_capacity >= min) && (m_capacity > 0)) { return; } this.RemoveDeadEntries(); if (m_size * 2L < m_capacity) { return; } var capacity = WeakDictionary <TKey, TValue> .FindNextSize(min); Debug.Assert(capacity > 0); m_buckets = new int[capacity]; Array.Resize(ref m_entries, capacity); Array.Resize(ref m_entriesInfo, capacity); for (int i = 0; i < m_buckets.Length; i++) { m_buckets[i] = -1; } for (int i = m_capacity; i < capacity; i++) { m_entries[i] = null; m_entriesInfo[i] = new EntryInfo((i < capacity - 1) ? i + 1 : -1); } // Rehash the elements to initialize the buckets. for (int i = 0; i < m_capacity; i++) { // Do not rehash free entries. if (WeakDictionary <TKey, TValue> .IsFree(m_entries[i])) { continue; } var entryInfo = m_entriesInfo[i]; var bucket = WeakDictionary <TKey, TValue> .GetBucket(entryInfo.HashCode, capacity); Debug.Assert((bucket >= 0) && (bucket < capacity)); var index = m_buckets[bucket]; m_buckets[bucket] = i; m_entriesInfo[i] = entryInfo.SetNext(index); } // Link the remaining free entries to the newly created free entries. if (m_free >= 0) { var index = m_free; while (true) { var next = m_entriesInfo[index].Next; if (next < 0) { m_entriesInfo[index] = new EntryInfo(m_capacity); break; } index = next; } } else { m_free = m_capacity; } m_capacity = capacity; unchecked { m_version++; } }
private int GetBucket(int hashCode) { return(WeakDictionary <TKey, TValue> .GetBucket(hashCode, m_capacity)); }