This class does not provide thread-safeness, for the sake of * efficiency, again it should be used with care ! * @author Antonio Ramírez */ public class ArrayDictionary extends Dictionary implements Cloneable { /** The array of keys */ protected Object[] keys ; /** The array of corresponding values */ protected Object[] values ; /** How many real elements are in */ protected int nelems ; /** By how much to grow */ protected int incr ; /** * Create an ArrayDictionary using default values for initial size and * increment. */ public ArrayDictionary() { this(10,10) ; } /** * Create an ArrayDictionary using the given initial size. * (The increment is set to the same value). * @param init The initial size */ public ArrayDictionary(int init) { this(init,init) ; } /** * Clone this array dictionary. *
As for hashtables, a shallow copy is made, the keys and elements
* themselves are not cloned.
* @return The clone.
*/
public Object clone() {
try {
ArrayDictionary cl = (ArrayDictionary) super.clone();
cl.values = new Object[values.length];
System.arraycopy(values, 0, cl.values, 0, values.length);
cl.keys = new Object[values.length];
System.arraycopy(keys, 0, cl.keys, 0, keys.length);
return cl;
} catch (CloneNotSupportedException ex) {
throw new InternalError();
}
}
/**
* Create an ArrayDictionary using the given initial size and
* the given increment for growing the array.
* @param init the initial size
* @param incr the increment
*/
public ArrayDictionary(int init, int incr) {
keys = new Object[init] ;
values = new Object[init] ;
this.incr = incr ;
nelems = 0 ;
}
/**
* Create an ArrayDictionary, contructing the arrays of keys and
* values from the two given vectors.
* The two vectors should have the same size.
* The increment is set to the number of elements.
* @param keys the vector of keys
* @param values the vector of values
*/
public ArrayDictionary(Vector keys,Vector values) {
this(keys,values,values.size()) ;
}
/**
* Create an ArrayDictionary, contructing the arrays of keys and
* values from the two given vectors.
* The two vectors should have the same size.
* @param keys the vector of keys
* @param values the vector of values
* @param incr the increment for growing the arrays
*/
public ArrayDictionary(Vector keys, Vector values, int incr) {
this.incr = incr ;
nelems = keys.size() ;
this.keys = new Object[nelems] ;
this.values = new Object[nelems] ;
keys.copyInto(this.keys) ;
values.copyInto(this.values) ;
}
/**
* Create an ArrayDicitonary, using (not copying) the given pair
* of arrays as keys and values. The increment is set to the length of the
* arrays.
* @param keys the array of keys
* @param values the array of values
*/
public ArrayDictionary(Object[] keys, Object[] values) {
this(keys,values,values.length) ;
}
/**
* Create an ArrayDicitonary, using (not copying) the given pair
* of arrays as keys and values.
* @param keys the array of keys
* @param values the array of values
* @param incr the increment for growing the arrays
*/
public ArrayDictionary(Object[] keys, Object[] values, int incr) {
this.incr = incr ;
nelems = keys.length ;
this.keys = keys ;
this.values = values ;
}
protected final void grow() {
grow(keys.length+incr) ;
}
protected void grow(int newCapacity) {
Object[] newKeys = new Object[newCapacity] ;
Object[] newVals = new Object[newCapacity] ;
System.arraycopy(keys,0,newKeys,0,keys.length) ;
System.arraycopy(values,0,newVals,0,values.length) ;
keys = newKeys ;
values = newVals ;
}
/**
* Returns an enumeration of the elements of the dictionary.
* @return the enumeration
*/
public Enumeration elements() {
return new ArrayEnumeration(values,nelems) ;
}
/**
* Returns the value that maps to the given key.
* @param key the key
* @return the value
*/
public Object get(Object key) {
int n,i;
for(i=0,n=0;i