/** * Return TRUE if <code>self</code> has zero entries. * * @return boolean */ public boolean emptyP() { { StellaHashTable self = this; return (self.length() == 0); } }
/** * Remove all entries from <code>self</code>. This will result in a re-initialization of the table * upon the first insertion into <code>self</code>. */ public void clear() { { StellaHashTable self = this; self.theTable = null; self.size = Stella.NULL_INTEGER; self.freeElements = Stella.NULL_INTEGER; } }
public static void stellaHashTableRemoveAt(StellaHashTable self, Stella_Object key) { { KvCons[] table = self.theTable; int bucketindex = 0; KvCons bucket = null; boolean equaltestP = self.equalTestP; if (table == null) { return; } if (equaltestP) { bucketindex = (((key.equalHashCode()) & 0x7FFFFFFF) % (self.size)); } else { bucketindex = (((key.hashCode_()) & 0x7FFFFFFF) % (self.size)); } bucket = table[bucketindex]; if (bucket == null) { return; } else if ((!equaltestP) && Stella_Object.eqlP(bucket.key, key)) { table[bucketindex] = bucket.rest; self.freeElements = self.freeElements + 1; return; } else if (equaltestP && Stella_Object.equalP(bucket.key, key)) { table[bucketindex] = bucket.rest; self.freeElements = self.freeElements + 1; return; } else { { KvCons trailer = bucket; bucket = bucket.rest; while (bucket != null) { if ((!equaltestP) && Stella_Object.eqlP(bucket.key, key)) { trailer.rest = bucket.rest; self.freeElements = self.freeElements + 1; return; } else if (equaltestP && Stella_Object.equalP(bucket.key, key)) { trailer.rest = bucket.rest; self.freeElements = self.freeElements + 1; return; } else { trailer = bucket; bucket = bucket.rest; } } } } } }
/** * Lookup the entry identified by <code>key</code> in <code>self</code> and return its value, or * NULL if no such entry exists. Uses an <code>eqlP</code> test by default or <code>equalP</code> * if <code>equalTestP</code> of <code>self</code> is TRUE. * * @param key * @return Stella_Object */ public Stella_Object lookup(Stella_Object key) { { StellaHashTable self = this; return (StellaHashTable.stellaHashTableLookup(self, key)); } }
/** * Set the value of the entry identified by <code>key</code> in <code>self</code> to <code>value * </code> or add a new entry if no entry with <code>key</code> exists yet. Uses an <code>eqlP * </code> test by default or <code>equalP</code> if <code>equalTestP</code> of <code>self</code> * is TRUE. * * @param key * @param value */ public void insertAt(Stella_Object key, Stella_Object value) { { StellaHashTable self = this; StellaHashTable.stellaHashTableInsertAt(self, key, value); return; } }
/** * Remove the entry identified by <code>key</code> from <code>self</code>. Uses an <code>eqlP * </code> test by default or <code>equalP</code> if <code>equalTestP</code> of <code>self</code> * is TRUE. * * @param key */ public void removeAt(Stella_Object key) { { StellaHashTable self = this; StellaHashTable.stellaHashTableRemoveAt(self, key); return; } }
/** * Collect all entries of <code>self</code> into a cons list of <code>_LkeyGLvalueG_</code> pairs * and return the result. * * @return Cons */ public Cons consify() { { StellaHashTable self = this; { Cons result = Stella.NIL; { Stella_Object key = null; Stella_Object value = null; StellaHashTableIterator iter000 = ((StellaHashTableIterator) (self.allocateIterator())); Cons collect000 = null; while (iter000.nextP()) { key = iter000.key; value = iter000.value; if (collect000 == null) { { collect000 = Stella_Object.cons( Stella_Object.cons(key, Stella_Object.cons(value, Stella.NIL)), Stella.NIL); if (result == Stella.NIL) { result = collect000; } else { Cons.addConsToEndOfConsList(result, collect000); } } } else { { collect000.rest = Stella_Object.cons( Stella_Object.cons(key, Stella_Object.cons(value, Stella.NIL)), Stella.NIL); collect000 = collect000.rest; } } } } return (result); } } }
/** * Return TRUE if <code>x</code> and <code>y</code> represent the same set of key/value pairs. * * @param y * @return boolean */ public boolean objectEqualP(Stella_Object y) { { StellaHashTable x = this; if (Surrogate.subtypeOfP( Stella_Object.safePrimaryType(y), Stella.SGT_STELLA_STELLA_HASH_TABLE)) { { StellaHashTable y000 = ((StellaHashTable) (y)); if (x.length() == y000.length()) { { Stella_Object key = null; Stella_Object value = null; StellaHashTableIterator iter000 = ((StellaHashTableIterator) (x.allocateIterator())); while (iter000.nextP()) { key = iter000.key; value = iter000.value; if (!Stella_Object.equalP(value, y000.lookup(key))) { return (false); } } } return (true); } } } else { } return (false); } }
public static void initializeStellaHashTable(StellaHashTable self) { { int size = Stella.pickHashTableSizePrime( Native.floor(self.initialSize / Stella.$STELLA_HASH_TABLE_AVG_BUCKET_LENGTH$)); KvCons[] table = new KvCons[size]; { int i = Stella.NULL_INTEGER; int iter000 = 0; int upperBound000 = size - 1; for (; iter000 <= upperBound000; iter000 = iter000 + 1) { i = iter000; table[i] = null; } } self.theTable = table; self.size = size; self.freeElements = Native.floor(size * Stella.$STELLA_HASH_TABLE_AVG_BUCKET_LENGTH$); } }
public static StellaHashTable newStellaHashTable() { { StellaHashTable self = null; self = new StellaHashTable(); self.theStellaHashTable = null; self.theHashTable = null; self.equalTestP = false; self.freeElements = Stella.NULL_INTEGER; self.initialSize = 50; self.size = Stella.NULL_INTEGER; self.initializeHashTable(); return (self); } }
/** * Return an <code>equalP</code> hash code for <code>self</code>. Note that this is O(N) in the * number of entries of <code>self</code>. * * @return int */ public int equalHashCode() { { StellaHashTable self = this; { int code = 36273463; { Stella_Object key = null; Stella_Object value = null; StellaHashTableIterator iter000 = ((StellaHashTableIterator) (self.allocateIterator())); while (iter000.nextP()) { key = iter000.key; value = iter000.value; code = (code ^ (key.equalHashCode())); code = (code ^ (value.equalHashCode())); } } return (code); } } }
public static Stella_Object accessStellaHashTableSlotValue( StellaHashTable self, Symbol slotname, Stella_Object value, boolean setvalueP) { if (slotname == Stella.SYM_STELLA_SIZE) { if (setvalueP) { self.size = ((IntegerWrapper) (value)).wrapperValue; } else { value = IntegerWrapper.wrapInteger(self.size); } } else if (slotname == Stella.SYM_STELLA_INITIAL_SIZE) { if (setvalueP) { self.initialSize = ((IntegerWrapper) (value)).wrapperValue; } else { value = IntegerWrapper.wrapInteger(self.initialSize); } } else if (slotname == Stella.SYM_STELLA_FREE_ELEMENTS) { if (setvalueP) { self.freeElements = ((IntegerWrapper) (value)).wrapperValue; } else { value = IntegerWrapper.wrapInteger(self.freeElements); } } else if (slotname == Stella.SYM_STELLA_EQUAL_TESTp) { if (setvalueP) { self.equalTestP = BooleanWrapper.coerceWrappedBooleanToBoolean(((BooleanWrapper) (value))); } else { value = (self.equalTestP ? Stella.TRUE_WRAPPER : Stella.FALSE_WRAPPER); } } else { { OutputStringStream stream000 = OutputStringStream.newOutputStringStream(); stream000.nativeStream.print("`" + slotname + "' is not a valid case option"); throw ((StellaException) (StellaException.newStellaException(stream000.theStringReader()).fillInStackTrace())); } } return (value); }
/** * Return a copy of the hash table <code>self</code>. The bucket table and buckets are freshly * allocated, however, the keys and values of entries are not copied themselves (similar to what * we do for lists, etc.). * * @return StellaHashTable */ public StellaHashTable copy() { { StellaHashTable self = this; { int size = self.size; { StellaHashTable self000 = StellaHashTable.newStellaHashTable(); self000.size = size; self000.initialSize = self.initialSize; self000.freeElements = self.freeElements; self000.equalTestP = self.equalTestP; { StellaHashTable copy = self000; KvCons[] table = self.theTable; KvCons[] tablecopy = table; KvCons bucket = null; if (table != null) { tablecopy = new KvCons[size]; copy.theTable = tablecopy; { int i = Stella.NULL_INTEGER; int iter000 = 0; int upperBound000 = size - 1; for (; iter000 <= upperBound000; iter000 = iter000 + 1) { i = iter000; bucket = table[i]; if (bucket != null) { tablecopy[i] = KvCons.copyKvConsList(bucket); } else { tablecopy[i] = null; } } } } return (copy); } } } } }
public static void rehashStellaHashTable(StellaHashTable self, int newsize) { if (self.theTable == null) { StellaHashTable.initializeStellaHashTable(self); return; } { int size = self.size; KvCons[] table = self.theTable; KvCons[] newtable = new KvCons[newsize]; int newbucketindex = 0; KvCons newbucket = null; KvCons cursor = null; KvCons current = null; boolean equaltestP = self.equalTestP; { int i = Stella.NULL_INTEGER; int iter000 = 0; int upperBound000 = newsize - 1; for (; iter000 <= upperBound000; iter000 = iter000 + 1) { i = iter000; newtable[i] = null; } } { int i = Stella.NULL_INTEGER; int iter001 = 0; int upperBound001 = size - 1; for (; iter001 <= upperBound001; iter001 = iter001 + 1) { i = iter001; cursor = table[i]; while (cursor != null) { if (equaltestP) { newbucketindex = (((cursor.key.equalHashCode()) & 0x7FFFFFFF) % newsize); } else { newbucketindex = (((cursor.key.hashCode_()) & 0x7FFFFFFF) % newsize); } newbucket = newtable[newbucketindex]; current = cursor; cursor = cursor.rest; if (newbucket != null) { current.rest = newbucket.rest; newbucket.rest = current; } else { newtable[newbucketindex] = current; current.rest = null; } } } } self.theTable = newtable; self.size = newsize; self.freeElements = Stella.max( self.freeElements + Native.floor((newsize - size) * Stella.$STELLA_HASH_TABLE_AVG_BUCKET_LENGTH$), 0); } }
public static void stellaHashTableInsertAt( StellaHashTable self, Stella_Object key, Stella_Object value) { { KvCons[] table = self.theTable; int free = self.freeElements; int bucketindex = 0; KvCons bucket = null; boolean equaltestP = self.equalTestP; if (table == null) { StellaHashTable.initializeStellaHashTable(self); table = self.theTable; free = self.freeElements; } if (free == 0) { StellaHashTable.rehashStellaHashTable(self, Stella.pickHashTableSizePrime(self.size + 1)); table = self.theTable; free = self.freeElements; } if (equaltestP) { bucketindex = (((key.equalHashCode()) & 0x7FFFFFFF) % (self.size)); } else { bucketindex = (((key.hashCode_()) & 0x7FFFFFFF) % (self.size)); } bucket = table[bucketindex]; if (bucket == null) { { KvCons self000 = KvCons.newKvCons(); self000.key = key; self000.value = value; table[bucketindex] = self000; } self.freeElements = free - 1; } else { { KvCons cursor = bucket; if (equaltestP) { while ((cursor != null) && (!Stella_Object.equalP(cursor.key, key))) { cursor = cursor.rest; } } else { while ((cursor != null) && (!Stella_Object.eqlP(cursor.key, key))) { cursor = cursor.rest; } } if (cursor != null) { cursor.value = value; } else { { KvCons self001 = KvCons.newKvCons(); self001.key = key; self001.value = value; self001.rest = bucket.rest; bucket.rest = self001; } self.freeElements = free - 1; } } } } }