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 >
Wrap
Text File
|
1994-10-28
|
8KB
|
205 lines
-- Copyright (C) International Computer Science Institute, 1994. COPYRIGHT --
-- NOTICE: This code is provided "AS IS" WITHOUT ANY WARRANTY and is subject --
-- to the terms of the SATHER LIBRARY GENERAL PUBLIC LICENSE contained in --
-- the file "Doc/License" of the Sather distribution. The license is also --
-- available from ICSI, 1947 Center St., Suite 600, Berkeley CA 94704, USA. --
--------> Please email comments to "sather-bugs@icsi.berkeley.edu". <----------
-- aref.sa: Built-in reference arrays, primarily to be included.
-------------------------------------------------------------------
class AREF{T} is
-- Built-in arrays of elements of type T. Primarily intended to be
-- included in reference classes that need an array portion in their
-- objects. Array indices start at 0 and go up to "asize-1".
-- All feature names begin with "a" to minimize name conflicts when
-- include. None of the features work with void(self). This class is
-- primarily meant to be included. ARRAY{T} provides an array
-- abstraction that is meant to be used directly.
asize:INT
-- The number of elements in self. Classes which inherit this may
-- replace this by a constant to get constant sized objects (and
-- the compiler may optimize certain operations in this case).
-- Built-in.
pre ~void(self) is
raise "AREF{T}::asize:INT undefined." end;
create(n:INT):SAME
-- A new array with `n' elements.
pre n>=0 is
return new(n) end;
aget(ind:INT):T
-- The element of self with index `ind'. Built-in.
pre ~void(self) and ind.is_bet(0,asize-1) is
raise "AREF{T}::aget(INT):T undefined." end;
aset(ind:INT, val:T)
-- Set the element of self with index `ind' to `val'. Built-in.
pre ~void(self) and ind.is_bet(0,asize-1) is
raise "AREF{T}::aset(INT,T) undefined." end;
aclear
-- Set each element of self to nil. Built-in.
pre ~void(self) is
nil:T; loop aset!(nil) end end;
aelt!:T
-- Yield each element of self in order. Built-in.
pre ~void(self) is
loop yield [asize.times!] end end;
aelt!(beg:INT):T
-- Yield each element of self starting at `beg'. Built-in.
pre ~void(self) and beg.is_bet(0,asize-1) is
loop yield [beg.upto!(asize-1)] end end;
aelt!(beg,num:INT):T
-- Yield `num' successive elements of self starting at
-- index `beg'. Built-in.
pre ~void(self) and beg.is_bet(0,asize-1) and
num.is_bet(0,asize-beg) is
loop yield [beg.upto!(beg+num-1)] end end;
private is_legal_aelts_arg(beg,num,step:INT):BOOL is
-- True if the arguments are legal values for `aelts'.
return beg.is_bet(0,asize-1) and
((step>0 and num.is_bet(0,(asize-beg+step-1)/step)) or
(step<0 and num.is_bet(0,(beg-step)/-step))) end;
aelt!(beg,num,step:INT):T
-- Yield `num' elements of self starting at `beg' and stepping
-- by `step' which must not be zero. Built-in.
pre ~void(self) and is_legal_aelts_arg(beg,num,step) is
loop yield [beg.step!(num,step)] end end;
aset!(val:T!)
-- Set successive elements of self to the values `val'.
-- Built-in.
pre ~void(self) is
loop [asize.times!]:=val; yield end end;
aset!(beg:INT,val:T!)
-- Set successive elements of self starting at `beg' to the
-- values `val'. Built-in.
pre ~void(self) and beg.is_bet(0,asize-1) is
loop [beg.upto!(asize-1)]:=val; yield end end;
aset!(beg,num:INT,val:T!)
-- Set `num' successive elements of self starting at `beg'
-- to the values `val'. Built-in.
pre ~void(self) and beg.is_bet(0,asize-1) and
num.is_bet(0,asize-beg) is
loop [beg.upto!(beg+num-1)]:=val; yield end end;
aset!(beg,num,step:INT, val:T!)
-- Set `num' elements of self starting at `beg' stepping
-- by `step' to the values `val'. `step' must not be zero.
-- Built-in.
pre ~void(self) and is_legal_aelts_arg(beg,num,step) is
loop [beg.step!(num,step)]:=val; yield end end;
acopy(src:SAME)
-- Copy as many elements from `src' to self as will fit.
-- Built-in.
pre ~void(self) and ~void(src) is
loop aset!(src.aelt!) end end;
acopy(beg:INT, src:SAME)
-- Copy as many elements from `src' to self as will fit when
-- starting at index `beg' of self. Built-in.
pre ~void(self) and ~void(src) and (beg.is_bet(0,asize-1) or src.asize=0) is
loop aset!(beg,src.aelt!) end end;
acopy(beg,num:INT, src:SAME)
-- Copy `num' elements from `src' to self starting at index
-- `beg' of self. Built-in.
pre ~void(self) and ~void(src) and beg.is_bet(0,asize-1) and
num.is_bet(0,asize-beg) and num<=src.asize is
loop aset!(beg,num,src.aelt!) end end;
acopy(beg,num,srcbeg:INT, src:SAME)
-- Copy `num' elements from `src' to self starting at index
-- `beg' of self and index `srcbeg' of `src'. Built-in.
pre ~void(self) and ~void(src) and beg.is_bet(0,asize-1) and
num.is_bet(0,asize-beg) and num<=src.asize-srcbeg is
loop aset!(beg,num,src.aelt!(srcbeg,num)) end end;
aind!:INT
-- Yield the indices of self in order. Built-in.
pre ~void(self) is loop yield asize.times! end end;
end; -- class AREF{T}
-------------------------------------------------------------------
class TEST_AREF is
include TEST;
main is
class_name("AREF{INT}");
a ::= #AREF{INT}(5);
a[0] := 0; a[1] := 1; a[2] := 2; a[3]:=3; a[4]:=4;
test("aset and aget",a[2].str,"2");
res::=""; loop res := res+" "+a.aelt!; end;
test("aelt!",res," 0 1 2 3 4");
res:=""; loop res := res+" "+a.aelt!(1); end;
test("aelt!(beg)",res," 1 2 3 4");
res:=""; loop res := res+" "+a.aelt!(1,2); end;
test("aelt!(1,2)",res," 1 2");
res:=""; loop res := res+" "+a.aelt!(1,2,2); end;
test("aelt!(1,2,2)",res," 1 3");
b ::=#AREF{INT}(a.asize);
b.acopy(a);
res:=""; loop res := res+" "+b.aelt!; end;
test("acopy",res," 0 1 2 3 4");
c ::= #AREF{INT}(3);
c.acopy(a);
res:=""; loop res := res+" "+c.aelt!; end;
test("acopy",res," 0 1 2");
loop b.aset!(1); end;
res:=""; loop res := res+" "+b.aelt!; end;
test("aset",res," 1 1 1 1 1");
b.acopy(a);
loop b.aset!(3,1); end;
res:=""; loop res := res+" "+b.aelt!; end;
test("aset(3,1)",res," 0 1 2 1 1");
b.acopy(a);
loop b.aset!(2,2,7); end;
res:=""; loop res := res+" "+b.aelt!; end;
test("aset(2,2,7)",res," 0 1 7 7 4");
b.acopy(a);
loop b.aset!(1,2,2,9); end;
res:=""; loop res := res+" "+b.aelt!; end;
test("aset(1,2,2,9)",res," 0 9 2 9 4");
b.acopy(1,a);
res:=""; loop res := res+" "+b.aelt!; end;
test("acopy(1)",res," 0 0 1 2 3");
b.aclear;
res:=""; loop res := res+" "+b.aelt!; end;
test("acopy(1)",res," 0 0 0 0 0");
b.acopy(2,2,a);
res:=""; loop res := res+" "+b.aelt!; end;
test("acopy(2,2)",res," 0 0 0 1 0");
b.acopy(2,2,2,a);
res:=""; loop res := res+" "+b.aelt!; end;
test("acopy(2,2)",res," 0 0 2 3 0");
finish;
end;
end; -- class TEST_AREF
-------------------------------------------------------------------