Rewrite minimum_propagation algorithm in a functional way

Reviewed By: mbouaziz

Differential Revision: D7380975

fbshipit-source-id: 6052f78
master
Ezgi Çiçek 7 years ago committed by Facebook Github Bot
parent 9211388c32
commit 1cef32eaf7

@ -347,35 +347,37 @@ return the addends of the sum x_j1+x_j2+..+x_j_n*)
match plus_node with Plus (_ :: _ :: _) -> true | _ -> false match plus_node with Plus (_ :: _ :: _) -> true | _ -> false
(* TO DO: rewrite in a functional way rather than imperative T26418766 *)
let rec minimum_propagation (q: int) (visited: Int.Set.t) (constraints: Exp.t list) = let rec minimum_propagation (q: int) (visited: Int.Set.t) (constraints: Exp.t list) =
let node = ref (Min []) in let rec build_min node branch visited_acc worklist =
let worklist : int Stack.t = Stack.create () in match worklist with
Stack.push worklist q ; | [] ->
let branch = ref [] in (node, branch, visited_acc)
let visited_acc = ref visited in | k :: rest ->
while not (Stack.is_empty worklist) do if Int.Set.mem visited_acc k then build_min node branch visited_acc rest
let k = else
match Stack.pop worklist with Some k' -> k' | None -> assert false let visited_acc = Int.Set.add visited_acc k in
(* we cant be here *) let node = add_leaf node k (BoundMap.upperbound k) in
in
if Int.Set.mem !visited_acc k then ()
else (
visited_acc := Int.Set.add !visited_acc k ;
node := add_leaf !node k (BoundMap.upperbound k) ;
let k_constraints_upperbound = get_k_single_constraints constraints k in let k_constraints_upperbound = get_k_single_constraints constraints k in
List.iter let worklist =
~f:(fun ub_id -> if not (Int.Set.mem !visited_acc ub_id) then Stack.push worklist ub_id) List.filter
k_constraints_upperbound ; ~f:(fun ub_id -> not (Int.Set.mem visited_acc ub_id))
k_constraints_upperbound
|> List.rev_append worklist
in
let k_sum_constraints = get_k_sum_constraints constraints k in let k_sum_constraints = get_k_sum_constraints constraints k in
List.iter let branch =
~f:(fun set_addend -> List.fold_left
if Int.Set.is_empty (Int.Set.inter set_addend !visited_acc) then ~f:(fun branch set_addend ->
branch := add_without_rep set_addend !branch ) if Int.Set.is_empty (Int.Set.inter set_addend visited_acc) then
k_sum_constraints ) add_without_rep set_addend branch
done ; else branch )
List.iter ~init:branch k_sum_constraints
~f:(fun addend -> in
build_min node branch visited_acc worklist
in
let node, branch, visited_res = build_min (Min []) [] visited [q] in
List.fold_left
~f:(fun i_node addend ->
if Int.Set.length addend < 2 then assert false if Int.Set.length addend < 2 then assert false
else ( else (
L.(debug Analysis Medium) "@\n\n|Set addends| = %i {" (Int.Set.length addend) ; L.(debug Analysis Medium) "@\n\n|Set addends| = %i {" (Int.Set.length addend) ;
@ -384,14 +386,13 @@ return the addends of the sum x_j1+x_j2+..+x_j_n*)
let plus_node = let plus_node =
Set.fold Set.fold
~f:(fun acc n -> ~f:(fun acc n ->
let child = minimum_propagation n !visited_acc constraints in let child = minimum_propagation n visited_res constraints in
add_child acc child ) add_child acc child )
~init:(Plus []) addend ~init:(Plus []) addend
in in
(* without this check it would add plus node with just one child, and give wrong results *) (* without this check it would add plus node with just one child, and give wrong results *)
if is_well_formed_plus_node plus_node then node := add_child !node plus_node ) if is_well_formed_plus_node plus_node then add_child i_node plus_node else i_node )
!branch ; ~init:node branch
!node
let compute_trees_from_contraints cfg constraints = let compute_trees_from_contraints cfg constraints =

Loading…
Cancel
Save