Skip to content

Commit

Permalink
Common up find_unique
Browse files Browse the repository at this point in the history
  • Loading branch information
acl-cqc committed Apr 26, 2024
1 parent 4e875b3 commit 7819211
Showing 1 changed file with 11 additions and 18 deletions.
29 changes: 11 additions & 18 deletions hugr/src/algorithm/merge_bbs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -302,12 +302,9 @@ mod test {
HashSet::from([expected_backedge_target, exit])
);
// And the Noop in the entry block is consumed by the custom Test op
let tst = h
.nodes()
.filter(|n| matches!(h.get_optype(*n), OpType::CustomOp(_)))
.exactly_one()
.ok()
.unwrap();
let tst = find_unique(h.nodes(), |n| {
matches!(h.get_optype(*n), OpType::CustomOp(_))
});
assert_eq!(h.get_parent(tst), Some(entry));
assert_eq!(
h.output_neighbours(entry_nop).collect::<Vec<_>>(),
Expand Down Expand Up @@ -380,20 +377,12 @@ mod test {

// Should only be one BB left
let [bb, _exit] = h.children(h.root()).collect::<Vec<_>>().try_into().unwrap();
let tst = h
.nodes()
.filter(|n| matches!(h.get_optype(*n), OpType::CustomOp(_)))
.exactly_one()
.ok()
.unwrap();
let tst = find_unique(h.nodes(), |n| {
matches!(h.get_optype(*n), OpType::CustomOp(_))
});
assert_eq!(h.get_parent(tst), Some(bb));

let inp = h
.nodes()
.filter(|n| matches!(h.get_optype(*n), OpType::Input(_)))
.exactly_one()
.ok()
.unwrap();
let inp = find_unique(h.nodes(), |n| matches!(h.get_optype(*n), OpType::Input(_)));
let mut tst_inputs = h.input_neighbours(tst).collect::<Vec<_>>();
tst_inputs.remove(tst_inputs.iter().find_position(|n| **n == inp).unwrap().0);
let [other_input] = tst_inputs.try_into().unwrap();
Expand All @@ -403,4 +392,8 @@ mod test {
);
Ok(())
}

fn find_unique<T>(items: impl Iterator<Item = T>, pred: impl Fn(&T) -> bool) -> T {
items.filter(pred).exactly_one().ok().unwrap()
}
}

0 comments on commit 7819211

Please sign in to comment.