Subversion Repositories Kolibri OS

Rev

Rev 9896 | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

  1. (*
  2.     Copyright 2016, 2023 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. IMPORT SYSTEM, K := KOSAPI;
  23.  
  24.  
  25. CONST
  26.         ptr_size = 4;
  27.  
  28.  
  29. TYPE
  30.  
  31.         DESC_VECTOR = RECORD
  32.                 data   : INTEGER;
  33.                 count* : INTEGER;
  34.                 size   : INTEGER
  35.         END;
  36.  
  37.         VECTOR* = POINTER TO DESC_VECTOR;
  38.  
  39.         ANYREC* = RECORD END;
  40.  
  41.         ANYPTR* = POINTER TO ANYREC;
  42.  
  43.         DESTRUCTOR* = PROCEDURE (VAR ptr: ANYPTR);
  44.  
  45.  
  46. PROCEDURE push* (vector: VECTOR; value: ANYPTR);
  47. BEGIN
  48.         IF vector.count = vector.size THEN
  49.                 vector.data := K.realloc(vector.data, (vector.size + 1024) * ptr_size);
  50.                 vector.size := vector.size + 1024
  51.         END;
  52.         SYSTEM.PUT(vector.data + vector.count * ptr_size, value);
  53.         INC(vector.count)
  54. END push;
  55.  
  56.  
  57. PROCEDURE get* (vector: VECTOR; idx: INTEGER): ANYPTR;
  58. VAR
  59.         res: ANYPTR;
  60. BEGIN
  61.         ASSERT( (0 <= idx) & (idx < vector.count) );
  62.         SYSTEM.GET(vector.data + idx * ptr_size, res)
  63.         RETURN res
  64. END get;
  65.  
  66.  
  67. PROCEDURE put* (vector: VECTOR; idx: INTEGER; value: ANYPTR);
  68. BEGIN
  69.         ASSERT( (0 <= idx) & (idx < vector.count) );
  70.         SYSTEM.PUT(vector.data + idx * ptr_size, value)
  71. END put;
  72.  
  73.  
  74. PROCEDURE create* (size: INTEGER): VECTOR;
  75. VAR
  76.         vector: VECTOR;
  77. BEGIN
  78.         NEW(vector);
  79.         vector.data  := K.malloc(ptr_size * size);
  80.         vector.size  := size;
  81.         vector.count := 0
  82.         RETURN vector
  83. END create;
  84.  
  85.  
  86. PROCEDURE def_destructor (VAR any: ANYPTR);
  87. BEGIN
  88.         DISPOSE(any)
  89. END def_destructor;
  90.  
  91.  
  92. PROCEDURE destroy* (VAR vector: VECTOR; destructor: DESTRUCTOR);
  93. VAR
  94.         i: INTEGER;
  95.         any: ANYPTR;
  96. BEGIN
  97.         IF destructor = NIL THEN
  98.                 destructor := def_destructor
  99.         END;
  100.         FOR i := 0 TO vector.count - 1 DO
  101.                 any := get(vector, i);
  102.                 destructor(any)
  103.         END;
  104.         vector.data := K.free(vector.data);
  105.         DISPOSE(vector)
  106. END destroy;
  107.  
  108.  
  109. END Vector.
  110.