Subversion Repositories Kolibri OS

Rev

Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

  1. (*
  2.     Copyright 2016 Anton Krotov
  3.  
  4.     This file is part of fb2read.
  5.  
  6.     fb2read is free software: you can redistribute it and/or modify
  7.     it under the terms of the GNU General Public License as published by
  8.     the Free Software Foundation, either version 3 of the License, or
  9.     (at your option) any later version.
  10.  
  11.     fb2read is distributed in the hope that it will be useful,
  12.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  13.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14.     GNU General Public License for more details.
  15.  
  16.     You should have received a copy of the GNU General Public License
  17.     along with fb2read. If not, see <http://www.gnu.org/licenses/>.
  18. *)
  19.  
  20. MODULE Vector;
  21.  
  22.  
  23. IMPORT sys := SYSTEM, K := KOSAPI;
  24.  
  25.  
  26. TYPE
  27.  
  28.   DESC_VECTOR = RECORD
  29.  
  30.     data   : INTEGER;
  31.     count* : INTEGER;
  32.     size   : INTEGER
  33.  
  34.   END;
  35.  
  36.   VECTOR* = POINTER TO DESC_VECTOR;
  37.  
  38.   ANYREC* = RECORD END;
  39.  
  40.   ANYPTR* = POINTER TO ANYREC;
  41.  
  42.   DESTRUCTOR* = PROCEDURE (VAR ptr: ANYPTR);
  43.  
  44.  
  45. PROCEDURE push* (vector: VECTOR; value: ANYPTR);
  46. BEGIN
  47.   IF vector.count = vector.size THEN
  48.     vector.data := K.realloc(vector.data, (vector.size + 1024) * 4);
  49.     vector.size := vector.size + 1024
  50.   END;
  51.   sys.PUT(vector.data + vector.count * 4, value);
  52.   INC(vector.count)
  53. END push;
  54.  
  55.  
  56. PROCEDURE get* (vector: VECTOR; idx: INTEGER): ANYPTR;
  57. VAR res: ANYPTR;
  58. BEGIN
  59.   ASSERT( (0 <= idx) & (idx < vector.count) );
  60.   sys.GET(vector.data + idx * 4, res)
  61.   RETURN res
  62. END get;
  63.  
  64.  
  65. PROCEDURE put* (vector: VECTOR; idx: INTEGER; value: ANYPTR);
  66. BEGIN
  67.   ASSERT( (0 <= idx) & (idx < vector.count) );
  68.   sys.PUT(vector.data + idx * 4, value)
  69. END put;
  70.  
  71.  
  72. PROCEDURE create* (size: INTEGER): VECTOR;
  73. VAR vector: VECTOR;
  74. BEGIN
  75.   NEW(vector);
  76.   vector.data  := K.malloc(4 * size);
  77.   vector.size  := size;
  78.   vector.count := 0
  79.   RETURN vector
  80. END create;
  81.  
  82.  
  83. PROCEDURE def_destructor (VAR any: ANYPTR);
  84. BEGIN
  85.   DISPOSE(any)
  86. END def_destructor;
  87.  
  88.  
  89. PROCEDURE destroy* (VAR vector: VECTOR; destructor: DESTRUCTOR);
  90. VAR i: INTEGER;
  91.     any: ANYPTR;
  92. BEGIN
  93.   IF destructor = NIL THEN
  94.     destructor := def_destructor
  95.   END;
  96.   FOR i := 0 TO vector.count - 1 DO
  97.     any := get(vector, i);
  98.     destructor(any)
  99.   END;
  100.   vector.data := K.free(vector.data);
  101.   DISPOSE(vector)
  102. END destroy;
  103.  
  104.  
  105. END Vector.
  106.