123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159(* [Array0] defines array functions that are primitives or can be simply defined in terms
of [Stdlib.Array]. [Array0] is intended to completely express the part of [Stdlib.Array]
that [Base] uses -- no other file in Base other than array0.ml should use [Stdlib.Array].
[Array0] has few dependencies, and so is available early in Base's build order. All
Base files that need to use arrays and come before [Base.Array] in build order should
do [module Array = Array0]. This includes uses of subscript syntax ([x.(i)], [x.(i) <-
e]), which the OCaml parser desugars into calls to [Array.get] and [Array.set].
Defining [module Array = Array0] is also necessary because it prevents ocamldep from
mistakenly causing a file to depend on [Base.Array]. *)open!Import0moduleSys=Sys0letinvalid_argf=Printf.invalid_argfmoduleArray=structexternalcreate:int->'a->'aarray="caml_make_vect"externalcreate_local:int->'a->('aarray[@local])="caml_make_vect"externalcreate_float_uninitialized:int->floatarray="caml_make_float_vect"externalget:('aarray[@local_opt])->(int[@local_opt])->'a="%array_safe_get"externallength:('aarray[@local_opt])->int="%array_length"externalset:('aarray[@local_opt])->(int[@local_opt])->'a->unit="%array_safe_set"externalunsafe_get:('aarray[@local_opt])->(int[@local_opt])->'a="%array_unsafe_get"externalunsafe_set:('aarray[@local_opt])->(int[@local_opt])->'a->unit="%array_unsafe_set"externalunsafe_blit:src:('aarray[@local_opt])->src_pos:int->dst:('aarray[@local_opt])->dst_pos:int->len:int->unit="caml_array_blit"endincludeArrayletmax_length=Sys.max_array_lengthletcreate~lenx=trycreatelenxwith|Invalid_argument_->invalid_argf"Array.create ~len:%d: invalid length"len();;letcreate_local~lenx=(trycreate_locallenxwith|Invalid_argument_->invalid_argf"Array.create_local ~len:%d: invalid length"len());;letcreate_float_uninitialized~len=trycreate_float_uninitializedlenwith|Invalid_argument_->invalid_argf"Array.create_float_uninitialized ~len:%d: invalid length"len();;letappend=Stdlib.Array.appendletblit=Stdlib.Array.blitletconcat=Stdlib.Array.concatletcopy=Stdlib.Array.copyletfill=Stdlib.Array.fillletinitlen~f:((f:_->_)[@local])=iflen=0then[||]elseiflen<0theninvalid_arg"Array.init"else(letres=create~len(f0)infori=1toInt0.predlendounsafe_setresi(fi)done;res);;letmake_matrix=Stdlib.Array.make_matrixletof_list=Stdlib.Array.of_listletsub=Stdlib.Array.subletto_list=Stdlib.Array.to_listletfoldt~init~f:((f:_->_->_)[@local])=letr=refinitinfori=0tolengtht-1dor:=f!r(unsafe_getti)done;!r;;letfold_rightt~f:((f:_->_->_)[@local])~init=letr=refinitinfori=lengtht-1downto0dor:=f(unsafe_getti)!rdone;!r;;letitert~f:((f:_->_)[@local])=fori=0tolengtht-1dof(unsafe_getti)done;;letiterit~f:((f:_->_->_)[@local])=fori=0tolengtht-1dofi(unsafe_getti)done;;letmapt~f:((f:_->_)[@local])=letlen=lengthtiniflen=0then[||]else(letr=create~len(f(unsafe_gett0))infori=1tolen-1dounsafe_setri(f(unsafe_getti))done;r);;letmapit~f:((f:_->_->_)[@local])=letlen=lengthtiniflen=0then[||]else(letr=create~len(f0(unsafe_gett0))infori=1tolen-1dounsafe_setri(fi(unsafe_getti))done;r);;letstable_sortt~compare=Stdlib.Array.stable_sortt~cmp:compareletswaptij=letelt_i=t.(i)inletelt_j=t.(j)inunsafe_settielt_j;unsafe_settjelt_i;;