Tanoda
RingBuffer.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
9using System;
10using UnityEngine;
11
12namespace Leap.Unity {
13
14 public class RingBuffer<T> : IIndexable<T> {
15
16 private T[] arr;
17 private int firstIdx = 0;
18 private int lastIdx = -1;
19
20 public RingBuffer(int bufferSize) {
21 bufferSize = System.Math.Max(1, bufferSize);
22 arr = new T[bufferSize];
23 }
24
25 public int Count {
26 get {
27 if (lastIdx == -1) return 0;
28
29 int endIdx = (lastIdx + 1) % arr.Length;
30
31 if (endIdx <= firstIdx) { endIdx += arr.Length; }
32 return endIdx - firstIdx;
33 }
34 }
35
36 public int Capacity {
37 get { return arr.Length; }
38 }
39
40 public bool IsFull {
41 get { return lastIdx != -1
42 && ((lastIdx + 1 + arr.Length) % arr.Length) == firstIdx; }
43 }
44
45 public bool IsEmpty {
46 get { return lastIdx == -1; }
47 }
48
52 public T this[int idx] {
53 get { return Get(idx); }
54 set { Set(idx, value); }
55 }
56
57 public void Clear() {
58 firstIdx = 0;
59 lastIdx = -1;
60 }
61
62 public void Add(T t) {
63 if (IsFull) {
64 firstIdx += 1;
65 firstIdx %= arr.Length;
66 }
67 lastIdx += 1;
68 lastIdx %= arr.Length;
69
70 arr[lastIdx] = t;
71 }
72
74 public void Push(T t) {
75 Add(t);
76 }
77
81 public T Get(int idx) {
82 if (idx < 0 || idx > Count - 1) {
83 Debug.Log("Tried to access index " + idx + " of RingBuffer with count " + Count);
84 throw new IndexOutOfRangeException();
85 }
86
87 return arr[(firstIdx + idx) % arr.Length];
88 }
89
90 public T GetLatest() {
91 if (Count == 0) {
92 throw new IndexOutOfRangeException("Can't get latest value in an empty RingBuffer.");
93 }
94
95 return Get(Count - 1);
96 }
97
98 public T GetOldest() {
99 if (Count == 0) {
100 throw new IndexOutOfRangeException("Can't get oldest value in an empty RingBuffer.");
101 }
102
103 return Get(0);
104 }
105
106 public void Set(int idx, T t) {
107 if (idx < 0 || idx > Count - 1) { throw new IndexOutOfRangeException(); }
108
109 int actualIdx = (firstIdx + idx) % arr.Length;
110 arr[actualIdx] = t;
111 }
112
113 public void SetLatest(T t) {
114 if (Count == 0) {
115 throw new IndexOutOfRangeException("Can't set latest value in an empty RingBuffer.");
116 }
117
118 Set(Count - 1, t);
119 }
120
121 public override string ToString() {
122 var sb = new System.Text.StringBuilder();
123 sb.Append("[RingBuffer: ");
124 for (int i = 0; i < this.Count; i++) {
125 sb.Append(this[i]);
126 sb.Append(", ");
127 }
128 sb.Length -= 2;
129 sb.Append("]");
130 return sb.ToString();
131 }
132
134 return new RingBufferEnumerator(this);
135 }
136
137 public struct RingBufferEnumerator {
138 private RingBuffer<T> _buffer;
139 private int _idx;
141 this._buffer = buffer;
142 this._idx = -1;
143 }
144 public bool HasCurrent { get { return _idx >= 0 && _idx < _buffer.Count; }}
145 public bool MoveNext() {
146 if (_idx >= _buffer.Count) { return false; }
147 _idx += 1;
148 if (_idx < 0 || _idx >= _buffer.Count) { return false; }
149 return true;
150 }
151 public T Current { get { return _buffer[_idx]; }}
152 }
153
154 }
155
156}
UnityEngine.Debug Debug
Definition: TanodaServer.cs:19
T Get(int idx)
Oldest element is at index 0, youngest is at Count - 1.
Definition: RingBuffer.cs:81
RingBuffer(int bufferSize)
Definition: RingBuffer.cs:20
void Push(T t)
Synonym for "Add".
Definition: RingBuffer.cs:74
RingBufferEnumerator GetEnumerator()
Definition: RingBuffer.cs:133
override string ToString()
Definition: RingBuffer.cs:121
void Set(int idx, T t)
Definition: RingBuffer.cs:106
This easy-to-implement interface represents the ability to index into a collection of elements of typ...
Definition: IIndexable.cs:21
RingBufferEnumerator(RingBuffer< T > buffer)
Definition: RingBuffer.cs:140