X-Git-Url: http://git.ozlabs.org/?p=ccan;a=blobdiff_plain;f=ccan%2Ftally%2Ftally.c;h=774373ca0f88e0b4a48bed5029107cf28bded0d1;hp=e166dd03c532252df2b0e31ed7d41198f390f88b;hb=926996e88c32445c874ff9c4f47f159db6b45995;hpb=6697a3dde2b3e1aa180622bd5f909bba3a8fd948 diff --git a/ccan/tally/tally.c b/ccan/tally/tally.c index e166dd03..774373ca 100644 --- a/ccan/tally/tally.c +++ b/ccan/tally/tally.c @@ -367,10 +367,11 @@ static ssize_t bucket_range(const struct tally *tally, unsigned b, size_t *err) ssize_t min, max; min = bucket_min(tally->min, tally->step_bits, b); - if (b == tally->buckets - 1) + if (b == tally->buckets - 1) { max = tally->max; - else + } else { max = bucket_min(tally->min, tally->step_bits, b+1) - 1; + } /* FIXME: Think harder about cumulative error; is this enough?. */ *err = (max - min + 1) / 2; @@ -385,8 +386,9 @@ ssize_t tally_approx_median(const struct tally *tally, size_t *err) for (i = 0; i < tally->buckets; i++) { total += tally->counts[i]; - if (total * 2 >= count) + if (total * 2 >= count) { break; + } } return bucket_range(tally, i, err); } @@ -420,9 +422,11 @@ static unsigned get_max_bucket(const struct tally *tally) { unsigned int i; - for (i = tally->buckets; i > 0; i--) - if (tally->counts[i-1]) + for (i = tally->buckets; i > 0; i--) { + if (tally->counts[i-1]) { break; + } + } return i; } @@ -447,15 +451,17 @@ char *tally_histogram(const struct tally *tally, /* We create a temporary then renormalize so < height. */ /* FIXME: Antialias properly! */ tmp = tally_new(tally->buckets); - if (!tmp) + if (!tmp) { return NULL; + } tmp->min = tally->min; tmp->max = tally->max; tmp->step_bits = tally->step_bits; memcpy(tmp->counts, tally->counts, sizeof(tally->counts[0]) * tmp->buckets); - while ((max_bucket = get_max_bucket(tmp)) >= height) + while ((max_bucket = get_max_bucket(tmp)) >= height) { renormalize(tmp, tmp->min, tmp->max * 2); + } /* Restore max */ tmp->max = tally->max; tally = tmp; @@ -465,8 +471,9 @@ char *tally_histogram(const struct tally *tally, /* Figure out longest line, for scale. */ largest_bucket = 0; for (i = 0; i < tally->buckets; i++) { - if (tally->counts[i] > largest_bucket) + if (tally->counts[i] > largest_bucket) { largest_bucket = tally->counts[i]; + } } p = graph = (char *)malloc(height * (width + 1) + 1); @@ -482,23 +489,26 @@ char *tally_histogram(const struct tally *tally, row = height - i - 1; count = (double)tally->counts[row] / largest_bucket * (width-1)+1; - if (row == 0) + if (row == 0) { covered = snprintf(p, width, "%zi", tally->min); - else if (row == height - 1) + } else if (row == height - 1) { covered = snprintf(p, width, "%zi", tally->max); - else if (row == bucket_of(tally->min, tally->step_bits, 0)) + } else if (row == bucket_of(tally->min, tally->step_bits, 0)) { *p = '+'; - else + } else { *p = '|'; + } - if (covered > width) + if (covered > width) { covered = width; + } p += covered; - if (count > covered) + if (count > covered) { count -= covered; - else + } else { count = 0; + } memset(p, '*', count); p += count;