Tanoda
ProduceConsumeBuffer.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 UnityEngine;
10using System;
11
12namespace Leap.Unity {
13
14 public class ProduceConsumeBuffer<T> {
15
16 private T[] _buffer;
17 private uint _bufferMask;
18 private uint _head, _tail;
19
31 public ProduceConsumeBuffer(int minCapacity) {
32 if (minCapacity <= 0) {
33 throw new ArgumentOutOfRangeException(
34 "The capacity of the ProduceConsumeBuffer must be positive and " +
35 "non-zero.");
36 }
37
38 int capacity;
39 int closestPowerOfTwo = Mathf.ClosestPowerOfTwo(minCapacity);
40 if (closestPowerOfTwo == minCapacity) {
41 capacity = minCapacity;
42 } else {
43 if (closestPowerOfTwo < minCapacity) {
44 capacity = closestPowerOfTwo * 2;
45 } else {
46 capacity = closestPowerOfTwo;
47 }
48 }
49
50 _buffer = new T[capacity];
51 _bufferMask = (uint)(capacity - 1);
52 _head = 0;
53 _tail = 0;
54 }
55
59 public int Capacity {
60 get {
61 return _buffer.Length;
62 }
63 }
64
68 public int Count {
69 get {
70 int tail = (int)_tail;
71 int head = (int)_head;
72
73 if (tail < head) {
74 tail += Capacity;
75 }
76
77 return tail - head;
78 }
79 }
80
86 public bool TryEnqueue(ref T t) {
87 uint nextTail = (_tail + 1) & _bufferMask;
88 if (nextTail == _head) return false;
89
90 _buffer[_tail] = t;
91 _tail = nextTail;
92 return true;
93 }
94
100 public bool TryEnqueue(T t) {
101 return TryEnqueue(ref t);
102 }
103
112 public bool TryPeek(out T t) {
113 if (Count == 0) {
114 t = default(T);
115 return false;
116 } else {
117 // No risk of an enqueue corrupting this element
118 // since we don't modify head or tail, an enqueue targeting this element
119 // would fail.
120 t = _buffer[_head];
121 return true;
122 }
123 }
124
130 public bool TryDequeue(out T t) {
131 if (_tail == _head) {
132 t = default(T);
133 return false;
134 }
135
136 t = _buffer[_head];
137 _head = (_head + 1) & _bufferMask;
138 return true;
139 }
140
146 public bool TryDequeue() {
147 if (_tail == _head) {
148 return false;
149 }
150
151 _head = (_head + 1) & _bufferMask;
152 return true;
153 }
154
160 public bool TryDequeueAll(out T mostRecent) {
161 if (!TryDequeue(out mostRecent)) {
162 return false;
163 }
164
165 T temp;
166 while (TryDequeue(out temp)) {
167 mostRecent = temp;
168 }
169
170 return true;
171 }
172 }
173}
int Capacity
Returns the maximum number of elements that the buffer can hold.
bool TryDequeue()
Tries to dequeue a value off of the buffer. If the buffer is empty this method will perform no action...
bool TryEnqueue(ref T t)
Tries to enqueue a value into the buffer. If the buffer is already full, this method will perform no ...
bool TryPeek(out T t)
Tries to get the next element that would be dequeued from this buffer. If there is no element yet,...
bool TryDequeueAll(out T mostRecent)
Tries to dequeue all values off of the buffer, returning the most recently added element....
bool TryDequeue(out T t)
Tries to dequeue a value off of the buffer. If the buffer is empty this method will perform no action...
int Count
Returns the current number of elements that are held inside the buffer.
ProduceConsumeBuffer(int minCapacity)
Constructs a new produce consumer buffer of at least a certain capacity. Once the buffer is created,...
bool TryEnqueue(T t)
Tries to enqueue a value into the buffer. If the buffer is already full, this method will perform no ...