From 72befa1ec86ab21e02a682920c231edfc3375836 Mon Sep 17 00:00:00 2001 From: alexcere <48130030+alexcere@users.noreply.github.com> Date: Mon, 23 Dec 2024 19:36:01 +0100 Subject: [PATCH] #18 Store deepest element that must not be moved further --- src/greedy/greedy_new_version.py | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/src/greedy/greedy_new_version.py b/src/greedy/greedy_new_version.py index f37fd82a..641cc8d5 100644 --- a/src/greedy/greedy_new_version.py +++ b/src/greedy/greedy_new_version.py @@ -116,8 +116,14 @@ def __init__(self, initial_stack: List[var_id_T], dependency_graph: nx.DiGraph, for i, (ini_var, fin_var) in enumerate(zip(reversed(initial_stack), reversed(final_stack))) if ini_var == fin_var} + + # We store instead the elements not solved, as these are the ones we need to study self.not_solved = set(range(len(self.final_stack))).difference(solved) + # Maximum element that is sorted (i.e. elements from that point on must not be modified + # in the current stack) + self.max_solved = max(self.not_solved) + 1 + # Number of times each variable is computed in the stack self.n_computed: Counter = Counter(initial_stack) @@ -136,10 +142,18 @@ def _remove_solved(self, idx: fstack_pos_T): def _add_solved(self, idx: fstack_pos_T): """ - Annotates that the idx is solved + Annotates that the idx is solved, updating the maximum value if necessary """ try: self.not_solved.remove(idx) + + # TODO: use here an ordered set that preserves order when inserting and removing elements + if idx + 1 == self.max_solved: + if len(self.not_solved) == 0: + self.max_solved = 0 + else: + self.max_solved = max(self.not_solved) + 1 + except KeyError: pass @@ -1102,6 +1116,7 @@ def debug_loop(self, dep_graph, optg: List[instr_id_T], self._logger.debug(f"Ops not computed {list(dep_graph.nodes)}") self._logger.debug(f"Ops computed: {optg}") self._logger.debug(cstate) + self._logger.debug(cstate.max_solved) self._logger.debug("") def debug_pop(self, var_top: var_id_T, cstate: SymbolicState):