Resizable Arrays
An array that can grow automatically as items are added to the end of it. Roughly
equivalent to std::vector
in C++ – i.e., it supports constant time item
retrieval and modification and amortized constant time appends.
Mutable one-dimensional and two-dimensional fixed-size arrays are available from the Standard Basis Library. Immutable one-dimensional fixed-size vectors are also available from the Standard Basis Library.
Basic usage
(* Create an empty array *)
val a = Mlarray.make (0, 0)
(* Append 12 to the array *)
val _ = Mlarray.pushback (a, 12)
(* ln = 1 *)
val ln = Mlarray.length (a)
(* set the first item to 99 *)
val _ = Mlarray.update (a, 0, 99)
Interface
Types
'a mlarray
- A resizable array with elements of type
'a
- A resizable array with elements of type
Methods
val makeInit : int * 'a -> 'a mlarray
val make : int * 'a -> 'a mlarray
val copy : 'a mlarray -> 'a mlarray
val foldl : ('a * 'b -> 'b) * 'b * 'a mlarray -> 'b
val foldli : (int * 'a * 'b -> 'b) * 'b * 'a mlarray -> 'b
val fromList : 'a list -> 'a mlarray
val length : 'a mlarray -> int
val maxLen : 'a mlarray -> int
val modify : ('a -> 'a) * 'a mlarray -> unit
val modifyi : (int * 'a -> 'a) * 'a mlarray -> unit
val sub : 'a mlarray * int -> 'a
val update : 'a mlarray * int * 'a -> unit
val toString : ('a -> string) * 'a mlarray -> string
val toStringSub : ('a -> string) * 'a mlarray * (int * int) -> string
val compre : ('a -> 'b) * 'a mlarray -> 'b mlarray
val comprei : ((int * 'a) -> 'b) * 'a mlarray -> 'b mlarray
val pushback : 'a mlarray * 'a -> unit
val pop : 'a mlarray -> 'a
Method Overview
makeInit (nun, default)
- Makes an array of length
num
with every element having valuedefault
.
- Makes an array of length
make(num, default)
.- Allocate an array that can store
num
items, but is initially empty (i.e.,Mlarray.length
will return 0). Thedefault
value is used to initialize the underlying storage.
- Allocate an array that can store
copy arr
.- Creates a copy of
arr
.
- Creates a copy of
foldl (f, base, arr)
.- Combines the elements of
arr
using the functionf
, starting withbase
. The elements are combined from left to right (lowest index to highest index). The functionf
should take as an argument a tuple of the next element ofarr
to process and the result accumulated so far.
- Combines the elements of
foldli (f, base, arr)
.- Like foldl, but the function also takes indices as input.
fromList lst
- Creates an array containing the contents of the list
lst
.
- Creates an array containing the contents of the list
length arr
- Returns the number of elements in the array.
maxLen arr
- Returns the current capacity of the array.
modify (f, arr)
- Replaces each value in
arr
with the result of applyingf
to the original value. This modifiesarr
.
- Replaces each value in
modifyi (f, arr)
- Like modify, but the modification function also takes indices as input.
sub (arr, n)
.- Returns the
n
th element ofarr
(likearr[n]
in other languages).
- Returns the
update (arr, idx, a)
.- Updates the
idx
th element ofarr
to bea
(likearr[idx] := a
in some languages).
- Updates the
toString (f, arr)
.- Converts
arr
to a string. The functionf
is a function for converting an individual element of the array to a string.
- Converts
toStringSub (f, arr, (start, end))
- Like
toString
, but converts a sub-array to a string. The subarray ranges over indices [start
,end
) (that is, inclusive ofstart
and exclusive ofend
).
- Like
compre (f, arr)
- Creates a new array, such that each index in the new array contains the
result of applying
f
to the value at that index in the old array.
- Creates a new array, such that each index in the new array contains the
result of applying
comprei (f, arr)
- Creates a new array, such that each index in the new array contains the
result of applying
f
to the index and value at that index in the old array.
- Creates a new array, such that each index in the new array contains the
result of applying
pushback (arr, a)
- Pushes an element to the end of array.
pop arr
- Pop an element from the end of array.