Wed, 03 Sep 2025 20:19:41 -0500
decompose
| 5 | 1 | /*! |
| 2 | Aggregation / summarisation of information in branches of bisection trees. | |
| 3 | */ | |
| 4 | ||
|
97
4e80fb049dca
MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents:
96
diff
changeset
|
5 | pub use crate::bounds::Bounds; |
|
96
962c8e346ab9
Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents:
63
diff
changeset
|
6 | use crate::types::*; |
| 0 | 7 | |
| 5 | 8 | /// Trait for aggregating information about a branch of a [bisection tree][super::BT]. |
| 9 | /// | |
| 10 | /// Currently [`Bounds`] is the only provided aggregator. | |
| 11 | /// It keeps track of upper and lower bounds of a function representeed by the `BT` by | |
| 12 | /// summing [`Bounds`] produced by [`LocalAnalysis`][super::support::LocalAnalysis] of the | |
| 13 | /// [`Support`][super::support::Support]s of the data stored in the tree. | |
| 14 | /// For the `Bounds` aggregator: | |
| 15 | /// * [`Self::aggregate`] sums input bounds to the current bound. This provides a conservative | |
| 16 | /// estimate of the upper and lower bounds of a sum of functions. | |
| 17 | /// * [`Self::summarise`] takes the maximum of the input bounds. This calculates the bounds | |
| 18 | /// of a function on a greater domain from bounds on subdomains | |
| 19 | /// (in practise [`Cube`][crate::sets::Cube]s). | |
| 20 | /// | |
|
96
962c8e346ab9
Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents:
63
diff
changeset
|
21 | pub trait Aggregator: Clone + Sync + Send + 'static + std::fmt::Debug { |
| 5 | 22 | /// Aggregate a new data to current state. |
|
96
962c8e346ab9
Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents:
63
diff
changeset
|
23 | fn aggregate<I>(&mut self, aggregates: I) |
|
962c8e346ab9
Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents:
63
diff
changeset
|
24 | where |
|
962c8e346ab9
Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents:
63
diff
changeset
|
25 | I: Iterator<Item = Self>; |
| 0 | 26 | |
| 5 | 27 | /// Summarise several other aggregators, resetting current state. |
|
96
962c8e346ab9
Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents:
63
diff
changeset
|
28 | fn summarise<'a, I>(&'a mut self, aggregates: I) |
|
962c8e346ab9
Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents:
63
diff
changeset
|
29 | where |
|
962c8e346ab9
Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents:
63
diff
changeset
|
30 | I: Iterator<Item = &'a Self>; |
| 0 | 31 | |
| 5 | 32 | /// Create a new “empty” aggregate data. |
| 0 | 33 | fn new() -> Self; |
| 34 | } | |
| 35 | ||
| 36 | /// An [`Aggregator`] that doesn't aggregate anything. | |
|
96
962c8e346ab9
Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents:
63
diff
changeset
|
37 | #[derive(Clone, Debug)] |
| 0 | 38 | pub struct NullAggregator; |
| 39 | ||
| 40 | impl Aggregator for NullAggregator { | |
|
96
962c8e346ab9
Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents:
63
diff
changeset
|
41 | fn aggregate<I>(&mut self, _aggregates: I) |
|
962c8e346ab9
Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents:
63
diff
changeset
|
42 | where |
|
962c8e346ab9
Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents:
63
diff
changeset
|
43 | I: Iterator<Item = Self>, |
|
962c8e346ab9
Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents:
63
diff
changeset
|
44 | { |
|
962c8e346ab9
Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents:
63
diff
changeset
|
45 | } |
| 0 | 46 | |
|
96
962c8e346ab9
Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents:
63
diff
changeset
|
47 | fn summarise<'a, I>(&'a mut self, _aggregates: I) |
|
962c8e346ab9
Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents:
63
diff
changeset
|
48 | where |
|
962c8e346ab9
Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents:
63
diff
changeset
|
49 | I: Iterator<Item = &'a Self>, |
|
962c8e346ab9
Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents:
63
diff
changeset
|
50 | { |
|
962c8e346ab9
Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents:
63
diff
changeset
|
51 | } |
| 0 | 52 | |
|
96
962c8e346ab9
Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents:
63
diff
changeset
|
53 | fn new() -> Self { |
|
962c8e346ab9
Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents:
63
diff
changeset
|
54 | NullAggregator |
|
962c8e346ab9
Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents:
63
diff
changeset
|
55 | } |
| 0 | 56 | } |
| 57 | ||
|
96
962c8e346ab9
Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents:
63
diff
changeset
|
58 | impl<F: Float> Aggregator for Bounds<F> { |
| 0 | 59 | #[inline] |
|
96
962c8e346ab9
Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents:
63
diff
changeset
|
60 | fn aggregate<I>(&mut self, aggregates: I) |
|
962c8e346ab9
Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents:
63
diff
changeset
|
61 | where |
|
962c8e346ab9
Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents:
63
diff
changeset
|
62 | I: Iterator<Item = Self>, |
|
962c8e346ab9
Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents:
63
diff
changeset
|
63 | { |
| 0 | 64 | *self = aggregates.fold(*self, |a, b| a + b); |
| 65 | } | |
| 66 | ||
| 67 | #[inline] | |
|
96
962c8e346ab9
Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents:
63
diff
changeset
|
68 | fn summarise<'a, I>(&'a mut self, mut aggregates: I) |
|
962c8e346ab9
Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents:
63
diff
changeset
|
69 | where |
|
962c8e346ab9
Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents:
63
diff
changeset
|
70 | I: Iterator<Item = &'a Self>, |
|
962c8e346ab9
Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents:
63
diff
changeset
|
71 | { |
| 0 | 72 | *self = match aggregates.next() { |
| 73 | None => Bounds(F::ZERO, F::ZERO), // No parts in this cube; the function is zero | |
|
96
962c8e346ab9
Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents:
63
diff
changeset
|
74 | Some(&bounds) => aggregates.fold(bounds, |a, b| a.common(b)), |
| 0 | 75 | } |
| 76 | } | |
| 77 | ||
| 78 | #[inline] | |
| 79 | fn new() -> Self { | |
| 80 | Bounds(F::ZERO, F::ZERO) | |
| 81 | } | |
| 82 | } |