Rework block-batching, create draft of chunk batching

This commit is contained in:
Kenzie Togami
2018-10-04 00:12:06 -07:00
parent d1cb6e2156
commit e059490cd1
9 changed files with 306 additions and 269 deletions

View File

@ -0,0 +1,66 @@
/*
* 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 Lesser 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 Lesser General Public License
* for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package com.sk89q.worldedit.util;
import static com.google.common.base.Preconditions.checkNotNull;
import com.sk89q.worldedit.Vector;
import com.sk89q.worldedit.world.block.BlockStateHolder;
import java.util.Objects;
/**
* Represents a block located at some position.
*/
public final class LocatedBlock {
private final Vector location;
private final BlockStateHolder block;
public LocatedBlock(Vector location, BlockStateHolder block) {
this.location = checkNotNull(location);
this.block = checkNotNull(block);
}
public Vector getLocation() {
return location;
}
public BlockStateHolder getBlock() {
return block;
}
@Override
public int hashCode() {
return Objects.hash(location, block);
}
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (this.getClass() != obj.getClass()) {
return false;
}
LocatedBlock lb = (LocatedBlock) obj;
return Objects.equals(location, lb.location) && Objects.equals(block, lb.block);
}
}

View File

@ -1,114 +0,0 @@
/*
* 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 Lesser 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 Lesser General Public License
* for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package com.sk89q.worldedit.util.collection;
import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Preconditions.checkNotNull;
import java.util.Iterator;
import java.util.List;
import java.util.NoSuchElementException;
/**
* 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.</p>
*
* @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<>(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) {
if (!list.isEmpty()) {
return new FastListIterator<>(list, list.size() - 1, list.size(), -1);
} else {
return new FastListIterator<>(list, 0, 0, -1);
}
}
}

View File

@ -0,0 +1,87 @@
/*
* 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 Lesser 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 Lesser General Public License
* for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package com.sk89q.worldedit.util.collection;
import static com.google.common.base.Preconditions.checkNotNull;
import com.sk89q.worldedit.Vector;
import com.sk89q.worldedit.util.LocatedBlock;
import com.sk89q.worldedit.world.block.BlockStateHolder;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;
/**
* Wrapper around a list of blocks located in the world.
*/
public class LocatedBlockList implements Iterable<LocatedBlock> {
private final List<LocatedBlock> list;
public LocatedBlockList() {
list = new ArrayList<>();
}
public LocatedBlockList(Collection<? extends LocatedBlock> collection) {
list = new ArrayList<>(collection);
}
public void add(LocatedBlock setBlockCall) {
checkNotNull(setBlockCall);
list.add(setBlockCall);
}
public void add(Vector location, BlockStateHolder block) {
add(new LocatedBlock(location, block));
}
public int size() {
return list.size();
}
public void clear() {
list.clear();
}
@Override
public Iterator<LocatedBlock> iterator() {
return list.iterator();
}
public Iterator<LocatedBlock> reverseIterator() {
return new Iterator<LocatedBlock>() {
private final ListIterator<LocatedBlock> backingIterator = list.listIterator(list.size());
@Override
public boolean hasNext() {
return backingIterator.hasPrevious();
}
@Override
public LocatedBlock next() {
return backingIterator.previous();
}
};
}
}

View File

@ -1,94 +0,0 @@
/*
* 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 Lesser 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 Lesser General Public License
* for more details.
*
* You should have received a copy of the GNU Lesser 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));
}
/**
* 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();
}
}
}