OLD | NEW |
1 #ifndef Stats_DEFINED | 1 #ifndef Stats_DEFINED |
2 #define Stats_DEFINED | 2 #define Stats_DEFINED |
3 | 3 |
4 #include <math.h> | 4 #include <math.h> |
5 | 5 |
6 #include "SkString.h" | 6 #include "SkString.h" |
7 #include "SkTSort.h" | 7 #include "SkTSort.h" |
8 | 8 |
9 static const char* kBars[] = { "▁", "▂", "▃", "▄", "▅", "▆", "▇", "█" }; | 9 static const char* kBars[] = { "▁", "▂", "▃", "▄", "▅", "▆", "▇", "█" }; |
10 | 10 |
(...skipping 16 matching lines...) Expand all Loading... |
27 for (int i = 0 ; i < n; i++) { | 27 for (int i = 0 ; i < n; i++) { |
28 err += (samples[i] - mean) * (samples[i] - mean); | 28 err += (samples[i] - mean) * (samples[i] - mean); |
29 } | 29 } |
30 var = err / (n-1); | 30 var = err / (n-1); |
31 | 31 |
32 SkAutoTMalloc<double> sorted(n); | 32 SkAutoTMalloc<double> sorted(n); |
33 memcpy(sorted.get(), samples, n * sizeof(double)); | 33 memcpy(sorted.get(), samples, n * sizeof(double)); |
34 SkTQSort(sorted.get(), sorted.get() + n - 1); | 34 SkTQSort(sorted.get(), sorted.get() + n - 1); |
35 median = sorted[n/2]; | 35 median = sorted[n/2]; |
36 | 36 |
| 37 // Normalize samples to [min, max] in as many quanta as we have distinct
bars to print. |
37 for (int i = 0; i < n; i++) { | 38 for (int i = 0; i < n; i++) { |
| 39 if (min == max) { |
| 40 // All samples are the same value. Don't divide by zero. |
| 41 plot.append(kBars[0]); |
| 42 continue; |
| 43 } |
| 44 |
38 double s = samples[i]; | 45 double s = samples[i]; |
39 // Normalize samples to [min, max] in as many quanta as we have dist
inct bars to print. | |
40 s -= min; | 46 s -= min; |
41 s /= (max - min); | 47 s /= (max - min); |
42 s *= (SK_ARRAY_COUNT(kBars) - 1); | 48 s *= (SK_ARRAY_COUNT(kBars) - 1); |
43 const size_t bar = (size_t)round(s); | 49 const size_t bar = (size_t)round(s); |
44 SK_ALWAYSBREAK(bar < SK_ARRAY_COUNT(kBars)); | 50 SK_ALWAYSBREAK(bar < SK_ARRAY_COUNT(kBars)); |
45 plot.append(kBars[bar]); | 51 plot.append(kBars[bar]); |
46 } | 52 } |
47 } | 53 } |
48 | 54 |
49 double min; | 55 double min; |
50 double max; | 56 double max; |
51 double mean; // Estimate of population mean. | 57 double mean; // Estimate of population mean. |
52 double var; // Estimate of population variance. | 58 double var; // Estimate of population variance. |
53 double median; | 59 double median; |
54 SkString plot; // A single-line bar chart (_not_ histogram) of the samples. | 60 SkString plot; // A single-line bar chart (_not_ histogram) of the samples. |
55 }; | 61 }; |
56 | 62 |
57 #endif//Stats_DEFINED | 63 #endif//Stats_DEFINED |
OLD | NEW |