aboutsummaryrefslogtreecommitdiffstats
path: root/mppa_k1c/abstractbb/Impure/ocaml/ImpHConsOracles.ml
blob: b7a80679d9899964caec37bae7296aa59cca3fed (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
open ImpPrelude

exception Stop;;

let make_dict (type key) (p: key Dict.hash_params) =
  let module MyHashedType = struct
    type t = key
    let equal = p.Dict.test_eq 
    let hash = p.Dict.hashing 
  end in
  let module MyHashtbl = Hashtbl.Make(MyHashedType) in
  let dict = MyHashtbl.create 1000 in
  {
    Dict.set = (fun (k,d) -> MyHashtbl.replace dict k d);
    Dict.get = (fun k -> MyHashtbl.find_opt dict k)
  }


let xhCons (type a) (hash_eq, error: (a -> a -> bool)*(a pre_hashV -> a hashV)) =
  let module MyHashedType = struct
    type t = a pre_hashV
    let equal x y = hash_eq x.pre_data y.pre_data
    let hash x = Hashtbl.hash x.hcodes 
  end in
  let module MyHashtbl = Hashtbl.Make(MyHashedType) in
  let pick t =
    let res = ref None in
    try
      MyHashtbl.iter (fun k d -> res:=Some (k,d); raise Stop) t;
      None
    with
    | Stop -> !res  
    in
  let t = MyHashtbl.create 1000 in
  let logs = ref [] in
  {
   hC = (fun (x:a pre_hashV) ->
     match MyHashtbl.find_opt t x with
     | Some x' -> x'
     | None -> (*print_string "+";*)
        let x' = { data = x.pre_data ;
                   hid = MyHashtbl.length t }
        in MyHashtbl.add t x x'; x');
   hC_known = (fun (x:a pre_hashV) ->
     match MyHashtbl.find_opt t x with
     | Some x' -> x'
     | None -> error x);
   next_log = (fun info -> logs := (MyHashtbl.length t, info)::(!logs));
   export = fun () ->
     match pick t with
     | None -> { get_hashV = (fun _ -> raise Not_found);  iterall = (fun _ -> ()) } 
     | Some (k,_) ->
        (* the state is fully copied at export ! *)
        let logs = ref (List.rev_append (!logs) []) in
        let rec step_log i =
          match !logs with
          | (j, info)::l' when i>=j -> logs:=l'; info::(step_log i)
          | _ -> []
        in let a = Array.make (MyHashtbl.length t) k in
           MyHashtbl.iter (fun k d -> a.(d.hid) <- k) t;
           {
             get_hashV = (fun i -> a.(i));
             iterall = (fun iter_node -> Array.iteri (fun i k -> iter_node (step_log i) i k) a)
           }    
  }