123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131(* This file is free software, part of containers. See file "license" for more details. *)(** {1 Immutable Arrays} *)(* TODO: tests *)(* TODO: transient API? for batch modifications *)type'at='aarrayletempty=[||]letlength=Array.lengthletsingletonx=[|x|]letdoubletonxy=[|x;y|]letmakenx=Array.makenxletinitnf=Array.initnfletget=Array.getletsetanx=leta'=Array.copyaina'.(n)<-x;a'letsub=Array.sub(* Would this not be better implemented with CCArray_slice *)letmap=Array.mapletmapi=Array.mapiletappendab=letna=lengthainArray.init(na+lengthb)(funi->ifi<nathena.(i)elseb.(i-na))letiter=Array.iterletiteri=Array.iteriletfold=Array.fold_leftletfoldifacca=letn=ref0inArray.fold_left(funaccx->letacc=facc!nxinincrn;acc)accaexceptionExitNowletfor_allpa=tryArray.iter(funx->ifnot(px)thenraiseExitNow)a;truewithExitNow->falseletexistspa=tryArray.iter(funx->ifpxthenraiseExitNow)a;falsewithExitNow->true(** {2 Conversions} *)type'asequence=('a->unit)->unittype'agen=unit->'aoptionletof_list=Array.of_listletto_list=Array.to_listletof_array_unsafea=a(* careful with that axe, Eugene *)letto_seqak=iterkaletof_seqs=letl=ref[]ins(funx->l:=x::!l);Array.of_list(List.rev!l)(*$Q
Q.(list int) (fun l -> \
let g = Iter.of_list l in \
of_seq g |> to_seq |> Iter.to_list = l)
*)letrecgen_to_list_accg=matchg()with|None->List.revacc|Somex->gen_to_list_(x::acc)gletof_geng=letl=gen_to_list_[]ginArray.of_listlletto_gena=leti=ref0infun()->if!i<Array.lengthathen(letx=a.(!i)inincri;Somex)elseNone(*$Q
Q.(list int) (fun l -> \
let g = Gen.of_list l in \
of_gen g |> to_gen |> Gen.to_list = l)
*)(** {2 IO} *)type'aprinter=Format.formatter->'a->unitletpp?(start="")?(stop="")?(sep=", ")pp_itemouta=Format.pp_print_stringoutstart;fork=0toArray.lengtha-1doifk>0then(Format.pp_print_stringoutsep;Format.pp_print_cutout());pp_itemouta.(k)done;Format.pp_print_stringoutstop;()