mdh.hephaestus2.containers
Class IntHash

java.lang.Object
  |
  +--mdh.hephaestus2.containers.SparseArray
        |
        +--mdh.hephaestus2.containers.IntHash
All Implemented Interfaces:
Cloneable, Serializable

public final class IntHash
extends SparseArray
implements Serializable, Cloneable

An IntHash is a linked-list hashtable using an int key rather than an object. It uses less memory and fewer objects than Hashtable, and runs 2-10 times faster.

Author:
Copyright © 2000 by Mark Hughes <kamikaze@kuoi.asui.uidaho.edu>
See Also:
Serialized Form

Field Summary
 
Fields inherited from class mdh.hephaestus2.containers.SparseArray
DEFAULT_CAPACITY, DEFAULT_LOADFACTOR, PROBABLY_NOT_FOUND
 
Constructor Summary
IntHash()
          Creates a new IntHash with a default size of DEFAULT_CAPACITY and loadFactor of DEFAULT_LOADFACTOR.
IntHash(int capacity)
          Creates a new IntHash with a given capacity and default loadFactor of DEFAULT_LOADFACTOR.
IntHash(int capacity, float loadFactor)
          Creates an IntHash.
 
Method Summary
 void clear()
          erase all contents of the IntHash
 Object clone()
           
 boolean contains(Object value)
           
 boolean containsKey(int key)
           
 Enumeration elements()
          Returns an Enumeration of the values in this IntHash
 Object[] elementsArray()
          Produces an unsorted Object[] array of the elements stored herein, or a zero-length array if it is empty.
 Enumeration elementsCopy()
          Returns an Enumeration of the values in this IntHash, using a copy of the arrays, so you can continue to manipulate the IntHash while enumerating.
 void ensureCapacity(int newcap)
          Resizes the hash buckets to hold at least newcap entries, before the loadFactor.
 Object get(int key)
           
 int indexOf(Object find)
          Finds a value that matches 'find' and returns the key.
 IntEnumeration keys()
          Returns an Enumeration of the keys in this IntHash, each wrapped in Integers.
 int[] keysArray()
          Produces an unsorted int[] array of the keys stored herein, or a zero-length array if the IntHash is empty.
 IntEnumeration keysCopy()
          Returns an Enumeration of the keys in this IntHash, using a copy of the arrays, so you can continue to manipulate the IntHash while enumerating.
 void put(int key, Object value)
          Assigns the 'value' to the entry at 'key'.
 Object remove(int key)
          Removes the entry at 'key', and returns the former value.
 
Methods inherited from class mdh.hephaestus2.containers.SparseArray
getBoolean, getByte, getCapacity, getChar, getDouble, getFloat, getInt, getLoadFactor, getLong, getShort, getString, isEmpty, put, put, put, put, put, put, put, put, set, set, set, set, set, set, set, set, set, setLoadFactor, size, toString
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

IntHash

public IntHash()
Creates a new IntHash with a default size of DEFAULT_CAPACITY and loadFactor of DEFAULT_LOADFACTOR.


IntHash

public IntHash(int capacity)
Creates a new IntHash with a given capacity and default loadFactor of DEFAULT_LOADFACTOR.


IntHash

public IntHash(int capacity,
               float loadFactor)
Creates an IntHash.

Method Detail

clear

public void clear()
erase all contents of the IntHash

Specified by:
clear in class SparseArray

clone

public Object clone()
Overrides:
clone in class Object

contains

public boolean contains(Object value)
Specified by:
contains in class SparseArray
Returns:
true if the value is contained in the IntHash (slow).

containsKey

public boolean containsKey(int key)
Specified by:
containsKey in class SparseArray
Returns:
true if the key is contained in the IntHash (fast).

elements

public Enumeration elements()
Returns an Enumeration of the values in this IntHash

Specified by:
elements in class SparseArray

elementsArray

public Object[] elementsArray()
Produces an unsorted Object[] array of the elements stored herein, or a zero-length array if it is empty. You can continue to manipulate the IntHash while using this array. This is only a shallow copy, it does not duplicate the contents.

Specified by:
elementsArray in class SparseArray

elementsCopy

public Enumeration elementsCopy()
Returns an Enumeration of the values in this IntHash, using a copy of the arrays, so you can continue to manipulate the IntHash while enumerating. This is only a shallow copy, it does not duplicate the contents.

Specified by:
elementsCopy in class SparseArray

ensureCapacity

public void ensureCapacity(int newcap)
Resizes the hash buckets to hold at least newcap entries, before the loadFactor.

Specified by:
ensureCapacity in class SparseArray

get

public Object get(int key)
Specified by:
get in class SparseArray
Returns:
(Object) value stored for the given key, or 'null' if not found.

indexOf

public int indexOf(Object find)
Finds a value that matches 'find' and returns the key. Note that this is not necessarily the lowest-numbered index. If it returns PROBABLY_NOT_FOUND, and it's possible for that to be a valid key in your program, you should check get(PROBABLY_NOT_FOUND) to see if it's defined or not.

This is fairly slow - it's O(n) instead of O(log n) as most things are.

Specified by:
indexOf in class SparseArray

keys

public IntEnumeration keys()
Returns an Enumeration of the keys in this IntHash, each wrapped in Integers.

Specified by:
keys in class SparseArray

keysArray

public int[] keysArray()
Produces an unsorted int[] array of the keys stored herein, or a zero-length array if the IntHash is empty. You can continue to manipulate the IntHash while using this array. This is only a shallow copy, it does not duplicate the contents.

Specified by:
keysArray in class SparseArray

keysCopy

public IntEnumeration keysCopy()
Returns an Enumeration of the keys in this IntHash, using a copy of the arrays, so you can continue to manipulate the IntHash while enumerating. This is only a shallow copy, it does not duplicate the contents.

Specified by:
keysCopy in class SparseArray

put

public void put(int key,
                Object value)
Assigns the 'value' to the entry at 'key'. If you try to store a null, it just deletes whatever was formerly at that key.

Specified by:
put in class SparseArray

remove

public Object remove(int key)
Removes the entry at 'key', and returns the former value.

Specified by:
remove in class SparseArray
Returns:
(Object) former value, or null if nothing was found to remove.