From 8e33ab4f2dae6619400eb61222e6be2ad9b16da7 Mon Sep 17 00:00:00 2001 From: Xavier Leroy Date: Sun, 18 Sep 2011 09:35:27 +0000 Subject: [PATCH] Improve backward compatibility for Hashtbl functorial interface: Hashtbl.Make returns a "create" function without an optional seed parameter. (Which would be ignored anyway.) Hashtbl.MakeSeeded returns a "create" function with an optional seed parameter. git-svn-id: http://caml.inria.fr/svn/ocaml/trunk@11204 f963ae5c-01c2-4b8c-9fe0-0dff7051ff02 --- stdlib/hashtbl.ml | 36 +++++++++++++++++++++++++++++------- stdlib/hashtbl.mli | 31 +++++++++++++++++++++++++------ stdlib/moreLabels.mli | 22 +++++++++++++++++++++- 3 files changed, 75 insertions(+), 14 deletions(-) diff --git a/stdlib/hashtbl.ml b/stdlib/hashtbl.ml index b91b8e512..501213836 100644 --- a/stdlib/hashtbl.ml +++ b/stdlib/hashtbl.ml @@ -220,7 +220,7 @@ module type S = sig type key type 'a t - val create: ?seed:int -> int -> 'a t + val create: int -> 'a t val clear: 'a t -> unit val copy: 'a t -> 'a t val add: 'a t -> key -> 'a -> unit @@ -235,7 +235,26 @@ module type S = val stats: 'a t -> statistics end -module MakeSeeded(H: SeededHashedType): (S with type key = H.t) = +module type SeededS = + sig + type key + type 'a t + val create : ?seed:int -> int -> 'a t + val clear : 'a t -> unit + val copy : 'a t -> 'a t + val add : 'a t -> key -> 'a -> unit + val remove : 'a t -> key -> unit + val find : 'a t -> key -> 'a + val find_all : 'a t -> key -> 'a list + val replace : 'a t -> key -> 'a -> unit + val mem : 'a t -> key -> bool + val iter : (key -> 'a -> unit) -> 'a t -> unit + val fold : (key -> 'a -> 'b -> 'b) -> 'a t -> 'b -> 'b + val length : 'a t -> int + val stats: 'a t -> statistics + end + +module MakeSeeded(H: SeededHashedType): (SeededS with type key = H.t) = struct type key = H.t type 'a hashtbl = (key, 'a) t @@ -327,8 +346,11 @@ module MakeSeeded(H: SeededHashedType): (S with type key = H.t) = end module Make(H: HashedType): (S with type key = H.t) = - MakeSeeded(struct - type t = H.t - let equal = H.equal - let hash (seed: int) x = H.hash x - end) + struct + include MakeSeeded(struct + type t = H.t + let equal = H.equal + let hash (seed: int) x = H.hash x + end) + let create sz = create ~seed:0 sz + end diff --git a/stdlib/hashtbl.mli b/stdlib/hashtbl.mli index 6fb3c1029..6dcfdafcf 100644 --- a/stdlib/hashtbl.mli +++ b/stdlib/hashtbl.mli @@ -155,7 +155,7 @@ module type S = sig type key type 'a t - val create : ?seed:int -> int -> 'a t + val create : int -> 'a t val clear : 'a t -> unit val copy : 'a t -> 'a t val add : 'a t -> key -> 'a -> unit @@ -179,8 +179,7 @@ module Make (H : HashedType) : S with type key = H.t The operations perform similarly to those of the generic interface, but use the hashing and equality functions specified in the functor argument [H] instead of generic - equality and hashing. The optional [seed] argument to the - [create] function is ignored. *) + equality and hashing. *) module type SeededHashedType = sig @@ -198,7 +197,28 @@ module type SeededHashedType = (** The input signature of the functor {!Hashtbl.MakeSeeded}. @since 3.13.0 *) -module MakeSeeded (H : SeededHashedType) : S with type key = H.t +module type SeededS = + sig + type key + type 'a t + val create : ?seed:int -> int -> 'a t + val clear : 'a t -> unit + val copy : 'a t -> 'a t + val add : 'a t -> key -> 'a -> unit + val remove : 'a t -> key -> unit + val find : 'a t -> key -> 'a + val find_all : 'a t -> key -> 'a list + val replace : 'a t -> key -> 'a -> unit + val mem : 'a t -> key -> bool + val iter : (key -> 'a -> unit) -> 'a t -> unit + val fold : (key -> 'a -> 'b -> 'b) -> 'a t -> 'b -> 'b + val length : 'a t -> int + val stats: 'a t -> statistics + end +(** The output signature of the functor {!Hashtbl.MakeSeeded}. + @since 3.13.0 *) + +module MakeSeeded (H : SeededHashedType) : SeededS with type key = H.t (** Functor building an implementation of the hashtable structure. The functor [Hashtbl.MakeSeeded] returns a structure containing a type [key] of keys and a type ['a t] of hash tables @@ -206,8 +226,7 @@ module MakeSeeded (H : SeededHashedType) : S with type key = H.t The operations perform similarly to those of the generic interface, but use the seeded hashing and equality functions specified in the functor argument [H] instead of generic - equality and hashing. The optional [seed] argument to the [create] - function is honored. + equality and hashing. @since 3.13.0 *) diff --git a/stdlib/moreLabels.mli b/stdlib/moreLabels.mli index accf7f3e8..c2691cba5 100644 --- a/stdlib/moreLabels.mli +++ b/stdlib/moreLabels.mli @@ -44,6 +44,26 @@ module Hashtbl : sig module type HashedType = Hashtbl.HashedType module type SeededHashedType = Hashtbl.SeededHashedType module type S = + sig + type key + and 'a t + val create : int -> 'a t + val clear : 'a t -> unit + val copy : 'a t -> 'a t + val add : 'a t -> key:key -> data:'a -> unit + val remove : 'a t -> key -> unit + val find : 'a t -> key -> 'a + val find_all : 'a t -> key -> 'a list + val replace : 'a t -> key:key -> data:'a -> unit + val mem : 'a t -> key -> bool + val iter : f:(key:key -> data:'a -> unit) -> 'a t -> unit + val fold : + f:(key:key -> data:'a -> 'b -> 'b) -> + 'a t -> init:'b -> 'b + val length : 'a t -> int + val stats: 'a t -> statistics + end + module type SeededS = sig type key and 'a t @@ -64,7 +84,7 @@ module Hashtbl : sig val stats: 'a t -> statistics end module Make : functor (H : HashedType) -> S with type key = H.t - module MakeSeeded (H : SeededHashedType) : S with type key = H.t + module MakeSeeded (H : SeededHashedType) : SeededS with type key = H.t val hash : 'a -> int val seeded_hash : int -> 'a -> int val hash_param : int -> int -> 'a -> int