ValueRect:
PROC [table: Table, value:
REF]
RETURNS [rect: Rect] = {
sizeOfValues: REF INT ← NARROW [table.userData];
pos: REF LIST OF INT ← NARROW [value];
posx: INT ← pos.first;
posy: INT ← pos.rest.first;
rect ← [[posx-sizeOfValues^, posx+sizeOfValues^], [posy-sizeOfValues^, posy+sizeOfValues^]];
};
Test:
PROC [numberOfValues:
NAT, rangeMax:
NAT]
RETURNS [table: Table, results:
LIST
OF
INT ←
NIL, rects:
LIST
OF Rect ←
NIL] = {
sizeOfValues: NAT ← 2;
rs: Random.RandomStream ← Random.Create[rangeMax, rangeMax];
last: REF;
posx, posy: INT;
count: INT ← 0;
Count: PROC [table: Table, v: Value] RETURNS [BOOL ← FALSE] = {count ← count+1};
Each: PROC [table: Table, value: Value] RETURNS [BOOL ← FALSE] = {rects ← CONS [ValueRect[table, value], rects]};
MakeError: PROC [table: Table, v: Value] RETURNS [BOOL ← FALSE] = {ERROR};
table ← Create[range: [[-sizeOfValues, rangeMax+sizeOfValues], [-sizeOfValues, rangeMax+sizeOfValues]], valueRect: ValueRect, userData: NEW [INT ← sizeOfValues]];
THROUGH [0 .. numberOfValues]
DO
posx ← Random.NextInt[rs];
posy ← Random.NextInt[rs];
Insert[table, (last ← NEW [LIST OF INT ← LIST [posx, posy]])];
ENDLOOP;
IF table.size#numberOfValues+1 THEN ERROR;
Delete[table, last];
IF table.size#numberOfValues THEN ERROR;
Delete[table, last];
IF table.size#numberOfValues THEN ERROR;
FOR i:
NAT
IN [0 .. table.data.hashSize)
DO
results ← CONS [IF table.data[i]#NIL THEN table.data[i].size ELSE -1, results];
ENDLOOP;
[] ← Enumerate[table, Count];
IF count#numberOfValues THEN ERROR;
[] ← Enumerate[table, Each, [[rangeMax/2-10*sizeOfValues, rangeMax/2+10*sizeOfValues], [rangeMax/2-10*sizeOfValues, rangeMax/2+10*sizeOfValues]]];
[] ← Enumerate[table, MakeError, [[-sizeOfValues-10, -sizeOfValues-5], [-sizeOfValues-10, -sizeOfValues-5]]];
};