From 679f78869d0ce5dbdd57cc4aa250bf13cfd1e756 Mon Sep 17 00:00:00 2001 From: Alexey Kudravtsev Date: Thu, 30 Jun 2011 19:05:47 +0400 Subject: [PATCH] indexing bug fix --- .../com/intellij/util/containers/Queue.java | 28 +++++++++++-------- 1 file changed, 17 insertions(+), 11 deletions(-) diff --git a/platform/util/src/com/intellij/util/containers/Queue.java b/platform/util/src/com/intellij/util/containers/Queue.java index 0ddf7b38fea1..c6d373e22943 100644 --- a/platform/util/src/com/intellij/util/containers/Queue.java +++ b/platform/util/src/com/intellij/util/containers/Queue.java @@ -22,7 +22,10 @@ public class Queue { private Object[] myArray; private int myFirst; private int myLast; - private boolean isInverted; + + // if true, elements are located at myFirst..myArray.length and 0..myLast + // otherwise, they are at myFirst..myLast + private boolean isWrapped; public Queue(int initialCapacity) { myArray = new Object[initialCapacity]; @@ -34,12 +37,12 @@ public class Queue { myArray = normalize(currentSize * 2); myFirst = 0; myLast = currentSize; - isInverted = false; + isWrapped = false; } myArray[myLast] = object; myLast++; if (myLast == myArray.length) { - isInverted = !isInverted; + isWrapped = !isWrapped; myLast = 0; } } @@ -49,20 +52,24 @@ public class Queue { } public int size() { - return isInverted ? myArray.length - myFirst + myLast : myLast - myFirst; + return isWrapped ? myArray.length - myFirst + myLast : myLast - myFirst; } public List toList() { return Arrays.asList(normalize(size())); } + public Object[] toArray() { + return normalize(size()); + } + public T pullFirst() { T result = (T)myArray[myFirst]; myArray[myFirst] = null; myFirst++; if (myFirst == myArray.length) { myFirst = 0; - isInverted = !isInverted; + isWrapped = !isWrapped; } return result; } @@ -79,14 +86,13 @@ public class Queue { private T[] normalize(int capacity) { T[] result = (T[])new Object[capacity]; - int tailLength; - if (isInverted) { - tailLength = copyFromTo(myFirst, myArray.length, result, 0); + if (isWrapped) { + int tailLength = copyFromTo(myFirst, myArray.length, result, 0); + copyFromTo(0, myLast, result, tailLength); } else { - tailLength = 0; + copyFromTo(myFirst, myLast, result, 0); } - copyFromTo(0, myLast, result, tailLength); return result; } @@ -94,7 +100,7 @@ public class Queue { for (int i = 0; i < myArray.length; i++) { myArray[i] = null; } - + isWrapped = false; myFirst = myLast = 0; } }