pep508: some simplification in 'and' and 'or'

Basically, and'ing or or'ing the same expression can be entire
skipped. And we try harder to avoid singleton conjunctions or
disjunctions, as these are considered unequal otherwise. (Thus
defeating our attempts to avoid and'ing or or'ing a superfluous
marker.)
This commit is contained in:
Andrew Gallant 2024-07-17 13:49:46 -04:00 committed by Andrew Gallant
parent 8b8f34ac21
commit 7fce59e4bc

View file

@ -1105,6 +1105,9 @@ impl MarkerTree {
/// already, then `tree` is added to it instead of creating a new
/// conjunction.
pub fn and(&mut self, tree: MarkerTree) {
if self == &tree {
return;
}
match *self {
MarkerTree::Expression(_) | MarkerTree::Or(_) => {
let this = std::mem::replace(self, MarkerTree::And(vec![]));
@ -1118,6 +1121,9 @@ impl MarkerTree {
} else {
exprs.push(tree);
}
if exprs.len() == 1 {
*self = exprs.pop().unwrap();
}
}
}
@ -1127,6 +1133,9 @@ impl MarkerTree {
/// already, then `tree` is added to it instead of creating a new
/// disjunction.
pub fn or(&mut self, tree: MarkerTree) {
if self == &tree {
return;
}
match *self {
MarkerTree::Expression(_) | MarkerTree::And(_) => {
let this = std::mem::replace(self, MarkerTree::And(vec![]));
@ -1140,6 +1149,9 @@ impl MarkerTree {
} else {
exprs.push(tree);
}
if exprs.len() == 1 {
*self = exprs.pop().unwrap();
}
}
}