|
|
@ -9,7 +9,17 @@
|
|
|
|
|
|
|
|
|
|
|
|
open NS0
|
|
|
|
open NS0
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
module type MULTIPLICITY = sig
|
|
|
|
|
|
|
|
type t [@@deriving compare, equal, hash, sexp]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
val zero : t
|
|
|
|
|
|
|
|
val add : t -> t -> t
|
|
|
|
|
|
|
|
val sub : t -> t -> t
|
|
|
|
|
|
|
|
val neg : t -> t
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
module type S = sig
|
|
|
|
module type S = sig
|
|
|
|
|
|
|
|
type mul
|
|
|
|
type elt
|
|
|
|
type elt
|
|
|
|
type t
|
|
|
|
type t
|
|
|
|
|
|
|
|
|
|
|
@ -18,16 +28,16 @@ module type S = sig
|
|
|
|
val hash_fold_t : elt Hash.folder -> t Hash.folder
|
|
|
|
val hash_fold_t : elt Hash.folder -> t Hash.folder
|
|
|
|
val sexp_of_t : t -> Sexp.t
|
|
|
|
val sexp_of_t : t -> Sexp.t
|
|
|
|
val t_of_sexp : (Sexp.t -> elt) -> Sexp.t -> t
|
|
|
|
val t_of_sexp : (Sexp.t -> elt) -> Sexp.t -> t
|
|
|
|
val pp : (unit, unit) fmt -> (elt * Q.t) pp -> t pp
|
|
|
|
val pp : (unit, unit) fmt -> (elt * mul) pp -> t pp
|
|
|
|
|
|
|
|
|
|
|
|
(* constructors *)
|
|
|
|
(* constructors *)
|
|
|
|
|
|
|
|
|
|
|
|
val empty : t
|
|
|
|
val empty : t
|
|
|
|
(** The empty multiset over the provided order. *)
|
|
|
|
(** The empty multiset over the provided order. *)
|
|
|
|
|
|
|
|
|
|
|
|
val of_ : elt -> Q.t -> t
|
|
|
|
val of_ : elt -> mul -> t
|
|
|
|
|
|
|
|
|
|
|
|
val add : t -> elt -> Q.t -> t
|
|
|
|
val add : t -> elt -> mul -> t
|
|
|
|
(** Add to multiplicity of single element. [O(log n)] *)
|
|
|
|
(** Add to multiplicity of single element. [O(log n)] *)
|
|
|
|
|
|
|
|
|
|
|
|
val remove : t -> elt -> t
|
|
|
|
val remove : t -> elt -> t
|
|
|
@ -36,11 +46,11 @@ module type S = sig
|
|
|
|
val union : t -> t -> t
|
|
|
|
val union : t -> t -> t
|
|
|
|
(** Sum multiplicities pointwise. [O(n + m)] *)
|
|
|
|
(** Sum multiplicities pointwise. [O(n + m)] *)
|
|
|
|
|
|
|
|
|
|
|
|
val map : t -> f:(elt -> Q.t -> elt * Q.t) -> t
|
|
|
|
val map : t -> f:(elt -> mul -> elt * mul) -> t
|
|
|
|
(** Map over the elements in ascending order. Preserves physical equality
|
|
|
|
(** Map over the elements in ascending order. Preserves physical equality
|
|
|
|
if [f] does. *)
|
|
|
|
if [f] does. *)
|
|
|
|
|
|
|
|
|
|
|
|
val map_counts : t -> f:(elt -> Q.t -> Q.t) -> t
|
|
|
|
val map_counts : t -> f:(elt -> mul -> mul) -> t
|
|
|
|
(** Map over the multiplicities of the elements in ascending order. *)
|
|
|
|
(** Map over the multiplicities of the elements in ascending order. *)
|
|
|
|
|
|
|
|
|
|
|
|
(* queries *)
|
|
|
|
(* queries *)
|
|
|
@ -51,44 +61,44 @@ module type S = sig
|
|
|
|
val length : t -> int
|
|
|
|
val length : t -> int
|
|
|
|
(** Number of elements with non-zero multiplicity. [O(1)]. *)
|
|
|
|
(** Number of elements with non-zero multiplicity. [O(1)]. *)
|
|
|
|
|
|
|
|
|
|
|
|
val count : t -> elt -> Q.t
|
|
|
|
val count : t -> elt -> mul
|
|
|
|
(** Multiplicity of an element. [O(log n)]. *)
|
|
|
|
(** Multiplicity of an element. [O(log n)]. *)
|
|
|
|
|
|
|
|
|
|
|
|
val choose_exn : t -> elt * Q.t
|
|
|
|
val choose_exn : t -> elt * mul
|
|
|
|
(** Find an unspecified element. [O(1)]. *)
|
|
|
|
(** Find an unspecified element. [O(1)]. *)
|
|
|
|
|
|
|
|
|
|
|
|
val choose : t -> (elt * Q.t) option
|
|
|
|
val choose : t -> (elt * mul) option
|
|
|
|
(** Find an unspecified element. [O(1)]. *)
|
|
|
|
(** Find an unspecified element. [O(1)]. *)
|
|
|
|
|
|
|
|
|
|
|
|
val pop : t -> (elt * Q.t * t) option
|
|
|
|
val pop : t -> (elt * mul * t) option
|
|
|
|
(** Find and remove an unspecified element. [O(1)]. *)
|
|
|
|
(** Find and remove an unspecified element. [O(1)]. *)
|
|
|
|
|
|
|
|
|
|
|
|
val min_elt : t -> (elt * Q.t) option
|
|
|
|
val min_elt : t -> (elt * mul) option
|
|
|
|
(** Minimum element. [O(log n)]. *)
|
|
|
|
(** Minimum element. [O(log n)]. *)
|
|
|
|
|
|
|
|
|
|
|
|
val pop_min_elt : t -> (elt * Q.t * t) option
|
|
|
|
val pop_min_elt : t -> (elt * mul * t) option
|
|
|
|
(** Find and remove minimum element. [O(log n)]. *)
|
|
|
|
(** Find and remove minimum element. [O(log n)]. *)
|
|
|
|
|
|
|
|
|
|
|
|
val classify : t -> [`Zero | `One of elt * Q.t | `Many]
|
|
|
|
val classify : t -> [`Zero | `One of elt * mul | `Many]
|
|
|
|
(** Classify a set as either empty, singleton, or otherwise. *)
|
|
|
|
(** Classify a set as either empty, singleton, or otherwise. *)
|
|
|
|
|
|
|
|
|
|
|
|
val find_and_remove : t -> elt -> (Q.t * t) option
|
|
|
|
val find_and_remove : t -> elt -> (mul * t) option
|
|
|
|
(** Find and remove an element. *)
|
|
|
|
(** Find and remove an element. *)
|
|
|
|
|
|
|
|
|
|
|
|
val to_list : t -> (elt * Q.t) list
|
|
|
|
val to_list : t -> (elt * mul) list
|
|
|
|
(** Convert to a list of elements in ascending order. *)
|
|
|
|
(** Convert to a list of elements in ascending order. *)
|
|
|
|
|
|
|
|
|
|
|
|
(* traversals *)
|
|
|
|
(* traversals *)
|
|
|
|
|
|
|
|
|
|
|
|
val iter : t -> f:(elt -> Q.t -> unit) -> unit
|
|
|
|
val iter : t -> f:(elt -> mul -> unit) -> unit
|
|
|
|
(** Iterate over the elements in ascending order. *)
|
|
|
|
(** Iterate over the elements in ascending order. *)
|
|
|
|
|
|
|
|
|
|
|
|
val exists : t -> f:(elt -> Q.t -> bool) -> bool
|
|
|
|
val exists : t -> f:(elt -> mul -> bool) -> bool
|
|
|
|
(** Search for an element satisfying a predicate. *)
|
|
|
|
(** Search for an element satisfying a predicate. *)
|
|
|
|
|
|
|
|
|
|
|
|
val for_all : t -> f:(elt -> Q.t -> bool) -> bool
|
|
|
|
val for_all : t -> f:(elt -> mul -> bool) -> bool
|
|
|
|
(** Test whether all elements satisfy a predicate. *)
|
|
|
|
(** Test whether all elements satisfy a predicate. *)
|
|
|
|
|
|
|
|
|
|
|
|
val fold : t -> f:(elt -> Q.t -> 's -> 's) -> init:'s -> 's
|
|
|
|
val fold : t -> f:(elt -> mul -> 's -> 's) -> init:'s -> 's
|
|
|
|
(** Fold over the elements in ascending order. *)
|
|
|
|
(** Fold over the elements in ascending order. *)
|
|
|
|
end
|
|
|
|
end
|
|
|
|