pool.ml 3.08 KB
Newer Older
1
2
module type T = 
sig
3
  include Custom.T
4
5
6
7
8
9
10
11
12
13
  type value
    
  val clear: unit -> unit  
  val mk: value -> t
  val dummy_min: t
  val dummy_max: t

  val value: t -> value
end

14
module Make(H : Custom.T) =
15
struct
16
  include Custom.Int
17
18
  type value = H.t

19
20
21
  module Tbl = Hashtbl.Make(H)

  let cache    = State.ref "Pool.cache" (Tbl.create 63)
22
23
  let values   = State.ref "Pool.values" (Array.create 63 None)
  let counter  = State.ref "Pool.counter" 0
24
25

  let clear () =
26
    Tbl.clear !cache;
27
28
29
    values := Array.create 63 None;
    counter := 0

30
31
32
  let check i = 
    assert((i >= 0) && (i < !counter) && (!values.(i) <> None))

33
  let mk x =
34
    try Tbl.find !cache x
35
36
37
    with Not_found ->
      let n = !counter in
      incr counter;
38
      Tbl.add !cache x n;
39
40
41
42
43
44
45
46
47
48
49
50
51
      if (n = Array.length !values) then
	( 
	  let new_values = Array.create (2 * Array.length !values) None in
	  Array.blit !values 0 new_values 0 n;
	  values := new_values
	);
      !values.(n) <- Some x;
      n

  let dummy_min = -1
  let dummy_max = max_int

  let value n = match !values.(n) with Some x -> x | None -> assert false
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77

  let memo = 
    Serialize.Put.mk_property (fun t -> Array.create !counter false)

  let serialize t i =
    let memo = Serialize.Put.get_property memo t in
    Serialize.Put.int t i;
    if not memo.(i) then (
      H.serialize t (value i);
      memo.(i) <- true
    )

  (* Use an array here ? *)
  module DMemo = Map.Make(Custom.Int)
  let memo = Serialize.Get.mk_property (fun t -> ref DMemo.empty)

  let deserialize t =
    let memo = Serialize.Get.get_property memo t in
    let i = Serialize.Get.int t in
    try DMemo.find i !memo
    with Not_found ->
      let j = mk (H.deserialize t) in
      memo := DMemo.add i j !memo;
      j
      
    
78
79
end

80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
module NoHash(H : Custom.T) =
struct
  include Custom.Int
  type value = H.t


  let values   = State.ref "Pool.values" (Array.create 63 None)
  let counter  = State.ref "Pool.counter" 0

  let clear () =
    values := Array.create 63 None;
    counter := 0

  let check i = 
    assert((i >= 0) && (i < !counter) && (!values.(i) <> None))

  let mk x =
    let n = !counter in
    incr counter;
    if (n = Array.length !values) then
      ( 
	let new_values = Array.create (2 * Array.length !values) None in
	Array.blit !values 0 new_values 0 n;
	values := new_values
      );
    !values.(n) <- Some x;
    n

  let dummy_min = -1
  let dummy_max = max_int

  let value n = match !values.(n) with Some x -> x | None -> assert false

  let memo = 
    Serialize.Put.mk_property (fun t -> Array.create !counter false)

  let serialize t i =
    let memo = Serialize.Put.get_property memo t in
    Serialize.Put.int t i;
    if not memo.(i) then (
      H.serialize t (value i);
      memo.(i) <- true
    )

  (* Use an array here ? *)
  module DMemo = Map.Make(Custom.Int)
  let memo = Serialize.Get.mk_property (fun t -> ref DMemo.empty)

  let deserialize t =
    let memo = Serialize.Get.get_property memo t in
    let i = Serialize.Get.int t in
    try DMemo.find i !memo
    with Not_found ->
      let j = mk (H.deserialize t) in
      memo := DMemo.add i j !memo;
      j
      
    
end