[Mono-list] Hashtable bucket growth

John Barnette jbarn@httcb.net
Sat, 14 Jul 2001 15:13:49 -0600


Hey kids,

This is what I'm currently using to generate the new available bucket count
during a rehash.  Any comments?


~ j.

--- CUT CUT CUT ---

// Max entries in a Hashtable == Int32.MaxValue == 2,147,483,647
private static int[] PRIMES = {
	2, 5, 11, 23, 47, 97, 197, 397, 797, 1597, 3203, 6421, 12853,
	25717, 51437, 102877, 205759, 411527, 823117, 1646237, 3292489,
	6584983, 13169977, 26339969, 52679969, 105359939, 210719881,
	421439783, 842879579, 1685759167 };
private static int NUMBER_OF_PRIMES = 30;


private int GetNewBucketSize(int oldBucketSize) {
	int target = oldBucketSize * 2;
	int first = 0, last = NUMBER_OF_PRIMES - 1;

	while (first < last) {
		int mid = (first + last) / 2;
		if      (target < PRIMES[mid]) last = mid;
		else if (target > PRIMES[mid]) first = mid + 1;
		else {
			// can't happen
			throw new Exception("Can't happen in prime search");
		}
	}

	return PRIMES[last];
}