Switched history's hash maps to double array lists; operations 18x faster.

This commit is contained in:
sk89q 2010-11-16 22:05:45 -08:00
parent 6945ddd258
commit 2068f40cd8
2 changed files with 139 additions and 10 deletions

View File

@ -46,15 +46,15 @@ public class EditSession {
/**
* Stores the original blocks before modification.
*/
private Map<BlockVector,BaseBlock> original = new HashMap<BlockVector,BaseBlock>();
private DoubleArrayList<BlockVector,BaseBlock> original = new DoubleArrayList<BlockVector,BaseBlock>();
/**
* Stores the current blocks.
*/
private Map<BlockVector,BaseBlock> current = new HashMap<BlockVector,BaseBlock>();
private DoubleArrayList<BlockVector,BaseBlock> current = new DoubleArrayList<BlockVector,BaseBlock>();
/**
* Queue.
*/
private Map<BlockVector,BaseBlock> queue = new HashMap<BlockVector,BaseBlock>();
private DoubleArrayList<BlockVector,BaseBlock> queue = new DoubleArrayList<BlockVector,BaseBlock>();
/**
* The maximum number of blocks to change at a time. If this number is
* exceeded, a MaxChangedBlocksException exception will be
@ -149,13 +149,13 @@ public class EditSession {
throws MaxChangedBlocksException {
BlockVector blockPt = pt.toBlockVector();
if (!original.containsKey(blockPt)) {
//if (!original.containsKey(blockPt)) {
original.put(blockPt, getBlock(pt));
if (maxBlocks != -1 && original.size() > maxBlocks) {
throw new MaxChangedBlocksException(maxBlocks);
}
}
//}
current.put(pt.toBlockVector(), block);
@ -213,9 +213,9 @@ public class EditSession {
if (queued) {
BlockVector blockPt = pt.toBlockVector();
if (current.containsKey(blockPt)) {
/*if (current.containsKey(blockPt)) {
return current.get(blockPt);
}
}*/
}
return rawGetBlock(pt);
@ -248,7 +248,7 @@ public class EditSession {
* Restores all blocks to their initial state.
*/
public void undo() {
for (Map.Entry<BlockVector,BaseBlock> entry : original.entrySet()) {
for (Map.Entry<BlockVector,BaseBlock> entry : original) {
BlockVector pt = (BlockVector)entry.getKey();
smartSetBlock(pt, (BaseBlock)entry.getValue());
}
@ -259,7 +259,7 @@ public class EditSession {
* Sets to new state.
*/
public void redo() {
for (Map.Entry<BlockVector,BaseBlock> entry : current.entrySet()) {
for (Map.Entry<BlockVector,BaseBlock> entry : current) {
BlockVector pt = (BlockVector)entry.getKey();
smartSetBlock(pt, (BaseBlock)entry.getValue());
}
@ -328,7 +328,7 @@ public class EditSession {
public void flushQueue() {
if (!queued) { return; }
for (Map.Entry<BlockVector,BaseBlock> entry : queue.entrySet()) {
for (Map.Entry<BlockVector,BaseBlock> entry : queue) {
BlockVector pt = (BlockVector)entry.getKey();
rawSetBlock(pt, (BaseBlock)entry.getValue());
}

View File

@ -0,0 +1,129 @@
// $Id$
/*
* WorldEdit
* Copyright (C) 2010 sk89q <http://www.sk89q.com>
*
* 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;
import java.util.List;
import java.util.ArrayList;
import java.util.Map;
import java.util.Iterator;
import java.util.ListIterator;
import java.util.NoSuchElementException;
/**
* Double array lists to work like a Map, but not really.
*
* @author sk89q
*/
public class DoubleArrayList<A,B> implements Iterable<Map.Entry<A,B>> {
/**
* First list.
*/
private List<A> listA = new ArrayList<A>();
/**
* Second list.
*/
private List<B> listB = new ArrayList<B>();
/**
* Add an item.
*
* @param a
* @param b
*/
public void put(A a, B b) {
listA.add(a);
listB.add(b);
}
/**
* Get size.
* @return
*/
public int size() {
return listA.size();
}
/**
* Get an entry set.
*
* @return
*/
public Iterator<Map.Entry<A,B>> iterator() {
return new EntryIterator<Map.Entry<A,B>>(
listA.listIterator(listA.size()),
listB.listIterator(listB.size()));
}
/**
* Entry iterator.
*
* @param <A>
* @param <B>
*/
public class EntryIterator<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) {
this.keyIterator = keyIterator;
this.valueIterator = valueIterator;
}
public boolean hasNext() {
return keyIterator.hasPrevious();
}
public Map.Entry<A,B> next() throws NoSuchElementException {
return new Entry<A,B>(keyIterator.previous(), valueIterator.previous());
}
public void remove() {
throw new UnsupportedOperationException();
}
}
/**
* Class to masquerade as Map.Entry.
*
* @param <A>
* @param <B>
*/
public class Entry<A,B> implements Map.Entry<A,B> {
private A key;
private B value;
private Entry(A key, B value) {
this.key = key;
this.value = value;
}
public A getKey() {
return key;
}
public B getValue() {
return value;
}
public B setValue(B value) {
throw new UnsupportedOperationException();
}
}
}