-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCircularBuffer.cs
More file actions
39 lines (31 loc) · 1.15 KB
/
CircularBuffer.cs
File metadata and controls
39 lines (31 loc) · 1.15 KB
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
34
35
36
37
38
39
using System;
namespace Plugins.LeapMotion {
/// <summary>
/// Circular LIFO Buffer
/// </summary>
public class CircularBuffer<T> {
T[] _buffer;
// define the current Frame to fill, so the previous one is the last filled and the real current
int _currentToFill = 0;
public CircularBuffer(int size) {
_buffer = new T[size];
}
public void Put(T o) {
_buffer[_currentToFill] = o;
_currentToFill = WrapIndex(_currentToFill + 1);
}
/// <returns>Return the last filled value</returns>
public T Get() {
return History(0);
}
public T History(int history) {
if (history < 0 || history >= _buffer.Length) throw new ArgumentException();
return _buffer[WrapIndex(_currentToFill - 1 - history)];
}
int WrapIndex(int index) {
if (index <= -_buffer.Length) throw new NotSupportedException();
if (index >= 0) return index % _buffer.Length;
return index + _buffer.Length;
}
}
}