Debian Codegraph @

gcc/m2/gm2-libs/Indexing.mod

raw
1 (* Indexing.mod provides a dynamic indexing mechanism for CARDINAL.
2
3 Copyright (C) 2003-2025 Free Software Foundation, Inc.
4 Contributed by Gaius Mulley <[email protected]>.
5
6 This file is part of GNU Modula-2.
7
8 GNU Modula-2 is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3, or (at your option)
11 any later version.
12
13 GNU Modula-2 is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 General Public License for more details.
17
18 Under Section 7 of GPL version 3, you are granted additional
19 permissions described in the GCC Runtime Library Exception, version
20 3.1, as published by the Free Software Foundation.
21
22 You should have received a copy of the GNU General Public License and
23 a copy of the GCC Runtime Library Exception along with this program;
24 see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
25 <http://www.gnu.org/licenses/>. *)
26
27 IMPLEMENTATION MODULE Indexing ;
28
29 FROM libc IMPORT memset, memmove ;
30 FROM Storage IMPORT ALLOCATE, REALLOCATE, DEALLOCATE ;
31 FROM SYSTEM IMPORT TSIZE, WORD, BYTE ;
32
33 CONST
34 MinSize = 128 ;
35 DefaultGrowFactor = 2 ;
36
37 TYPE
38 PtrToAddress = POINTER TO ADDRESS ;
39 PtrToByte = POINTER TO BYTE ;
40
41 Index = POINTER TO RECORD
42 ArrayStart: ADDRESS ;
43 ArraySize : CARDINAL ;
44 Used,
45 Low,
46 High : CARDINAL ;
47 Debug : BOOLEAN ;
48 Map : BITSET ;
49 GrowFactor: CARDINAL ;
50 END ;
51
52 (*
53 InitIndexTuned - creates a dynamic array with low indice.
54 The minsize is the initial number of elements the
55 array is allocated and growfactor determines how
56 it will be resized once it becomes full.
57 *)
58
59 PROCEDURE InitIndexTuned (low, minsize, growfactor: CARDINAL) : Index ;
60 VAR
61 i: Index ;
62 BEGIN
63 NEW (i) ;
64 WITH i^ DO
65 Low := low ;
66 High := 0 ;
67 ArraySize := minsize * TSIZE (ADDRESS) ;
68 ALLOCATE (ArrayStart, ArraySize) ;
69 ArrayStart := memset (ArrayStart, 0, ArraySize) ;
70 Debug := FALSE ;
71 Used := 0 ;
72 Map := BITSET {} ;
73 GrowFactor := growfactor
74 END ;
75 RETURN i
76 END InitIndexTuned ;
77
78
79 (*
80 InitIndex - creates and returns an Index.
81 *)
82
83 PROCEDURE InitIndex (low: CARDINAL) : Index ;
84 BEGIN
85 RETURN InitIndexTuned (low, MinSize, DefaultGrowFactor)
86 END InitIndex ;
87
88
89 (*
90 KillIndex - returns Index to free storage.
91 *)
92
93 PROCEDURE KillIndex (i: Index) : Index ;
94 BEGIN
95 WITH i^ DO
96 DEALLOCATE(ArrayStart, ArraySize)
97 END ;
98 DISPOSE(i) ;
99 RETURN( NIL )
100 END KillIndex ;
101
102
103 (*
104 DebugIndex - turns on debugging within an index.
105 *)
106
107 PROCEDURE DebugIndex (i: Index) : Index ;
108 BEGIN
109 i^.Debug := TRUE ;
110 RETURN( i )
111 END DebugIndex ;
112
113
114 (*
115 InBounds - returns TRUE if indice, n, is within the bounds
116 of the dynamic array.
117 *)
118
119 PROCEDURE InBounds (i: Index; n: CARDINAL) : BOOLEAN ;
120 BEGIN
121 IF i=NIL
122 THEN
123 HALT
124 ELSE
125 WITH i^ DO
126 RETURN( (n>=Low) AND (n<=High) )
127 END
128 END
129 END InBounds ;
130
131
132 (*
133 HighIndice - returns the last legally accessible indice of this array.
134 *)
135
136 PROCEDURE HighIndice (i: Index) : CARDINAL ;
137 BEGIN
138 IF i=NIL
139 THEN
140 HALT
141 ELSE
142 RETURN( i^.High )
143 END
144 END HighIndice ;
145
146
147 (*
148 LowIndice - returns the first legally accessible indice of this array.
149 *)
150
151 PROCEDURE LowIndice (i: Index) : CARDINAL ;
152 BEGIN
153 IF i=NIL
154 THEN
155 HALT
156 ELSE
157 RETURN( i^.Low )
158 END
159 END LowIndice ;
160
161
162 (*
163 PutIndice - places, a, into the dynamic array at position i[n]
164 *)
165
166 PROCEDURE PutIndice (i: Index; n: CARDINAL; a: ADDRESS) ;
167 VAR
168 oldSize: CARDINAL ;
169 b : ADDRESS ;
170 p : POINTER TO POINTER TO WORD ;
171 BEGIN
172 WITH i^ DO
173 IF NOT InBounds(i, n)
174 THEN
175 IF n<Low
176 THEN
177 HALT
178 ELSE
179 oldSize := ArraySize ;
180 WHILE (n-Low)*TSIZE(ADDRESS)>=ArraySize DO
181 ArraySize := ArraySize * GrowFactor
182 END ;
183 IF oldSize#ArraySize
184 THEN
185 (*
186 IF Debug
187 THEN
188 printf2('increasing memory hunk from %d to %d\n',
189 oldSize, ArraySize)
190 END ;
191 *)
192 REALLOCATE(ArrayStart, ArraySize) ;
193 (* and initialize the remainder of the array to NIL *)
194 b := ArrayStart ;
195 INC(b, oldSize) ;
196 b := memset(b, 0, ArraySize-oldSize)
197 END ;
198 High := n
199 END
200 END ;
201 b := ArrayStart ;
202 INC(b, (n-Low)*TSIZE(ADDRESS)) ;
203 p := b;
204 p^ := a ;
205 INC(Used) ;
206 IF Debug
207 THEN
208 IF n<32
209 THEN
210 INCL(Map, n)
211 END
212 END
213 END
214 END PutIndice ;
215
216
217 (*
218 GetIndice - retrieves, element i[n] from the dynamic array.
219 *)
220
221 PROCEDURE GetIndice (i: Index; n: CARDINAL) : ADDRESS ;
222 VAR
223 b: PtrToByte ;
224 p: PtrToAddress ;
225 BEGIN
226 WITH i^ DO
227 IF NOT InBounds(i, n)
228 THEN
229 HALT
230 END ;
231 b := ArrayStart ;
232 INC(b, (n-Low)*TSIZE(ADDRESS)) ;
233 p := VAL(PtrToAddress, b) ;
234 IF Debug
235 THEN
236 IF (n<32) AND (NOT (n IN Map)) AND (p^#NIL)
237 THEN
238 HALT
239 END
240 END ;
241 RETURN( p^ )
242 END
243 END GetIndice ;
244
245
246 (*
247 IsIndiceInIndex - returns TRUE if, a, is in the index, i.
248 *)
249
250 PROCEDURE IsIndiceInIndex (i: Index; a: ADDRESS) : BOOLEAN ;
251 VAR
252 j: CARDINAL ;
253 b: PtrToByte ;
254 p: PtrToAddress ;
255 BEGIN
256 WITH i^ DO
257 j := Low ;
258 b := ArrayStart ;
259 WHILE j<=High DO
260 p := VAL(PtrToAddress, b) ;
261 IF p^=a
262 THEN
263 RETURN( TRUE )
264 END ;
265 (* we must not INC(p, ..) as p2c gets confused *)
266 INC(b, TSIZE(ADDRESS)) ;
267 INC(j)
268 END
269 END ;
270 RETURN( FALSE )
271 END IsIndiceInIndex ;
272
273
274 (*
275 RemoveIndiceFromIndex - removes, a, from Index, i.
276 *)
277
278 PROCEDURE RemoveIndiceFromIndex (i: Index; a: ADDRESS) ;
279 VAR
280 j: CARDINAL ;
281 p: PtrToAddress ;
282 b: PtrToByte ;
283 BEGIN
284 WITH i^ DO
285 j := Low ;
286 b := ArrayStart ;
287 WHILE j<=High DO
288 p := VAL(PtrToAddress, b) ;
289 INC(b, TSIZE(ADDRESS)) ;
290 IF p^=a
291 THEN
292 DeleteIndice(i, j)
293 END ;
294 INC(j)
295 END
296 END
297 END RemoveIndiceFromIndex ;
298
299
300 (*
301 DeleteIndice - delete i[j] from the array.
302 *)
303
304 PROCEDURE DeleteIndice (i: Index; j: CARDINAL) ;
305 VAR
306 p: PtrToAddress ;
307 b: PtrToByte ;
308 BEGIN
309 WITH i^ DO
310 IF InBounds(i, j)
311 THEN
312 b := ArrayStart ;
313 INC(b, TSIZE(ADDRESS)*(j-Low)) ;
314 p := VAL(PtrToAddress, b) ;
315 INC(b, TSIZE(ADDRESS)) ;
316 p := memmove(p, b, (High-j)*TSIZE(ADDRESS)) ;
317 DEC(High) ;
318 DEC(Used)
319 ELSE
320 HALT
321 END
322 END
323 END DeleteIndice ;
324
325
326 (*
327 IncludeIndiceIntoIndex - if the indice is not in the index, then
328 add it at the end.
329 *)
330
331 PROCEDURE IncludeIndiceIntoIndex (i: Index; a: ADDRESS) ;
332 BEGIN
333 IF NOT IsIndiceInIndex(i, a)
334 THEN
335 IF i^.Used=0
336 THEN
337 PutIndice(i, LowIndice(i), a)
338 ELSE
339 PutIndice(i, HighIndice(i)+1, a)
340 END
341 END
342 END IncludeIndiceIntoIndex ;
343
344
345 (*
346 FindIndice - returns the indice containing a.
347 It returns zero if a is not found in array i.
348 *)
349
350 PROCEDURE FindIndice (i: Index; a: ADDRESS) : CARDINAL ;
351 VAR
352 j: CARDINAL ;
353 p: PtrToAddress ;
354 b: PtrToByte ;
355 BEGIN
356 WITH i^ DO
357 j := Low ;
358 b := ArrayStart ;
359 WHILE j <= High DO
360 p := VAL (PtrToAddress, b) ;
361 INC (b, TSIZE (ADDRESS)) ;
362 IF p^ = a
363 THEN
364 RETURN j
365 END ;
366 INC (j)
367 END
368 END ;
369 RETURN 0
370 END FindIndice ;
371
372
373 (*
374 ForeachIndiceInIndexDo - for each j indice of i, call procedure p(i[j])
375 *)
376
377 PROCEDURE ForeachIndiceInIndexDo (i: Index; p: IndexProcedure) ;
378 VAR
379 j: CARDINAL ;
380 BEGIN
381 j := LowIndice(i) ;
382 WHILE j<=HighIndice(i) DO
383 p(GetIndice(i, j)) ;
384 INC(j)
385 END
386 END ForeachIndiceInIndexDo ;
387
388
389 (*
390 IsEmpty - return TRUE if the array has no entries it.
391 *)
392
393 PROCEDURE IsEmpty (i: Index) : BOOLEAN ;
394 BEGIN
395 RETURN i^.Used = 0
396 END IsEmpty ;
397
398
399 END Indexing.