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 mlarrayval make : int * 'a -> 'a mlarrayval copy : 'a mlarray -> 'a mlarrayval foldl : ('a * 'b -> 'b) * 'b * 'a mlarray -> 'bval foldli : (int * 'a * 'b -> 'b) * 'b * 'a mlarray -> 'bval fromList : 'a list -> 'a mlarrayval length : 'a mlarray -> intval maxLen : 'a mlarray -> intval modify : ('a -> 'a) * 'a mlarray -> unitval modifyi : (int * 'a -> 'a) * 'a mlarray -> unitval sub : 'a mlarray * int -> 'aval update : 'a mlarray * int * 'a -> unitval toString : ('a -> string) * 'a mlarray -> stringval toStringSub : ('a -> string) * 'a mlarray * (int * int) -> stringval compre : ('a -> 'b) * 'a mlarray -> 'b mlarrayval comprei : ((int * 'a) -> 'b) * 'a mlarray -> 'b mlarrayval pushback : 'a mlarray * 'a -> unitval pop : 'a mlarray -> 'a
Method Overview
makeInit (nun, default)- Makes an array of length
numwith every element having valuedefault.
- Makes an array of length
make(num, default).- Allocate an array that can store
numitems, but is initially empty (i.e.,Mlarray.lengthwill return 0). Thedefaultvalue 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
arrusing the functionf, starting withbase. The elements are combined from left to right (lowest index to highest index). The functionfshould take as an argument a tuple of the next element ofarrto 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
arrwith the result of applyingfto 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
nth element ofarr(likearr[n]in other languages).
- Returns the
update (arr, idx, a).- Updates the
idxth element ofarrto bea(likearr[idx] := ain some languages).
- Updates the
toString (f, arr).- Converts
arrto a string. The functionfis 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 ofstartand exclusive ofend).
- Like
compre (f, arr)- Creates a new array, such that each index in the new array contains the
result of applying
fto 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
fto 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.