- Changed default buckets to 16 from 64.

- Removed protected checkGrow() in favor of Collections-style public
  ensureCapacity().


git-svn-id: https://samskivert.googlecode.com/svn/trunk@1770 6335cc39-0255-0410-8fd6-9bcaacd3b74c
This commit is contained in:
ray
2006-01-23 23:05:37 +00:00
parent c0b7ad1942
commit 35a3c9c784
2 changed files with 33 additions and 24 deletions
@@ -50,7 +50,7 @@ public class HashIntMap extends AbstractMap
/** /**
* The default number of buckets to use for the hash table. * 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. * The default load factor.
@@ -148,7 +148,7 @@ public class HashIntMap extends AbstractMap
public Object put (int key, Object value) public Object put (int key, Object value)
{ {
// check to see if we've passed our load factor, if so: resize // check to see if we've passed our load factor, if so: resize
checkGrow(); ensureCapacity(_size + 1);
int index = keyToIndex(key); int index = keyToIndex(key);
Record rec = _buckets[index]; Record rec = _buckets[index];
@@ -244,6 +244,21 @@ public class HashIntMap extends AbstractMap
_size = 0; _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. * 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. * Resize the hashtable.
* *
@@ -44,7 +44,7 @@ public class IntIntMap
public int setIntValue (int value); public int setIntValue (int value);
} }
public final static int DEFAULT_BUCKETS = 64; public final static int DEFAULT_BUCKETS = 16;
/** /**
* The default load factor. * The default load factor.
@@ -84,7 +84,7 @@ public class IntIntMap
_modCount++; _modCount++;
// check to see if we've passed our load factor, if so: resize // check to see if we've passed our load factor, if so: resize
checkGrow(); ensureCapacity(_size + 1);
int index = Math.abs(key)%_buckets.length; int index = Math.abs(key)%_buckets.length;
Record rec = _buckets[index]; Record rec = _buckets[index];
@@ -193,6 +193,20 @@ public class IntIntMap
_size = 0; _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. * 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. * Resize the hashtable.
* *