home *** CD-ROM | disk | FTP | other *** search
/ Amiga ISO Collection / AmigaUtilCD2.iso / Programming / Misc / OB3.2D2.DMS / in.adf / Module / FArrays.mod < prev    next >
Encoding:
Text File  |  1994-08-05  |  3.0 KB  |  168 lines

  1. (*-------------------------------------------------------------------------*)
  2. (*                                                                         *)
  3. (*  Amiga Oberon Interface Module: FArray             Date: 02-Nov-92      *)
  4. (*                                                                         *)
  5. (*   © 1992 by Fridtjof Siebert                                            *)
  6. (*                                                                         *)
  7. (*-------------------------------------------------------------------------*)
  8.  
  9. MODULE FArrays;
  10. (*
  11.  * Provides a resizeable, flexible array type named FArray.
  12.  *
  13.  *)
  14.  
  15. IMPORT BT * := BasicTypes;
  16.  
  17. TYPE
  18.   FArray * = POINTER TO FArrayDesc;
  19.  
  20.   ArrayOfAny = POINTER TO ARRAY OF BT.ANY;
  21.  
  22.   FArrayDesc * = RECORD (BT.COLLECTIONDesc)
  23.     elements: ArrayOfAny;
  24.     lower-,upper-: LONGINT;
  25.   END;
  26.  
  27.  
  28. PROCEDURE Create * (minindex,maxindex: LONGINT): FArray;
  29. (*
  30.  * require
  31.  *   minindex <= maxindex
  32.  *
  33.  * ensure
  34.  *   Result.lower=minindex;
  35.  *   Result.upper=maxindex
  36.  *
  37.  *)
  38.  
  39. VAR
  40.   a: FArray;
  41. BEGIN
  42.   NEW(a);
  43.   a.lower := minindex;
  44.   a.upper := maxindex;
  45.   NEW(a.elements,maxindex-minindex+1);
  46.   RETURN a;
  47. END Create;
  48.  
  49.  
  50. PROCEDURE (a: FArray) Put * (v: BT.ANY; at: LONGINT);
  51. (*
  52.  * require
  53.  *   lower <= at;
  54.  *   at <= upper;
  55.  *   elements#NIL;
  56.  * ensure
  57.  *   Get(at)=v
  58.  *)
  59.  
  60. BEGIN
  61.   a.elements[at-a.lower] := v;
  62. END Put;
  63.  
  64.  
  65. PROCEDURE (a: FArray) Get * (at: LONGINT): BT.ANY;
  66. (*
  67.  * require
  68.  *   lower <= at;
  69.  *   at <= upper;
  70.  *   elements#NIL;
  71.  *)
  72.  
  73. BEGIN
  74.   RETURN a.elements[at-a.lower];
  75. END Get;
  76.  
  77.  
  78. PROCEDURE (a: FArray) Resize * (minindex,maxindex: LONGINT);
  79. (*
  80.  * require
  81.  *   a.elements#NIL
  82.  *
  83.  * ensure
  84.  *   lower<=minindex;
  85.  *   upper>=maxindex
  86.  *
  87.  *)
  88.  
  89. VAR
  90.   new: ArrayOfAny;
  91.   i: LONGINT;
  92.  
  93. BEGIN
  94.   IF (minindex<a.lower) OR (maxindex>a.upper) THEN
  95.     IF minindex>a.lower THEN minindex := a.lower END;
  96.     IF maxindex<a.upper THEN maxindex := a.upper END;
  97.     NEW(new,maxindex-minindex+1);
  98.     FOR i:=0 TO a.upper-a.lower DO
  99.       new[i+a.lower-minindex] := a.elements[i];
  100.     END;
  101.  
  102. (* $IFNOT GarbageCollector *)
  103.  
  104.     DISPOSE(a.elements);
  105.  
  106. (* $END  *)
  107.  
  108.     a.elements := new;
  109.     a.lower := minindex;
  110.     a.upper := maxindex;
  111.   END;
  112. END Resize;
  113.  
  114.  
  115. (*
  116.  * redefinition of inherited routines:
  117.  *
  118.  *)
  119.  
  120.  
  121. PROCEDURE (a: FArray) Add        * (x: BT.ANY);  
  122. (*
  123.  * Does nothing useful. DO NOT USE!
  124.  *)
  125. BEGIN
  126.   HALT(20);
  127. END Add;
  128.  
  129.  
  130. PROCEDURE (a: FArray) Remove     * (x: BT.ANY);
  131. (* removes x from c.
  132.  *)
  133. VAR
  134.   i: LONGINT;
  135. BEGIN
  136.   FOR i := a.lower TO a.upper DO
  137.     IF a.elements[i]=x THEN a.elements[i] := NIL END;
  138.   END;
  139. END Remove;
  140.  
  141.  
  142. PROCEDURE (a: FArray) nbElements * (): LONGINT;
  143. (* returns the number of elements within c.
  144.  *)
  145. BEGIN
  146.   RETURN a.upper-a.lower+1;
  147. END nbElements;
  148.  
  149.  
  150. PROCEDURE (a: FArray) Do         * (p: BT.DoProc; par: BT.ANY);
  151. (* calls p(x,par) for every element x stored within c.
  152.  * par passes some additional information to p. par is not touched by Do.
  153.  *)
  154. VAR
  155.   i: LONGINT;
  156. BEGIN
  157.   FOR i := a.lower TO a.upper DO
  158.     p(a.elements[i],par);
  159.   END;
  160. END Do;
  161.  
  162.  
  163. END FArrays.
  164.  
  165.  
  166.  
  167.  
  168.