home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / sa104os2.zip / SATHR104.ZIP / SATHER / LIBRARY / AREF.SA < prev    next >
Text File  |  1994-10-28  |  8KB  |  205 lines

  1. -- Copyright (C) International Computer Science Institute, 1994.  COPYRIGHT  --
  2. -- NOTICE: This code is provided "AS IS" WITHOUT ANY WARRANTY and is subject --
  3. -- to the terms of the SATHER LIBRARY GENERAL PUBLIC LICENSE contained in    --
  4. -- the file "Doc/License" of the Sather distribution.  The license is also   --
  5. -- available from ICSI, 1947 Center St., Suite 600, Berkeley CA 94704, USA.  --
  6. --------> Please email comments to "sather-bugs@icsi.berkeley.edu". <----------
  7.  
  8. -- aref.sa: Built-in reference arrays, primarily to be included.
  9.  
  10. -------------------------------------------------------------------
  11. class AREF{T} is
  12.    -- Built-in arrays of elements of type T. Primarily intended to be
  13.    -- included in reference classes that need an array portion in their
  14.    -- objects. Array indices start at 0 and go up to "asize-1".
  15.    -- All feature names begin with "a" to minimize name conflicts when
  16.    -- include. None of the features work with void(self).  This class is
  17.    -- primarily meant to be included. ARRAY{T} provides an array 
  18.    -- abstraction that is meant to be used directly.
  19.    
  20.    asize:INT 
  21.       -- The number of elements in self. Classes which inherit this may
  22.       -- replace this by a constant to get constant sized objects (and 
  23.       -- the compiler may optimize certain operations in this case).
  24.       -- Built-in.
  25.       pre ~void(self) is 
  26.       raise "AREF{T}::asize:INT undefined." end;
  27.  
  28.    create(n:INT):SAME 
  29.       -- A new array with `n' elements.
  30.       pre n>=0 is
  31.       return new(n) end;
  32.    
  33.    aget(ind:INT):T
  34.       -- The element of self with index `ind'. Built-in.
  35.       pre ~void(self) and ind.is_bet(0,asize-1) is
  36.       raise "AREF{T}::aget(INT):T undefined." end;
  37.  
  38.    aset(ind:INT, val:T)
  39.       -- Set the element of self with index `ind' to `val'. Built-in. 
  40.       pre ~void(self) and ind.is_bet(0,asize-1) is 
  41.       raise "AREF{T}::aset(INT,T) undefined." end;
  42.  
  43.    aclear 
  44.       -- Set each element of self to nil. Built-in.
  45.       pre ~void(self) is 
  46.       nil:T; loop aset!(nil) end end;
  47.  
  48.    aelt!:T 
  49.       -- Yield each element of self in order. Built-in.
  50.       pre ~void(self) is 
  51.       loop yield [asize.times!] end end;
  52.  
  53.    aelt!(beg:INT):T
  54.       -- Yield each element of self starting at `beg'. Built-in.
  55.       pre ~void(self) and beg.is_bet(0,asize-1) is
  56.       loop yield [beg.upto!(asize-1)] end end;
  57.    
  58.    aelt!(beg,num:INT):T
  59.       -- Yield `num' successive elements of self starting at
  60.       -- index `beg'. Built-in.
  61.       pre ~void(self) and beg.is_bet(0,asize-1) and 
  62.           num.is_bet(0,asize-beg) is
  63.       loop yield [beg.upto!(beg+num-1)] end end;
  64.  
  65.    private is_legal_aelts_arg(beg,num,step:INT):BOOL is
  66.       -- True if the arguments are legal values for `aelts'.
  67.       return beg.is_bet(0,asize-1) and
  68.          ((step>0 and num.is_bet(0,(asize-beg+step-1)/step)) or
  69.           (step<0 and num.is_bet(0,(beg-step)/-step))) end;
  70.    
  71.    aelt!(beg,num,step:INT):T
  72.       -- Yield `num' elements of self starting at `beg' and stepping
  73.       -- by `step' which must not be zero. Built-in.
  74.       pre ~void(self) and is_legal_aelts_arg(beg,num,step) is
  75.       loop yield [beg.step!(num,step)] end end;
  76.    
  77.    aset!(val:T!) 
  78.       -- Set successive elements of self to the values `val'. 
  79.       -- Built-in.
  80.       pre ~void(self) is 
  81.       loop [asize.times!]:=val; yield end end;
  82.  
  83.    aset!(beg:INT,val:T!)
  84.       -- Set successive elements of self starting at `beg' to the 
  85.       -- values `val'. Built-in.
  86.       pre ~void(self) and beg.is_bet(0,asize-1) is
  87.       loop [beg.upto!(asize-1)]:=val; yield end end;
  88.    
  89.    aset!(beg,num:INT,val:T!)
  90.       -- Set `num' successive elements of self starting at `beg'
  91.       -- to the values `val'. Built-in. 
  92.       pre ~void(self) and  beg.is_bet(0,asize-1) and 
  93.           num.is_bet(0,asize-beg) is
  94.       loop [beg.upto!(beg+num-1)]:=val; yield end end;
  95.  
  96.    aset!(beg,num,step:INT, val:T!)
  97.       -- Set `num' elements of self starting at `beg' stepping 
  98.       -- by `step' to the values `val'. `step' must not be zero. 
  99.       -- Built-in.
  100.       pre ~void(self) and is_legal_aelts_arg(beg,num,step) is
  101.       loop [beg.step!(num,step)]:=val; yield end end;
  102.    
  103.    acopy(src:SAME)
  104.       -- Copy as many elements from `src' to self as will fit.
  105.       -- Built-in.
  106.       pre ~void(self) and ~void(src) is
  107.       loop aset!(src.aelt!) end end;
  108.    
  109.    acopy(beg:INT, src:SAME)
  110.       -- Copy as many elements from `src' to self as will fit when
  111.       -- starting at index `beg' of self. Built-in.
  112.       pre ~void(self) and ~void(src) and (beg.is_bet(0,asize-1) or src.asize=0) is
  113.       loop aset!(beg,src.aelt!) end end;
  114.    
  115.    acopy(beg,num:INT, src:SAME)
  116.       -- Copy `num' elements from `src' to self starting at index
  117.       -- `beg' of self. Built-in.
  118.       pre ~void(self) and ~void(src) and beg.is_bet(0,asize-1) and 
  119.           num.is_bet(0,asize-beg) and num<=src.asize is
  120.       loop aset!(beg,num,src.aelt!) end end;
  121.    
  122.    acopy(beg,num,srcbeg:INT, src:SAME)
  123.       -- Copy `num' elements from `src' to self starting at index
  124.       -- `beg' of self and index `srcbeg' of `src'. Built-in.
  125.       pre ~void(self) and ~void(src) and beg.is_bet(0,asize-1) and 
  126.           num.is_bet(0,asize-beg) and num<=src.asize-srcbeg is   
  127.       loop aset!(beg,num,src.aelt!(srcbeg,num)) end end;
  128.    
  129.    aind!:INT
  130.       -- Yield the indices of self in order. Built-in.
  131.       pre ~void(self) is loop yield asize.times! end end;
  132.  
  133. end; -- class AREF{T}
  134.  
  135. -------------------------------------------------------------------
  136. class TEST_AREF is
  137.    include TEST;
  138.    
  139.    main is
  140.       class_name("AREF{INT}");
  141.       a ::= #AREF{INT}(5);
  142.       a[0] := 0; a[1] := 1; a[2] := 2; a[3]:=3; a[4]:=4;
  143.       test("aset and aget",a[2].str,"2");
  144.       res::=""; loop res := res+" "+a.aelt!; end;
  145.       test("aelt!",res," 0 1 2 3 4");
  146.       res:=""; loop res := res+" "+a.aelt!(1); end;
  147.       test("aelt!(beg)",res," 1 2 3 4");
  148.       res:=""; loop res := res+" "+a.aelt!(1,2); end;
  149.       test("aelt!(1,2)",res," 1 2");
  150.       res:=""; loop res := res+" "+a.aelt!(1,2,2); end;
  151.       test("aelt!(1,2,2)",res," 1 3");
  152.  
  153.  
  154.       b ::=#AREF{INT}(a.asize);  
  155.       
  156.       b.acopy(a);
  157.       res:=""; loop res := res+" "+b.aelt!; end;
  158.       test("acopy",res," 0 1 2 3 4");
  159.       
  160.       c ::= #AREF{INT}(3);
  161.       c.acopy(a);
  162.       res:=""; loop res := res+" "+c.aelt!; end;
  163.       test("acopy",res," 0 1 2");
  164.       
  165.       loop b.aset!(1); end;
  166.       res:=""; loop res := res+" "+b.aelt!; end;
  167.       test("aset",res," 1 1 1 1 1");
  168.       
  169.       b.acopy(a);
  170.       loop b.aset!(3,1); end;
  171.       res:=""; loop res := res+" "+b.aelt!; end;
  172.       test("aset(3,1)",res," 0 1 2 1 1");
  173.       
  174.       b.acopy(a);
  175.       loop b.aset!(2,2,7); end;
  176.       res:=""; loop res := res+" "+b.aelt!; end;
  177.       test("aset(2,2,7)",res," 0 1 7 7 4");
  178.  
  179.       b.acopy(a);
  180.       loop b.aset!(1,2,2,9); end;
  181.       res:=""; loop res := res+" "+b.aelt!; end;
  182.       test("aset(1,2,2,9)",res," 0 9 2 9 4");
  183.       
  184.       b.acopy(1,a);
  185.       res:=""; loop res := res+" "+b.aelt!; end;
  186.       test("acopy(1)",res," 0 0 1 2 3");
  187.       
  188.       b.aclear;
  189.       res:=""; loop res := res+" "+b.aelt!; end;
  190.       test("acopy(1)",res," 0 0 0 0 0");
  191.       
  192.       b.acopy(2,2,a);
  193.       res:=""; loop res := res+" "+b.aelt!; end;
  194.       test("acopy(2,2)",res," 0 0 0 1 0");
  195.  
  196.       b.acopy(2,2,2,a);
  197.       res:=""; loop res := res+" "+b.aelt!; end;
  198.       test("acopy(2,2)",res," 0 0 2 3 0");
  199.       finish;
  200.       end;
  201.    
  202. end; -- class TEST_AREF
  203.  
  204. -------------------------------------------------------------------
  205.