summaryrefslogtreecommitdiff
path: root/chromium/device/vr/util/sample_queue.cc
blob: 01787adccb91a816b130afdae67cc894fa5332a5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
// Copyright 2017 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include <stdint.h>

#include "device/vr/util/sample_queue.h"

namespace device {

SampleQueue::SampleQueue(size_t window_size) : window_size_(window_size) {
  samples_.reserve(window_size);
}

SampleQueue::~SampleQueue() = default;

void SampleQueue::AddSample(int64_t value) {
  sum_ += value;

  if (samples_.size() < window_size_) {
    samples_.push_back(value);
  } else {
    sum_ -= samples_[current_index_];
    samples_[current_index_] = value;
  }

  ++current_index_;
  if (current_index_ >= window_size_) {
    current_index_ = 0;
  }
}

}  // namespace device