Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><a href="http://bradforj287.blogspot.com/2010/11" rel="nofollow">Efficient Circular Buffer in Java</a></p> <p>Overwriting Circular buffers are great data structures to use if you would like to operate on a recent window of data. Elements are added and removed FIFO like a Queue, except additions on full buffers will cause the oldest (head of the queue) element to be removed. </p> <pre><code>import java.util.NoSuchElementException; /** * Thread safe fixed size circular buffer implementation. Backed by an array. * * @author brad */ public class ArrayCircularBuffer&lt;T&gt; { // internal data storage private T[] data; // indices for inserting and removing from queue private int front = 0; private int insertLocation = 0; // number of elements in queue private int size = 0; /** * Creates a circular buffer with the specified size. * * @param bufferSize * - the maximum size of the buffer */ public ArrayCircularBuffer(int bufferSize) { data = (T[]) new Object[bufferSize]; } /** * Inserts an item at the end of the queue. If the queue is full, the oldest * value will be removed and head of the queue will become the second oldest * value. * * @param item * - the item to be inserted */ public synchronized void insert(T item) { data[insertLocation] = item; insertLocation = (insertLocation + 1) % data.length; /** * If the queue is full, this means we just overwrote the front of the * queue. So increment the front location. */ if (size == data.length) { front = (front + 1) % data.length; } else { size++; } } /** * Returns the number of elements in the buffer * * @return int - the number of elements inside this buffer */ public synchronized int size() { return size; } /** * Returns the head element of the queue. * * @return T */ public synchronized T removeFront() { if (size == 0) { throw new NoSuchElementException(); } T retValue = data[front]; front = (front + 1) % data.length; size--; return retValue; } /** * Returns the head of the queue but does not remove it. * * @return */ public synchronized T peekFront() { if (size == 0) { return null; } else { return data[front]; } } /** * Returns the last element of the queue but does not remove it. * * @return T - the most recently added value */ public synchronized T peekLast() { if (size == 0) { return null; } else { int lastElement = insertLocation - 1; if (lastElement &lt; 0) { lastElement = data.length - 1; } return data[lastElement]; } } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload