FastImmutableTable for tile entities
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
group=net.gensokyoreimagined.nitori
|
||||
version=1.0.4-SNAPSHOT
|
||||
version=1.0.5-SNAPSHOT
|
||||
description=Converting patches into mixins, for the Ignite Framework
|
||||
|
||||
org.gradle.parallel=true
|
||||
|
||||
BIN
gradle/wrapper/gradle-wrapper.jar
vendored
BIN
gradle/wrapper/gradle-wrapper.jar
vendored
Binary file not shown.
1
gradle/wrapper/gradle-wrapper.properties
vendored
1
gradle/wrapper/gradle-wrapper.properties
vendored
@@ -2,5 +2,6 @@ distributionBase=GRADLE_USER_HOME
|
||||
distributionPath=wrapper/dists
|
||||
distributionUrl=https\://services.gradle.org/distributions/gradle-8.5-bin.zip
|
||||
networkTimeout=10000
|
||||
validateDistributionUrl=true
|
||||
zipStoreBase=GRADLE_USER_HOME
|
||||
zipStorePath=wrapper/dists
|
||||
|
||||
29
gradlew
vendored
29
gradlew
vendored
@@ -83,10 +83,8 @@ done
|
||||
# This is normally unused
|
||||
# shellcheck disable=SC2034
|
||||
APP_BASE_NAME=${0##*/}
|
||||
APP_HOME=$( cd "${APP_HOME:-./}" && pwd -P ) || exit
|
||||
|
||||
# Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
|
||||
DEFAULT_JVM_OPTS='"-Xmx64m" "-Xms64m"'
|
||||
# Discard cd standard output in case $CDPATH is set (https://github.com/gradle/gradle/issues/25036)
|
||||
APP_HOME=$( cd "${APP_HOME:-./}" > /dev/null && pwd -P ) || exit
|
||||
|
||||
# Use the maximum available, or set MAX_FD != -1 to use that value.
|
||||
MAX_FD=maximum
|
||||
@@ -133,10 +131,13 @@ location of your Java installation."
|
||||
fi
|
||||
else
|
||||
JAVACMD=java
|
||||
which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
|
||||
if ! command -v java >/dev/null 2>&1
|
||||
then
|
||||
die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
|
||||
|
||||
Please set the JAVA_HOME variable in your environment to match the
|
||||
location of your Java installation."
|
||||
fi
|
||||
fi
|
||||
|
||||
# Increase the maximum file descriptors if we can.
|
||||
@@ -144,7 +145,7 @@ if ! "$cygwin" && ! "$darwin" && ! "$nonstop" ; then
|
||||
case $MAX_FD in #(
|
||||
max*)
|
||||
# In POSIX sh, ulimit -H is undefined. That's why the result is checked to see if it worked.
|
||||
# shellcheck disable=SC3045
|
||||
# shellcheck disable=SC2039,SC3045
|
||||
MAX_FD=$( ulimit -H -n ) ||
|
||||
warn "Could not query maximum file descriptor limit"
|
||||
esac
|
||||
@@ -152,7 +153,7 @@ if ! "$cygwin" && ! "$darwin" && ! "$nonstop" ; then
|
||||
'' | soft) :;; #(
|
||||
*)
|
||||
# In POSIX sh, ulimit -n is undefined. That's why the result is checked to see if it worked.
|
||||
# shellcheck disable=SC3045
|
||||
# shellcheck disable=SC2039,SC3045
|
||||
ulimit -n "$MAX_FD" ||
|
||||
warn "Could not set maximum file descriptor limit to $MAX_FD"
|
||||
esac
|
||||
@@ -197,11 +198,15 @@ if "$cygwin" || "$msys" ; then
|
||||
done
|
||||
fi
|
||||
|
||||
# Collect all arguments for the java command;
|
||||
# * $DEFAULT_JVM_OPTS, $JAVA_OPTS, and $GRADLE_OPTS can contain fragments of
|
||||
# shell script including quotes and variable substitutions, so put them in
|
||||
# double quotes to make sure that they get re-expanded; and
|
||||
# * put everything else in single quotes, so that it's not re-expanded.
|
||||
|
||||
# Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
|
||||
DEFAULT_JVM_OPTS='"-Xmx64m" "-Xms64m"'
|
||||
|
||||
# Collect all arguments for the java command:
|
||||
# * DEFAULT_JVM_OPTS, JAVA_OPTS, JAVA_OPTS, and optsEnvironmentVar are not allowed to contain shell fragments,
|
||||
# and any embedded shellness will be escaped.
|
||||
# * For example: A user cannot expect ${Hostname} to be expanded, as it is an environment variable and will be
|
||||
# treated as '${Hostname}' itself on the command line.
|
||||
|
||||
set -- \
|
||||
"-Dorg.gradle.appname=$APP_BASE_NAME" \
|
||||
|
||||
@@ -0,0 +1,241 @@
|
||||
// Nitori Copyright (C) 2024 Gensokyo Reimagined
|
||||
//
|
||||
// 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 <https://www.gnu.org/licenses/>.
|
||||
package net.gensokyoreimagined.nitori.core;
|
||||
|
||||
import com.google.common.collect.Table;
|
||||
import it.unimi.dsi.fastutil.Hash;
|
||||
import it.unimi.dsi.fastutil.HashCommon;
|
||||
import org.apache.commons.lang3.ArrayUtils;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
import static it.unimi.dsi.fastutil.HashCommon.arraySize;
|
||||
|
||||
public class FastImmutableTable<R, C, V> implements Table<R, C, V> {
|
||||
private R[] rowKeys;
|
||||
private int[] rowIndices;
|
||||
private final int rowMask;
|
||||
|
||||
private C[] colKeys;
|
||||
private int[] colIndices;
|
||||
private final int colMask;
|
||||
private final int colCount;
|
||||
|
||||
private V[] values;
|
||||
private final int size;
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public FastImmutableTable(Table<R, C, V> table, FastImmutableTableCache<R, C, V> cache) {
|
||||
if (cache == null) {
|
||||
throw new IllegalArgumentException("Cache must not be null");
|
||||
}
|
||||
|
||||
float loadFactor = Hash.DEFAULT_LOAD_FACTOR;
|
||||
|
||||
Set<R> rowKeySet = table.rowKeySet();
|
||||
Set<C> colKeySet = table.columnKeySet();
|
||||
|
||||
int rowCount = rowKeySet.size();
|
||||
this.colCount = colKeySet.size();
|
||||
|
||||
int rowN = arraySize(rowCount, loadFactor);
|
||||
int colN = arraySize(this.colCount, loadFactor);
|
||||
|
||||
this.rowMask = rowN - 1;
|
||||
this.rowKeys = (R[]) new Object[rowN];
|
||||
this.rowIndices = new int[rowN];
|
||||
|
||||
this.colMask = colN - 1;
|
||||
this.colKeys = (C[]) new Object[colN];
|
||||
this.colIndices = new int[colN];
|
||||
|
||||
this.createIndex(this.colKeys, this.colIndices, this.colMask, colKeySet);
|
||||
this.createIndex(this.rowKeys, this.rowIndices, this.rowMask, rowKeySet);
|
||||
|
||||
this.values = (V[]) new Object[rowCount * this.colCount];
|
||||
|
||||
for (Cell<R, C, V> cell : table.cellSet()) {
|
||||
int colIdx = this.getIndex(this.colKeys, this.colIndices, this.colMask, cell.getColumnKey());
|
||||
int rowIdx = this.getIndex(this.rowKeys, this.rowIndices, this.rowMask, cell.getRowKey());
|
||||
|
||||
if (colIdx < 0 || rowIdx < 0) {
|
||||
throw new IllegalStateException("Missing index for " + cell);
|
||||
}
|
||||
|
||||
this.values[this.colCount * rowIdx + colIdx] = cell.getValue();
|
||||
}
|
||||
|
||||
this.size = table.size();
|
||||
|
||||
this.rowKeys = cache.dedupRows(this.rowKeys);
|
||||
this.rowIndices = cache.dedupIndices(this.rowIndices);
|
||||
|
||||
this.colIndices = cache.dedupIndices(this.colIndices);
|
||||
this.colKeys = cache.dedupColumns(this.colKeys);
|
||||
|
||||
this.values = cache.dedupValues(this.values);
|
||||
}
|
||||
|
||||
private <T> void createIndex(T[] keys, int[] indices, int mask, Collection<T> iterable) {
|
||||
int index = 0;
|
||||
|
||||
for (T obj : iterable) {
|
||||
int i = this.find(keys, mask, obj);
|
||||
|
||||
if (i < 0) {
|
||||
int pos = -i - 1;
|
||||
|
||||
keys[pos] = obj;
|
||||
indices[pos] = index++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private <T> int getIndex(T[] keys, int[] indices, int mask, T key) {
|
||||
int pos = this.find(keys, mask, key);
|
||||
|
||||
if (pos < 0) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
return indices[pos];
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean contains(Object rowKey, Object columnKey) {
|
||||
return this.get(rowKey, columnKey) != null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean containsRow(Object rowKey) {
|
||||
return this.find(this.rowKeys, this.rowMask, rowKey) >= 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean containsColumn(Object columnKey) {
|
||||
return this.find(this.colKeys, this.colMask, columnKey) >= 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean containsValue(Object value) {
|
||||
return ArrayUtils.contains(this.values, value);
|
||||
}
|
||||
|
||||
@Override
|
||||
public V get(Object rowKey, Object columnKey) {
|
||||
final int row = this.getIndex(this.rowKeys, this.rowIndices, this.rowMask, rowKey);
|
||||
final int col = this.getIndex(this.colKeys, this.colIndices, this.colMask, columnKey);
|
||||
|
||||
if (row < 0 || col < 0) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return this.values[this.colCount * row + col];
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isEmpty() {
|
||||
return this.size() == 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int size() {
|
||||
return this.size;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void clear() {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
@Override
|
||||
public V put(R rowKey, C columnKey, V val) {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
private <T> int find(T[] key, int mask, T value) {
|
||||
T curr;
|
||||
int pos;
|
||||
// The starting point.
|
||||
if ((curr = key[pos = HashCommon.mix(value.hashCode()) & mask]) == null) {
|
||||
return -(pos + 1);
|
||||
}
|
||||
if (value.equals(curr)) {
|
||||
return pos;
|
||||
}
|
||||
// There's always an unused entry.
|
||||
while (true) {
|
||||
if ((curr = key[pos = pos + 1 & mask]) == null) {
|
||||
return -(pos + 1);
|
||||
}
|
||||
if (value.equals(curr)) {
|
||||
return pos;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void putAll(@NotNull Table<? extends R, ? extends C, ? extends V> table) {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
@Override
|
||||
public V remove(Object rowKey, Object columnKey) {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NotNull Map<C, V> row(R rowKey) {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NotNull Map<R, V> column(C columnKey) {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NotNull Set<Cell<R, C, V>> cellSet() {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NotNull Set<R> rowKeySet() {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NotNull Set<C> columnKeySet() {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NotNull Collection<V> values() {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NotNull Map<R, Map<C, V>> rowMap() {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NotNull Map<C, Map<R, V>> columnMap() {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,53 @@
|
||||
// Nitori Copyright (C) 2024 Gensokyo Reimagined
|
||||
//
|
||||
// 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 <https://www.gnu.org/licenses/>.
|
||||
package net.gensokyoreimagined.nitori.core;
|
||||
|
||||
import it.unimi.dsi.fastutil.Hash;
|
||||
import it.unimi.dsi.fastutil.ints.IntArrays;
|
||||
import it.unimi.dsi.fastutil.objects.ObjectArrays;
|
||||
import it.unimi.dsi.fastutil.objects.ObjectOpenCustomHashSet;
|
||||
|
||||
public class FastImmutableTableCache<R, C, V> {
|
||||
private final ObjectOpenCustomHashSet<R[]> rows;
|
||||
private final ObjectOpenCustomHashSet<C[]> columns;
|
||||
private final ObjectOpenCustomHashSet<V[]> values;
|
||||
|
||||
private final ObjectOpenCustomHashSet<int[]> indices;
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public FastImmutableTableCache() {
|
||||
this.rows = new ObjectOpenCustomHashSet<>((Hash.Strategy<R[]>) ObjectArrays.HASH_STRATEGY);
|
||||
this.columns = new ObjectOpenCustomHashSet<>((Hash.Strategy<C[]>) ObjectArrays.HASH_STRATEGY);
|
||||
this.values = new ObjectOpenCustomHashSet<>((Hash.Strategy<V[]>) ObjectArrays.HASH_STRATEGY);
|
||||
|
||||
this.indices = new ObjectOpenCustomHashSet<>(IntArrays.HASH_STRATEGY);
|
||||
}
|
||||
|
||||
public synchronized V[] dedupValues(V[] values) {
|
||||
return this.values.addOrGet(values);
|
||||
}
|
||||
|
||||
public synchronized R[] dedupRows(R[] rows) {
|
||||
return this.rows.addOrGet(rows);
|
||||
}
|
||||
|
||||
public synchronized C[] dedupColumns(C[] columns) {
|
||||
return this.columns.addOrGet(columns);
|
||||
}
|
||||
|
||||
public synchronized int[] dedupIndices(int[] ints) {
|
||||
return this.indices.addOrGet(ints);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user