From 66dbc52030bad481bed41358265ec3b07e69eb2d Mon Sep 17 00:00:00 2001 From: ray Date: Tue, 24 Jan 2006 21:12:26 +0000 Subject: [PATCH] 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 --- .../src/java/com/samskivert/util/HashIntMap.java | 14 ++++---------- .../src/java/com/samskivert/util/IntIntMap.java | 14 +++++--------- 2 files changed, 9 insertions(+), 19 deletions(-) diff --git a/projects/samskivert/src/java/com/samskivert/util/HashIntMap.java b/projects/samskivert/src/java/com/samskivert/util/HashIntMap.java index 0f702b66..bee1646b 100644 --- a/projects/samskivert/src/java/com/samskivert/util/HashIntMap.java +++ b/projects/samskivert/src/java/com/samskivert/util/HashIntMap.java @@ -57,13 +57,6 @@ public class HashIntMap extends AbstractMap */ 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 * buckets. @@ -246,7 +239,8 @@ public class HashIntMap extends AbstractMap /** * 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) { @@ -279,8 +273,8 @@ public class HashIntMap extends AbstractMap protected void checkShrink () { if ((_buckets.length > DEFAULT_BUCKETS) && - (_size * SHRINK_FACTOR < _buckets.length)) { - resizeBuckets(_buckets.length >> 1); + (_size < (int) (_buckets.length * _loadFactor * .125))) { + resizeBuckets(Math.max(DEFAULT_BUCKETS, _buckets.length >> 1)); } } diff --git a/projects/samskivert/src/java/com/samskivert/util/IntIntMap.java b/projects/samskivert/src/java/com/samskivert/util/IntIntMap.java index 2114a8da..542cc7bf 100644 --- a/projects/samskivert/src/java/com/samskivert/util/IntIntMap.java +++ b/projects/samskivert/src/java/com/samskivert/util/IntIntMap.java @@ -51,12 +51,6 @@ public class IntIntMap */ 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) { @@ -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) { @@ -213,8 +209,8 @@ public class IntIntMap protected void checkShrink () { if ((_buckets.length > DEFAULT_BUCKETS) && - (_size * SHRINK_FACTOR < _buckets.length)) { - resizeBuckets(_buckets.length >> 1); + (_size < (int) (_buckets.length * _loadFactor * .125))) { + resizeBuckets(Math.max(DEFAULT_BUCKETS, _buckets.length >> 1)); } }