diff --git a/src/main/java/org/bukkit/craftbukkit/util/LongHashSet.java b/src/main/java/org/bukkit/craftbukkit/util/LongHashSet.java index f339c055..3e1baa8c 100644 --- a/src/main/java/org/bukkit/craftbukkit/util/LongHashSet.java +++ b/src/main/java/org/bukkit/craftbukkit/util/LongHashSet.java @@ -16,13 +16,12 @@ package org.bukkit.craftbukkit.util; -import java.util.Collection; +import java.util.Arrays; import java.util.Iterator; import java.util.ConcurrentModificationException; import java.util.NoSuchElementException; -import java.util.Set; -public class LongHashSet implements Set { +public class LongHashSet { private final static int INITIAL_SIZE = 3; private final static double LOAD_FACTOR = 0.75; @@ -33,7 +32,6 @@ public class LongHashSet implements Set { private int elements; private long[] values; private int modCount; - private org.spigotmc.FlatMap flat = new org.spigotmc.FlatMap(); // Spigot public LongHashSet() { this(INITIAL_SIZE); @@ -46,46 +44,23 @@ public LongHashSet(int size) { modCount = 0; } - @Override - public Iterator iterator() { + public Iterator iterator() { return new Itr(); } - @Override public int size() { return elements; } - @Override public boolean isEmpty() { return elements == 0; } public boolean contains(int msw, int lsw) { - // Spigot start - if ( elements == 0 ) - { - return false; - } - if ( flat.contains( msw, lsw ) ) - { - return true; - } - // Spigot end return contains(LongHash.toLong(msw, lsw)); } public boolean contains(long value) { - // Spigot start - if ( elements == 0 ) - { - return false; - } - if ( flat.contains( value ) ) - { - return true; - } - // Spigot end int hash = hash(value); int index = (hash & 0x7FFFFFFF) % values.length; int offset = 1; @@ -108,7 +83,6 @@ public boolean add(int msw, int lsw) { } public boolean add(long value) { - flat.put( value, Boolean.TRUE ); // Spigot int hash = hash(value); int index = (hash & 0x7FFFFFFF) % values.length; int offset = 1; @@ -152,18 +126,10 @@ public boolean add(long value) { } public void remove(int msw, int lsw) { - // Spigot start - flat.remove(msw, lsw); - remove0(LongHash.toLong(msw, lsw)); + remove(LongHash.toLong(msw, lsw)); } public boolean remove(long value) { - flat.remove(value); - return remove0(value); - } - - private boolean remove0(long value) { - // Spigot end int hash = hash(value); int index = (hash & 0x7FFFFFFF) % values.length; int offset = 1; @@ -188,7 +154,6 @@ private boolean remove0(long value) { } } - @Override public void clear() { elements = 0; for (int ix = 0; ix < values.length; ix++) { @@ -197,12 +162,11 @@ public void clear() { freeEntries = values.length; modCount++; - flat = new org.spigotmc.FlatMap(); } - public long[] toPrimitiveArray() { + public long[] toArray() { long[] result = new long[elements]; - long[] values = Java15Compat.Arrays_copyOf(this.values, this.values.length); + long[] values = Arrays.copyOf(this.values, this.values.length); int pos = 0; for (long value : values) { @@ -214,26 +178,6 @@ public long[] toPrimitiveArray() { return result; } - @Override - public Long[] toArray() { - Long[] result = new Long[elements]; - long[] values = Java15Compat.Arrays_copyOf(this.values, this.values.length); - int pos = 0; - - for (long value : values) { - if (value != FREE && value != REMOVED) { - result[pos++] = value; - } - } - - return result; - } - - @Override - public T[] toArray(T[] arg0) { - throw new UnsupportedOperationException(); - } - public long popFirst() { for (long value : values) { if (value != FREE && value != REMOVED) { @@ -246,7 +190,7 @@ public long popFirst() { } public long[] popAll() { - long[] ret = toPrimitiveArray(); + long[] ret = toArray(); clear(); return ret; } @@ -299,7 +243,7 @@ private void rehash(int newCapacity) { freeEntries = values.length - elements; } - private class Itr implements Iterator { + private class Itr implements Iterator { private int index; private int lastReturned = -1; private int expectedModCount; @@ -311,12 +255,10 @@ public Itr() { expectedModCount = modCount; } - @Override public boolean hasNext() { return index != values.length; } - @Override public Long next() { if (modCount != expectedModCount) { throw new ConcurrentModificationException(); @@ -340,7 +282,6 @@ public Long next() { } } - @Override public void remove() { if (modCount != expectedModCount) { throw new ConcurrentModificationException(); @@ -358,53 +299,4 @@ public void remove() { } } } - - @Override - public boolean add(Long value) { - return add(value.longValue()); - } - - @Override - public boolean addAll(Collection collection) { - boolean result = false; - for (Long value : collection) result |= add(value.longValue()); - return result; - } - - @Override - public boolean contains(Object o) { - return o instanceof Long ? contains(((Long) o).longValue()) : false; - } - - @Override - public boolean containsAll(Collection collection) { - for (Object value : collection) if (!contains(value)) return false; - return true; - } - - @Override - public boolean remove(Object o) { - return o instanceof Long ? remove(((Long) o).longValue()) : false; - } - - @Override - public boolean removeAll(Collection collection) { - boolean result = false; - for (Object value : collection) result |= remove(value); - return result; - } - - @Override - public boolean retainAll(Collection collection) { - boolean result = false; - Iterator iterator = iterator(); - while(iterator.hasNext()) { - Long l = iterator.next(); - if (!collection.contains(l)) { - iterator.remove(); - result = true; - } - } - return result; - } -} +} \ No newline at end of file diff --git a/src/main/java/org/bukkit/craftbukkit/util/LongObjectHashMap.java b/src/main/java/org/bukkit/craftbukkit/util/LongObjectHashMap.java index edf748e3..e1c4429d 100644 --- a/src/main/java/org/bukkit/craftbukkit/util/LongObjectHashMap.java +++ b/src/main/java/org/bukkit/craftbukkit/util/LongObjectHashMap.java @@ -15,8 +15,6 @@ import java.util.NoSuchElementException; import java.util.Set; -import static org.bukkit.craftbukkit.util.Java15Compat.Arrays_copyOf; - @SuppressWarnings("unchecked") public class LongObjectHashMap implements Cloneable, Serializable { static final long serialVersionUID = 2841537710170573815L; @@ -28,7 +26,6 @@ public class LongObjectHashMap implements Cloneable, Serializable { private transient V[][] values; private transient int modCount; private transient int size; - private transient org.spigotmc.FlatMap flat = new org.spigotmc.FlatMap(); // Spigot public LongObjectHashMap() { initialize(); @@ -62,17 +59,6 @@ public boolean containsValue(V value) { } public V get(long key) { - // Spigot start - if ( size == 0 ) - { - return null; - } - V val = flat.get( key ); - if ( val != null ) - { - return val; - } - // Spigot end int index = (int) (keyIndex(key) & (BUCKET_SIZE - 1)); long[] inner = keys[index]; if (inner == null) return null; @@ -90,7 +76,6 @@ public V get(long key) { } public V put(long key, V value) { - flat.put(key, value); // Spigot int index = (int) (keyIndex(key) & (BUCKET_SIZE - 1)); long[] innerKeys = keys[index]; V[] innerValues = values[index]; @@ -125,9 +110,9 @@ public V put(long key, V value) { } // chain is full, resize it and add our new entry - keys[index] = innerKeys = Arrays_copyOf(innerKeys, i << 1); + keys[index] = innerKeys = Arrays.copyOf(innerKeys, i << 1); Arrays.fill(innerKeys, i, innerKeys.length, EMPTY_KEY); - values[index] = innerValues = Arrays_copyOf(innerValues, i << 1); + values[index] = innerValues = Arrays.copyOf(innerValues, i << 1); innerKeys[i] = key; innerValues[i] = value; size++; @@ -137,7 +122,6 @@ public V put(long key, V value) { } public V remove(long key) { - flat.remove(key); // Spigot int index = (int) (keyIndex(key) & (BUCKET_SIZE - 1)); long[] inner = keys[index]; if (inner == null) { @@ -188,7 +172,6 @@ public void clear() { size = 0; Arrays.fill(keys, null); Arrays.fill(values, null); - flat = new org.spigotmc.FlatMap(); } public Set keySet() { @@ -205,9 +188,16 @@ public Collection values() { * this reason it should be avoided if at all possible. * * @return Set of Entry objects + * @deprecated */ + @Deprecated public Set> entrySet() { - return new EntrySet(); + HashSet> set = new HashSet>(); + for (long key : keySet()) { + set.add(new Entry(key, get(key))); + } + + return set; } public Object clone() throws CloneNotSupportedException { @@ -406,9 +396,14 @@ public Iterator iterator() { private class Entry implements Map.Entry { - private Long key; + private final Long key; private V value; + Entry(long k, V v) { + key = k; + value = v; + } + public Long getKey() { return key; } @@ -423,42 +418,5 @@ public V setValue(V v) { put(key, v); return old; } - - private void bind(long key, V value) { - this.key = key; - this.value = value; - } - } - - private class EntrySet extends AbstractSet> { - @Override - public Iterator> iterator() { - return new Iterator>() { - final Entry entry = new Entry(); - final ValueIterator valueIterator = new ValueIterator(); - - @Override - public boolean hasNext() { - return valueIterator.hasNext(); - } - - @Override - public LongObjectHashMap.Entry next() { - V value = valueIterator.next(); - entry.bind(valueIterator.prevKey, value); - return entry; - } - - @Override - public void remove() { - valueIterator.remove(); - } - }; - } - - @Override - public int size() { - return LongObjectHashMap.this.size; - } } -} +} \ No newline at end of file diff --git a/src/main/java/org/spigotmc/FlatMap.java b/src/main/java/org/spigotmc/FlatMap.java deleted file mode 100644 index 9416f6eb..00000000 --- a/src/main/java/org/spigotmc/FlatMap.java +++ /dev/null @@ -1,64 +0,0 @@ -package org.spigotmc; - -import org.bukkit.craftbukkit.util.LongHash; - -public class FlatMap -{ - - private static final int FLAT_LOOKUP_SIZE = 512; - private final Object[][] flatLookup = new Object[ FLAT_LOOKUP_SIZE * 2 ][ FLAT_LOOKUP_SIZE * 2 ]; - - public void put(long msw, long lsw, V value) - { - long acx = Math.abs( msw ); - long acz = Math.abs( lsw ); - if ( acx < FLAT_LOOKUP_SIZE && acz < FLAT_LOOKUP_SIZE ) - { - flatLookup[(int) ( msw + FLAT_LOOKUP_SIZE )][(int) ( lsw + FLAT_LOOKUP_SIZE )] = value; - } - } - - public void put(long key, V value) - { - put( LongHash.msw( key ), LongHash.lsw( key ), value ); - - } - - public void remove(long key) - { - put( key, null ); - } - - public void remove(long msw, long lsw) - { - put( msw, lsw, null ); - } - - public boolean contains(long msw, long lsw) - { - return get( msw, lsw ) != null; - } - - public boolean contains(long key) - { - return get( key ) != null; - } - - public V get(long msw, long lsw) - { - long acx = Math.abs( msw ); - long acz = Math.abs( lsw ); - if ( acx < FLAT_LOOKUP_SIZE && acz < FLAT_LOOKUP_SIZE ) - { - return (V) flatLookup[(int) ( msw + FLAT_LOOKUP_SIZE )][(int) ( lsw + FLAT_LOOKUP_SIZE )]; - } else - { - return null; - } - } - - public V get(long key) - { - return get( LongHash.msw( key ), LongHash.lsw( key ) ); - } -}