tally.h 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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>
  6. struct tally;
  7. /**
  8. * tally_new - allocate the tally structure.
  9. * @buckets: the number of frequency buckets.
  10. *
  11. * This allocates a tally structure using malloc(). The greater the value
  12. * of @buckets, the more accurate tally_approx_median() and tally_approx_mode()
  13. * and tally_histogram() will be, but more memory is consumed. If you want
  14. * to use tally_histogram(), the optimal bucket value is the same as that
  15. * @height argument.
  16. */
  17. struct tally *tally_new(unsigned int buckets);
  18. /**
  19. * tally_add - add a value.
  20. * @tally: the tally structure.
  21. * @val: the value to add.
  22. */
  23. void tally_add(struct tally *tally, ssize_t val);
  24. /**
  25. * tally_num - how many times as tally_add been called?
  26. * @tally: the tally structure.
  27. */
  28. size_t tally_num(const struct tally *tally);
  29. /**
  30. * tally_min - the minimum value passed to tally_add.
  31. * @tally: the tally structure.
  32. *
  33. * Undefined if tally_num() == 0.
  34. */
  35. ssize_t tally_min(const struct tally *tally);
  36. /**
  37. * tally_max - the maximum value passed to tally_add.
  38. * @tally: the tally structure.
  39. *
  40. * Undefined if tally_num() == 0.
  41. */
  42. ssize_t tally_max(const struct tally *tally);
  43. /**
  44. * tally_mean - the mean value passed to tally_add.
  45. * @tally: the tally structure.
  46. *
  47. * Undefined if tally_num() == 0, but will not crash.
  48. */
  49. ssize_t tally_mean(const struct tally *tally);
  50. /**
  51. * tally_total - the total value passed to tally_add.
  52. * @tally: the tally structure.
  53. * @overflow: the overflow value (or NULL).
  54. *
  55. * If your total can't overflow a ssize_t, you don't need @overflow.
  56. * Otherwise, @overflow is the upper ssize_t, and the return value should
  57. * be treated as the lower size_t (ie. the sign bit is in @overflow).
  58. */
  59. ssize_t tally_total(const struct tally *tally, ssize_t *overflow);
  60. /**
  61. * tally_approx_median - the approximate median value passed to tally_add.
  62. * @tally: the tally structure.
  63. * @err: the error in the returned value (ie. real median is +/- @err).
  64. *
  65. * Undefined if tally_num() == 0, but will not crash. Because we
  66. * don't reallocate, we don't store all values, so this median cannot be
  67. * exact.
  68. */
  69. ssize_t tally_approx_median(const struct tally *tally, size_t *err);
  70. /**
  71. * tally_approx_mode - the approximate mode value passed to tally_add.
  72. * @tally: the tally structure.
  73. * @err: the error in the returned value (ie. real mode is +/- @err).
  74. *
  75. * Undefined if tally_num() == 0, but will not crash. Because we
  76. * don't reallocate, we don't store all values, so this mode cannot be
  77. * exact. It could well be a value which was never passed to tally_add!
  78. */
  79. ssize_t tally_approx_mode(const struct tally *tally, size_t *err);
  80. #define TALLY_MIN_HISTO_WIDTH 8
  81. #define TALLY_MIN_HISTO_HEIGHT 3
  82. /**
  83. * tally_graph - return an ASCII image of the tally_add distribution
  84. * @tally: the tally structure.
  85. * @width: the maximum string width to use (>= TALLY_MIN_HISTO_WIDTH)
  86. * @height: the maximum string height to use (>= TALLY_MIN_HISTO_HEIGHT)
  87. *
  88. * Returns a malloc()ed string which draws a multi-line graph of the
  89. * distribution of values. On out of memory returns NULL.
  90. */
  91. char *tally_histogram(const struct tally *tally,
  92. unsigned width, unsigned height);
  93. #endif /* CCAN_TALLY_H */