Deque.cs
5.46 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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
/******************************************************************************
* Copyright (C) Leap Motion, Inc. 2011-2017. *
* Leap Motion proprietary and confidential. *
* *
* Use subject to the terms of the Leap Motion SDK Agreement available at *
* https://developer.leapmotion.com/sdk_agreement, or another agreement *
* between Leap Motion and you, your company or other organization. *
******************************************************************************/
using System;
using UnityEngine;
namespace Leap.Unity {
public class Deque<T> {
private T[] _array;
private uint _front, _count;
private uint _indexMask;
public Deque(int minCapacity = 8) {
if (minCapacity <= 0) {
throw new ArgumentException("Capacity must be positive and nonzero.");
}
//Find next highest power of two
int capacity = Mathf.ClosestPowerOfTwo(minCapacity);
if (capacity < minCapacity) {
capacity *= 2;
}
_array = new T[capacity];
recalculateIndexMask();
_front = 0;
_count = 0;
}
public int Count {
get {
return (int)_count;
}
}
public void Clear() {
if (_count != 0) {
Array.Clear(_array, 0, _array.Length);
_front = 0;
_count = 0;
}
}
public void PushBack(T t) {
doubleCapacityIfFull();
++_count;
_array[getBackIndex()] = t;
}
public void PushFront(T t) {
doubleCapacityIfFull();
++_count;
_front = (_front - 1) & _indexMask;
_array[_front] = t;
}
public void PopBack() {
checkForEmpty("pop back");
_array[getBackIndex()] = default(T);
--_count;
}
public void PopFront() {
checkForEmpty("pop front");
_array[_front] = default(T);
--_count;
_front = (_front + 1) & _indexMask;
}
public void PopBack(out T back) {
checkForEmpty("pop back");
uint backIndex = getBackIndex();
back = _array[backIndex];
_array[backIndex] = default(T);
--_count;
}
public void PopFront(out T front) {
checkForEmpty("pop front");
front = _array[_front];
_array[_front] = default(T);
_front = (_front + 1) & _indexMask;
--_count;
}
public T Front {
get {
checkForEmpty("get front");
return _array[_front];
}
set {
checkForEmpty("set front");
_array[_front] = value;
}
}
public T Back {
get {
checkForEmpty("get back");
return _array[getBackIndex()];
}
set {
checkForEmpty("set back");
_array[getBackIndex()] = value;
}
}
public T this[int index] {
get {
uint uindex = (uint)index;
checkForValidIndex(uindex);
return _array[getIndex(uindex)];
}
set {
uint uindex = (uint)index;
checkForValidIndex(uindex);
_array[getIndex(uindex)] = value;
}
}
public string ToDebugString() {
string debug = "[";
uint back = getBackIndex();
for (uint i = 0; i < _array.Length; i++) {
bool isEmpty;
if (_count == 0) {
isEmpty = true;
} else if (_count == 1) {
isEmpty = i != _front;
} else if (_front < back) {
isEmpty = (i < _front) || (i > back);
} else {
isEmpty = (i < _front) && (i > back);
}
string element = "";
if (i == _front) {
element = "{";
} else {
element = " ";
}
if (isEmpty) {
element += ".";
} else {
element += _array[i].ToString();
}
if (i == back) {
element += "}";
} else {
element += " ";
}
debug += element;
}
debug += "]";
return debug;
}
private uint getBackIndex() {
return (_front + _count - 1) & _indexMask;
}
private uint getIndex(uint index) {
return (_front + index) & _indexMask;
}
private void doubleCapacityIfFull() {
if (_count >= _array.Length) {
T[] newArray = new T[_array.Length * 2];
uint front = getBackIndex();
if (_front <= front) {
//values do not wrap around, we can use a simple copy
Array.Copy(_array, _front, newArray, 0, _count);
} else {
//values do wrap around, we need to use 2 copies
uint backOffset = (uint)_array.Length - _front;
Array.Copy(_array, _front, newArray, 0, backOffset);
Array.Copy(_array, 0, newArray, backOffset, _count - backOffset);
}
_front = 0;
_array = newArray;
recalculateIndexMask();
}
}
private void recalculateIndexMask() {
//array length is always power of 2, so length-1 is the bitmask we need
//8 = 1000
//7 = 0111 = mask for values 0-7
_indexMask = (uint)_array.Length - 1;
}
private void checkForValidIndex(uint index) {
if (index >= _count) {
throw new IndexOutOfRangeException("The index " + index + " was out of range for the RingBuffer with size " + _count + ".");
}
}
private void checkForEmpty(string actionName) {
if (_count == 0) {
throw new InvalidOperationException("Cannot " + actionName + " because the RingBuffer is empty.");
}
}
}
}