/rust/registry/src/index.crates.io-1949cf8c6b5b557f/criterion-0.5.1/src/stats/tuple.rs
Line | Count | Source |
1 | | //! Helper traits for tupling/untupling |
2 | | |
3 | | use crate::stats::Distribution; |
4 | | |
5 | | /// Any tuple: `(A, B, ..)` |
6 | | pub trait Tuple: Sized { |
7 | | /// A tuple of distributions associated with this tuple |
8 | | type Distributions: TupledDistributions<Item = Self>; |
9 | | |
10 | | /// A tuple of vectors associated with this tuple |
11 | | type Builder: TupledDistributionsBuilder<Item = Self>; |
12 | | } |
13 | | |
14 | | /// A tuple of distributions: `(Distribution<A>, Distribution<B>, ..)` |
15 | | pub trait TupledDistributions: Sized { |
16 | | /// A tuple that can be pushed/inserted into the tupled distributions |
17 | | type Item: Tuple<Distributions = Self>; |
18 | | } |
19 | | |
20 | | /// A tuple of vecs used to build distributions. |
21 | | pub trait TupledDistributionsBuilder: Sized { |
22 | | /// A tuple that can be pushed/inserted into the tupled distributions |
23 | | type Item: Tuple<Builder = Self>; |
24 | | |
25 | | /// Creates a new tuple of vecs |
26 | | fn new(size: usize) -> Self; |
27 | | |
28 | | /// Push one element into each of the vecs |
29 | | fn push(&mut self, tuple: Self::Item); |
30 | | |
31 | | /// Append one tuple of vecs to this one, leaving the vecs in the other tuple empty |
32 | | fn extend(&mut self, other: &mut Self); |
33 | | |
34 | | /// Convert the tuple of vectors into a tuple of distributions |
35 | | fn complete(self) -> <Self::Item as Tuple>::Distributions; |
36 | | } |
37 | | |
38 | | impl<A> Tuple for (A,) |
39 | | where |
40 | | A: Copy, |
41 | | { |
42 | | type Distributions = (Distribution<A>,); |
43 | | type Builder = (Vec<A>,); |
44 | | } |
45 | | |
46 | | impl<A> TupledDistributions for (Distribution<A>,) |
47 | | where |
48 | | A: Copy, |
49 | | { |
50 | | type Item = (A,); |
51 | | } |
52 | | impl<A> TupledDistributionsBuilder for (Vec<A>,) |
53 | | where |
54 | | A: Copy, |
55 | | { |
56 | | type Item = (A,); |
57 | | |
58 | 0 | fn new(size: usize) -> (Vec<A>,) { |
59 | 0 | (Vec::with_capacity(size),) |
60 | 0 | } |
61 | | |
62 | 0 | fn push(&mut self, tuple: (A,)) { |
63 | 0 | (self.0).push(tuple.0); |
64 | 0 | } |
65 | | |
66 | 0 | fn extend(&mut self, other: &mut (Vec<A>,)) { |
67 | 0 | (self.0).append(&mut other.0); |
68 | 0 | } |
69 | | |
70 | 0 | fn complete(self) -> (Distribution<A>,) { |
71 | 0 | (Distribution(self.0.into_boxed_slice()),) |
72 | 0 | } |
73 | | } |
74 | | |
75 | | impl<A, B> Tuple for (A, B) |
76 | | where |
77 | | A: Copy, |
78 | | B: Copy, |
79 | | { |
80 | | type Distributions = (Distribution<A>, Distribution<B>); |
81 | | type Builder = (Vec<A>, Vec<B>); |
82 | | } |
83 | | |
84 | | impl<A, B> TupledDistributions for (Distribution<A>, Distribution<B>) |
85 | | where |
86 | | A: Copy, |
87 | | B: Copy, |
88 | | { |
89 | | type Item = (A, B); |
90 | | } |
91 | | impl<A, B> TupledDistributionsBuilder for (Vec<A>, Vec<B>) |
92 | | where |
93 | | A: Copy, |
94 | | B: Copy, |
95 | | { |
96 | | type Item = (A, B); |
97 | | |
98 | 0 | fn new(size: usize) -> (Vec<A>, Vec<B>) { |
99 | 0 | (Vec::with_capacity(size), Vec::with_capacity(size)) |
100 | 0 | } |
101 | | |
102 | 0 | fn push(&mut self, tuple: (A, B)) { |
103 | 0 | (self.0).push(tuple.0); |
104 | 0 | (self.1).push(tuple.1); |
105 | 0 | } |
106 | | |
107 | 0 | fn extend(&mut self, other: &mut (Vec<A>, Vec<B>)) { |
108 | 0 | (self.0).append(&mut other.0); |
109 | 0 | (self.1).append(&mut other.1); |
110 | 0 | } |
111 | | |
112 | 0 | fn complete(self) -> (Distribution<A>, Distribution<B>) { |
113 | 0 | ( |
114 | 0 | Distribution(self.0.into_boxed_slice()), |
115 | 0 | Distribution(self.1.into_boxed_slice()), |
116 | 0 | ) |
117 | 0 | } |
118 | | } |
119 | | |
120 | | impl<A, B, C> Tuple for (A, B, C) |
121 | | where |
122 | | A: Copy, |
123 | | B: Copy, |
124 | | C: Copy, |
125 | | { |
126 | | type Distributions = (Distribution<A>, Distribution<B>, Distribution<C>); |
127 | | type Builder = (Vec<A>, Vec<B>, Vec<C>); |
128 | | } |
129 | | |
130 | | impl<A, B, C> TupledDistributions for (Distribution<A>, Distribution<B>, Distribution<C>) |
131 | | where |
132 | | A: Copy, |
133 | | B: Copy, |
134 | | C: Copy, |
135 | | { |
136 | | type Item = (A, B, C); |
137 | | } |
138 | | impl<A, B, C> TupledDistributionsBuilder for (Vec<A>, Vec<B>, Vec<C>) |
139 | | where |
140 | | A: Copy, |
141 | | B: Copy, |
142 | | C: Copy, |
143 | | { |
144 | | type Item = (A, B, C); |
145 | | |
146 | 0 | fn new(size: usize) -> (Vec<A>, Vec<B>, Vec<C>) { |
147 | 0 | ( |
148 | 0 | Vec::with_capacity(size), |
149 | 0 | Vec::with_capacity(size), |
150 | 0 | Vec::with_capacity(size), |
151 | 0 | ) |
152 | 0 | } |
153 | | |
154 | 0 | fn push(&mut self, tuple: (A, B, C)) { |
155 | 0 | (self.0).push(tuple.0); |
156 | 0 | (self.1).push(tuple.1); |
157 | 0 | (self.2).push(tuple.2); |
158 | 0 | } |
159 | | |
160 | 0 | fn extend(&mut self, other: &mut (Vec<A>, Vec<B>, Vec<C>)) { |
161 | 0 | (self.0).append(&mut other.0); |
162 | 0 | (self.1).append(&mut other.1); |
163 | 0 | (self.2).append(&mut other.2); |
164 | 0 | } |
165 | | |
166 | 0 | fn complete(self) -> (Distribution<A>, Distribution<B>, Distribution<C>) { |
167 | 0 | ( |
168 | 0 | Distribution(self.0.into_boxed_slice()), |
169 | 0 | Distribution(self.1.into_boxed_slice()), |
170 | 0 | Distribution(self.2.into_boxed_slice()), |
171 | 0 | ) |
172 | 0 | } |
173 | | } |
174 | | |
175 | | impl<A, B, C, D> Tuple for (A, B, C, D) |
176 | | where |
177 | | A: Copy, |
178 | | B: Copy, |
179 | | C: Copy, |
180 | | D: Copy, |
181 | | { |
182 | | type Distributions = ( |
183 | | Distribution<A>, |
184 | | Distribution<B>, |
185 | | Distribution<C>, |
186 | | Distribution<D>, |
187 | | ); |
188 | | type Builder = (Vec<A>, Vec<B>, Vec<C>, Vec<D>); |
189 | | } |
190 | | |
191 | | impl<A, B, C, D> TupledDistributions |
192 | | for ( |
193 | | Distribution<A>, |
194 | | Distribution<B>, |
195 | | Distribution<C>, |
196 | | Distribution<D>, |
197 | | ) |
198 | | where |
199 | | A: Copy, |
200 | | B: Copy, |
201 | | C: Copy, |
202 | | D: Copy, |
203 | | { |
204 | | type Item = (A, B, C, D); |
205 | | } |
206 | | impl<A, B, C, D> TupledDistributionsBuilder for (Vec<A>, Vec<B>, Vec<C>, Vec<D>) |
207 | | where |
208 | | A: Copy, |
209 | | B: Copy, |
210 | | C: Copy, |
211 | | D: Copy, |
212 | | { |
213 | | type Item = (A, B, C, D); |
214 | | |
215 | 0 | fn new(size: usize) -> (Vec<A>, Vec<B>, Vec<C>, Vec<D>) { |
216 | 0 | ( |
217 | 0 | Vec::with_capacity(size), |
218 | 0 | Vec::with_capacity(size), |
219 | 0 | Vec::with_capacity(size), |
220 | 0 | Vec::with_capacity(size), |
221 | 0 | ) |
222 | 0 | } |
223 | | |
224 | 0 | fn push(&mut self, tuple: (A, B, C, D)) { |
225 | 0 | (self.0).push(tuple.0); |
226 | 0 | (self.1).push(tuple.1); |
227 | 0 | (self.2).push(tuple.2); |
228 | 0 | (self.3).push(tuple.3); |
229 | 0 | } |
230 | | |
231 | 0 | fn extend(&mut self, other: &mut (Vec<A>, Vec<B>, Vec<C>, Vec<D>)) { |
232 | 0 | (self.0).append(&mut other.0); |
233 | 0 | (self.1).append(&mut other.1); |
234 | 0 | (self.2).append(&mut other.2); |
235 | 0 | (self.3).append(&mut other.3); |
236 | 0 | } |
237 | | |
238 | 0 | fn complete( |
239 | 0 | self, |
240 | 0 | ) -> ( |
241 | 0 | Distribution<A>, |
242 | 0 | Distribution<B>, |
243 | 0 | Distribution<C>, |
244 | 0 | Distribution<D>, |
245 | 0 | ) { |
246 | 0 | ( |
247 | 0 | Distribution(self.0.into_boxed_slice()), |
248 | 0 | Distribution(self.1.into_boxed_slice()), |
249 | 0 | Distribution(self.2.into_boxed_slice()), |
250 | 0 | Distribution(self.3.into_boxed_slice()), |
251 | 0 | ) |
252 | 0 | } |
253 | | } |