1 (* wlists.mod word lists module.
3 Copyright (C) 2015-2022 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 wlists ;
25 FROM Storage IMPORT ALLOCATE, DEALLOCATE ;
31 wlist = POINTER TO RECORD
32 noOfElements: CARDINAL ;
33 elements : ARRAY [1..maxNoOfElements] OF WORD ;
39 initList - creates a new wlist, l.
42 PROCEDURE initList () : wlist ;
56 killList - deletes the complete wlist, l.
59 PROCEDURE killList (VAR l: wlist) ;
73 replaceItemInList - replace the nth WORD in wlist, l.
74 The first item in a wlists is at index, 1.
75 If the index, n, is out of range nothing is changed.
78 PROCEDURE replaceItemInList (l: wlist; n: CARDINAL; w: WORD) ;
91 END replaceItemInList ;
95 putItemIntoList - places an WORD, c, into wlist, l.
98 PROCEDURE putItemIntoList (l: wlist; c: WORD) ;
101 IF noOfElements<maxNoOfElements
104 elements[noOfElements] := c
107 putItemIntoList (next, c)
109 next := initList () ;
110 putItemIntoList (next, c)
113 END putItemIntoList ;
117 getItemFromList - retrieves the nth WORD from wlist, l.
120 PROCEDURE getItemFromList (l: wlist; n: CARDINAL) : WORD ;
128 DEC (n, noOfElements)
134 END getItemFromList ;
138 getIndexOfList - returns the index for WORD, c, in wlist, l.
139 If more than one WORD, c, exists the index
140 for the first is returned.
143 PROCEDURE getIndexOfList (l: wlist; c: WORD) : CARDINAL ;
153 WHILE i<=noOfElements DO
161 RETURN noOfElements + getIndexOfList (next, c)
168 noOfItemsInList - returns the number of items in wlist, l.
171 PROCEDURE noOfItemsInList (l: wlist) : CARDINAL ;
182 INC (t, noOfElements)
188 END noOfItemsInList ;
192 includeItemIntoList - adds an WORD, c, into a wlist providing
193 the value does not already exist.
196 PROCEDURE includeItemIntoList (l: wlist; c: WORD) ;
198 IF NOT isItemInList (l, c)
200 putItemIntoList (l, c)
202 END includeItemIntoList ;
206 removeItem - remove an element at index, i, from the wlist data type.
209 PROCEDURE removeItem (p, l: wlist; i: CARDINAL) ;
213 WHILE i<=noOfElements DO
214 elements[i] := elements[i+1] ;
217 IF (noOfElements=0) AND (p#NIL)
227 removeItemFromList - removes a WORD, c, from a wlist.
228 It assumes that this value only appears once.
231 PROCEDURE removeItemFromList (l: wlist; c: WORD) ;
244 WHILE (i<=noOfElements) AND (elements[i]#c) DO
248 IF (i<=l^.noOfElements) AND (l^.elements[i]=c)
255 UNTIL (l=NIL) OR found ;
261 END removeItemFromList ;
265 isItemInList - returns true if a WORD, c, was found in wlist, l.
268 PROCEDURE isItemInList (l: wlist; c: WORD) : BOOLEAN ;
275 WHILE i<=noOfElements DO
291 foreachItemInListDo - calls procedure, P, foreach item in wlist, l.
294 PROCEDURE foreachItemInListDo (l: wlist; p: performOperation) ;
298 n := noOfItemsInList(l) ;
301 p (getItemFromList (l, i)) ;
304 END foreachItemInListDo ;
308 duplicateList - returns a duplicate wlist derived from, l.
311 PROCEDURE duplicateList (l: wlist) : wlist ;
317 n := noOfItemsInList (l) ;
320 putItemIntoList (m, getItemFromList (l, i)) ;