array2.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 ARRAY2{T} < $ELT{T}

class ARRAY2{T} < $ELT{T} is -- This class implements the two-dimensional arrays of elements of -- type T. -- Version 1.2 Nov 98. Copyright K Hopper, U of Waikato -- Development History -- ------------------- -- Date Who By Detail -- ---- ------ ------ -- 11 Apr 96 bg Original -- 13 Jan 97 kh Changed to CARD from INT -- 4 Nov 98 kh Refined from 1.2 dist, added pre/posts private include AREF{T} aclear -> aclear, array_ptr -> array_ptr ; readonly attr size2 : CARD ; -- Size of fastest changing dim. readonly attr size1 : CARD ; -- Size of slowest changing dim. create( dim1, dim2 : CARD -- Big case for ORDINAL!!!!! ) : SAME pre (dim1 > 0) and (dim2 > 0) post ~void(result) is -- This routine creates a new two-dimensional array with the given -- dimensions initialised to void. me : SAME := new(dim1 * dim2) ; me.size1 := dim1 ; me.size2 := dim2 ; return me end ; create( arr : ARRAY{ARRAY{T}} ) : SAME pre (arr.size > 0) and (arr[0].size > 0) post (result.size = (arr.size * arr[0].size)) is -- This routine creates a new two-dimensional array from the array -- of arrays argument. -- -- NOTE This routine ASSUMES that all rows of arr have the same number of -- elements! sz1 : CARD := arr.size ; sz2 : CARD := arr[0].size ; me : SAME := create(sz1,sz2) ; loop out_index : CARD := sz1.times! ; loop in_index : CARD := sz2.times! ; me[out_index,in_index] := arr[out_index][in_index] end end ; return me end ; copy : SAME pre ~void(self) and (size1 > 0) and (size2 > 0) post true is -- This returns a new two-dimensional array which has all elements set -- to have the value of the corresponding elements of self. res : SAME := create(size1,size2) ; res.acopy(self) ; return res end ; copy( arr : ARRAY{ARRAY{T}} ) pre ~void(arr) post true is -- This routine copies as much of the data in arr as will fit into self, -- row by row. loop res : CARD := size1.min(arr.size).times! ; row : ARRAY{T} := arr[res] ; loop set_row!(res,row.elt!) end end end ; nr : CARD pre ~void(self) post (result = size1) is -- This routine returns the number of elements in the first dimension -- (rows) of the array. return size1 end ; nc : CARD pre ~void(self) post (result = size2) is -- This routine returns the number of elements in the second dimension -- (columns) of the array. return size2 end ; size : CARD is -- This routine returns the total size of the two-dimensional array as -- the product of row and column sizes. if void(self) then return 0 else return size1 * size2 end end ; aget( out_index, in_index : CARD ) : T pre ~void(self) and (out_index < size1) and (in_index < size2) post (result = [out_index * size2 + in_index]) is -- This routine returns the value of the element indicated by the outer -- and inner indices given. return([out_index * size2 + in_index]) end ; aset( out_index, in_index : CARD, val : T ) pre ~void(self) and (out_index < size1) and (in_index < size2) post ([out_index * size2 + in_index] = val) is -- This routine sets the element indicated by the given outer and inner -- indices to have the value val. [out_index * size2 + in_index] := val end ; private to_transpose_of( arr : SAME ) pre ~void(self) and ~void(arr) and (arr.size1 = size2) and (arr.size2 = size1) post true is -- This routine sets self to be the array formed by transposing row -- and column indices. loop indices : TUP{CARD,CARD} := inds! ; [indices.t1,indices.t2] := arr[indices.t2,indices.t1] end end ; transpose : SAME pre ~void(self) post true -- (result = to_transpose_of(self)) is -- This routine returns a new array with the row and column indices -- transposed. res : SAME := create(size2,size1) ; res.to_transpose_of(self) ; return res end ; to_portion_of( arr : SAME ) pre ~void(self) and ~void(arr) post true is -- This routine copies into self as much of the argument array as will -- fit. If there are more elements in self then these are not altered. loop index : CARD :=size1.min(arr.size1).times! ; loop set1!(index,arr.elt1!(index)) end end end ; resize( sz1, sz2 : CARD ) : SAME pre ~void(self) and(sz1 > 0) and (sz2 > 0) post (result.size1 = sz1) and (result.size2 = sz2) is -- This routine creates a new two-dimensional array and copies to it -- as much of self as will fit. Any unset elements will be left void. res : SAME := create(sz1, sz2) ; col : CARD ; loop size2.min(sz1).times! ; col := 0.upto!(sz2.min(size1)) ; loop res.set2!(col,elt2!(col)) end end ; return res end ; -- The remaining portion of this class provides iters. ind1! : CARD pre ~void(self) post (result < size1) is -- This iter yields each value of the first index in order from zero. loop yield(size1.times!) end end ; ind2! : CARD pre ~void(self) post (result < size2) is -- This iter yields each value of the second index in order from zero. loop yield(size2.times!) end end ; row_ind! : CARD pre ~void(self) post (result < size1) is -- This iter yields each value of the first index in order from zero. loop yield(size1.times!) end end ; col_ind! : CARD pre ~void(self) post (result < size2) is -- This iter yields each value of the second index in order from zero. loop yield(size2.times!) end end ; diag_elt! : T pre ~void(self) post true -- (result = [ind1!,ind1!]) is -- This iter yields successive values of the elements on the diagonal -- of self (defined as being the line across the 'square' of the smaller -- dimension). loop index : CARD := (size1.min(size2)).times! ; yield([index,index]) end end ; set_diag_elt!( val : T ) pre ~void(self) post true -- ([ind1!,ind1!] = val) is -- This iter sets the value of successive elements on the diagonal of -- self to values val. The diagonal is defined as being the line across -- the 'square' of the smaller dimension. loop index : CARD := (size1.min(size2)).times! ; [index,index] := val ; yield end end ; inds! : TUP{CARD,CARD} pre ~void(self) post (result.t1 < size1) and (result.t2 < size2) is -- This iter yields tuples of the indices of self by rows and columns -- within rows. loop row : CARD := size1.times! ; loop yield(TUP{CARD,CARD}::create(row,size2.times!)) end end end ; elt! : T pre ~void(self) post true -- (result = [aind!]) is -- This iter yields the values of the elements of self by row and -- column within row. loop yield(aelt!) end end ; set!( val : T ) pre ~void(self) post true -- ([aind!] = val) is -- This iter sets the values of the elements of self by row and -- column within row to the given values. loop aset!(val) ; yield end end ; elt1!( once out_index : CARD ) : T pre ~void(self) and (out_index < size1) post true is -- This iter yields the elements of the sequence formed by keeping the -- outer index constant and varying the inner one. loop yield(aelt!(out_index * size2,size2,1.int)) end end ; elt2!( once in_index : CARD ) : T pre ~void(self) and (in_index < size2) post true is -- This iter yields the elements of the sequence formed by keeping the -- inner index constant and varying the outer one. loop yield(aelt!(in_index,size1,size2.int)) end end ; row_elt!( once row : CARD ) : T pre ~void(self) and (row < size1) post true is -- This iter yields the elements of a row of the two-dimensional array -- indicated by the row argument. loop yield(aelt!(row * size2,size2,1.int)) end end ; col_elt!( once column : CARD ) : T pre ~void(self) and (column < size2) post true is -- This iter yields the elements of a column of the two-dimensional -- array indicated by the column argument. loop yield(aelt!(column,size1,size2.int)) end end ; set1!( once out_index : CARD, val : T ) pre ~void(self) and (out_index < size1) post true is -- This iter sets successive elements of the 'row' indicated by -- out_index to the values val. loop aset!(out_index * size2,size2,val) ; yield end end ; set2!( once in_index : CARD, val : T ) pre ~void(self) and (in_index < size2) post true is -- This iter sets successive elements of the 'column' indicated by -- in_index to the values val. loop aset!(in_index,size1,size2.int,val) ; yield end end ; set_row!( once row : CARD, val : T ) pre ~void(self) and (row < size1) post true is -- This iter sets successive elements of the given row to the values -- val. loop aset!(row * size2,size2,val) ; yield end end ; set_col!( once column : CARD, val : T ) pre ~void(self) and (column < size2) post true is -- This iter sets successive elements of the given column to the values -- val. loop aset!(column,size1,size2.int,val) ; yield end end ; end ; -- ARRAY2{T}