home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 7 / Apprentice-Release7.iso / Source Code / C / Applications / Moscow ML 1.42 / lib / Array2.sig < prev    next >
Encoding:
Text File  |  1997-08-18  |  5.7 KB  |  140 lines  |  [TEXT/Moml]

  1. (* Array2 -- SML Basis Library *)
  2.  
  3. eqtype 'a array2
  4.  
  5. val array      : int * int * '_a -> '_a array2
  6. val fromList   : '_a list list -> '_a array2
  7. val tabulate   : int * int * (int * int -> '_a) -> '_a array2
  8.  
  9. val dimensions : 'a array2 -> int * int
  10. val width      : 'a array2 -> int
  11. val height     : 'a array2 -> int
  12.  
  13. val sub        : 'a array2 * int * int -> 'a
  14. val update     : 'a array2 * int * int * 'a -> unit
  15.  
  16. val row        : 'a array2 * int -> 'a Vector.vector
  17. val column     : 'a array2 * int -> 'a Vector.vector
  18.  
  19. type region = { row : int, col : int, ht : int option, wd : int option}
  20. datatype traversal = RowMajor | ColMajor
  21.  
  22. val copy       : { src : 'a array2, dst : 'a array2, src_reg : region, 
  23.            dst_row : int, dst_col : int } -> unit
  24.  
  25. val app        : ('a -> unit) -> 'a array2 -> unit
  26. val modify     : ('a -> 'a) -> 'a array2 -> unit
  27. val fold       : traversal -> ('a * 'b -> 'b) -> 'b -> 'a array2 -> 'b
  28.  
  29. val appi       : (int * int * 'a -> unit) -> 'a array2 * region -> unit
  30. val modifyi    : (int * int * 'a -> 'a) -> 'a array2 * region -> unit
  31. val foldi      : traversal -> (int * int * 'a * 'b -> 'b) -> 'b 
  32.                  -> 'a array2 * region -> 'b
  33.  
  34.  
  35. (* Type [ty array2] is the type of two-dimensional, mutable,
  36.    zero-based constant-time-access arrays with elements of type ty.
  37.    Type ty array admits equality even if ty does not.  Arrays a1 and
  38.    a2 are equal if both were created by the same call to one of the
  39.    primitives array, fromList, and tabulate.
  40.  
  41.    [array(m, n, x)] returns a new m * n matrix whose elements are all x.  
  42.    Raises Size if n<0 or m<0.
  43.  
  44.    [fromlist xss] returns a new array whose first row has elements
  45.    xs1, second row has elements xs2, ..., where xss = [xs1,xs2,...,xsm].  
  46.    Raises Size if the lists in xss do not all have the same length.
  47.  
  48.    [tabulate(m, n, f)] returns a new m-by-n array whose elements
  49.    are f(0,0), f(0,1), ..., f(0, n-1), 
  50.        f(1,0), f(1,1), ..., f(1, n-1),
  51.                     ...
  52.        f(m-1,0),    ...,    f(m-1, n-1)
  53.    created in that order.  Raises Size if n<0 or m<0.
  54.  
  55.    [dimensions a] returns the dimensions (m, n) of a, where m is the
  56.    number of rows and n the number of columns.
  57.  
  58.    [width a] returns the number of n of columns of a.
  59.  
  60.    [height a] returns the number of m of rows of a.
  61.  
  62.    [sub(a, i, j)] returns the i'th row's j'th element, counting from 0.
  63.    Raises Subscript if i<0 or j<0 or i>=m or j>=n 
  64.    where (m,n) = dimensions a.
  65.  
  66.    [update(a, i, j, x)] destructively replaces the (i,j)'th element of a 
  67.    by x. Raises Subscript if i<0 or j<0 or i>=m or j>=n 
  68.    where (m,n) = dimensions a. 
  69.  
  70.    [row (a, i)] returns a vector containing the elements of the ith
  71.    row of a.  Raises Subscript if i < 0 or i >= height a.
  72.  
  73.    [column (a, j)] returns a vector containing the elements of the jth
  74.    column of a.  Raises Subscript if j < 0 or j >= width a.
  75.  
  76.    [app f a] applies f to the elements a[0,0], a[0,1], ..., a[0,n-1],
  77.    a[1,0], ..., a[m-1, n-1] of a, where (m, n) = dimensions a.
  78.  
  79.    [modify f a] applies f to the elements a[0,0], a[0,1], ..., a[0,n-1], 
  80.    a[1,0], ..., a[m-1, n-1] of a, updating each element with the
  81.    result of the application, where (m, n) = dimensions a.
  82.  
  83.    [fold RowMajor f b a] folds f left-right and top-down over the
  84.    elements of in row-major order.  That is, computes
  85.     f(a[m-1, n-1], f(a[m-1, n-2], ..., f(a[0,1], f(a[0,0], b)) ...))
  86.    where (m, n) = dimensions a.
  87.  
  88.    [fold ColMajor f b a] folds f left-right and top-down over the
  89.    elements of a in column-major order.  That is, computes
  90.     f(a[m-1, n-1], f(a[m-2, n-1], ..., f(a[1,0], f(a[0,0], b)) ...))
  91.    where (m, n) = dimensions a.
  92.  
  93.  
  94.    The following iterators generalize the above ones in two ways:
  95.  
  96.      . the indexes i and j are also being passed to the function;
  97.      . the iterators work on a region (submatrix) of a matrix.          
  98.  
  99.    The region { row, col, ht, wd } determines a region or submatrix
  100.    whose upper left corner has index (row, col).  
  101.    If ht = SOME h, then the region has h rows: row, row+1, ..., row+h-1.
  102.    If ht = NONE, then the region extends to the bottom of the matrix.
  103.    The field wd similarly determines the number of columns.
  104.  
  105.    A region is valid for an array with dimensions (m, n) if 
  106.        (1) either ht = NONE and 0 <= row <= m 
  107.            or ht = SOME h and 0 <= row <= row + h <= m 
  108.    and (2) either wd = NONE and 0 <= col <= n
  109.            or wd = SOME w and 0 <= col <= col + w <= n.
  110.  
  111.    [appi f (a, reg)] applies f to (i, j, a[i, j]) for lexicographically 
  112.    increasing (i, j) within the region reg.  Raises Subscript if reg is 
  113.    not valid.  Note that app f a has the same effect as 
  114.    appi (f o #3) (a, {row=0, col=0, ht=NONE, wd=NONE}).
  115.  
  116.    [modifyi f (a, reg)] applies f to (i, j, a[i, j]) for lexicographically 
  117.    increasing (i, j) within the region reg.  Raises Subscript if reg is 
  118.    not valid.  Note that modify f a has the same effect as 
  119.    modifyi (f o #3) (a, {row=0, col=0, ht=NONE, wd=NONE}).
  120.  
  121.    [foldi RowMajor f b a] folds f over (i, j, a[i, j]) in row-major
  122.    order within the region reg, that is, for lexicographically
  123.    increasing (i, j) in the region.  Raises Subscript if reg is not
  124.    valid.
  125.  
  126.    [foldi ColMajor f b a] folds f over (i, j, a[i, j]) in column-major
  127.    order within the region reg, that is, for lexicographically
  128.    increasing (j, i) in the region.  Raises Subscript if reg is not
  129.    valid.
  130.  
  131.    [copy { src, dst, src_reg, dst_row, dst_col }] copies the region
  132.    src_reg from array src to array dst, such that the upper leftmost
  133.    corner of src_reg is copied to dst[dst_row, dst_col].  Works
  134.    correctly even when src and dst are the same and the source region
  135.    overlaps with the destination region.  Raises Subscript if the
  136.    region src_reg is invalid for src, or if src_reg translated to
  137.    (dst_row, dst_col) is invalid for dst.
  138.  
  139. *)
  140.