123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139(** Implements iterators for arbitrary types, allowing sequential access to arbitrary
containers. The interface is designed so that multiple kinds of iteration can be
implemented efficiently. For example, both lists and strings have iterators
implemented below that do not allocate. *)open!Base(** [Impl*] define the interface to the underlying implementation of an iterator. *)moduletypeImpl1=sig(** Sequential collections. *)type'aseq(** Elements of a collection. *)type'aelt(** Iterators into a collection. *)type'at(** Creates an iterator at the first element of a collection. *)valstart:'aseq->'at(** Reports [true] when the iterator is past all elements of a collection. *)valis_finished:'at->'aseq->bool(** Gets the current element of a collection. May raise if [is_finished t]. *)valget_exn:'at->'aseq->'aelt(** Returns an iterator past the current element. May raise if [is_finished t]. *)valnext_exn:'at->'aseq->'atendmoduletypeImpl0=sigtypeseqtypeelttypetincludeImpl1withtype_seq:=seqandtype_elt:=eltandtype_t:=tend(** [S*] provide the interface to iterators with phantom types. An [Iterator.S0] is like a
[Comparator.S], it carries an implementation of iteration for a specific type and a
phantom type witnessing that specific implementation. *)moduletypeS1=sig(** The type of an iterator implementation. See [t] in [Iterator], below. *)type('iter,'seq,'elt,'idx)iterator(** Sequential collections. *)type'aseq(** Elements of a collection. *)type'aelt(** Iterators into a collection. *)type'at(** The phantom type identifying this implementation. *)typeiterator_witnessvaliterator:('at,'aseq,'aelt,iterator_witness)iteratorendmoduletypeS0=sigtypeseqtypeelttypetincludeS1withtype_seq:=seqandtype_elt:=eltandtype_t:=tend(** [Listable*] implement types that may be converted to lists. Used to construct
iterators into these types. *)moduletypeListable1=sigtype'aelttype'atvalto_list:'at->'aeltlistendmoduletypeListable0=sigtypeelttypetincludeListable1withtype_elt:=eltandtype_t:=tendmoduletypeIterator=sigmoduletypeImpl0=Impl0moduletypeImpl1=Impl1moduletypeListable0=Listable0moduletypeListable1=Listable1(** Represents an iterator implementation. Iterators have type ['iter]. They iterate
over sequential collections of type ['seq] containing elements of type ['elt]. The
implementation is identified by the phantom type ['idx]. *)type('iter,'seq,'elt,'idx)t=private(moduleImpl0withtypet='iterandtypeseq='seqandtypeelt='elt)moduletypeS0=S0withtype('iter,'seq,'elt,'idx)iterator:=('iter,'seq,'elt,'idx)tmoduletypeS1=S1withtype('iter,'seq,'elt,'idx)iterator:=('iter,'seq,'elt,'idx)t(** Accessors into the implementation of an iterator. *)valstart:('iter,'seq,_,_)t->'seq->'itervalis_finished:('iter,'seq,_,_)t->'iter->'seq->boolvalget_exn:('iter,'seq,'elt,_)t->'iter->'seq->'eltvalnext_exn:('iter,'seq,_,_)t->'iter->'seq->'iter(** Modules and functors for constructing iterators. *)moduleMake1(Impl:Impl1):S1withtype'at='aImpl.tandtype'aseq='aImpl.seqandtype'aelt='aImpl.eltmoduleMake0(Impl:Impl0):S0withtypet=Impl.tandtypeseq=Impl.seqandtypeelt=Impl.eltmoduleMonomorphic(Iterator:S1)(Elt:T):S0withtypet=Elt.tIterator.tandtypeseq=Elt.tIterator.seqandtypeelt=Elt.tIterator.eltandtypeiterator_witness=Iterator.iterator_witnessmoduleOf_string:S0withtypeseq=stringandtypeelt=charmoduleOf_list:S1withtype'aseq='alistandtype'aelt='amoduleOf_listable0(Seq:Listable0):S0withtypeseq=Seq.tandtypeelt=Seq.eltmoduleOf_listable1(Seq:Listable1):S1withtype'aseq='aSeq.tandtype'aelt='aSeq.eltend