You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
98 lines
2.8 KiB
98 lines
2.8 KiB
6 years ago
|
(*
|
||
6 years ago
|
* Copyright (c) Facebook, Inc. and its affiliates.
|
||
6 years ago
|
*
|
||
|
* This source code is licensed under the MIT license found in the
|
||
|
* LICENSE file in the root directory of this source tree.
|
||
|
*)
|
||
|
|
||
6 years ago
|
(** Qset - Set with (signed) rational multiplicity for each element *)
|
||
6 years ago
|
|
||
5 years ago
|
open NS0
|
||
5 years ago
|
include Qset_intf
|
||
6 years ago
|
|
||
5 years ago
|
module Make (Elt : sig
|
||
|
type t [@@deriving compare, sexp_of]
|
||
|
end) =
|
||
|
struct
|
||
|
module M = Map.Make (Elt)
|
||
5 years ago
|
|
||
|
type elt = Elt.t
|
||
|
type t = Q.t M.t
|
||
|
|
||
|
let compare = M.compare Q.compare
|
||
|
let equal = M.equal Q.equal
|
||
|
|
||
|
let hash_fold_t hash_fold_elt s m =
|
||
|
let hash_fold_q s q = Hash.fold_int s (Hashtbl.hash q) in
|
||
5 years ago
|
M.fold m
|
||
|
~init:(Hash.fold_int s (M.length m))
|
||
|
~f:(fun ~key ~data state -> hash_fold_q (hash_fold_elt state key) data)
|
||
5 years ago
|
|
||
|
let sexp_of_t s =
|
||
|
let sexp_of_q q = Sexp.Atom (Q.to_string q) in
|
||
|
List.sexp_of_t
|
||
|
(Sexplib.Conv.sexp_of_pair Elt.sexp_of_t sexp_of_q)
|
||
5 years ago
|
(M.to_alist s)
|
||
5 years ago
|
|
||
|
let t_of_sexp elt_of_sexp sexp =
|
||
|
let q_of_sexp = function
|
||
|
| Sexp.Atom s -> Q.of_string s
|
||
|
| _ -> assert false
|
||
|
in
|
||
|
List.fold_left
|
||
5 years ago
|
~f:(fun m (key, data) -> M.add_exn m ~key ~data)
|
||
5 years ago
|
~init:M.empty
|
||
|
(List.t_of_sexp
|
||
|
(Sexplib.Conv.pair_of_sexp elt_of_sexp q_of_sexp)
|
||
|
sexp)
|
||
|
|
||
5 years ago
|
let pp sep pp_elt fs s = List.pp sep pp_elt fs (M.to_alist s)
|
||
5 years ago
|
let empty = M.empty
|
||
5 years ago
|
let of_ = M.singleton
|
||
5 years ago
|
let if_nz q = if Q.equal Q.zero q then None else Some q
|
||
|
|
||
|
let add m x i =
|
||
5 years ago
|
M.change m x ~f:(function Some j -> if_nz Q.(i + j) | None -> if_nz i)
|
||
5 years ago
|
|
||
5 years ago
|
let remove m x = M.remove m x
|
||
5 years ago
|
let find_and_remove = M.find_and_remove
|
||
5 years ago
|
|
||
|
let union m n =
|
||
5 years ago
|
M.merge m n ~f:(fun ~key:_ -> function
|
||
|
| `Both (i, j) -> if_nz Q.(i + j) | `Left i | `Right i -> Some i )
|
||
5 years ago
|
|
||
|
let map m ~f =
|
||
5 years ago
|
let m' = empty in
|
||
5 years ago
|
let m, m' =
|
||
5 years ago
|
M.fold m ~init:(m, m') ~f:(fun ~key:x ~data:i (m, m') ->
|
||
5 years ago
|
let x', i' = f x i in
|
||
|
if x' == x then
|
||
5 years ago
|
if Q.equal i' i then (m, m') else (M.set m ~key:x ~data:i', m')
|
||
|
else (M.remove m x, add m' x' i') )
|
||
5 years ago
|
in
|
||
5 years ago
|
M.fold m' ~init:m ~f:(fun ~key:x ~data:i m -> add m x i)
|
||
5 years ago
|
|
||
5 years ago
|
let map_counts m ~f = M.mapi ~f:(fun ~key ~data -> f key data) m
|
||
5 years ago
|
let is_empty = M.is_empty
|
||
5 years ago
|
let is_singleton = M.is_singleton
|
||
5 years ago
|
let length m = M.length m
|
||
|
let count m x = match M.find m x with Some q -> q | None -> Q.zero
|
||
5 years ago
|
let choose = M.choose
|
||
5 years ago
|
let choose_exn = M.choose_exn
|
||
5 years ago
|
let pop = M.pop
|
||
5 years ago
|
let min_elt = M.min_elt
|
||
5 years ago
|
let pop_min_elt = M.pop_min_elt
|
||
5 years ago
|
|
||
|
let classify s =
|
||
|
match pop s with
|
||
|
| None -> `Zero
|
||
|
| Some (elt, q, s') when is_empty s' -> `One (elt, q)
|
||
|
| _ -> `Many
|
||
|
|
||
5 years ago
|
let to_list m = M.to_alist m
|
||
|
let iter m ~f = M.iteri ~f:(fun ~key ~data -> f key data) m
|
||
|
let exists m ~f = M.existsi ~f:(fun ~key ~data -> f key data) m
|
||
5 years ago
|
let for_all m ~f = M.for_alli ~f:(fun ~key ~data -> f key data) m
|
||
5 years ago
|
let fold m ~f ~init = M.fold ~f:(fun ~key ~data -> f key data) m ~init
|
||
5 years ago
|
end
|