Skip to content
Snippets Groups Projects
Commit 238e1dc8 authored by Christian Wulf's avatar Christian Wulf
Browse files

added minimum capacity to CommittableResizableArrayQueue

parent 4104b2ed
No related branches found
No related tags found
No related merge requests found
...@@ -2,6 +2,8 @@ package teetime.util.list; ...@@ -2,6 +2,8 @@ package teetime.util.list;
public class CommittableResizableArrayQueue<T> implements CommittableQueue<T> { public class CommittableResizableArrayQueue<T> implements CommittableQueue<T> {
private final int MIN_CAPACITY;
private final ArrayPool<T> arrayPool; private final ArrayPool<T> arrayPool;
private T[] elements; private T[] elements;
...@@ -11,6 +13,7 @@ public class CommittableResizableArrayQueue<T> implements CommittableQueue<T> { ...@@ -11,6 +13,7 @@ public class CommittableResizableArrayQueue<T> implements CommittableQueue<T> {
public CommittableResizableArrayQueue(final Object emptyObject, final int initialCapacity) { public CommittableResizableArrayQueue(final Object emptyObject, final int initialCapacity) {
super(); super();
this.arrayPool = new ArrayPool<T>(); this.arrayPool = new ArrayPool<T>();
this.MIN_CAPACITY = initialCapacity;
this.elements = this.arrayPool.acquire(initialCapacity); this.elements = this.arrayPool.acquire(initialCapacity);
this.elements[0] = (T) emptyObject; // optimization: avoids the use of an index out-of-bounds check this.elements[0] = (T) emptyObject; // optimization: avoids the use of an index out-of-bounds check
...@@ -33,7 +36,7 @@ public class CommittableResizableArrayQueue<T> implements CommittableQueue<T> { ...@@ -33,7 +36,7 @@ public class CommittableResizableArrayQueue<T> implements CommittableQueue<T> {
@Override @Override
public T removeFromHeadUncommitted() { public T removeFromHeadUncommitted() {
if (this.lastFreeIndexUncommitted < this.capacity() / 2) { if (this.capacity() > this.MIN_CAPACITY && this.lastFreeIndexUncommitted < this.capacity() / 2) {
this.shrink(); this.shrink();
} }
T element = this.get(--this.lastFreeIndexUncommitted); T element = this.get(--this.lastFreeIndexUncommitted);
...@@ -73,12 +76,14 @@ public class CommittableResizableArrayQueue<T> implements CommittableQueue<T> { ...@@ -73,12 +76,14 @@ public class CommittableResizableArrayQueue<T> implements CommittableQueue<T> {
} }
private void grow() { private void grow() {
T[] newElements = this.arrayPool.acquire(this.capacity() * 2); T[] newElements = this.arrayPool.acquire(this.elements.length * 2);
// System.out.println("grow: " + this.lastFreeIndexUncommitted);
this.replaceCurrentArrayBy(newElements); this.replaceCurrentArrayBy(newElements);
} }
private void shrink() { private void shrink() {
T[] newElements = this.arrayPool.acquire(this.capacity() / 2); T[] newElements = this.arrayPool.acquire(this.elements.length / 2);
// System.out.println("shrink: " + this.lastFreeIndexUncommitted);
this.replaceCurrentArrayBy(newElements); this.replaceCurrentArrayBy(newElements);
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment