|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||
java.lang.Objectuka.util.IDConstants
uka.util.ShortIDMap
An instance of IDMap establishes a mapping between objects and
identifiers of type int.
Suppose, 3 objects have already been assigned an
identifier. Each of these objects is stored at an uniquie index
within the idToObject array. This index is the object's
identifier. The identifier that will be assigned to the next object
is #nextID.
nextID = 3
nextID
v
idToObject = [ obj0 | obj1 | obj2 ]
nextID
v
idToObject = [ obj0 | obj1 | obj2 | obj ]
An index into the hashToID array is generated from a
hashcode of an object using a hash function. At
this index, the identifier corresponding to the object is
stored.
index =hashFunction.ShortHashFunction.getHashCode(short)% hashSize
In case of a hash clash, for another object the same hash index
could have been generated. Therefore all objects with the same hash
index are chained. This happens in the chainID array.
In the following picture, the object with ID 1 (obj1) has the
same hash index as the newly inserted object (obj). Therefore a
link from the newly inserted object with identifier 4 is created to
the formerly inserted object with identifier 1. The old value
(identifer 1) at hashToID[index] can be replaced with the
ID of the newly inserted object (identifier 4).
Before: index
v
hashToID = [ | | | | | | | 1 | | | | ]
|
+--------------------+
v
chainID = [ | -1 | ]
|
-
After: index
v
hashToID = [ | | | | | | | nextID | | | | ]
|
- +-------+
| v
chainID = [ | -1 | | 1 ]
^ |
+------------+
When searching for an object with a known hash index, all
objects from idToObject with the following identifiers
have to be considered:
hashToID[index], chainID.get(chainID.get(hashToID.get(index))), chainID.get(chainID.get(chainID.get(hashToID.get[index]))), chainID.get(chainID.get(chainID.get(chainID.get(hashToID[index])))), ...
The search stops, either if the object is found in idToObject, or chainID points to -1 for the current
identifier.
Last but not least, the next identifier is incremented.
nextID = 4
| Field Summary | |
private EnlargingIntArray |
chainID
Function ID \rightarrow ID \cup {INVALID_ID} |
private int |
hashCapacity
|
private ShortHashFunction |
hashFunction
|
private int |
hashSize
|
private int[] |
hashToID
|
protected EnlargingShortArray |
idToObject
|
protected static int |
INVALID_INDEX
|
private int |
size
|
protected IDSpace |
space
|
| Fields inherited from class uka.util.IDConstants |
FIRST_NEW_ID, FIRST_VALID_ID, INVALID_ID, UNUSED_ID |
| Constructor Summary | |
ShortIDMap()
|
|
ShortIDMap(IDSpace space)
|
|
| Method Summary | |
void |
appendTo(ToString s)
This method should append the contents of each instance variable of the current
object to the given ToString object. |
void |
clear()
|
private static int |
computeHashCapacity(int hashSize)
|
private static int |
computeHashSize(int size)
|
void |
dump()
|
int |
findID(short obj)
|
private int |
findIDInternal(short obj,
int index)
|
protected short |
get(int id)
|
protected int |
getHash(short obj)
Computes an index into the hashToID array from the
hash value for the given object. |
int |
getID(short obj)
|
protected int |
getIndexForHash(int hash)
|
protected int |
getIndexForID(int id)
|
short |
getObject(int id)
|
ShortIterator |
getObjects()
|
int |
getSize()
|
IntIterator |
getUsedIDs()
|
private void |
growHashtable()
|
private void |
hashInsert(int index,
int id)
|
private void |
hashRemove(int id)
|
private void |
init()
|
boolean |
isUsed(int id)
|
protected int |
rawFindID(short obj,
int hash)
|
private void |
readObject(java.io.ObjectInputStream in)
|
void |
remove(int id)
|
void |
resetObject(int id,
short obj)
|
protected void |
restoreBeforeReadObject()
|
protected void |
set(int id,
short obj)
Access to the underlying idToObject array is
encapsulated through the set(int, short) and get(int)
methods. |
void |
setObject(int id,
short obj)
|
private void |
writeObject(java.io.ObjectOutputStream out)
For serializing an instance of this class, it has to be externalized, because weak references are not serializable. |
| Methods inherited from class uka.util.IDConstants |
isNew, isValid, normalizeID, toggleNew |
| Methods inherited from class java.lang.Object |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
| Field Detail |
protected static final int INVALID_INDEX
protected IDSpace space
protected EnlargingShortArray idToObject
private transient ShortHashFunction hashFunction
private transient int size
private transient int[] hashToID
private transient EnlargingIntArray chainID
private transient int hashCapacity
private transient int hashSize
| Constructor Detail |
public ShortIDMap()
public ShortIDMap(IDSpace space)
| Method Detail |
private static int computeHashCapacity(int hashSize)
private static int computeHashSize(int size)
private void init()
public int getSize()
public void clear()
public int getID(short obj)
public IntIterator getUsedIDs()
public ShortIterator getObjects()
public int findID(short obj)
private int findIDInternal(short obj,
int index)
protected final int rawFindID(short obj,
int hash)
public void dump()
public void setObject(int id,
short obj)
public void resetObject(int id,
short obj)
protected void set(int id,
short obj)
idToObject array is
encapsulated through the set(int, short) and get(int)
methods. Subclasses may use this hooks to add functionality
whenever an object is stored or retrieved.
public final boolean isUsed(int id)
public short getObject(int id)
protected short get(int id)
set(int, short)protected final int getHash(short obj)
hashToID array from the
hash value for the given object.
protected final int getIndexForHash(int hash)
protected int getIndexForID(int id)
private void growHashtable()
private void hashInsert(int index,
int id)
private void hashRemove(int id)
public void remove(int id)
private void writeObject(java.io.ObjectOutputStream out)
throws java.io.IOException
java.io.IOException
protected void restoreBeforeReadObject()
throws java.lang.ClassNotFoundException,
java.io.IOException
java.lang.ClassNotFoundException
java.io.IOException
private void readObject(java.io.ObjectInputStream in)
throws java.io.IOException,
java.lang.ClassNotFoundException
java.io.IOException
java.lang.ClassNotFoundExceptionpublic void appendTo(ToString s)
Printableappend the contents of each instance variable of the current
object to the given ToString object. The appended data
should be labeled with the name of the corresponding instance
variable.
appendTo in interface PrintableToString,
ToString.append(String, Object),
ToString.append(String, boolean),
ToString.append(String, byte),
ToString.append(String, int)
|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||