From cca13430753986affe9fd7e2d432ec44d39c0e66 Mon Sep 17 00:00:00 2001 From: Anton Krotov Date: Thu, 24 Nov 2016 16:49:43 +0000 Subject: [PATCH] add module "Vector" git-svn-id: svn://kolibrios.org@6755 a494cfbc-eb01-0410-851d-a64ba20cac60 --- .../develop/oberon07/Lib/KolibriOS/API.ob07 | 6 +- .../oberon07/Lib/KolibriOS/Vector.ob07 | 121 ++++++++++++++++++ .../develop/oberon07/Samples/vector_ex.ob07 | 57 +++++++++ 3 files changed, 181 insertions(+), 3 deletions(-) create mode 100644 programs/develop/oberon07/Lib/KolibriOS/Vector.ob07 create mode 100644 programs/develop/oberon07/Samples/vector_ex.ob07 diff --git a/programs/develop/oberon07/Lib/KolibriOS/API.ob07 b/programs/develop/oberon07/Lib/KolibriOS/API.ob07 index 70f1bd52f0..404d3ac6c1 100644 --- a/programs/develop/oberon07/Lib/KolibriOS/API.ob07 +++ b/programs/develop/oberon07/Lib/KolibriOS/API.ob07 @@ -60,7 +60,7 @@ BEGIN RETURN Res END strncmp; -PROCEDURE [stdcall] sysfunc1(arg1: INTEGER): INTEGER; +PROCEDURE [stdcall] sysfunc1*(arg1: INTEGER): INTEGER; BEGIN sys.CODE("8B4508"); (* mov eax, [ebp + 08h] *) sys.CODE("CD40"); (* int 40h *) @@ -69,7 +69,7 @@ BEGIN RETURN 0 END sysfunc1; -PROCEDURE [stdcall] sysfunc2(arg1, arg2: INTEGER): INTEGER; +PROCEDURE [stdcall] sysfunc2*(arg1, arg2: INTEGER): INTEGER; BEGIN sys.CODE("53"); (* push ebx *) sys.CODE("8B4508"); (* mov eax, [ebp + 08h] *) @@ -81,7 +81,7 @@ BEGIN RETURN 0 END sysfunc2; -PROCEDURE [stdcall] sysfunc3(arg1, arg2, arg3: INTEGER): INTEGER; +PROCEDURE [stdcall] sysfunc3*(arg1, arg2, arg3: INTEGER): INTEGER; BEGIN sys.CODE("53"); (* push ebx *) sys.CODE("8B4508"); (* mov eax, [ebp + 08h] *) diff --git a/programs/develop/oberon07/Lib/KolibriOS/Vector.ob07 b/programs/develop/oberon07/Lib/KolibriOS/Vector.ob07 new file mode 100644 index 0000000000..1dc5069bc6 --- /dev/null +++ b/programs/develop/oberon07/Lib/KolibriOS/Vector.ob07 @@ -0,0 +1,121 @@ +(* + Copyright 2016 Anton Krotov + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program 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 Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with this program. If not, see . +*) + +MODULE Vector; + + +IMPORT sys := SYSTEM, K := KOSAPI; + + +TYPE + + DESC_VECTOR = RECORD + + data : INTEGER; + count : INTEGER; + size : INTEGER + + END; + + VECTOR* = POINTER TO DESC_VECTOR; + + ANYREC* = RECORD END; + + ANYPTR* = POINTER TO ANYREC; + + DESTRUCTOR* = PROCEDURE (VAR ptr: ANYPTR); + + +PROCEDURE count* (vector: VECTOR): INTEGER; +BEGIN + ASSERT(vector # NIL) + RETURN vector.count +END count; + + +PROCEDURE push* (vector: VECTOR; value: ANYPTR); +BEGIN + ASSERT(vector # NIL); + IF vector.count = vector.size THEN + vector.data := K.realloc(vector.data, (vector.size + 1024) * 4); + ASSERT(vector.data # 0); + vector.size := vector.size + 1024 + END; + sys.PUT(vector.data + vector.count * 4, value); + INC(vector.count) +END push; + + +PROCEDURE get* (vector: VECTOR; idx: INTEGER): ANYPTR; +VAR res: ANYPTR; +BEGIN + ASSERT(vector # NIL); + ASSERT( (0 <= idx) & (idx < vector.count) ); + sys.GET(vector.data + idx * 4, res) + RETURN res +END get; + + +PROCEDURE put* (vector: VECTOR; idx: INTEGER; value: ANYPTR); +BEGIN + ASSERT(vector # NIL); + ASSERT( (0 <= idx) & (idx < vector.count) ); + sys.PUT(vector.data + idx * 4, value) +END put; + + +PROCEDURE create* (size: INTEGER): VECTOR; +VAR vector: VECTOR; +BEGIN + NEW(vector); + IF vector # NIL THEN + vector.data := K.malloc(4 * size); + IF vector.data # 0 THEN + vector.size := size; + vector.count := 0 + ELSE + DISPOSE(vector) + END + END + RETURN vector +END create; + + +PROCEDURE def_destructor (VAR any: ANYPTR); +BEGIN + DISPOSE(any) +END def_destructor; + + +PROCEDURE destroy* (VAR vector: VECTOR; destructor: DESTRUCTOR); +VAR i: INTEGER; + any: ANYPTR; +BEGIN + ASSERT(vector # NIL); + 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) +END destroy; + + +END Vector. \ No newline at end of file diff --git a/programs/develop/oberon07/Samples/vector_ex.ob07 b/programs/develop/oberon07/Samples/vector_ex.ob07 new file mode 100644 index 0000000000..fb32cda191 --- /dev/null +++ b/programs/develop/oberon07/Samples/vector_ex.ob07 @@ -0,0 +1,57 @@ +MODULE vector_ex; + +IMPORT C := ConsoleLib, Out, V := Vector; + + +TYPE + + STRING = ARRAY 240 OF CHAR; + + Item = POINTER TO RECORD (V.ANYREC) inf: STRING END; + + +PROCEDURE add(v: V.VECTOR; s: STRING); +VAR item: Item; +BEGIN + NEW(item); + item.inf := s; + V.push(v, item) +END add; + + +PROCEDURE print(v: V.VECTOR; first, last: INTEGER); +VAR any : V.ANYPTR; + i : INTEGER; +BEGIN + i := first; + WHILE i <= last DO + any := V.get(v, i); + Out.String(any(Item).inf); + Out.Ln; + INC(i) + END; +END print; + + +PROCEDURE main; +VAR v: V.VECTOR; +BEGIN + C.open(-1, -1, -1, -1, "vector"); + + v := V.create(1024); + + add(v, "abc"); + add(v, "def"); + add(v, "123"); + add(v, "qwerty"); + add(v, "hello"); + + print(v, 0, V.count(v) - 1); + + C.exit(FALSE) +END main; + + +BEGIN + main +END vector_ex. \ No newline at end of file