MONO_ARRAY
signature
signature MONO_ARRAY
structure Word8Array
:> MONO_ARRAY
where type vector = Word8Vector.vector
where type elem = Word8.word
structure CharArray
:> MONO_ARRAY
where type vector = CharVector.vector
where type elem = char
structure WideCharArray
:> MONO_ARRAY (* OPTIONAL *)
where type vector = WideCharVector.vector
where type elem = WideChar.char
structure BoolArray
:> MONO_ARRAY (* OPTIONAL *)
where type vector = BoolVector.vector
where type elem = bool
structure IntArray
:> MONO_ARRAY (* OPTIONAL *)
where type vector = IntVector.vector
where type elem = int
structure WordArray
:> MONO_ARRAY (* OPTIONAL *)
where type vector = WordVector.vector
where type elem = word
structure RealArray
:> MONO_ARRAY (* OPTIONAL *)
where type vector = RealVector.vector
where type elem = real
structure LargeIntArray
:> MONO_ARRAY (* OPTIONAL *)
where type vector = LargeIntVector.vector
where type elem = LargeInt.int
structure LargeWordArray
:> MONO_ARRAY (* OPTIONAL *)
where type vector = LargeWordVector.vector
where type elem = LargeWord.word
structure LargeRealArray
:> MONO_ARRAY (* OPTIONAL *)
where type vector = LargeRealVector.vector
where type elem = LargeReal.real
structure Int<N>Array
:> MONO_ARRAY (* OPTIONAL *)
where type vector = Int{N}Vector.vector
where type elem = Int{N}.int
structure Word<N>Array
:> MONO_ARRAY (* OPTIONAL *)
where type vector = Word{N}Vector.vector
where type elem = Word{N}.word
structure Real<N>Array
:> MONO_ARRAY (* OPTIONAL *)
where type vector = Real{N}Vector.vector
where type elem = Real{N}.real
The MONO_ARRAY
signature is a generic interface to monomorphic arrays, mutable sequences with constant-time access and update. Monomorphic arrays allow more compact representations than the analogous polymorphic arrays over the same element type.
Arrays 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.
eqtype array
type elem
type 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 vector : array -> vector
val copy : {src : array, dst : array, di : int} -> unit
val copyVec : {src : vector, dst : array, di : int} -> unit
val appi : (int * elem -> unit) -> array -> unit
val app : (elem -> unit) -> array -> unit
val modifyi : (int * elem -> elem) -> array -> unit
val modify : (elem -> elem) -> array -> unit
val foldli : (int * elem * 'b -> 'b) -> 'b -> array -> 'b
val foldri : (int * elem * 'b -> 'b) -> 'b -> array -> 'b
val foldl : (elem * 'b -> 'b) -> 'b -> array -> 'b
val foldr : (elem * 'b -> 'b) -> 'b -> array -> 'b
val findi : (int * elem -> bool)
-> array -> (int * elem) option
val find : (elem -> bool) -> array -> elem option
val exists : (elem -> bool) -> array -> bool
val all : (elem -> bool) -> array -> bool
val collate : (elem * elem -> order)
-> array * array -> order
type vector
vector
by |vec|.
val maxLen : int
Size
exception being raised.
array (n, init)
maxLen
< n, then the Size
exception is raised.
fromList l
length l
and with the i(th) element of l used as the i(th) element of the array. If the length of the list is greater than maxLen
, then the Size
exception is raised.
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)
Subscript
exception is raised.
update (arr, i, x)
Subscript
exception is raised.
vector arr
sub (arr, i)
.
copy {src, dst, di}
copyVec {src, dst, di}
Subscript
exception is raised.
Implementation note:
In
copy
, if dst and src are equal, we must havedi = 0
to avoid an exception, andcopy
is then the identity.
appi f arr
app f arr
appi
function supplies both the element and the element's index to the function f. The expression app f arr
is equivalent to:
appi (f o #2) arr
modifyi f arr
modify f arr
modifyi
function supplies both the element and the element's index to the function f. The expression modify f arr
is equivalent to:
modifyi (f o #2) arr
foldli f init arr
foldri f init arr
foldl f init arr
foldr f init arr
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
supply f with the array index of the corresponding element.
The indexed versions could be implemented as:
fun foldli f init seq = let val len = length seq fun loop (i, b) = if i = len then b else loop(i+1,f(i,sub(seq,i),b)) in loop(0,init) end fun foldri f init seq = let val len = length seq fun loop (i, b) = if i = ~1 then b else loop(i-1,f(i,sub(seq,i),b)) in loop(len-1,init) end
The expression foldl f init arr
is equivalent to:
foldli (fn (_, a, x) => f(a, x)) init arrThe analogous equivalences hold for
foldri
and foldr
.
findi f arr
find f arr
true
value is returned. If this occurs, the functions return the element; otherwise, they return NONE
. The more general version findi
also supplies f with the array index of the element and, upon finding an entry satisfying the predicate, returns that index with the element.
exists f arr
f x
evaluates to true
; it returns true
if such an x exists and false
otherwise.
all f arr
f x
evaluates to false
; it returns false
if such an x exists and true
otherwise. It is equivalent to not
(exists
(not
o f) arr))
.
collate f (a1, a2)
Array
,MONO_ARRAY_SLICE
,MONO_VECTOR
,MONO_VECTOR_SLICE
If an implementation provides a structure matching MONO_ARRAY
for some element type ty
, it must provide the corresponding monomorphic structure matching MONO_VECTOR
with the vector types in the two structures identified.
Generated October 02, 2003
Last Modified June 11, 2000
Comments to John Reppy.
This document may be distributed freely over the internet as long as the copyright notice and license terms below are prominently displayed within every machine-readable copy.
Copyright © 2003 AT&T and Lucent Technologies. All rights reserved.
Permission is granted for internet users to make one paper copy for their
own personal use. Further hardcopy reproduction is strictly prohibited.
Permission to distribute the HTML document electronically on any medium
other than the internet must be requested from the copyright holders by
contacting the editors.
Printed versions of the SML Basis Manual are available from Cambridge
University Press.
To order, please visit
www.cup.org (North America) or
www.cup.cam.ac.uk (outside North America). |