From ff2ab244f5ec7f3f4ee2715619d5dcc444d2d981 Mon Sep 17 00:00:00 2001 From: Simon Cruanes Date: Fri, 18 Jul 2014 02:14:02 +0200 Subject: [PATCH] sequence and CCMultiSet --- core/CCMultiSet.ml | 14 ++++++++++++++ core/CCMultiSet.mli | 6 ++++++ 2 files changed, 20 insertions(+) diff --git a/core/CCMultiSet.ml b/core/CCMultiSet.ml index 41d147f8..60640411 100644 --- a/core/CCMultiSet.ml +++ b/core/CCMultiSet.ml @@ -25,6 +25,8 @@ for any direct, indirect, incidental, special, exemplary, or consequential (** {1 Multiset} *) +type 'a sequence = ('a -> unit) -> unit + module type S = sig type elt type t @@ -69,6 +71,10 @@ module type S = sig val of_list : elt list -> t val to_list : t -> elt list + + val to_seq : t -> elt sequence + + val of_seq : elt sequence -> t end module Make(O : Set.OrderedType) = struct @@ -172,4 +178,12 @@ module Make(O : Set.OrderedType) = struct | _ -> n_cons (n-1) x (x::l) in fold m [] (fun acc n x -> n_cons n x acc) + + let to_seq m k = + M.iter (fun x n -> for _i = 1 to n do k x done) m + + let of_seq seq = + let m = ref empty in + seq (fun x -> m := add !m x); + !m end diff --git a/core/CCMultiSet.mli b/core/CCMultiSet.mli index 99e0521f..4c994901 100644 --- a/core/CCMultiSet.mli +++ b/core/CCMultiSet.mli @@ -25,6 +25,8 @@ for any direct, indirect, incidental, special, exemplary, or consequential (** {1 Multiset} *) +type 'a sequence = ('a -> unit) -> unit + module type S = sig type elt type t @@ -69,6 +71,10 @@ module type S = sig val of_list : elt list -> t val to_list : t -> elt list + + val to_seq : t -> elt sequence + + val of_seq : elt sequence -> t end module Make(O : Set.OrderedType) : S with type elt = O.t