Array
structure
The Array structure provides polymorphic 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. Thus, type t array
admits equality even if ty
does not.
signature ARRAY
structure Array
: ARRAY
eqtype 'a array
type 'a vector
val maxLen : int
val array : (int * 'a) -> 'a array
val fromList : 'a list -> 'a array
val tabulate : (int * (int -> 'a)) -> 'a array
val length : 'a array -> int
val sub : ('a array * int) -> 'a
val update : ('a array * int * 'a) -> unit
val extract : ('a array * int * int option) -> 'a vector
val copy : {src : 'a array, si : int, len : int option, dst : 'a array, di : int} -> unit
val copyVec : {src : 'a vector, si : int, len : int option, dst : 'a array, di : int} -> unit
val appi : ((int * 'a) -> unit) -> ('a array * int * int option) -> unit
val app : ('a -> unit) -> 'a array -> unit
val foldli : ((int * 'a * 'b) -> 'b) -> 'b -> ('a array * int * int option) -> 'b
val foldri : ((int * 'a * 'b) -> 'b) -> 'b -> ('a array * int * int option) -> 'b
val foldl : (('a * 'b) -> 'b) -> 'b -> 'a array -> 'b
val foldr : (('a * 'b) -> 'b) -> 'b -> 'a array -> 'b
val modifyi : ((int * 'a) -> 'a) -> ('a array * int * int option) -> unit
val modify : ('a -> 'a) -> 'a array -> unit
eqtype 'a array
type 'a vector
maxLen
array (n, init)
maxLen
< n, then the Size exception is raised.
fromList l
tabulate (n, f)
fromList (List.tabulate (n, f))If n < 0 or
maxLen
< n, then the Size exception is raised.
length arr
sub (arr, i)
update (arr, i, x)
extract slice
copy {src, si, len, dst, di}
copyVec {src, si, len, dst, di}
(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
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
More precisely, if extract slice = Vector.fromList [A0,A1,...,An]
, then the expression foldli f init slice
is equivalent to:
List.foldl (fn ((i, a), x) => f(i, a, x)) init [(0,A0),(1,A1),...,(n,An)]The expression
foldl f init arr
is equivalent to:
foldli (fn (_, a, x) => f(a, x)) init (arr, 0, NONE)The analogous equivalences hold for foldri and List.foldr.
Example:
One can extract the list of elements in an array arr by the expression:
foldr (op ::) [] arr
modifyi f slice
modify f arr
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)
MONO_ARRAY, Vector
Last Modified April 16, 1996
Comments to John Reppy.
Copyright © 1997 Bell Labs, Lucent Technologies