Compound PartialEq

This commit is contained in:
Tony Garnock-Jones 2022-10-31 21:36:05 +01:00
parent bb100cc0dd
commit 7225960363
1 changed files with 32 additions and 1 deletions

View File

@ -173,6 +173,21 @@ impl<'a, D: Domain> Hash for dyn Value<D> + 'a {
}
}
fn iters_eq<'a, D: Domain>(
mut i1: Box<dyn Iterator<Item = &dyn Value<D>> + 'a>,
mut i2: Box<dyn Iterator<Item = &dyn Value<D>> + 'a>,
) -> bool {
loop {
match i1.next() {
None => return i2.next().is_none(),
Some(v1) => match i2.next() {
None => return false,
Some(v2) => if v1 != v2 { return false; },
}
}
}
}
impl<'a, D: Domain> PartialEq for dyn Value<D> + 'a {
fn eq(&self, other: &Self) -> bool {
let cls = self.value_class();
@ -195,7 +210,23 @@ impl<'a, D: Domain> PartialEq for dyn Value<D> + 'a {
self.as_symbol().unwrap() == other.as_symbol().unwrap(),
}
ValueClass::Compound(c) => match c {
_ => todo!(),
CompoundClass::Record => {
if self.label().unwrap() != other.label().unwrap() { return false; }
iters_eq(self.iter().unwrap(), other.iter().unwrap())
}
CompoundClass::Sequence => {
iters_eq(self.iter().unwrap(), other.iter().unwrap())
}
CompoundClass::Set => {
let s1 = self.iter().unwrap().collect::<Set<_>>();
let s2 = other.iter().unwrap().collect::<Set<_>>();
s1 == s2
}
CompoundClass::Dictionary => {
let d1 = self.entries().unwrap().collect::<Map<_, _>>();
let d2 = other.entries().unwrap().collect::<Map<_, _>>();
d1 == d2
}
}
ValueClass::Embedded => self.embedded().unwrap() == other.embedded().unwrap(),
}