From 2068f40cd898ab49aaf9ca4cf05cce7aee5de3e8 Mon Sep 17 00:00:00 2001 From: sk89q Date: Tue, 16 Nov 2010 22:05:45 -0800 Subject: [PATCH] Switched history's hash maps to double array lists; operations 18x faster. --- src/EditSession.java | 20 +-- src/com/sk89q/worldedit/DoubleArrayList.java | 129 +++++++++++++++++++ 2 files changed, 139 insertions(+), 10 deletions(-) create mode 100644 src/com/sk89q/worldedit/DoubleArrayList.java diff --git a/src/EditSession.java b/src/EditSession.java index 39e28db9a..7e112417d 100644 --- a/src/EditSession.java +++ b/src/EditSession.java @@ -46,15 +46,15 @@ public class EditSession { /** * Stores the original blocks before modification. */ - private Map original = new HashMap(); + private DoubleArrayList original = new DoubleArrayList(); /** * Stores the current blocks. */ - private Map current = new HashMap(); + private DoubleArrayList current = new DoubleArrayList(); /** * Queue. */ - private Map queue = new HashMap(); + private DoubleArrayList queue = new DoubleArrayList(); /** * 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 entry : original.entrySet()) { + for (Map.Entry 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 entry : current.entrySet()) { + for (Map.Entry 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 entry : queue.entrySet()) { + for (Map.Entry entry : queue) { BlockVector pt = (BlockVector)entry.getKey(); rawSetBlock(pt, (BaseBlock)entry.getValue()); } diff --git a/src/com/sk89q/worldedit/DoubleArrayList.java b/src/com/sk89q/worldedit/DoubleArrayList.java new file mode 100644 index 000000000..972f6baa2 --- /dev/null +++ b/src/com/sk89q/worldedit/DoubleArrayList.java @@ -0,0 +1,129 @@ +// $Id$ +/* + * WorldEdit + * Copyright (C) 2010 sk89q + * + * 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; + +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 implements Iterable> { + /** + * First list. + */ + private List listA = new ArrayList(); + /** + * Second list. + */ + private List listB = new ArrayList(); + + /** + * 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> iterator() { + return new EntryIterator>( + listA.listIterator(listA.size()), + listB.listIterator(listB.size())); + } + + /** + * Entry iterator. + * + * @param + * @param + */ + public class EntryIterator implements Iterator> { + private ListIterator keyIterator; + private ListIterator valueIterator; + + public EntryIterator(ListIterator keyIterator, ListIterator valueIterator) { + this.keyIterator = keyIterator; + this.valueIterator = valueIterator; + } + + public boolean hasNext() { + return keyIterator.hasPrevious(); + } + + public Map.Entry next() throws NoSuchElementException { + return new Entry(keyIterator.previous(), valueIterator.previous()); + } + + public void remove() { + throw new UnsupportedOperationException(); + } + } + + /** + * Class to masquerade as Map.Entry. + * + * @param + * @param + */ + public class Entry implements Map.Entry { + 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(); + } + } +}