RollingMean.h 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
  2. /* vim: set ts=8 sts=2 et sw=2 tw=80: */
  3. /* This Source Code Form is subject to the terms of the Mozilla Public
  4. * License, v. 2.0. If a copy of the MPL was not distributed with this
  5. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  6. /* A set abstraction for enumeration values. */
  7. #ifndef mozilla_RollingMean_h_
  8. #define mozilla_RollingMean_h_
  9. #include "mozilla/Assertions.h"
  10. #include "mozilla/TypeTraits.h"
  11. #include "mozilla/Vector.h"
  12. #include <stddef.h>
  13. namespace mozilla {
  14. /**
  15. * RollingMean<T> calculates a rolling mean of the values it is given. It
  16. * accumulates the total as values are added and removed. The second type
  17. * argument S specifies the type of the total. This may need to be a bigger
  18. * type in order to maintain that the sum of all values in the average doesn't
  19. * exceed the maximum input value.
  20. *
  21. * WARNING: Float types are not supported due to rounding errors.
  22. */
  23. template<typename T, typename S>
  24. class RollingMean
  25. {
  26. private:
  27. size_t mInsertIndex;
  28. size_t mMaxValues;
  29. Vector<T> mValues;
  30. S mTotal;
  31. public:
  32. static_assert(!IsFloatingPoint<T>::value,
  33. "floating-point types are unsupported due to rounding "
  34. "errors");
  35. explicit RollingMean(size_t aMaxValues)
  36. : mInsertIndex(0),
  37. mMaxValues(aMaxValues),
  38. mTotal(0)
  39. {
  40. MOZ_ASSERT(aMaxValues > 0);
  41. }
  42. RollingMean& operator=(RollingMean&& aOther)
  43. {
  44. MOZ_ASSERT(this != &aOther, "self-assignment is forbidden");
  45. this->~RollingMean();
  46. new(this) RollingMean(aOther.mMaxValues);
  47. mInsertIndex = aOther.mInsertIndex;
  48. mTotal = aOther.mTotal;
  49. mValues.swap(aOther.mValues);
  50. return *this;
  51. }
  52. /**
  53. * Insert a value into the rolling mean.
  54. */
  55. bool insert(T aValue)
  56. {
  57. MOZ_ASSERT(mValues.length() <= mMaxValues);
  58. if (mValues.length() == mMaxValues) {
  59. mTotal = mTotal - mValues[mInsertIndex] + aValue;
  60. mValues[mInsertIndex] = aValue;
  61. } else {
  62. if (!mValues.append(aValue)) {
  63. return false;
  64. }
  65. mTotal = mTotal + aValue;
  66. }
  67. mInsertIndex = (mInsertIndex + 1) % mMaxValues;
  68. return true;
  69. }
  70. /**
  71. * Calculate the rolling mean.
  72. */
  73. T mean()
  74. {
  75. MOZ_ASSERT(!empty());
  76. return T(mTotal / int64_t(mValues.length()));
  77. }
  78. bool empty()
  79. {
  80. return mValues.empty();
  81. }
  82. /**
  83. * Remove all values from the rolling mean.
  84. */
  85. void clear()
  86. {
  87. mValues.clear();
  88. mInsertIndex = 0;
  89. mTotal = T(0);
  90. }
  91. size_t maxValues()
  92. {
  93. return mMaxValues;
  94. }
  95. };
  96. } // namespace mozilla
  97. #endif // mozilla_RollingMean_h_