mfbt/RollingMean.h
author Geoff Brown <gbrown@mozilla.com>
Fri, 12 May 2017 15:18:38 -0600
changeset 406366 f0f6bd0a45434bcd6159ae9f7c4f558367332fe7
parent 266851 fba3aaf06f106b4aa9aab2d6299b6b35abcf8df9
child 434354 b0e818e98bb51abf5336484a7cf47b10ddfb88fa
permissions -rw-r--r--
Bug 1361732 - Mozharness support for a second virtualenv using python3; r=aki Currently Mozharness creates a Python 2.7 virtualenvironment with all the packages needed for Mozharness to run plus any applications that need to execute within it. For Talos, we now need a second Python virtual environment for mitmproxy, however, this package is for Python 3 and not for Python 2 (perhaps it could have worked). In any case, having the ability to support create Python 3 virtual environments will be handy. Specially since the approach of creating virtual environments is not by using 'virtualenv /path/to/venv' but by calling the venv module like 'python3 -m venv /path/to/venv'. The initial implementation only supports a list of modules. The following iteration will support requirement files and other parameters needed for pip and internal pypi hosts. Originally authored by armenzg.

/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

/* A set abstraction for enumeration values. */

#ifndef mozilla_RollingMean_h_
#define mozilla_RollingMean_h_

#include "mozilla/Assertions.h"
#include "mozilla/TypeTraits.h"
#include "mozilla/Vector.h"

#include <stddef.h>

namespace mozilla {

/**
 * RollingMean<T> calculates a rolling mean of the values it is given. It
 * accumulates the total as values are added and removed. The second type
 * argument S specifies the type of the total. This may need to be a bigger
 * type in order to maintain that the sum of all values in the average doesn't
 * exceed the maximum input value.
 *
 * WARNING: Float types are not supported due to rounding errors.
 */
template<typename T, typename S>
class RollingMean
{
private:
  size_t mInsertIndex;
  size_t mMaxValues;
  Vector<T> mValues;
  S mTotal;

public:
  static_assert(!IsFloatingPoint<T>::value,
                "floating-point types are unsupported due to rounding "
                "errors");

  explicit RollingMean(size_t aMaxValues)
    : mInsertIndex(0),
      mMaxValues(aMaxValues),
      mTotal(0)
  {
    MOZ_ASSERT(aMaxValues > 0);
  }

  RollingMean& operator=(RollingMean&& aOther)
  {
    MOZ_ASSERT(this != &aOther, "self-assignment is forbidden");
    this->~RollingMean();
    new(this) RollingMean(aOther.mMaxValues);
    mInsertIndex = aOther.mInsertIndex;
    mTotal = aOther.mTotal;
    mValues.swap(aOther.mValues);
    return *this;
  }

  /**
   * Insert a value into the rolling mean.
   */
  bool insert(T aValue)
  {
    MOZ_ASSERT(mValues.length() <= mMaxValues);

    if (mValues.length() == mMaxValues) {
      mTotal = mTotal - mValues[mInsertIndex] + aValue;
      mValues[mInsertIndex] = aValue;
    } else {
      if (!mValues.append(aValue)) {
        return false;
      }
      mTotal = mTotal + aValue;
    }

    mInsertIndex = (mInsertIndex + 1) % mMaxValues;
    return true;
  }

  /**
   * Calculate the rolling mean.
   */
  T mean()
  {
    MOZ_ASSERT(!empty());
    return T(mTotal / int64_t(mValues.length()));
  }

  bool empty()
  {
    return mValues.empty();
  }

  /**
   * Remove all values from the rolling mean.
   */
  void clear()
  {
    mValues.clear();
    mInsertIndex = 0;
    mTotal = T(0);
  }

  size_t maxValues()
  {
    return mMaxValues;
  }
};

} // namespace mozilla

#endif // mozilla_RollingMean_h_