2023-01-22 15:20:23 +01:00
|
|
|
(*
|
2023-02-01 14:36:55 +01:00
|
|
|
Copyright 2016, 2023 Anton Krotov
|
2023-01-22 15:20:23 +01:00
|
|
|
|
|
|
|
This file is part of fb2read.
|
|
|
|
|
|
|
|
fb2read is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
fb2read is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with fb2read. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*)
|
|
|
|
|
|
|
|
MODULE Vector;
|
|
|
|
|
2023-02-01 14:36:55 +01:00
|
|
|
IMPORT SYSTEM, K := KOSAPI;
|
2023-01-22 15:20:23 +01:00
|
|
|
|
|
|
|
|
2023-02-01 14:36:55 +01:00
|
|
|
CONST
|
|
|
|
ptr_size = 4;
|
2023-01-22 15:20:23 +01:00
|
|
|
|
|
|
|
|
2023-02-01 14:36:55 +01:00
|
|
|
TYPE
|
2023-01-22 15:20:23 +01:00
|
|
|
|
2023-02-01 14:36:55 +01:00
|
|
|
DESC_VECTOR = RECORD
|
|
|
|
data : INTEGER;
|
|
|
|
count* : INTEGER;
|
|
|
|
size : INTEGER
|
|
|
|
END;
|
2023-01-22 15:20:23 +01:00
|
|
|
|
2023-02-01 14:36:55 +01:00
|
|
|
VECTOR* = POINTER TO DESC_VECTOR;
|
2023-01-22 15:20:23 +01:00
|
|
|
|
2023-02-01 14:36:55 +01:00
|
|
|
ANYREC* = RECORD END;
|
2023-01-22 15:20:23 +01:00
|
|
|
|
2023-02-01 14:36:55 +01:00
|
|
|
ANYPTR* = POINTER TO ANYREC;
|
2023-01-22 15:20:23 +01:00
|
|
|
|
2023-02-01 14:36:55 +01:00
|
|
|
DESTRUCTOR* = PROCEDURE (VAR ptr: ANYPTR);
|
2023-01-22 15:20:23 +01:00
|
|
|
|
|
|
|
|
|
|
|
PROCEDURE push* (vector: VECTOR; value: ANYPTR);
|
|
|
|
BEGIN
|
2023-02-01 14:36:55 +01:00
|
|
|
IF vector.count = vector.size THEN
|
|
|
|
vector.data := K.realloc(vector.data, (vector.size + 1024) * ptr_size);
|
|
|
|
vector.size := vector.size + 1024
|
|
|
|
END;
|
|
|
|
SYSTEM.PUT(vector.data + vector.count * ptr_size, value);
|
|
|
|
INC(vector.count)
|
2023-01-22 15:20:23 +01:00
|
|
|
END push;
|
|
|
|
|
|
|
|
|
|
|
|
PROCEDURE get* (vector: VECTOR; idx: INTEGER): ANYPTR;
|
2023-02-01 14:36:55 +01:00
|
|
|
VAR
|
|
|
|
res: ANYPTR;
|
2023-01-22 15:20:23 +01:00
|
|
|
BEGIN
|
2023-02-01 14:36:55 +01:00
|
|
|
ASSERT( (0 <= idx) & (idx < vector.count) );
|
|
|
|
SYSTEM.GET(vector.data + idx * ptr_size, res)
|
|
|
|
RETURN res
|
2023-01-22 15:20:23 +01:00
|
|
|
END get;
|
|
|
|
|
|
|
|
|
|
|
|
PROCEDURE put* (vector: VECTOR; idx: INTEGER; value: ANYPTR);
|
|
|
|
BEGIN
|
2023-02-01 14:36:55 +01:00
|
|
|
ASSERT( (0 <= idx) & (idx < vector.count) );
|
|
|
|
SYSTEM.PUT(vector.data + idx * ptr_size, value)
|
2023-01-22 15:20:23 +01:00
|
|
|
END put;
|
|
|
|
|
|
|
|
|
|
|
|
PROCEDURE create* (size: INTEGER): VECTOR;
|
2023-02-01 14:36:55 +01:00
|
|
|
VAR
|
|
|
|
vector: VECTOR;
|
2023-01-22 15:20:23 +01:00
|
|
|
BEGIN
|
2023-02-01 14:36:55 +01:00
|
|
|
NEW(vector);
|
|
|
|
vector.data := K.malloc(ptr_size * size);
|
|
|
|
vector.size := size;
|
|
|
|
vector.count := 0
|
|
|
|
RETURN vector
|
2023-01-22 15:20:23 +01:00
|
|
|
END create;
|
|
|
|
|
|
|
|
|
|
|
|
PROCEDURE def_destructor (VAR any: ANYPTR);
|
|
|
|
BEGIN
|
2023-02-01 14:36:55 +01:00
|
|
|
DISPOSE(any)
|
2023-01-22 15:20:23 +01:00
|
|
|
END def_destructor;
|
|
|
|
|
|
|
|
|
|
|
|
PROCEDURE destroy* (VAR vector: VECTOR; destructor: DESTRUCTOR);
|
2023-02-01 14:36:55 +01:00
|
|
|
VAR
|
|
|
|
i: INTEGER;
|
|
|
|
any: ANYPTR;
|
2023-01-22 15:20:23 +01:00
|
|
|
BEGIN
|
2023-02-01 14:36:55 +01:00
|
|
|
IF destructor = NIL THEN
|
|
|
|
destructor := def_destructor
|
|
|
|
END;
|
|
|
|
FOR i := 0 TO vector.count - 1 DO
|
|
|
|
any := get(vector, i);
|
|
|
|
destructor(any)
|
|
|
|
END;
|
|
|
|
vector.data := K.free(vector.data);
|
|
|
|
DISPOSE(vector)
|
2023-01-22 15:20:23 +01:00
|
|
|
END destroy;
|
|
|
|
|
|
|
|
|
|
|
|
END Vector.
|