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