IntToIntMapperpublic class IntToIntMapper extends Object This class implements a mapping int --> int.
(Analogs: vector, hash table, etc.) The implementation uses two arrays:
one for keys and one for values.
The methods are analogous to those of the java.util.Vector class. |
Fields Summary |
---|
protected int[] | elementDataThe array buffer into which the values to be returned are
stored. The capacity of the mapper is the length of this array buffer. | protected int[] | elementKeyThe array buffer into which the values used as keys are
stored. The capacity of the mapper is the length of this array buffer. | protected int | elementCountThe number of valid components in the mapper. | protected int | capacityIncrementThe amount by which the capacity is automatically
incremented when its size becomes greater than its capacity. If
the capacity increment is 0 , the capacity
is doubled each time it needs to grow. | public int | defaultValueThe value returned if no value is associated with the key
searched for. |
Constructors Summary |
---|
public IntToIntMapper(int initialCapacity, int defaultElement, int capacityIncrement)Constructs an empty mapper with the specified initial capacity and
capacity increment.
super();
if (initialCapacity < 0) {
throw new IllegalArgumentException();
}
this.elementData = new int[initialCapacity];
this.elementKey = new int[initialCapacity];
this.capacityIncrement = capacityIncrement;
this.defaultValue = defaultElement;
|
Methods Summary |
---|
private void | addElement(int obj, int key)Adds the specified component to the end of this mapper,
increasing its size by one. The capacity of this mapper is
increased if its size becomes greater than its capacity.
int newcount = elementCount + 1;
if (newcount > elementData.length) {
ensureCapacityHelper(newcount);
}
elementKey[elementCount] = key;
elementData[elementCount++] = obj;
| public synchronized int | elementAt(int key)Returns the component at the specified key.
for (int i = 0; i < elementCount; i++) {
if (key == elementKey[i]) {
return elementData[i];
}
}
return defaultValue;
| private void | ensureCapacityHelper(int minCapacity)This implements the unsynchronized semantics of ensureCapacity.
Synchronized methods in this class can internally call this
method for ensuring capacity without incurring the cost of an
extra synchronization.
This function increases the size of the mapper according to the
value of capacityIncrement, and makes sure that the new size
is not less than minCapacity.
int oldCapacity = elementData.length;
int oldData[] = elementData;
int oldKey[] = elementKey;
int newCapacity = (capacityIncrement > 0) ?
(oldCapacity + capacityIncrement) : (oldCapacity * 2);
if (newCapacity < minCapacity) {
newCapacity = minCapacity;
}
elementData = new int[newCapacity];
elementKey = new int[newCapacity];
System.arraycopy(oldData, 0, elementData, 0, elementCount);
System.arraycopy(oldKey, 0, elementKey, 0, elementCount);
| public boolean | isEmpty()Tests if this mapper has no components.
return elementCount == 0;
| public synchronized void | removeElementAt(int key)Deletes the component at the specified key. Nothing happens if
no component has been associated with the key.
final int nowhere = -1;
int where = nowhere;
for (int i = 0; i < elementCount; i++)
{
if (key == elementKey[i]) {
where = i;
break;
}
}
if (where == nowhere) {
return;
}
// breaking the order :(
if (where < elementCount--)
{
elementKey[where] = elementKey[elementCount];
elementData[where] = elementData[elementCount];
}
| public synchronized void | setElementAt(int obj, int key)Sets the component at the specified key of this
mapper to be the specified value. The previous component at that
position is discarded.
for (int i = 0; i < elementCount; i++)
if (key == elementKey[i]) {
elementData[i] = obj;
return;
}
addElement(obj, key);
| public int | size()Returns the number of components in this mapper.
return elementCount;
|
|