tally.h 3.1 KB

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