1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980openSetmoduletypeS=sigincludeSvalof_list:eltlist->tval(+):t->t->t(** union *)val(-):t->t->t(** diff *)valone:elt->t(** singleton *)valunions:tlist->tvalto_list:t->eltlist(** elements *)valunsafe_top_node:t->(t*elt*t)optionvalunsafe_middle:t->eltoption(** find the middle element in [t] *)valunsafe_find:elt->t->eltoption(** find the "same" element as [elt] in [t] *)valunsafe_find_node:elt->t->(t*elt*t)option(** find the node of the "same" element as [elt] in [t] *)endmoduleMake(O:OrderedType)=structincludeMake(O)(* Warning! Unsafe operation!
Currently, there is no easy way to get the binary tree structure from a set,
though it is very handy for efficient binary search.
The following easily breaks when the internal implementation of Set.t is changed.
If the program crashes, check this first.
*)typet_internal=Empty|Nodeoft*elt*t*intletunsafe_internalt=(Obj.magict:t_internal)letof_list=letrecof_listst=function|[]->st|x::xs->of_list(addxst)xsinof_listemptylet(+)=unionlet(-)=diffletone=singletonletunions=List.fold_leftunionemptyletto_list=elementslet_dummy()=[Empty;Node(assertfalse,assertfalse,assertfalse,0)]letunsafe_top_nodet=matchunsafe_internaltwith|Empty->None|Node(left,v,right,_)->Some(left,v,right)letunsafe_middlet=matchunsafe_internaltwith|Empty->None|Node(_,v,_,_)->Somevletrecunsafe_findeltt=matchunsafe_internaltwith|Empty->None|Node(left,elt',right,_)->matchO.compareeltelt'with|0->Someelt'|-1->unsafe_findeltleft|1->unsafe_findeltright|_->assertfalseletrecunsafe_find_nodeeltt=matchunsafe_internaltwith|Empty->None|Node(left,elt',right,_)->(* Format.eprintf "looking %a@." Ord.format elt'; *)matchO.compareeltelt'with|0->Some(left,elt',right)|-1->unsafe_find_nodeeltleft|1->unsafe_find_nodeeltright|_->assertfalseend