def _create_smart_compare(tt: TourneyTree, elements: [int]) -> SmartCompare: sc = SmartCompare() for element, pos in _get_element_positions(tt, elements).items(): parent = tt.get_parent(pos) if not parent: continue sc.set_greater_than(tt[pos], tt[parent]) return sc
def _get_element_positions(tt: TourneyTree, elements: [int]) -> { int: Position }: elements_left = set(elements) element_positions = dict() for row in range(tt.height - 1, -1, -1): for pos in tt.iter_row(row): parent = tt.get_parent(pos) if tt[pos] not in elements_left: continue element_positions[tt[pos]] = pos elements_left.remove(tt[pos]) if len(elements_left) == 0: return element_positions return element_positions