Package io.deephaven.base.ringbuffer
Class DoubleRingBuffer
java.lang.Object
io.deephaven.base.ringbuffer.DoubleRingBuffer
- All Implemented Interfaces:
Serializable
A simple circular buffer for primitive values, like java.util.concurrent.ArrayBlockingQueue but without the
synchronization and collection overhead. Storage is between head (inclusive) and tail (exclusive) using incrementing
long
values. Head and tail will not wrap around; instead we use storage arrays sized to 2^N to allow fast
determination of storage indices through a mask operation.- See Also:
-
Nested Class Summary
-
Constructor Summary
ConstructorDescriptionDoubleRingBuffer
(int capacity) Create an unbounded-growth ring buffer of double primitives.DoubleRingBuffer
(int capacity, boolean growable) Create a ring buffer of double primitives. -
Method Summary
Modifier and TypeMethodDescriptionboolean
add
(double e) Adds an entry to the ring buffer, will throw an exception if buffer is full.double
addOverwrite
(double e, double notFullResult) Add an entry to the ring buffer.void
addUnsafe
(double e) Add values without overflow detection.double
back()
Returns the element at the tail of the ring bufferint
capacity()
void
clear()
protected void
copyRingBufferToArray
(double[] dest) Copy the contents of the buffer to a destination buffer.double
element()
If the ring buffer is non-empty, returns the element at the head of the ring buffer.void
ensureRemaining
(int count) Ensure that there is sufficient empty space to storecount
items in the buffer.double
front()
Returns the element at the head of the ring bufferdouble
front
(int offset) Returns the element at the specified offset in the ring buffer.double[]
getAll()
Make a copy of the elements in the ring buffer.protected void
grow
(int increase) Increase the capacity of the ring buffer.boolean
isEmpty()
boolean
isFull()
iterator()
Create an iterator for the ring bufferboolean
offer
(double e) Attempt to add an entry to the ring buffer.double
peek
(double onEmpty) If the ring buffer is non-empty, returns the element at the head of the ring buffer.double
peekBack
(double onEmpty) If the ring buffer is non-empty, returns the element at the tail of the ring buffer.double
poll
(double onEmpty) If the ring buffer is non-empty, removes the element at the head of the ring buffer.int
double
remove()
Remove one element from the front of the ring buffer.double[]
remove
(int count) Remove multiple elements from the front of the ring bufferdouble
Remove an element without empty buffer detection.int
size()
-
Constructor Details
-
DoubleRingBuffer
public DoubleRingBuffer(int capacity) Create an unbounded-growth ring buffer of double primitives.- Parameters:
capacity
- minimum capacity of the ring buffer
-
DoubleRingBuffer
public DoubleRingBuffer(int capacity, boolean growable) Create a ring buffer of double primitives.- Parameters:
capacity
- minimum capacity of ring buffergrowable
- whether to allow growth when the buffer is full.
-
-
Method Details
-
grow
protected void grow(int increase) Increase the capacity of the ring buffer.- Parameters:
increase
- Increase amount. The ring buffer's capacity will be increased by at least this amount.
-
copyRingBufferToArray
protected void copyRingBufferToArray(double[] dest) Copy the contents of the buffer to a destination buffer. If the destination buffer capacity is smaller thansize()
, the copy will not fail but will terminate after the buffer is full.- Parameters:
dest
- The destination buffer.
-
isFull
public boolean isFull() -
isEmpty
public boolean isEmpty() -
size
public int size() -
capacity
public int capacity() -
remaining
public int remaining() -
clear
public void clear() -
add
public boolean add(double e) Adds an entry to the ring buffer, will throw an exception if buffer is full. For a graceful failure, useoffer(double)
- Parameters:
e
- the double to be added to the buffer- Returns:
true
if the double was added successfully- Throws:
UnsupportedOperationException
- whengrowable
isfalse
and buffer is full
-
ensureRemaining
public void ensureRemaining(int count) Ensure that there is sufficient empty space to storecount
items in the buffer. If the buffer isgrowable
, this may result in an internal growth operation. This call should be used in conjunction withaddUnsafe(double)
.- Parameters:
count
- the minimum number of empty entries in the buffer after this call- Throws:
UnsupportedOperationException
- whengrowable
isfalse
and buffer is full
-
addUnsafe
public void addUnsafe(double e) Add values without overflow detection. The caller *must* ensure that there is at least one element of free space in the ring buffer before calling this method. The caller may useensureRemaining(int)
orremaining()
for this purpose.- Parameters:
e
- the value to add to the buffer
-
addOverwrite
public double addOverwrite(double e, double notFullResult) Add an entry to the ring buffer. If the buffer is full, will overwrite the oldest entry with the new one.- Parameters:
e
- the double to be added to the buffernotFullResult
- value to return is the buffer is not full- Returns:
- the overwritten entry if the buffer is full, the provided value otherwise
-
offer
public boolean offer(double e) Attempt to add an entry to the ring buffer. If the buffer is full, the add will fail and the buffer will not grow even if growable.- Parameters:
e
- the double to be added to the buffer- Returns:
- true if the value was added successfully, false otherwise
-
remove
public double[] remove(int count) Remove multiple elements from the front of the ring buffer- Parameters:
count
- The number of elements to remove.- Throws:
NoSuchElementException
- if the buffer is empty
-
remove
public double remove()Remove one element from the front of the ring buffer.- Throws:
NoSuchElementException
- if the buffer is empty
-
removeUnsafe
public double removeUnsafe()Remove an element without empty buffer detection. The caller *must* ensure that there is at least one element in the ring buffer. Thesize()
method may be used for this purpose.- Returns:
- the value removed from the buffer
-
poll
public double poll(double onEmpty) If the ring buffer is non-empty, removes the element at the head of the ring buffer. Otherwise does nothing.- Parameters:
onEmpty
- the value to return if the ring buffer is empty- Returns:
- The removed element if the ring buffer was non-empty, otherwise the value of 'onEmpty'
-
element
public double element()If the ring buffer is non-empty, returns the element at the head of the ring buffer.- Returns:
- The head element if the ring buffer is non-empty, otherwise the value of 'onEmpty'
- Throws:
NoSuchElementException
- if the buffer is empty
-
peek
public double peek(double onEmpty) If the ring buffer is non-empty, returns the element at the head of the ring buffer. Otherwise returns the specified element.- Parameters:
onEmpty
- the value to return if the ring buffer is empty- Returns:
- The head element if the ring buffer is non-empty, otherwise the value of 'onEmpty'
-
front
public double front()Returns the element at the head of the ring buffer- Returns:
- The element at the head of the ring buffer
-
front
public double front(int offset) Returns the element at the specified offset in the ring buffer.- Parameters:
offset
- The specified offset.- Returns:
- The element at the specified offset
- Throws:
NoSuchElementException
- if the buffer is empty
-
back
public double back()Returns the element at the tail of the ring buffer- Returns:
- The element at the tail of the ring buffer
- Throws:
NoSuchElementException
- if the buffer is empty
-
peekBack
public double peekBack(double onEmpty) If the ring buffer is non-empty, returns the element at the tail of the ring buffer. Otherwise returns the specified element.- Parameters:
onEmpty
- the value to return if the ring buffer is empty- Returns:
- The tail element if the ring buffer is non-empty, otherwise the value of 'onEmpty'
-
getAll
public double[] getAll()Make a copy of the elements in the ring buffer.- Returns:
- An array containing a copy of the elements in the ring buffer.
-
iterator
Create an iterator for the ring buffer
-