program brent (input, output); const tabsize = 7; tabsizeless1 = tabsize - 1; type typekey = integer; dataarray = array[0..tabsizeless1] of typekey; var hash: dataarray; n: integer; function hashfunction (key: typekey): integer; begin hashfunction := key mod tabsize end; function increment (key: typekey): integer; var work: integer; begin work := (key div tabsize) mod tabsize; if work = 0 then work := 1; increment := work; end; procedure initTable; var i: integer; begin n := 0; for i := 0 to tabsize - 1 do hash[i] := (-1) end; procedure insert (key: typekey; var r: dataarray); label 999; var i, ii, inc, init, j, jj: integer; begin init := hashfunction(key); inc := increment(key); for i := 0 to tabsize do begin writeln('trying to add just ', i + 1, ' to total of probe lengths'); for j := i downto 0 do begin jj := (init + inc * j) mod tabsize; ii := (jj + increment(r[jj]) * (i - j)) mod tabsize; writeln('i=', i, ' j=', j, ' jj=', jj, ' ii=', ii); if r[ii] < 0 then begin r[ii] := r[jj]; r[jj] := key; n := n + 1; goto 999 end end end; 999: end; procedure doInserts; var key: typekey; i, j, k: integer; probelengthtotal: integer; begin for k := 1 to tabsize do begin readln(key); insert(key, hash); writeln('hash=', hashfunction(key), ' increment=', increment(key)); for i := 0 to tabsize - 1 do writeln(i, hash[i]); probelengthtotal := 0; for i := 0 to tabsizeless1 do if hash[i] > (-1) then begin j := hashfunction(hash[i]); probelengthtotal := probelengthtotal + 1; while i <> j do begin j := (j + increment(hash[i])) mod tabsize; probelengthtotal := probelengthtotal + 1 end; end; writeln('probelengthtotal=', probelengthtotal, ' expected=', probelengthtotal / n : 10 : 4); end end; begin initTable; doInserts end.