Grow and shrink the hash so that our performance stays near O(1).

(Altered Serializable compatability).
Reformatted tabs into 8 spaces.


git-svn-id: https://samskivert.googlecode.com/svn/trunk@755 6335cc39-0255-0410-8fd6-9bcaacd3b74c
This commit is contained in:
ray
2002-05-23 23:24:08 +00:00
parent 18cf7f6763
commit ea1c96d2d7
@@ -1,5 +1,5 @@
// //
// $Id: HashIntMap.java,v 1.7 2001/12/16 23:10:15 mdb Exp $ // $Id: HashIntMap.java,v 1.8 2002/05/23 23:24:08 ray Exp $
// //
// samskivert library - useful routines for java programs // samskivert library - useful routines for java programs
// Copyright (C) 2001 Michael Bayne // Copyright (C) 2001 Michael Bayne
@@ -46,13 +46,32 @@ public class HashIntMap
*/ */
public final static int DEFAULT_BUCKETS = 64; public final static int DEFAULT_BUCKETS = 64;
/**
* The default load factor.
*/
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.
*/ */
public HashIntMap (int buckets) public HashIntMap (int buckets, float loadFactor)
{ {
_buckets = new Record[buckets]; // force the capacity to be a power of 2
int capacity = 1;
while (capacity < buckets) {
capacity <<= 1;
}
_buckets = new Record[capacity];
_loadFactor = loadFactor;
} }
/** /**
@@ -61,7 +80,7 @@ public class HashIntMap
*/ */
public HashIntMap () public HashIntMap ()
{ {
this(DEFAULT_BUCKETS); this(DEFAULT_BUCKETS, DEFAULT_LOAD_FACTOR);
} }
// documentation inherited // documentation inherited
@@ -104,7 +123,7 @@ public class HashIntMap
// documentation inherited // documentation inherited
public Object get (int key) public Object get (int key)
{ {
int index = Math.abs(key) % _buckets.length; int index = keyToIndex(key);
for (Record rec = _buckets[index]; rec != null; rec = rec.next) { for (Record rec = _buckets[index]; rec != null; rec = rec.next) {
if (rec.key == key) { if (rec.key == key) {
return rec.value; return rec.value;
@@ -127,7 +146,10 @@ public class HashIntMap
throw new IllegalArgumentException(); throw new IllegalArgumentException();
} }
int index = Math.abs(key)%_buckets.length; // check to see if we've passed our load factor, if so: resize
checkGrow();
int index = keyToIndex(key);
Record rec = _buckets[index]; Record rec = _buckets[index];
// either we start a new chain // either we start a new chain
@@ -163,25 +185,29 @@ public class HashIntMap
// documentation inherited // documentation inherited
public Object remove (int key) public Object remove (int key)
{ {
int index = Math.abs(key) % _buckets.length; Object removed = removeImpl(key);
Record rec = _buckets[index]; if (removed != null) {
checkShrink();
// if there's no chain, there's no object }
if (rec == null) { return removed;
return null;
} }
// maybe it's the first one in this chain /**
* Remove an element with no checking to see if we should shrink.
*/
protected Object removeImpl (int key)
{
int index = keyToIndex(key);
Record prev = null;
// go through the chain looking for a match
for (Record rec = _buckets[index]; rec != null; rec = rec.next) {
if (rec.key == key) { if (rec.key == key) {
if (prev == null) {
_buckets[index] = rec.next; _buckets[index] = rec.next;
_size--; } else {
return rec.value;
}
// or maybe it's an element further down the chain
for (Record prev = rec; rec != null; rec = rec.next) {
if (rec.key == key) {
prev.next = rec.next; prev.next = rec.next;
}
_size--; _size--;
return rec.value; return rec.value;
} }
@@ -202,6 +228,62 @@ public class HashIntMap
_size = 0; _size = 0;
} }
/**
* Turn the specified key into an index.
*/
protected final int keyToIndex (int key)
{
// multiply the key by -127 and take the low bits
return ((key - (key << 7)) & (_buckets.length - 1));
}
/**
* Check to see if we want to shrink the table.
*/
protected void checkShrink ()
{
if ((_buckets.length > DEFAULT_BUCKETS) &&
(_size * SHRINK_FACTOR < _buckets.length)) {
resizeBuckets(_buckets.length >> 1);
}
}
/**
* 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.
*
* @param newsize MUST be a power of 2.
*/
protected void resizeBuckets (int newsize)
{
Record[] oldbuckets = _buckets;
_buckets = new Record[newsize];
// we shuffle the records around without allocating new ones
int index = oldbuckets.length;
while (index-- > 0) {
Record oldrec = oldbuckets[index];
while (oldrec != null) {
Record newrec = oldrec;
oldrec = oldrec.next;
// always put the newrec at the start of a chain
int newdex = keyToIndex(newrec.key);
newrec.next = _buckets[newdex];
_buckets[newdex] = newrec;
}
}
}
// documentation inherited // documentation inherited
public Set entrySet () public Set entrySet ()
{ {
@@ -268,7 +350,7 @@ public class HashIntMap
} }
// remove the record the hard way // remove the record the hard way
HashIntMap.this.remove(_last.key); HashIntMap.this.removeImpl(_last.key);
_last = null; _last = null;
} }
@@ -302,6 +384,7 @@ public class HashIntMap
{ {
// write out number of buckets // write out number of buckets
s.writeInt(_buckets.length); s.writeInt(_buckets.length);
s.writeFloat(_loadFactor);
// write out size (number of mappings) // write out size (number of mappings)
s.writeInt(_size); s.writeInt(_size);
@@ -323,6 +406,7 @@ public class HashIntMap
{ {
// read in number of buckets and allocate the bucket array // read in number of buckets and allocate the bucket array
_buckets = new Record[s.readInt()]; _buckets = new Record[s.readInt()];
_loadFactor = s.readFloat();
// read in size (number of mappings) // read in size (number of mappings)
int size = s.readInt(); int size = s.readInt();
@@ -385,6 +469,7 @@ public class HashIntMap
} }
} }
private Record[] _buckets; protected Record[] _buckets;
private int _size; protected int _size;
protected float _loadFactor;
} }