X-Git-Url: http://git.nguyen.vg/gitweb/?a=blobdiff_plain;f=hcons.ml;h=7bc8823b2dfd5020296a0a4e520f5db96c3d406c;hb=861944c24f8cad360fb9478cb0a15863cb52e803;hp=9226842713da94adf7be1e0afdc8058a78ef5de1;hpb=9f227961ae2219728e4cdd56e4d4c4e7165e4306;p=SXSI%2Fxpathcomp.git diff --git a/hcons.ml b/hcons.ml index 9226842..7bc8823 100644 --- a/hcons.ml +++ b/hcons.ml @@ -8,10 +8,13 @@ module type SA = val hash : t -> int val uid : t -> Uid.t val equal : t -> t -> bool - end + + val with_id : Uid.t -> t + end module type S = sig + type data type t = private { id : Uid.t; key : int; @@ -22,10 +25,13 @@ module type S = val uid : t -> Uid.t val equal : t -> t -> bool + + val with_id : Uid.t -> t end module Make (H : Hashtbl.HashedType) : S with type data = H.t = struct + let uid_make = Uid.make_maker() type data = H.t type t = { id : Uid.t; key : int; @@ -38,10 +44,22 @@ struct type _t = t type t = _t let hash = hash - let equal a b = a==b || H.equal a.node b.node + let equal a b = a == b || H.equal a.node b.node end) let pool = WH.create MED_H_SIZE let make x = - let cell = { id = Uid.make(); key = H.hash x; node = x } in + let cell = { id = uid_make(); key = H.hash x; node = x } in WH.merge pool cell + + exception Found of t + + let with_id id = + try + WH.iter (fun r -> if r.id == id then raise (Found r)) pool; + raise Not_found + with + | Found r -> r + | e -> raise e + ;; + end