X-Git-Url: https://git.ozlabs.org/?p=ccan;a=blobdiff_plain;f=ccan%2Ftally%2Ftally.c;h=d67112c2cf6da49017904be9ec91b49f1f11ced5;hp=88407cf985ccfb30996383b51e7c291af00319fe;hb=237fc67d26def3f4458594b4aff7cb617f4522f7;hpb=e0fd4d1173f6d761dd6e09f820e1901e9400d8ba diff --git a/ccan/tally/tally.c b/ccan/tally/tally.c index 88407cf9..d67112c2 100644 --- a/ccan/tally/tally.c +++ b/ccan/tally/tally.c @@ -1,4 +1,4 @@ -#include "config.h" +/* Licensed under LGPLv3+ - see LICENSE file for details */ #include #include #include @@ -7,6 +7,7 @@ #include #include #include +#include #define SIZET_BITS (sizeof(size_t)*CHAR_BIT) @@ -15,40 +16,36 @@ struct tally { ssize_t min, max; size_t total[2]; /* This allows limited frequency analysis. */ - size_t buckets; - size_t step_bits; - size_t counts[1 /* [buckets] */ ]; + unsigned buckets, step_bits; + size_t counts[1 /* Actually: [buckets] */ ]; }; -struct tally *tally_new(size_t buckets) +struct tally *tally_new(unsigned buckets) { struct tally *tally; - /* Check for overflow. */ - if (buckets && SIZE_MAX / buckets < sizeof(tally->counts[0])) + /* There is always 1 bucket. */ + if (buckets == 0) { + buckets = 1; + } + + /* Overly cautious check for overflow. */ + if (sizeof(*tally) * buckets / sizeof(*tally) != buckets) { return NULL; - tally = malloc(sizeof(*tally) + sizeof(tally->counts[0])*buckets); - if (tally) { - /* SSIZE_MAX isn't portable, so make it one of these types. */ - BUILD_ASSERT(sizeof(tally->min) == sizeof(int) - || sizeof(tally->min) == sizeof(long) - || sizeof(tally->min) == sizeof(long long)); - if (sizeof(tally->min) == sizeof(int)) { - tally->min = INT_MAX; - tally->max = INT_MIN; - } else if (sizeof(tally->min) == sizeof(long)) { - tally->min = LONG_MAX; - tally->max = LONG_MIN; - } else if (sizeof(tally->min) == sizeof(long long)) { - tally->min = (ssize_t)LLONG_MAX; - tally->max = (ssize_t)LLONG_MIN; - } - tally->total[0] = tally->total[1] = 0; - /* There is always 1 bucket. */ - tally->buckets = buckets+1; - tally->step_bits = 0; - memset(tally->counts, 0, sizeof(tally->counts[0])*(buckets+1)); } + + tally = (struct tally *)malloc( + sizeof(*tally) + sizeof(tally->counts[0])*(buckets-1)); + if (tally == NULL) { + return NULL; + } + + tally->max = ((size_t)1 << (SIZET_BITS - 1)); + tally->min = ~tally->max; + tally->total[0] = tally->total[1] = 0; + tally->buckets = buckets; + tally->step_bits = 0; + memset(tally->counts, 0, sizeof(tally->counts[0])*buckets); return tally; } @@ -332,7 +329,7 @@ ssize_t tally_total(const struct tally *tally, ssize_t *overflow) } /* If result is negative, make sure we can represent it. */ - if (tally->total[1] & (1 << (SIZET_BITS-1))) { + if (tally->total[1] & ((size_t)1 << (SIZET_BITS-1))) { /* Must have only underflowed once, and must be able to * represent result at ssize_t. */ if ((~tally->total[1])+1 != 0 @@ -435,7 +432,7 @@ char *tally_histogram(const struct tally *tally, } else { /* We create a temporary then renormalize so < height. */ /* FIXME: Antialias properly! */ - tmp = tally_new(tally->buckets-1); + tmp = tally_new(tally->buckets); if (!tmp) return NULL; tmp->min = tally->min; @@ -444,7 +441,7 @@ char *tally_histogram(const struct tally *tally, memcpy(tmp->counts, tally->counts, sizeof(tally->counts[0]) * tmp->buckets); while ((max_bucket = get_max_bucket(tmp)) >= height) - renormalize(tmp, tmp->min, tmp->max *= 2); + renormalize(tmp, tmp->min, tmp->max * 2); /* Restore max */ tmp->max = tally->max; tally = tmp; @@ -458,28 +455,37 @@ char *tally_histogram(const struct tally *tally, largest_bucket = tally->counts[i]; } - p = graph = malloc(height * (width + 1) + 1); + p = graph = (char *)malloc(height * (width + 1) + 1); if (!graph) { free(tmp); return NULL; } + for (i = 0; i < height; i++) { - unsigned covered = 0; - count = (double)tally->counts[i] / largest_bucket * width; + unsigned covered = 1, row; + + /* People expect minimum at the bottom. */ + row = height - i - 1; + count = (double)tally->counts[row] / largest_bucket * (width-1)+1; - if (i == 0) + if (row == 0) covered = snprintf(p, width, "%zi", tally->min); - else if (i == height - 1) + else if (row == height - 1) covered = snprintf(p, width, "%zi", tally->max); - if (covered) { - if (covered > width) - covered = width; - p += covered; - if (count > covered) - count -= covered; - else - count = 0; - } + else if (row == bucket_of(tally->min, tally->step_bits, 0)) + *p = '+'; + else + *p = '|'; + + if (covered > width) + covered = width; + p += covered; + + if (count > covered) + count -= covered; + else + count = 0; + memset(p, '*', count); p += count; *p = '\n';