Tanoda
SlidingMax.cs
Go to the documentation of this file.
1/******************************************************************************
2 * Copyright (C) Ultraleap, Inc. 2011-2020. *
3 * *
4 * Use subject to the terms of the Apache License 2.0 available at *
5 * http://www.apache.org/licenses/LICENSE-2.0, or another agreement *
6 * between Ultraleap and you, your company or other organization. *
7 ******************************************************************************/
8
9namespace Leap.Unity {
10
11 public class SlidingMax {
12
13 private struct IndexValuePair {
14 public int index;
15 public float value;
16
17 public IndexValuePair(int index, float value) {
18 this.index = index;
19 this.value = value;
20 }
21 }
22
23 private int _history;
24 private int _count;
25 private Deque<IndexValuePair> _buffer = new Deque<IndexValuePair>();
26
27 public SlidingMax(int history) {
28 _history = history;
29 _count = 0;
30 }
31
32 public void AddValue(float value) {
33 while (_buffer.Count != 0 && _buffer.Front.value <= value) {
34 _buffer.PopFront();
35 }
36
37 _buffer.PushFront(new IndexValuePair(_count, value));
38 _count++;
39
40 while (_buffer.Back.index < (_count - _history)) {
41 _buffer.PopBack();
42 }
43 }
44
45 public float Max {
46 get {
47 return _buffer.Back.value;
48 }
49 }
50 }
51}
void PopFront()
Definition: Deque.cs:70
void PopBack()
Definition: Deque.cs:63
void PushFront(T t)
Definition: Deque.cs:56
SlidingMax(int history)
Definition: SlidingMax.cs:27
void AddValue(float value)
Definition: SlidingMax.cs:32