Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 82 lines (62 sloc) 1.941 kB
fccc685 Initial open-source release
MLstate authored
1 (*
2 Copyright © 2011 MLstate
3
4 This file is part of OPA.
5
6 OPA is free software: you can redistribute it and/or modify it under the
7 terms of the GNU Affero General Public License, version 3, as published by
8 the Free Software Foundation.
9
10 OPA is distributed in the hope that it will be useful, but WITHOUT ANY
11 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
12 FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for
13 more details.
14
15 You should have received a copy of the GNU Affero General Public License
16 along with OPA. If not, see <http://www.gnu.org/licenses/>.
17 *)
18 (* http://remi.vanicat.free.fr/ocaml/hweak/ *)
19 (* Perhaps we can replace this module by (modified?) above hweak
20 implementation... ? *)
21
22 module type WeakedType = sig
23 type t
24 val compare : t -> t -> int
25 val hash : t -> int
26 end
27
28 module Make (HK : Hashtbl.HashedType)(HD : WeakedType) = struct
29 module WS = Weak.Make
30 (struct
31 include HD
32 let equal a b = HD.compare a b = 0
33 end)
34
35 module HT = Hashtbl.Make(HK)
36
37 type key = HK.t
38
39 module S = Set.Make(HD)
40
41 type t = {
42 htbl : HD.t HT.t;
43 wset : WS.t;
44 mutable strong : S.t;
45 }
46
47 let create i = {
48 htbl = HT.create i;
49 wset = WS.create i;
50 strong = S.empty;
51 }
52
53 let remove t k =
54 try
55 let v = HT.find t.htbl k in
56 WS.remove t.wset v;
57 HT.remove t.htbl k;
58 t.strong <- S.remove v t.strong
59 with Not_found -> ()
60
61 let add t k v =
62 let vcopy = Obj.obj (Obj.dup (Obj.repr v)) in
63 HT.add t.htbl k vcopy;
64 WS.add t.wset v;
65 Gc.finalise (fun _ -> HT.remove t.htbl (k:HK.t)) v
66
67 let mem t k = (HT.mem t.htbl k)
68
69 let find t k = WS.find t.wset (HT.find t.htbl k)
70
71 let strong t k v =
72 if not (mem t k) then add t k v;
73 t.strong <- S.add v t.strong
74
75 let relax t k =
76 t.strong <- S.remove (find t k) t.strong
77
c58bfda @BourgerieQuentin [feature] session: Add some stats
BourgerieQuentin authored
78 let size t =
79 HT.length t.htbl
fccc685 Initial open-source release
MLstate authored
80
81 end
Something went wrong with that request. Please try again.