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

  val value: t -> value
end

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

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

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

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

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

34
  let mk x =
35
    try Tbl.find !cache x
36 37 38
    with Not_found ->
      let n = !counter in
      incr counter;
39
      Tbl.add !cache x n;
40 41 42 43 44 45 46 47 48 49 50 51 52
      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
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 78

  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
      
    
79 80
end

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 139
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
140 141 142 143 144 145 146 147 148 149

module Weak(H : Custom.T) = struct
  type value = H.t
  module P = Weak.Make(H)
  let pool = P.create 17

  include H
  let mk = P.merge pool
  let value x = x
end
150
*)