Mon, 24 Oct 2022 09:41:43 +0300
Allow step closure of AlgIterators to indicate succesfull termination or failure.
0 | 1 | |
2 | use std::slice::{Iter,IterMut}; | |
3 | use std::iter::once; | |
4 | use std::rc::Rc; | |
5 | use serde::{Serialize, Deserialize}; | |
6 | pub use nalgebra::Const; | |
7 | use itertools::izip; | |
8 | ||
9 | use crate::iter::{MapF,Mappable}; | |
10 | use crate::types::{Float, Num}; | |
11 | use crate::coefficients::pow; | |
12 | use crate::maputil::{ | |
13 | array_init, | |
14 | map2, | |
15 | map2_indexed, | |
16 | collect_into_array_unchecked | |
17 | }; | |
18 | pub use crate::sets::Cube; | |
19 | pub use crate::loc::Loc; | |
20 | use super::support::*; | |
21 | use super::aggregator::*; | |
22 | ||
23 | #[derive(Clone,Debug)] | |
24 | pub enum NodeOption<F : Num, D, A : Aggregator, const N : usize, const P : usize> { | |
25 | // TODO: Could optimise Uninitialised away by simply treat Leaf with an empty Vec as | |
26 | // something that can be still replaced with Branches. | |
27 | Uninitialised, | |
28 | // TODO: replace with QuickVec fast and w/o allocs on single elements. | |
29 | Leaf(Rc<Vec<D>>), | |
30 | Branches(Rc<Branches<F, D, A, N, P>>), | |
31 | } | |
32 | ||
33 | /// Node of a [`BT`] bisection tree. | |
34 | #[derive(Clone,Debug)] | |
35 | pub struct Node<F : Num, D, A : Aggregator, const N : usize, const P : usize> { | |
36 | pub(super) data : NodeOption<F, D, A, N, P>, | |
37 | /// Aggregator for `data`. | |
38 | pub(super) aggregator : A, | |
39 | } | |
40 | ||
41 | /// Branch information of a [`Node`] of a [`BT`] bisection tree. | |
42 | #[derive(Clone,Debug)] | |
43 | pub struct Branches<F : Num, D, A : Aggregator, const N : usize, const P : usize> { | |
44 | /// Point for subdivision of the (unstored) [`Cube`] corresponding to the node. | |
45 | pub(super) branch_at : Loc<F, N>, | |
46 | /// Subnodes | |
47 | pub(super) nodes : [Node<F, D, A, N, P>; P], | |
48 | } | |
49 | ||
50 | /// Dirty workaround to broken Rust drop, see [https://github.com/rust-lang/rust/issues/58068](). | |
51 | impl<F : Num, D, A : Aggregator, const N : usize, const P : usize> | |
52 | Drop for Node<F, D, A, N, P> { | |
53 | fn drop(&mut self) { | |
54 | use NodeOption as NO; | |
55 | ||
56 | let process = |brc : Rc<Branches<F, D, A, N, P>>, | |
57 | to_drop : &mut Vec<Rc<Branches<F, D, A, N, P>>>| { | |
58 | // We only drop Branches if we have the only strong reference. | |
59 | Rc::try_unwrap(brc).ok().map(|branches| branches.nodes.map(|mut node| { | |
60 | if let NO::Branches(brc2) = std::mem::replace(&mut node.data, NO::Uninitialised) { | |
61 | to_drop.push(brc2) | |
62 | } | |
63 | })); | |
64 | }; | |
65 | ||
66 | // We mark Self as NodeOption::Uninitialised, extracting the real contents. | |
67 | // If we have subprocess, we need to process them. | |
68 | if let NO::Branches(brc1) = std::mem::replace(&mut self.data, NO::Uninitialised) { | |
69 | // We store a queue of Rc<Branches> to drop into a vector | |
70 | let mut to_drop = Vec::new(); | |
71 | process(brc1, &mut to_drop); | |
72 | ||
73 | // While there are any Branches in the drop queue vector, we continue the process, | |
74 | // pushing all internal branching nodes into the queue. | |
75 | while let Some(brc) = to_drop.pop() { | |
76 | process(brc, &mut to_drop) | |
77 | } | |
78 | } | |
79 | } | |
80 | } | |
81 | ||
82 | pub trait Depth : 'static + Copy + std::fmt::Debug { | |
83 | type Lower : Depth; | |
84 | fn lower(&self) -> Option<Self::Lower>; | |
85 | fn lower_or(&self) -> Self::Lower; | |
86 | } | |
87 | ||
88 | #[derive(Copy,Clone,Debug,Serialize,Deserialize)] | |
89 | pub struct DynamicDepth(pub u8); | |
90 | impl Depth for DynamicDepth { | |
91 | type Lower = Self; | |
92 | #[inline] | |
93 | fn lower(&self) -> Option<Self> { | |
94 | if self.0>0 { | |
95 | Some(DynamicDepth(self.0-1)) | |
96 | } else { | |
97 | None | |
98 | } | |
99 | } | |
100 | #[inline] | |
101 | fn lower_or(&self) -> Self { | |
102 | DynamicDepth(if self.0>0 { self.0 - 1 } else { 0 }) | |
103 | } | |
104 | } | |
105 | ||
106 | impl Depth for Const<0> { | |
107 | type Lower = Self; | |
108 | fn lower(&self) -> Option<Self::Lower> { None } | |
109 | fn lower_or(&self) -> Self::Lower { Const } | |
110 | } | |
111 | ||
112 | macro_rules! impl_constdepth { | |
113 | ($($n:literal)*) => { $( | |
114 | impl Depth for Const<$n> { | |
115 | type Lower = Const<{$n-1}>; | |
116 | fn lower(&self) -> Option<Self::Lower> { Some(Const) } | |
117 | fn lower_or(&self) -> Self::Lower { Const } | |
118 | } | |
119 | )* }; | |
120 | } | |
121 | 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); | |
122 | ||
123 | ||
124 | pub trait BranchCount<const N : usize> {} | |
125 | macro_rules! impl_branchcount { | |
126 | ($($n:literal)*) => { $( | |
127 | impl BranchCount<$n> for Const<{pow(2, $n)}>{} | |
128 | )* } | |
129 | } | |
130 | impl_branchcount!(1 2 3 4 5 6 7 8); | |
131 | ||
132 | impl<F : Float, D, A, const N : usize, const P : usize> Branches<F,D,A,N,P> | |
133 | where Const<P> : BranchCount<N>, | |
134 | A : Aggregator | |
135 | { | |
136 | fn get_node_index(&self, x : &Loc<F, N>) -> usize { | |
137 | izip!(0..P, x.iter(), self.branch_at.iter()).map(|(i, x_i, branch_i)| | |
138 | if x_i > branch_i { 1<<i } else { 0 } | |
139 | ).sum() | |
140 | } | |
141 | ||
142 | #[inline] | |
143 | pub fn get_node(&self, x : &Loc<F,N>) -> &Node<F,D,A,N,P> { | |
144 | &self.nodes[self.get_node_index(x)] | |
145 | } | |
146 | } | |
147 | ||
148 | ||
149 | pub struct BTIter<'a, D> { | |
150 | iter : Iter<'a, D>, | |
151 | } | |
152 | ||
153 | pub struct SubcubeIter<'b, F : Float, const N : usize, const P : usize> { | |
154 | domain : &'b Cube<F, N>, | |
155 | branch_at : Loc<F, N>, | |
156 | index : usize, | |
157 | } | |
158 | ||
159 | #[inline] | |
160 | fn get_subcube<F : Float, const N : usize>(branch_at : &Loc<F, N>, domain : &Cube<F, N>, i : usize) -> Cube<F, N> { | |
161 | map2_indexed(branch_at, domain, move |j, &branch, &[start, end]| { | |
162 | if i & (1 << j) != 0 { | |
163 | [branch, end] | |
164 | } else { | |
165 | [start, branch] | |
166 | } | |
167 | }).into() | |
168 | } | |
169 | ||
170 | impl<'a, 'b, F : Float, const N : usize, const P : usize> Iterator | |
171 | for SubcubeIter<'b, F, N, P> { | |
172 | type Item = Cube<F, N>; | |
173 | #[inline] | |
174 | fn next(&mut self) -> Option<Self::Item> { | |
175 | if self.index < P { | |
176 | let i = self.index; | |
177 | self.index += 1; | |
178 | Some(get_subcube(&self.branch_at, self.domain, i)) | |
179 | } else { | |
180 | None | |
181 | } | |
182 | } | |
183 | } | |
184 | ||
185 | impl<F : Float, D : Copy, A, const N : usize, const P : usize> | |
186 | Branches<F,D,A,N,P> | |
187 | where Const<P> : BranchCount<N>, | |
188 | A : Aggregator { | |
189 | ||
190 | pub fn new_with<S : LocalAnalysis <F, A, N>>( | |
191 | domain : &Cube<F,N>, | |
192 | support : &S | |
193 | ) -> Self { | |
194 | let hint = support.bisection_hint(domain); | |
195 | let branch_at = map2(&hint, domain, |h, r| { | |
196 | h.unwrap_or_else(|| (r[0]+r[1])/F::TWO).max(r[0]).min(r[1]) | |
197 | }).into(); | |
198 | Branches{ | |
199 | branch_at : branch_at, | |
200 | nodes : array_init(|| Node::new()), | |
201 | } | |
202 | } | |
203 | ||
204 | /// Get an iterator over the aggregators of the nodes of this branch head. | |
205 | #[inline] | |
206 | pub fn aggregators(&self) -> MapF<Iter<'_, Node<F,D,A,N,P>>, &'_ A> { | |
207 | self.nodes.iter().mapF(Node::get_aggregator) | |
208 | } | |
209 | ||
210 | #[inline] | |
211 | pub fn iter_subcubes<'b>(&self, domain : &'b Cube<F, N>) | |
212 | -> SubcubeIter<'b, F, N, P> { | |
213 | SubcubeIter { | |
214 | domain : domain, | |
215 | branch_at : self.branch_at, | |
216 | index : 0, | |
217 | } | |
218 | } | |
219 | ||
220 | /// Iterate over all nodes and corresponding subcubes of self. | |
221 | #[inline] | |
222 | pub fn nodes_and_cubes<'a, 'b>(&'a self, domain : &'b Cube<F, N>) | |
223 | -> std::iter::Zip<Iter<'a, Node<F,D,A,N,P>>, SubcubeIter<'b, F, N, P>> { | |
224 | self.nodes.iter().zip(self.iter_subcubes(domain)) | |
225 | } | |
226 | ||
227 | /// Mutably iterate over all nodes and corresponding subcubes of self. | |
228 | #[inline] | |
229 | pub fn nodes_and_cubes_mut<'a, 'b>(&'a mut self, domain : &'b Cube<F, N>) | |
230 | -> std::iter::Zip<IterMut<'a, Node<F,D,A,N,P>>, SubcubeIter<'b, F, N, P>> { | |
231 | let subcube_iter = self.iter_subcubes(domain); | |
232 | self.nodes.iter_mut().zip(subcube_iter) | |
233 | } | |
234 | ||
235 | /// Insert data into the branch. | |
236 | pub fn insert<M : Depth, S : LocalAnalysis<F, A, N>>( | |
237 | &mut self, | |
238 | domain : &Cube<F,N>, | |
239 | d : D, | |
240 | new_leaf_depth : M, | |
241 | support : &S | |
242 | ) { | |
243 | let support_hint = support.support_hint(); | |
244 | for (node, subcube) in self.nodes_and_cubes_mut(&domain) { | |
245 | if support_hint.intersects(&subcube) { | |
246 | node.insert( | |
247 | &subcube, | |
248 | d, | |
249 | new_leaf_depth, | |
250 | support | |
251 | ); | |
252 | } | |
253 | } | |
254 | } | |
255 | ||
256 | /// Construct a new instance for a different aggregator | |
257 | pub fn convert_aggregator<ANew, G>( | |
258 | self, | |
259 | generator : &G, | |
260 | domain : &Cube<F, N> | |
261 | ) -> Branches<F,D,ANew,N,P> | |
262 | where ANew : Aggregator, | |
263 | G : SupportGenerator<F, N, Id=D>, | |
264 | G::SupportType : LocalAnalysis<F, ANew, N> { | |
265 | let branch_at = self.branch_at; | |
266 | let subcube_iter = self.iter_subcubes(domain); | |
267 | let new_nodes = self.nodes.into_iter().zip(subcube_iter).map(|(node, subcube)| { | |
268 | // TODO: avoid clone | |
269 | node.convert_aggregator(generator, &subcube) | |
270 | }); | |
271 | Branches { | |
272 | branch_at : branch_at, | |
273 | nodes : collect_into_array_unchecked(new_nodes), | |
274 | } | |
275 | } | |
276 | ||
277 | /// Recalculate aggregator after changes to generator | |
278 | pub fn refresh_aggregator<G>( | |
279 | &mut self, | |
280 | generator : &G, | |
281 | domain : &Cube<F, N> | |
282 | ) where G : SupportGenerator<F, N, Id=D>, | |
283 | G::SupportType : LocalAnalysis<F, A, N> { | |
284 | for (node, subcube) in self.nodes_and_cubes_mut(domain) { | |
285 | node.refresh_aggregator(generator, &subcube) | |
286 | } | |
287 | } | |
288 | } | |
289 | ||
290 | impl<F : Float, D : Copy, A, const N : usize, const P : usize> | |
291 | Node<F,D,A,N,P> | |
292 | where Const<P> : BranchCount<N>, | |
293 | A : Aggregator { | |
294 | ||
295 | #[inline] | |
296 | pub fn new() -> Self { | |
297 | Node { | |
298 | data : NodeOption::Uninitialised, | |
299 | aggregator : A::new(), | |
300 | } | |
301 | } | |
302 | ||
303 | /// Get leaf data | |
304 | #[inline] | |
305 | pub fn get_leaf_data(&self, x : &Loc<F, N>) -> Option<&Vec<D>> { | |
306 | match self.data { | |
307 | NodeOption::Uninitialised => None, | |
308 | NodeOption::Leaf(ref data) => Some(data), | |
309 | NodeOption::Branches(ref b) => b.get_node(x).get_leaf_data(x), | |
310 | } | |
311 | } | |
312 | ||
313 | /// Returns a reference to the aggregator of this node | |
314 | #[inline] | |
315 | pub fn get_aggregator(&self) -> &A { | |
316 | &self.aggregator | |
317 | } | |
318 | ||
319 | /// Insert `d` into the tree. If an `Incomplete` node is encountered, a new | |
320 | /// leaf is created at a minimum depth of `new_leaf_depth` | |
321 | pub fn insert<M : Depth, S : LocalAnalysis <F, A, N>>( | |
322 | &mut self, | |
323 | domain : &Cube<F,N>, | |
324 | d : D, | |
325 | new_leaf_depth : M, | |
326 | support : &S | |
327 | ) { | |
328 | match &mut self.data { | |
329 | NodeOption::Uninitialised => { | |
330 | // Replace uninitialised node with a leaf or a branch | |
331 | self.data = match new_leaf_depth.lower() { | |
332 | None => { | |
333 | let a = support.local_analysis(&domain); | |
334 | self.aggregator.aggregate(once(a)); | |
335 | // TODO: this is currently a dirty hard-coded heuristic; | |
336 | // should add capacity as a parameter | |
337 | let mut vec = Vec::with_capacity(2*P+1); | |
338 | vec.push(d); | |
339 | NodeOption::Leaf(Rc::new(vec)) | |
340 | }, | |
341 | Some(lower) => { | |
342 | let b = Rc::new({ | |
343 | let mut b0 = Branches::new_with(domain, support); | |
344 | b0.insert(domain, d, lower, support); | |
345 | b0 | |
346 | }); | |
347 | self.aggregator.summarise(b.aggregators()); | |
348 | NodeOption::Branches(b) | |
349 | } | |
350 | } | |
351 | }, | |
352 | NodeOption::Leaf(leaf) => { | |
353 | Rc::make_mut(leaf).push(d); | |
354 | let a = support.local_analysis(&domain); | |
355 | self.aggregator.aggregate(once(a)); | |
356 | }, | |
357 | NodeOption::Branches(b) => { | |
358 | Rc::make_mut(b).insert(domain, d, new_leaf_depth.lower_or(), support); | |
359 | self.aggregator.summarise(b.aggregators()); | |
360 | }, | |
361 | } | |
362 | } | |
363 | ||
364 | /// Construct a new instance for a different aggregator | |
365 | pub fn convert_aggregator<ANew, G>( | |
366 | mut self, | |
367 | generator : &G, | |
368 | domain : &Cube<F, N> | |
369 | ) -> Node<F,D,ANew,N,P> | |
370 | where ANew : Aggregator, | |
371 | G : SupportGenerator<F, N, Id=D>, | |
372 | G::SupportType : LocalAnalysis<F, ANew, N> { | |
373 | ||
374 | // The mem::replace is needed due to the [`Drop`] implementation to extract self.data. | |
375 | match std::mem::replace(&mut self.data, NodeOption::Uninitialised) { | |
376 | NodeOption::Uninitialised => Node { | |
377 | data : NodeOption::Uninitialised, | |
378 | aggregator : ANew::new(), | |
379 | }, | |
380 | NodeOption::Leaf(v) => { | |
381 | let mut anew = ANew::new(); | |
382 | anew.aggregate(v.iter().map(|d| { | |
383 | let support = generator.support_for(*d); | |
384 | support.local_analysis(&domain) | |
385 | })); | |
386 | ||
387 | Node { | |
388 | data : NodeOption::Leaf(v), | |
389 | aggregator : anew, | |
390 | } | |
391 | }, | |
392 | NodeOption::Branches(b) => { | |
393 | // TODO: now with Rc, convert_aggregator should be reference-based. | |
394 | let bnew = Rc::new(Rc::unwrap_or_clone(b).convert_aggregator(generator, domain)); | |
395 | let mut anew = ANew::new(); | |
396 | anew.summarise(bnew.aggregators()); | |
397 | Node { | |
398 | data : NodeOption::Branches(bnew), | |
399 | aggregator : anew, | |
400 | } | |
401 | } | |
402 | } | |
403 | } | |
404 | ||
405 | /// Refresh aggregator after changes to generator | |
406 | pub fn refresh_aggregator<G>( | |
407 | &mut self, | |
408 | generator : &G, | |
409 | domain : &Cube<F, N> | |
410 | ) where G : SupportGenerator<F, N, Id=D>, | |
411 | G::SupportType : LocalAnalysis<F, A, N> { | |
412 | match &mut self.data { | |
413 | NodeOption::Uninitialised => { }, | |
414 | NodeOption::Leaf(v) => { | |
415 | self.aggregator = A::new(); | |
416 | self.aggregator.aggregate(v.iter().map(|d| { | |
417 | generator.support_for(*d) | |
418 | .local_analysis(&domain) | |
419 | })); | |
420 | }, | |
421 | NodeOption::Branches(ref mut b) => { | |
422 | // TODO: now with Rc, convert_aggregator should be reference-based. | |
423 | Rc::make_mut(b).refresh_aggregator(generator, domain); | |
424 | self.aggregator.summarise(b.aggregators()); | |
425 | } | |
426 | } | |
427 | } | |
428 | } | |
429 | ||
430 | impl<'a, D> Iterator for BTIter<'a,D> { | |
431 | type Item = &'a D; | |
432 | #[inline] | |
433 | fn next(&mut self) -> Option<&'a D> { | |
434 | self.iter.next() | |
435 | } | |
436 | } | |
437 | ||
438 | ||
439 | // | |
440 | // BT | |
441 | // | |
442 | ||
443 | /// Internal structure to hide the `const P : usize` parameter of [`Node`] until | |
444 | /// const generics are flexible enough to fix `P=pow(2, N)`. | |
445 | pub trait BTNode<F, D, A, const N : usize> | |
446 | where F : Float, | |
447 | D : 'static + Copy, | |
448 | A : Aggregator { | |
449 | type Node : Clone + std::fmt::Debug; | |
450 | } | |
451 | ||
452 | #[derive(Debug)] | |
453 | pub struct BTNodeLookup; | |
454 | ||
455 | /// Interface to a [`BT`] bisection tree. | |
456 | pub trait BTImpl<F : Float, const N : usize> : std::fmt::Debug + Clone + GlobalAnalysis<F, Self::Agg> { | |
457 | type Data : 'static + Copy; | |
458 | type Depth : Depth; | |
459 | type Agg : Aggregator; | |
460 | type Converted<ANew> : BTImpl<F, N, Data=Self::Data, Agg=ANew> where ANew : Aggregator; | |
461 | ||
462 | /// Insert d into the `BisectionTree`. | |
463 | fn insert<S : LocalAnalysis<F, Self::Agg, N>>( | |
464 | &mut self, | |
465 | d : Self::Data, | |
466 | support : &S | |
467 | ); | |
468 | ||
469 | /// Construct a new instance for a different aggregator | |
470 | fn convert_aggregator<ANew, G>(self, generator : &G) | |
471 | -> Self::Converted<ANew> | |
472 | where ANew : Aggregator, | |
473 | G : SupportGenerator<F, N, Id=Self::Data>, | |
474 | G::SupportType : LocalAnalysis<F, ANew, N>; | |
475 | ||
476 | ||
477 | /// Refresh aggregator after changes to generator | |
478 | fn refresh_aggregator<G>(&mut self, generator : &G) | |
479 | where G : SupportGenerator<F, N, Id=Self::Data>, | |
480 | G::SupportType : LocalAnalysis<F, Self::Agg, N>; | |
481 | ||
482 | /// Iterarate items at x | |
483 | fn iter_at<'a>(&'a self, x : &'a Loc<F,N>) -> BTIter<'a, Self::Data>; | |
484 | ||
485 | /// Create a new instance | |
486 | fn new(domain : Cube<F, N>, depth : Self::Depth) -> Self; | |
487 | } | |
488 | ||
489 | /// The main bisection tree structure. The interface operations are via [`BTImpl`] | |
490 | /// to hide the `const P : usize` parameter until const generics are flexible enough | |
491 | /// to fix `P=pow(2, N)`. | |
492 | #[derive(Clone,Debug)] | |
493 | pub struct BT< | |
494 | M : Depth, | |
495 | F : Float, | |
496 | D : 'static + Copy, | |
497 | A : Aggregator, | |
498 | const N : usize, | |
499 | > where BTNodeLookup : BTNode<F, D, A, N> { | |
500 | pub(super) depth : M, | |
501 | pub(super) domain : Cube<F, N>, | |
502 | pub(super) topnode : <BTNodeLookup as BTNode<F, D, A, N>>::Node, | |
503 | } | |
504 | ||
505 | macro_rules! impl_bt { | |
506 | ($($n:literal)*) => { $( | |
507 | impl<F, D, A> BTNode<F, D, A, $n> for BTNodeLookup | |
508 | where F : Float, | |
509 | D : 'static + Copy + std::fmt::Debug, | |
510 | A : Aggregator { | |
511 | type Node = Node<F,D,A,$n,{pow(2, $n)}>; | |
512 | } | |
513 | ||
514 | impl<M,F,D,A> BTImpl<F,$n> for BT<M,F,D,A,$n> | |
515 | where M : Depth, | |
516 | F : Float, | |
517 | D : 'static + Copy + std::fmt::Debug, | |
518 | A : Aggregator { | |
519 | type Data = D; | |
520 | type Depth = M; | |
521 | type Agg = A; | |
522 | type Converted<ANew> = BT<M,F,D,ANew,$n> where ANew : Aggregator; | |
523 | ||
524 | /// Insert `d` into the tree. | |
525 | fn insert<S: LocalAnalysis<F, A, $n>>( | |
526 | &mut self, | |
527 | d : D, | |
528 | support : &S | |
529 | ) { | |
530 | self.topnode.insert( | |
531 | &self.domain, | |
532 | d, | |
533 | self.depth, | |
534 | support | |
535 | ); | |
536 | } | |
537 | ||
538 | /// Construct a new instance for a different aggregator | |
539 | fn convert_aggregator<ANew, G>(self, generator : &G) -> Self::Converted<ANew> | |
540 | where ANew : Aggregator, | |
541 | G : SupportGenerator<F, $n, Id=D>, | |
542 | G::SupportType : LocalAnalysis<F, ANew, $n> { | |
543 | let topnode = self.topnode.convert_aggregator(generator, &self.domain); | |
544 | BT { | |
545 | depth : self.depth, | |
546 | domain : self.domain, | |
547 | topnode | |
548 | } | |
549 | } | |
550 | ||
551 | /// Refresh aggregator after changes to generator | |
552 | fn refresh_aggregator<G>(&mut self, generator : &G) | |
553 | where G : SupportGenerator<F, $n, Id=Self::Data>, | |
554 | G::SupportType : LocalAnalysis<F, Self::Agg, $n> { | |
555 | self.topnode.refresh_aggregator(generator, &self.domain); | |
556 | } | |
557 | ||
558 | /// Iterate elements at `x`. | |
559 | fn iter_at<'a>(&'a self, x : &'a Loc<F,$n>) -> BTIter<'a,D> { | |
560 | match self.topnode.get_leaf_data(x) { | |
561 | Some(data) => BTIter { iter : data.iter() }, | |
562 | None => BTIter { iter : [].iter() } | |
563 | } | |
564 | } | |
565 | ||
566 | fn new(domain : Cube<F, $n>, depth : M) -> Self { | |
567 | BT { | |
568 | depth : depth, | |
569 | domain : domain, | |
570 | topnode : Node::new(), | |
571 | } | |
572 | } | |
573 | } | |
574 | ||
575 | impl<M,F,D,A> GlobalAnalysis<F,A> for BT<M,F,D,A,$n> | |
576 | where M : Depth, | |
577 | F : Float, | |
578 | D : 'static + Copy + std::fmt::Debug, | |
579 | A : Aggregator { | |
580 | fn global_analysis(&self) -> A { | |
581 | self.topnode.get_aggregator().clone() | |
582 | } | |
583 | } | |
584 | )* } | |
585 | } | |
586 | ||
587 | impl_bt!(1 2 3 4); | |
588 |