src/bisection_tree/aggregator.rs

branch
dev
changeset 96
962c8e346ab9
parent 63
f7b87d84864d
child 97
4e80fb049dca
--- a/src/bisection_tree/aggregator.rs	Sun Apr 27 14:48:13 2025 -0500
+++ b/src/bisection_tree/aggregator.rs	Sun Apr 27 15:45:40 2025 -0500
@@ -2,9 +2,9 @@
 Aggregation / summarisation of information in branches of bisection trees.
 */
 
-use crate::types::*;
+use crate::instance::Instance;
 use crate::sets::Set;
-use crate::instance::Instance;
+use crate::types::*;
 
 /// Trait for aggregating information about a branch of a [bisection tree][super::BT].
 ///
@@ -19,53 +19,67 @@
 ///    of a function on a greater domain from bounds on subdomains
 ///    (in practise [`Cube`][crate::sets::Cube]s).
 ///
-pub trait Aggregator : Clone + Sync + Send + 'static + std::fmt::Debug {
+pub trait Aggregator: Clone + Sync + Send + 'static + std::fmt::Debug {
     /// Aggregate a new data to current state.
-    fn aggregate<I>(&mut self, aggregates : I)
-    where I : Iterator<Item=Self>;
+    fn aggregate<I>(&mut self, aggregates: I)
+    where
+        I: Iterator<Item = Self>;
 
     /// Summarise several other aggregators, resetting current state.
-    fn summarise<'a, I>(&'a mut self, aggregates : I)
-    where I : Iterator<Item=&'a Self>;
+    fn summarise<'a, I>(&'a mut self, aggregates: I)
+    where
+        I: Iterator<Item = &'a Self>;
 
     /// Create a new “empty” aggregate data.
     fn new() -> Self;
 }
 
 /// An [`Aggregator`] that doesn't aggregate anything.
-#[derive(Clone,Debug)]
+#[derive(Clone, Debug)]
 pub struct NullAggregator;
 
 impl Aggregator for NullAggregator {
-    fn aggregate<I>(&mut self, _aggregates : I)
-    where I : Iterator<Item=Self> {}
+    fn aggregate<I>(&mut self, _aggregates: I)
+    where
+        I: Iterator<Item = Self>,
+    {
+    }
 
-    fn summarise<'a, I>(&'a mut self, _aggregates : I)
-    where I : Iterator<Item=&'a Self> {}
+    fn summarise<'a, I>(&'a mut self, _aggregates: I)
+    where
+        I: Iterator<Item = &'a Self>,
+    {
+    }
 
-    fn new() -> Self { NullAggregator }
+    fn new() -> Self {
+        NullAggregator
+    }
 }
 
 /// Upper and lower bounds on an `F`-valued function.
-#[derive(Copy,Clone,Debug)]
+#[derive(Copy, Clone, Debug)]
 pub struct Bounds<F>(
     /// Lower bound
     pub F,
     /// Upper bound
-    pub F
+    pub F,
 );
 
-impl<F : Copy> Bounds<F> {
+impl<F: Copy> Bounds<F> {
     /// Returns the lower bound
     #[inline]
-    pub fn lower(&self) -> F { self.0 }
+    pub fn lower(&self) -> F {
+        self.0
+    }
 
     /// Returns the upper bound
     #[inline]
-    pub fn upper(&self) -> F { self.1 }
+    pub fn upper(&self) -> F {
+        self.1
+    }
 }
 
-impl<F : Float> Bounds<F> {
+impl<F: Float> Bounds<F> {
     /// Returns a uniform bound.
     ///
     /// This is maximum over the absolute values of the upper and lower bound.
@@ -77,7 +91,7 @@
 
     /// Construct a bounds, making sure `lower` bound is less than `upper`
     #[inline]
-    pub fn corrected(lower : F, upper : F) -> Self {
+    pub fn corrected(lower: F, upper: F) -> Self {
         if lower <= upper {
             Bounds(lower, upper)
         } else {
@@ -87,7 +101,7 @@
 
     /// Refine the lower bound
     #[inline]
-    pub fn refine_lower(&self, lower : F) -> Self {
+    pub fn refine_lower(&self, lower: F) -> Self {
         let &Bounds(l, u) = self;
         debug_assert!(l <= u);
         Bounds(l.max(lower), u.max(lower))
@@ -95,27 +109,27 @@
 
     /// Refine the lower bound
     #[inline]
-    pub fn refine_upper(&self, upper : F) -> Self {
+    pub fn refine_upper(&self, upper: F) -> Self {
         let &Bounds(l, u) = self;
         debug_assert!(l <= u);
         Bounds(l.min(upper), u.min(upper))
     }
 }
 
-impl<'a, F : Float> std::ops::Add<Self> for Bounds<F> {
+impl<'a, F: Float> std::ops::Add<Self> for Bounds<F> {
     type Output = Self;
     #[inline]
-    fn add(self, Bounds(l2, u2) : Self) -> Self::Output {
+    fn add(self, Bounds(l2, u2): Self) -> Self::Output {
         let Bounds(l1, u1) = self;
         debug_assert!(l1 <= u1 && l2 <= u2);
         Bounds(l1 + l2, u1 + u2)
     }
 }
 
-impl<'a, F : Float> std::ops::Mul<Self> for Bounds<F> {
+impl<'a, F: Float> std::ops::Mul<Self> for Bounds<F> {
     type Output = Self;
     #[inline]
-    fn mul(self, Bounds(l2, u2) : Self) -> Self::Output {
+    fn mul(self, Bounds(l2, u2): Self) -> Self::Output {
         let Bounds(l1, u1) = self;
         debug_assert!(l1 <= u1 && l2 <= u2);
         let a = l1 * l2;
@@ -125,19 +139,21 @@
     }
 }
 
-impl<F : Float> std::iter::Product for Bounds<F> {
+impl<F: Float> std::iter::Product for Bounds<F> {
     #[inline]
     fn product<I>(mut iter: I) -> Self
-    where I: Iterator<Item = Self> {
+    where
+        I: Iterator<Item = Self>,
+    {
         match iter.next() {
             None => Bounds(F::ZERO, F::ZERO),
-            Some(init) => iter.fold(init, |a, b| a*b)
+            Some(init) => iter.fold(init, |a, b| a * b),
         }
     }
 }
 
-impl<F : Float> Set<F> for Bounds<F> {
-    fn contains<I : Instance<F>>(&self, item : I) -> bool {
+impl<F: Float> Set<F> for Bounds<F> {
+    fn contains<I: Instance<F>>(&self, item: I) -> bool {
         let v = item.own();
         let &Bounds(l, u) = self;
         debug_assert!(l <= u);
@@ -145,10 +161,10 @@
     }
 }
 
-impl<F : Float> Bounds<F> {
+impl<F: Float> Bounds<F> {
     /// Calculate a common bound (glb, lub) for two bounds.
     #[inline]
-    pub fn common(&self, &Bounds(l2, u2) : &Self) -> Self {
+    pub fn common(&self, &Bounds(l2, u2): &Self) -> Self {
         let &Bounds(l1, u1) = self;
         debug_assert!(l1 <= u1 && l2 <= u2);
         Bounds(l1.min(l2), u1.max(u2))
@@ -156,7 +172,7 @@
 
     /// Indicates whether `Self` is a superset of the argument bound.
     #[inline]
-    pub fn superset(&self, &Bounds(l2, u2) : &Self) -> bool {
+    pub fn superset(&self, &Bounds(l2, u2): &Self) -> bool {
         let &Bounds(l1, u1) = self;
         debug_assert!(l1 <= u1 && l2 <= u2);
         l1 <= l2 && u2 <= u1
@@ -164,7 +180,7 @@
 
     /// Returns the greatest bound contained by both argument bounds, if one exists.
     #[inline]
-    pub fn glb(&self, &Bounds(l2, u2) : &Self) -> Option<Self> {
+    pub fn glb(&self, &Bounds(l2, u2): &Self) -> Option<Self> {
         let &Bounds(l1, u1) = self;
         debug_assert!(l1 <= u1 && l2 <= u2);
         let l = l1.max(l2);
@@ -178,21 +194,23 @@
     }
 }
 
-impl<F : Float> Aggregator for Bounds<F> {
+impl<F: Float> Aggregator for Bounds<F> {
     #[inline]
-    fn aggregate<I>(&mut self, aggregates : I)
-    where I : Iterator<Item=Self> {
+    fn aggregate<I>(&mut self, aggregates: I)
+    where
+        I: Iterator<Item = Self>,
+    {
         *self = aggregates.fold(*self, |a, b| a + b);
     }
 
     #[inline]
-    fn summarise<'a, I>(&'a mut self, mut aggregates : I)
-    where I : Iterator<Item=&'a Self> {
+    fn summarise<'a, I>(&'a mut self, mut aggregates: I)
+    where
+        I: Iterator<Item = &'a Self>,
+    {
         *self = match aggregates.next() {
             None => Bounds(F::ZERO, F::ZERO), // No parts in this cube; the function is zero
-            Some(&bounds) => {
-                aggregates.fold(bounds, |a, b| a.common(b))
-            }
+            Some(&bounds) => aggregates.fold(bounds, |a, b| a.common(b)),
         }
     }
 

mercurial