a_list.sa


Generated by gen_html_sa_files from ICSI. Contact gomes@icsi.berkeley.edu for details
 
---------------------------> Sather 1.1 source file <--------------------------
-- a_list.sa: Array based list
-- Author: Benedict A. Gomes <gomes@samosa.ICSI.Berkeley.EDU>
-- Copyright (C) 1995, International Computer Science Institute
-- $Id: a_list.sa,v 1.6 1996/04/09 10:04:31 borisv Exp $
--
-- COPYRIGHT NOTICE: This code is provided WITHOUT ANY WARRANTY
-- and is subject to the terms of the SATHER LIBRARY GENERAL PUBLIC
-- LICENSE contained in the file: Sather/Doc/License of the
-- Sather distribution. The license is also available from ICSI,
-- 1947 Center St., Suite 600, Berkeley CA 94704, USA.


class LIST{ETP} < $LIST{ETP}

class LIST{ETP} < $LIST{ETP} is include A_LIST{ETP}; end;

class A_LIST{ETP} < $LIST{ETP}

class A_LIST{ETP} < $LIST{ETP} is -- A safe wrapper for FLIST{ETP} private attr arr: FLIST{ETP}; -- ------ Initialization/Duplication ------ create: SAME is res ::= new; res.arr := #FLIST{ETP}(5); return res; end; create_sized(n: INT): SAME post result.size = n is -- Create a list with "n" empty locations if n = 0 then return #SAME; else res ::= new; res.arr := FLIST{ETP}::create_empty_sized(n); return res; end; end; create_from(a: ARRAY{ETP}): SAME is -- Create an array which contains the elements of "a" return create(a) end; create(a: $ELT{ETP}): SAME is -- Convert "a" into a list res: SAME := #; loop res.append(a.elt!) end; return res; end; resize(n: INT) pre ~void(self) is -- Allocate a new array and copy whatever will fit of the -- old portion. Changes the actual size resarr::=FLIST{ETP}::create_empty_sized(n); i ::= 0; loop until!(i = n); resarr[i] := elt!; i := i + 1; end; arr := resarr; end; copy:SAME is -- A copy of self. res: SAME := create_sized(size); loop i ::= size.times!; res[i] := [i]; end; return res; end; copy_from(src:$ELT{ETP}) is -- Copy as many elements from `src' to self as will fit. loop set!(src.elt!) end; end; -- ------ Insertion/Removal --------------- remove_index(i: INT) is arr.delete_ordered(i) end; insert_after(l:INT,v:ETP) pre ~void(self) is -- Insert v at location i, pushing elements upward arr := arr.insert_after(l,v); end; insert_before(l: INT, v: ETP) pre ~void(self) is arr := arr.insert_before(l,v); end; insert_all_after(l: INT, e: $CONTAINER{ETP}) pre ~void(self) is arr := arr.insert_all_after(l,e); end; insert_all_before(l: INT, e: $CONTAINER{ETP}) pre ~void(self) is arr := arr.insert_all_before(l,e); end; append(a:ETP) is -- Append element "a", even if a is void arr := arr.push(a); end; append_all(a:$CONTAINER{ETP}) is -- Append a into self if void(a) then return else loop arr := arr.push(a.elt!); end; end; end; clear pre ~void(self) is -- Set each array element to void. Set size to zero arr.clear end; -- ------ Access/Measurement -------------- aget(i: INT): ETP pre has_ind(i) is return arr[i] end; aset(i: INT,v: ETP) pre has_ind(i) is arr[i] := v end; size: INT is return arr.size end; -- ------ Queries/Comparison -------------- has(e: ETP): BOOL is return arr.has(e) end; -- True if array contains "e" has_ind(i: INT): BOOL is return ~void(self) and 0<=i and i < size end; has_range(beg,num: INT): BOOL is if ~void(self) then return 0<=beg and beg<size and num>= 0 and beg+num<=size; else return false end; end; equals(a: $RO_ARR{ETP}):BOOL is return arr.equals(a) end; -- ------ Cursor -------------------------- ind!:INT is -- Yield the indices of self in order. Self may be void. i::=0; sz::=size; loop until!(i>=sz); yield i; i := i+1; end; end; elt!: ETP is -- Yield each element of self in order. Self may be void. if ~void(self) then sz: INT := size; i::=0; loop until!(i = sz); yield arr[i]; i := i + 1; end; end end; set!(val:ETP) is -- Set successive elements of self to the values of `val'. sz: INT := size; i::=0; loop until!(i = sz); arr[i] := val; yield; i := i + 1; end; end; -- ------ Conversion ---------------------- str: STR is -- Prints out a string version of the array of the components -- that are under $STR, and their associated indices res ::= #FSTR("{"); i:INT :=0; asz ::= size; loop until!(i>=asz); e ::= [i]; res := res+",".separate!(elt_str(e,i)); i := i + 1; end; res := res +"}"; return(res.str); end; private elt_str(e: ETP,i: INT): STR is typecase e when $STR then return e.str else return "Unprintable:"+i end; end; -- subarr(beg,num:INT): LIST{ETP} -- -- A new array with `num' entries copied from self -- -- starting at `beg'. Self may not be void. -- pre ~void(self) and beg.is_bet(0,size-1) and num.is_bet(0,size-beg) -- is -- r::=create(num); -- r.copy_from(beg,num,self); -- return r -- end; end;