adrian_nicholl |
I have a Unit written out, but how the frigg do i get it to run? Its a pasfile but pascal doesnt know what to do with it at all, and i dont know how to make delphi make it work.
I tried opening a new project and adding a unit, and then renaming the code deplhi added for me, but it says cant find the file 'hashtable.dcu'
below is the unit code, i am sure its real simple how to get it to run but i cant find anything on google about it
quote: |
unit HashTable;
///////////////////////////////
// "White Box" Hash Table 1 //
///////////////////////////////
(* This unit implements a hash table using *
* open addressing and double hashing. *
* It is designed to store URLs *)
interface
const
tableSize = 893;
type
HTable = array [0..tableSize-1] of String;
function hash(str: String): integer;
// Computes the hash function
function hash2(str: String): integer;
// Computes the secondary hash function
procedure initialise(var h: HTable);
// pre: TRUE
// post: h is initialised to the empty hash table,
// (i.e. every string in the array is an empty string)
function search(h: HTable; searchurl: String): boolean;
// pre: searchurl<>''
// post: the result is true if and only if the given string
// searchurl is stored in the hash table h
procedure insert(var h: HTable; url: String);
// pre: url<>''
// post: If there is still an empty slot in the table
// (ie the hash table is not full) then the url is
// (if it isn't in the table already) inserted into
// an empty slot in the table in accordance with the double
// hashing scheme, leaving all other strings in the table
// unchanged.
// If there is no empty slot in the table (i.e. the table
// is full), then the table is unchanged and an error message
// 'Error: table full.' is printed to the console window.
////////////////////////////////////////////////////////////////////
implementation
var opCount: integer;
function hash(str: String): integer;
// Computes the hash function
var count, i: integer;
begin
count := 0;
for i := 0 to length(str)-1 do
count := (count + ord(str[i])) mod tableSize;
hash := count
end;
function hash2(str: String): integer;
// Computes the secondary hash function
const primesArray : array [0..5] of integer
= (1289, 197, 563, 223, 373, 1637); // using these primes to
// multiply the chr numbers by
// mixes up the hash2 function,
// making it more different from
// the hash value itself
var count, i: integer;
begin
count := 0;
for i := 0 to length(str)-1 do
count := (count + (primesArray[i mod 6]*ord(str[i]))) mod (tableSize - 1);
hash2 := count + 1
end;
procedure initialise(var h: HTable);
// pre: TRUE
// post: h is initialised to the empty hash table,
// i.e. every string in the array is an empty string
var i: integer;
begin
if opCount=0 then
for i := 0 to tableSize-1 do
h[i] := ''
end; // initialise
function search(h: HTable; searchurl: String): boolean;
// pre: searchurl<>''
// post: the result is true if and only if the given string
// searchurl is stored in the hash table h
var count, initial, current, h2: integer;
begin
inc(opCount);
initial := hash(searchurl);
h2 := hash2(searchurl);
current := initial;
count := 0;
while (count
and (h[current]<>'') // not empty, keep searching
and (h[current]<>searchurl) // not what we're looking for
do
begin
inc(count);
current := (current+h2) mod tableSize;
end;
search := (h[current]=searchurl);
end;
procedure insert(var h: HTable; url: String);
// pre: url<>''
// post: If the given url string is in the table already, then
// the table is unchanged by the insert procedure.
// Otherwise, if there is still an empty slot in the table
// (ie the hash table is not full) then the url is
// inserted into an empty slot in the table in accordance with
// the double hashing scheme, leaving all other strings in the
// table unchanged.
// If there is no empty slot in the table to insert the
// url string in (i.e. the table is full), then the table is
// left unchanged and an error message
// 'Error: table full.' is printed to the console window.
var count, initial, current, h2: integer;
begin
inc(opCount);
initial := hash(url);
h2 := hash2(url);
current := initial;
count := 0;
while (count
and (h[current]<>'') // not empty, keep going
and (h[current]<>url) // not what we're looking for
do
begin
inc(count);
current := (current+h2) mod tableSize;
end;
if count>=tableSize then // table is full
writeln('Error: table full.')
else if h[current]='' then // can insert here
h[current] := url
// else it must be the case that (h[current]=url) so do nothing
end;
begin
opCount := 0
end.
|
|
|
|