Skip to content

Commit

Permalink
expr.ml: ExprHashtbl
Browse files Browse the repository at this point in the history
  • Loading branch information
monadius committed Oct 11, 2020
1 parent 7e0fd0b commit 80c29e6
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 7 deletions.
20 changes: 14 additions & 6 deletions expr.ml
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,14 @@ let rec eq_expr e1 e2 =
Lib.itlist (fun (a1, a2) x -> eq_expr a1 a2 && x) (Lib.zip as1 as2) true
| _ -> false

module ExprHashtbl = Hashtbl.Make (
struct
type t = expr
let equal e1 e2 = eq_expr e1 e2
(* TODO: is it possible that eq_expr e1 e2 = true but hashes are different? *)
let hash e = Hashtbl.hash e
end)

let rec vars_in_expr e =
match e with
| Var v -> [v]
Expand Down Expand Up @@ -197,12 +205,12 @@ let index_of_ref_var = function
(* Finds common subexpressions and returns a list of expressions
with references *)
let expr_ref_list_of_expr ex =
let hc = Hashtbl.create 128 in
let hi = Hashtbl.create 128 in
let get_count ex = try Hashtbl.find hc ex with Not_found -> 0 in
let incr_count ex = Hashtbl.replace hc ex (1 + get_count ex) in
let get_index ex = try Hashtbl.find hi ex with Not_found -> -1 in
let set_index ex i = Hashtbl.add hi ex i in
let hc = ExprHashtbl.create 128 in
let hi = ExprHashtbl.create 128 in
let get_count ex = try ExprHashtbl.find hc ex with Not_found -> 0 in
let incr_count ex = ExprHashtbl.replace hc ex (1 + get_count ex) in
let get_index ex = try ExprHashtbl.find hi ex with Not_found -> -1 in
let set_index ex i = ExprHashtbl.add hi ex i in
let rec count ex =
incr_count ex;
match ex with
Expand Down
2 changes: 2 additions & 0 deletions expr.mli
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,8 @@ val gen_op_name : gen_op_type -> string

val eq_expr : expr -> expr -> bool

module ExprHashtbl : Hashtbl.S with type key = expr

val vars_in_expr : expr -> string list

val is_ref_var : expr -> bool
Expand Down
2 changes: 1 addition & 1 deletion opt_bb_eval.ml
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,8 @@ let rec eval_expr'_list refs vars i = function

let min_max_expr (pars : Opt_common.opt_pars) max_only (cs : constraints) e =
(* ExprOut.(
let es = expr_ref_list_of_expr e in
Log.report `Main "Testing: %s" (Info.print_str e);
let es = expr_ref_list_of_expr e in
es |> List.iteri (fun i e -> Log.report `Main "%d: %s" i (Info.print_str e));
Log.report `Main "---"); *)
if Config.debug () then
Expand Down

0 comments on commit 80c29e6

Please sign in to comment.