You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Original bug ID: 1017 Reporter: administrator Status: closed (set by @xavierleroy on 2013-08-31T10:44:32Z) Resolution: won't fix Priority: normal Severity: feature Category: ~DO NOT USE (was: OCaml general)
Bug description
Bonjour,
les nouvelles weak hash table semblent idéales pour faire du hash-consing;
je pense qu'une implementation comme ci-dessous pourrait être un ajout
intéressant à la bibliothèque standard (il faudrait peut-être le rendre
thread-safe et detecter les débordements du compteur interne, enfin, il
y a de la marge).
-- Alain
module type HashConsType =
sig
type data
type t
val cons: data -> t
val data: t -> data
val compare: t -> t -> int
val hash: t -> int
val equal: t -> t -> bool
end
(* Implements Hashtbl.HashedType and Set.SortedType interfaces,
with all operations in O(1).
equal t1 t2 iff H.equal (data t1) (data t2)
compare and hash are compatible with equal
*)
module HashCons(H : Hashtbl.HashedType) : HashConsType with type data = H.t =
struct
module Stamp = struct
type t = H.t * int
let equal (h1,) (h2,) = H.equal h1 h2
let hash (h,_) = H.hash h
end
module WH = Weak.Make(Stamp)
type data = H.t
type t = Stamp.t
let count = ref 0
let table = WH.create 5
let data (x,_) = x
(* Not thread safe ! *)
let cons x =
let c = (x,!count) in
let d = WH.merge table c in
if (c == d) then incr count;
d
let equal ((,i) : t) ((,j) : t) = i = j
let hash ((,i) : t) = i
let compare ((,i) : t) ((_,j) : t) = compare i j
end
The text was updated successfully, but these errors were encountered:
Original bug ID: 1017
Reporter: administrator
Status: closed (set by @xavierleroy on 2013-08-31T10:44:32Z)
Resolution: won't fix
Priority: normal
Severity: feature
Category: ~DO NOT USE (was: OCaml general)
Bug description
Bonjour,
les nouvelles weak hash table semblent idéales pour faire du hash-consing;
je pense qu'une implementation comme ci-dessous pourrait être un ajout
intéressant à la bibliothèque standard (il faudrait peut-être le rendre
thread-safe et detecter les débordements du compteur interne, enfin, il
y a de la marge).
-- Alain
module type HashConsType =
sig
type data
type t
val cons: data -> t
val data: t -> data
val compare: t -> t -> int
val hash: t -> int
val equal: t -> t -> bool
end
(* Implements Hashtbl.HashedType and Set.SortedType interfaces,
with all operations in O(1).
*)
module HashCons(H : Hashtbl.HashedType) : HashConsType with type data = H.t =
struct
module Stamp = struct
type t = H.t * int
let equal (h1,) (h2,) = H.equal h1 h2
let hash (h,_) = H.hash h
end
module WH = Weak.Make(Stamp)
type data = H.t
type t = Stamp.t
let count = ref 0
let table = WH.create 5
let data (x,_) = x
(* Not thread safe ! *)
let cons x =
let c = (x,!count) in
let d = WH.merge table c in
if (c == d) then incr count;
d
let equal ((,i) : t) ((,j) : t) = i = j
let hash ((,i) : t) = i
let compare ((,i) : t) ((_,j) : t) = compare i j
end
The text was updated successfully, but these errors were encountered: