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