Changed SimpleBlockReorder to TupleArrayList.

This commit is contained in:
sk89q 2014-03-31 19:48:10 -07:00
parent 0c14737891
commit 031d40f9e6
3 changed files with 212 additions and 5 deletions

View File

@ -20,16 +20,19 @@
package com.sk89q.worldedit.extent.reorder;
import com.google.common.collect.Iterators;
import com.sk89q.worldedit.*;
import com.sk89q.worldedit.BlockVector;
import com.sk89q.worldedit.PlayerDirection;
import com.sk89q.worldedit.Vector;
import com.sk89q.worldedit.WorldEditException;
import com.sk89q.worldedit.blocks.BaseBlock;
import com.sk89q.worldedit.blocks.BlockID;
import com.sk89q.worldedit.blocks.BlockType;
import com.sk89q.worldedit.extent.ExtentDelegate;
import com.sk89q.worldedit.extent.Extent;
import com.sk89q.worldedit.extent.ExtentDelegate;
import com.sk89q.worldedit.function.operation.BlockMapEntryPlacer;
import com.sk89q.worldedit.function.operation.Operation;
import com.sk89q.worldedit.function.operation.OperationQueue;
import com.sk89q.worldedit.util.collection.TupleArrayList;
import java.util.*;
@ -38,9 +41,9 @@
*/
public class SimpleBlockReorder extends ExtentDelegate {
private DoubleArrayList<BlockVector, BaseBlock> stage1 = new DoubleArrayList<BlockVector, BaseBlock>(false);
private DoubleArrayList<BlockVector, BaseBlock> stage2 = new DoubleArrayList<BlockVector, BaseBlock>(false);
private DoubleArrayList<BlockVector, BaseBlock> stage3 = new DoubleArrayList<BlockVector, BaseBlock>(false);
private TupleArrayList<BlockVector, BaseBlock> stage1 = new TupleArrayList<BlockVector, BaseBlock>();
private TupleArrayList<BlockVector, BaseBlock> stage2 = new TupleArrayList<BlockVector, BaseBlock>();
private TupleArrayList<BlockVector, BaseBlock> stage3 = new TupleArrayList<BlockVector, BaseBlock>();
private boolean enabled;
/**

View File

@ -0,0 +1,110 @@
/*
* WorldEdit, a Minecraft world manipulation toolkit
* Copyright (C) sk89q <http://www.sk89q.com>
* Copyright (C) WorldEdit team and contributors
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package com.sk89q.worldedit.util.collection;
import java.util.Iterator;
import java.util.List;
import java.util.NoSuchElementException;
import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Preconditions.checkNotNull;
/**
* A fast iterator for lists that uses an internal index integer
* and caches the size of the list. The size of the list cannot change
* during iteration and {@link Iterator#remove()} is not supported.
* </p>
* The iterator in Java, at least in older Java versions, is very slow,
* causing a significant amount of time in operations in WorldEdit
* being spent on {@link Iterator#hasNext()}. In contrast, the iterator
* implemented by this class is very quick, as long as
* {@link List#get(int)} is fast.
*
* @param <E> the element
*/
public class FastListIterator<E> implements Iterator<E> {
private final List<E> list;
private int index;
private final int size;
private final int increment;
/**
* Create a new fast iterator.
*
* @param list the list
* @param index the index to start from
* @param size the size of the list
* @param increment the increment amount (i.e. 1 or -1)
*/
private FastListIterator(List<E> list, int index, int size, int increment) {
checkNotNull(list);
checkArgument(size >= 0, "size >= 0 required");
checkArgument(index >= 0, "index >= 0 required");
this.list = list;
this.index = index;
this.size = size;
this.increment = increment;
}
@Override
public boolean hasNext() {
return index >= 0 && index < size;
}
@Override
public E next() {
if (hasNext()) {
E entry = list.get(index);
index += increment;
return entry;
} else {
throw new NoSuchElementException();
}
}
@Override
public void remove() {
throw new UnsupportedOperationException("Not supported");
}
/**
* Create a new forward iterator for the given list.
*
* @param list the list
* @param <E> the element
* @return an iterator
*/
public static <E> Iterator<E> forwardIterator(List<E> list) {
return new FastListIterator<E>(list, 0, list.size(), 1);
}
/**
* Create a new reverse iterator for the given list.
*
* @param list the list
* @param <E> the element
* @return an iterator
*/
public static <E> Iterator<E> reverseIterator(List<E> list) {
return new FastListIterator<E>(list, list.size() - 1, list.size(), -1);
}
}

View File

@ -0,0 +1,94 @@
/*
* WorldEdit, a Minecraft world manipulation toolkit
* Copyright (C) sk89q <http://www.sk89q.com>
* Copyright (C) WorldEdit team and contributors
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package com.sk89q.worldedit.util.collection;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Map;
/**
* An {@link ArrayList} that takes {@link Map.Entry}-like tuples. This class
* exists for legacy reasons.
*
* @param <A> the first type in the tuple
* @param <B> the second type in the tuple
*/
public class TupleArrayList<A, B> extends ArrayList<Map.Entry<A, B>> {
/**
* Add an item to the list.
*
* @param a the 'key'
* @param b the 'value'
*/
public void put(A a, B b) {
add(new Tuple<A, B>(a, b));
}
/**
* Return an entry iterator that traverses in the reverse direction.
*
* @param reverse true to return the reverse iterator
* @return an entry iterator
*/
public Iterator<Map.Entry<A, B>> iterator(boolean reverse) {
return reverse ? reverseIterator() : iterator();
}
@Override
public Iterator<Map.Entry<A, B>> iterator() {
return FastListIterator.forwardIterator(this);
}
/**
* Return an entry iterator that traverses in the reverse direction.
*
* @return an entry iterator
*/
public Iterator<Map.Entry<A, B>> reverseIterator() {
return FastListIterator.reverseIterator(this);
}
private static class Tuple<A, B> implements Map.Entry<A, B> {
private A key;
private B value;
private Tuple(A key, B value) {
this.key = key;
this.value = value;
}
@Override
public A getKey() {
return key;
}
@Override
public B getValue() {
return value;
}
@Override
public B setValue(B value) {
throw new UnsupportedOperationException();
}
}
}