include Binary_searchable.S1 with type a t := a t
val binary_search : ('a t, 'a, 'key) Binary_searchable_intf.binary_search
val binary_search_segmented : ('a t, 'a) Binary_searchable_intf.binary_search_segmented
include Container.S1 with type a t := a t
val mem : 'a t ‑> 'a ‑> equal:('a ‑> 'a ‑> bool) ‑> bool
Checks whether the provided element is there, using equal
.
val length : 'a t ‑> int
val is_empty : 'a t ‑> bool
val iter : 'a t ‑> f:('a ‑> unit) ‑> unit
val fold : 'a t ‑> init:'accum ‑> f:('accum ‑> 'a ‑> 'accum) ‑> 'accum
fold t ~init ~f
returns f (... f (f (f init e1) e2) e3 ...) en
, where e1..en
are the elements of t
val fold_result : 'a t ‑> init:'accum ‑> f:('accum ‑> 'a ‑> ('accum, 'e) Result.t) ‑> ('accum, 'e) Result.t
fold_result t ~init ~f
is a short-circuiting version of fold
that runs in the
Result
monad. If f
returns an Error _
, that value is returned without any
additional invocations of f
.
val fold_until : 'a t ‑> init:'accum ‑> f:('accum ‑> 'a ‑> ('accum, 'stop) Container_intf.Continue_or_stop.t) ‑> ('accum, 'stop) Container_intf.Finished_or_stopped_early.t
fold_until t ~init ~f
is a short-circuiting version of fold
. If f
returns Stop _
the computation ceases and results in that value. If f
returns
Continue _
, the fold will proceed.
val exists : 'a t ‑> f:('a ‑> bool) ‑> bool
Returns true
if and only if there exists an element for which the provided
function evaluates to true
. This is a short-circuiting operation.
val for_all : 'a t ‑> f:('a ‑> bool) ‑> bool
Returns true
if and only if the provided function evaluates to true
for all
elements. This is a short-circuiting operation.
val count : 'a t ‑> f:('a ‑> bool) ‑> int
Returns the number of elements for which the provided function evaluates to true.
val sum : (module Commutative_group.S with type t = 'sum) ‑> 'a t ‑> f:('a ‑> 'sum) ‑> 'sum
Returns the sum of f i
for all i
in the container.
val find : 'a t ‑> f:('a ‑> bool) ‑> 'a option
Returns as an option
the first element for which f
evaluates to true.
val find_map : 'a t ‑> f:('a ‑> 'b option) ‑> 'b option
Returns the first evaluation of f
that returns Some
, and returns None
if there
is no such element.
val to_list : 'a t ‑> 'a list
val to_array : 'a t ‑> 'a array
val min_elt : 'a t ‑> cmp:('a ‑> 'a ‑> int) ‑> 'a option
Returns a minimum (resp maximum) element from the collection using the provided
cmp
function, or None
if the collection is empty. In case of a tie, the first
element encountered while traversing the collection is returned. The implementation
uses fold
so it has the same complexity as fold
.
val max_elt : 'a t ‑> cmp:('a ‑> 'a ‑> int) ‑> 'a option
include Invariant.S1 with type a t := a t
val invariant : 'a Base__.Invariant_intf.inv ‑> 'a t Base__.Invariant_intf.inv
val max_length : int
Maximum length of a normal array. The maximum length of a float array is
max_length/2
on 32-bit machines and max_length
on 64-bit machines.
external get : 'a t ‑> int ‑> 'a = "%array_safe_get"
Array.get a n
returns the element number n
of array a
.
The first element has number 0.
The last element has number Array.length a - 1
.
You can also write a.(n)
instead of Array.get a n
.
Raise Invalid_argument "index out of bounds"
if n
is outside the range 0 to (Array.length a - 1)
.
external set : 'a t ‑> int ‑> 'a ‑> unit = "%array_safe_set"
Array.set a n x
modifies array a
in place, replacing
element number n
with x
.
You can also write a.(n) <- x
instead of Array.set a n x
.
Raise Invalid_argument "index out of bounds"
if n
is outside the range 0 to Array.length a - 1
.
external unsafe_get : 'a t ‑> int ‑> 'a = "%array_unsafe_get"
Unsafe version of get
. Can cause arbitrary behavior when used for an out-of-bounds
array access
external unsafe_set : 'a t ‑> int ‑> 'a ‑> unit = "%array_unsafe_set"
Unsafe version of set
. Can cause arbitrary behavior when used for an out-of-bounds
array access
val create : len:int ‑> 'a ‑> 'a t
create ~len x
creates an array of length len
with the value x
populated in
each element
val init : int ‑> f:(int ‑> 'a) ‑> 'a t
init n ~f
creates an array of length n
where the i
th element is initialized
with f i
(starting at zero)
Array.make_matrix dimx dimy e
returns a two-dimensional array
(an array of arrays) with first dimension dimx
and
second dimension dimy
. All the elements of this new matrix
are initially physically equal to e
.
The element (x,y
) of a matrix m
is accessed
with the notation m.(x).(y)
.
Raise Invalid_argument
if dimx
or dimy
is negative or
greater than Array.max_length
.
If the value of e
is a floating-point number, then the maximum
size is only Array.max_length / 2
.
val fill : 'a t ‑> pos:int ‑> len:int ‑> 'a ‑> unit
Array.fill a ofs len x
modifies the array a
in place,
storing x
in elements number ofs
to ofs + len - 1
.
Raise Invalid_argument "Array.fill"
if ofs
and len
do not
designate a valid subarray of a
.
Array.blit v1 o1 v2 o2 len
copies len
elements from array v1
, starting at
element number o1
, to array v2
, starting at element number o2
. It works
correctly even if v1
and v2
are the same array, and the source and destination
chunks overlap.
Raise Invalid_argument "Array.blit"
if o1
and len
do not designate a valid
subarray of v1
, or if o2
and len
do not designate a valid subarray of v2
.
int_blit
and float_blit
provide fast bound-checked blits for immediate
data types. The unsafe versions do not bound-check the arguments.
include Blit.S1 with type a t := a t
val blit : ('a t, 'a t) Blit_intf.blit
val blito : ('a t, 'a t) Blit_intf.blito
val unsafe_blit : ('a t, 'a t) Blit_intf.blit
val sub : ('a t, 'a t) Blit_intf.sub
val subo : ('a t, 'a t) Blit_intf.subo
Array.map t ~f
applies function f
to all the elements of t
,
and builds an array with the results returned by f
:
[| f t.(0); f t.(1); ...; f t.(Array.length t - 1) |]
.
Array.fold_map
is a combination of Array.fold
and Array.map
that threads an
accumulator through calls to f
.
val iteri : 'a t ‑> f:(int ‑> 'a ‑> unit) ‑> unit
Like Array.iter, but the function is applied to the index of the element as first argument, and the element itself as second argument.
Like Array.map, but the function is applied to the index of the element as first argument, and the element itself as second argument.
val foldi : 'a t ‑> init:'b ‑> f:(int ‑> 'b ‑> 'a ‑> 'b) ‑> 'b
val fold_right : 'a t ‑> f:('a ‑> 'b ‑> 'b) ‑> init:'b ‑> 'b
Array.fold_right f a ~init
computes
f a.(0) (f a.(1) ( ... (f a.(n-1) init) ...))
,
where n
is the length of the array a
.
All sort functions in this module sort in increasing order by default.
val sort : ?pos:int ‑> ?len:int ‑> 'a t ‑> cmp:('a ‑> 'a ‑> int) ‑> unit
sort
uses constant heap space. stable_sort
uses linear heap space.
To sort only part of the array, specify pos
to be the index to start sorting from
and len
indicating how many elements to sort.
val stable_sort : 'a t ‑> cmp:('a ‑> 'a ‑> int) ‑> unit
val is_sorted : 'a t ‑> cmp:('a ‑> 'a ‑> int) ‑> bool
val is_sorted_strictly : 'a t ‑> cmp:('a ‑> 'a ‑> int) ‑> bool
is_sorted_strictly xs ~cmp
iff is_sorted xs ~cmp
and no two consecutive elements
in xs
are equal according to cmp
val concat_mapi : 'a t ‑> f:(int ‑> 'a ‑> 'b array) ‑> 'b array
transpose
in the sense of a matrix transpose. It returns None
if the arrays are
not all the same length.
val normalize : 'a t ‑> int ‑> int
normalize array index
returns a new index into the array such that if index is
less than zero, the returned index will "wrap around" -- i.e. array.(normalize array
(-1)) returns the last element of the array.
slice array start stop
returns a fresh array including elements array.(start)
through array.(stop-1)
with the small tweak that the start and stop positions are
normalized and a stop index of 0 means the same thing a stop index of Array.length
array
. In summary, it's mostly like the slicing in Python or Matlab. One
difference is that a stop value of 0 here is like not specifying a stop value in
Python.
filter_opt array
returns a new array where None
entries are omitted and Some x
entries are replaced with x
. Note that this changes the index at which elements
will appear.
val for_alli : 'a t ‑> f:(int ‑> 'a ‑> bool) ‑> bool
Like for_all
, but passes the index as an argument.
val existsi : 'a t ‑> f:(int ‑> 'a ‑> bool) ‑> bool
Like exists
, but passes the index as an argument.
val of_list_map : 'a list ‑> f:('a ‑> 'b) ‑> 'b t
of_list_map l ~f
is the same as of_list (List.map l ~f)
val of_list_rev_map : 'a list ‑> f:('a ‑> 'b) ‑> 'b t
of_list_rev_map l ~f
is the same as rev_inplace (of_list_map l ~f)
val replace_all : 'a t ‑> f:('a ‑> 'a) ‑> unit
modifies an array in place -- ar.(i)
will be set to f(ar.(i))
val find_exn : 'a t ‑> f:('a ‑> bool) ‑> 'a
find_exn f t
returns the first a
in t
for which f t.(i)
is true.
It raises Not_found
if there is no such a
.
val find_map_exn : 'a t ‑> f:('a ‑> 'b option) ‑> 'b
Returns the first evaluation of f
that returns Some
.
Raises Not_found
if f
always returns None
.
val findi : 'a t ‑> f:(int ‑> 'a ‑> bool) ‑> (int * 'a) option
findi t f
returns the first index i
of t
for which f i t.(i)
is true
val findi_exn : 'a t ‑> f:(int ‑> 'a ‑> bool) ‑> int * 'a
findi_exn t f
returns the first index i
of t
for which f i t.(i)
is
true. It raises Not_found
if there is no such element.
val find_mapi : 'a t ‑> f:(int ‑> 'a ‑> 'b option) ‑> 'b option
find_mapi t f
is the like find_map
but passes the index as an argument.
val find_mapi_exn : 'a t ‑> f:(int ‑> 'a ‑> 'b option) ‑> 'b
find_mapi_exn
is the like find_map_exn
but passes the index as an argument.
val find_consecutive_duplicate : 'a t ‑> equal:('a ‑> 'a ‑> bool) ‑> ('a * 'a) option
find_consecutive_duplicate t ~equal
returns the first pair of consecutive elements
(a1, a2)
in t
such that equal a1 a2
. They are returned in the same order as
they appear in t
.
val reduce : 'a t ‑> f:('a ‑> 'a ‑> 'a) ‑> 'a option
reduce f [a1; ...; an]
is Some (f (... (f (f a1 a2) a3) ...) an)
.
Returns None
on the empty array.
val reduce_exn : 'a t ‑> f:('a ‑> 'a ‑> 'a) ‑> 'a
val permute : ?random_state:Random.State.t ‑> 'a t ‑> unit
permute ?random_state t
randomly permutes t
in place.
permute
side affects random_state
by repeated calls to Random.State.int
.
If random_state
is not supplied, permute
uses Random.State.default
.
val random_element : ?random_state:Random.State.t ‑> 'a t ‑> 'a option
random_element ?random_state t
is None
if t
is empty, else it is Some x
for
some x
chosen uniformly at random from t
.
random_element
side affects random_state
by calling Random.State.int
. If
random_state
is not supplied, random_element
uses Random.State.default
.
val random_element_exn : ?random_state:Random.State.t ‑> 'a t ‑> 'a
val last : 'a t ‑> 'a
val unsafe_truncate : _ t ‑> len:int ‑> unit
unsafe_truncate t ~len
drops length t - len
elements from the end of t
, changing
t
so that length t = len
afterwards.
unsafe_truncate
raises if len <= 0 || len > length t
.
It is not safe to do unsafe_truncate
in the middle of a call to map
,
iter
, etc, or if you have given this array out to anything not under your
control: in general, code can rely on an array's length not changing.
One must ensure code that calls unsafe_truncate
on an array does not
interfere with other code that manipulates the array.
val to_sequence : 'a t ‑> 'a Sequence.t
The input array is copied internally so that future modifications of it do not change the sequence.
val to_sequence_mutable : 'a t ‑> 'a Sequence.t
The input array is shared with the sequence and modifications of it will result in modification of the sequence.