home *** CD-ROM | disk | FTP | other *** search
- (***************************************************************************)
- (* *)
- (* HASH1.PAS *)
- (* Copyright 1990,1991 By Software Technology International *)
- (* *)
- (* This unit shows how, by interfacing to the STIHASH unit, you can make *)
- (* a unit that creates a generic symbol table. This example uses only a *)
- (* string based hash table, but any data type can be used. *)
- (* *)
- (* *)
- (***************************************************************************)
-
- Unit STI_SMBT;
- {$R-,S-,V-}
-
- Interface
-
- Uses STIHash; {the generic hash table }
-
- Type
- DataRec = Record {a sample data record }
- Symb : String; {has a symbol }
- Addr : Word; {symbol address in source }
- Line : Word; {line number in source }
- End;
-
- KeyRec = string; {the key type : string }
-
- Var
- SymbolTable : HashtPtr; {this is the hash table }
-
- Procedure SymbolTableCreate;
- Procedure SymbolTableEnter (TheKey : KeyRec; D : DataRec;
- Var Duplicate : Boolean);
- Procedure SymbolTableRetrieve (Key : KeyRec; Var Found : Boolean;
- Var D : DataRec);
- procedure SymbolTableDestroy;
-
- Implementation
-
- {---------------------------------------------------------------------------}
-
- Procedure SymbolTableCreate; {creates a hash table }
-
- Begin
- New(SymbolTable); {get the new symbol table }
- HTable := SymbolTable; {set the generic table to this }
- STI_HashTableCreate; {create the table }
- End;
-
- {---------------------------------------------------------------------------}
-
- Procedure SymbolTableEnter (TheKey : KeyRec; D : DataRec;
- Var Duplicate : Boolean);
-
- Var
- Temp : EntryRec; {dummy entry into the table }
-
- Begin
- STI_EntryCreate(Temp); {create the entry }
- STI_EntryInit(Temp,sizeof(DataRec), {initialise thr entry }
- sizeof(KeyRec)); {must assign all the data 256b }
- STI_EntrySet_Data(Temp,D,sizeof(D)); {set the data up in the entry }
- STI_EntrySet_Key(Temp,TheKey,ord(TheKey[0])+1); {get the key }
- {uses length of string for speed}
- STI_HashTableEnter(Temp,ord(TheKey[0])+1,Duplicate);
- {enter it into the table }
- {again, use the length to avoid }
- {hashing 256 bytes : slow }
- if Duplicate then {check for a duplicate }
- begin {if it is, then destroy the }
- STI_EntryDestroy(Temp,sizeof(KeyRec),sizeof(DataRec)); {entry else }
- end; {have all the memory eaten !!! }
- End;
-
- {---------------------------------------------------------------------------}
-
- Procedure SymbolTableRetrieve (Key : KeyRec; Var Found : Boolean;
- Var D : DataRec);
-
- Var
- TheKey, {the key : uses an entry }
- Temp : EntryRec; {dummy entry }
-
- Begin
- STI_EntrySet_Key(TheKey,Key,ord(Key[0])+1); {get the key }
- {size is length for speed }
- STI_HashTableRetrieve(TheKey,ord(Key[0])+1,Found,Temp);
- {find the entry }
- {size is length for speed }
- If Found Then
- STI_EntryGet_Data(Temp,D,sizeof(D)); {copy over the data }
- End;
-
- {---------------------------------------------------------------------------}
-
- procedure SymbolTableDestroy;
-
- begin
- STI_HashTableDestroy(sizeof(KeyRec),Sizeof(DataRec));
- dispose(SymbolTable);
- end;
-
- {---------------------------------------------------------------------------}
-
- begin
- end.