aref.sa


Generated by gen_html_sa_files from ICSI. Contact gomes@icsi.berkeley.edu for details
 
------------------------->  GNU Sather - sourcefile  <-------------------------
-- Copyright (C) 2000 by K Hopper, University of Waikato, New Zealand        --
-- This file is part of the GNU Sather library. It is free software; you may --
-- redistribute  and/or modify it under the terms of the GNU Library General --
-- Public  License (LGPL)  as published  by the  Free  Software  Foundation; --
-- either version 2 of the license, or (at your option) any later version.   --
-- This  library  is distributed  in the  hope that it will  be  useful, but --
-- WITHOUT ANY WARRANTY without even the implied warranty of MERCHANTABILITY --
-- or FITNESS FOR A PARTICULAR PURPOSE. See Doc/LGPL for more details.       --
-- The license text is also available from:  Free Software Foundation, Inc., --
-- 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA                     --
-------------->  Please email comments to <bug-sather@gnu.org>  <--------------


class AREF{ELT}

class AREF{ELT} is -- This class is forms part of the Sather language. It is designed to -- be included in those library or user-defined classes which are or require -- one or more features which are an array of references. -- -- All feature names begin with "a" to minimize name conflicts when -- included. None of the features work with void(self). -- NOTE This class is designed to be included. The class ARRAY{T} provides -- an array abstraction which may be used directly. -- Version 1.2 May 2001. Copyright K Hopper, U of Waikato -- Development History -- ------------------- -- Date Who By Detail -- ---- ------ ------ -- 16 Dec 96 kh Original from standard Sather dist. -- 3 Jan 97 kh Change indexing to cardinal! -- 24 May 01 kh Added terminated_ptr feature asize : CARD pre ~void(self) post true is -- This routine returns the number of elements in self. Classes which -- inherit this may replace this by a constant to get constant sized objects -- (when the implementationis free to optimize certain operations. builtin AREF_ASIZE end ; create(cnt : CARD) : SAME is -- This routine returns a new array with cnt elements. return new(cnt) end ; aget(index : CARD) : ELT pre ~void(self) and (index <asize) post true -- result = [index] is -- This routine returns the element of self with the given index. builtin AREF_AGET end ; aget(index : INT) : ELT pre ~void(self) and index.is_non_neg and (index.card <asize) is return aget(index.card); end; aset(index : CARD, val : ELT) pre ~void(self) and (index < asize) post true -- [index] = val is -- This routine sets the element of self with the given index to val. builtin AREF_ASET end ; aset(index : INT, val:ELT) pre ~void(self) and index.is_non_neg and (index.card < asize) is aset(index.card,val); end; aclear pre ~void(self) post true -- self = create(self.asize) is -- This routine sets each element of self to the state of a newly -- created object. builtin AREF_ACLEAR end ; private for_all(first, second : SAME) : BOOL is -- This private predicate returns true if and only if all elements of -- first and second are the same! loop index : CARD := first.aind! ; if first[index] /= second[index] then return false end end ; return true end ; acopy(src : SAME) pre ~void(self) and ~void(src) post for_all(self,src) is -- This routine copies as many elements from src to self as will fit. builtin AREF_ACOPY end ; acopy(beg : CARD, src : SAME) pre ~void(self) and ~void(src) and ((beg < asize ) or (src.asize = 0)) post true is -- This routine copies as many elements from src to self as will fit -- when starting at index beg of self. -- -- forall(inds member {0..(beg - 1)} | initial[inds] = self[inds] -- and forall(inds member {beg..(beg + src.size - 1)} : -- inds member(0..(asize - 1)} | -- initial[inds] = src[inds - beg] -- and forall(inds member {(beg + src.size)..(asize - 1)} : -- inds member(0..(asize - 1)} | -- initial[inds] = self[inds] builtin AREF_ACOPY_BEG end ; acopy(beg, num : CARD, src : SAME) pre ~void(self) and ~void(src) and (beg<asize) and (num <=asize-beg) and (num <= src.asize) post true is -- This routine copies num elements from src to self starting at index -- beg of self. -- -- forall(inds member {0..(beg - 1)} | initial[inds] = self[inds] -- and forall(inds member {beg..(beg + num)} : -- inds member(0..(asize - 1)} | -- initial[inds] = src[inds - beg] -- and forall(inds member {(beg + num)..(asize - 1)} : -- inds member(0..(asize - 1)} | -- initial[inds] = self[inds] builtin AREF_ACOPY_BEG_NUM end ; acopy(beg, num, srcbeg : CARD, src : SAME) pre ~void(self) and ~void(src) and (beg< asize) and (num <= asize-beg) and (srcbeg< src.asize) and (num <= src.asize-srcbeg) post true is -- This routine copies num elements from src to self starting at index -- beg of self and index srcbeg of src -- -- forall(inds member {0..(beg - 1)} | initial[inds] = self[inds] -- and forall(inds member {beg..(beg + num)} : -- inds member(0..(asize - 1)} -- and (indsb member {srcbeg..(srcbeg + num - 1)} | -- initial[inds] = src[indsb] -- and forall(inds member {(beg + num)..(asize - 1)} : -- inds member(0..(asize - 1)} | -- initial[inds] = self[inds] builtin AREF_ACOPY_BEG_NUM_SRCBEG end ; array_ptr : REFERENCE pre ~void(self) post ~void(result) is -- This built-in routine returns a reference 'pointer' to self. builtin AREF_ARRAY_PTR end ; aind! : CARD pre ~void(self) post result < asize -- or the iter has quit! is -- This iter yields the indices of self in order. builtin AREF_AINDB end ; aelt! : ELT pre ~void(self) post true -- result = self[aind!] is -- This iter yields each element of self in order builtin AREF_AELTB end ; aelt!(once beg : CARD) : ELT pre ~void(self) and (beg < asize) post true -- and result is next elem. is -- This iter yields each element of self starting at beg builtin AREF_AELT_BEGB end ; aelt!(once beg, once num : CARD) : ELT pre ~void(self) and (beg<asize)and(num <= asize-beg) post true -- and result is next elem. is -- This iter yields num successive elements of self starting at index beg. builtin AREF_AELT_BEG_NUMB end ; private is_legal_aelts_arg(beg, num : CARD, step : INT) : BOOL is -- This predicate returns true if and only if the arguments are legal -- values for aelt and aset iters below. return (beg < asize) and ( ( (step > INT::zero)and (num <= (asize - 1 - beg + step.card)/step.card) ) or ( (step < INT::zero) and (beg >= (-step).card) and (num <= (beg - (-step).card)/(-step).card) ) ) end ; aelt!(once beg,once num : CARD,once step : INT) : ELT pre ~void(self) and is_legal_aelts_arg(beg,num,step) post true -- and result is next elem. is -- This iter yields num elements of self starting at beg and stepping -- by step -- which must not be zero builtin AREF_AELT_BEG_NUM_STEPB end ; aset!(val : ELT) pre ~void(self) post true -- [ind!] = val is -- This iter sets successive elements of self to the value val. builtin AREF_ASETB end ; aset!(once beg : CARD,val : ELT) pre ~void(self) and (beg<asize) post true -- [beg + ind!] = val is -- This iter sets successive elements of self starting at beg to the values val. builtin AREF_ASET_BEGB end ; aset!(once beg, once num : CARD, val : ELT) pre ~void(self) and (beg<asize) and (num<=(asize-beg)) post true -- [beg + ind!] = val is -- This iter sets num successive elements of self starting at beg -- to the values val. builtin AREF_ASET_BEG_NUMB end ; aset!(once beg, once num : CARD, once step : INT,val : ELT) pre ~void(self) and is_legal_aelts_arg(beg,num,step) post true -- [((aind!.int * step) + beg.int).card] = result is -- This iter sets num elements of self starting at beg stepping -- by step to the values val. step must not be zero. builtin AREF_ASET_BEG_NUM_STEPB end ; end ; -- AREF{ELT}