IntHashTableImpl.Mesa
Adapted from RefTabImpl.mesa by Bertrand Serlet, July 24, 1985 4:29:40 pm PDT
Last Edited by Bertrand Serlet, August 12, 1985 7:06:47 pm PDT
Last Edited by Widom, July 25, 1985 4:46:10 pm PDT
Spreitzer, February 6, 1986 6:04:57 pm PST
DIRECTORY IntHashTable;
IntHashTableImpl: CEDAR MONITOR LOCKS table USING table: Table
EXPORTS IntHashTable = {
OPEN IntHashTable;
Public operations
Create: PUBLIC PROC [mod: SeqIndex ← 17, class: Class] RETURNS [Table] = {
mod ← MAX[mod, minMod];
RETURN [NEW [TableRec ← [
class: class,
size: 0,
sizeLimit: mod,
inhibitCount: 0,
data: NEW [Seq[mod]]
]]];
};
minMod: SeqIndex = 2;
GetSize: PUBLIC PROC [table: Table] RETURNS [INT] = {
RETURN [table.size];
};
ComputeIndex: PROC [table: Table, key: Key] RETURNS [index: SeqIndex] = INLINE {
key ← key MOD table.data.max;
index ← IF key < 0 THEN key + table.data.max ELSE key;
};
Match: PROC [table: Table, key: Key, node: Value] RETURNS [match: BOOL] = INLINE {match ← key = table.class.GetKey[node]};
Fetch: PUBLIC ENTRY PROC [table: Table, key: Key] RETURNS [found: BOOL, value: Value] = {
OPEN table.class;
ENABLE UNWIND => NULL;
hash: SeqIndex ← ComputeIndex[table, key];
node: Value ← table.data[hash];
WHILE node # NIL DO
IF Match[table, key, node] THEN RETURN [TRUE, node];
node ← GetLink[node];
ENDLOOP;
RETURN [FALSE, NIL];
};
Replace: PUBLIC ENTRY PROC [table: Table, key: Key, value: Value] RETURNS [BOOL] = {
OPEN table.class;
ENABLE UNWIND => NULL;
hash: SeqIndex ← ComputeIndex[table, key];
node: Value ← table.data[hash];
prev: Value ← NIL;
WHILE node # NIL DO
IF Match[table, key, node] THEN {
SetLink[value, GetLink[node]];
IF prev = NIL THEN table.data[hash] ← value ELSE SetLink[prev, value];
RETURN [FALSE]};
prev ← node;
node ← GetLink[node];
ENDLOOP;
RETURN [FALSE];
};
Store: PUBLIC ENTRY PROC [table: Table, key: Key, value: Value] RETURNS [BOOL] = {
OPEN table.class;
ENABLE UNWIND => NULL;
hash: SeqIndex ← ComputeIndex[table, key];
node: Value ← table.data[hash];
prev: Value ← NIL;
WHILE node # NIL DO
IF Match[table, key, node] THEN {
SetLink[value, GetLink[node]];
IF prev = NIL THEN table.data[hash] ← value ELSE SetLink[prev, value];
RETURN [FALSE]};
prev ← node;
node ← GetLink[node];
ENDLOOP;
SetLink[value, table.data[hash]];
table.data[hash] ← value;
IF (table.size ← table.size + 1) > table.sizeLimit AND table.inhibitCount = 0 THEN ReHash[table];
RETURN [TRUE];
};
Insert: PUBLIC ENTRY PROC [table: Table, key: Key, value: Value] RETURNS [BOOL] = {
OPEN table.class;
ENABLE UNWIND => NULL;
hash: SeqIndex ← ComputeIndex[table, key];
node: Value ← table.data[hash];
WHILE node # NIL DO
IF Match[table, key, node] THEN RETURN [FALSE];
node ← GetLink[node];
ENDLOOP;
SetLink[value, table.data[hash]];
table.data[hash] ← value;
IF (table.size ← table.size + 1) > table.sizeLimit AND table.inhibitCount = 0 THEN ReHash[table];
RETURN [TRUE];
};
Delete: PUBLIC ENTRY PROC [table: Table, key: Key] RETURNS [BOOL] = {
OPEN table.class;
ENABLE UNWIND => NULL;
hash: SeqIndex ← ComputeIndex[table, key];
node: Value ← table.data[hash];
lag: Value ← NIL;
WHILE node # NIL DO
IF Match[table, key, node] THEN {
IF lag = NIL THEN table.data[hash] ← GetLink[node] ELSE SetLink[lag, GetLink[node]];
table.size ← table.size - 1;
RETURN [TRUE];
};
lag ← node;
node ← GetLink[node];
ENDLOOP;
RETURN [FALSE];
};
Pairs: PUBLIC PROC [table: Table, action: EachPairAction] RETURNS [quit: BOOL] = {
OPEN table.class;
node: Value ← NIL;
DInhibit[table, +1];
{ENABLE UNWIND => DInhibit[table, -1];
index: CARDINAL ← table.data.max;
DO
[node, index] ← GetNext[table, node, index];
IF node = NIL THEN {quit ← FALSE; EXIT};
IF action[GetKey[node], node] THEN {quit ← TRUE; EXIT};
ENDLOOP;
};
DInhibit[table, -1];
};
DInhibit: ENTRY PROC [table: Table, D: INT] = {
table.inhibitCount ← table.inhibitCount + D;
WHILE table.inhibitCount = 0 AND table.size > table.sizeLimit DO ReHash[table] ENDLOOP;
};
GetNext: ENTRY PROC [table: Table, node: Value, index: CARDINAL] RETURNS [Value, CARDINAL] = {
OPEN table.class;
ENABLE UNWIND => NULL;
IF node # NIL THEN {
node ← GetLink[node];
IF node # NIL THEN RETURN [node, index];
};
WHILE index > 0 DO
index ← index - 1;
node ← table.data[index];
IF node # NIL THEN RETURN [node, index];
ENDLOOP;
RETURN [NIL, 0];
};
Erase: PUBLIC ENTRY PROC [table: Table] = {
FOR i: CARDINAL IN [0..table.data.max) DO table.data[i] ← NIL ENDLOOP;
table.size ← 0;
};
ReHash: INTERNAL PROC [table: Table] = {
OPEN table.class;
oldData: REF Seq = table.data;
newData: REF Seq;
seek: CARDINAL = table.data.max * 2;
newPTI, newMod: CARDINAL ← 0;
IF primeTable[PrimeTableSize-1] > LAST[SeqIndex] THEN ERROR;
FOR newPTI ← 0, newPTI+1 WHILE newPTI+1 < PrimeTableSize AND primeTable[newPTI] < seek DO NULL ENDLOOP;
newMod ← primeTable[newPTI];
IF newMod = table.data.max THEN {table.sizeLimit ← LAST[INT]; RETURN};
table.sizeLimit ← newMod;
table.data ← newData ← NEW [Seq[newMod]];
FOR i: NAT IN [0 .. oldData.max) DO
next: Value ← NIL;
FOR cur: Value ← oldData[i], next WHILE cur # NIL DO
hash: SeqIndex ← ComputeIndex[table, GetKey[cur]];
next ← GetLink[cur];
SetLink[cur, newData[hash]];
newData[hash] ← cur;
ENDLOOP;
IF next # NIL THEN ERROR;
ENDLOOP;
table ← table;
};
PrimeTableSize: NAT = 14;
primeTable: ARRAY [0 .. PrimeTableSize) OF CARDINAL = [
00002, 00005, 00011, 00023, 00053, 00113, 00251, 00509, 01019, 02039, 04079, 08179, 16369, 32749];
}.