Debian Codegraph @

gcc/m2/mc/lists.mod

raw
1 (* Dynamic list library for pointers.
2 Copyright (C) 2015-2025 Free Software Foundation, Inc.
3
4 This file is part of GNU Modula-2.
5
6 GNU Modula-2 is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 3, or (at your option) any later
9 version.
10
11 GNU Modula-2 is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING3. If not see
18 <http://www.gnu.org/licenses/>. *)
19
20 IMPLEMENTATION MODULE lists ;
21
22
23 FROM Storage IMPORT ALLOCATE, DEALLOCATE ;
24
25 CONST
26 MaxnoOfelements = 5 ;
27
28 TYPE
29 list = POINTER TO RECORD
30 noOfelements: CARDINAL ;
31 elements : ARRAY [1..MaxnoOfelements] OF ADDRESS ;
32 next : list ;
33 END ;
34
35
36 (*
37 initList - creates a new list, l.
38 *)
39
40 PROCEDURE initList () : list ;
41 VAR
42 l: list ;
43 BEGIN
44 NEW (l) ;
45 WITH l^ DO
46 noOfelements := 0 ;
47 next := NIL
48 END ;
49 RETURN l
50 END initList ;
51
52
53 (*
54 killList - deletes the complete list, l.
55 *)
56
57 PROCEDURE killList (VAR l: list) ;
58 BEGIN
59 IF l#NIL
60 THEN
61 IF l^.next#NIL
62 THEN
63 killList (l^.next)
64 END ;
65 DISPOSE (l)
66 END
67 END killList ;
68
69
70 (*
71 putItemIntoList - places an ADDRESS, c, into list, l.
72 *)
73
74 PROCEDURE putItemIntoList (l: list; c: ADDRESS) ;
75 BEGIN
76 WITH l^ DO
77 IF noOfelements<MaxnoOfelements
78 THEN
79 INC (noOfelements) ;
80 elements[noOfelements] := c
81 ELSIF next#NIL
82 THEN
83 putItemIntoList (next, c)
84 ELSE
85 next := initList () ;
86 putItemIntoList (next, c)
87 END
88 END
89 END putItemIntoList ;
90
91
92 (*
93 getItemFromList - retrieves the nth WORD from list, l.
94 *)
95
96 PROCEDURE getItemFromList (l: list; n: CARDINAL) : ADDRESS ;
97 BEGIN
98 WHILE l#NIL DO
99 WITH l^ DO
100 IF n<=noOfelements
101 THEN
102 RETURN elements[n]
103 ELSE
104 DEC (n, noOfelements)
105 END
106 END ;
107 l := l^.next
108 END ;
109 RETURN 0
110 END getItemFromList ;
111
112
113 (*
114 getIndexOfList - returns the index for WORD, c, in list, l.
115 If more than one WORD, c, exists the index
116 for the first is returned.
117 *)
118
119 PROCEDURE getIndexOfList (l: list; c: ADDRESS) : CARDINAL ;
120 VAR
121 i: CARDINAL ;
122 BEGIN
123 IF l=NIL
124 THEN
125 RETURN 0
126 ELSE
127 WITH l^ DO
128 i := 1 ;
129 WHILE i<=noOfelements DO
130 IF elements[i]=c
131 THEN
132 RETURN i
133 ELSE
134 INC(i)
135 END
136 END ;
137 RETURN noOfelements + getIndexOfList (next, c)
138 END
139 END
140 END getIndexOfList ;
141
142
143 (*
144 noOfItemsInList - returns the number of items in list, l.
145 *)
146
147 PROCEDURE noOfItemsInList (l: list) : CARDINAL ;
148 VAR
149 t: CARDINAL ;
150 BEGIN
151 IF l=NIL
152 THEN
153 RETURN 0
154 ELSE
155 t := 0 ;
156 REPEAT
157 WITH l^ DO
158 INC (t, noOfelements)
159 END ;
160 l := l^.next
161 UNTIL l=NIL;
162 RETURN t
163 END
164 END noOfItemsInList ;
165
166
167 (*
168 includeItemIntoList - adds an ADDRESS, c, into a list providing
169 the value does not already exist.
170 *)
171
172 PROCEDURE includeItemIntoList (l: list; c: ADDRESS) ;
173 BEGIN
174 IF NOT isItemInList (l, c)
175 THEN
176 putItemIntoList (l, c)
177 END
178 END includeItemIntoList ;
179
180
181 (*
182 removeItem - remove an element at index, i, from the list data type.
183 *)
184
185 PROCEDURE removeItem (p, l: list; i: CARDINAL) ;
186 BEGIN
187 WITH l^ DO
188 DEC (noOfelements) ;
189 WHILE i<=noOfelements DO
190 elements[i] := elements[i+1] ;
191 INC (i)
192 END ;
193 IF (noOfelements=0) AND (p#NIL)
194 THEN
195 p^.next := l^.next ;
196 DISPOSE (l)
197 END
198 END
199 END removeItem ;
200
201
202 (*
203 removeItemFromList - removes a ADDRESS, c, from a list.
204 It assumes that this value only appears once.
205 *)
206
207 PROCEDURE removeItemFromList (l: list; c: ADDRESS) ;
208 VAR
209 p : list ;
210 i : CARDINAL ;
211 found: BOOLEAN ;
212 BEGIN
213 IF l#NIL
214 THEN
215 found := FALSE ;
216 p := NIL ;
217 REPEAT
218 WITH l^ DO
219 i := 1 ;
220 WHILE (i<=noOfelements) AND (elements[i]#c) DO
221 INC (i)
222 END ;
223 END ;
224 IF (i<=l^.noOfelements) AND (l^.elements[i]=c)
225 THEN
226 found := TRUE
227 ELSE
228 p := l ;
229 l := l^.next
230 END
231 UNTIL (l=NIL) OR found ;
232 IF found
233 THEN
234 removeItem (p, l, i)
235 END
236 END
237 END removeItemFromList ;
238
239
240 (*
241 isItemInList - returns true if a ADDRESS, c, was found in list, l.
242 *)
243
244 PROCEDURE isItemInList (l: list; c: ADDRESS) : BOOLEAN ;
245 VAR
246 i: CARDINAL ;
247 BEGIN
248 REPEAT
249 WITH l^ DO
250 i := 1 ;
251 WHILE i<=noOfelements DO
252 IF elements[i]=c
253 THEN
254 RETURN TRUE
255 ELSE
256 INC (i)
257 END
258 END
259 END ;
260 l := l^.next
261 UNTIL l=NIL ;
262 RETURN FALSE
263 END isItemInList ;
264
265
266 (*
267 foreachItemInListDo - calls procedure, P, foreach item in list, l.
268 *)
269
270 PROCEDURE foreachItemInListDo (l: list; p: performOperation) ;
271 VAR
272 i, n: CARDINAL ;
273 BEGIN
274 n := noOfItemsInList(l) ;
275 i := 1 ;
276 WHILE i<=n DO
277 p (getItemFromList (l, i)) ;
278 INC(i)
279 END
280 END foreachItemInListDo ;
281
282
283 (*
284 duplicateList - returns a duplicate list derived from, l.
285 *)
286
287 PROCEDURE duplicateList (l: list) : list ;
288 VAR
289 m : list ;
290 n, i: CARDINAL ;
291 BEGIN
292 m := initList () ;
293 n := noOfItemsInList (l) ;
294 i := 1 ;
295 WHILE i<=n DO
296 putItemIntoList (m, getItemFromList (l, i)) ;
297 INC (i)
298 END ;
299 RETURN m
300 END duplicateList ;
301
302
303 END lists.