home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD1.mdf / pascal / library / dos / nicol / sti_hash / sti_smbt.pas < prev    next >
Encoding:
Pascal/Delphi Source File  |  1979-12-31  |  4.7 KB  |  107 lines

  1. (***************************************************************************)
  2. (*                                                                         *)
  3. (*                             HASH1.PAS                                   *)
  4. (*        Copyright 1990,1991 By Software Technology International         *)
  5. (*                                                                         *)
  6. (*  This unit shows how, by interfacing to the STIHASH unit, you can make  *)
  7. (*  a unit that creates a generic symbol table. This example uses only a   *)
  8. (*  string based hash table, but any data type can be used.                *)
  9. (*                                                                         *)
  10. (*                                                                         *)
  11. (***************************************************************************)
  12.  
  13. Unit STI_SMBT;
  14. {$R-,S-,V-}
  15.  
  16. Interface
  17.  
  18. Uses STIHash;                               {the generic hash table         }
  19.  
  20. Type
  21.   DataRec = Record                          {a sample data record           }
  22.               Symb : String;                {has a symbol                   }
  23.               Addr : Word;                  {symbol address in source       }
  24.               Line : Word;                  {line number in source          }
  25.             End;
  26.  
  27.   KeyRec  = string;                         {the key type : string          }
  28.  
  29. Var
  30.   SymbolTable : HashtPtr;                   {this is the hash table         }
  31.  
  32. Procedure SymbolTableCreate;
  33. Procedure SymbolTableEnter (TheKey : KeyRec; D : DataRec;
  34.                                   Var Duplicate : Boolean);
  35. Procedure SymbolTableRetrieve (Key : KeyRec; Var Found : Boolean;
  36.                                  Var D : DataRec);
  37. procedure SymbolTableDestroy;
  38.  
  39. Implementation
  40.  
  41. {---------------------------------------------------------------------------}
  42.  
  43. Procedure SymbolTableCreate;                {creates a hash table           }
  44.  
  45. Begin
  46.   New(SymbolTable);                         {get the new symbol table       }
  47.   HTable := SymbolTable;                    {set the generic table to this  }
  48.   STI_HashTableCreate;                      {create the table               }
  49. End;
  50.  
  51. {---------------------------------------------------------------------------}
  52.  
  53. Procedure SymbolTableEnter (TheKey : KeyRec; D : DataRec;
  54.                                   Var Duplicate : Boolean);
  55.  
  56. Var
  57.   Temp : EntryRec;                          {dummy entry into the table     }
  58.  
  59. Begin
  60.   STI_EntryCreate(Temp);                    {create the entry               }
  61.   STI_EntryInit(Temp,sizeof(DataRec),       {initialise thr entry           }
  62.                     sizeof(KeyRec));        {must assign all the data 256b  }
  63.   STI_EntrySet_Data(Temp,D,sizeof(D));      {set the data up in the entry   }
  64.   STI_EntrySet_Key(Temp,TheKey,ord(TheKey[0])+1); {get the key              }
  65.                                             {uses length of string for speed}
  66.   STI_HashTableEnter(Temp,ord(TheKey[0])+1,Duplicate);
  67.                                             {enter it into the table        }
  68.                                             {again, use the length to avoid }
  69.                                             {hashing 256 bytes : slow       }
  70.   if Duplicate then                         {check for a duplicate          }
  71.     begin                                   {if it is, then destroy the     }
  72.       STI_EntryDestroy(Temp,sizeof(KeyRec),sizeof(DataRec)); {entry else    }
  73.     end;                                    {have all the memory eaten !!!  }
  74. End;
  75.  
  76. {---------------------------------------------------------------------------}
  77.  
  78. Procedure SymbolTableRetrieve (Key : KeyRec; Var Found : Boolean;
  79.                                   Var D : DataRec);
  80.  
  81. Var
  82.   TheKey,                                   {the key : uses an entry        }
  83.   Temp : EntryRec;                          {dummy entry                    }
  84.  
  85. Begin
  86.   STI_EntrySet_Key(TheKey,Key,ord(Key[0])+1);   {get the key                }
  87.                                             {size is length for speed       }
  88.   STI_HashTableRetrieve(TheKey,ord(Key[0])+1,Found,Temp);
  89.                                             {find the entry                 }
  90.                                             {size is length for speed       }
  91.   If Found Then
  92.     STI_EntryGet_Data(Temp,D,sizeof(D));    {copy over the data             }
  93. End;
  94.  
  95. {---------------------------------------------------------------------------}
  96.  
  97. procedure SymbolTableDestroy;
  98.  
  99. begin
  100.   STI_HashTableDestroy(sizeof(KeyRec),Sizeof(DataRec));
  101.   dispose(SymbolTable);
  102. end;
  103.  
  104. {---------------------------------------------------------------------------}
  105.  
  106. begin
  107. end.