diff --git a/projects/samskivert/src/java/com/samskivert/util/HashIntMap.java b/projects/samskivert/src/java/com/samskivert/util/HashIntMap.java index 32c3b05e..0f702b66 100644 --- a/projects/samskivert/src/java/com/samskivert/util/HashIntMap.java +++ b/projects/samskivert/src/java/com/samskivert/util/HashIntMap.java @@ -50,7 +50,7 @@ public class HashIntMap extends AbstractMap /** * The default number of buckets to use for the hash table. */ - public final static int DEFAULT_BUCKETS = 64; + public final static int DEFAULT_BUCKETS = 16; /** * The default load factor. @@ -148,7 +148,7 @@ public class HashIntMap extends AbstractMap public Object put (int key, Object value) { // check to see if we've passed our load factor, if so: resize - checkGrow(); + ensureCapacity(_size + 1); int index = keyToIndex(key); Record rec = _buckets[index]; @@ -244,6 +244,21 @@ public class HashIntMap extends AbstractMap _size = 0; } + /** + * Ensure that the hash can comfortably hold the specified number + * of elements. + */ + public void ensureCapacity (int minCapacity) + { + int size = _buckets.length; + while (minCapacity > (int) (size * _loadFactor)) { + size *= 2; + } + if (size != _buckets.length) { + resizeBuckets(size); + } + } + /** * Turn the specified key into an index. */ @@ -269,16 +284,6 @@ public class HashIntMap extends AbstractMap } } - /** - * Check to see if we want to grow the table. - */ - protected void checkGrow () - { - if (_size > (int) (_buckets.length * _loadFactor)) { - resizeBuckets(_buckets.length << 1); - } - } - /** * Resize the hashtable. * diff --git a/projects/samskivert/src/java/com/samskivert/util/IntIntMap.java b/projects/samskivert/src/java/com/samskivert/util/IntIntMap.java index f876357c..2114a8da 100644 --- a/projects/samskivert/src/java/com/samskivert/util/IntIntMap.java +++ b/projects/samskivert/src/java/com/samskivert/util/IntIntMap.java @@ -44,7 +44,7 @@ public class IntIntMap public int setIntValue (int value); } - public final static int DEFAULT_BUCKETS = 64; + public final static int DEFAULT_BUCKETS = 16; /** * The default load factor. @@ -84,7 +84,7 @@ public class IntIntMap _modCount++; // check to see if we've passed our load factor, if so: resize - checkGrow(); + ensureCapacity(_size + 1); int index = Math.abs(key)%_buckets.length; Record rec = _buckets[index]; @@ -193,6 +193,20 @@ public class IntIntMap _size = 0; } + /** + * Ensure that the specified number of elements may be added. + */ + public void ensureCapacity (int minCapacity) + { + int size = _buckets.length; + while (minCapacity > (int) (size * _loadFactor)) { + size *= 2; + } + if (size != _buckets.length) { + resizeBuckets(size); + } + } + /** * Check to see if we want to shrink the table. */ @@ -204,16 +218,6 @@ public class IntIntMap } } - /** - * Check to see if we want to grow the table. - */ - protected void checkGrow () - { - if (_size > (int) (_buckets.length * _loadFactor)) { - resizeBuckets(_buckets.length << 1); - } - } - /** * Resize the hashtable. *