886a904595
C++ algorithms have largely standardized on a [begin, end) half-open range, as seen in standard library containers. SkTQSort now adheres to this model, and takes vec.begin() and vec.end() as its inputs. To avoid confusion between inclusive and half-open ranges inside the implementation, internal helper functions now take "left" and "count" arguments instead of "left"/"right" or "begin"/"end". This avoids any ambiguity. (Although performance was not the main goal, this CL appears to slightly improve our sorting benchmark on my machine.) Change-Id: I5e96b6730be96cf23d001ee0915c69764b2c024a Reviewed-on: https://skia-review.googlesource.com/c/skia/+/302579 Reviewed-by: Mike Klein <mtklein@google.com> Commit-Queue: John Stiles <johnstiles@google.com>
81 lines
2.2 KiB
C++
81 lines
2.2 KiB
C++
/*
|
|
* Copyright 2015 Google Inc.
|
|
*
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
* found in the LICENSE file.
|
|
*/
|
|
|
|
#ifndef Stats_DEFINED
|
|
#define Stats_DEFINED
|
|
|
|
#include <algorithm>
|
|
#include <vector>
|
|
|
|
#include "include/core/SkString.h"
|
|
#include "include/private/SkFloatingPoint.h"
|
|
|
|
#ifdef SK_BUILD_FOR_WIN
|
|
static const char* kBars[] = { ".", "o", "O" };
|
|
#else
|
|
static const char* kBars[] = { "▁", "▂", "▃", "▄", "▅", "▆", "▇", "█" };
|
|
#endif
|
|
|
|
struct Stats {
|
|
Stats(const SkTArray<double>& samples, bool want_plot) {
|
|
int n = samples.count();
|
|
if (!n) {
|
|
min = max = mean = var = median = 0;
|
|
return;
|
|
}
|
|
|
|
min = samples[0];
|
|
max = samples[0];
|
|
for (int i = 0; i < n; i++) {
|
|
if (samples[i] < min) { min = samples[i]; }
|
|
if (samples[i] > max) { max = samples[i]; }
|
|
}
|
|
|
|
double sum = 0.0;
|
|
for (int i = 0 ; i < n; i++) {
|
|
sum += samples[i];
|
|
}
|
|
mean = sum / n;
|
|
|
|
double err = 0.0;
|
|
for (int i = 0 ; i < n; i++) {
|
|
err += (samples[i] - mean) * (samples[i] - mean);
|
|
}
|
|
var = sk_ieee_double_divide(err, n-1);
|
|
|
|
std::vector<double> sorted(samples.begin(), samples.end());
|
|
std::sort(sorted.begin(), sorted.end());
|
|
median = sorted[n/2];
|
|
|
|
// Normalize samples to [min, max] in as many quanta as we have distinct bars to print.
|
|
for (int i = 0; want_plot && i < n; i++) {
|
|
if (min == max) {
|
|
// All samples are the same value. Don't divide by zero.
|
|
plot.append(kBars[0]);
|
|
continue;
|
|
}
|
|
|
|
double s = samples[i];
|
|
s -= min;
|
|
s /= (max - min);
|
|
s *= (SK_ARRAY_COUNT(kBars) - 1);
|
|
const size_t bar = (size_t)(s + 0.5);
|
|
SkASSERT_RELEASE(bar < SK_ARRAY_COUNT(kBars));
|
|
plot.append(kBars[bar]);
|
|
}
|
|
}
|
|
|
|
double min;
|
|
double max;
|
|
double mean; // Estimate of population mean.
|
|
double var; // Estimate of population variance.
|
|
double median;
|
|
SkString plot; // A single-line bar chart (_not_ histogram) of the samples.
|
|
};
|
|
|
|
#endif//Stats_DEFINED
|