123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172moduleA=Stdlib.ArrayincludeAopenBase(** [for_all2 p a1 a2] checks if the corresponding elements of arrays [a1] and
[a2] satisfy the predicate [p]. The [Invalid_argument] exception is raised
if the arrays do not have the same size. *)letfor_all2:('a->'b->bool)->'aarray->'barray->bool=funfa1a2->letexceptionDoneinletfxy=ifnot(fxy)thenraiseDoneintryiter2fa1a2;truewithDone->false(** [pp pp_elt sep oc a] prints the array list [a] on the channel [oc] using
[sep] as separator, and [pp_e] for printing the elements. *)letpp:'app->string->'aarraypp=funpp_eltsepoca->letn=A.lengthainifn>0thenpp_eltoc(A.geta0);fori=1ton-1doFormat.fprintfoc"%s%a"seppp_elt(A.getai)done(** Comparison function on arrays. *)letcmp:'acmp->'aarraycmp=funcmp_elt->letcmpa1a2(* of the same length *)=letexceptionDistinctofintinletreccmpi=ifi>=0thenmatchcmp_elt(A.geta1i)(A.geta2i)with|0->cmp(i-1)|n->raise(Distinctn)intrycmp(A.lengtha1-1);0withDistinctn->ninfuna1a2->ifa1==a2then0elselex(cmp_mapStdlib.compareA.length)cmp(a1,a1)(a2,a2)(** [eq eq_elt a1 a2] tests the equality of [a1] and [a2], comparing their
elements with [eq_elt]. *)leteq:'aeq->'aarrayeq=funeq_elta1a2->a1==a2||(A.lengtha1=A.lengtha2&&for_all2eq_elta1a2)(** [max_index ?cmp a] returns the index of the first maximum of array [a]
according to comparison [?cmp]. If [cmp] is not given, defaults to
[Stdlib.compare]. *)letmax_index:?cmp:('a->'a->int)->'aarray->int=fun?(cmp=Stdlib.compare)arr->letlen=A.lengtharriniflen=0theninvalid_arg"Extra.Array.max_index"elseletmax=ref0infori=1tolen-1doifcmp(A.getarr!max)(A.getarri)<0thenmax:=idone;!max(** [max ?cmp a] returns the higher element according to comparison function
[?cmp], using [Stdlib.compare] if not given, in array [a]. *)letmax:?cmp:('a->'a->int)->'aarray->'a=fun?(cmp=Stdlib.compare)arr->A.getarr(max_index~cmparr)(** [unzip a] is [List.unzip (Array.to_list a)]. *)letunzip:('a*'b)array->'alist*'blist=funa->letaux(el,er)(accl,accr)=(el::accl,er::accr)inA.fold_rightauxa([],[])(** [drop n a] discards the first [n] elements of [a]. The empty array is
returned if [n > length a]. *)letdrop:int->'aarray->'aarray=funna->letl=lengthainifn>=lthen[||]elseA.suban(l-n)