diff -r 495448cca603 -r 6aa955ad8122 src/bisection_tree/either.rs
--- a/src/bisection_tree/either.rs Thu May 01 08:40:33 2025 -0500
+++ b/src/bisection_tree/either.rs Thu May 01 13:06:58 2025 -0500
@@ -1,36 +1,26 @@
-
use std::iter::Chain;
use std::sync::Arc;
+use crate::iter::{MapF, MapZ, Mappable};
+use crate::loc::Loc;
+use crate::mapping::{DifferentiableImpl, DifferentiableMapping, Instance, Mapping, Space};
+use crate::sets::Cube;
use crate::types::*;
-use crate::mapping::{
- Instance,
- Mapping,
- DifferentiableImpl,
- DifferentiableMapping,
- Space,
-};
-use crate::iter::{Mappable, MapF, MapZ};
-use crate::sets::Cube;
-use crate::loc::Loc;
+use super::aggregator::*;
use super::support::*;
-use super::aggregator::*;
/// A structure for storing two [`SupportGenerator`]s summed/chain together.
///
/// This is needed to work with sums of different types of [`Support`]s.
-#[derive(Debug,Clone)]
-pub struct BothGenerators(
- pub(super) Arc,
- pub(super) Arc,
-);
+#[derive(Debug, Clone)]
+pub struct BothGenerators(pub(super) Arc, pub(super) Arc);
/// A structure for a [`Support`] that can be either `A` or `B`.
///
/// This is needed to work with sums of different types of [`Support`]s.
-#[derive(Debug,Clone)]
-pub enum EitherSupport {
+#[derive(Debug, Clone)]
+pub enum EitherSupport {
Left(A),
Right(B),
}
@@ -38,46 +28,55 @@
// We need type alias bounds to access associate types.
#[allow(type_alias_bounds)]
type BothAllDataIter<
- 'a, F,
- G1 : SupportGenerator,
- G2 : SupportGenerator,
- const N : usize
+ 'a,
+ F,
+ G1: SupportGenerator,
+ G2: SupportGenerator,
+ const N: usize,
> = Chain<
- MapF, (usize, EitherSupport)>,
- MapZ, usize, (usize, EitherSupport)>,
+ MapF, (usize, EitherSupport)>,
+ MapZ, usize, (usize, EitherSupport)>,
>;
impl BothGenerators {
/// Helper for [`all_left_data`].
#[inline]
- fn map_left((d, support) : (G1::Id, G1::SupportType))
- -> (usize, EitherSupport)
- where G1 : SupportGenerator,
- G2 : SupportGenerator {
-
- let id : usize = d.into();
+ fn map_left(
+ (d, support): (G1::Id, G1::SupportType),
+ ) -> (usize, EitherSupport)
+ where
+ G1: SupportGenerator,
+ G2: SupportGenerator,
+ {
+ let id: usize = d.into();
(id.into(), EitherSupport::Left(support))
}
/// Helper for [`all_right_data`].
#[inline]
- fn map_right(n0 : &usize, (d, support) : (G2::Id, G2::SupportType))
- -> (usize, EitherSupport)
- where G1 : SupportGenerator,
- G2 : SupportGenerator {
-
- let id : usize = d.into();
- ((n0+id).into(), EitherSupport::Right(support))
+ fn map_right(
+ n0: &usize,
+ (d, support): (G2::Id, G2::SupportType),
+ ) -> (usize, EitherSupport)
+ where
+ G1: SupportGenerator,
+ G2: SupportGenerator,
+ {
+ let id: usize = d.into();
+ ((n0 + id).into(), EitherSupport::Right(support))
}
/// Calls [`SupportGenerator::all_data`] on the “left” support generator.
///
/// Converts both the id and the [`Support`] into a form that corresponds to `BothGenerators`.
#[inline]
- pub(super) fn all_left_data(&self)
- -> MapF, (usize, EitherSupport)>
- where G1 : SupportGenerator,
- G2 : SupportGenerator {
+ pub(super) fn all_left_data(
+ &self,
+ ) -> MapF, (usize, EitherSupport)>
+ where
+ G1: SupportGenerator,
+ G2: SupportGenerator,
+ {
self.0.all_data().mapF(Self::map_left)
}
@@ -85,33 +84,38 @@
///
/// Converts both the id and the [`Support`] into a form that corresponds to `BothGenerators`.
#[inline]
- pub(super) fn all_right_data(&self)
- -> MapZ, usize, (usize, EitherSupport)>
- where G1 : SupportGenerator,
- G2 : SupportGenerator {
+ pub(super) fn all_right_data(
+ &self,
+ ) -> MapZ, usize, (usize, EitherSupport)>
+ where
+ G1: SupportGenerator,
+ G2: SupportGenerator,
+ {
let n0 = self.0.support_count();
self.1.all_data().mapZ(n0, Self::map_right)
}
}
-impl
-SupportGenerator
-for BothGenerators
-where G1 : SupportGenerator,
- G2 : SupportGenerator {
-
+impl SupportGenerator for BothGenerators
+where
+ G1: SupportGenerator,
+ G2: SupportGenerator,
+{
type Id = usize;
- type SupportType = EitherSupport;
- type AllDataIter<'a> = BothAllDataIter<'a, F, G1, G2, N> where G1 : 'a, G2 : 'a;
+ type SupportType = EitherSupport;
+ type AllDataIter<'a>
+ = BothAllDataIter<'a, F, G1, G2, N>
+ where
+ G1: 'a,
+ G2: 'a;
#[inline]
- fn support_for(&self, id : Self::Id)
- -> Self::SupportType {
+ fn support_for(&self, id: Self::Id) -> Self::SupportType {
let n0 = self.0.support_count();
if id < n0 {
EitherSupport::Left(self.0.support_for(id.into()))
} else {
- EitherSupport::Right(self.1.support_for((id-n0).into()))
+ EitherSupport::Right(self.1.support_for((id - n0).into()))
}
}
@@ -126,12 +130,13 @@
}
}
-impl Support for EitherSupport
-where S1 : Support,
- S2 : Support {
-
+impl Support for EitherSupport
+where
+ S1: Support,
+ S2: Support,
+{
#[inline]
- fn support_hint(&self) -> Cube {
+ fn support_hint(&self) -> Cube {
match self {
EitherSupport::Left(ref a) => a.support_hint(),
EitherSupport::Right(ref b) => b.support_hint(),
@@ -139,7 +144,7 @@
}
#[inline]
- fn in_support(&self, x : &Loc) -> bool {
+ fn in_support(&self, x: &Loc) -> bool {
match self {
EitherSupport::Left(ref a) => a.in_support(x),
EitherSupport::Right(ref b) => b.in_support(x),
@@ -147,7 +152,7 @@
}
#[inline]
- fn bisection_hint(&self, cube : &Cube) -> [Option; N] {
+ fn bisection_hint(&self, cube: &Cube) -> [Option; N] {
match self {
EitherSupport::Left(ref a) => a.bisection_hint(cube),
EitherSupport::Right(ref b) => b.bisection_hint(cube),
@@ -155,13 +160,14 @@
}
}
-impl LocalAnalysis for EitherSupport
-where A : Aggregator,
- S1 : LocalAnalysis,
- S2 : LocalAnalysis, {
-
+impl LocalAnalysis for EitherSupport
+where
+ A: Aggregator,
+ S1: LocalAnalysis,
+ S2: LocalAnalysis,
+{
#[inline]
- fn local_analysis(&self, cube : &Cube) -> A {
+ fn local_analysis(&self, cube: &Cube) -> A {
match self {
EitherSupport::Left(ref a) => a.local_analysis(cube),
EitherSupport::Right(ref b) => b.local_analysis(cube),
@@ -169,11 +175,12 @@
}
}
-impl GlobalAnalysis for EitherSupport
-where A : Aggregator,
- S1 : GlobalAnalysis,
- S2 : GlobalAnalysis, {
-
+impl GlobalAnalysis for EitherSupport
+where
+ A: Aggregator,
+ S1: GlobalAnalysis,
+ S2: GlobalAnalysis,
+{
#[inline]
fn global_analysis(&self) -> A {
match self {
@@ -183,17 +190,17 @@
}
}
-impl Mapping for EitherSupport
+impl Mapping for EitherSupport
where
- F : Space,
- X : Space,
- S1 : Mapping,
- S2 : Mapping,
+ F: Space,
+ X: Space,
+ S1: Mapping,
+ S2: Mapping,
{
type Codomain = F;
#[inline]
- fn apply>(&self, x : I) -> F {
+ fn apply>(&self, x: I) -> F {
match self {
EitherSupport::Left(ref a) => a.apply(x),
EitherSupport::Right(ref b) => b.apply(x),
@@ -201,17 +208,17 @@
}
}
-impl DifferentiableImpl for EitherSupport
+impl DifferentiableImpl for EitherSupport
where
- O : Space,
- X : Space,
- S1 : DifferentiableMapping,
- S2 : DifferentiableMapping,
+ O: Space,
+ X: Space,
+ S1: DifferentiableMapping,
+ S2: DifferentiableMapping,
{
type Derivative = O;
#[inline]
- fn differential_impl>(&self, x : I) -> O {
+ fn differential_impl>(&self, x: I) -> O {
match self {
EitherSupport::Left(ref a) => a.differential(x),
EitherSupport::Right(ref b) => b.differential(x),
@@ -221,44 +228,47 @@
macro_rules! make_either_scalarop_rhs {
($trait:ident, $fn:ident, $trait_assign:ident, $fn_assign:ident) => {
- impl
- std::ops::$trait_assign
- for BothGenerators
- where G1 : std::ops::$trait_assign + Clone,
- G2 : std::ops::$trait_assign + Clone, {
+ impl std::ops::$trait_assign for BothGenerators
+ where
+ G1: std::ops::$trait_assign + Clone,
+ G2: std::ops::$trait_assign + Clone,
+ {
#[inline]
- fn $fn_assign(&mut self, t : F) {
+ fn $fn_assign(&mut self, t: F) {
Arc::make_mut(&mut self.0).$fn_assign(t);
Arc::make_mut(&mut self.1).$fn_assign(t);
}
}
- impl<'a, F : Float, G1, G2>
- std::ops::$trait
- for &'a BothGenerators
- where &'a G1 : std::ops::$trait,
- &'a G2 : std::ops::$trait {
+ impl<'a, F: Float, G1, G2> std::ops::$trait for &'a BothGenerators
+ where
+ &'a G1: std::ops::$trait,
+ &'a G2: std::ops::$trait,
+ {
type Output = BothGenerators;
#[inline]
- fn $fn(self, t : F) -> BothGenerators {
- BothGenerators(Arc::new(self.0.$fn(t)),
- Arc::new(self.1.$fn(t)))
+ fn $fn(self, t: F) -> BothGenerators {
+ BothGenerators(Arc::new(self.0.$fn(t)), Arc::new(self.1.$fn(t)))
}
}
- }
+ };
}
make_either_scalarop_rhs!(Mul, mul, MulAssign, mul_assign);
make_either_scalarop_rhs!(Div, div, DivAssign, div_assign);
impl std::ops::Neg for BothGenerators
-where G1 : std::ops::Neg + Clone,
- G2 : std::ops::Neg + Clone, {
+where
+ G1: std::ops::Neg + Clone,
+ G2: std::ops::Neg + Clone,
+{
type Output = BothGenerators;
#[inline]
fn neg(self) -> Self::Output {
- BothGenerators(Arc::new(Arc::unwrap_or_clone(self.0).neg()),
- Arc::new(Arc::unwrap_or_clone(self.1).neg()))
+ BothGenerators(
+ Arc::new(Arc::unwrap_or_clone(self.0).neg()),
+ Arc::new(Arc::unwrap_or_clone(self.1).neg()),
+ )
}
}
/*
@@ -270,4 +280,4 @@
BothGenerators(self.0.neg(), self.1.neg())
}
}
-*/
\ No newline at end of file
+*/