From fca9a0bca2713a52180261be61d9003a391e0ebd Mon Sep 17 00:00:00 2001 From: Alexander Udalov Date: Mon, 9 Jun 2014 22:44:27 +0400 Subject: [PATCH] Delete unneeded interface dependencies of HashPMap To reduce class count and to avoid unneeded invokeinterface's --- .../jvm/internal/pcollections/ConsPStack.java | 5 +- .../jvm/internal/pcollections/HashPMap.java | 31 ++-- .../jvm/internal/pcollections/MapEntry.java | 47 ++++++ .../jvm/internal/pcollections/PMap.java | 14 -- .../jvm/internal/pcollections/PStack.java | 14 -- .../pcollections/SimpleImmutableEntry.java | 135 ------------------ 6 files changed, 61 insertions(+), 185 deletions(-) create mode 100644 core/runtime.jvm/src/kotlin/reflect/jvm/internal/pcollections/MapEntry.java delete mode 100644 core/runtime.jvm/src/kotlin/reflect/jvm/internal/pcollections/PMap.java delete mode 100644 core/runtime.jvm/src/kotlin/reflect/jvm/internal/pcollections/PStack.java delete mode 100644 core/runtime.jvm/src/kotlin/reflect/jvm/internal/pcollections/SimpleImmutableEntry.java diff --git a/core/runtime.jvm/src/kotlin/reflect/jvm/internal/pcollections/ConsPStack.java b/core/runtime.jvm/src/kotlin/reflect/jvm/internal/pcollections/ConsPStack.java index 90ea3b6dee2..93cb7996f30 100644 --- a/core/runtime.jvm/src/kotlin/reflect/jvm/internal/pcollections/ConsPStack.java +++ b/core/runtime.jvm/src/kotlin/reflect/jvm/internal/pcollections/ConsPStack.java @@ -11,7 +11,7 @@ import java.util.NoSuchElementException; * * @author harold */ -public final class ConsPStack implements PStack { +public final class ConsPStack implements Iterable { private static final ConsPStack EMPTY = new ConsPStack(); @SuppressWarnings("unchecked") @@ -48,7 +48,6 @@ public final class ConsPStack implements PStack { return listIterator(0); } - @Override public int size() { return size; } @@ -111,7 +110,6 @@ public final class ConsPStack implements PStack { }; } - @Override public ConsPStack plus(E e) { return new ConsPStack(e, this); } @@ -126,7 +124,6 @@ public final class ConsPStack implements PStack { return new ConsPStack(first, newRest); } - @Override public ConsPStack minus(int i) { return minus(get(i)); } diff --git a/core/runtime.jvm/src/kotlin/reflect/jvm/internal/pcollections/HashPMap.java b/core/runtime.jvm/src/kotlin/reflect/jvm/internal/pcollections/HashPMap.java index 36c6abc287e..2989de823e6 100644 --- a/core/runtime.jvm/src/kotlin/reflect/jvm/internal/pcollections/HashPMap.java +++ b/core/runtime.jvm/src/kotlin/reflect/jvm/internal/pcollections/HashPMap.java @@ -1,7 +1,5 @@ package kotlin.reflect.jvm.internal.pcollections; -import static java.util.Map.Entry; - /** * A persistent map from non-null keys to non-null values. *

@@ -13,18 +11,18 @@ import static java.util.Map.Entry; * * @author harold */ -public final class HashPMap implements PMap { - public static final HashPMap EMPTY = new HashPMap(IntTreePMap.>>empty(), 0); +public final class HashPMap { + private static final HashPMap EMPTY = new HashPMap(IntTreePMap.>>empty(), 0); @SuppressWarnings("unchecked") public static HashPMap empty() { return (HashPMap) HashPMap.EMPTY; } - private final IntTreePMap>> intMap; + private final IntTreePMap>> intMap; private final int size; - private HashPMap(IntTreePMap>> intMap, int size) { + private HashPMap(IntTreePMap>> intMap, int size) { this.intMap = intMap; this.size = size; } @@ -37,28 +35,25 @@ public final class HashPMap implements PMap { return keyIndexIn(getEntries(key.hashCode()), key) != -1; } - @Override public V get(Object key) { - PStack> entries = getEntries(key.hashCode()); - for (Entry entry : entries) + ConsPStack> entries = getEntries(key.hashCode()); + for (MapEntry entry : entries) if (entry.getKey().equals(key)) return entry.getValue(); return null; } - @Override public HashPMap plus(K key, V value) { - PStack> entries = getEntries(key.hashCode()); + ConsPStack> entries = getEntries(key.hashCode()); int size0 = entries.size(); int i = keyIndexIn(entries, key); if (i != -1) entries = entries.minus(i); - entries = entries.plus(new SimpleImmutableEntry(key, value)); + entries = entries.plus(new MapEntry(key, value)); return new HashPMap(intMap.plus(key.hashCode(), entries), size - size0 + entries.size()); } - @Override public HashPMap minus(Object key) { - PStack> entries = getEntries(key.hashCode()); + ConsPStack> entries = getEntries(key.hashCode()); int i = keyIndexIn(entries, key); if (i == -1) // key not in this return this; @@ -69,15 +64,15 @@ public final class HashPMap implements PMap { return new HashPMap(intMap.plus(key.hashCode(), entries), size - 1); } - private PStack> getEntries(int hash) { - PStack> entries = intMap.get(hash); + private ConsPStack> getEntries(int hash) { + ConsPStack> entries = intMap.get(hash); if (entries == null) return ConsPStack.empty(); return entries; } - private static int keyIndexIn(PStack> entries, Object key) { + private static int keyIndexIn(ConsPStack> entries, Object key) { int i = 0; - for (Entry entry : entries) { + for (MapEntry entry : entries) { if (entry.getKey().equals(key)) return i; i++; diff --git a/core/runtime.jvm/src/kotlin/reflect/jvm/internal/pcollections/MapEntry.java b/core/runtime.jvm/src/kotlin/reflect/jvm/internal/pcollections/MapEntry.java new file mode 100644 index 00000000000..9251bdb4e6b --- /dev/null +++ b/core/runtime.jvm/src/kotlin/reflect/jvm/internal/pcollections/MapEntry.java @@ -0,0 +1,47 @@ +package kotlin.reflect.jvm.internal.pcollections; + +/** + *

+ * An Entry maintaining an immutable key and value. This class + * does not support method setValue. This class may be + * convenient in methods that return thread-safe snapshots of + * key-value mappings. + */ +final class MapEntry implements java.io.Serializable { + private static final long serialVersionUID = 7138329143949025153L; + + private final K key; + private final V value; + + public MapEntry(K key, V value) { + this.key = key; + this.value = value; + } + + public K getKey() { + return key; + } + + public V getValue() { + return value; + } + + @Override + public boolean equals(Object o) { + if (!(o instanceof MapEntry)) return false; + MapEntry e = (MapEntry) o; + return (key == null ? e.key == null : key.equals(e.key)) && + (value == null ? e.value == null : value.equals(e.value)); + } + + @Override + public int hashCode() { + return (key == null ? 0 : key.hashCode()) ^ + (value == null ? 0 : value.hashCode()); + } + + @Override + public String toString() { + return key + "=" + value; + } +} diff --git a/core/runtime.jvm/src/kotlin/reflect/jvm/internal/pcollections/PMap.java b/core/runtime.jvm/src/kotlin/reflect/jvm/internal/pcollections/PMap.java deleted file mode 100644 index e8e1c2c4217..00000000000 --- a/core/runtime.jvm/src/kotlin/reflect/jvm/internal/pcollections/PMap.java +++ /dev/null @@ -1,14 +0,0 @@ -package kotlin.reflect.jvm.internal.pcollections; - -/** - * An immutable, persistent map from non-null keys of type K to non-null values of type V. - * - * @author harold - */ -public interface PMap { - public PMap plus(K key, V value); - - public PMap minus(Object key); - - V get(Object key); -} diff --git a/core/runtime.jvm/src/kotlin/reflect/jvm/internal/pcollections/PStack.java b/core/runtime.jvm/src/kotlin/reflect/jvm/internal/pcollections/PStack.java deleted file mode 100644 index 5b52152671a..00000000000 --- a/core/runtime.jvm/src/kotlin/reflect/jvm/internal/pcollections/PStack.java +++ /dev/null @@ -1,14 +0,0 @@ -package kotlin.reflect.jvm.internal.pcollections; - -/** - * An immutable, persistent stack. - * - * @author harold - */ -public interface PStack extends Iterable { - PStack plus(E e); - - PStack minus(int i); - - int size(); -} diff --git a/core/runtime.jvm/src/kotlin/reflect/jvm/internal/pcollections/SimpleImmutableEntry.java b/core/runtime.jvm/src/kotlin/reflect/jvm/internal/pcollections/SimpleImmutableEntry.java deleted file mode 100644 index ae79bc2bb6c..00000000000 --- a/core/runtime.jvm/src/kotlin/reflect/jvm/internal/pcollections/SimpleImmutableEntry.java +++ /dev/null @@ -1,135 +0,0 @@ -package kotlin.reflect.jvm.internal.pcollections; - -import java.util.Map; - -/** - *

- * An Entry maintaining an immutable key and value. This class - * does not support method setValue. This class may be - * convenient in methods that return thread-safe snapshots of - * key-value mappings. - * - * @since 1.6 - */ -final class SimpleImmutableEntry implements Map.Entry, java.io.Serializable { - private static final long serialVersionUID = 7138329143949025153L; - - private final K key; - private final V value; - - /** - * Creates an entry representing a mapping from the specified - * key to the specified value. - * - * @param key the key represented by this entry - * @param value the value represented by this entry - */ - public SimpleImmutableEntry(K key, V value) { - this.key = key; - this.value = value; - } - - /** - * Returns the key corresponding to this entry. - * - * @return the key corresponding to this entry - */ - @Override - public K getKey() { - return key; - } - - /** - * Returns the value corresponding to this entry. - * - * @return the value corresponding to this entry - */ - @Override - public V getValue() { - return value; - } - - /** - * Replaces the value corresponding to this entry with the specified - * value (optional operation). This implementation simply throws - * UnsupportedOperationException, as this class implements - * an immutable map entry. - * - * @param value new value to be stored in this entry - * @return (Does not return) - * @throws UnsupportedOperationException always - */ - @Override - public V setValue(V value) { - throw new UnsupportedOperationException(); - } - - /** - * Compares the specified object with this entry for equality. - * Returns {@code true} if the given object is also a map entry and - * the two entries represent the same mapping. More formally, two - * entries {@code e1} and {@code e2} represent the same mapping - * if

-     *   (e1.getKey()==null ?
-     *    e2.getKey()==null :
-     *    e1.getKey().equals(e2.getKey()))
-     *   &&
-     *   (e1.getValue()==null ?
-     *    e2.getValue()==null :
-     *    e1.getValue().equals(e2.getValue()))
- * This ensures that the {@code equals} method works properly across - * different implementations of the {@code Map.Entry} interface. - * - * @param o object to be compared for equality with this map entry - * @return {@code true} if the specified object is equal to this map - * entry - * @see #hashCode - */ - @Override - public boolean equals(Object o) { - if (!(o instanceof Map.Entry)) - return false; - Map.Entry e = (Map.Entry) o; - return eq(key, e.getKey()) && eq(value, e.getValue()); - } - - /** - * Returns the hash code value for this map entry. The hash code - * of a map entry {@code e} is defined to be:
-     *   (e.getKey()==null   ? 0 : e.getKey().hashCode()) ^
-     *   (e.getValue()==null ? 0 : e.getValue().hashCode())
- * This ensures that {@code e1.equals(e2)} implies that - * {@code e1.hashCode()==e2.hashCode()} for any two Entries - * {@code e1} and {@code e2}, as required by the general - * contract of {@link Object#hashCode}. - * - * @return the hash code value for this map entry - * @see #equals - */ - @Override - public int hashCode() { - return (key == null ? 0 : key.hashCode()) ^ - (value == null ? 0 : value.hashCode()); - } - - /** - * Returns a String representation of this map entry. This - * implementation returns the string representation of this - * entry's key followed by the equals character ("=") - * followed by the string representation of this entry's value. - * - * @return a String representation of this map entry - */ - @Override - public String toString() { - return key + "=" + value; - } - - /** - * Utility method for SimpleEntry and SimpleImmutableEntry. - * Test for equality, checking for nulls. - */ - private static boolean eq(Object o1, Object o2) { - return o1 == null ? o2 == null : o1.equals(o2); - } -}