123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216(* This file is free software, part of containers. See file "license" for more details. *)(** {1 Wrapper around Set} *)type'aiter=('a->unit)->unittype'asequence=('a->unit)->unittype'aprinter=Format.formatter->'a->unitmoduletypeOrderedType=Set.OrderedType(*$inject
module S = CCSet.Make(struct
type t = int
let compare x y = Stdlib.compare x y
end)
*)moduletypeS=sigincludeSet.Svalmin_elt_opt:t->eltoption(** Safe version of {!min_elt}.
@since 1.5 *)valmax_elt_opt:t->eltoption(** Safe version of {!max_elt}.
@since 1.5 *)valchoose_opt:t->eltoption(** Safe version of {!choose}.
@since 1.5 *)valfind_opt:elt->t->eltoption(** Safe version of {!find}.
@since 1.5 *)valfind_first:(elt->bool)->t->elt(** Find minimum element satisfying predicate.
@since 1.5 *)valfind_first_opt:(elt->bool)->t->eltoption(** Safe version of {!find_first}.
@since 1.5 *)valfind_last:(elt->bool)->t->elt(** Find maximum element satisfying predicate.
@since 1.5 *)valfind_last_opt:(elt->bool)->t->eltoption(** Safe version of {!find_last}.
@since 1.5 *)valof_iter:eltiter->t(** Build a set from the given [iter] of elements.
@since 2.8 *)valadd_iter:t->eltiter->t(** @since 2.8 *)valto_iter:t->eltiter(** [to_iter t] converts the set [t] to a [iter] of the elements.
@since 2.8 *)valof_seq:eltsequence->t(** Build a set from the given [sequence] of elements.
@deprecated use {!of_iter} instead. *)[@@ocaml.deprecated"use of_iter instead"]valadd_seq:t->eltsequence->t(** @since 0.14
@deprecated use {!add_iter} instead. *)[@@ocaml.deprecated"use add_iter instead"]valto_seq:t->eltsequence(** [to_seq t] converts the set [t] to a [sequence] of the elements.
@deprecated use {!to_iter} instead. *)[@@ocaml.deprecated"use to_iter instead"]valof_list:eltlist->t(** Build a set from the given list of elements,
added in order using {!add}. *)valadd_list:t->eltlist->t(** @since 0.14 *)valto_list:t->eltlist(** [to_list t] converts the set [t] to a list of the elements. *)valto_string:?start:string->?stop:string->?sep:string->(elt->string)->t->string(** Print the set in a string
@since 2.7 *)valpp:?start:string->?stop:string->?sep:string->eltprinter->tprinter(** Print the set. *)endmoduleMake(O:Map.OrderedType)=structmoduleS=Set.Make(O)(* backport functions from recent stdlib.
they will be shadowed by inclusion of [S] if present. *)letfind_optxs=trySome(S.findxs)withNot_found->Noneletchoose_opts=trySome(S.chooses)withNot_found->Noneletmin_elt_opts=trySome(S.min_elts)withNot_found->Noneletmax_elt_opts=trySome(S.max_elts)withNot_found->NoneexceptionFind_binding_exitletfind_first_optfm=letres=refNoneintryS.iter(funx->iffxthen(res:=Somex;raiseFind_binding_exit))m;NonewithFind_binding_exit->!resletfind_firstfm=matchfind_first_optfmwith|None->raiseNot_found|Somex->x(* linear time, must traverse the whole set… *)letfind_last_optfm=letres=refNoneinS.iter(funx->iffxthenres:=Somex)m;!resletfind_lastfm=matchfind_last_optfmwith|None->raiseNot_found|Somex->xincludeSletadd_std_seqsetseq=letset=refsetinSeq.iter(funx->set:=addx!set)seq;!setletof_std_seqs=add_std_seqemptysletadd_iterseti=letset=refsetini(funx->set:=addx!set);!setletof_iters=add_iteremptysletto_itersyield=iteryieldsletadd_seq=add_iterletof_seq=of_iterletto_seq=to_iterletadd_list=List.fold_left(funsetx->addxset)letof_listl=add_listemptylletto_list=elementsletto_string?(start="")?(stop="")?(sep=",")elt_to_stringh=to_listh|>CCList.to_string~start~stop~sepelt_to_string(*$= & ~printer:(fun s -> s)
(S.to_string string_of_int (S.of_list [4; 3])) "3,4"
*)(*$Q
Q.(list int) (fun l -> \
let s = S.of_list l in \
(S.to_string string_of_int s) \
= (CCList.sort_uniq ~cmp:CCInt.compare l \
|> List.map string_of_int |> String.concat ","))
Q.(list int) (fun l -> \
let s = S.of_list l in \
(S.to_string ~sep:" " string_of_int s) \
= (CCList.sort_uniq ~cmp:CCInt.compare l \
|> List.map string_of_int |> String.concat " "))
*)letpp?(start="")?(stop="")?(sep=", ")pp_xfmtm=Format.pp_print_stringfmtstart;letfirst=reftrueiniter(funx->if!firstthenfirst:=falseelse(Format.pp_print_stringfmtsep;Format.pp_print_cutfmt());pp_xfmtx)m;Format.pp_print_stringfmtstopend