tally.h 3.2 KB

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