The Standard ML Basis Library


The MONO_ARRAY signature

The MONO_ARRAY signature is a generic interface to monomorphic mutable sequences. Arrays also have a special equality property: two arrays are equal if they are the same array, i.e., created by the same call to a primitive array constructor such as array, fromList, etc.; otherwise they are not equal. This also holds for arrays of zero length.


Synopsis

signature MONO_ARRAY
structure Word8Array : MONO_ARRAY
structure CharArray : MONO_ARRAY
structure WideCharArray : MONO_ARRAY
structure BoolArray : MONO_ARRAY
structure IntArray : MONO_ARRAY
structure RealArray : MONO_ARRAY
structure Int{N}Array : MONO_ARRAY
structure Real{N}Array : MONO_ARRAY

Interface

eqtype array
type elem
structure Vector : MONO_VECTOR
val maxLen : int
val array : (int * elem) -> array
val fromList : elem list -> array
val tabulate : (int * (int -> elem)) -> array
val length : array -> int
val sub : (array * int) -> elem
val update : (array * int * elem) -> unit
val extract : (array * int * int option) -> Vector.vector
val copy : {src : array, si : int, len : int option, dst : array, di : int} -> unit
val copyVec : {src : Vector.vector, si : int, len : int option, dst : array, di : int} -> unit
val appi : ((int * elem) -> unit) -> (array * int * int option) -> unit
val app : (elem -> unit) -> array -> unit
val foldli : ((int * elem * 'b) -> 'b) -> 'b -> (array * int * int option) -> 'b
val foldri : ((int * elem * 'b) -> 'b) -> 'b -> (array * int * int option) -> 'b
val foldl : ((elem * 'b) -> 'b) -> 'b -> array -> 'b
val foldr : ((elem * 'b) -> 'b) -> 'b -> array -> 'b
val modifyi : ((int * elem) -> elem) -> (array * int * int option) -> unit
val modify : (elem -> elem) -> array -> unit

Description

eqtype array

type elem

structure Vector

maxLen
is the maximum length of arrays supported by this implementation. Attempts to create larger arrays will result in the Size exception being raised.

array (n, init)
creates a new array of length n; each element is initialized to the value init. If n < 0 or maxLen < n, then the Size exception is raised.

fromList l
creates a new array from a list of elements. If the length of the list is greater than maxLen, then the Size exception is raised.

tabulate (n, f)
creates an array of n elements, where the elements are defined in order of increasing index by applying f to the element's index. This is equivalent to the expression:
	  fromList (List.tabulate (n, f))
	  
If n < 0 or maxLen < n, then the Size exception is raised.

length arr
returns |arr|, the length of the array arr.

sub (arr, i)
returns the ith element of the array arr. If i < 0 or |arr| <= i, then the Subscript exception is raised.

update (arr, i, x)
sets the ith element of the array arr to x. If i <= 0 or |arr| < i, then the Subscript exception is raised.

extract slice
extracts the array slice slice from the array arr, and returns it as a vector. If the slice is not valid, then the exception Subscript is raised.

copy {src, si, len, dst, di}
copyVec {src, si, len, dst, di}
copy the slice specified by (src, si, len) into the array dst, with element si being copied to position di in the destination array. The function copy takes an array slice as its source, while the function copyVec uses a vector slice. If the source slice is not valid, then the Subscript exception is raised. Likewise, if di < 0 or if |dst| < di+n, where n is the number of elements copied, then the Subscript exception is raised.
Implementation note:

The copy function must correctly handle the case in which src and dst are equal, and the source and destination slices overlap.



appi f slice
app f arr
apply the function f to the elements of an array in left to right order (i.e., increasing indices). The more general appi function applies f to the elements of the array slice slice and supplies both the element and the element's index to the function f. If slice is not valid, then the exception Subscript is raised.

The function app applies f to the whole array and does not supply the element index to f. Thus the expression app f arr is equivalent to:

	    appi (f o #2) (arr, 0, NONE)
	  


foldli f init slice
foldri f init slice
foldl f init arr
foldr f init arr
fold the function f over the elements of an array, using the value init as the initial value. The functions foldli and foldl apply the function f from left to right (increasing indices), while the functions foldri and foldr work from right to left (decreasing indices). The more general functions foldli and foldri work on array slices, and supply both the element and the element's index to the function f.

The functions foldl and foldr work on the whole array arr and do not supply the element index to f. Thus the expression foldl f init arr is equivalent to:

	    foldl (fn (_, a, x) => f(a, x))
	      init (arr, 0, NONE)
	  
Example:

One can extract the list of elements in an array arr by the expression:

	      foldr (op ::) [] arr
	    


modifyi f slice
modify f arr
apply the function f to the elements of an array in left to right order (i.e., increasing indices), and replace the elements with the results of f. The more general modifyi function applies f to the elements of the array slice slice and supplies both the element and the element's index to the function f. If slice is not valid, then the exception Subscript is raised.

The function modify applies f to the whole array and does not supply the element index to f. Thus the expression modify f arr is equivalent to:

	    modifyi (f o #2) (arr, 0, NONE)
	  



See Also

Array, MONO_VECTOR

[ INDEX | TOP | Parent | Root ]

Last Modified April 16, 1997
Comments to John Reppy.
Copyright © 1997 Bell Labs, Lucent Technologies