Package de.gsi.dataset.utils
Class DoubleCircularBuffer
- java.lang.Object
-
- de.gsi.dataset.utils.DoubleCircularBuffer
-
public class DoubleCircularBuffer extends java.lang.Objectsimple circular ring buffer implementation for double type (with read == write position)- Author:
- rstein
-
-
Constructor Summary
Constructors Constructor Description DoubleCircularBuffer(double[] initalElements, int capacity)DoubleCircularBuffer(int capacity)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description intavailable()intcapacity()double[]elements()doubleget()double[]get(double[] into, int length)double[]get(double[] into, int readPos, int length)doubleget(int readPos)protected intgetIndex(int readPos)booleanisBufferFlipped()booleanput(double element)add new elementintput(double[] newElements, int length)add multiple new elementsintput(double[] newElements, int startIndex, int length)add multiple new elementsintremainingCapacity()doublereplace(double element)doublereplace(double element, int atIndex)voidreset()resets and clears bufferintwritePosition()
-
-
-
Constructor Detail
-
DoubleCircularBuffer
public DoubleCircularBuffer(double[] initalElements, int capacity)- Parameters:
initalElements- adds element the buffer should be initialised withcapacity- maximum capacity of the buffer
-
DoubleCircularBuffer
public DoubleCircularBuffer(int capacity)
- Parameters:
capacity- maximum capacity of buffer
-
-
Method Detail
-
available
public int available()
- Returns:
- number of available buffer elements
-
capacity
public int capacity()
- Returns:
- the maximum possible/filled number of available buffer elements
-
elements
public double[] elements()
- Returns:
- internal field array N.B. this is the raw internal double pointer do not use this unless you know what you are doing
-
get
public double get()
- Returns:
- value at head
-
get
public double[] get(double[] into, int length)- Parameters:
into- storage containerlength- number of elements to be read- Returns:
- either into or newly allocated array containing the result
-
get
public double[] get(double[] into, int readPos, int length)- Parameters:
into- storage containerreadPos- circular index (wraps around)length- number of elements to be read- Returns:
- either into or newly allocated array containing the result
-
get
public double get(int readPos)
- Parameters:
readPos- circular index (wraps around)- Returns:
- the value
-
getIndex
protected int getIndex(int readPos)
-
isBufferFlipped
public boolean isBufferFlipped()
- Returns:
- whether write position exceeded at least once the capacity
-
put
public boolean put(double element)
add new element- Parameters:
element- new element- Returns:
- true
-
put
public int put(double[] newElements, int length)add multiple new elements- Parameters:
newElements- array of new elementslength- number of elements that are to be written from array- Returns:
- true: write index is smaller than read index
-
put
public int put(double[] newElements, int startIndex, int length)add multiple new elements- Parameters:
newElements- array of new elementsstartIndex- 'null'length- number of elements that are to be written from array- Returns:
- true: write index is smaller than read index
-
remainingCapacity
public int remainingCapacity()
- Returns:
- number of elements that can be written before buffer wraps-around
-
replace
public double replace(double element)
- Parameters:
element- to replace an existing element at the head buffer position- Returns:
- the previous element stored at that location
-
replace
public double replace(double element, int atIndex)- Parameters:
element- to replace an existing element at given buffer positionatIndex- index at which to replace the value- Returns:
- the previous element stored at that location
-
reset
public void reset()
resets and clears buffer
-
writePosition
public int writePosition()
- Returns:
- internal write position
-
-