From 22e5b26b9417ddc00904dc78adc7ea83077a6527 Mon Sep 17 00:00:00 2001 From: Vincent Bernardoff Date: Mon, 22 Dec 2014 16:40:40 +0100 Subject: [PATCH 1/3] CCHashtbl: Added functions {keys,values}_list. --- src/core/CCHashtbl.ml | 3 +++ src/core/CCHashtbl.mli | 6 ++++++ 2 files changed, 9 insertions(+) diff --git a/src/core/CCHashtbl.ml b/src/core/CCHashtbl.ml index fe5289b7..4bf972f0 100644 --- a/src/core/CCHashtbl.ml +++ b/src/core/CCHashtbl.ml @@ -40,6 +40,9 @@ let keys tbl k = Hashtbl.iter (fun key _ -> k key) tbl let values tbl k = Hashtbl.iter (fun _ v -> k v) tbl +let keys_list tbl = Hashtbl.fold (fun k _ a -> k::a) tbl [] +let values_list tbl = Hashtbl.fold (fun _ v a -> v::a) tbl [] + let map_list f h = Hashtbl.fold (fun x y acc -> f x y :: acc) diff --git a/src/core/CCHashtbl.mli b/src/core/CCHashtbl.mli index fcfde106..9327745d 100644 --- a/src/core/CCHashtbl.mli +++ b/src/core/CCHashtbl.mli @@ -44,6 +44,12 @@ val keys : ('a,'b) Hashtbl.t -> 'a sequence val values : ('a,'b) Hashtbl.t -> 'b sequence (** Iterate on values in the table *) +val keys_list : ('a, 'b) Hashtbl.t -> 'a list +(** [keys t] is the list of keys in [t]. *) + +val values_list : ('a, 'b) Hashtbl.t -> 'b list +(** [values t] is the list of values in [t]. *) + val map_list : ('a -> 'b -> 'c) -> ('a, 'b) Hashtbl.t -> 'c list (** Map on a hashtable's items, collect into a list *) From 86fdb22d5ab0fb4e326b06558387f8b8466c0d02 Mon Sep 17 00:00:00 2001 From: Vincent Bernardoff Date: Mon, 22 Dec 2014 17:27:04 +0100 Subject: [PATCH 2/3] CCHashtbl: Added {keys,values}_list in the functor as well. Better doc. --- src/core/CCHashtbl.ml | 11 +++++++++++ src/core/CCHashtbl.mli | 14 ++++++++++++-- 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/src/core/CCHashtbl.ml b/src/core/CCHashtbl.ml index 4bf972f0..580e6f29 100644 --- a/src/core/CCHashtbl.ml +++ b/src/core/CCHashtbl.ml @@ -84,6 +84,14 @@ module type S = sig val values : 'a t -> 'a sequence (** Iterate on values in the table *) + val keys_list : ('a, 'b) Hashtbl.t -> 'a list + (** [keys t] is the list of keys in [t]. + @since NEXT_RELEASE *) + + val values_list : ('a, 'b) Hashtbl.t -> 'b list + (** [values t] is the list of values in [t]. + @since NEXT_RELEASE *) + val map_list : (key -> 'a -> 'b) -> 'a t -> 'b list (** Map on a hashtable's items, collect into a list *) @@ -111,6 +119,9 @@ module Make(X : Hashtbl.HashedType) = struct let values tbl k = iter (fun _ v -> k v) tbl + let keys_list tbl = Hashtbl.fold (fun k _ a -> k::a) tbl [] + let values_list tbl = Hashtbl.fold (fun _ v a -> v::a) tbl [] + let map_list f h = fold (fun x y acc -> f x y :: acc) diff --git a/src/core/CCHashtbl.mli b/src/core/CCHashtbl.mli index 9327745d..b862bcad 100644 --- a/src/core/CCHashtbl.mli +++ b/src/core/CCHashtbl.mli @@ -45,10 +45,12 @@ val values : ('a,'b) Hashtbl.t -> 'b sequence (** Iterate on values in the table *) val keys_list : ('a, 'b) Hashtbl.t -> 'a list -(** [keys t] is the list of keys in [t]. *) +(** [keys t] is the list of keys in [t]. + @since NEXT_RELEASE *) val values_list : ('a, 'b) Hashtbl.t -> 'b list -(** [values t] is the list of values in [t]. *) +(** [values t] is the list of values in [t]. + @since NEXT_RELEASE *) val map_list : ('a -> 'b -> 'c) -> ('a, 'b) Hashtbl.t -> 'c list (** Map on a hashtable's items, collect into a list *) @@ -79,6 +81,14 @@ module type S = sig val values : 'a t -> 'a sequence (** Iterate on values in the table *) + val keys_list : ('a, 'b) Hashtbl.t -> 'a list + (** [keys t] is the list of keys in [t]. + @since NEXT_RELEASE *) + + val values_list : ('a, 'b) Hashtbl.t -> 'b list + (** [values t] is the list of values in [t]. + @since NEXT_RELEASE *) + val map_list : (key -> 'a -> 'b) -> 'a t -> 'b list (** Map on a hashtable's items, collect into a list *) From fe9a0e58e3743f1499b15066328df9e8a0e461f0 Mon Sep 17 00:00:00 2001 From: Vincent Bernardoff Date: Mon, 22 Dec 2014 17:27:13 +0100 Subject: [PATCH 3/3] Added myself in the list of authors. --- AUTHORS.md | 1 + 1 file changed, 1 insertion(+) diff --git a/AUTHORS.md b/AUTHORS.md index 39b3731c..77bf7dcd 100644 --- a/AUTHORS.md +++ b/AUTHORS.md @@ -8,3 +8,4 @@ - hcarty (Hezekiah M. Carty) - struktured (Carmelo Piccione) - Bernardo da Costa +- Vincent Bernardoff (vbmithr) \ No newline at end of file