| 1 |
(* Indexing provides a dynamic array of 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 Indexing ; |
| 21 |
|
| 22 |
FROM libc IMPORT memset, memmove ; |
| 23 |
FROM Storage IMPORT ALLOCATE, REALLOCATE, DEALLOCATE ; |
| 24 |
FROM SYSTEM IMPORT TSIZE, ADDRESS, WORD, BYTE ; |
| 25 |
FROM mcDebug IMPORT assert ; |
| 26 |
|
| 27 |
CONST |
| 28 |
MinSize = 128 ; |
| 29 |
|
| 30 |
TYPE |
| 31 |
PtrToAddress = POINTER TO ADDRESS ; |
| 32 |
PtrToByte = POINTER TO BYTE ; |
| 33 |
|
| 34 |
Index = POINTER TO RECORD |
| 35 |
ArrayStart: ADDRESS ; |
| 36 |
ArraySize : CARDINAL ; |
| 37 |
Used, |
| 38 |
Low, |
| 39 |
High : CARDINAL ; |
| 40 |
Debug : BOOLEAN ; |
| 41 |
Map : BITSET ; |
| 42 |
END ; |
| 43 |
|
| 44 |
(* |
| 45 |
InitIndex - creates and returns an Index. |
| 46 |
*) |
| 47 |
|
| 48 |
PROCEDURE InitIndex (low: CARDINAL) : Index ; |
| 49 |
VAR |
| 50 |
i: Index ; |
| 51 |
BEGIN |
| 52 |
NEW(i) ; |
| 53 |
WITH i^ DO |
| 54 |
Low := low ; |
| 55 |
High := 0 ; |
| 56 |
ArraySize := MinSize ; |
| 57 |
ALLOCATE(ArrayStart, MinSize) ; |
| 58 |
ArrayStart := memset(ArrayStart, 0, ArraySize) ; |
| 59 |
Debug := FALSE ; |
| 60 |
Used := 0 ; |
| 61 |
Map := BITSET{} |
| 62 |
END ; |
| 63 |
RETURN( i ) |
| 64 |
END InitIndex ; |
| 65 |
|
| 66 |
|
| 67 |
(* |
| 68 |
KillIndex - returns Index to free storage. |
| 69 |
*) |
| 70 |
|
| 71 |
PROCEDURE KillIndex (i: Index) : Index ; |
| 72 |
BEGIN |
| 73 |
WITH i^ DO |
| 74 |
DEALLOCATE(ArrayStart, ArraySize) |
| 75 |
END ; |
| 76 |
DISPOSE(i) ; |
| 77 |
RETURN( NIL ) |
| 78 |
END KillIndex ; |
| 79 |
|
| 80 |
|
| 81 |
(* |
| 82 |
DebugIndex - turns on debugging within an index. |
| 83 |
*) |
| 84 |
|
| 85 |
PROCEDURE DebugIndex (i: Index) : Index ; |
| 86 |
BEGIN |
| 87 |
i^.Debug := TRUE ; |
| 88 |
RETURN( i ) |
| 89 |
END DebugIndex ; |
| 90 |
|
| 91 |
|
| 92 |
(* |
| 93 |
InBounds - returns TRUE if indice, n, is within the bounds |
| 94 |
of the dynamic array. |
| 95 |
*) |
| 96 |
|
| 97 |
PROCEDURE InBounds (i: Index; n: CARDINAL) : BOOLEAN ; |
| 98 |
BEGIN |
| 99 |
IF i=NIL |
| 100 |
THEN |
| 101 |
HALT |
| 102 |
ELSE |
| 103 |
WITH i^ DO |
| 104 |
RETURN( (n>=Low) AND (n<=High) ) |
| 105 |
END |
| 106 |
END |
| 107 |
END InBounds ; |
| 108 |
|
| 109 |
|
| 110 |
(* |
| 111 |
HighIndice - returns the last legally accessible indice of this array. |
| 112 |
*) |
| 113 |
|
| 114 |
PROCEDURE HighIndice (i: Index) : CARDINAL ; |
| 115 |
BEGIN |
| 116 |
IF i=NIL |
| 117 |
THEN |
| 118 |
HALT |
| 119 |
ELSE |
| 120 |
RETURN( i^.High ) |
| 121 |
END |
| 122 |
END HighIndice ; |
| 123 |
|
| 124 |
|
| 125 |
(* |
| 126 |
LowIndice - returns the first legally accessible indice of this array. |
| 127 |
*) |
| 128 |
|
| 129 |
PROCEDURE LowIndice (i: Index) : CARDINAL ; |
| 130 |
BEGIN |
| 131 |
IF i=NIL |
| 132 |
THEN |
| 133 |
HALT |
| 134 |
ELSE |
| 135 |
RETURN( i^.Low ) |
| 136 |
END |
| 137 |
END LowIndice ; |
| 138 |
|
| 139 |
|
| 140 |
(* |
| 141 |
PutIndice - places, a, into the dynamic array at position i[n] |
| 142 |
*) |
| 143 |
|
| 144 |
PROCEDURE PutIndice (i: Index; n: CARDINAL; a: ADDRESS) ; |
| 145 |
VAR |
| 146 |
oldSize: CARDINAL ; |
| 147 |
b : ADDRESS ; |
| 148 |
p : POINTER TO POINTER TO WORD ; |
| 149 |
BEGIN |
| 150 |
WITH i^ DO |
| 151 |
IF NOT InBounds(i, n) |
| 152 |
THEN |
| 153 |
IF n<Low |
| 154 |
THEN |
| 155 |
HALT |
| 156 |
ELSE |
| 157 |
oldSize := ArraySize ; |
| 158 |
WHILE (n-Low)*TSIZE(ADDRESS)>=ArraySize DO |
| 159 |
ArraySize := ArraySize * 2 |
| 160 |
END ; |
| 161 |
IF oldSize#ArraySize |
| 162 |
THEN |
| 163 |
(* |
| 164 |
IF Debug |
| 165 |
THEN |
| 166 |
printf2('increasing memory hunk from %d to %d\n', |
| 167 |
oldSize, ArraySize) |
| 168 |
END ; |
| 169 |
*) |
| 170 |
REALLOCATE(ArrayStart, ArraySize) ; |
| 171 |
(* and initialize the remainder of the array to NIL *) |
| 172 |
b := ArrayStart ; |
| 173 |
INC(b, oldSize) ; |
| 174 |
b := memset(b, 0, ArraySize-oldSize) |
| 175 |
END ; |
| 176 |
High := n |
| 177 |
END |
| 178 |
END ; |
| 179 |
b := ArrayStart ; |
| 180 |
INC(b, (n-Low)*TSIZE(ADDRESS)) ; |
| 181 |
p := b; |
| 182 |
p^ := a ; |
| 183 |
INC(Used) ; |
| 184 |
IF Debug |
| 185 |
THEN |
| 186 |
IF n<32 |
| 187 |
THEN |
| 188 |
INCL(Map, n) |
| 189 |
END |
| 190 |
END |
| 191 |
END |
| 192 |
END PutIndice ; |
| 193 |
|
| 194 |
|
| 195 |
(* |
| 196 |
GetIndice - retrieves, element i[n] from the dynamic array. |
| 197 |
*) |
| 198 |
|
| 199 |
PROCEDURE GetIndice (i: Index; n: CARDINAL) : ADDRESS ; |
| 200 |
VAR |
| 201 |
b: PtrToByte ; |
| 202 |
p: PtrToAddress ; |
| 203 |
BEGIN |
| 204 |
WITH i^ DO |
| 205 |
IF NOT InBounds(i, n) |
| 206 |
THEN |
| 207 |
HALT |
| 208 |
END ; |
| 209 |
b := ArrayStart ; |
| 210 |
INC(b, (n-Low)*TSIZE(ADDRESS)) ; |
| 211 |
p := VAL(PtrToAddress, b) ; |
| 212 |
IF Debug |
| 213 |
THEN |
| 214 |
IF (n<32) AND (NOT (n IN Map)) AND (p^#NIL) |
| 215 |
THEN |
| 216 |
HALT |
| 217 |
END |
| 218 |
END ; |
| 219 |
RETURN( p^ ) |
| 220 |
END |
| 221 |
END GetIndice ; |
| 222 |
|
| 223 |
|
| 224 |
(* |
| 225 |
IsIndiceInIndex - returns TRUE if, a, is in the index, i. |
| 226 |
*) |
| 227 |
|
| 228 |
PROCEDURE IsIndiceInIndex (i: Index; a: ADDRESS) : BOOLEAN ; |
| 229 |
VAR |
| 230 |
j: CARDINAL ; |
| 231 |
b: PtrToByte ; |
| 232 |
p: PtrToAddress ; |
| 233 |
BEGIN |
| 234 |
WITH i^ DO |
| 235 |
j := Low ; |
| 236 |
b := ArrayStart ; |
| 237 |
WHILE j<=High DO |
| 238 |
p := VAL(PtrToAddress, b) ; |
| 239 |
IF p^=a |
| 240 |
THEN |
| 241 |
RETURN( TRUE ) |
| 242 |
END ; |
| 243 |
(* we must not INC(p, ..) as p2c gets confused *) |
| 244 |
INC(b, TSIZE(ADDRESS)) ; |
| 245 |
INC(j) |
| 246 |
END |
| 247 |
END ; |
| 248 |
RETURN( FALSE ) |
| 249 |
END IsIndiceInIndex ; |
| 250 |
|
| 251 |
|
| 252 |
(* |
| 253 |
RemoveIndiceFromIndex - removes, a, from Index, i. |
| 254 |
*) |
| 255 |
|
| 256 |
PROCEDURE RemoveIndiceFromIndex (i: Index; a: ADDRESS) ; |
| 257 |
VAR |
| 258 |
j, k: CARDINAL ; |
| 259 |
p : PtrToAddress ; |
| 260 |
b : PtrToByte ; |
| 261 |
BEGIN |
| 262 |
WITH i^ DO |
| 263 |
j := Low ; |
| 264 |
b := ArrayStart ; |
| 265 |
WHILE j<=High DO |
| 266 |
p := VAL(PtrToAddress, b) ; |
| 267 |
INC(b, TSIZE(ADDRESS)) ; |
| 268 |
IF p^=a |
| 269 |
THEN |
| 270 |
DeleteIndice(i, j) |
| 271 |
END ; |
| 272 |
INC(j) |
| 273 |
END |
| 274 |
END |
| 275 |
END RemoveIndiceFromIndex ; |
| 276 |
|
| 277 |
|
| 278 |
(* |
| 279 |
DeleteIndice - delete i[j] from the array. |
| 280 |
*) |
| 281 |
|
| 282 |
PROCEDURE DeleteIndice (i: Index; j: CARDINAL) ; |
| 283 |
VAR |
| 284 |
p: PtrToAddress ; |
| 285 |
b: PtrToByte ; |
| 286 |
BEGIN |
| 287 |
WITH i^ DO |
| 288 |
IF InBounds(i, j) |
| 289 |
THEN |
| 290 |
b := ArrayStart ; |
| 291 |
INC(b, TSIZE(ADDRESS)*(j-Low)) ; |
| 292 |
p := VAL(PtrToAddress, b) ; |
| 293 |
INC(b, TSIZE(ADDRESS)) ; |
| 294 |
p := memmove(p, b, (High-j)*TSIZE(ADDRESS)) ; |
| 295 |
DEC(High) ; |
| 296 |
DEC(Used) |
| 297 |
ELSE |
| 298 |
HALT |
| 299 |
END |
| 300 |
END |
| 301 |
END DeleteIndice ; |
| 302 |
|
| 303 |
|
| 304 |
(* |
| 305 |
IncludeIndiceIntoIndex - if the indice is not in the index, then |
| 306 |
add it at the end. |
| 307 |
*) |
| 308 |
|
| 309 |
PROCEDURE IncludeIndiceIntoIndex (i: Index; a: ADDRESS) ; |
| 310 |
BEGIN |
| 311 |
IF NOT IsIndiceInIndex(i, a) |
| 312 |
THEN |
| 313 |
IF i^.Used=0 |
| 314 |
THEN |
| 315 |
PutIndice(i, LowIndice(i), a) |
| 316 |
ELSE |
| 317 |
PutIndice(i, HighIndice(i)+1, a) |
| 318 |
END |
| 319 |
END |
| 320 |
END IncludeIndiceIntoIndex ; |
| 321 |
|
| 322 |
|
| 323 |
(* |
| 324 |
ForeachIndiceInIndexDo - for each j indice of i, call procedure p(i[j]) |
| 325 |
*) |
| 326 |
|
| 327 |
PROCEDURE ForeachIndiceInIndexDo (i: Index; p: IndexProcedure) ; |
| 328 |
VAR |
| 329 |
j: CARDINAL ; |
| 330 |
q: IndexProcedure ; |
| 331 |
BEGIN |
| 332 |
j := LowIndice (i) ; |
| 333 |
q := p ; |
| 334 |
WHILE j <= HighIndice (i) DO |
| 335 |
assert (q = p) ; |
| 336 |
p (GetIndice (i, j)) ; |
| 337 |
INC (j) |
| 338 |
END |
| 339 |
END ForeachIndiceInIndexDo ; |
| 340 |
|
| 341 |
|
| 342 |
END Indexing. |