struct
type t = Biocaml_iset.t Map.t
let inter u v =
Map.fold u ~init:Map.empty ~f:(fun ~key:k ~data:set_u accu ->
match Map.find v k with
| Some set_v -> Map.add accu ~key:k ~data:(Biocaml_iset.inter set_u set_v)
| None -> accu
)
let union u v =
let keys = List.dedup (Map.keys u @ Map.keys v) in
List.fold keys ~init:Map.empty ~f:(fun accu k ->
Map.add accu ~key:k ~data:(
Biocaml_iset.union
(Option.value (Map.find u k) ~default:Biocaml_iset.empty)
(Option.value (Map.find v k) ~default:Biocaml_iset.empty)
)
)
let diff u v =
Map.fold u ~init:Map.empty ~f:(fun ~key:k ~data:set_u accu ->
let set_u' =
match Map.find v k with
| Some set_v -> Biocaml_iset.diff set_u set_v
| None -> set_u
in
Map.add ~key:k ~data:set_u' accu
)
let size x =
Map.fold x ~init:0 ~f:(fun ~key ~data:set accu -> Biocaml_iset.cardinal set + accu)
let overlap sel (k,r) = Biocaml_iset.(
match Map.find sel k with
| Some x ->
inter Range.(add_range empty r.lo r.hi) x
|> cardinal
| None -> 0
)
let intersects sel (k,r) =
Option.value_map
(Map.find sel k)
~default:false
~f:(fun x -> Range.(Biocaml_iset.intersects_range x r.lo r.hi))
let to_stream sel =
(Map.to_stream sel)
/@ (fun (k,s) -> Stream.map ~f:(fun (lo,hi) -> k, ok_exn (Range.make lo hi)) (Biocaml_iset.to_stream s))
|> Stream.concat
let of_stream e =
let accu = Accu.create Biocaml_iset.empty fst (fun (_,r) -> Range.(fun x -> Biocaml_iset.add_range x r.lo r.hi)) in
Stream.iter ~f:(fun loc -> Accu.add accu loc loc ) e ;
Map.of_stream (Accu.stream accu)
end