use std::collections::BTreeSet;
use super::{Section, Side};
use crate::verdict::Verdict;
type Place = (usize, String, usize, bool, usize);
impl Side {
fn placed(&self) -> BTreeSet<Place> {
let mut out = BTreeSet::new();
for (s, groups) in &self.sections {
let section = Section::ALL.iter().position(|x| x == s).unwrap_or_default();
for g in groups {
for (n, list) in g.lists.iter().chain(g.bare.iter().flatten()).enumerate() {
for (neg, ms) in [(false, &list.pos), (true, &list.neg)] {
out.extend(ms.iter().map(|m| (section, g.id.clone(), n, neg, *m)));
}
}
}
}
out
}
pub(super) fn differing(&self, new: &Self) -> Option<BTreeSet<usize>> {
if self.default != new.default {
return None;
}
let (old_places, new_places) = (self.placed(), new.placed());
let groups = |places: &BTreeSet<Place>| -> BTreeSet<(usize, String)> {
places.iter().map(|(s, id, ..)| (*s, id.clone())).collect()
};
let (old_groups, new_groups) = (groups(&old_places), groups(&new_places));
let holds = |section: usize| match Section::ALL.get(section) {
Some(Section::Deny) => true,
Some(Section::Review) => self.default <= Verdict::Review,
_ => false,
};
let removed = old_places
.difference(&new_places)
.filter(|(s, id, _, neg, _)| {
!(*neg && holds(*s) && new_groups.contains(&(*s, id.clone())))
});
let added = new_places
.difference(&old_places)
.filter(|(s, id, _, neg, _)| {
!(holds(*s) && (!*neg || !old_groups.contains(&(*s, id.clone()))))
});
Some(removed.chain(added).map(|(.., m)| *m).collect())
}
}