program MergeHashFiles(HashFile, NewHashFile, input, output);

const
  maxwd    = 20;
  hashsize = 2003;
  blank    = ' ';

type
  word      = packed array[1..maxwd] of char;
  reference = record
                wd: word;
                pg: integer
              end;
  hashentry = 1..hashsize;

var
  HashFile,
  NewHashFile:           text;
  blankword:             word;
  i:  integer;

procedure UpdateHashFile;
{reads in old hash table, inserts file of new entries; writes out to HashFile}
{Updated version, uses hash tables of reference (not word) and hash files of
 type text (not word).}
var
  hash:  array[hashentry] of reference;
  x:     hashentry;
  NewFile,
  w:     word;

  function HashAddress(w: word): hashentry;
  {calculates the location in hash table of word w, or, if not there,
   returns pointing to the blank word where w should go}

  var
    x,                            {calculated location}
    inc:     integer;             {increment for open addressing}
  begin                           {function HashAddress}
    x := abs(ord(w[1])*ord(w[2])+ord(w[4])+ord(w[6])) mod hashsize + 1;
{Hash function assumes long word length. For short word machines
 we must ensure that the result is non-negative, and worry about overflow.}

    if (hash[x].wd <> w) and (hash[x].wd <> blankword) then
      begin
        inc   := (abs(ord(w[3])-95) mod 29);
                  {A key dependent increment is used to avoid clustering.}
        repeat
          inc := inc + 1;
          if inc > hashsize then
            writeln(w,' causes hash table to become full, infinite loop.');
          x := x + inc;
          if x > hashsize then x := x - hashsize;
        until (w =  hash[x].wd)  or  (blankword = hash[x].wd)
      end;
    HashAddress := x
  end;                            {function HashAddress}


begin                               {procedure UpdateHashFile}
  reset(HashFile);
  if eof(HashFile) then         {Error handler}
    for x := 1 to hashsize do   {HashFile is empty; create new table.}
      begin
        hash[x].wd := blankword;
        hash[x].pg := 0
      end
  else
    for x := 1 to hashsize do
      begin
        read(HashFile, hash[x].pg);
        while (HashFile^ = ' ') and not eoln(HashFile) do
          get(HashFile);
        readln(HashFile, hash[x].wd)
      end;

  reset(NewHashFile);
  while not eof(NewHashFile) do
  begin
    readln(NewHashFile, w);
    hash[HashAddress(w)].wd := w
{An identical hash function as used in phase 1 will be used.
 Overflow is a possibility.}
  end;

  close(HashFile);
{ In some system it may be necessary to close file HashFile at this point.}

  write('File name for updated hashfile?');
  readln(NewFile);
  open(HashFile, NewFile, new);
  rewrite(HashFile);

  for x := 1 to hashsize do
    with hash[x] do
    begin
      write(HashFile, pg:4, ' ');
      if wd <> blankword then
        write(HashFile, wd);
      writeln(HashFile)
    end
end;                                {procedure UpdateHashFile}

begin                    {main program MergeHashFiles}
  for i := 1 to maxwd do
    blankword[i] := blank;
  UpdateHashFile;
end.                     {main program MergeHashFiles}
