1 (* alists.mod address lists module.
3 Copyright (C) 2015-2023 Free Software Foundation, Inc.
4 Contributed by Gaius Mulley <gaius@glam.ac.uk>.
6 This file is part of GNU Modula-2.
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)
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.
18 You should have received a copy of the GNU General Public License
19 along with GNU Modula-2; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. *)
22 IMPLEMENTATION MODULE alists ;
25 FROM Storage IMPORT ALLOCATE, DEALLOCATE ;
31 alist = POINTER TO RECORD
32 noOfelements: CARDINAL ;
33 elements : ARRAY [1..MaxnoOfelements] OF ADDRESS ;
39 initList - creates a new alist, l.
42 PROCEDURE initList () : alist ;
56 killList - deletes the complete alist, l.
59 PROCEDURE killList (VAR l: alist) ;
73 putItemIntoList - places an ADDRESS, c, into alist, l.
76 PROCEDURE putItemIntoList (l: alist; c: ADDRESS) ;
79 IF noOfelements<MaxnoOfelements
82 elements[noOfelements] := c
85 putItemIntoList (next, c)
88 putItemIntoList (next, c)
95 getItemFromList - retrieves the nth WORD from alist, l.
98 PROCEDURE getItemFromList (l: alist; n: CARDINAL) : ADDRESS ;
106 DEC (n, noOfelements)
112 END getItemFromList ;
116 getIndexOfList - returns the index for WORD, c, in alist, l.
117 If more than one WORD, c, exists the index
118 for the first is returned.
121 PROCEDURE getIndexOfList (l: alist; c: ADDRESS) : CARDINAL ;
131 WHILE i<=noOfelements DO
139 RETURN noOfelements + getIndexOfList (next, c)
146 noOfItemsInList - returns the number of items in alist, l.
149 PROCEDURE noOfItemsInList (l: alist) : CARDINAL ;
160 INC (t, noOfelements)
166 END noOfItemsInList ;
170 includeItemIntoList - adds an ADDRESS, c, into a alist providing
171 the value does not already exist.
174 PROCEDURE includeItemIntoList (l: alist; c: ADDRESS) ;
176 IF NOT isItemInList (l, c)
178 putItemIntoList (l, c)
180 END includeItemIntoList ;
184 removeItem - remove an element at index, i, from the alist data type.
187 PROCEDURE removeItem (p, l: alist; i: CARDINAL) ;
191 WHILE i<=noOfelements DO
192 elements[i] := elements[i+1] ;
195 IF (noOfelements=0) AND (p#NIL)
205 removeItemFromList - removes a ADDRESS, c, from a alist.
206 It assumes that this value only appears once.
209 PROCEDURE removeItemFromList (l: alist; c: ADDRESS) ;
222 WHILE (i<=noOfelements) AND (elements[i]#c) DO
226 IF (i<=l^.noOfelements) AND (l^.elements[i]=c)
233 UNTIL (l=NIL) OR found ;
239 END removeItemFromList ;
243 isItemInList - returns true if a ADDRESS, c, was found in alist, l.
246 PROCEDURE isItemInList (l: alist; c: ADDRESS) : BOOLEAN ;
253 WHILE i<=noOfelements DO
269 foreachItemInListDo - calls procedure, P, foreach item in alist, l.
272 PROCEDURE foreachItemInListDo (l: alist; p: performOperation) ;
276 n := noOfItemsInList(l) ;
279 p (getItemFromList (l, i)) ;
282 END foreachItemInListDo ;
286 duplicateList - returns a duplicate alist derived from, l.
289 PROCEDURE duplicateList (l: alist) : alist ;
295 n := noOfItemsInList (l) ;
298 putItemIntoList (m, getItemFromList (l, i)) ;