Module Base.Array
Mutable vector of elements with O(1) get and set operations.
include Binary_searchable.S1 with type 'a t := 'a t
val binary_search : ('a t, 'a, 'key) Base__.Binary_searchable_intf.binary_searchval binary_search_segmented : ('a t, 'a) Base__.Binary_searchable_intf.binary_search_segmented
include Container.S1 with type 'a t := 'a t
val mem : 'a t -> 'a -> equal:('a -> 'a -> bool) -> boolChecks whether the provided element is there, using
equal.
val length : 'a t -> intval is_empty : 'a t -> boolval iter : 'a t -> f:('a -> unit) -> unitval fold : 'a t -> init:'accum -> f:('accum -> 'a -> 'accum) -> 'accumfold t ~init ~freturnsf (... f (f (f init e1) e2) e3 ...) en, wheree1..enare the elements oft
val fold_result : 'a t -> init:'accum -> f:('accum -> 'a -> ('accum, 'e) Result.t) -> ('accum, 'e) Result.tfold_result t ~init ~fis a short-circuiting version offoldthat runs in theResultmonad. Iffreturns 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) -> 'finalfold_until t ~init ~f ~finishis a short-circuiting version offold. IffreturnsStop _the computation ceases and results in that value. IffreturnsContinue _, the fold will proceed. Iffnever 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) -> boolReturns
trueif 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) -> boolReturns
trueif and only if the provided function evaluates totruefor all elements. This is a short-circuiting operation.
val count : 'a t -> f:('a -> bool) -> intReturns 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) -> 'sumReturns the sum of
f ifor alliin the container.
val find : 'a t -> f:('a -> bool) -> 'a optionReturns as an
optionthe first element for whichfevaluates to true.
val find_map : 'a t -> f:('a -> 'b option) -> 'b optionReturns the first evaluation of
fthat returnsSome, and returnsNoneif there is no such element.
val to_list : 'a t -> 'a listval to_array : 'a t -> 'a arrayval min_elt : 'a t -> compare:('a -> 'a -> int) -> 'a optionReturns a minimum (resp maximum) element from the collection using the provided
comparefunction, orNoneif the collection is empty. In case of a tie, the first element encountered while traversing the collection is returned. The implementation usesfoldso it has the same complexity asfold.
val max_elt : 'a t -> compare:('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 : intMaximum length of a normal array. The maximum length of a float array is
max_length/2on 32-bit machines andmax_lengthon 64-bit machines.
val get : 'a t -> int -> 'aArray.get a nreturns the element numbernof 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"ifnis outside the range 0 to(Array.length a - 1).
val set : 'a t -> int -> 'a -> unitArray.set a n xmodifies arrayain place, replacing element numbernwithx. You can also writea.(n) <- xinstead ofArray.set a n x.Raise
Invalid_argument "index out of bounds"ifnis outside the range 0 toArray.length a - 1.
val unsafe_get : 'a t -> int -> 'aUnsafe version of
get. Can cause arbitrary behavior when used for an out-of-bounds array access.
val unsafe_set : 'a t -> int -> 'a -> unitUnsafe version of
set. Can cause arbitrary behavior when used for an out-of-bounds array access.
val create : len:int -> 'a -> 'a tcreate ~len xcreates an array of lengthlenwith the valuexpopulated in each element.
val init : int -> f:(int -> 'a) -> 'a tinit n ~fcreates an array of lengthnwhere theith element (starting at zero) is initialized withf i.
val make_matrix : dimx:int -> dimy:int -> 'a -> 'a t tArray.make_matrix dimx dimy ereturns a two-dimensional array (an array of arrays) with first dimensiondimxand second dimensiondimy. All the elements of this new matrix are initially physically equal toe. The element (x,y) of a matrixmis accessed with the notationm.(x).(y).Raise
Invalid_argumentifdimxordimyis negative or greater thanArray.max_length.If the value of
eis a floating-point number, then the maximum size is onlyArray.max_length / 2.
val append : 'a t -> 'a t -> 'a tArray.append v1 v2returns a fresh array containing the concatenation of the arraysv1andv2.
val copy : 'a t -> 'a tArray.copy areturns a copy ofa, that is, a fresh array containing the same elements asa.
val fill : 'a t -> pos:int -> len:int -> 'a -> unitArray.fill a ofs len xmodifies the arrayain place, storingxin elements numberofstoofs + len - 1.Raise
Invalid_argument "Array.fill"ifofsandlendo 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 Blit.S1 with type 'a t := 'a t
val blit : ('a t, 'a t) Base__.Blit_intf.blitval blito : ('a t, 'a t) Base__.Blit_intf.blitoval unsafe_blit : ('a t, 'a t) Base__.Blit_intf.blitval sub : ('a t, 'a t) Base__.Blit_intf.subval subo : ('a t, 'a t) Base__.Blit_intf.subo
val of_list : 'a list -> 'a tArray.of_list lreturns a fresh array containing the elements ofl.
val map : 'a t -> f:('a -> 'b) -> 'b tArray.map t ~fapplies functionfto 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 tfolding_mapis a version ofmapthat threads an accumulator through calls tof.
val folding_mapi : 'a t -> init:'b -> f:(int -> 'b -> 'a -> 'b * 'c) -> 'c tval fold_map : 'a t -> init:'b -> f:('b -> 'a -> 'b * 'c) -> 'b * 'c tArray.fold_mapis a combination ofArray.foldandArray.mapthat threads an accumulator through calls tof.
val fold_mapi : 'a t -> init:'b -> f:(int -> 'b -> 'a -> 'b * 'c) -> 'b * 'c tval iteri : 'a t -> f:(int -> 'a -> unit) -> unitLike
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 tLike
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) -> 'bval fold_right : 'a t -> f:('a -> 'b -> 'b) -> init:'b -> 'bArray.fold_right f a ~initcomputesf a.(0) (f a.(1) ( ... (f a.(n-1) init) ...)), wherenis the length of the arraya.
val sort : ?pos:int -> ?len:int -> 'a t -> compare:('a -> 'a -> int) -> unitsortuses constant heap space.stable_sortuses linear heap space.To sort only part of the array, specify
posto be the index to start sorting from andlenindicating how many elements to sort.
val stable_sort : 'a t -> compare:('a -> 'a -> int) -> unitval is_sorted : 'a t -> compare:('a -> 'a -> int) -> boolval is_sorted_strictly : 'a t -> compare:('a -> 'a -> int) -> boolis_sorted_strictly xs ~compareiffis_sorted xs ~compareand no two consecutive elements inxsare equal according tocompare.
val concat_map : 'a t -> f:('a -> 'b array) -> 'b arrayLike
List.concat_map,List.concat_mapi.
val concat_mapi : 'a t -> f:(int -> 'a -> 'b array) -> 'b arrayval partition_tf : 'a t -> f:('a -> bool) -> 'a t * 'a tval partitioni_tf : 'a t -> f:(int -> 'a -> bool) -> 'a t * 'a tval cartesian_product : 'a t -> 'b t -> ('a * 'b) tval transpose : 'a t t -> 'a t t optiontransposein the sense of a matrix transpose. It returnsNoneif the arrays are not all the same length.
val transpose_exn : 'a t t -> 'a t tval filter_opt : 'a option t -> 'a tfilter_opt arrayreturns a new array whereNoneentries are omitted andSome xentries are replaced withx. Note that this changes the index at which elements will appear.
val filter_map : 'a t -> f:('a -> 'b option) -> 'b tfilter_map ~f arraymapsfoverarrayand filtersNoneout of the results.
val filter_mapi : 'a t -> f:(int -> 'a -> 'b option) -> 'b tLike
filter_mapbut usesArray.mapi.
val for_alli : 'a t -> f:(int -> 'a -> bool) -> boolLike
for_all, but passes the index as an argument.
val existsi : 'a t -> f:(int -> 'a -> bool) -> boolLike
exists, but passes the index as an argument.
val counti : 'a t -> f:(int -> 'a -> bool) -> intLike
count, but passes the index as an argument.
val iter2_exn : 'a t -> 'b t -> f:('a -> 'b -> unit) -> unitval map2_exn : 'a t -> 'b t -> f:('a -> 'b -> 'c) -> 'c tval fold2_exn : 'a t -> 'b t -> init:'c -> f:('c -> 'a -> 'b -> 'c) -> 'cval for_all2_exn : 'a t -> 'b t -> f:('a -> 'b -> bool) -> boolfor_all2_exn t1 t2 ~ffails iflength t1 <> length t2.
val exists2_exn : 'a t -> 'b t -> f:('a -> 'b -> bool) -> boolexists2_exn t1 t2 ~ffails iflength t1 <> length t2.
val filter : 'a t -> f:('a -> bool) -> 'a tfilter t ~fremoves the elements for whichfreturns false.
val swap : 'a t -> int -> int -> unitswap arr i jswaps the value at indexiwith that at indexj.
val rev_inplace : 'a t -> unitrev_inplace treversestin place.
val of_list_rev : 'a list -> 'a tof_list_rev lconverts from list then reverses in place.
val of_list_map : 'a list -> f:('a -> 'b) -> 'b tof_list_map l ~fis the same asof_list (List.map l ~f).
val of_list_mapi : 'a list -> f:(int -> 'a -> 'b) -> 'b tof_list_mapi l ~fis the same asof_list (List.mapi l ~f).
val of_list_rev_map : 'a list -> f:('a -> 'b) -> 'b tof_list_rev_map l ~fis the same asof_list (List.rev_map l ~f).
val of_list_rev_mapi : 'a list -> f:(int -> 'a -> 'b) -> 'b tof_list_rev_mapi l ~fis the same asof_list (List.rev_mapi l ~f).
val map_inplace : 'a t -> f:('a -> 'a) -> unitModifies an array in place, applying
fto every element of the array
val find_exn : 'a t -> f:('a -> bool) -> 'afind_exn f treturns the firstaintfor whichf t.(i)is true. It raisesCaml.Not_foundorNot_found_sif there is no sucha.
val find_map_exn : 'a t -> f:('a -> 'b option) -> 'bReturns the first evaluation of
fthat returnsSome. RaisesCaml.Not_foundorNot_found_siffalways returnsNone.
val findi : 'a t -> f:(int -> 'a -> bool) -> (int * 'a) optionfindi t freturns the first indexioftfor whichf i t.(i)is true
val findi_exn : 'a t -> f:(int -> 'a -> bool) -> int * 'afindi_exn t freturns the first indexioftfor whichf i t.(i)is true. It raisesCaml.Not_foundorNot_found_sif there is no such element.
val find_mapi : 'a t -> f:(int -> 'a -> 'b option) -> 'b optionfind_mapi t fis likefind_mapbut passes the index as an argument.
val find_mapi_exn : 'a t -> f:(int -> 'a -> 'b option) -> 'bfind_mapi_exnis likefind_map_exnbut passes the index as an argument.
val find_consecutive_duplicate : 'a t -> equal:('a -> 'a -> bool) -> ('a * 'a) optionfind_consecutive_duplicate t ~equalreturns the first pair of consecutive elements(a1, a2)intsuch thatequal a1 a2. They are returned in the same order as they appear int.
val reduce : 'a t -> f:('a -> 'a -> 'a) -> 'a optionreduce f [a1; ...; an]isSome (f (... (f (f a1 a2) a3) ...) an). ReturnsNoneon the empty array.
val reduce_exn : 'a t -> f:('a -> 'a -> 'a) -> 'aval permute : ?random_state:Random.State.t -> 'a t -> unitpermute ?random_state trandomly permutestin place.permuteside-effectsrandom_stateby repeated calls toRandom.State.int. Ifrandom_stateis not supplied,permuteusesRandom.State.default.
val random_element : ?random_state:Random.State.t -> 'a t -> 'a optionrandom_element ?random_state tisNoneiftis empty, else it isSome xfor somexchosen uniformly at random fromt.random_elementside-effectsrandom_stateby callingRandom.State.int. Ifrandom_stateis not supplied,random_elementusesRandom.State.default.
val random_element_exn : ?random_state:Random.State.t -> 'a t -> 'aval zip : 'a t -> 'b t -> ('a * 'b) t optionzipis likeList.zip, but for arrays.
val zip_exn : 'a t -> 'b t -> ('a * 'b) tval unzip : ('a * 'b) t -> 'a t * 'b tunzipis likeList.unzip, but for arrays.
val sorted_copy : 'a t -> compare:('a -> 'a -> int) -> 'a tsorted_copy ar comparereturns a shallow copy ofarthat is sorted. Similar to List.sort
val last : 'a t -> 'aval equal : ('a -> 'a -> bool) -> 'a t -> 'a t -> boolval unsafe_truncate : _ t -> len:int -> unitunsafe_truncate t ~lendropslength t - lenelements from the end oft, changingtso thatlength t = lenafterwards.unsafe_truncateraises iflen <= 0 || len > length t.It is not safe to do
unsafe_truncatein 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_truncateon an array does not interfere with other code that manipulates the array.
val to_sequence : 'a t -> 'a Sequence.tThe input array is copied internally so that future modifications of it do not change the sequence.
val to_sequence_mutable : 'a t -> 'a Sequence.tThe input array is shared with the sequence and modifications of it will result in modification of the sequence.