Code cleanup and properly synchronized.

This commit is contained in:
Simone Bordet 2011-08-16 18:26:37 +02:00
parent ee1afa5e97
commit faaeabd243
1 changed files with 161 additions and 141 deletions

View File

@ -18,8 +18,9 @@ import java.util.NoSuchElementException;
import java.util.Queue; import java.util.Queue;
/* ------------------------------------------------------------ */ /* ------------------------------------------------------------ */
/** Queue backed by circular array. /**
* * Queue backed by circular array.
* <p/>
* This partial Queue implementation (also with {@link #remove()} for stack operation) * This partial Queue implementation (also with {@link #remove()} for stack operation)
* is backed by a growable circular array. * is backed by a growable circular array.
* *
@ -27,49 +28,50 @@ import java.util.Queue;
*/ */
public class ArrayQueue<E> extends AbstractList<E> implements Queue<E> public class ArrayQueue<E> extends AbstractList<E> implements Queue<E>
{ {
public final int DEFAULT_CAPACITY=64; public static final int DEFAULT_CAPACITY = 64;
public final int DEFAULT_GROWTH=32; public static final int DEFAULT_GROWTH = 32;
protected Object _lock=this;
protected final Object _lock;
protected final int _growCapacity;
protected Object[] _elements; protected Object[] _elements;
protected int _nextE; protected int _nextE;
protected int _nextSlot; protected int _nextSlot;
protected volatile int _size; protected int _size;
protected int _growCapacity;
/* ------------------------------------------------------------ */ /* ------------------------------------------------------------ */
public ArrayQueue() public ArrayQueue()
{ {
_elements=new Object[64]; this(DEFAULT_CAPACITY, -1);
_growCapacity=32;
} }
/* ------------------------------------------------------------ */ /* ------------------------------------------------------------ */
public ArrayQueue(int capacity) public ArrayQueue(int capacity)
{ {
_elements=new Object[capacity]; this(capacity, -1);
_growCapacity=-1;
} }
/* ------------------------------------------------------------ */ /* ------------------------------------------------------------ */
public ArrayQueue(int initCapacity, int growBy) public ArrayQueue(int initCapacity, int growBy)
{ {
_elements=new Object[initCapacity]; this(initCapacity, growBy, null);
_growCapacity=growBy;
} }
/* ------------------------------------------------------------ */ /* ------------------------------------------------------------ */
public ArrayQueue(int initCapacity, int growBy, Object lock) public ArrayQueue(int initCapacity, int growBy, Object lock)
{ {
_elements=new Object[initCapacity]; _lock = lock == null ? this : lock;
_growCapacity = growBy; _growCapacity = growBy;
_lock=lock; _elements = new Object[initCapacity];
} }
/* ------------------------------------------------------------ */ /* ------------------------------------------------------------ */
public int getCapacity() public int getCapacity()
{
synchronized (_lock)
{ {
return _elements.length; return _elements.length;
} }
}
/* ------------------------------------------------------------ */ /* ------------------------------------------------------------ */
@Override @Override
@ -84,6 +86,13 @@ public class ArrayQueue<E> extends AbstractList<E> implements Queue<E>
public boolean offer(E e) public boolean offer(E e)
{ {
synchronized (_lock) synchronized (_lock)
{
return enqueue(e);
}
}
/* ------------------------------------------------------------ */
private boolean enqueue(E e)
{ {
if (_size == _elements.length && !grow()) if (_size == _elements.length && !grow())
return false; return false;
@ -92,24 +101,21 @@ public class ArrayQueue<E> extends AbstractList<E> implements Queue<E>
_elements[_nextSlot++] = e; _elements[_nextSlot++] = e;
if (_nextSlot == _elements.length) if (_nextSlot == _elements.length)
_nextSlot = 0; _nextSlot = 0;
}
return true; return true;
} }
/* ------------------------------------------------------------ */ /* ------------------------------------------------------------ */
/** /**
* Add without synchronization or bounds checking * Add without synchronization or bounds checking
*
* @param e the element to add
* @see #add(Object) * @see #add(Object)
*/ */
public void addUnsafe(E e) public void addUnsafe(E e)
{ {
if (_size==_elements.length && !grow()) if (!enqueue(e))
throw new IllegalStateException("Full"); throw new IllegalStateException("Full");
_size++;
_elements[_nextSlot++]=e;
if (_nextSlot==_elements.length)
_nextSlot=0;
} }
/* ------------------------------------------------------------ */ /* ------------------------------------------------------------ */
@ -117,20 +123,26 @@ public class ArrayQueue<E> extends AbstractList<E> implements Queue<E>
{ {
synchronized (_lock) synchronized (_lock)
{ {
if (_size==0) if (isEmpty())
throw new NoSuchElementException(); throw new NoSuchElementException();
return (E)_elements[_nextE]; return at(_nextE);
} }
} }
@SuppressWarnings("unchecked")
private E at(int index)
{
return (E)_elements[index];
}
/* ------------------------------------------------------------ */ /* ------------------------------------------------------------ */
public E peek() public E peek()
{ {
synchronized (_lock) synchronized (_lock)
{ {
if (_size==0) if (isEmpty())
return null; return null;
return (E)_elements[_nextE]; return at(_nextE);
} }
} }
@ -141,14 +153,20 @@ public class ArrayQueue<E> extends AbstractList<E> implements Queue<E>
{ {
if (_size == 0) if (_size == 0)
return null; return null;
E e = (E)_elements[_nextE]; return dequeue();
}
}
/* ------------------------------------------------------------ */
private E dequeue()
{
E e = at(_nextE);
_elements[_nextE] = null; _elements[_nextE] = null;
_size--; _size--;
if (++_nextE == _elements.length) if (++_nextE == _elements.length)
_nextE = 0; _nextE = 0;
return e; return e;
} }
}
/* ------------------------------------------------------------ */ /* ------------------------------------------------------------ */
public E remove() public E remove()
@ -157,12 +175,7 @@ public class ArrayQueue<E> extends AbstractList<E> implements Queue<E>
{ {
if (_size == 0) if (_size == 0)
throw new NoSuchElementException(); throw new NoSuchElementException();
E e = (E)_elements[_nextE]; return dequeue();
_elements[_nextE]=null;
_size--;
if (++_nextE==_elements.length)
_nextE=0;
return e;
} }
} }
@ -188,13 +201,15 @@ public class ArrayQueue<E> extends AbstractList<E> implements Queue<E>
} }
} }
/* ------------------------------------------------------------ */ /* ------------------------------------------------------------ */
@Override @Override
public int size() public int size()
{
synchronized (_lock)
{ {
return _size; return _size;
} }
}
/* ------------------------------------------------------------ */ /* ------------------------------------------------------------ */
@Override @Override
@ -204,20 +219,22 @@ public class ArrayQueue<E> extends AbstractList<E> implements Queue<E>
{ {
if (index < 0 || index >= _size) if (index < 0 || index >= _size)
throw new IndexOutOfBoundsException("!(" + 0 + "<" + index + "<=" + _size + ")"); throw new IndexOutOfBoundsException("!(" + 0 + "<" + index + "<=" + _size + ")");
int i = (_nextE+index)%_elements.length; return getUnsafe(index);
return (E)_elements[i];
} }
} }
/* ------------------------------------------------------------ */ /* ------------------------------------------------------------ */
/** /**
* Get without synchronization or bounds checking. * Get without synchronization or bounds checking.
*
* @param index index of the element to return
* @return the element at the specified index
* @see #get(int) * @see #get(int)
*/ */
public E getUnsafe(int index) public E getUnsafe(int index)
{ {
int i = (_nextE + index) % _elements.length; int i = (_nextE + index) % _elements.length;
return (E)_elements[i]; return at(i);
} }
/* ------------------------------------------------------------ */ /* ------------------------------------------------------------ */
@ -230,7 +247,7 @@ public class ArrayQueue<E> extends AbstractList<E> implements Queue<E>
throw new IndexOutOfBoundsException("!(" + 0 + "<" + index + "<=" + _size + ")"); throw new IndexOutOfBoundsException("!(" + 0 + "<" + index + "<=" + _size + ")");
int i = (_nextE + index) % _elements.length; int i = (_nextE + index) % _elements.length;
E old=(E)_elements[i]; E old = at(i);
if (i < _nextSlot) if (i < _nextSlot)
{ {
@ -273,7 +290,7 @@ public class ArrayQueue<E> extends AbstractList<E> implements Queue<E>
int i = _nextE + index; int i = _nextE + index;
if (i >= _elements.length) if (i >= _elements.length)
i -= _elements.length; i -= _elements.length;
E old=(E)_elements[i]; E old = at(i);
_elements[i] = element; _elements[i] = element;
return old; return old;
} }
@ -332,7 +349,10 @@ public class ArrayQueue<E> extends AbstractList<E> implements Queue<E>
} }
} }
/* ------------------------------------------------------------ */
protected boolean grow() protected boolean grow()
{
synchronized (_lock)
{ {
if (_growCapacity <= 0) if (_growCapacity <= 0)
return false; return false;
@ -350,5 +370,5 @@ public class ArrayQueue<E> extends AbstractList<E> implements Queue<E>
_nextSlot = _size; _nextSlot = _size;
return true; return true;
} }
}
} }