Incorporate the load factor when calculating the minimum size to trigger

a shrink.


git-svn-id: https://samskivert.googlecode.com/svn/trunk@1771 6335cc39-0255-0410-8fd6-9bcaacd3b74c
This commit is contained in:
ray
2006-01-24 21:12:26 +00:00
parent 35a3c9c784
commit 66dbc52030
2 changed files with 9 additions and 19 deletions
@@ -57,13 +57,6 @@ public class HashIntMap extends AbstractMap
*/ */
public final static float DEFAULT_LOAD_FACTOR = 1.75f; public final static float DEFAULT_LOAD_FACTOR = 1.75f;
/**
* The shrink factor.
* When the number of elements multiplied by this number is less
* than the size of the array, we shrink the array by half.
*/
protected static final int SHRINK_FACTOR = 8;
/** /**
* Constructs an empty hash int map with the specified number of hash * Constructs an empty hash int map with the specified number of hash
* buckets. * buckets.
@@ -246,7 +239,8 @@ public class HashIntMap extends AbstractMap
/** /**
* Ensure that the hash can comfortably hold the specified number * Ensure that the hash can comfortably hold the specified number
* of elements. * of elements. Calling this method is not necessary, but can improve
* performance if done prior to adding many elements.
*/ */
public void ensureCapacity (int minCapacity) public void ensureCapacity (int minCapacity)
{ {
@@ -279,8 +273,8 @@ public class HashIntMap extends AbstractMap
protected void checkShrink () protected void checkShrink ()
{ {
if ((_buckets.length > DEFAULT_BUCKETS) && if ((_buckets.length > DEFAULT_BUCKETS) &&
(_size * SHRINK_FACTOR < _buckets.length)) { (_size < (int) (_buckets.length * _loadFactor * .125))) {
resizeBuckets(_buckets.length >> 1); resizeBuckets(Math.max(DEFAULT_BUCKETS, _buckets.length >> 1));
} }
} }
@@ -51,12 +51,6 @@ public class IntIntMap
*/ */
public final static float DEFAULT_LOAD_FACTOR = 1.75f; public final static float DEFAULT_LOAD_FACTOR = 1.75f;
/**
* The shrink factor.
* When the number of elements multiplied by this number is less
* than the size of the array, we shrink the array by half.
*/
protected static final int SHRINK_FACTOR = 8;
public IntIntMap (int buckets, float loadFactor) public IntIntMap (int buckets, float loadFactor)
{ {
@@ -194,7 +188,9 @@ public class IntIntMap
} }
/** /**
* Ensure that the specified number of elements may be added. * Ensure that the hash can comfortably hold the specified number
* of elements. Calling this method is not necessary, but can improve
* performance if done prior to adding many elements.
*/ */
public void ensureCapacity (int minCapacity) public void ensureCapacity (int minCapacity)
{ {
@@ -213,8 +209,8 @@ public class IntIntMap
protected void checkShrink () protected void checkShrink ()
{ {
if ((_buckets.length > DEFAULT_BUCKETS) && if ((_buckets.length > DEFAULT_BUCKETS) &&
(_size * SHRINK_FACTOR < _buckets.length)) { (_size < (int) (_buckets.length * _loadFactor * .125))) {
resizeBuckets(_buckets.length >> 1); resizeBuckets(Math.max(DEFAULT_BUCKETS, _buckets.length >> 1));
} }
} }