AverageRoller.h 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. /* vim: set expandtab ts=4 sw=4: */
  2. /*
  3. * You may redistribute this program and/or modify it under the terms of
  4. * the GNU General Public License as published by the Free Software Foundation,
  5. * either version 3 of the License, or (at your option) any later version.
  6. *
  7. * This program is distributed in the hope that it will be useful,
  8. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. * GNU General Public License for more details.
  11. *
  12. * You should have received a copy of the GNU General Public License
  13. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  14. */
  15. #ifndef AverageRoller_H
  16. #define AverageRoller_H
  17. #include "memory/Allocator.h"
  18. #include <stdint.h>
  19. #include "util/events/EventBase.h"
  20. #include "util/Linker.h"
  21. Linker_require("util/AverageRoller.c")
  22. struct AverageRoller
  23. {
  24. int unused;
  25. };
  26. /**
  27. * Create a new AverageRoller.
  28. *
  29. * @param windowSeconds the number of seconds the average should run over.
  30. * @return a new roller.
  31. */
  32. struct AverageRoller* AverageRoller_new(const uint32_t windowSeconds,
  33. struct EventBase* eventBase,
  34. struct Allocator* allocator);
  35. /**
  36. * Get the average.
  37. *
  38. * @param roller the roller context created by AverageRoller_new()
  39. * @return the average over the last windowSeconds active seconds.
  40. */
  41. uint32_t AverageRoller_getAverage(struct AverageRoller* roller);
  42. /**
  43. * Update the roller with a new entry at a manually specified point in time.
  44. *
  45. * @param averageRoller the roller to update.
  46. * @param now the number of seconds since the epoch.
  47. * @param newEntry the a new number to be factored into the average.
  48. * @return the average over the last windowSeconds seconds.
  49. */
  50. uint32_t AverageRoller_updateAtTime(struct AverageRoller* averageRoller,
  51. const uint64_t now,
  52. const uint32_t newEntry);
  53. /**
  54. * Update the roller with a new entry and get the average.
  55. * If there are no new updates then past seconds do not get trimmed off and
  56. * the average stays the same. This, though destroying its mathmatical correctness,
  57. * provides some forgiveness to the average since it will not drop to 0 if windowSeconds
  58. * seconds elapse with no update.
  59. *
  60. * @param roller the roller context created by AverageRoller_new()
  61. * @param newEntry the a new number to be factored into the average.
  62. * @return the average over the last windowSeconds active seconds.
  63. */
  64. uint32_t AverageRoller_update(struct AverageRoller* roller, const uint32_t newEntry);
  65. #endif