s3:torture: call fault_setup() to get usage backtraces
[Samba/gebeck_regimport.git] / lib / ccan / tally / tally.h
blobc9272f1000fa516cb5c321249ee711088301a2c2
1 /* Licensed under LGPLv3+ - see LICENSE file for details */
2 #ifndef CCAN_TALLY_H
3 #define CCAN_TALLY_H
4 #include "config.h"
5 #include <sys/types.h>
7 struct tally;
9 /**
10 * tally_new - allocate the tally structure.
11 * @buckets: the number of frequency buckets.
13 * This allocates a tally structure using malloc(). The greater the value
14 * of @buckets, the more accurate tally_approx_median() and tally_approx_mode()
15 * and tally_histogram() will be, but more memory is consumed. If you want
16 * to use tally_histogram(), the optimal bucket value is the same as that
17 * @height argument.
19 struct tally *tally_new(unsigned int buckets);
21 /**
22 * tally_add - add a value.
23 * @tally: the tally structure.
24 * @val: the value to add.
26 void tally_add(struct tally *tally, ssize_t val);
28 /**
29 * tally_num - how many times as tally_add been called?
30 * @tally: the tally structure.
32 size_t tally_num(const struct tally *tally);
34 /**
35 * tally_min - the minimum value passed to tally_add.
36 * @tally: the tally structure.
38 * Undefined if tally_num() == 0.
40 ssize_t tally_min(const struct tally *tally);
42 /**
43 * tally_max - the maximum value passed to tally_add.
44 * @tally: the tally structure.
46 * Undefined if tally_num() == 0.
48 ssize_t tally_max(const struct tally *tally);
50 /**
51 * tally_mean - the mean value passed to tally_add.
52 * @tally: the tally structure.
54 * Undefined if tally_num() == 0, but will not crash.
56 ssize_t tally_mean(const struct tally *tally);
58 /**
59 * tally_total - the total value passed to tally_add.
60 * @tally: the tally structure.
61 * @overflow: the overflow value (or NULL).
63 * If your total can't overflow a ssize_t, you don't need @overflow.
64 * Otherwise, @overflow is the upper ssize_t, and the return value should
65 * be treated as the lower size_t (ie. the sign bit is in @overflow).
67 ssize_t tally_total(const struct tally *tally, ssize_t *overflow);
69 /**
70 * tally_approx_median - the approximate median value passed to tally_add.
71 * @tally: the tally structure.
72 * @err: the error in the returned value (ie. real median is +/- @err).
74 * Undefined if tally_num() == 0, but will not crash. Because we
75 * don't reallocate, we don't store all values, so this median cannot be
76 * exact.
78 ssize_t tally_approx_median(const struct tally *tally, size_t *err);
80 /**
81 * tally_approx_mode - the approximate mode value passed to tally_add.
82 * @tally: the tally structure.
83 * @err: the error in the returned value (ie. real mode is +/- @err).
85 * Undefined if tally_num() == 0, but will not crash. Because we
86 * don't reallocate, we don't store all values, so this mode cannot be
87 * exact. It could well be a value which was never passed to tally_add!
89 ssize_t tally_approx_mode(const struct tally *tally, size_t *err);
91 #define TALLY_MIN_HISTO_WIDTH 8
92 #define TALLY_MIN_HISTO_HEIGHT 3
94 /**
95 * tally_graph - return an ASCII image of the tally_add distribution
96 * @tally: the tally structure.
97 * @width: the maximum string width to use (>= TALLY_MIN_HISTO_WIDTH)
98 * @height: the maximum string height to use (>= TALLY_MIN_HISTO_HEIGHT)
100 * Returns a malloc()ed string which draws a multi-line graph of the
101 * distribution of values. On out of memory returns NULL.
103 char *tally_histogram(const struct tally *tally,
104 unsigned width, unsigned height);
105 #endif /* CCAN_TALLY_H */