Module Base__.Array
include Base.Sexpable.S1 with type 'a t := 'a t
val t_of_sexp : (Base.Sexp.t -> 'a) -> Base.Sexp.t -> 'a t
val sexp_of_t : ('a -> Base.Sexp.t) -> 'a t -> Base.Sexp.t
include Base.Binary_searchable.S1 with type 'a t := 'a t
val binary_search : ('a t, 'a, 'key) Base__.Binary_searchable_intf.binary_search
val binary_search_segmented : ('a t, 'a) Base__.Binary_searchable_intf.binary_search_segmented
include Base.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
returnsf (... f (f (f init e1) e2) e3 ...) en
, wheree1..en
are the elements oft
val fold_result : 'a t -> init:'accum -> f:('accum -> 'a -> ('accum, 'e) Base.Result.t) -> ('accum, 'e) Base.Result.t
fold_result t ~init ~f
is a short-circuiting version offold
that runs in theResult
monad. Iff
returns anError _
, that value is returned without any additional invocations off
.
val fold_until : 'a t -> init:'accum -> f:('accum -> 'a -> ('accum, 'final) Base__.Container_intf.Continue_or_stop.t) -> finish:('accum -> 'final) -> 'final
fold_until t ~init ~f ~finish
is a short-circuiting version offold
. Iff
returnsStop _
the computation ceases and results in that value. Iff
returnsContinue _
, the fold will proceed. Iff
never returnsStop _
, the final result is computed byfinish
.Example:
type maybe_negative = | Found_negative of int | All_nonnegative of { sum : int } (** [first_neg_or_sum list] returns the first negative number in [list], if any, otherwise returns the sum of the list. *) let first_neg_or_sum = List.fold_until ~init:0 ~f:(fun sum x -> if x < 0 then Stop (Found_negative x) else Continue (sum + x)) ~finish:(fun sum -> All_nonnegative { sum }) ;; let x = first_neg_or_sum [1; 2; 3; 4; 5] val x : maybe_negative = All_nonnegative {sum = 15} let y = first_neg_or_sum [1; 2; -3; 4; 5] val y : maybe_negative = Found_negative -3
val exists : 'a t -> f:('a -> bool) -> bool
Returns
true
if and only if there exists an element for which the provided function evaluates totrue
. 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 totrue
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 Base__.Container_intf.Summable with type t = 'sum) -> 'a t -> f:('a -> 'sum) -> 'sum
Returns the sum of
f i
for alli
in the container.
val find : 'a t -> f:('a -> bool) -> 'a option
Returns as an
option
the first element for whichf
evaluates to true.
val find_map : 'a t -> f:('a -> 'b option) -> 'b option
Returns the first evaluation of
f
that returnsSome
, and returnsNone
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 -> compare:('a -> 'a -> int) -> 'a option
Returns a minimum (resp maximum) element from the collection using the provided
compare
function, orNone
if the collection is empty. In case of a tie, the first element encountered while traversing the collection is returned. The implementation usesfold
so it has the same complexity asfold
.
val max_elt : 'a t -> compare:('a -> 'a -> int) -> 'a option
include Base.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 andmax_length
on 64-bit machines.
val get : 'a t -> int -> 'a
Array.get a n
returns the element numbern
of arraya
. The first element has number 0. The last element has numberArray.length a - 1
. You can also writea.(n)
instead ofArray.get a n
.Raise
Invalid_argument "index out of bounds"
ifn
is outside the range 0 to(Array.length a - 1)
.
val set : 'a t -> int -> 'a -> unit
Array.set a n x
modifies arraya
in place, replacing element numbern
withx
. You can also writea.(n) <- x
instead ofArray.set a n x
.Raise
Invalid_argument "index out of bounds"
ifn
is outside the range 0 toArray.length a - 1
.
val unsafe_get : 'a t -> int -> 'a
Unsafe version of
get
. Can cause arbitrary behavior when used for an out-of-bounds array access.
val unsafe_set : 'a t -> int -> 'a -> unit
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 lengthlen
with the valuex
populated in each element.
val init : int -> f:(int -> 'a) -> 'a t
init n ~f
creates an array of lengthn
where thei
th element (starting at zero) is initialized withf i
.
val make_matrix : dimx:int -> dimy:int -> 'a -> 'a t t
Array.make_matrix dimx dimy e
returns a two-dimensional array (an array of arrays) with first dimensiondimx
and second dimensiondimy
. All the elements of this new matrix are initially physically equal toe
. The element (x,y
) of a matrixm
is accessed with the notationm.(x).(y)
.Raise
Invalid_argument
ifdimx
ordimy
is negative or greater thanArray.max_length
.If the value of
e
is a floating-point number, then the maximum size is onlyArray.max_length / 2
.
val append : 'a t -> 'a t -> 'a t
Array.append v1 v2
returns a fresh array containing the concatenation of the arraysv1
andv2
.
val copy : 'a t -> 'a t
Array.copy a
returns a copy ofa
, that is, a fresh array containing the same elements asa
.
val fill : 'a t -> pos:int -> len:int -> 'a -> unit
Array.fill a ofs len x
modifies the arraya
in place, storingx
in elements numberofs
toofs + len - 1
.Raise
Invalid_argument "Array.fill"
ifofs
andlen
do not designate a valid subarray ofa
.
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 Base.Blit.S1 with type 'a t := 'a t
val blit : ('a t, 'a t) Base__.Blit_intf.blit
val blito : ('a t, 'a t) Base__.Blit_intf.blito
val unsafe_blit : ('a t, 'a t) Base__.Blit_intf.blit
val sub : ('a t, 'a t) Base__.Blit_intf.sub
val subo : ('a t, 'a t) Base__.Blit_intf.subo
val of_list : 'a list -> 'a t
Array.of_list l
returns a fresh array containing the elements ofl
.
val map : 'a t -> f:('a -> 'b) -> 'b t
Array.map t ~f
applies functionf
to all the elements oft
, and builds an array with the results returned byf
:[| f t.(0); f t.(1); ...; f t.(Array.length t - 1) |]
.
val folding_map : 'a t -> init:'b -> f:('b -> 'a -> 'b * 'c) -> 'c t
folding_map
is a version ofmap
that threads an accumulator through calls tof
.
val folding_mapi : 'a t -> init:'b -> f:(int -> 'b -> 'a -> 'b * 'c) -> 'c t
val fold_map : 'a t -> init:'b -> f:('b -> 'a -> 'b * 'c) -> 'b * 'c t
Array.fold_map
is a combination ofArray.fold
andArray.map
that threads an accumulator through calls tof
.
val fold_mapi : 'a t -> init:'b -> f:(int -> 'b -> 'a -> 'b * 'c) -> 'b * 'c t
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.
val mapi : 'a t -> f:(int -> 'a -> 'b) -> 'b t
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
computesf a.(0) (f a.(1) ( ... (f a.(n-1) init) ...))
, wheren
is the length of the arraya
.
val sort : ?pos:int -> ?len:int -> 'a t -> compare:('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 andlen
indicating how many elements to sort.
val stable_sort : 'a t -> compare:('a -> 'a -> int) -> unit
val is_sorted : 'a t -> compare:('a -> 'a -> int) -> bool
val is_sorted_strictly : 'a t -> compare:('a -> 'a -> int) -> bool
is_sorted_strictly xs ~compare
iffis_sorted xs ~compare
and no two consecutive elements inxs
are equal according tocompare
.
val concat_map : 'a t -> f:('a -> 'b array) -> 'b array
Like
List.concat_map
,List.concat_mapi
.
val concat_mapi : 'a t -> f:(int -> 'a -> 'b array) -> 'b array
val partition_tf : 'a t -> f:('a -> bool) -> 'a t * 'a t
val partitioni_tf : 'a t -> f:(int -> 'a -> bool) -> 'a t * 'a t
val cartesian_product : 'a t -> 'b t -> ('a * 'b) t
val transpose : 'a t t -> 'a t t option
transpose
in the sense of a matrix transpose. It returnsNone
if the arrays are not all the same length.
val transpose_exn : 'a t t -> 'a t t
val filter_opt : 'a option t -> 'a t
filter_opt array
returns a new array whereNone
entries are omitted andSome x
entries are replaced withx
. Note that this changes the index at which elements will appear.
val filter_map : 'a t -> f:('a -> 'b option) -> 'b t
filter_map ~f array
mapsf
overarray
and filtersNone
out of the results.
val filter_mapi : 'a t -> f:(int -> 'a -> 'b option) -> 'b t
Like
filter_map
but usesArray.mapi
.
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 counti : 'a t -> f:(int -> 'a -> bool) -> int
Like
count
, but passes the index as an argument.
val iter2_exn : 'a t -> 'b t -> f:('a -> 'b -> unit) -> unit
val map2_exn : 'a t -> 'b t -> f:('a -> 'b -> 'c) -> 'c t
val fold2_exn : 'a t -> 'b t -> init:'c -> f:('c -> 'a -> 'b -> 'c) -> 'c
val for_all2_exn : 'a t -> 'b t -> f:('a -> 'b -> bool) -> bool
for_all2_exn t1 t2 ~f
fails iflength t1 <> length t2
.
val exists2_exn : 'a t -> 'b t -> f:('a -> 'b -> bool) -> bool
exists2_exn t1 t2 ~f
fails iflength t1 <> length t2
.
val filter : 'a t -> f:('a -> bool) -> 'a t
filter t ~f
removes the elements for whichf
returns false.
val swap : 'a t -> int -> int -> unit
swap arr i j
swaps the value at indexi
with that at indexj
.
val rev_inplace : 'a t -> unit
rev_inplace t
reversest
in place.
val of_list_rev : 'a list -> 'a t
of_list_rev l
converts from list then reverses in place.
val of_list_map : 'a list -> f:('a -> 'b) -> 'b t
of_list_map l ~f
is the same asof_list (List.map l ~f)
.
val of_list_mapi : 'a list -> f:(int -> 'a -> 'b) -> 'b t
of_list_mapi l ~f
is the same asof_list (List.mapi l ~f)
.
val of_list_rev_map : 'a list -> f:('a -> 'b) -> 'b t
of_list_rev_map l ~f
is the same asof_list (List.rev_map l ~f)
.
val of_list_rev_mapi : 'a list -> f:(int -> 'a -> 'b) -> 'b t
of_list_rev_mapi l ~f
is the same asof_list (List.rev_mapi l ~f)
.
val replace : 'a t -> int -> f:('a -> 'a) -> unit
replace t i ~f
=t.(i) <- f (t.(i))
.
val replace_all : 'a t -> f:('a -> 'a) -> unit
Modifies an array in place --
ar.(i)
will be set tof(ar.(i))
.
val map_inplace : 'a t -> f:('a -> 'a) -> unit
Modifies an array in place, applying
f
to every element of the array
val find_exn : 'a t -> f:('a -> bool) -> 'a
find_exn f t
returns the firsta
int
for whichf t.(i)
is true. It raisesCaml.Not_found
orNot_found_s
if there is no sucha
.
val find_map_exn : 'a t -> f:('a -> 'b option) -> 'b
Returns the first evaluation of
f
that returnsSome
. RaisesCaml.Not_found
orNot_found_s
iff
always returnsNone
.
val findi : 'a t -> f:(int -> 'a -> bool) -> (int * 'a) option
findi t f
returns the first indexi
oft
for whichf i t.(i)
is true
val findi_exn : 'a t -> f:(int -> 'a -> bool) -> int * 'a
findi_exn t f
returns the first indexi
oft
for whichf i t.(i)
is true. It raisesCaml.Not_found
orNot_found_s
if there is no such element.
val find_mapi : 'a t -> f:(int -> 'a -> 'b option) -> 'b option
find_mapi t f
is likefind_map
but passes the index as an argument.
val find_mapi_exn : 'a t -> f:(int -> 'a -> 'b option) -> 'b
find_mapi_exn
is likefind_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)
int
such thatequal a1 a2
. They are returned in the same order as they appear int
.
val reduce : 'a t -> f:('a -> 'a -> 'a) -> 'a option
reduce f [a1; ...; an]
isSome (f (... (f (f a1 a2) a3) ...) an)
. ReturnsNone
on the empty array.
val reduce_exn : 'a t -> f:('a -> 'a -> 'a) -> 'a
val permute : ?random_state:Base.Random.State.t -> 'a t -> unit
permute ?random_state t
randomly permutest
in place.permute
side-effectsrandom_state
by repeated calls toRandom.State.int
. Ifrandom_state
is not supplied,permute
usesRandom.State.default
.
val random_element : ?random_state:Base.Random.State.t -> 'a t -> 'a option
random_element ?random_state t
isNone
ift
is empty, else it isSome x
for somex
chosen uniformly at random fromt
.random_element
side-effectsrandom_state
by callingRandom.State.int
. Ifrandom_state
is not supplied,random_element
usesRandom.State.default
.
val random_element_exn : ?random_state:Base.Random.State.t -> 'a t -> 'a
val zip : 'a t -> 'b t -> ('a * 'b) t option
zip
is likeList.zip
, but for arrays.
val zip_exn : 'a t -> 'b t -> ('a * 'b) t
val unzip : ('a * 'b) t -> 'a t * 'b t
unzip
is likeList.unzip
, but for arrays.
val sorted_copy : 'a t -> compare:('a -> 'a -> int) -> 'a t
sorted_copy ar compare
returns a shallow copy ofar
that is sorted. Similar to List.sort
val last : 'a t -> 'a
val equal : ('a -> 'a -> bool) -> 'a t -> 'a t -> bool
val unsafe_truncate : _ t -> len:int -> unit
unsafe_truncate t ~len
dropslength t - len
elements from the end oft
, changingt
so thatlength t = len
afterwards.unsafe_truncate
raises iflen <= 0 || len > length t
.It is not safe to do
unsafe_truncate
in the middle of a call tomap
,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 callsunsafe_truncate
on an array does not interfere with other code that manipulates the array.
val to_sequence : 'a t -> 'a Base.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 Base.Sequence.t
The input array is shared with the sequence and modifications of it will result in modification of the sequence.