skia2/tools/Stats.h
djsollen aa97a84404 Revert of Consolidate SK_CRASH and sk_throw into SK_ABORT (patchset #5 id:80001 of https://codereview.chromium.org/1610823002/ )
Reason for revert:
Chrome is calling SK_CRASH

Original issue's description:
> Consolidate SK_CRASH and sk_throw into SK_ABORT
> GOLD_TRYBOT_URL= https://gold.skia.org/search2?unt=true&query=source_type%3Dgm&master=false&issue=1610823002
>
> Committed: https://skia.googlesource.com/skia/+/4c5cd7d527ed29aabfa72aa47b23a4496eeda357

TBR=reed@google.com
# Skipping CQ checks because original CL landed less than 1 days ago.
NOPRESUBMIT=true
NOTREECHECKS=true
NOTRY=true

Review URL: https://codereview.chromium.org/1620333002
2016-01-22 06:50:25 -08:00

79 lines
2.1 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 "SkString.h"
#include "SkTSort.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) {
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 = err / (n-1);
SkAutoTMalloc<double> sorted(n);
memcpy(sorted.get(), samples.begin(), n * sizeof(double));
SkTQSort(sorted.get(), sorted.get() + n - 1);
median = sorted[n/2];
// Normalize samples to [min, max] in as many quanta as we have distinct bars to print.
for (int i = 0; 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);
SK_ALWAYSBREAK(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