mfbt/RollingMean.h

Tue, 06 Jan 2015 21:39:09 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Tue, 06 Jan 2015 21:39:09 +0100
branch
TOR_BUG_9701
changeset 8
97036ab72558
permissions
-rw-r--r--

Conditionally force memory storage according to privacy.thirdparty.isolate;
This solves Tor bug #9701, complying with disk avoidance documented in
https://www.torproject.org/projects/torbrowser/design/#disk-avoidance.

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

mercurial