OLD | NEW |
1 #ifndef Stats_DEFINED | 1 #ifndef Stats_DEFINED |
2 #define Stats_DEFINED | 2 #define Stats_DEFINED |
3 | 3 |
| 4 #include "SkTSort.h" |
| 5 |
4 struct Stats { | 6 struct Stats { |
5 Stats(const double samples[], int n) { | 7 Stats(const double samples[], int n) { |
6 min = samples[0]; | 8 min = samples[0]; |
7 max = samples[0]; | 9 max = samples[0]; |
8 for (int i = 0; i < n; i++) { | 10 for (int i = 0; i < n; i++) { |
9 if (samples[i] < min) { min = samples[i]; } | 11 if (samples[i] < min) { min = samples[i]; } |
10 if (samples[i] > max) { max = samples[i]; } | 12 if (samples[i] > max) { max = samples[i]; } |
11 } | 13 } |
12 | 14 |
13 double sum = 0.0; | 15 double sum = 0.0; |
14 for (int i = 0 ; i < n; i++) { | 16 for (int i = 0 ; i < n; i++) { |
15 sum += samples[i]; | 17 sum += samples[i]; |
16 } | 18 } |
17 mean = sum / n; | 19 mean = sum / n; |
18 | 20 |
19 double err = 0.0; | 21 double err = 0.0; |
20 for (int i = 0 ; i < n; i++) { | 22 for (int i = 0 ; i < n; i++) { |
21 err += (samples[i] - mean) * (samples[i] - mean); | 23 err += (samples[i] - mean) * (samples[i] - mean); |
22 } | 24 } |
23 var = err / (n-1); | 25 var = err / (n-1); |
| 26 |
| 27 SkAutoTMalloc<double> sorted(n); |
| 28 memcpy(sorted.get(), samples, n * sizeof(double)); |
| 29 SkTQSort(sorted.get(), sorted.get() + n - 1); |
| 30 median = sorted[n/2]; |
24 } | 31 } |
25 | 32 |
26 double min; | 33 double min; |
27 double max; | 34 double max; |
28 double mean; // Estimate of population mean. | 35 double mean; // Estimate of population mean. |
29 double var; // Estimate of population variance. | 36 double var; // Estimate of population variance. |
| 37 double median; |
30 }; | 38 }; |
31 | 39 |
32 #endif//Stats_DEFINED | 40 #endif//Stats_DEFINED |
OLD | NEW |