]> git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/testsuite/gm2/projects/pim/run/pass/random/StoreCoords.mod
Merge modula-2 front end onto gcc.
[thirdparty/gcc.git] / gcc / testsuite / gm2 / projects / pim / run / pass / random / StoreCoords.mod
1 (* Copyright (C) 2005, 2006, 2007, 2008, 2009, 2010
2 Free Software Foundation, Inc. *)
3 (* This file is part of Chisel.
4
5 Chisel 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 Chisel 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
19 IMPLEMENTATION MODULE StoreCoords ;
20
21
22 FROM MapOptions IMPORT isVerbose ;
23 FROM StrIO IMPORT WriteString, WriteLn ;
24 FROM NumberIO IMPORT WriteCard ;
25 FROM Chance IMPORT GetRand ;
26
27
28 CONST
29 MaxCoord = 150000 ;
30 MaxIndex = 5000 ;
31
32 TYPE
33 Coord = RECORD
34 X,
35 Y: CARDINAL ;
36 END ;
37
38 Index = RECORD
39 Start, (* Start of the Coord list *)
40 End : CARDINAL ; (* End of the Coord list *)
41 END ;
42
43 VAR
44 CoordIndex : ARRAY [0..MaxIndex] OF Index ;
45 Coords : ARRAY [1..MaxCoord] OF Coord ;
46 NoOfCoords : CARDINAL ; (* Number of coordinates in array Coords *)
47 NoOfIndices: CARDINAL ; (* Number of indices in CoordIndex *)
48
49
50 (*
51 InitCoords - Initializes a potential list of coordinates.
52 An index to this potential coordinate list is returned.
53 *)
54
55 PROCEDURE InitCoords () : CARDINAL ;
56 BEGIN
57 IF NoOfIndices=MaxIndex
58 THEN
59 WriteString('too many coordinate list indices in Module StoreCoords') ;
60 WriteLn ;
61 WriteString('increase MaxIndex') ;
62 WriteLn ;
63 HALT
64 ELSE
65 INC(NoOfIndices) ;
66 WITH CoordIndex[NoOfIndices] DO
67 Start := NoOfCoords+1 ;
68 End := 0
69 END ;
70 AddCoord(NoOfIndices, 0, 0) ; (* Dummy coordinate that we keep *)
71 RETURN(NoOfIndices) (* for the life of this list. *)
72 END
73 END InitCoords ;
74
75
76 (*
77 KillCoords - Kills a complete coordinate list.
78 *)
79
80 PROCEDURE KillCoords (CoordListIndex: CARDINAL) ;
81 BEGIN
82 IF NoOfIndices>0
83 THEN
84 (* Destroy index to Coord list *)
85 WITH CoordIndex[CoordListIndex] DO
86 IF isVerbose ()
87 THEN
88 WriteString('No of coords') ; WriteCard(End-Start+1, 4) ; WriteLn
89 END ;
90 Start := 0 ;
91 End := 0
92 END ;
93 (*
94 If killed last Coord list see if we can garbage collect
95 previously killed middle indices.
96 *)
97 IF NoOfIndices=CoordListIndex
98 THEN
99 REPEAT
100 DEC(NoOfIndices)
101 UNTIL (NoOfIndices=0) OR (CoordIndex[NoOfIndices].Start#0)
102 END ;
103 NoOfCoords := CoordIndex[NoOfIndices].End
104 ELSE
105 WriteString('all Coordinate lists have been killed - Module StoreCoords') ;
106 WriteLn ;
107 HALT
108 END
109 END KillCoords ;
110
111
112
113 (*
114 AddCoord - places a coordinate into the specified list.
115 *)
116
117 PROCEDURE AddCoord (CoordListIndex: CARDINAL; x, y: CARDINAL) ;
118 BEGIN
119 IF NoOfCoords=MaxCoord
120 THEN
121 WriteString('too many coordinates in a list in Module StoreCoords') ;
122 WriteLn ;
123 WriteString('increase MaxCoord') ;
124 WriteLn ;
125 HALT
126 ELSIF UniqueCoord(CoordListIndex, x, y)
127 THEN
128 INC(NoOfCoords) ;
129 WITH Coords[NoOfCoords] DO
130 X := x ;
131 Y := y
132 END ;
133 WITH CoordIndex[CoordListIndex] DO
134 End := NoOfCoords
135 END
136 END
137 END AddCoord ;
138
139
140 (*
141 UniqueCoord - returns true if x and y are unique in the coord list.
142 *)
143
144 PROCEDURE UniqueCoord (CoordListIndex: CARDINAL;
145 x, y: CARDINAL) : BOOLEAN ;
146 VAR
147 i : CARDINAL ;
148 Found: BOOLEAN ;
149 BEGIN
150 WITH CoordIndex[CoordListIndex] DO
151 i := Start ;
152 Found := FALSE ;
153 WHILE (NOT Found) AND (i<=End) DO
154 WITH Coords[i] DO
155 Found := (X=x) AND (Y=y)
156 END ;
157 INC(i)
158 END
159 END ;
160 RETURN( NOT Found )
161 END UniqueCoord ;
162
163
164 (*
165 GetAndDeleteRandomCoord - Returns a random coordinate from the coordinate
166 list and then it is deleted from the list.
167 *)
168
169 PROCEDURE GetAndDeleteRandomCoord (CoordListIndex: CARDINAL;
170 VAR x, y: CARDINAL) ;
171 VAR
172 i, j: CARDINAL ;
173 BEGIN
174 WITH CoordIndex[CoordListIndex] DO
175 i := Start+GetRand(End-Start+1) ; (* +1 for GetRand *)
176 j := i ;
177 REPEAT
178 IF Coords[j].X=0
179 THEN
180 INC(j) ;
181 IF j>End
182 THEN
183 j := Start
184 END
185 END
186 UNTIL (j=i) OR (Coords[j].X#0) ;
187 WITH Coords[j] DO
188 x := X ;
189 y := Y ;
190 X := 0 ; (* Now delete this box *)
191 Y := 0
192 END
193 END
194 END GetAndDeleteRandomCoord ;
195
196
197 (*
198 CoordsExist - returns true if a coordinate exists
199 within the CoordListIndex.
200 *)
201
202 PROCEDURE CoordsExist (CoordListIndex: CARDINAL) : BOOLEAN ;
203 VAR
204 i : CARDINAL ;
205 ok: BOOLEAN ;
206 BEGIN
207 ok := FALSE ;
208 WITH CoordIndex[CoordListIndex] DO
209 IF End>0
210 THEN
211 (* Was at least one coordinate *)
212 i := Start ;
213 WHILE (NOT ok) AND (i<=End) DO
214 ok := (Coords[i].X#0) ; (* #0 means coordinate still exists *)
215 INC(i)
216 END
217 END
218 END ;
219 RETURN( ok )
220 END CoordsExist ;
221
222
223 PROCEDURE Init ;
224 BEGIN
225 NoOfCoords := 0 ;
226 NoOfIndices := 0 ;
227 WITH CoordIndex[NoOfIndices] DO
228 End := 0
229 END
230 END Init ;
231
232
233 BEGIN
234 Init
235 END StoreCoords.