Skip to content

Commit

Permalink
Fixed the history/queue array lists being iterated in the wrong direc…
Browse files Browse the repository at this point in the history
…tion.
  • Loading branch information
sk89q committed Dec 2, 2010
1 parent c710e38 commit 2e77753
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 9 deletions.
8 changes: 4 additions & 4 deletions src/EditSession.java
Expand Up @@ -49,22 +49,22 @@ public class EditSession {
* Stores the original blocks before modification.
*/
private DoubleArrayList<BlockVector,BaseBlock> original =
new DoubleArrayList<BlockVector,BaseBlock>();
new DoubleArrayList<BlockVector,BaseBlock>(true);
/**
* Stores the current blocks.
*/
private DoubleArrayList<BlockVector,BaseBlock> current =
new DoubleArrayList<BlockVector,BaseBlock>();
new DoubleArrayList<BlockVector,BaseBlock>(false);
/**
* Blocks that should be placed before last.
*/
private DoubleArrayList<BlockVector,BaseBlock> queueAfter =
new DoubleArrayList<BlockVector,BaseBlock>();
new DoubleArrayList<BlockVector,BaseBlock>(false);
/**
* Blocks that should be placed last.
*/
private DoubleArrayList<BlockVector,BaseBlock> queueLast =
new DoubleArrayList<BlockVector,BaseBlock>();
new DoubleArrayList<BlockVector,BaseBlock>(false);
/**
* The maximum number of blocks to change at a time. If this number is
* exceeded, a MaxChangedBlocksException exception will be
Expand Down
57 changes: 52 additions & 5 deletions src/com/sk89q/worldedit/DoubleArrayList.java
Expand Up @@ -40,6 +40,19 @@ public class DoubleArrayList<A,B> implements Iterable<Map.Entry<A,B>> {
* Second list.
*/
private List<B> listB = new ArrayList<B>();
/**
* Is reversed when iterating.
*/
private boolean isReversed = false;

/**
* Construct the object.
*
* @param isReversed
*/
public DoubleArrayList(boolean isReversed) {
this.isReversed = isReversed;
}

/**
* Add an item.
Expand Down Expand Up @@ -74,9 +87,43 @@ public void clear() {
* @return
*/
public Iterator<Map.Entry<A,B>> iterator() {
return new EntryIterator<Map.Entry<A,B>>(
listA.listIterator(listA.size()),
listB.listIterator(listB.size()));
if (isReversed) {
return new ReverseEntryIterator<Map.Entry<A,B>>(
listA.listIterator(listA.size()),
listB.listIterator(listB.size()));
} else {
return new ForwardEntryIterator<Map.Entry<A,B>>(
listA.iterator(),
listB.iterator());
}
}

/**
* Entry iterator.
*
* @param <A>
* @param <B>
*/
public class ForwardEntryIterator<T extends Map.Entry> implements Iterator<Map.Entry<A,B>> {
private Iterator<A> keyIterator;
private Iterator<B> valueIterator;

public ForwardEntryIterator(Iterator<A> keyIterator, Iterator<B> valueIterator) {
this.keyIterator = keyIterator;
this.valueIterator = valueIterator;
}

public boolean hasNext() {
return keyIterator.hasNext();
}

public Map.Entry<A,B> next() throws NoSuchElementException {
return new Entry<A,B>(keyIterator.next(), valueIterator.next());
}

public void remove() {
throw new UnsupportedOperationException();
}
}

/**
Expand All @@ -85,11 +132,11 @@ public Iterator<Map.Entry<A,B>> iterator() {
* @param <A>
* @param <B>
*/
public class EntryIterator<T extends Map.Entry> implements Iterator<Map.Entry<A,B>> {
public class ReverseEntryIterator<T extends Map.Entry> implements Iterator<Map.Entry<A,B>> {
private ListIterator<A> keyIterator;
private ListIterator<B> valueIterator;

public EntryIterator(ListIterator<A> keyIterator, ListIterator<B> valueIterator) {
public ReverseEntryIterator(ListIterator<A> keyIterator, ListIterator<B> valueIterator) {
this.keyIterator = keyIterator;
this.valueIterator = valueIterator;
}
Expand Down

0 comments on commit 2e77753

Please sign in to comment.