From 031d40f9e699879db7fb39eacdd9cb6f713864d5 Mon Sep 17 00:00:00 2001
From: sk89q
Date: Mon, 31 Mar 2014 19:48:10 -0700
Subject: [PATCH] Changed SimpleBlockReorder to TupleArrayList.
---
.../extent/reorder/SimpleBlockReorder.java | 13 ++-
.../util/collection/FastListIterator.java | 110 ++++++++++++++++++
.../util/collection/TupleArrayList.java | 94 +++++++++++++++
3 files changed, 212 insertions(+), 5 deletions(-)
create mode 100644 src/main/java/com/sk89q/worldedit/util/collection/FastListIterator.java
create mode 100644 src/main/java/com/sk89q/worldedit/util/collection/TupleArrayList.java
diff --git a/src/main/java/com/sk89q/worldedit/extent/reorder/SimpleBlockReorder.java b/src/main/java/com/sk89q/worldedit/extent/reorder/SimpleBlockReorder.java
index e077d4c0d..044abbb54 100644
--- a/src/main/java/com/sk89q/worldedit/extent/reorder/SimpleBlockReorder.java
+++ b/src/main/java/com/sk89q/worldedit/extent/reorder/SimpleBlockReorder.java
@@ -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 @@ import java.util.*;
*/
public class SimpleBlockReorder extends ExtentDelegate {
- private DoubleArrayList stage1 = new DoubleArrayList(false);
- private DoubleArrayList stage2 = new DoubleArrayList(false);
- private DoubleArrayList stage3 = new DoubleArrayList(false);
+ private TupleArrayList stage1 = new TupleArrayList();
+ private TupleArrayList stage2 = new TupleArrayList();
+ private TupleArrayList stage3 = new TupleArrayList();
private boolean enabled;
/**
diff --git a/src/main/java/com/sk89q/worldedit/util/collection/FastListIterator.java b/src/main/java/com/sk89q/worldedit/util/collection/FastListIterator.java
new file mode 100644
index 000000000..7aec4ca6b
--- /dev/null
+++ b/src/main/java/com/sk89q/worldedit/util/collection/FastListIterator.java
@@ -0,0 +1,110 @@
+/*
+ * WorldEdit, a Minecraft world manipulation toolkit
+ * Copyright (C) sk89q
+ * 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 .
+ */
+
+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.
+ *
+ * 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 the element
+ */
+public class FastListIterator implements Iterator {
+
+ private final List 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 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 the element
+ * @return an iterator
+ */
+ public static Iterator forwardIterator(List list) {
+ return new FastListIterator(list, 0, list.size(), 1);
+ }
+
+ /**
+ * Create a new reverse iterator for the given list.
+ *
+ * @param list the list
+ * @param the element
+ * @return an iterator
+ */
+ public static Iterator reverseIterator(List list) {
+ return new FastListIterator(list, list.size() - 1, list.size(), -1);
+ }
+
+}
diff --git a/src/main/java/com/sk89q/worldedit/util/collection/TupleArrayList.java b/src/main/java/com/sk89q/worldedit/util/collection/TupleArrayList.java
new file mode 100644
index 000000000..69fd9e05d
--- /dev/null
+++ b/src/main/java/com/sk89q/worldedit/util/collection/TupleArrayList.java
@@ -0,0 +1,94 @@
+/*
+ * WorldEdit, a Minecraft world manipulation toolkit
+ * Copyright (C) sk89q
+ * 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 .
+ */
+
+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 the first type in the tuple
+ * @param the second type in the tuple
+ */
+public class TupleArrayList extends ArrayList> {
+
+ /**
+ * 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));
+ }
+
+ /**
+ * Return an entry iterator that traverses in the reverse direction.
+ *
+ * @param reverse true to return the reverse iterator
+ * @return an entry iterator
+ */
+ public Iterator> iterator(boolean reverse) {
+ return reverse ? reverseIterator() : iterator();
+ }
+
+ @Override
+ public Iterator> iterator() {
+ return FastListIterator.forwardIterator(this);
+ }
+
+ /**
+ * Return an entry iterator that traverses in the reverse direction.
+ *
+ * @return an entry iterator
+ */
+ public Iterator> reverseIterator() {
+ return FastListIterator.reverseIterator(this);
+ }
+
+ private static class Tuple implements Map.Entry {
+ 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();
+ }
+ }
+
+}