Fri, 18 Nov 2022 10:28:47 +0200
Add package metadata
5 | 1 | |
2 | use std::iter::Chain; | |
8
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
3 | use std::sync::Arc; |
0 | 4 | |
5 | use crate::types::*; | |
5 | 6 | use crate::mapping::Mapping; |
7 | use crate::iter::{Mappable,MapF,MapZ}; | |
8 | use crate::sets::Cube; | |
9 | use crate::loc::Loc; | |
10 | ||
0 | 11 | use super::support::*; |
12 | use super::aggregator::*; | |
13 | ||
5 | 14 | /// A structure for storing two [`SupportGenerator`]s summed/chain together. |
15 | /// | |
16 | /// This is needed to work with sums of different types of [`Support`]s. | |
0 | 17 | #[derive(Debug,Clone)] |
18 | pub struct BothGenerators<A, B>( | |
8
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
19 | pub(super) Arc<A>, |
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
20 | pub(super) Arc<B>, |
0 | 21 | ); |
22 | ||
5 | 23 | /// A structure for a [`Support`] that can be either `A` or `B`. |
24 | /// | |
25 | /// This is needed to work with sums of different types of [`Support`]s. | |
0 | 26 | #[derive(Debug,Clone)] |
27 | pub enum EitherSupport<A, B> { | |
28 | Left(A), | |
29 | Right(B), | |
30 | } | |
31 | ||
32 | // We need type alias bounds to access associate types. | |
33 | #[allow(type_alias_bounds)] | |
34 | type BothAllDataIter< | |
35 | 'a, F, | |
36 | G1 : SupportGenerator<F, N>, | |
37 | G2 : SupportGenerator<F, N>, | |
38 | const N : usize | |
39 | > = Chain< | |
40 | MapF<G1::AllDataIter<'a>, (usize, EitherSupport<G1::SupportType, G2::SupportType>)>, | |
41 | MapZ<G2::AllDataIter<'a>, usize, (usize, EitherSupport<G1::SupportType, G2::SupportType>)>, | |
42 | >; | |
43 | ||
44 | impl<G1, G2> BothGenerators<G1, G2> { | |
5 | 45 | /// Helper for [`all_left_data`]. |
0 | 46 | #[inline] |
47 | fn map_left<F : Float, const N : usize>((d, support) : (G1::Id, G1::SupportType)) | |
48 | -> (usize, EitherSupport<G1::SupportType, G2::SupportType>) | |
49 | where G1 : SupportGenerator<F, N, Id=usize>, | |
50 | G2 : SupportGenerator<F, N, Id=usize> { | |
51 | ||
52 | let id : usize = d.into(); | |
53 | (id.into(), EitherSupport::Left(support)) | |
54 | } | |
55 | ||
5 | 56 | /// Helper for [`all_right_data`]. |
0 | 57 | #[inline] |
58 | fn map_right<F : Float, const N : usize>(n0 : &usize, (d, support) : (G2::Id, G2::SupportType)) | |
59 | -> (usize, EitherSupport<G1::SupportType, G2::SupportType>) | |
60 | where G1 : SupportGenerator<F, N, Id=usize>, | |
61 | G2 : SupportGenerator<F, N, Id=usize> { | |
62 | ||
63 | let id : usize = d.into(); | |
64 | ((n0+id).into(), EitherSupport::Right(support)) | |
65 | } | |
66 | ||
5 | 67 | /// Calls [`SupportGenerator::all_data`] on the “left” support generator. |
68 | /// | |
69 | /// Converts both the id and the [`Support`] into a form that corresponds to `BothGenerators`. | |
0 | 70 | #[inline] |
71 | pub(super) fn all_left_data<F : Float, const N : usize>(&self) | |
72 | -> MapF<G1::AllDataIter<'_>, (usize, EitherSupport<G1::SupportType, G2::SupportType>)> | |
73 | where G1 : SupportGenerator<F, N, Id=usize>, | |
74 | G2 : SupportGenerator<F, N, Id=usize> { | |
75 | self.0.all_data().mapF(Self::map_left) | |
76 | } | |
77 | ||
5 | 78 | /// Calls [`SupportGenerator::all_data`] on the “right” support generator. |
79 | /// | |
80 | /// Converts both the id and the [`Support`] into a form that corresponds to `BothGenerators`. | |
0 | 81 | #[inline] |
82 | pub(super) fn all_right_data<F : Float, const N : usize>(&self) | |
83 | -> MapZ<G2::AllDataIter<'_>, usize, (usize, EitherSupport<G1::SupportType, G2::SupportType>)> | |
84 | where G1 : SupportGenerator<F, N, Id=usize>, | |
85 | G2 : SupportGenerator<F, N, Id=usize> { | |
86 | let n0 = self.0.support_count(); | |
87 | self.1.all_data().mapZ(n0, Self::map_right) | |
88 | } | |
89 | } | |
90 | ||
91 | impl<F : Float, G1, G2, const N : usize> | |
92 | SupportGenerator<F, N> | |
93 | for BothGenerators<G1, G2> | |
94 | where G1 : SupportGenerator<F, N, Id=usize>, | |
95 | G2 : SupportGenerator<F, N, Id=usize> { | |
96 | ||
97 | type Id = usize; | |
98 | type SupportType = EitherSupport<G1::SupportType, G2::SupportType>; | |
99 | type AllDataIter<'a> = BothAllDataIter<'a, F, G1, G2, N> where G1 : 'a, G2 : 'a; | |
100 | ||
101 | #[inline] | |
102 | fn support_for(&self, id : Self::Id) | |
103 | -> Self::SupportType { | |
104 | let n0 = self.0.support_count(); | |
105 | if id < n0 { | |
106 | EitherSupport::Left(self.0.support_for(id.into())) | |
107 | } else { | |
108 | EitherSupport::Right(self.1.support_for((id-n0).into())) | |
109 | } | |
110 | } | |
111 | ||
112 | #[inline] | |
113 | fn support_count(&self) -> usize { | |
114 | self.0.support_count() + self.1.support_count() | |
115 | } | |
116 | ||
117 | #[inline] | |
118 | fn all_data(&self) -> Self::AllDataIter<'_> { | |
119 | self.all_left_data().chain(self.all_right_data()) | |
120 | } | |
121 | } | |
122 | ||
123 | impl<F: Float, S1, S2, const N : usize> Support<F, N> for EitherSupport<S1, S2> | |
124 | where S1 : Support<F, N>, | |
125 | S2 : Support<F, N> { | |
126 | ||
127 | #[inline] | |
128 | fn support_hint(&self) -> Cube<F,N> { | |
129 | match self { | |
130 | EitherSupport::Left(ref a) => a.support_hint(), | |
131 | EitherSupport::Right(ref b) => b.support_hint(), | |
132 | } | |
133 | } | |
134 | ||
135 | #[inline] | |
136 | fn in_support(&self, x : &Loc<F,N>) -> bool { | |
137 | match self { | |
138 | EitherSupport::Left(ref a) => a.in_support(x), | |
139 | EitherSupport::Right(ref b) => b.in_support(x), | |
140 | } | |
141 | } | |
142 | ||
143 | #[inline] | |
144 | fn bisection_hint(&self, cube : &Cube<F, N>) -> [Option<F>; N] { | |
145 | match self { | |
146 | EitherSupport::Left(ref a) => a.bisection_hint(cube), | |
147 | EitherSupport::Right(ref b) => b.bisection_hint(cube), | |
148 | } | |
149 | } | |
150 | } | |
151 | ||
152 | impl<F : Float, A, S1, S2, const N : usize> LocalAnalysis<F, A, N> for EitherSupport<S1, S2> | |
153 | where A : Aggregator, | |
154 | S1 : LocalAnalysis<F, A, N>, | |
155 | S2 : LocalAnalysis<F, A, N>, { | |
156 | ||
157 | #[inline] | |
158 | fn local_analysis(&self, cube : &Cube<F, N>) -> A { | |
159 | match self { | |
160 | EitherSupport::Left(ref a) => a.local_analysis(cube), | |
161 | EitherSupport::Right(ref b) => b.local_analysis(cube), | |
162 | } | |
163 | } | |
164 | } | |
165 | ||
166 | impl<F : Float, A, S1, S2> GlobalAnalysis<F, A> for EitherSupport<S1, S2> | |
167 | where A : Aggregator, | |
168 | S1 : GlobalAnalysis<F, A>, | |
169 | S2 : GlobalAnalysis<F, A>, { | |
170 | ||
171 | #[inline] | |
172 | fn global_analysis(&self) -> A { | |
173 | match self { | |
174 | EitherSupport::Left(ref a) => a.global_analysis(), | |
175 | EitherSupport::Right(ref b) => b.global_analysis(), | |
176 | } | |
177 | } | |
178 | } | |
179 | ||
180 | impl<F, S1, S2, X> Mapping<X> for EitherSupport<S1, S2> | |
181 | where S1 : Mapping<X, Codomain=F>, | |
182 | S2 : Mapping<X, Codomain=F> { | |
183 | type Codomain = F; | |
184 | #[inline] | |
185 | fn value(&self, x : X) -> F { | |
186 | match self { | |
187 | EitherSupport::Left(ref a) => a.value(x), | |
188 | EitherSupport::Right(ref b) => b.value(x), | |
189 | } | |
190 | } | |
191 | } | |
192 | ||
193 | macro_rules! make_either_scalarop_rhs { | |
194 | ($trait:ident, $fn:ident, $trait_assign:ident, $fn_assign:ident) => { | |
195 | impl<F : Float, G1, G2> | |
196 | std::ops::$trait_assign<F> | |
197 | for BothGenerators<G1, G2> | |
8
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
198 | where G1 : std::ops::$trait_assign<F> + Clone, |
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
199 | G2 : std::ops::$trait_assign<F> + Clone, { |
0 | 200 | #[inline] |
201 | fn $fn_assign(&mut self, t : F) { | |
8
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
202 | Arc::make_mut(&mut self.0).$fn_assign(t); |
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
203 | Arc::make_mut(&mut self.1).$fn_assign(t); |
0 | 204 | } |
205 | } | |
206 | ||
207 | impl<'a, F : Float, G1, G2> | |
208 | std::ops::$trait<F> | |
209 | for &'a BothGenerators<G1, G2> | |
210 | where &'a G1 : std::ops::$trait<F,Output=G1>, | |
211 | &'a G2 : std::ops::$trait<F,Output=G2> { | |
212 | type Output = BothGenerators<G1, G2>; | |
213 | #[inline] | |
214 | fn $fn(self, t : F) -> BothGenerators<G1, G2> { | |
8
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
215 | BothGenerators(Arc::new(self.0.$fn(t)), |
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
216 | Arc::new(self.1.$fn(t))) |
0 | 217 | } |
218 | } | |
219 | } | |
220 | } | |
221 | ||
222 | make_either_scalarop_rhs!(Mul, mul, MulAssign, mul_assign); | |
223 | make_either_scalarop_rhs!(Div, div, DivAssign, div_assign); | |
224 | ||
225 | impl<G1, G2> std::ops::Neg for BothGenerators<G1, G2> | |
8
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
226 | where G1 : std::ops::Neg + Clone, |
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
227 | G2 : std::ops::Neg + Clone, { |
0 | 228 | type Output = BothGenerators<G1::Output, G2::Output>; |
229 | #[inline] | |
230 | fn neg(self) -> Self::Output { | |
8
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
231 | BothGenerators(Arc::new(Arc::unwrap_or_clone(self.0).neg()), |
4e09b7829b51
Multithreaded bisection tree operations
Tuomo Valkonen <tuomov@iki.fi>
parents:
5
diff
changeset
|
232 | Arc::new(Arc::unwrap_or_clone(self.1).neg())) |
0 | 233 | } |
234 | } | |
235 | /* | |
236 | impl<'a, G1, G2> std::ops::Neg for &'a BothGenerators<G1, G2> | |
237 | where &'a G1 : std::ops::Neg, &'a G2 : std::ops::Neg, { | |
238 | type Output = BothGenerators<<&'a G1 as std::ops::Neg>::Output, | |
239 | <&'a G2 as std::ops::Neg>::Output>; | |
240 | fn neg(self) -> Self::Output { | |
241 | BothGenerators(self.0.neg(), self.1.neg()) | |
242 | } | |
243 | } | |
244 | */ |