T
- the type of instances to store.public class RingBufferArrayFast<T> extends Object implements Cloneable, IRingBuffer<T>
This implementation differs from the RingBufferArray
in one
point:
If setBufferSize(int asize)
decreases the size of the buffer and
it will get smaller than the actual amount of elements stored, they will get
lost. This avoids the need for an internal List to store elements
overhanging. Some tests may be left out that may speed up this
IRingBuffer
. Adding 5000000 elements was about 25 % faster
compared to the RingBufferArray
on an Athlon 1200, 256 MB RAM.
For allowing high performance single-threaded use this implementation and the
implementations of the retrievable Iterator
- instances are not
synchronized at all.
Modifier and Type | Class and Description |
---|---|
protected class |
RingBufferArrayFast.ARingBufferIterator
Base for ring buffer iterators that has access to the ring buffer by being
an non-static inner class.
|
IRingBuffer.RingBufferException
Modifier and Type | Field and Description |
---|---|
static boolean |
DEBUG
Flip the switch and you will see how the compiler changes the size of the
class file.
|
protected Object[] |
m_buffer
The internal array used as buffer.
|
protected boolean |
m_empty
Flag that marks whether this buffer is empty or not.
|
protected int |
m_headpointer
The internal index to buffer where the next element is going to be placed
(not placed yet!).
|
protected int |
m_size
The internal size of the buffer.
|
protected int |
m_tailpointer
The internal index to buffer where the next element is going to be read.
|
Constructor and Description |
---|
RingBufferArrayFast(int aSize)
Constructs a RingBuffer with the given size.
|
Modifier and Type | Method and Description |
---|---|
T |
add(T anObject)
Adds an element to the ring buffer, potentially removing the first element
to make more room.
|
void |
clear()
Fast method to clear the buffer - only needs to set three primitive
members.
|
protected Object |
clone() |
boolean |
equals(Object obj) |
int |
getBufferSize()
Returns the absolute amount of space in the buffer.
|
T |
getOldest()
Returns the oldest element from the buffer.
|
T |
getYoungest()
Returns the last element added.
|
int |
hashCode() |
boolean |
isEmpty()
Tests whether no elements are stored in the buffer.
|
boolean |
isFull()
Returns true if no more space in the buffer is available.
|
Iterator<T> |
iterator()
Delegates to
. |
Iterator<T> |
iteratorF2L()
Returns an
Iterator that will return the elements in exactly
the inverse order the subsequent call to remove() would do. |
Iterator<T> |
iteratorL2F()
Returns an
Iterator that will return the elements in exactly
the order the subsequent call to remove() would do. |
T |
remove()
Removes the oldest element from the buffer.
|
T[] |
removeAll()
Clears the buffer.
|
void |
setBufferSize(int newSize)
Sets a new buffer- size.
|
int |
size()
Returns the actual amount of elements stored in the buffer.
|
String |
toString()
Returns a string representation of the RingBuffer and it's contents.
|
public static final boolean DEBUG
protected Object[] m_buffer
protected boolean m_empty
headpointer | +---+---+---+---+ | 0 | 1 | 2 | 3 | +---+---+---+---+ | tailpointer From where to where are the elements? Where is empty space? empty == true: 0 elements are contained: buffer empty empty == false: 4 elements are contained: buffer full remember: -the headpointer points to the space where the next element will be inserted. -the tailpointer points to the space to read the next element from.
protected int m_headpointer
protected int m_size
For performance reasons the size of the buffer -1!
protected int m_tailpointer
public RingBufferArrayFast(int aSize)
aSize
- the size of the buffer.public T add(T anObject)
add
in interface IRingBuffer<T>
anObject
- the instance to add.public void clear()
clear
in interface IRingBuffer<T>
IRingBuffer.clear()
protected Object clone() throws CloneNotSupportedException
clone
in class Object
CloneNotSupportedException
Object.clone()
public boolean equals(Object obj)
equals
in class Object
Object.equals(java.lang.Object)
public int getBufferSize()
IRingBuffer
getBufferSize
in interface IRingBuffer<T>
IRingBuffer.getBufferSize()
public T getOldest() throws IRingBuffer.RingBufferException
IRingBuffer
getOldest
in interface IRingBuffer<T>
IRingBuffer.RingBufferException
- if the buffer is empty.IRingBuffer.getOldest()
public T getYoungest() throws IRingBuffer.RingBufferException
IRingBuffer
getYoungest
in interface IRingBuffer<T>
IRingBuffer.RingBufferException
- if the buffer is empty.IRingBuffer.getYoungest()
public int hashCode()
hashCode
in class Object
Object.hashCode()
public boolean isEmpty()
IRingBuffer
isEmpty
in interface IRingBuffer<T>
IRingBuffer.isEmpty()
public boolean isFull()
IRingBuffer
IRingBuffer.add(Object)
.
isFull
in interface IRingBuffer<T>
IRingBuffer.isFull()
public Iterator<T> iterator()
iteratorL2F()
.
iterator
in interface Iterable<T>
Iterable.iterator()
public Iterator<T> iteratorF2L()
Iterator
that will return the elements in exactly
the inverse order the subsequent call to remove()
would do.
The youngest elements are returned first. The Iterator
returned is not thread- safe!
iteratorF2L
in interface IRingBuffer<T>
Iterator
that will return the elements in exactly
the inverse order the subsequent call to remove()
would do.public Iterator<T> iteratorL2F()
Iterator
that will return the elements in exactly
the order the subsequent call to remove()
would do.
The oldest elements are returned first. The Iterator
returned is not thread- safe!
iteratorL2F
in interface IRingBuffer<T>
Iterator
that will return the elements in exactly
the order the subsequent call to remove()
would do.public T remove()
IRingBuffer
remove
in interface IRingBuffer<T>
IRingBuffer.remove()
public T[] removeAll()
IRingBuffer
removeAll
in interface IRingBuffer<T>
IRingBuffer.removeAll()
public void setBufferSize(int newSize)
A new size is assigned but the elements "overhanging" are returned by the
Object remove()
- method first. This may take time until the
buffer has its actual size again. Don't pretend on calling this method for
saving of memory very often as the whole buffer has to be copied into a new
array every time- and if newSize < getSize() additional the overhanging
elements references have to be moved to the internal
List pendingremove
.
setBufferSize
in interface IRingBuffer<T>
newSize
- the new size of the buffer.public int size()
IRingBuffer
size
in interface IRingBuffer<T>
IRingBuffer.size()
public String toString()
Don't call this in your application too often: hard arraycopy - operation an malloc are triggered.
Copyright © 2001 - 2010 LGPL, All Rights Footloose.