@Test
public void testDirectBufferLoop() throws Exception
{
// [[[start:iteration-deques-using-direct-buffer-access]]]
final IntArrayDeque deque = prepare(10);
// For the fastest iteration, you can access the deque's data buffer directly.
// Note that this time it's a little more complicated than with array lists.
final int [] buffer = deque.buffer;
final int bufferSize = buffer.length;
// Iteration from head to tail
final int forwardStart = deque.head;
final int forwardStop = forwardStart + deque.size();
for (int i = forwardStart; i < forwardStop; i++)
{
System.out.println(buffer[i % bufferSize]);
}
// Iteration from tail to head
final int backwardStart = deque.tail + bufferSize - 1;
final int backwardStop = backwardStart - deque.size();
for (int i = backwardStart; i > backwardStop; i--)
{
System.out.println(buffer[i % bufferSize]);
}
// [[[end:iteration-deques-using-direct-buffer-access]]]