@ -6,57 +6,78 @@
* )
open NS0
module Option = CCOpt
include Set_intf
module Make ( Elt : sig
type t [ @@ deriving compare , sexp_of ]
end ) : S with type elt = Elt . t = struct
module EltSet = Core . Set . Make_plain ( Elt )
module Elt = EltSet . Elt
module S = CCSet . Make ( Elt )
type elt = Elt . t
type t = S . t [ @@ deriving compare , equal ]
include EltSet . Tree
let sexp_of_t s = S . to_list s | > Sexplib . Conv . sexp_of_list Elt . sexp_of_t
let hash_fold_t hash_fold_elt s m =
fold ~ f : hash_fold_elt ~ init : ( Hash . fold_int s ( length m ) ) m
module Provide_of_sexp ( Elt : sig
type t = elt [ @@ deriving of_sexp ]
end ) =
struct
let t_of_sexp s =
s | > Sexplib . Conv . list_of_sexp Elt . t_of_sexp | > S . of_list
end
let pp ? pre ? suf ? ( sep = ( " ,@ " : ( unit , unit ) fmt ) ) pp_elt fs x =
List . pp ? pre ? suf sep pp_elt fs ( elements x )
let pp_diff pp_elt fs ( xs , ys ) =
let lose = diff xs ys and gain = diff ys xs in
if not ( is_empty lose ) then Format . fprintf fs " -- %a " ( pp pp_elt ) lose ;
if not ( is_empty gain ) then Format . fprintf fs " ++ %a " ( pp pp_elt ) gain
let of_ x = add empty x
let of_option = Option . fold ~ f : add ~ init : empty
let of_iarray a = of_array ( IArray . to_array a )
let add_option xo s = Option . fold ~ f : add ~ init : s xo
let add_list xs s = List . fold ~ f : add ~ init : s xs
let empty = S . empty
let of_ = S . singleton
let of_option xo = Option . map_or S . singleton xo ~ default : empty
let of_list = S . of_list
let add s x = S . add x s
let add_option xo s = Option . fold add s xo
let add_list xs s = S . add_list s xs
let diff = S . diff
let inter = S . inter
let union = S . union
let diff_inter s t = ( diff s t , inter s t )
let union_list ss = List . fold ~ f : union ~ init : empty ss
let is_empty = S . is_empty
let mem s x = S . mem x s
let is_subset s ~ of_ : t = S . subset s t
let disjoint = S . disjoint
let max_elt = S . max_elt_opt
let rec disjoint s1 s2 =
match choose s1 with
| None -> true
| _ when is_empty s2 -> true
| _ when s1 = = s2 -> false
| Some x -> (
let l1 , _ , r1 = split s1 x in
match split s2 x with
| _ , Some _ , _ -> false
| l2 , None , r2 -> disjoint l1 l2 && disjoint r1 r2 )
let choose_exn s =
let @ { return } = with_return in
binary_search_segmented s ` Last_on_left ~ segment_of : return | > ignore ;
raise ( Not_found_s ( Atom _ _ LOC__ ) )
let root_elt s =
let exception Found in
let found = ref None in
try
S . for_all
( fun elt ->
found := Some elt ;
raise Found )
s
| > ignore ;
None
with Found -> ! found
let choose s = try Some ( choose_exn s ) with Not_found_s _ -> None
let choose = root_elt
let choose_exn m = Option . get_exn ( choose m )
let pop_exn s =
let elt = choose_exn s in
( elt , remove s elt )
( elt , S . remove elt s )
let pop s = choose s | > Option . map ~ f : ( fun elt -> ( elt , remove s elt ) )
let elements = S . elements
let map s ~ f = S . map f s
let filter s ~ f = S . filter f s
let iter s ~ f = S . iter f s
let exists s ~ f = S . exists f s
let for_all s ~ f = S . for_all f s
let fold s ~ init ~ f = S . fold ( fun x a -> f a x ) s init
let pp ? pre ? suf ? ( sep = ( " ,@ " : ( unit , unit ) fmt ) ) pp_elt fs x =
List . pp ? pre ? suf sep pp_elt fs ( S . elements x )
let pp_diff pp_elt fs ( xs , ys ) =
let lose = diff xs ys and gain = diff ys xs in
if not ( is_empty lose ) then Format . fprintf fs " -- %a " ( pp pp_elt ) lose ;
if not ( is_empty gain ) then Format . fprintf fs " ++ %a " ( pp pp_elt ) gain
end