17 private uint _bufferMask;
18 private uint _head, _tail;
32 if (minCapacity <= 0) {
33 throw new ArgumentOutOfRangeException(
34 "The capacity of the ProduceConsumeBuffer must be positive and " +
39 int closestPowerOfTwo = Mathf.ClosestPowerOfTwo(minCapacity);
40 if (closestPowerOfTwo == minCapacity) {
41 capacity = minCapacity;
43 if (closestPowerOfTwo < minCapacity) {
44 capacity = closestPowerOfTwo * 2;
46 capacity = closestPowerOfTwo;
50 _buffer =
new T[capacity];
51 _bufferMask = (uint)(capacity - 1);
61 return _buffer.Length;
70 int tail = (int)_tail;
71 int head = (int)_head;
87 uint nextTail = (_tail + 1) & _bufferMask;
88 if (nextTail == _head)
return false;
131 if (_tail == _head) {
137 _head = (_head + 1) & _bufferMask;
147 if (_tail == _head) {
151 _head = (_head + 1) & _bufferMask;
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 ...