mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
introduce OpenTHashSet.getOrAdd, which may be used to avoid hashing twice when interning
GitOrigin-RevId: 21f3bc68974f4fd3213f72139c80e017b2295aaf
This commit is contained in:
committed by
intellij-monorepo-bot
parent
afde020692
commit
3905fdb644
@@ -33,15 +33,7 @@ public class HashSetInterner<T> extends Interner<T> {
|
||||
@Override
|
||||
@NotNull
|
||||
public T intern(@NotNull T name) {
|
||||
T interned = mySet.get(name);
|
||||
if (interned != null) {
|
||||
return interned;
|
||||
}
|
||||
|
||||
boolean added = mySet.add(name);
|
||||
assert added;
|
||||
|
||||
return name;
|
||||
return mySet.getOrAdd(name);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -17,6 +17,7 @@ package com.intellij.util.containers;
|
||||
|
||||
import gnu.trove.THashSet;
|
||||
import gnu.trove.TObjectHashingStrategy;
|
||||
import org.jetbrains.annotations.Contract;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
@@ -83,4 +84,22 @@ public class OpenTHashSet<T> extends THashSet<T> {
|
||||
final int index = index(obj);
|
||||
return index < 0 ? null : get(index);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns an element of this set equal to the given one, or adds the given one to the set and returns it.
|
||||
* Can be used for interning objects to save memory.
|
||||
*/
|
||||
@Contract("!null -> !null")
|
||||
public T getOrAdd(final T obj) {
|
||||
final int index = insertionIndex(obj);
|
||||
|
||||
boolean alreadyStored = index < 0;
|
||||
if (alreadyStored) return get(-index - 1);
|
||||
|
||||
Object old = _set[index];
|
||||
_set[index] = obj;
|
||||
|
||||
postInsertHook(old == null);
|
||||
return obj;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user