tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

RollingMean.h (2393B)


      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 
      7 /* Calculate the rolling mean of a series of values. */
      8 
      9 #ifndef mozilla_RollingMean_h_
     10 #define mozilla_RollingMean_h_
     11 
     12 #include "mozilla/Assertions.h"
     13 #include "mozilla/Vector.h"
     14 
     15 #include <stddef.h>
     16 #include <type_traits>
     17 
     18 namespace mozilla {
     19 
     20 /**
     21 * RollingMean<T> calculates a rolling mean of the values it is given. It
     22 * accumulates the total as values are added and removed. The second type
     23 * argument S specifies the type of the total. This may need to be a bigger
     24 * type in order to maintain that the sum of all values in the average doesn't
     25 * exceed the maximum input value.
     26 *
     27 * WARNING: Float types are not supported due to rounding errors.
     28 */
     29 template <typename T, typename S>
     30 class RollingMean {
     31 private:
     32  size_t mInsertIndex;
     33  size_t mMaxValues;
     34  Vector<T> mValues;
     35  S mTotal;
     36 
     37 public:
     38  static_assert(!std::is_floating_point_v<T>,
     39                "floating-point types are unsupported due to rounding "
     40                "errors");
     41 
     42  explicit RollingMean(size_t aMaxValues)
     43      : mInsertIndex(0), mMaxValues(aMaxValues), mTotal(0) {
     44    MOZ_ASSERT(aMaxValues > 0);
     45  }
     46 
     47  RollingMean& operator=(RollingMean&& aOther) = default;
     48 
     49  /**
     50   * Insert a value into the rolling mean.
     51   */
     52  bool insert(T aValue) {
     53    MOZ_ASSERT(mValues.length() <= mMaxValues);
     54 
     55    if (mValues.length() == mMaxValues) {
     56      mTotal = mTotal - mValues[mInsertIndex] + aValue;
     57      mValues[mInsertIndex] = aValue;
     58    } else {
     59      if (!mValues.append(aValue)) {
     60        return false;
     61      }
     62      mTotal = mTotal + aValue;
     63    }
     64 
     65    mInsertIndex = (mInsertIndex + 1) % mMaxValues;
     66    return true;
     67  }
     68 
     69  /**
     70   * Calculate the rolling mean.
     71   */
     72  T mean() const {
     73    MOZ_ASSERT(!empty());
     74    return T(mTotal / int64_t(mValues.length()));
     75  }
     76 
     77  bool empty() const { return mValues.empty(); }
     78 
     79  /**
     80   * Remove all values from the rolling mean.
     81   */
     82  void clear() {
     83    mValues.clear();
     84    mInsertIndex = 0;
     85    mTotal = T(0);
     86  }
     87 
     88  size_t maxValues() const { return mMaxValues; }
     89 };
     90 
     91 }  // namespace mozilla
     92 
     93 #endif  // mozilla_RollingMean_h_