RollingMean.h 2.6 KB

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