/// <summary> /// Resizes the cache to a particular number of buckets. /// </summary> /// <param name="bucketCount"> /// The number of buckets to resize the cache to. /// </param> /// <remarks>This method is not thread-safe.</remarks> private void ResizeToImpl(int bucketCount) { var newBuckets = new WeakCacheBucket <TKey, TValue> [ bucketCount]; initializedBucketCount = 0; for (int i = 0; i < buckets.Length; i++) { var oldBucket = buckets[i]; while (!oldBucket.IsEmpty) { var kvPair = oldBucket.keyValuePair; TKey entryKey; TValue entryValue; if (kvPair.Key.TryGetTarget(out entryKey) && kvPair.Value.TryGetTarget(out entryValue)) { // This entry is still alive. Add it to the right // bucket in the new bucket array. var newBucketIndex = TruncateHashCode(kvPair.KeyHashCode, bucketCount); var newBucket = newBuckets[newBucketIndex]; if (newBucket.IsEmpty) { initializedBucketCount++; } newBucket.Add(kvPair); newBuckets[newBucketIndex] = newBucket; } if (oldBucket.spilloverList == null) { break; } else { oldBucket = oldBucket.spilloverList.contents; } } } buckets = newBuckets; for (int i = 0; i < accessCounters.Length; i++) { accessCounters[i] = 0; } }
public WeakCacheBucketNode( WeakCacheBucket <TKey, TValue> contents) { this.contents = contents; }