unit hash;
interface
  const max_ilg = 128;
  type pagr_string = string[max_ilg];
       sarasas = ^elem;
       elem = record
                nr : word;
                st : pagr_string;
                kitas : sarasas;
              end;
       hash_lentele = array [1..max_ilg] of sarasas;
  procedure hash_paruosk (var hl : hash_lentele);
  {paruo÷ia "hash" lentelñ darbui}
  procedure hash_isvalyk (var hl : hash_lentele);
  {atlaisvina "hash" lentelós uýimamÝ atmintõ}
  procedure hash_pridek (var hl : hash_lentele; st : string; nr : word);
  {õ "hash" lentelñ õtraukia naujÝ elementÝ}
  function hash_numeris (var hl : hash_lentele; st : string) : integer;
  {graýina õra÷o i÷ "hash" lentelós numerõ, pagal õra÷e saugomÝ informacijÝ}
implementation
  procedure hash_paruosk (var hl : hash_lentele);
    var i : byte;
  begin {hash_paruosk}
    for i := 1 to max_ilg do
      hl[i] := nil;
  end; {hash_paruosk}
  procedure hash_isvalyk (var hl : hash_lentele);
    var tmp : sarasas;
        i : byte;
  begin {hash_isvalyk}
    for i := 1 to max_ilg do
      while hl[i] <> nil do
        begin
          tmp := hl[i];
          hl[i] := hl[i]^.kitas;
          dispose (tmp);
        end;
  end; {hash_isvalyk}
  procedure hash_pridek (var hl : hash_lentele; st : string; nr : word);
    var tmp, tmp1 : sarasas;
        ilg : byte;
  begin {hash_pridek}
    if memavail <= sizeof (elem)
      then exit;
    ilg := length (st);
    if ilg > max_ilg
      then ilg := 1;
    if hl[ilg] = nil
      then
        begin
          new (hl[ilg]);
          hl[ilg]^.st := st;
          hl[ilg]^.nr := nr;
          hl[ilg]^.kitas := nil;
        end
      else if st < hl[ilg]^.st
        then
          begin
            new (tmp);
            tmp^.st := st;
            tmp^.nr := nr;
            tmp^.kitas := hl[ilg];
            hl[ilg] := tmp;
          end
        else
          begin
            tmp := hl[ilg];
            while (tmp^.kitas <> nil) and (st > tmp^.kitas^.st) do
              tmp := tmp^.kitas;
            tmp1 := tmp^.kitas;
            new (tmp^.kitas);
            tmp := tmp^.kitas;
            tmp^.st := st;
            tmp^.nr := nr;
            tmp^.kitas := tmp1;
          end;
  end; {hash_pridek}
  function hash_numeris (var hl : hash_lentele; st : string) : integer;
    var tmp : sarasas;
        nr : integer;
        ilg : byte;
  begin {hash_numeris}
    nr := -1;
    ilg := length (st);
    if ilg > max_ilg
      then ilg := 1;
    tmp := hl[ilg];
    while (tmp <> nil) and (st > tmp^.st) do
      tmp := tmp^.kitas;
    if st = tmp^.st
      then nr := tmp^.nr;
    hash_numeris := nr;
  end; {hash_numeris}
end.