Delete dependency of 'serialization' on trove4j
Use HashMap of wrapped objects instead of THashMap with custom hashing strategy
This commit is contained in:
@@ -8,7 +8,6 @@
|
||||
<orderEntry type="inheritedJdk" />
|
||||
<orderEntry type="sourceFolder" forTests="false" />
|
||||
<orderEntry type="library" exported="" name="protobuf-java" level="project" />
|
||||
<orderEntry type="library" name="trove4j" level="project" />
|
||||
<orderEntry type="module" module-name="descriptors" exported="" />
|
||||
<orderEntry type="module" module-name="util.runtime" />
|
||||
</component>
|
||||
|
||||
@@ -16,65 +16,52 @@
|
||||
|
||||
package org.jetbrains.kotlin.serialization;
|
||||
|
||||
import gnu.trove.TObjectHashingStrategy;
|
||||
import gnu.trove.TObjectIntHashMap;
|
||||
import kotlin.Function1;
|
||||
import kotlin.KotlinPackage;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
public final class Interner<T> {
|
||||
private final Interner<T> parent;
|
||||
private final int firstIndex;
|
||||
private final TObjectIntHashMap<T> interned;
|
||||
private final Map<T, Integer> interned = new HashMap<T, Integer>();
|
||||
|
||||
public Interner(Interner<T> parent, @NotNull TObjectHashingStrategy<T> hashing) {
|
||||
public Interner(Interner<T> parent) {
|
||||
this.parent = parent;
|
||||
this.firstIndex = parent != null ? parent.interned.size() + parent.firstIndex : 0;
|
||||
this.interned = new TObjectIntHashMap<T>(hashing);
|
||||
}
|
||||
|
||||
public Interner(@NotNull TObjectHashingStrategy<T> hashing) {
|
||||
this(null, hashing);
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public Interner(@Nullable Interner<T> parent) {
|
||||
this(parent, TObjectHashingStrategy.CANONICAL);
|
||||
}
|
||||
|
||||
public Interner() {
|
||||
this((Interner<T>) null);
|
||||
this(null);
|
||||
}
|
||||
|
||||
private int find(@NotNull T obj) {
|
||||
@Nullable
|
||||
private Integer find(@NotNull T obj) {
|
||||
assert parent == null || parent.interned.size() + parent.firstIndex == firstIndex :
|
||||
"Parent changed in parallel with child: indexes will be wrong";
|
||||
if (parent != null) {
|
||||
int index = parent.find(obj);
|
||||
if (index >= 0) return index;
|
||||
Integer index = parent.find(obj);
|
||||
if (index != null) return index;
|
||||
}
|
||||
if (interned.contains(obj)) {
|
||||
return interned.get(obj);
|
||||
}
|
||||
return -1;
|
||||
return interned.get(obj);
|
||||
}
|
||||
|
||||
public int intern(@NotNull T obj) {
|
||||
int index = find(obj);
|
||||
if (index >= 0) return index;
|
||||
Integer index = find(obj);
|
||||
if (index != null) return index;
|
||||
|
||||
index = firstIndex + interned.size();
|
||||
interned.put(obj, index);
|
||||
return index;
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
@NotNull
|
||||
public List<T> getAllInternedObjects() {
|
||||
return KotlinPackage.toSortedListBy((T[]) interned.keys(), new Function1<T, Integer>() {
|
||||
return KotlinPackage.toSortedListBy(interned.keySet(), new Function1<T, Integer>() {
|
||||
@Override
|
||||
public Integer invoke(T key) {
|
||||
return interned.get(key);
|
||||
|
||||
@@ -16,7 +16,8 @@
|
||||
|
||||
package org.jetbrains.kotlin.serialization;
|
||||
|
||||
import gnu.trove.TObjectHashingStrategy;
|
||||
import kotlin.Function1;
|
||||
import kotlin.KotlinPackage;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.kotlin.descriptors.ClassDescriptor;
|
||||
import org.jetbrains.kotlin.descriptors.ClassOrPackageFragmentDescriptor;
|
||||
@@ -30,27 +31,35 @@ import java.util.List;
|
||||
import static org.jetbrains.kotlin.serialization.ProtoBuf.QualifiedNameTable.QualifiedName;
|
||||
|
||||
public class StringTable {
|
||||
public static final TObjectHashingStrategy<QualifiedName.Builder> QUALIFIED_NAME_BUILDER_HASHING =
|
||||
new TObjectHashingStrategy<ProtoBuf.QualifiedNameTable.QualifiedName.Builder>() {
|
||||
@Override
|
||||
public int computeHashCode(QualifiedName.Builder object) {
|
||||
int result = 13;
|
||||
result = 31 * result + object.getParentQualifiedName();
|
||||
result = 31 * result + object.getShortName();
|
||||
result = 31 * result + object.getKind().hashCode();
|
||||
return result;
|
||||
}
|
||||
private static final class FqNameProto {
|
||||
public final QualifiedName.Builder fqName;
|
||||
|
||||
@Override
|
||||
public boolean equals(QualifiedName.Builder o1, QualifiedName.Builder o2) {
|
||||
return o1.getParentQualifiedName() == o2.getParentQualifiedName()
|
||||
&& o1.getShortName() == o2.getShortName()
|
||||
&& o1.getKind() == o2.getKind();
|
||||
}
|
||||
};
|
||||
public FqNameProto(@NotNull QualifiedName.Builder fqName) {
|
||||
this.fqName = fqName;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
int result = 13;
|
||||
result = 31 * result + fqName.getParentQualifiedName();
|
||||
result = 31 * result + fqName.getShortName();
|
||||
result = 31 * result + fqName.getKind().hashCode();
|
||||
return result;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object obj) {
|
||||
if (obj == null || getClass() != obj.getClass()) return false;
|
||||
|
||||
QualifiedName.Builder other = ((FqNameProto) obj).fqName;
|
||||
return fqName.getParentQualifiedName() == other.getParentQualifiedName()
|
||||
&& fqName.getShortName() == other.getShortName()
|
||||
&& fqName.getKind() == other.getKind();
|
||||
}
|
||||
}
|
||||
|
||||
private final Interner<String> strings = new Interner<String>();
|
||||
private final Interner<QualifiedName.Builder> qualifiedNames = new Interner<QualifiedName.Builder>(QUALIFIED_NAME_BUILDER_HASHING);
|
||||
private final Interner<FqNameProto> qualifiedNames = new Interner<FqNameProto>();
|
||||
|
||||
@NotNull
|
||||
public List<String> getStrings() {
|
||||
@@ -59,7 +68,12 @@ public class StringTable {
|
||||
|
||||
@NotNull
|
||||
public List<QualifiedName.Builder> getFqNames() {
|
||||
return qualifiedNames.getAllInternedObjects();
|
||||
return KotlinPackage.map(qualifiedNames.getAllInternedObjects(), new Function1<FqNameProto, QualifiedName.Builder>() {
|
||||
@Override
|
||||
public QualifiedName.Builder invoke(FqNameProto proto) {
|
||||
return proto.fqName;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public int getSimpleNameIndex(@NotNull Name name) {
|
||||
@@ -92,7 +106,7 @@ public class StringTable {
|
||||
throw new IllegalStateException("FQ names are only stored for top-level or inner classes: " + descriptor);
|
||||
}
|
||||
|
||||
return qualifiedNames.intern(builder);
|
||||
return qualifiedNames.intern(new FqNameProto(builder));
|
||||
}
|
||||
|
||||
public int getFqNameIndex(@NotNull FqName fqName) {
|
||||
@@ -103,7 +117,7 @@ public class StringTable {
|
||||
if (result != -1) {
|
||||
builder.setParentQualifiedName(result);
|
||||
}
|
||||
result = qualifiedNames.intern(builder);
|
||||
result = qualifiedNames.intern(new FqNameProto(builder));
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user