-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
FastImmutableTable for tile entities
- Loading branch information
Showing
6 changed files
with
313 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
241 changes: 241 additions & 0 deletions
241
src/main/java/net/gensokyoreimagined/nitori/core/FastImmutableTable.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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(); | ||
} | ||
} |
53 changes: 53 additions & 0 deletions
53
src/main/java/net/gensokyoreimagined/nitori/core/FastImmutableTableCache.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
} |