src/bisection_tree/bt.rs

Thu, 01 May 2025 13:06:58 -0500

author
Tuomo Valkonen <tuomov@iki.fi>
date
Thu, 01 May 2025 13:06:58 -0500
branch
dev
changeset 124
6aa955ad8122
parent 97
4e80fb049dca
permissions
-rw-r--r--

Transpose loc parameters to allow f64 defaults

5
59dc4c5883f4 Improve documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 0
diff changeset
1 /*!
59dc4c5883f4 Improve documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 0
diff changeset
2 Bisection tree basics, [`BT`] type and the [`BTImpl`] trait.
59dc4c5883f4 Improve documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 0
diff changeset
3 */
0
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
4
96
962c8e346ab9 Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents: 9
diff changeset
5 use itertools::izip;
5
59dc4c5883f4 Improve documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 0
diff changeset
6 pub(super) use nalgebra::Const;
96
962c8e346ab9 Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents: 9
diff changeset
7 use serde::{Deserialize, Serialize};
962c8e346ab9 Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents: 9
diff changeset
8 use std::iter::once;
962c8e346ab9 Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents: 9
diff changeset
9 use std::slice::IterMut;
962c8e346ab9 Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents: 9
diff changeset
10 use std::sync::Arc;
0
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
11
96
962c8e346ab9 Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents: 9
diff changeset
12 use super::aggregator::*;
962c8e346ab9 Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents: 9
diff changeset
13 use super::support::*;
0
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
14 use crate::coefficients::pow;
96
962c8e346ab9 Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents: 9
diff changeset
15 use crate::loc::Loc;
962c8e346ab9 Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents: 9
diff changeset
16 use crate::maputil::{array_init, collect_into_array_unchecked, map2, map2_indexed};
962c8e346ab9 Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents: 9
diff changeset
17 use crate::parallelism::{with_task_budget, TaskBudget};
5
59dc4c5883f4 Improve documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 0
diff changeset
18 use crate::sets::Cube;
96
962c8e346ab9 Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents: 9
diff changeset
19 use crate::types::{Float, Num};
0
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
20
5
59dc4c5883f4 Improve documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 0
diff changeset
21 /// An enum that indicates whether a [`Node`] of a [`BT`] is uninitialised, leaf, or branch.
59dc4c5883f4 Improve documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 0
diff changeset
22 ///
59dc4c5883f4 Improve documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 0
diff changeset
23 /// For the type and const parametere, see the [module level documentation][super].
96
962c8e346ab9 Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents: 9
diff changeset
24 #[derive(Clone, Debug)]
962c8e346ab9 Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents: 9
diff changeset
25 pub(super) enum NodeOption<F: Num, D, A: Aggregator, const N: usize, const P: usize> {
5
59dc4c5883f4 Improve documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 0
diff changeset
26 /// Indicates an uninitilised node; may become a branch or a leaf.
0
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
27 // TODO: Could optimise Uninitialised away by simply treat Leaf with an empty Vec as
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
28 // something that can be still replaced with Branches.
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
29 Uninitialised,
5
59dc4c5883f4 Improve documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 0
diff changeset
30 /// Indicates a leaf node containing a copy-on-write reference-counted vector
59dc4c5883f4 Improve documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 0
diff changeset
31 /// of data of type `D`.
8
4e09b7829b51 Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents: 5
diff changeset
32 Leaf(Vec<D>),
5
59dc4c5883f4 Improve documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 0
diff changeset
33 /// Indicates a branch node, cotaning a copy-on-write reference to the [`Branches`].
8
4e09b7829b51 Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents: 5
diff changeset
34 Branches(Arc<Branches<F, D, A, N, P>>),
0
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
35 }
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
36
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
37 /// Node of a [`BT`] bisection tree.
5
59dc4c5883f4 Improve documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 0
diff changeset
38 ///
59dc4c5883f4 Improve documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 0
diff changeset
39 /// For the type and const parameteres, see the [module level documentation][super].
8
4e09b7829b51 Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents: 5
diff changeset
40 #[derive(Clone, Debug)]
96
962c8e346ab9 Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents: 9
diff changeset
41 pub struct Node<F: Num, D, A: Aggregator, const N: usize, const P: usize> {
5
59dc4c5883f4 Improve documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 0
diff changeset
42 /// The data or branches under the node.
96
962c8e346ab9 Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents: 9
diff changeset
43 pub(super) data: NodeOption<F, D, A, N, P>,
0
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
44 /// Aggregator for `data`.
96
962c8e346ab9 Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents: 9
diff changeset
45 pub(super) aggregator: A,
0
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
46 }
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
47
5
59dc4c5883f4 Improve documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 0
diff changeset
48 /// Branching information of a [`Node`] of a [`BT`] bisection tree into `P` subnodes.
59dc4c5883f4 Improve documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 0
diff changeset
49 ///
59dc4c5883f4 Improve documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 0
diff changeset
50 /// For the type and const parameters, see the [module level documentation][super].
8
4e09b7829b51 Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents: 5
diff changeset
51 #[derive(Clone, Debug)]
96
962c8e346ab9 Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents: 9
diff changeset
52 pub(super) struct Branches<F: Num, D, A: Aggregator, const N: usize, const P: usize> {
0
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
53 /// Point for subdivision of the (unstored) [`Cube`] corresponding to the node.
124
6aa955ad8122 Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents: 97
diff changeset
54 pub(super) branch_at: Loc<N, F>,
0
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
55 /// Subnodes
96
962c8e346ab9 Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents: 9
diff changeset
56 pub(super) nodes: [Node<F, D, A, N, P>; P],
0
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
57 }
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
58
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
59 /// Dirty workaround to broken Rust drop, see [https://github.com/rust-lang/rust/issues/58068]().
96
962c8e346ab9 Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents: 9
diff changeset
60 impl<F: Num, D, A: Aggregator, const N: usize, const P: usize> Drop for Node<F, D, A, N, P> {
0
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
61 fn drop(&mut self) {
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
62 use NodeOption as NO;
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
63
96
962c8e346ab9 Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents: 9
diff changeset
64 let process = |brc: Arc<Branches<F, D, A, N, P>>,
962c8e346ab9 Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents: 9
diff changeset
65 to_drop: &mut Vec<Arc<Branches<F, D, A, N, P>>>| {
0
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
66 // We only drop Branches if we have the only strong reference.
8
4e09b7829b51 Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents: 5
diff changeset
67 // FIXME: update the RwLocks on Nodes.
96
962c8e346ab9 Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents: 9
diff changeset
68 Arc::try_unwrap(brc).ok().map(|branches| {
962c8e346ab9 Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents: 9
diff changeset
69 branches.nodes.map(|mut node| {
962c8e346ab9 Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents: 9
diff changeset
70 if let NO::Branches(brc2) = std::mem::replace(&mut node.data, NO::Uninitialised)
962c8e346ab9 Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents: 9
diff changeset
71 {
962c8e346ab9 Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents: 9
diff changeset
72 to_drop.push(brc2)
962c8e346ab9 Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents: 9
diff changeset
73 }
962c8e346ab9 Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents: 9
diff changeset
74 })
962c8e346ab9 Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents: 9
diff changeset
75 });
0
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
76 };
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
77
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
78 // We mark Self as NodeOption::Uninitialised, extracting the real contents.
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
79 // If we have subprocess, we need to process them.
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
80 if let NO::Branches(brc1) = std::mem::replace(&mut self.data, NO::Uninitialised) {
8
4e09b7829b51 Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents: 5
diff changeset
81 // We store a queue of Arc<Branches> to drop into a vector
0
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
82 let mut to_drop = Vec::new();
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
83 process(brc1, &mut to_drop);
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
84
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
85 // While there are any Branches in the drop queue vector, we continue the process,
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
86 // pushing all internal branching nodes into the queue.
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
87 while let Some(brc) = to_drop.pop() {
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
88 process(brc, &mut to_drop)
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
89 }
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
90 }
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
91 }
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
92 }
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
93
5
59dc4c5883f4 Improve documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 0
diff changeset
94 /// Trait for the depth of a [`BT`].
59dc4c5883f4 Improve documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 0
diff changeset
95 ///
59dc4c5883f4 Improve documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 0
diff changeset
96 /// This will generally be either a runtime [`DynamicDepth`] or compile-time [`Const`] depth.
96
962c8e346ab9 Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents: 9
diff changeset
97 pub trait Depth: 'static + Copy + Send + Sync + std::fmt::Debug {
5
59dc4c5883f4 Improve documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 0
diff changeset
98 /// Lower depth type.
96
962c8e346ab9 Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents: 9
diff changeset
99 type Lower: Depth;
8
4e09b7829b51 Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents: 5
diff changeset
100
5
59dc4c5883f4 Improve documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 0
diff changeset
101 /// Returns a lower depth, if there still is one.
0
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
102 fn lower(&self) -> Option<Self::Lower>;
8
4e09b7829b51 Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents: 5
diff changeset
103
5
59dc4c5883f4 Improve documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 0
diff changeset
104 /// Returns a lower depth or self if this is the lowest depth.
0
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
105 fn lower_or(&self) -> Self::Lower;
8
4e09b7829b51 Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents: 5
diff changeset
106
4e09b7829b51 Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents: 5
diff changeset
107 /// Returns the numeric value of the depth
9
f40dfaf2166d Improvements and minor fixes to bisection tree refinement.
Tuomo Valkonen <tuomov@iki.fi>
parents: 8
diff changeset
108 fn value(&self) -> u32;
0
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
109 }
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
110
5
59dc4c5883f4 Improve documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 0
diff changeset
111 /// Dynamic (runtime) [`Depth`] for a [`BT`].
96
962c8e346ab9 Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents: 9
diff changeset
112 #[derive(Copy, Clone, Debug, Serialize, Deserialize)]
5
59dc4c5883f4 Improve documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 0
diff changeset
113 pub struct DynamicDepth(
59dc4c5883f4 Improve documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 0
diff changeset
114 /// The depth
96
962c8e346ab9 Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents: 9
diff changeset
115 pub u8,
5
59dc4c5883f4 Improve documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 0
diff changeset
116 );
59dc4c5883f4 Improve documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 0
diff changeset
117
0
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
118 impl Depth for DynamicDepth {
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
119 type Lower = Self;
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
120 #[inline]
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
121 fn lower(&self) -> Option<Self> {
96
962c8e346ab9 Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents: 9
diff changeset
122 if self.0 > 0 {
962c8e346ab9 Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents: 9
diff changeset
123 Some(DynamicDepth(self.0 - 1))
962c8e346ab9 Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents: 9
diff changeset
124 } else {
0
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
125 None
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
126 }
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
127 }
8
4e09b7829b51 Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents: 5
diff changeset
128
0
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
129 #[inline]
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
130 fn lower_or(&self) -> Self {
96
962c8e346ab9 Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents: 9
diff changeset
131 DynamicDepth(if self.0 > 0 { self.0 - 1 } else { 0 })
0
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
132 }
8
4e09b7829b51 Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents: 5
diff changeset
133
4e09b7829b51 Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents: 5
diff changeset
134 #[inline]
9
f40dfaf2166d Improvements and minor fixes to bisection tree refinement.
Tuomo Valkonen <tuomov@iki.fi>
parents: 8
diff changeset
135 fn value(&self) -> u32 {
f40dfaf2166d Improvements and minor fixes to bisection tree refinement.
Tuomo Valkonen <tuomov@iki.fi>
parents: 8
diff changeset
136 self.0 as u32
8
4e09b7829b51 Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents: 5
diff changeset
137 }
0
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
138 }
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
139
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
140 impl Depth for Const<0> {
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
141 type Lower = Self;
96
962c8e346ab9 Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents: 9
diff changeset
142 fn lower(&self) -> Option<Self::Lower> {
962c8e346ab9 Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents: 9
diff changeset
143 None
962c8e346ab9 Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents: 9
diff changeset
144 }
962c8e346ab9 Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents: 9
diff changeset
145 fn lower_or(&self) -> Self::Lower {
962c8e346ab9 Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents: 9
diff changeset
146 Const
962c8e346ab9 Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents: 9
diff changeset
147 }
962c8e346ab9 Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents: 9
diff changeset
148 fn value(&self) -> u32 {
962c8e346ab9 Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents: 9
diff changeset
149 0
962c8e346ab9 Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents: 9
diff changeset
150 }
0
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
151 }
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
152
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
153 macro_rules! impl_constdepth {
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
154 ($($n:literal)*) => { $(
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
155 impl Depth for Const<$n> {
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
156 type Lower = Const<{$n-1}>;
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
157 fn lower(&self) -> Option<Self::Lower> { Some(Const) }
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
158 fn lower_or(&self) -> Self::Lower { Const }
9
f40dfaf2166d Improvements and minor fixes to bisection tree refinement.
Tuomo Valkonen <tuomov@iki.fi>
parents: 8
diff changeset
159 fn value(&self) -> u32 { $n }
0
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
160 }
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
161 )* };
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
162 }
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
163 impl_constdepth!(1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32);
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
164
5
59dc4c5883f4 Improve documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 0
diff changeset
165 /// Trait for counting the branching factor of a [`BT`] of dimension `N`.
59dc4c5883f4 Improve documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 0
diff changeset
166 ///
59dc4c5883f4 Improve documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 0
diff changeset
167 /// The const parameter `P` from the [module level documentation][super] is required to satisfy
59dc4c5883f4 Improve documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 0
diff changeset
168 /// `Const<P> : Branchcount<N>`.
59dc4c5883f4 Improve documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 0
diff changeset
169 /// This trait is implemented for `P=pow(2, N)` for small `N`.
96
962c8e346ab9 Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents: 9
diff changeset
170 pub trait BranchCount<const N: usize> {}
0
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
171 macro_rules! impl_branchcount {
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
172 ($($n:literal)*) => { $(
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
173 impl BranchCount<$n> for Const<{pow(2, $n)}>{}
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
174 )* }
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
175 }
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
176 impl_branchcount!(1 2 3 4 5 6 7 8);
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
177
96
962c8e346ab9 Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents: 9
diff changeset
178 impl<F: Float, D, A, const N: usize, const P: usize> Branches<F, D, A, N, P>
962c8e346ab9 Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents: 9
diff changeset
179 where
962c8e346ab9 Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents: 9
diff changeset
180 Const<P>: BranchCount<N>,
962c8e346ab9 Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents: 9
diff changeset
181 A: Aggregator,
0
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
182 {
5
59dc4c5883f4 Improve documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 0
diff changeset
183 /// Returns the index in {0, …, `P`-1} for the branch to which the point `x` corresponds.
59dc4c5883f4 Improve documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 0
diff changeset
184 ///
59dc4c5883f4 Improve documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 0
diff changeset
185 /// This only takes the branch subdivision point $d$ into account, so is always succesfull.
59dc4c5883f4 Improve documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 0
diff changeset
186 /// Thus, for this point, each branch corresponds to a quadrant of $ℝ^N$ relative to $d$.
124
6aa955ad8122 Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents: 97
diff changeset
187 fn get_node_index(&self, x: &Loc<N, F>) -> usize {
96
962c8e346ab9 Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents: 9
diff changeset
188 izip!(0..P, x.iter(), self.branch_at.iter())
962c8e346ab9 Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents: 9
diff changeset
189 .map(|(i, x_i, branch_i)| if x_i > branch_i { 1 << i } else { 0 })
962c8e346ab9 Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents: 9
diff changeset
190 .sum()
0
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
191 }
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
192
5
59dc4c5883f4 Improve documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 0
diff changeset
193 /// Returns the node within `Self` containing the point `x`.
59dc4c5883f4 Improve documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 0
diff changeset
194 ///
59dc4c5883f4 Improve documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 0
diff changeset
195 /// This only takes the branch subdivision point $d$ into account, so is always succesfull.
59dc4c5883f4 Improve documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 0
diff changeset
196 /// Thus, for this point, each branch corresponds to a quadrant of $ℝ^N$ relative to $d$.
0
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
197 #[inline]
124
6aa955ad8122 Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents: 97
diff changeset
198 fn get_node(&self, x: &Loc<N, F>) -> &Node<F, D, A, N, P> {
96
962c8e346ab9 Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents: 9
diff changeset
199 &self.nodes[self.get_node_index(x)]
0
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
200 }
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
201 }
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
202
5
59dc4c5883f4 Improve documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 0
diff changeset
203 /// An iterator over the $P=2^N$ subcubes of a [`Cube`] subdivided at a point `d`.
96
962c8e346ab9 Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents: 9
diff changeset
204 pub(super) struct SubcubeIter<'b, F: Float, const N: usize, const P: usize> {
124
6aa955ad8122 Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents: 97
diff changeset
205 domain: &'b Cube<N, F>,
6aa955ad8122 Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents: 97
diff changeset
206 branch_at: Loc<N, F>,
96
962c8e346ab9 Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents: 9
diff changeset
207 index: usize,
0
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
208 }
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
209
5
59dc4c5883f4 Improve documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 0
diff changeset
210 /// Returns the `i`:th subcube of `domain` subdivided at `branch_at`.
0
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
211 #[inline]
96
962c8e346ab9 Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents: 9
diff changeset
212 fn get_subcube<F: Float, const N: usize>(
124
6aa955ad8122 Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents: 97
diff changeset
213 branch_at: &Loc<N, F>,
6aa955ad8122 Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents: 97
diff changeset
214 domain: &Cube<N, F>,
96
962c8e346ab9 Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents: 9
diff changeset
215 i: usize,
124
6aa955ad8122 Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents: 97
diff changeset
216 ) -> Cube<N, F> {
0
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
217 map2_indexed(branch_at, domain, move |j, &branch, &[start, end]| {
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
218 if i & (1 << j) != 0 {
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
219 [branch, end]
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
220 } else {
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
221 [start, branch]
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
222 }
96
962c8e346ab9 Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents: 9
diff changeset
223 })
962c8e346ab9 Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents: 9
diff changeset
224 .into()
0
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
225 }
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
226
96
962c8e346ab9 Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents: 9
diff changeset
227 impl<'a, 'b, F: Float, const N: usize, const P: usize> Iterator for SubcubeIter<'b, F, N, P> {
124
6aa955ad8122 Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents: 97
diff changeset
228 type Item = Cube<N, F>;
0
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
229 #[inline]
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
230 fn next(&mut self) -> Option<Self::Item> {
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
231 if self.index < P {
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
232 let i = self.index;
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
233 self.index += 1;
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
234 Some(get_subcube(&self.branch_at, self.domain, i))
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
235 } else {
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
236 None
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
237 }
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
238 }
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
239 }
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
240
96
962c8e346ab9 Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents: 9
diff changeset
241 impl<F: Float, D, A, const N: usize, const P: usize> Branches<F, D, A, N, P>
962c8e346ab9 Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents: 9
diff changeset
242 where
962c8e346ab9 Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents: 9
diff changeset
243 Const<P>: BranchCount<N>,
962c8e346ab9 Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents: 9
diff changeset
244 A: Aggregator,
962c8e346ab9 Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents: 9
diff changeset
245 D: 'static + Copy + Send + Sync,
962c8e346ab9 Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents: 9
diff changeset
246 {
5
59dc4c5883f4 Improve documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 0
diff changeset
247 /// Creates a new node branching structure, subdividing `domain` based on the
59dc4c5883f4 Improve documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 0
diff changeset
248 /// [hint][Support::support_hint] of `support`.
124
6aa955ad8122 Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents: 97
diff changeset
249 pub(super) fn new_with<S: LocalAnalysis<F, A, N> + Support<N, F>>(
6aa955ad8122 Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents: 97
diff changeset
250 domain: &Cube<N, F>,
97
4e80fb049dca MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents: 96
diff changeset
251 support: &S,
4e80fb049dca MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents: 96
diff changeset
252 ) -> Self {
0
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
253 let hint = support.bisection_hint(domain);
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
254 let branch_at = map2(&hint, domain, |h, r| {
96
962c8e346ab9 Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents: 9
diff changeset
255 h.unwrap_or_else(|| (r[0] + r[1]) / F::TWO)
962c8e346ab9 Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents: 9
diff changeset
256 .max(r[0])
962c8e346ab9 Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents: 9
diff changeset
257 .min(r[1])
962c8e346ab9 Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents: 9
diff changeset
258 })
962c8e346ab9 Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents: 9
diff changeset
259 .into();
962c8e346ab9 Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents: 9
diff changeset
260 Branches {
962c8e346ab9 Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents: 9
diff changeset
261 branch_at: branch_at,
962c8e346ab9 Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents: 9
diff changeset
262 nodes: array_init(|| Node::new()),
0
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
263 }
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
264 }
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
265
8
4e09b7829b51 Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents: 5
diff changeset
266 /// Summarises the aggregators of these branches into `agg`
96
962c8e346ab9 Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents: 9
diff changeset
267 pub(super) fn summarise_into(&self, agg: &mut A) {
8
4e09b7829b51 Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents: 5
diff changeset
268 // We need to create an array of the aggregators clones due to the RwLock.
4e09b7829b51 Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents: 5
diff changeset
269 agg.summarise(self.nodes.iter().map(Node::get_aggregator));
0
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
270 }
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
271
5
59dc4c5883f4 Improve documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 0
diff changeset
272 /// Returns an iterator over the subcubes of `domain` subdivided at the branching point
59dc4c5883f4 Improve documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 0
diff changeset
273 /// of `self`.
0
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
274 #[inline]
124
6aa955ad8122 Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents: 97
diff changeset
275 pub(super) fn iter_subcubes<'b>(&self, domain: &'b Cube<N, F>) -> SubcubeIter<'b, F, N, P> {
0
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
276 SubcubeIter {
96
962c8e346ab9 Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents: 9
diff changeset
277 domain: domain,
962c8e346ab9 Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents: 9
diff changeset
278 branch_at: self.branch_at,
962c8e346ab9 Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents: 9
diff changeset
279 index: 0,
0
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
280 }
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
281 }
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
282
5
59dc4c5883f4 Improve documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 0
diff changeset
283 /*
59dc4c5883f4 Improve documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 0
diff changeset
284 /// Returns an iterator over all nodes and corresponding subcubes of `self`.
0
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
285 #[inline]
124
6aa955ad8122 Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents: 97
diff changeset
286 pub(super) fn nodes_and_cubes<'a, 'b>(&'a self, domain : &'b Cube<N, F>)
0
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
287 -> std::iter::Zip<Iter<'a, Node<F,D,A,N,P>>, SubcubeIter<'b, F, N, P>> {
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
288 self.nodes.iter().zip(self.iter_subcubes(domain))
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
289 }
5
59dc4c5883f4 Improve documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 0
diff changeset
290 */
0
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
291
5
59dc4c5883f4 Improve documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 0
diff changeset
292 /// Mutably iterate over all nodes and corresponding subcubes of `self`.
0
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
293 #[inline]
96
962c8e346ab9 Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents: 9
diff changeset
294 pub(super) fn nodes_and_cubes_mut<'a, 'b>(
962c8e346ab9 Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents: 9
diff changeset
295 &'a mut self,
124
6aa955ad8122 Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents: 97
diff changeset
296 domain: &'b Cube<N, F>,
96
962c8e346ab9 Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents: 9
diff changeset
297 ) -> std::iter::Zip<IterMut<'a, Node<F, D, A, N, P>>, SubcubeIter<'b, F, N, P>> {
0
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
298 let subcube_iter = self.iter_subcubes(domain);
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
299 self.nodes.iter_mut().zip(subcube_iter)
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
300 }
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
301
8
4e09b7829b51 Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents: 5
diff changeset
302 /// Call `f` on all `(subnode, subcube)` pairs in multiple threads, if `guard` so deems.
4e09b7829b51 Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents: 5
diff changeset
303 #[inline]
4e09b7829b51 Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents: 5
diff changeset
304 fn recurse<'scope, 'smaller, 'refs>(
4e09b7829b51 Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents: 5
diff changeset
305 &'smaller mut self,
124
6aa955ad8122 Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents: 97
diff changeset
306 domain: &'smaller Cube<N, F>,
96
962c8e346ab9 Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents: 9
diff changeset
307 task_budget: TaskBudget<'scope, 'refs>,
124
6aa955ad8122 Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents: 97
diff changeset
308 guard: impl Fn(&Node<F, D, A, N, P>, &Cube<N, F>) -> bool + Send + 'smaller,
6aa955ad8122 Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents: 97
diff changeset
309 mut f: impl for<'a> FnMut(&mut Node<F, D, A, N, P>, &Cube<N, F>, TaskBudget<'smaller, 'a>)
96
962c8e346ab9 Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents: 9
diff changeset
310 + Send
962c8e346ab9 Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents: 9
diff changeset
311 + Copy
962c8e346ab9 Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents: 9
diff changeset
312 + 'smaller,
962c8e346ab9 Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents: 9
diff changeset
313 ) where
962c8e346ab9 Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents: 9
diff changeset
314 'scope: 'smaller,
962c8e346ab9 Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents: 9
diff changeset
315 {
8
4e09b7829b51 Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents: 5
diff changeset
316 let subs = self.nodes_and_cubes_mut(domain);
4e09b7829b51 Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents: 5
diff changeset
317 task_budget.zoom(move |s| {
4e09b7829b51 Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents: 5
diff changeset
318 for (node, subcube) in subs {
4e09b7829b51 Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents: 5
diff changeset
319 if guard(node, &subcube) {
4e09b7829b51 Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents: 5
diff changeset
320 s.execute(move |new_budget| f(node, &subcube, new_budget))
4e09b7829b51 Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents: 5
diff changeset
321 }
4e09b7829b51 Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents: 5
diff changeset
322 }
4e09b7829b51 Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents: 5
diff changeset
323 });
4e09b7829b51 Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents: 5
diff changeset
324 }
4e09b7829b51 Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents: 5
diff changeset
325
0
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
326 /// Insert data into the branch.
5
59dc4c5883f4 Improve documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 0
diff changeset
327 ///
59dc4c5883f4 Improve documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 0
diff changeset
328 /// The parameters are as follows:
59dc4c5883f4 Improve documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 0
diff changeset
329 /// * `domain` is the cube corresponding to this branch.
59dc4c5883f4 Improve documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 0
diff changeset
330 /// * `d` is the data to be inserted
59dc4c5883f4 Improve documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 0
diff changeset
331 /// * `new_leaf_depth` is the depth relative to `self` at which the data is to be inserted.
59dc4c5883f4 Improve documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 0
diff changeset
332 /// * `support` is the [`Support`] that is used determine with which subcubes of `domain`
59dc4c5883f4 Improve documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 0
diff changeset
333 /// (at subdivision depth `new_leaf_depth`) the data `d` is to be associated with.
59dc4c5883f4 Improve documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 0
diff changeset
334 ///
124
6aa955ad8122 Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents: 97
diff changeset
335 pub(super) fn insert<'refs, 'scope, M: Depth, S: LocalAnalysis<F, A, N> + Support<N, F>>(
0
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
336 &mut self,
124
6aa955ad8122 Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents: 97
diff changeset
337 domain: &Cube<N, F>,
96
962c8e346ab9 Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents: 9
diff changeset
338 d: D,
962c8e346ab9 Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents: 9
diff changeset
339 new_leaf_depth: M,
962c8e346ab9 Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents: 9
diff changeset
340 support: &S,
962c8e346ab9 Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents: 9
diff changeset
341 task_budget: TaskBudget<'scope, 'refs>,
0
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
342 ) {
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
343 let support_hint = support.support_hint();
96
962c8e346ab9 Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents: 9
diff changeset
344 self.recurse(
962c8e346ab9 Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents: 9
diff changeset
345 domain,
962c8e346ab9 Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents: 9
diff changeset
346 task_budget,
962c8e346ab9 Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents: 9
diff changeset
347 |_, subcube| support_hint.intersects(&subcube),
962c8e346ab9 Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents: 9
diff changeset
348 move |node, subcube, new_budget| {
962c8e346ab9 Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents: 9
diff changeset
349 node.insert(subcube, d, new_leaf_depth, support, new_budget)
962c8e346ab9 Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents: 9
diff changeset
350 },
962c8e346ab9 Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents: 9
diff changeset
351 );
0
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
352 }
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
353
5
59dc4c5883f4 Improve documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 0
diff changeset
354 /// Construct a new instance of the branch for a different aggregator.
59dc4c5883f4 Improve documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 0
diff changeset
355 ///
59dc4c5883f4 Improve documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 0
diff changeset
356 /// The `generator` is used to convert the data of type `D` of the branch into corresponding
59dc4c5883f4 Improve documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 0
diff changeset
357 /// [`Support`]s. The `domain` is the cube corresponding to `self`.
59dc4c5883f4 Improve documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 0
diff changeset
358 /// The type parameter `ANew´ is the new aggregator, and needs to be implemented for the
59dc4c5883f4 Improve documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 0
diff changeset
359 /// generator's `SupportType`.
59dc4c5883f4 Improve documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 0
diff changeset
360 pub(super) fn convert_aggregator<ANew, G>(
0
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
361 self,
96
962c8e346ab9 Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents: 9
diff changeset
362 generator: &G,
124
6aa955ad8122 Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents: 97
diff changeset
363 domain: &Cube<N, F>,
96
962c8e346ab9 Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents: 9
diff changeset
364 ) -> Branches<F, D, ANew, N, P>
962c8e346ab9 Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents: 9
diff changeset
365 where
962c8e346ab9 Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents: 9
diff changeset
366 ANew: Aggregator,
124
6aa955ad8122 Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents: 97
diff changeset
367 G: SupportGenerator<N, F, Id = D>,
96
962c8e346ab9 Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents: 9
diff changeset
368 G::SupportType: LocalAnalysis<F, ANew, N>,
962c8e346ab9 Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents: 9
diff changeset
369 {
0
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
370 let branch_at = self.branch_at;
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
371 let subcube_iter = self.iter_subcubes(domain);
96
962c8e346ab9 Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents: 9
diff changeset
372 let new_nodes = self
962c8e346ab9 Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents: 9
diff changeset
373 .nodes
962c8e346ab9 Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents: 9
diff changeset
374 .into_iter()
962c8e346ab9 Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents: 9
diff changeset
375 .zip(subcube_iter)
962c8e346ab9 Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents: 9
diff changeset
376 .map(|(node, subcube)| Node::convert_aggregator(node, generator, &subcube));
0
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
377 Branches {
96
962c8e346ab9 Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents: 9
diff changeset
378 branch_at: branch_at,
962c8e346ab9 Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents: 9
diff changeset
379 nodes: collect_into_array_unchecked(new_nodes),
0
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
380 }
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
381 }
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
382
5
59dc4c5883f4 Improve documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 0
diff changeset
383 /// Recalculate aggregator after changes to generator.
59dc4c5883f4 Improve documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 0
diff changeset
384 ///
59dc4c5883f4 Improve documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 0
diff changeset
385 /// The `generator` is used to convert the data of type `D` of the branch into corresponding
59dc4c5883f4 Improve documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 0
diff changeset
386 /// [`Support`]s. The `domain` is the cube corresponding to `self`.
8
4e09b7829b51 Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents: 5
diff changeset
387 pub(super) fn refresh_aggregator<'refs, 'scope, G>(
0
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
388 &mut self,
96
962c8e346ab9 Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents: 9
diff changeset
389 generator: &G,
124
6aa955ad8122 Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents: 97
diff changeset
390 domain: &Cube<N, F>,
96
962c8e346ab9 Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents: 9
diff changeset
391 task_budget: TaskBudget<'scope, 'refs>,
962c8e346ab9 Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents: 9
diff changeset
392 ) where
124
6aa955ad8122 Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents: 97
diff changeset
393 G: SupportGenerator<N, F, Id = D>,
96
962c8e346ab9 Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents: 9
diff changeset
394 G::SupportType: LocalAnalysis<F, A, N>,
962c8e346ab9 Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents: 9
diff changeset
395 {
962c8e346ab9 Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents: 9
diff changeset
396 self.recurse(
962c8e346ab9 Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents: 9
diff changeset
397 domain,
962c8e346ab9 Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents: 9
diff changeset
398 task_budget,
962c8e346ab9 Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents: 9
diff changeset
399 |_, _| true,
962c8e346ab9 Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents: 9
diff changeset
400 move |node, subcube, new_budget| {
962c8e346ab9 Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents: 9
diff changeset
401 node.refresh_aggregator(generator, subcube, new_budget)
962c8e346ab9 Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents: 9
diff changeset
402 },
962c8e346ab9 Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents: 9
diff changeset
403 );
0
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
404 }
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
405 }
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
406
96
962c8e346ab9 Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents: 9
diff changeset
407 impl<F: Float, D, A, const N: usize, const P: usize> Node<F, D, A, N, P>
962c8e346ab9 Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents: 9
diff changeset
408 where
962c8e346ab9 Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents: 9
diff changeset
409 Const<P>: BranchCount<N>,
962c8e346ab9 Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents: 9
diff changeset
410 A: Aggregator,
962c8e346ab9 Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents: 9
diff changeset
411 D: 'static + Copy + Send + Sync,
962c8e346ab9 Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents: 9
diff changeset
412 {
5
59dc4c5883f4 Improve documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 0
diff changeset
413 /// Create a new node
0
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
414 #[inline]
5
59dc4c5883f4 Improve documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 0
diff changeset
415 pub(super) fn new() -> Self {
0
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
416 Node {
96
962c8e346ab9 Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents: 9
diff changeset
417 data: NodeOption::Uninitialised,
962c8e346ab9 Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents: 9
diff changeset
418 aggregator: A::new(),
0
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
419 }
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
420 }
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
421
8
4e09b7829b51 Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents: 5
diff changeset
422 /*
0
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
423 /// Get leaf data
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
424 #[inline]
124
6aa955ad8122 Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents: 97
diff changeset
425 pub(super) fn get_leaf_data(&self, x : &Loc<N, F>) -> Option<&Vec<D>> {
0
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
426 match self.data {
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
427 NodeOption::Uninitialised => None,
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
428 NodeOption::Leaf(ref data) => Some(data),
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
429 NodeOption::Branches(ref b) => b.get_node(x).get_leaf_data(x),
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
430 }
8
4e09b7829b51 Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents: 5
diff changeset
431 }*/
4e09b7829b51 Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents: 5
diff changeset
432
4e09b7829b51 Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents: 5
diff changeset
433 /// Get leaf data iterator
4e09b7829b51 Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents: 5
diff changeset
434 #[inline]
124
6aa955ad8122 Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents: 97
diff changeset
435 pub(super) fn get_leaf_data_iter(&self, x: &Loc<N, F>) -> Option<std::slice::Iter<'_, D>> {
8
4e09b7829b51 Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents: 5
diff changeset
436 match self.data {
4e09b7829b51 Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents: 5
diff changeset
437 NodeOption::Uninitialised => None,
4e09b7829b51 Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents: 5
diff changeset
438 NodeOption::Leaf(ref data) => Some(data.iter()),
4e09b7829b51 Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents: 5
diff changeset
439 NodeOption::Branches(ref b) => b.get_node(x).get_leaf_data_iter(x),
4e09b7829b51 Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents: 5
diff changeset
440 }
0
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
441 }
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
442
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
443 /// Returns a reference to the aggregator of this node
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
444 #[inline]
5
59dc4c5883f4 Improve documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 0
diff changeset
445 pub(super) fn get_aggregator(&self) -> &A {
0
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
446 &self.aggregator
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
447 }
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
448
5
59dc4c5883f4 Improve documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 0
diff changeset
449 /// Insert data under the node.
59dc4c5883f4 Improve documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 0
diff changeset
450 ///
59dc4c5883f4 Improve documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 0
diff changeset
451 /// The parameters are as follows:
59dc4c5883f4 Improve documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 0
diff changeset
452 /// * `domain` is the cube corresponding to this branch.
59dc4c5883f4 Improve documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 0
diff changeset
453 /// * `d` is the data to be inserted
59dc4c5883f4 Improve documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 0
diff changeset
454 /// * `new_leaf_depth` is the depth relative to `self` at which new leaves are created.
59dc4c5883f4 Improve documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 0
diff changeset
455 /// * `support` is the [`Support`] that is used determine with which subcubes of `domain`
59dc4c5883f4 Improve documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 0
diff changeset
456 /// (at subdivision depth `new_leaf_depth`) the data `d` is to be associated with.
59dc4c5883f4 Improve documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 0
diff changeset
457 ///
59dc4c5883f4 Improve documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 0
diff changeset
458 /// If `self` is already [`NodeOption::Leaf`], the data is inserted directly in this node.
59dc4c5883f4 Improve documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 0
diff changeset
459 /// If `self` is a [`NodeOption::Branches`], the data is passed to branches whose subcubes
59dc4c5883f4 Improve documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 0
diff changeset
460 /// `support` intersects. If an [`NodeOption::Uninitialised`] node is encountered, a new leaf is
59dc4c5883f4 Improve documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 0
diff changeset
461 /// created at a minimum depth of `new_leaf_depth`.
124
6aa955ad8122 Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents: 97
diff changeset
462 pub(super) fn insert<'refs, 'scope, M: Depth, S: LocalAnalysis<F, A, N> + Support<N, F>>(
0
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
463 &mut self,
124
6aa955ad8122 Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents: 97
diff changeset
464 domain: &Cube<N, F>,
96
962c8e346ab9 Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents: 9
diff changeset
465 d: D,
962c8e346ab9 Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents: 9
diff changeset
466 new_leaf_depth: M,
962c8e346ab9 Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents: 9
diff changeset
467 support: &S,
962c8e346ab9 Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents: 9
diff changeset
468 task_budget: TaskBudget<'scope, 'refs>,
0
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
469 ) {
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
470 match &mut self.data {
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
471 NodeOption::Uninitialised => {
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
472 // Replace uninitialised node with a leaf or a branch
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
473 self.data = match new_leaf_depth.lower() {
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
474 None => {
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
475 let a = support.local_analysis(&domain);
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
476 self.aggregator.aggregate(once(a));
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
477 // TODO: this is currently a dirty hard-coded heuristic;
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
478 // should add capacity as a parameter
96
962c8e346ab9 Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents: 9
diff changeset
479 let mut vec = Vec::with_capacity(2 * P + 1);
0
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
480 vec.push(d);
8
4e09b7829b51 Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents: 5
diff changeset
481 NodeOption::Leaf(vec)
96
962c8e346ab9 Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents: 9
diff changeset
482 }
0
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
483 Some(lower) => {
8
4e09b7829b51 Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents: 5
diff changeset
484 let b = Arc::new({
0
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
485 let mut b0 = Branches::new_with(domain, support);
8
4e09b7829b51 Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents: 5
diff changeset
486 b0.insert(domain, d, lower, support, task_budget);
0
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
487 b0
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
488 });
8
4e09b7829b51 Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents: 5
diff changeset
489 b.summarise_into(&mut self.aggregator);
0
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
490 NodeOption::Branches(b)
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
491 }
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
492 }
96
962c8e346ab9 Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents: 9
diff changeset
493 }
0
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
494 NodeOption::Leaf(leaf) => {
8
4e09b7829b51 Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents: 5
diff changeset
495 leaf.push(d);
0
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
496 let a = support.local_analysis(&domain);
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
497 self.aggregator.aggregate(once(a));
96
962c8e346ab9 Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents: 9
diff changeset
498 }
0
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
499 NodeOption::Branches(b) => {
8
4e09b7829b51 Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents: 5
diff changeset
500 // FIXME: recursion that may cause stack overflow if the tree becomes
4e09b7829b51 Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents: 5
diff changeset
501 // very deep, e.g. due to [`BTSearch::search_and_refine`].
9
f40dfaf2166d Improvements and minor fixes to bisection tree refinement.
Tuomo Valkonen <tuomov@iki.fi>
parents: 8
diff changeset
502 let bm = Arc::make_mut(b);
f40dfaf2166d Improvements and minor fixes to bisection tree refinement.
Tuomo Valkonen <tuomov@iki.fi>
parents: 8
diff changeset
503 bm.insert(domain, d, new_leaf_depth.lower_or(), support, task_budget);
f40dfaf2166d Improvements and minor fixes to bisection tree refinement.
Tuomo Valkonen <tuomov@iki.fi>
parents: 8
diff changeset
504 bm.summarise_into(&mut self.aggregator);
96
962c8e346ab9 Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents: 9
diff changeset
505 }
0
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
506 }
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
507 }
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
508
5
59dc4c5883f4 Improve documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 0
diff changeset
509 /// Construct a new instance of the node for a different aggregator
59dc4c5883f4 Improve documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 0
diff changeset
510 ///
59dc4c5883f4 Improve documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 0
diff changeset
511 /// The `generator` is used to convert the data of type `D` of the node into corresponding
59dc4c5883f4 Improve documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 0
diff changeset
512 /// [`Support`]s. The `domain` is the cube corresponding to `self`.
59dc4c5883f4 Improve documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 0
diff changeset
513 /// The type parameter `ANew´ is the new aggregator, and needs to be implemented for the
59dc4c5883f4 Improve documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 0
diff changeset
514 /// generator's `SupportType`.
59dc4c5883f4 Improve documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 0
diff changeset
515 pub(super) fn convert_aggregator<ANew, G>(
0
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
516 mut self,
96
962c8e346ab9 Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents: 9
diff changeset
517 generator: &G,
124
6aa955ad8122 Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents: 97
diff changeset
518 domain: &Cube<N, F>,
96
962c8e346ab9 Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents: 9
diff changeset
519 ) -> Node<F, D, ANew, N, P>
962c8e346ab9 Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents: 9
diff changeset
520 where
962c8e346ab9 Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents: 9
diff changeset
521 ANew: Aggregator,
124
6aa955ad8122 Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents: 97
diff changeset
522 G: SupportGenerator<N, F, Id = D>,
96
962c8e346ab9 Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents: 9
diff changeset
523 G::SupportType: LocalAnalysis<F, ANew, N>,
962c8e346ab9 Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents: 9
diff changeset
524 {
0
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
525 // The mem::replace is needed due to the [`Drop`] implementation to extract self.data.
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
526 match std::mem::replace(&mut self.data, NodeOption::Uninitialised) {
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
527 NodeOption::Uninitialised => Node {
96
962c8e346ab9 Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents: 9
diff changeset
528 data: NodeOption::Uninitialised,
962c8e346ab9 Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents: 9
diff changeset
529 aggregator: ANew::new(),
0
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
530 },
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
531 NodeOption::Leaf(v) => {
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
532 let mut anew = ANew::new();
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
533 anew.aggregate(v.iter().map(|d| {
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
534 let support = generator.support_for(*d);
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
535 support.local_analysis(&domain)
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
536 }));
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
537
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
538 Node {
96
962c8e346ab9 Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents: 9
diff changeset
539 data: NodeOption::Leaf(v),
962c8e346ab9 Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents: 9
diff changeset
540 aggregator: anew,
0
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
541 }
96
962c8e346ab9 Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents: 9
diff changeset
542 }
0
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
543 NodeOption::Branches(b) => {
8
4e09b7829b51 Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents: 5
diff changeset
544 // FIXME: recursion that may cause stack overflow if the tree becomes
4e09b7829b51 Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents: 5
diff changeset
545 // very deep, e.g. due to [`BTSearch::search_and_refine`].
9
f40dfaf2166d Improvements and minor fixes to bisection tree refinement.
Tuomo Valkonen <tuomov@iki.fi>
parents: 8
diff changeset
546 let bnew = Arc::unwrap_or_clone(b).convert_aggregator(generator, domain);
0
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
547 let mut anew = ANew::new();
8
4e09b7829b51 Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents: 5
diff changeset
548 bnew.summarise_into(&mut anew);
0
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
549 Node {
96
962c8e346ab9 Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents: 9
diff changeset
550 data: NodeOption::Branches(Arc::new(bnew)),
962c8e346ab9 Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents: 9
diff changeset
551 aggregator: anew,
0
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
552 }
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
553 }
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
554 }
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
555 }
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
556
5
59dc4c5883f4 Improve documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 0
diff changeset
557 /// Refresh aggregator after changes to generator.
59dc4c5883f4 Improve documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 0
diff changeset
558 ///
59dc4c5883f4 Improve documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 0
diff changeset
559 /// The `generator` is used to convert the data of type `D` of the node into corresponding
59dc4c5883f4 Improve documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 0
diff changeset
560 /// [`Support`]s. The `domain` is the cube corresponding to `self`.
8
4e09b7829b51 Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents: 5
diff changeset
561 pub(super) fn refresh_aggregator<'refs, 'scope, G>(
0
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
562 &mut self,
96
962c8e346ab9 Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents: 9
diff changeset
563 generator: &G,
124
6aa955ad8122 Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents: 97
diff changeset
564 domain: &Cube<N, F>,
96
962c8e346ab9 Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents: 9
diff changeset
565 task_budget: TaskBudget<'scope, 'refs>,
962c8e346ab9 Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents: 9
diff changeset
566 ) where
124
6aa955ad8122 Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents: 97
diff changeset
567 G: SupportGenerator<N, F, Id = D>,
96
962c8e346ab9 Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents: 9
diff changeset
568 G::SupportType: LocalAnalysis<F, A, N>,
962c8e346ab9 Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents: 9
diff changeset
569 {
0
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
570 match &mut self.data {
96
962c8e346ab9 Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents: 9
diff changeset
571 NodeOption::Uninitialised => {}
0
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
572 NodeOption::Leaf(v) => {
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
573 self.aggregator = A::new();
96
962c8e346ab9 Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents: 9
diff changeset
574 self.aggregator.aggregate(
962c8e346ab9 Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents: 9
diff changeset
575 v.iter()
962c8e346ab9 Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents: 9
diff changeset
576 .map(|d| generator.support_for(*d).local_analysis(&domain)),
962c8e346ab9 Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents: 9
diff changeset
577 );
962c8e346ab9 Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents: 9
diff changeset
578 }
0
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
579 NodeOption::Branches(ref mut b) => {
8
4e09b7829b51 Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents: 5
diff changeset
580 // FIXME: recursion that may cause stack overflow if the tree becomes
4e09b7829b51 Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents: 5
diff changeset
581 // very deep, e.g. due to [`BTSearch::search_and_refine`].
9
f40dfaf2166d Improvements and minor fixes to bisection tree refinement.
Tuomo Valkonen <tuomov@iki.fi>
parents: 8
diff changeset
582 let bm = Arc::make_mut(b);
f40dfaf2166d Improvements and minor fixes to bisection tree refinement.
Tuomo Valkonen <tuomov@iki.fi>
parents: 8
diff changeset
583 bm.refresh_aggregator(generator, domain, task_budget);
f40dfaf2166d Improvements and minor fixes to bisection tree refinement.
Tuomo Valkonen <tuomov@iki.fi>
parents: 8
diff changeset
584 bm.summarise_into(&mut self.aggregator);
0
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
585 }
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
586 }
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
587 }
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
588 }
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
589
5
59dc4c5883f4 Improve documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 0
diff changeset
590 /// Helper trait for working with [`Node`]s without the knowledge of `P`.
59dc4c5883f4 Improve documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 0
diff changeset
591 ///
59dc4c5883f4 Improve documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 0
diff changeset
592 /// This can be removed and the methods implemented directly on [`BT`] once Rust's const generics
59dc4c5883f4 Improve documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 0
diff changeset
593 /// are flexible enough to allow fixing `P=pow(2, N)`.
96
962c8e346ab9 Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents: 9
diff changeset
594 pub trait BTNode<F, D, A, const N: usize>
962c8e346ab9 Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents: 9
diff changeset
595 where
962c8e346ab9 Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents: 9
diff changeset
596 F: Float,
962c8e346ab9 Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents: 9
diff changeset
597 D: 'static + Copy,
962c8e346ab9 Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents: 9
diff changeset
598 A: Aggregator,
962c8e346ab9 Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents: 9
diff changeset
599 {
962c8e346ab9 Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents: 9
diff changeset
600 type Node: Clone + std::fmt::Debug;
0
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
601 }
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
602
5
59dc4c5883f4 Improve documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 0
diff changeset
603 /// Helper structure for looking up a [`Node`] without the knowledge of `P`.
59dc4c5883f4 Improve documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 0
diff changeset
604 ///
59dc4c5883f4 Improve documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 0
diff changeset
605 /// This can be removed once Rust's const generics are flexible enough to allow fixing
59dc4c5883f4 Improve documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 0
diff changeset
606 /// `P=pow(2, N)`.
0
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
607 #[derive(Debug)]
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
608 pub struct BTNodeLookup;
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
609
5
59dc4c5883f4 Improve documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 0
diff changeset
610 /// Basic interface to a [`BT`] bisection tree.
59dc4c5883f4 Improve documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 0
diff changeset
611 ///
59dc4c5883f4 Improve documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 0
diff changeset
612 /// Further routines are provided by the [`BTSearch`][super::refine::BTSearch] trait.
124
6aa955ad8122 Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents: 97
diff changeset
613 pub trait BTImpl<const N: usize, F: Float = f64>:
96
962c8e346ab9 Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents: 9
diff changeset
614 std::fmt::Debug + Clone + GlobalAnalysis<F, Self::Agg>
962c8e346ab9 Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents: 9
diff changeset
615 {
5
59dc4c5883f4 Improve documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 0
diff changeset
616 /// The data type stored in the tree
96
962c8e346ab9 Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents: 9
diff changeset
617 type Data: 'static + Copy + Send + Sync;
5
59dc4c5883f4 Improve documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 0
diff changeset
618 /// The depth type of the tree
96
962c8e346ab9 Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents: 9
diff changeset
619 type Depth: Depth;
5
59dc4c5883f4 Improve documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 0
diff changeset
620 /// The type for the [aggregate information][Aggregator] about the `Data` stored in each node
59dc4c5883f4 Improve documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 0
diff changeset
621 /// of the tree.
96
962c8e346ab9 Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents: 9
diff changeset
622 type Agg: Aggregator;
5
59dc4c5883f4 Improve documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 0
diff changeset
623 /// The type of the tree with the aggregator converted to `ANew`.
124
6aa955ad8122 Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents: 97
diff changeset
624 type Converted<ANew>: BTImpl<N, F, Data = Self::Data, Agg = ANew>
96
962c8e346ab9 Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents: 9
diff changeset
625 where
962c8e346ab9 Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents: 9
diff changeset
626 ANew: Aggregator;
0
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
627
5
59dc4c5883f4 Improve documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 0
diff changeset
628 /// Insert the data `d` into the tree for `support`.
59dc4c5883f4 Improve documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 0
diff changeset
629 ///
59dc4c5883f4 Improve documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 0
diff changeset
630 /// Every leaf node of the tree that intersects the `support` will contain a copy of
59dc4c5883f4 Improve documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 0
diff changeset
631 /// `d`.
124
6aa955ad8122 Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents: 97
diff changeset
632 fn insert<S: LocalAnalysis<F, Self::Agg, N> + Support<N, F>>(
97
4e80fb049dca MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents: 96
diff changeset
633 &mut self,
4e80fb049dca MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents: 96
diff changeset
634 d: Self::Data,
4e80fb049dca MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents: 96
diff changeset
635 support: &S,
4e80fb049dca MinMaxMapping trait to allow alternatives to BTFN with relevant properties.
Tuomo Valkonen <tuomov@iki.fi>
parents: 96
diff changeset
636 );
0
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
637
5
59dc4c5883f4 Improve documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 0
diff changeset
638 /// Construct a new instance of the tree for a different aggregator
59dc4c5883f4 Improve documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 0
diff changeset
639 ///
59dc4c5883f4 Improve documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 0
diff changeset
640 /// The `generator` is used to convert the data of type [`Self::Data`] contained in the tree
59dc4c5883f4 Improve documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 0
diff changeset
641 /// into corresponding [`Support`]s.
96
962c8e346ab9 Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents: 9
diff changeset
642 fn convert_aggregator<ANew, G>(self, generator: &G) -> Self::Converted<ANew>
962c8e346ab9 Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents: 9
diff changeset
643 where
962c8e346ab9 Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents: 9
diff changeset
644 ANew: Aggregator,
124
6aa955ad8122 Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents: 97
diff changeset
645 G: SupportGenerator<N, F, Id = Self::Data>,
96
962c8e346ab9 Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents: 9
diff changeset
646 G::SupportType: LocalAnalysis<F, ANew, N>;
0
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
647
5
59dc4c5883f4 Improve documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 0
diff changeset
648 /// Refreshes the aggregator of the three after possible changes to the support generator.
59dc4c5883f4 Improve documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 0
diff changeset
649 ///
59dc4c5883f4 Improve documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 0
diff changeset
650 /// The `generator` is used to convert the data of type [`Self::Data`] contained in the tree
59dc4c5883f4 Improve documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 0
diff changeset
651 /// into corresponding [`Support`]s.
96
962c8e346ab9 Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents: 9
diff changeset
652 fn refresh_aggregator<G>(&mut self, generator: &G)
962c8e346ab9 Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents: 9
diff changeset
653 where
124
6aa955ad8122 Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents: 97
diff changeset
654 G: SupportGenerator<N, F, Id = Self::Data>,
96
962c8e346ab9 Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents: 9
diff changeset
655 G::SupportType: LocalAnalysis<F, Self::Agg, N>;
0
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
656
8
4e09b7829b51 Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents: 5
diff changeset
657 /// Returns an iterator over all [`Self::Data`] items at the point `x` of the domain.
124
6aa955ad8122 Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents: 97
diff changeset
658 fn iter_at(&self, x: &Loc<N, F>) -> std::slice::Iter<'_, Self::Data>;
8
4e09b7829b51 Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents: 5
diff changeset
659
4e09b7829b51 Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents: 5
diff changeset
660 /*
4e09b7829b51 Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents: 5
diff changeset
661 /// Returns all [`Self::Data`] items at the point `x` of the domain.
124
6aa955ad8122 Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents: 97
diff changeset
662 fn data_at(&self, x : &Loc<N, F>) -> Arc<Vec<Self::Data>>;
8
4e09b7829b51 Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents: 5
diff changeset
663 */
0
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
664
5
59dc4c5883f4 Improve documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 0
diff changeset
665 /// Create a new tree on `domain` of indicated `depth`.
124
6aa955ad8122 Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents: 97
diff changeset
666 fn new(domain: Cube<N, F>, depth: Self::Depth) -> Self;
0
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
667 }
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
668
5
59dc4c5883f4 Improve documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 0
diff changeset
669 /// The main bisection tree structure.
59dc4c5883f4 Improve documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 0
diff changeset
670 ///
59dc4c5883f4 Improve documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 0
diff changeset
671 /// It should be accessed via the [`BTImpl`] trait to hide the `const P : usize` parameter until
59dc4c5883f4 Improve documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 0
diff changeset
672 /// const generics are flexible enough to fix `P=pow(2, N)` and thus also get rid of
59dc4c5883f4 Improve documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 0
diff changeset
673 /// the `BTNodeLookup : BTNode<F, D, A, N>` trait bound.
96
962c8e346ab9 Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents: 9
diff changeset
674 #[derive(Clone, Debug)]
962c8e346ab9 Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents: 9
diff changeset
675 pub struct BT<M: Depth, F: Float, D: 'static + Copy, A: Aggregator, const N: usize>
962c8e346ab9 Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents: 9
diff changeset
676 where
962c8e346ab9 Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents: 9
diff changeset
677 BTNodeLookup: BTNode<F, D, A, N>,
962c8e346ab9 Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents: 9
diff changeset
678 {
5
59dc4c5883f4 Improve documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 0
diff changeset
679 /// The depth of the tree (initial, before refinement)
96
962c8e346ab9 Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents: 9
diff changeset
680 pub(super) depth: M,
5
59dc4c5883f4 Improve documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 0
diff changeset
681 /// The domain of the toplevel node
124
6aa955ad8122 Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents: 97
diff changeset
682 pub(super) domain: Cube<N, F>,
5
59dc4c5883f4 Improve documentation
Tuomo Valkonen <tuomov@iki.fi>
parents: 0
diff changeset
683 /// The toplevel node of the tree
96
962c8e346ab9 Allow Zed to reindent btfn.rs, support.rs, aggregator.rs, and bt.rs.
Tuomo Valkonen <tuomov@iki.fi>
parents: 9
diff changeset
684 pub(super) topnode: <BTNodeLookup as BTNode<F, D, A, N>>::Node,
0
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
685 }
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
686
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
687 macro_rules! impl_bt {
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
688 ($($n:literal)*) => { $(
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
689 impl<F, D, A> BTNode<F, D, A, $n> for BTNodeLookup
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
690 where F : Float,
8
4e09b7829b51 Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents: 5
diff changeset
691 D : 'static + Copy + Send + Sync + std::fmt::Debug,
0
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
692 A : Aggregator {
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
693 type Node = Node<F,D,A,$n,{pow(2, $n)}>;
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
694 }
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
695
124
6aa955ad8122 Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents: 97
diff changeset
696 impl<M,F,D,A> BTImpl<$n, F> for BT<M,F,D,A,$n>
0
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
697 where M : Depth,
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
698 F : Float,
8
4e09b7829b51 Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents: 5
diff changeset
699 D : 'static + Copy + Send + Sync + std::fmt::Debug,
0
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
700 A : Aggregator {
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
701 type Data = D;
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
702 type Depth = M;
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
703 type Agg = A;
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
704 type Converted<ANew> = BT<M,F,D,ANew,$n> where ANew : Aggregator;
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
705
124
6aa955ad8122 Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents: 97
diff changeset
706 fn insert<S: LocalAnalysis<F, A, $n> + Support< $n, F>>(
0
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
707 &mut self,
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
708 d : D,
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
709 support : &S
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
710 ) {
8
4e09b7829b51 Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents: 5
diff changeset
711 with_task_budget(|task_budget|
4e09b7829b51 Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents: 5
diff changeset
712 self.topnode.insert(
4e09b7829b51 Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents: 5
diff changeset
713 &self.domain,
4e09b7829b51 Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents: 5
diff changeset
714 d,
4e09b7829b51 Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents: 5
diff changeset
715 self.depth,
4e09b7829b51 Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents: 5
diff changeset
716 support,
4e09b7829b51 Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents: 5
diff changeset
717 task_budget
4e09b7829b51 Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents: 5
diff changeset
718 )
4e09b7829b51 Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents: 5
diff changeset
719 )
0
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
720 }
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
721
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
722 fn convert_aggregator<ANew, G>(self, generator : &G) -> Self::Converted<ANew>
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
723 where ANew : Aggregator,
124
6aa955ad8122 Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents: 97
diff changeset
724 G : SupportGenerator< $n, F, Id=D>,
0
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
725 G::SupportType : LocalAnalysis<F, ANew, $n> {
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
726 let topnode = self.topnode.convert_aggregator(generator, &self.domain);
8
4e09b7829b51 Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents: 5
diff changeset
727
0
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
728 BT {
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
729 depth : self.depth,
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
730 domain : self.domain,
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
731 topnode
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
732 }
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
733 }
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
734
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
735 fn refresh_aggregator<G>(&mut self, generator : &G)
124
6aa955ad8122 Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents: 97
diff changeset
736 where G : SupportGenerator< $n, F, Id=Self::Data>,
0
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
737 G::SupportType : LocalAnalysis<F, Self::Agg, $n> {
8
4e09b7829b51 Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents: 5
diff changeset
738 with_task_budget(|task_budget|
4e09b7829b51 Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents: 5
diff changeset
739 self.topnode.refresh_aggregator(generator, &self.domain, task_budget)
4e09b7829b51 Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents: 5
diff changeset
740 )
0
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
741 }
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
742
124
6aa955ad8122 Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents: 97
diff changeset
743 /*fn data_at(&self, x : &Loc<$n, F>) -> Arc<Vec<D>> {
8
4e09b7829b51 Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents: 5
diff changeset
744 self.topnode.get_leaf_data(x).unwrap_or_else(|| Arc::new(Vec::new()))
4e09b7829b51 Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents: 5
diff changeset
745 }*/
4e09b7829b51 Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents: 5
diff changeset
746
124
6aa955ad8122 Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents: 97
diff changeset
747 fn iter_at(&self, x : &Loc<$n, F>) -> std::slice::Iter<'_, D> {
8
4e09b7829b51 Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents: 5
diff changeset
748 self.topnode.get_leaf_data_iter(x).unwrap_or_else(|| [].iter())
0
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
749 }
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
750
124
6aa955ad8122 Transpose loc parameters to allow f64 defaults
Tuomo Valkonen <tuomov@iki.fi>
parents: 97
diff changeset
751 fn new(domain : Cube<$n, F>, depth : M) -> Self {
0
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
752 BT {
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
753 depth : depth,
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
754 domain : domain,
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
755 topnode : Node::new(),
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
756 }
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
757 }
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
758 }
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
759
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
760 impl<M,F,D,A> GlobalAnalysis<F,A> for BT<M,F,D,A,$n>
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
761 where M : Depth,
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
762 F : Float,
8
4e09b7829b51 Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents: 5
diff changeset
763 D : 'static + Copy + Send + Sync + std::fmt::Debug,
0
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
764 A : Aggregator {
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
765 fn global_analysis(&self) -> A {
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
766 self.topnode.get_aggregator().clone()
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
767 }
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
768 }
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
769 )* }
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
770 }
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
771
9f27689eb130 Initialise new clean repository
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
772 impl_bt!(1 2 3 4);

mercurial