Legend:
Page
Library
Module
Module type
Parameter
Class
Class type
Source
Source file CCMultiSet.ml
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236(* This file is free software, part of containers. See file "license" for more details. *)(** {1 Multiset} *)type'aiter=('a->unit)->unitletmax_int=maxletmin_int=minmoduletypeS=sigtypeelttypetvalempty:tvalis_empty:t->boolvalmem:t->elt->boolvalcount:t->elt->intvalsingleton:elt->tvaladd:t->elt->tvalremove:t->elt->tvaladd_mult:t->elt->int->t(** [add_mult set x n] adds [n] occurrences of [x] to [set]
@raise Invalid_argument if [n < 0]
@since 0.6 *)valremove_mult:t->elt->int->t(** [remove_mult set x n] removes at most [n] occurrences of [x] from [set]
@raise Invalid_argument if [n < 0]
@since 0.6 *)valremove_all:t->elt->t(** [remove_all set x] removes all occurrences of [x] from [set]
@since 0.22 *)valupdate:t->elt->(int->int)->t(** [update set x f] calls [f n] where [n] is the current multiplicity
of [x] in [set] ([0] to indicate its absence); the result of [f n]
is the new multiplicity of [x].
@raise Invalid_argument if [f n < 0]
@since 0.6 *)valmin:t->elt(** Minimal element w.r.t the total ordering on elements *)valmax:t->elt(** Maximal element w.r.t the total ordering on elements *)valunion:t->t->t(** [union a b] contains as many occurrences of an element [x]
as [count a x + count b x]. *)valmeet:t->t->t(** [meet a b] is a multiset such that
[count (meet a b) x = max (count a x) (count b x)] *)valintersection:t->t->t(** [intersection a b] is a multiset such that
[count (intersection a b) x = min (count a x) (count b x)] *)valdiff:t->t->t(** MultiSet difference.
[count (diff a b) x = max (count a x - count b x) 0] *)valcontains:t->t->bool(** [contains a x = (count m x > 0)] *)valcompare:t->t->intvalequal:t->t->boolvalcardinal:t->int(** Number of distinct elements *)valiter:t->(int->elt->unit)->unitvalfold:t->'b->('b->int->elt->'b)->'bvalof_list:eltlist->tvalto_list:t->eltlistvalto_iter:t->eltitervalof_iter:eltiter->tvalof_list_mult:(elt*int)list->t(** @since 0.19 *)valto_list_mult:t->(elt*int)list(** @since 0.19 *)valto_iter_mult:t->(elt*int)iter(** @since 0.19 *)valof_iter_mult:(elt*int)iter->t(** @since 0.19 *)endmoduleMake(O:Set.OrderedType)=structmoduleM=Map.Make(O)typet=intM.ttypeelt=O.tletempty=M.emptyletis_empty=M.is_emptyletmemmsx=M.memxmsletcountmsx=tryM.findxmswithNot_found->0letsingletonx=M.singletonx1letaddmsx=letn=countmsxinM.addx(n+1)msletadd_multmsxn=ifn<0theninvalid_arg"CCMultiSet.add_mult";ifn=0thenmselseM.addx(countmsx+n)msletremove_multmsxn=ifn<0theninvalid_arg"CCMultiSet.remove_mult";letcur_n=countmsxinletnew_n=cur_n-ninifnew_n<=0thenM.removexmselseM.addxnew_nmsletremovemsx=remove_multmsx1letremove_allmsx=M.removexmsletupdatemsxf=letn=countmsxinmatchfnwith|0->ifn=0thenmselseM.removexms|n'->ifn'<0theninvalid_arg"CCMultiSet.update"elseM.addxn'msletminms=fst(M.min_bindingms)letmaxms=fst(M.max_bindingms)letunionm1m2=M.merge(fun_xn1n2->matchn1,n2with|None,None->assertfalse|Somen,None|None,Somen->Somen|Somen1,Somen2->Some(n1+n2))m1m2letmeetm1m2=M.merge(fun_n1n2->matchn1,n2with|None,None->assertfalse|Somen,None|None,Somen->Somen|Somen1,Somen2->Some(max_intn1n2))m1m2letintersectionm1m2=M.merge(fun_xn1n2->matchn1,n2with|None,None->assertfalse|Some_,None|None,Some_->None|Somen1,Somen2->Some(min_intn1n2))m1m2letdiffm1m2=M.merge(fun_xn1n2->matchn1,n2with|None,None->assertfalse|Somen1,None->Somen1|None,Some_n2->None|Somen1,Somen2->ifn1>n2thenSome(n1-n2)elseNone)m1m2letcontainsm1m2=tryM.for_all(funxc->M.findxm1>=c)m2withNot_found->falseletcomparem1m2=M.compare(funxy->x-y)m1m2letequalm1m2=M.equal(funxy->x=y)m1m2letcardinalm=M.cardinalmletitermf=M.iter(funxn->fnx)mletfoldmaccf=M.fold(funxnacc->faccnx)maccletof_listl=letrecbuildaccl=matchlwith|[]->acc|x::l'->build(addaccx)l'inbuildemptylletto_listm=(* [n_cons n x l] is the result of applying [fun l -> x :: l] [n] times
to [l] *)letrecn_consnxl=matchnwith|0->l|1->x::l|_->n_cons(n-1)x(x::l)infoldm[](funaccnx->n_consnxacc)letto_itermk=M.iter(funxn->for_i=1tondokxdone)mletof_iterseq=letm=refemptyinseq(funx->m:=add!mx);!mletof_list_multl=List.fold_left(funs(x,i)->add_multsxi)emptylletto_list_multm=foldm[](funaccnx->(x,n)::acc)letto_iter_multmk=M.iter(funxn->k(x,n))mletof_iter_multseq=letm=refemptyinseq(fun(x,n)->m:=add_mult!mxn);!mend