From 929914e66a7d3fa35630b40b0ddc2346cbc882a1 Mon Sep 17 00:00:00 2001 From: Cookiebowser <lucas.doering@live.de> Date: Mon, 28 Mar 2022 17:46:18 +0200 Subject: [PATCH] lazy_op progress --- btypes_lazy/src/main/rust/btypes/src/bset.rs | 12 +++ .../btypes/src/lazy_ops/set_ops/setops.rs | 82 ++++++++++++++++--- 2 files changed, 82 insertions(+), 12 deletions(-) diff --git a/btypes_lazy/src/main/rust/btypes/src/bset.rs b/btypes_lazy/src/main/rust/btypes/src/bset.rs index b374eb142..5d7771a78 100644 --- a/btypes_lazy/src/main/rust/btypes/src/bset.rs +++ b/btypes_lazy/src/main/rust/btypes/src/bset.rs @@ -37,6 +37,10 @@ pub struct BSet<T: BObject> { transformation: Option<Box<dyn SetOp<Item = T>>>, } +impl<T: BObject> BSet<T> { + const OP_NAME: &'static str = "set"; +} + impl<T: BObject> SetOp for BSet<T> { type Item = T; @@ -50,6 +54,14 @@ impl<T: BObject> SetOp for BSet<T> { fn clone_box(&self) -> Box<dyn SetOp<Item=Self::Item>> { todo!() } + + fn get_op_name(&self) -> &str { + return BSet::<T>::OP_NAME; + } + + fn get_rhs(&self) -> Option<&Box<dyn SetOp<Item=Self::Item>>> { + return self.transformation.as_ref(); + } } impl<T: BObject> SetOpTraits for BSet<T> { diff --git a/btypes_lazy/src/main/rust/btypes/src/lazy_ops/set_ops/setops.rs b/btypes_lazy/src/main/rust/btypes/src/lazy_ops/set_ops/setops.rs index d72d55426..787ae5c0b 100644 --- a/btypes_lazy/src/main/rust/btypes/src/lazy_ops/set_ops/setops.rs +++ b/btypes_lazy/src/main/rust/btypes/src/lazy_ops/set_ops/setops.rs @@ -11,17 +11,63 @@ pub trait SetOp: SetOpTraits + Debug { fn compute(&self, lhs: &BSet<Self::Item>) -> BSet<Self::Item>; fn clone_box(&self) -> Box<dyn SetOp<Item = Self::Item>>; + fn get_op_name(&self) -> &str; + fn get_rhs(&self) -> Option<&Box<dyn SetOp<Item = Self::Item>>>; + //PartialEq - fn eq(&self, other: &Box<dyn SetOp<Item = Self::Item>>) -> bool; + fn eq_box(&self, other: &Box<dyn SetOp<Item = Self::Item>>) -> bool { + self.get_op_name().eq(other.get_op_name()) && self.get_rhs().eq(&other.get_rhs()) + } //PartialOrd - fn partial_cmp(&self, other: &Box<dyn SetOp<Item = Self::Item>>) -> Option<Ordering>; + fn partial_cmp_box(&self, other: &Box<dyn SetOp<Item = Self::Item>>) -> Option<Ordering> { + let mut result = self.get_op_name().partial_cmp(other.get_op_name()); + if result.is_some() { + if result.clone().unwrap().eq(&Ordering::Equal) { + result = self.get_rhs().partial_cmp(&other.get_rhs()); + } + } + return result; + } //Ord - fn cmp(&self, other: &Box<dyn SetOp<Item = Self::Item>>) -> Ordering; - fn max(&self, other: &Box<dyn SetOp<Item = Self::Item>>) -> Box<dyn SetOp<Item = Self::Item>>; - fn min(&self, other: &Box<dyn SetOp<Item = Self::Item>>) -> Box<dyn SetOp<Item = Self::Item>>; - fn clamp(&self, min: &Box<dyn SetOp<Item = Self::Item>>, max: &Box<dyn SetOp<Item = Self::Item>>) -> Box<dyn SetOp<Item = Self::Item>>; + fn cmp_box(&self, other: &Box<dyn SetOp<Item = Self::Item>>) -> Ordering { + let mut result = self.get_op_name().cmp(other.get_op_name()); + if result.eq(&Ordering::Equal) { + result = self.get_rhs().cmp(&other.get_rhs()); + } + return result; + } + fn lt(&self, other: &Box<dyn SetOp<Item = Self::Item>>) -> bool { self.cmp_box(other).eq(&Ordering::Less) } + fn le(&self, other: &Box<dyn SetOp<Item = Self::Item>>) -> bool { [Ordering::Less, Ordering::Equal].contains(&self.cmp_box(other)) } + fn gt(&self, other: &Box<dyn SetOp<Item = Self::Item>>) -> bool { self.cmp_box(other).eq(&Ordering::Greater) } + fn ge(&self, other: &Box<dyn SetOp<Item = Self::Item>>) -> bool { [Ordering::Greater, Ordering::Equal].contains(&self.cmp_box(other)) } + + fn max(&self, other: &Box<dyn SetOp<Item = Self::Item>>) -> Box<dyn SetOp<Item = Self::Item>> { + match self.cmp_box(other) { + Ordering::Less => other.clone(), + Ordering::Equal => other.clone(), + Ordering::Greater => self.clone_box(), + } + } + + fn min(&self, other: &Box<dyn SetOp<Item = Self::Item>>) -> Box<dyn SetOp<Item = Self::Item>> { + match self.cmp_box(other) { + Ordering::Less => self.clone_box(), + Ordering::Equal => self.clone_box(), + Ordering::Greater => other.clone(), + } + } + + fn clamp(&self, min: &Box<dyn SetOp<Item = Self::Item>>, max: &Box<dyn SetOp<Item = Self::Item>>) -> Box<dyn SetOp<Item = Self::Item>> { + if self.lt(min) { + return min.clone() + } else if self.gt(max) { + max.clone() + } else { + self.clone_box() + } + } } pub trait SetOpTraits { @@ -49,28 +95,28 @@ impl<T: BObject> Eq for Box<dyn SetOp<Item=T>> {} impl<T: BObject> PartialEq<Self> for Box<dyn SetOp<Item=T>> { fn eq(&self, other: &Self) -> bool { - self.eq(other) + self.eq_box(other) } } impl<T: BObject> PartialOrd<Self> for Box<dyn SetOp<Item=T>> { fn partial_cmp(&self, other: &Self) -> Option<Ordering> { - self.partial_cmp(other) + self.partial_cmp_box(other) } } impl<T: BObject> Ord for Box<dyn SetOp<Item = T>> { fn cmp(&self, other: &Self) -> Ordering { - self.cmp(other) + self.cmp_box(other) } } - -impl<T: 'static + BObject> dyn SetOp<Item = T> { +/* +impl<T: 'static + BObject> Default for Box<dyn SetOp<Item = T>> { fn default() -> Box<Self> { Box::new(Identity {phantom: PhantomData}) } } - +*/ @@ -81,6 +127,10 @@ pub struct Identity<T: BObject> { phantom: PhantomData<T>, } +impl<T: BObject> Identity<T> { + const OP_NAME: &'static str = "Identity"; +} + impl<T: BObject> Debug for Identity<T> { fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result { write!(f, "Identity()") @@ -97,6 +147,14 @@ impl<T: 'static + BObject> SetOp for Identity<T> { fn clone_box(&self) -> Box<dyn SetOp<Item=Self::Item>> { Box::new(Identity{phantom: PhantomData}) } + + fn get_op_name(&self) -> &str { + return Identity::<T>::OP_NAME; + } + + fn get_rhs(&self) -> Option<&Box<dyn SetOp<Item=Self::Item>>> { + return Option::None; + } } impl<T: 'static + BObject> SetOpTraits for Identity<T>{} \ No newline at end of file -- GitLab