refactor subset
This commit is contained in:
parent
d481ec539e
commit
8db631f87e
@ -84,7 +84,7 @@ impl<'a, T: 'a + MonadFail<'a, ()>, A: 'a + Clone, D: 'a + PartialEq> SubsetCont
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn test(self) -> T::F<()> {
|
fn test_unoptimised(self) -> T::F<()> {
|
||||||
let split = self.n_subset.split();
|
let split = self.n_subset.split();
|
||||||
match self.comparator.compare(&split.2, &self.k_super) {
|
match self.comparator.compare(&split.2, &self.k_super) {
|
||||||
Comparison::L => self.on_l(split),
|
Comparison::L => self.on_l(split),
|
||||||
@ -92,6 +92,44 @@ impl<'a, T: 'a + MonadFail<'a, ()>, A: 'a + Clone, D: 'a + PartialEq> SubsetCont
|
|||||||
Comparison::R => self.on_r(split),
|
Comparison::R => self.on_r(split),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn outside_l(&self) -> bool {
|
||||||
|
outside_l(self.comparator, &self.k_l, &self.k_super)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn outside_r(&self) -> bool {
|
||||||
|
outside_r(self.comparator, &self.k_r, &self.k_super)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn test_r_only(self) -> T::F<()> {
|
||||||
|
t_subset_of_t(
|
||||||
|
self.comparator,
|
||||||
|
self.n_subset.to_tree(),
|
||||||
|
self.t_superr,
|
||||||
|
self.k_l,
|
||||||
|
self.k_r,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn test_l_only(self) -> T::F<()> {
|
||||||
|
t_subset_of_t(
|
||||||
|
self.comparator,
|
||||||
|
self.n_subset.to_tree(),
|
||||||
|
self.t_superl,
|
||||||
|
self.k_l,
|
||||||
|
self.k_r,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn test_optimised(self) -> T::F<()> {
|
||||||
|
if self.outside_l() {
|
||||||
|
self.test_r_only()
|
||||||
|
} else if self.outside_r() {
|
||||||
|
self.test_l_only()
|
||||||
|
} else {
|
||||||
|
self.test_unoptimised()
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn outside_l<A>(comparator: &dyn Comparator<A>, k_l: &Option<A>, k_super: &A) -> bool {
|
fn outside_l<A>(comparator: &dyn Comparator<A>, k_l: &Option<A>, k_super: &A) -> bool {
|
||||||
@ -118,11 +156,6 @@ pub fn n_subset_of_n<'a, T: MonadFail<'a, ()>, A: 'a + Clone, D: 'a + PartialEq>
|
|||||||
k_r: Option<A>,
|
k_r: Option<A>,
|
||||||
) -> T::F<()> {
|
) -> T::F<()> {
|
||||||
let (t_superl, t_superr, k_super) = n_superset.split();
|
let (t_superl, t_superr, k_super) = n_superset.split();
|
||||||
if outside_l(comparator, &k_l, &k_super) {
|
|
||||||
t_subset_of_t(comparator, n_subset.to_tree(), t_superr, k_l, k_r)
|
|
||||||
} else if outside_r(comparator, &k_r, &k_super) {
|
|
||||||
t_subset_of_t(comparator, n_subset.to_tree(), t_superl, k_l, k_r)
|
|
||||||
} else {
|
|
||||||
SubsetContext {
|
SubsetContext {
|
||||||
comparator,
|
comparator,
|
||||||
n_subset,
|
n_subset,
|
||||||
@ -133,11 +166,10 @@ pub fn n_subset_of_n<'a, T: MonadFail<'a, ()>, A: 'a + Clone, D: 'a + PartialEq>
|
|||||||
t_superr,
|
t_superr,
|
||||||
k_super,
|
k_super,
|
||||||
}
|
}
|
||||||
.test()
|
.test_optimised()
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn r_subset_of_r<'a, T: MonadFail<'a, ()>, A: 'a + Clone, D: 'a + PartialEq>(
|
pub fn r_subset_of_r_unoptimised<'a, T: MonadFail<'a, ()>, A: 'a + Clone, D: 'a + PartialEq>(
|
||||||
comparator: &'a dyn Comparator<A>,
|
comparator: &'a dyn Comparator<A>,
|
||||||
r_subset: Rc<dyn TraversibleBinaryReference<'a, T, A, D>>,
|
r_subset: Rc<dyn TraversibleBinaryReference<'a, T, A, D>>,
|
||||||
r_superset: Rc<dyn TraversibleBinaryReference<'a, T, A, D>>,
|
r_superset: Rc<dyn TraversibleBinaryReference<'a, T, A, D>>,
|
||||||
@ -151,6 +183,20 @@ pub fn r_subset_of_r<'a, T: MonadFail<'a, ()>, A: 'a + Clone, D: 'a + PartialEq>
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn r_subset_of_r_optimised<'a, T: MonadFail<'a, ()>, A: 'a + Clone, D: 'a + PartialEq>(
|
||||||
|
r_subset: Rc<dyn TraversibleBinaryReference<'a, T, A, D>>,
|
||||||
|
r_superset: Rc<dyn TraversibleBinaryReference<'a, T, A, D>>,
|
||||||
|
comparator: &'a dyn Comparator<A>,
|
||||||
|
k_l: Option<A>,
|
||||||
|
k_r: Option<A>,
|
||||||
|
) -> <T as WeakFunctor<'a>>::F<()> {
|
||||||
|
if r_subset.data() == r_superset.data() {
|
||||||
|
T::pure(())
|
||||||
|
} else {
|
||||||
|
r_subset_of_r_unoptimised(comparator, r_subset, r_superset, k_l, k_r)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
pub fn t_subset_of_t<'a, T: MonadFail<'a, ()>, A: 'a + Clone, D: 'a + PartialEq>(
|
pub fn t_subset_of_t<'a, T: MonadFail<'a, ()>, A: 'a + Clone, D: 'a + PartialEq>(
|
||||||
comparator: &'a dyn Comparator<A>,
|
comparator: &'a dyn Comparator<A>,
|
||||||
t_subset: Rc<dyn TraversibleBinaryTree<'a, T, A, D>>,
|
t_subset: Rc<dyn TraversibleBinaryTree<'a, T, A, D>>,
|
||||||
@ -162,11 +208,7 @@ pub fn t_subset_of_t<'a, T: MonadFail<'a, ()>, A: 'a + Clone, D: 'a + PartialEq>
|
|||||||
(None, _) => T::pure(()),
|
(None, _) => T::pure(()),
|
||||||
(Some(_), None) => T::fail(()),
|
(Some(_), None) => T::fail(()),
|
||||||
(Some(r_subset), Some(r_superset)) => {
|
(Some(r_subset), Some(r_superset)) => {
|
||||||
if r_subset.data() == r_superset.data() {
|
r_subset_of_r_optimised(r_subset, r_superset, comparator, k_l, k_r)
|
||||||
T::pure(())
|
|
||||||
} else {
|
|
||||||
r_subset_of_r(comparator, r_subset, r_superset, k_l, k_r)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -302,13 +302,11 @@ mod stackless_test {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[ignore]
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_factorial() {
|
fn test_factorial() {
|
||||||
assert_eq!(factorial(10).evaluate(), 3628800);
|
assert_eq!(factorial(10).evaluate(), 3628800);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[ignore]
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_dumb() {
|
fn test_dumb() {
|
||||||
let n = 1000;
|
let n = 1000;
|
||||||
|
@ -205,7 +205,6 @@ mod tests {
|
|||||||
.is_err());
|
.is_err());
|
||||||
}
|
}
|
||||||
|
|
||||||
#[ignore]
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_random_slices() {
|
fn test_random_slices() {
|
||||||
let ctr: Rc<UnbalancedConstructor<instances::result::ResultInstance<()>, _>> =
|
let ctr: Rc<UnbalancedConstructor<instances::result::ResultInstance<()>, _>> =
|
||||||
@ -255,7 +254,6 @@ mod tests {
|
|||||||
instances::result::ResultInstance<()>,
|
instances::result::ResultInstance<()>,
|
||||||
>;
|
>;
|
||||||
|
|
||||||
#[ignore]
|
|
||||||
#[test]
|
#[test]
|
||||||
fn trace_one_slice() {
|
fn trace_one_slice() {
|
||||||
let ctr: Rc<UnbalancedConstructor<TracedMonad, _>> =
|
let ctr: Rc<UnbalancedConstructor<TracedMonad, _>> =
|
||||||
|
Loading…
Reference in New Issue
Block a user