| 1 |
(* Copyright (C) 2005, 2006, 2007, 2008, 2009, 2010 |
| 2 |
Free Software Foundation, Inc. *) |
| 3 |
(* This file is part of GNU Modula-2. |
| 4 |
|
| 5 |
GNU Modula-2 is free software; you can redistribute it and/or modify it under |
| 6 |
the terms of the GNU General Public License as published by the Free |
| 7 |
Software Foundation; either version 3, or (at your option) any later |
| 8 |
version. |
| 9 |
|
| 10 |
GNU Modula-2 is distributed in the hope that it will be useful, but WITHOUT ANY |
| 11 |
WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 12 |
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 13 |
for more details. |
| 14 |
|
| 15 |
You should have received a copy of the GNU General Public License along |
| 16 |
with gm2; see the file COPYING. If not, write to the Free Software |
| 17 |
Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. *) |
| 18 |
IMPLEMENTATION MODULE StoreCoords ; |
| 19 |
|
| 20 |
|
| 21 |
FROM StrIO IMPORT WriteString, WriteLn ; |
| 22 |
FROM NumberIO IMPORT WriteCard ; |
| 23 |
FROM Chance IMPORT GetRand ; |
| 24 |
|
| 25 |
|
| 26 |
CONST |
| 27 |
MaxCoord = 15000 ; |
| 28 |
MaxIndex = 500 ; |
| 29 |
|
| 30 |
TYPE |
| 31 |
Coord = RECORD |
| 32 |
X, |
| 33 |
Y: CARDINAL ; |
| 34 |
END ; |
| 35 |
|
| 36 |
Index = RECORD |
| 37 |
Start, (* Start of the Coord list *) |
| 38 |
End : CARDINAL ; (* End of the Coord list *) |
| 39 |
END ; |
| 40 |
|
| 41 |
VAR |
| 42 |
CoordIndex : ARRAY [0..MaxIndex] OF Index ; |
| 43 |
Coords : ARRAY [1..MaxCoord] OF Coord ; |
| 44 |
NoOfCoords : CARDINAL ; (* Number of coordinates in array Coords *) |
| 45 |
NoOfIndices: CARDINAL ; (* Number of indices in CoordIndex *) |
| 46 |
|
| 47 |
|
| 48 |
(* |
| 49 |
InitCoords - Initializes a potential list of coordinates. |
| 50 |
An index to this potential coordinate list is returned. |
| 51 |
*) |
| 52 |
|
| 53 |
PROCEDURE InitCoords () : CARDINAL ; |
| 54 |
BEGIN |
| 55 |
IF NoOfIndices=MaxIndex |
| 56 |
THEN |
| 57 |
WriteString('Too many coordinate list indices in Module StoreCoords') ; |
| 58 |
WriteLn ; |
| 59 |
WriteString('Increase MaxIndex') ; |
| 60 |
WriteLn ; |
| 61 |
HALT |
| 62 |
ELSE |
| 63 |
INC(NoOfIndices) ; |
| 64 |
WITH CoordIndex[NoOfIndices] DO |
| 65 |
Start := NoOfCoords+1 ; |
| 66 |
End := 0 |
| 67 |
END ; |
| 68 |
AddCoord(NoOfIndices, 0, 0) ; (* Dummy coordinate that we keep *) |
| 69 |
RETURN(NoOfIndices) (* for the life of this list. *) |
| 70 |
END |
| 71 |
END InitCoords ; |
| 72 |
|
| 73 |
|
| 74 |
(* |
| 75 |
KillCoords - Kills a complete coordinate list. |
| 76 |
*) |
| 77 |
|
| 78 |
PROCEDURE KillCoords (CoordListIndex: CARDINAL) ; |
| 79 |
BEGIN |
| 80 |
IF NoOfIndices>0 |
| 81 |
THEN |
| 82 |
(* Destroy index to Coord list *) |
| 83 |
WITH CoordIndex[CoordListIndex] DO |
| 84 |
WriteString('No of coords') ; WriteCard(End-Start+1, 4) ; WriteLn ; |
| 85 |
Start := 0 ; |
| 86 |
End := 0 |
| 87 |
END ; |
| 88 |
(* |
| 89 |
If killed last Coord list see if we can garbage collect |
| 90 |
previously killed middle indices. |
| 91 |
*) |
| 92 |
IF NoOfIndices=CoordListIndex |
| 93 |
THEN |
| 94 |
REPEAT |
| 95 |
DEC(NoOfIndices) |
| 96 |
UNTIL (NoOfIndices=0) OR (CoordIndex[NoOfIndices].Start#0) |
| 97 |
END ; |
| 98 |
NoOfCoords := CoordIndex[NoOfIndices].End |
| 99 |
ELSE |
| 100 |
WriteString('All Coordinate lists have been killed - Module StoreCoords') ; |
| 101 |
WriteLn ; |
| 102 |
HALT |
| 103 |
END |
| 104 |
END KillCoords ; |
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
(* |
| 109 |
AddCoord - places a coordinate into the specified list. |
| 110 |
*) |
| 111 |
|
| 112 |
PROCEDURE AddCoord (CoordListIndex: CARDINAL; x, y: CARDINAL) ; |
| 113 |
BEGIN |
| 114 |
IF NoOfCoords=MaxCoord |
| 115 |
THEN |
| 116 |
WriteString('too many coordinates in a list in Module StoreCoords') ; |
| 117 |
WriteLn ; |
| 118 |
WriteString('increase MaxCoord') ; |
| 119 |
WriteLn ; |
| 120 |
HALT |
| 121 |
ELSIF UniqueCoord(CoordListIndex, x, y) |
| 122 |
THEN |
| 123 |
INC(NoOfCoords) ; |
| 124 |
WITH Coords[NoOfCoords] DO |
| 125 |
X := x ; |
| 126 |
Y := y |
| 127 |
END ; |
| 128 |
WITH CoordIndex[CoordListIndex] DO |
| 129 |
End := NoOfCoords |
| 130 |
END |
| 131 |
END |
| 132 |
END AddCoord ; |
| 133 |
|
| 134 |
|
| 135 |
(* |
| 136 |
UniqueCoord - returns true if x and y are unique in the coord list. |
| 137 |
*) |
| 138 |
|
| 139 |
PROCEDURE UniqueCoord (CoordListIndex: CARDINAL; |
| 140 |
x, y: CARDINAL) : BOOLEAN ; |
| 141 |
VAR |
| 142 |
i : CARDINAL ; |
| 143 |
Found: BOOLEAN ; |
| 144 |
BEGIN |
| 145 |
WITH CoordIndex[CoordListIndex] DO |
| 146 |
i := Start ; |
| 147 |
Found := FALSE ; |
| 148 |
WHILE (NOT Found) AND (i<=End) DO |
| 149 |
WITH Coords[i] DO |
| 150 |
Found := (X=x) AND (Y=y) |
| 151 |
END ; |
| 152 |
INC(i) |
| 153 |
END |
| 154 |
END ; |
| 155 |
RETURN( NOT Found ) |
| 156 |
END UniqueCoord ; |
| 157 |
|
| 158 |
|
| 159 |
(* |
| 160 |
GetAndDeleteRandomCoord - Returns a random coordinate from the coordinate |
| 161 |
list and then it is deleted from the list. |
| 162 |
*) |
| 163 |
|
| 164 |
PROCEDURE GetAndDeleteRandomCoord (CoordListIndex: CARDINAL; |
| 165 |
VAR x, y: CARDINAL) ; |
| 166 |
VAR |
| 167 |
i, j: CARDINAL ; |
| 168 |
BEGIN |
| 169 |
WITH CoordIndex[CoordListIndex] DO |
| 170 |
i := Start+GetRand(End-Start+1) ; (* +1 for GetRand *) |
| 171 |
j := i ; |
| 172 |
REPEAT |
| 173 |
IF Coords[j].X=0 |
| 174 |
THEN |
| 175 |
INC(j) ; |
| 176 |
IF j>End |
| 177 |
THEN |
| 178 |
j := Start |
| 179 |
END |
| 180 |
END |
| 181 |
UNTIL (j=i) OR (Coords[j].X#0) ; |
| 182 |
WITH Coords[j] DO |
| 183 |
x := X ; |
| 184 |
y := Y ; |
| 185 |
X := 0 ; (* Now delete this box *) |
| 186 |
Y := 0 |
| 187 |
END |
| 188 |
END |
| 189 |
END GetAndDeleteRandomCoord ; |
| 190 |
|
| 191 |
|
| 192 |
(* |
| 193 |
CoordsExist - returns true if a coordinate exists |
| 194 |
within the CoordListIndex. |
| 195 |
*) |
| 196 |
|
| 197 |
PROCEDURE CoordsExist (CoordListIndex: CARDINAL) : BOOLEAN ; |
| 198 |
VAR |
| 199 |
i : CARDINAL ; |
| 200 |
ok: BOOLEAN ; |
| 201 |
BEGIN |
| 202 |
ok := FALSE ; |
| 203 |
WITH CoordIndex[CoordListIndex] DO |
| 204 |
IF End>0 |
| 205 |
THEN |
| 206 |
(* Was at least one coordinate *) |
| 207 |
i := Start ; |
| 208 |
WHILE (NOT ok) AND (i<=End) DO |
| 209 |
ok := (Coords[i].X#0) ; (* #0 means coordinate still exists *) |
| 210 |
INC(i) |
| 211 |
END |
| 212 |
END |
| 213 |
END ; |
| 214 |
RETURN( ok ) |
| 215 |
END CoordsExist ; |
| 216 |
|
| 217 |
|
| 218 |
PROCEDURE Init ; |
| 219 |
BEGIN |
| 220 |
NoOfCoords := 0 ; |
| 221 |
NoOfIndices := 0 ; |
| 222 |
WITH CoordIndex[NoOfIndices] DO |
| 223 |
End := 0 |
| 224 |
END |
| 225 |
END Init ; |
| 226 |
|
| 227 |
|
| 228 |
BEGIN |
| 229 |
Init |
| 230 |
END StoreCoords. |