mirror of
https://github.com/GTNewHorizons/GTNHLib.git
synced 2026-11-26 14:53:14 +00:00
Port 2d & 3d maps & sets from CC (#426)
This commit is contained in:
1 parent
d6659740b0
commit
0fbbcadcce
24 files changed
+2135
-1
No files matched your search
@@ -2,9 +2,10 @@ package com.gtnewhorizon.gtnhlib.blockpos;
|
||||
|
||||
import net.minecraftforge.common.util.ForgeDirection;
|
||||
|
||||
import com.gtnewhorizon.gtnhlib.space.XYZAddressable;
|
||||
import com.gtnewhorizon.gtnhlib.util.CoordinatePacker;
|
||||
|
||||
public interface IBlockPos {
|
||||
public interface IBlockPos extends XYZAddressable {
|
||||
|
||||
int getX();
|
||||
|
||||
|
||||
@@ -0,0 +1,319 @@
|
||||
package com.gtnewhorizon.gtnhlib.datastructs.space;
|
||||
|
||||
import static com.gtnewhorizon.gtnhlib.util.CoordinatePacker2D.packChunk;
|
||||
import static com.gtnewhorizon.gtnhlib.util.CoordinatePacker2D.unpackChunkX;
|
||||
import static com.gtnewhorizon.gtnhlib.util.CoordinatePacker2D.unpackChunkZ;
|
||||
|
||||
import java.util.Iterator;
|
||||
import java.util.Spliterator;
|
||||
import java.util.Spliterators;
|
||||
import java.util.function.BiFunction;
|
||||
import java.util.function.Consumer;
|
||||
import java.util.stream.Stream;
|
||||
import java.util.stream.StreamSupport;
|
||||
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import com.gtnewhorizon.gtnhlib.functional.Compute2D;
|
||||
import com.gtnewhorizon.gtnhlib.functional.Compute2DWithValue;
|
||||
import com.gtnewhorizon.gtnhlib.functional.Consumer2DWithValue;
|
||||
import com.gtnewhorizon.gtnhlib.space.XZAddressable;
|
||||
|
||||
import it.unimi.dsi.fastutil.longs.Long2ObjectMaps;
|
||||
import it.unimi.dsi.fastutil.longs.Long2ObjectOpenHashMap;
|
||||
import it.unimi.dsi.fastutil.objects.AbstractObjectSet;
|
||||
import it.unimi.dsi.fastutil.objects.ObjectIterator;
|
||||
import it.unimi.dsi.fastutil.objects.ObjectSet;
|
||||
|
||||
@SuppressWarnings("unused")
|
||||
public class HashMap2D<V> extends Long2ObjectOpenHashMap<V> {
|
||||
|
||||
public V get(int posX, int posZ) {
|
||||
return super.get(packChunk(posX, posZ));
|
||||
}
|
||||
|
||||
public V get(XZAddressable xyz) {
|
||||
return get(xyz.getX(), xyz.getZ());
|
||||
}
|
||||
|
||||
public V remove(int posX, int posZ) {
|
||||
return super.remove(packChunk(posX, posZ));
|
||||
}
|
||||
|
||||
public V remove(XZAddressable xyz) {
|
||||
return remove(xyz.getX(), xyz.getZ());
|
||||
}
|
||||
|
||||
public boolean containsKey(int posX, int posZ) {
|
||||
return super.containsKey(packChunk(posX, posZ));
|
||||
}
|
||||
|
||||
public boolean containsKey(XZAddressable xyz) {
|
||||
return containsKey(xyz.getX(), xyz.getZ());
|
||||
}
|
||||
|
||||
public V put(int posX, int posZ, V v) {
|
||||
return super.put(packChunk(posX, posZ), v);
|
||||
}
|
||||
|
||||
public V put(XZAddressable xyz, V v) {
|
||||
return put(xyz.getX(), xyz.getZ(), v);
|
||||
}
|
||||
|
||||
public V computeIfAbsent(int posX, int posZ, @NotNull Compute2D<V> mappingFunction) {
|
||||
V v;
|
||||
|
||||
long key = packChunk(posX, posZ);
|
||||
|
||||
if ((v = get(key)) == null) {
|
||||
V newValue;
|
||||
if ((newValue = mappingFunction.apply(posX, posZ)) != null) {
|
||||
put(key, newValue);
|
||||
return newValue;
|
||||
}
|
||||
}
|
||||
|
||||
return v;
|
||||
}
|
||||
|
||||
public V computeIfAbsent(XZAddressable xyz, @NotNull Compute2D<V> mappingFunction) {
|
||||
return computeIfAbsent(xyz.getX(), xyz.getZ(), mappingFunction);
|
||||
}
|
||||
|
||||
public V compute(int posX, int posZ, @NotNull Compute2DWithValue<V> remappingFunction) {
|
||||
return super.compute(packChunk(posX, posZ), (k, v) -> remappingFunction.apply(posX, posZ, v));
|
||||
}
|
||||
|
||||
public V compute(XZAddressable xyz, @NotNull Compute2DWithValue<V> remappingFunction) {
|
||||
return compute(xyz.getX(), xyz.getZ(), remappingFunction);
|
||||
}
|
||||
|
||||
public V computeIfPresent(int posX, int posZ, @NotNull Compute2DWithValue<V> remappingFunction) {
|
||||
return super.computeIfPresent(packChunk(posX, posZ), (k, v) -> remappingFunction.apply(posX, posZ, v));
|
||||
}
|
||||
|
||||
public V computeIfPresent(XZAddressable xyz, @NotNull Compute2DWithValue<V> remappingFunction) {
|
||||
return computeIfPresent(xyz.getX(), xyz.getZ(), remappingFunction);
|
||||
}
|
||||
|
||||
public V getOrDefault(int posX, int posZ, V defaultValue) {
|
||||
return super.getOrDefault(packChunk(posX, posZ), defaultValue);
|
||||
}
|
||||
|
||||
public V getOrDefault(XZAddressable xyz, V defaultValue) {
|
||||
return getOrDefault(xyz.getX(), xyz.getZ(), defaultValue);
|
||||
}
|
||||
|
||||
/// Unlike [java.util.Map#putIfAbsent], a key already mapped to `null` is treated as present and will not be
|
||||
/// overwritten.
|
||||
public V putIfAbsent(int posX, int posZ, V v) {
|
||||
return super.putIfAbsent(packChunk(posX, posZ), v);
|
||||
}
|
||||
|
||||
public V putIfAbsent(XZAddressable xyz, V v) {
|
||||
return putIfAbsent(xyz.getX(), xyz.getZ(), v);
|
||||
}
|
||||
|
||||
public boolean remove(int posX, int posZ, V value) {
|
||||
return super.remove(packChunk(posX, posZ), value);
|
||||
}
|
||||
|
||||
public boolean remove(XZAddressable xyz, V value) {
|
||||
return remove(xyz.getX(), xyz.getZ(), value);
|
||||
}
|
||||
|
||||
public V replace(int posX, int posZ, V value) {
|
||||
return super.replace(packChunk(posX, posZ), value);
|
||||
}
|
||||
|
||||
public V replace(XZAddressable xyz, V value) {
|
||||
return replace(xyz.getX(), xyz.getZ(), value);
|
||||
}
|
||||
|
||||
public boolean replace(int posX, int posZ, V oldValue, V newValue) {
|
||||
return super.replace(packChunk(posX, posZ), oldValue, newValue);
|
||||
}
|
||||
|
||||
public boolean replace(XZAddressable xyz, V oldValue, V newValue) {
|
||||
return replace(xyz.getX(), xyz.getZ(), oldValue, newValue);
|
||||
}
|
||||
|
||||
public V merge(int posX, int posZ, @NotNull V value,
|
||||
@NotNull BiFunction<? super V, ? super V, ? extends V> remappingFunction) {
|
||||
return super.merge(packChunk(posX, posZ), value, remappingFunction);
|
||||
}
|
||||
|
||||
public V merge(XZAddressable xyz, @NotNull V value,
|
||||
@NotNull BiFunction<? super V, ? super V, ? extends V> remappingFunction) {
|
||||
return merge(xyz.getX(), xyz.getZ(), value, remappingFunction);
|
||||
}
|
||||
|
||||
public void forEach(Consumer2DWithValue<V> consumer) {
|
||||
for (var e : this.fastEntryIterable()) {
|
||||
consumer.accept(e.getX(), e.getZ(), e.getValue());
|
||||
}
|
||||
}
|
||||
|
||||
public void replaceAll(@NotNull Compute2DWithValue<V> function) {
|
||||
var iter = Long2ObjectMaps.fastIterator(this);
|
||||
|
||||
while (iter.hasNext()) {
|
||||
var e = iter.next();
|
||||
|
||||
V value = e.getValue();
|
||||
V newValue = function.apply(unpackChunkX(e.getLongKey()), unpackChunkZ(e.getLongKey()), value);
|
||||
|
||||
if (newValue != value) {
|
||||
e.setValue(newValue);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public interface FastEntrySet2D<V> extends ObjectSet<Entry2D<V>> {
|
||||
|
||||
/**
|
||||
* Returns a fast iterator over this entry set; the iterator might return always the same entry instance,
|
||||
* suitably mutated.
|
||||
*
|
||||
* @return a fast iterator over this entry set; the iterator might return always the same
|
||||
* {@link java.util.Map.Entry} instance, suitably mutated.
|
||||
*/
|
||||
ObjectIterator<Entry2D<V>> fastIterator();
|
||||
|
||||
/**
|
||||
* Iterates quickly over this entry set; the iteration might happen always on the same entry instance, suitably
|
||||
* mutated.
|
||||
*
|
||||
* <p>
|
||||
*
|
||||
* This default implementation just delegates to {@link #forEach(Consumer)}.
|
||||
*
|
||||
* @param consumer a consumer that will by applied to the entries of this set; the entries might be represented
|
||||
* by the same entry instance, suitably mutated.
|
||||
* @since 8.1.0
|
||||
*/
|
||||
default void fastForEach(final Consumer<? super Entry2D<V>> consumer) {
|
||||
forEach(consumer);
|
||||
}
|
||||
}
|
||||
|
||||
private FastEntrySet2DImpl entrySet;
|
||||
|
||||
public FastEntrySet2D<V> fastEntrySet() {
|
||||
if (entrySet == null) entrySet = new FastEntrySet2DImpl();
|
||||
|
||||
return entrySet;
|
||||
}
|
||||
|
||||
public Iterable<Entry2D<V>> fastEntryIterable() {
|
||||
return () -> fastEntrySet().fastIterator();
|
||||
}
|
||||
|
||||
/// Unlike {@link #fastEntryIterable()}, each returned {@link Entry2D} is an independent instance, so the result
|
||||
/// is safe to buffer (e.g. via {@link Stream#sorted()} or {@link Stream#toList()}).
|
||||
public Iterator<Entry2D<V>> slowIterator() {
|
||||
return fastEntrySet().iterator();
|
||||
}
|
||||
|
||||
public Stream<Entry2D<V>> slowStream() {
|
||||
return StreamSupport.stream(
|
||||
Spliterators.spliterator(
|
||||
slowIterator(),
|
||||
size(),
|
||||
Spliterator.SIZED | Spliterator.NONNULL | Spliterator.DISTINCT),
|
||||
false);
|
||||
}
|
||||
|
||||
private class FastEntrySet2DImpl extends AbstractObjectSet<Entry2D<V>> implements FastEntrySet2D<V> {
|
||||
|
||||
@Override
|
||||
public ObjectIterator<Entry2D<V>> fastIterator() {
|
||||
Entry2D<V> entry = new Entry2D<>();
|
||||
|
||||
var iter = HashMap2D.this.long2ObjectEntrySet().fastIterator();
|
||||
|
||||
return new ObjectIterator<>() {
|
||||
|
||||
@Override
|
||||
public boolean hasNext() {
|
||||
return iter.hasNext();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Entry2D<V> next() {
|
||||
var e = iter.next();
|
||||
|
||||
entry.setKey(e.getLongKey());
|
||||
entry.setValue(e.getValue());
|
||||
|
||||
return entry;
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NotNull ObjectIterator<Entry2D<V>> iterator() {
|
||||
var iter = HashMap2D.this.long2ObjectEntrySet().fastIterator();
|
||||
|
||||
return new ObjectIterator<>() {
|
||||
|
||||
@Override
|
||||
public boolean hasNext() {
|
||||
return iter.hasNext();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Entry2D<V> next() {
|
||||
var e = iter.next();
|
||||
|
||||
return new Entry2D<>(e.getLongKey(), e.getValue());
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
@Override
|
||||
public int size() {
|
||||
return HashMap2D.this.size();
|
||||
}
|
||||
}
|
||||
|
||||
public static class Entry2D<T> extends BasicEntry<T> implements XZAddressable {
|
||||
|
||||
public Entry2D() {}
|
||||
|
||||
/// @deprecated Use [#Entry2D(long, Object)] to avoid the long boxing.
|
||||
@Deprecated
|
||||
public Entry2D(Long key, T value) {
|
||||
super(key, value);
|
||||
}
|
||||
|
||||
public Entry2D(long key, T value) {
|
||||
super(key, value);
|
||||
}
|
||||
|
||||
public Entry2D(int posX, int posZ, T value) {
|
||||
super(packChunk(posX, posZ), value);
|
||||
}
|
||||
|
||||
void setKey(long key) {
|
||||
super.key = key;
|
||||
}
|
||||
|
||||
@Override
|
||||
public T setValue(T value) {
|
||||
T old = super.value;
|
||||
super.value = value;
|
||||
return old;
|
||||
}
|
||||
|
||||
@Override
|
||||
public final int getX() {
|
||||
return unpackChunkX(getLongKey());
|
||||
}
|
||||
|
||||
@Override
|
||||
public final int getZ() {
|
||||
return unpackChunkZ(getLongKey());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,323 @@
|
||||
package com.gtnewhorizon.gtnhlib.datastructs.space;
|
||||
|
||||
import static com.gtnewhorizon.gtnhlib.util.CoordinatePacker.pack;
|
||||
import static com.gtnewhorizon.gtnhlib.util.CoordinatePacker.unpackX;
|
||||
import static com.gtnewhorizon.gtnhlib.util.CoordinatePacker.unpackY;
|
||||
import static com.gtnewhorizon.gtnhlib.util.CoordinatePacker.unpackZ;
|
||||
|
||||
import java.util.Iterator;
|
||||
import java.util.Spliterator;
|
||||
import java.util.Spliterators;
|
||||
import java.util.function.BiFunction;
|
||||
import java.util.function.Consumer;
|
||||
import java.util.stream.Stream;
|
||||
import java.util.stream.StreamSupport;
|
||||
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import com.gtnewhorizon.gtnhlib.functional.Compute3D;
|
||||
import com.gtnewhorizon.gtnhlib.functional.Compute3DWithValue;
|
||||
import com.gtnewhorizon.gtnhlib.functional.Consumer3DWithValue;
|
||||
import com.gtnewhorizon.gtnhlib.space.XYZAddressable;
|
||||
|
||||
import it.unimi.dsi.fastutil.longs.Long2ObjectMaps;
|
||||
import it.unimi.dsi.fastutil.longs.Long2ObjectOpenHashMap;
|
||||
import it.unimi.dsi.fastutil.objects.AbstractObjectSet;
|
||||
import it.unimi.dsi.fastutil.objects.ObjectIterator;
|
||||
import it.unimi.dsi.fastutil.objects.ObjectSet;
|
||||
|
||||
@SuppressWarnings("unused")
|
||||
public class HashMap3D<V> extends Long2ObjectOpenHashMap<V> {
|
||||
|
||||
public V get(int posX, int posY, int posZ) {
|
||||
return super.get(pack(posX, posY, posZ));
|
||||
}
|
||||
|
||||
public V get(XYZAddressable xyz) {
|
||||
return get(xyz.getX(), xyz.getY(), xyz.getZ());
|
||||
}
|
||||
|
||||
public V remove(int posX, int posY, int posZ) {
|
||||
return super.remove(pack(posX, posY, posZ));
|
||||
}
|
||||
|
||||
public V remove(XYZAddressable xyz) {
|
||||
return remove(xyz.getX(), xyz.getY(), xyz.getZ());
|
||||
}
|
||||
|
||||
public boolean containsKey(int posX, int posY, int posZ) {
|
||||
return super.containsKey(pack(posX, posY, posZ));
|
||||
}
|
||||
|
||||
public boolean containsKey(XYZAddressable xyz) {
|
||||
return containsKey(xyz.getX(), xyz.getY(), xyz.getZ());
|
||||
}
|
||||
|
||||
public V put(int posX, int posY, int posZ, V v) {
|
||||
return super.put(pack(posX, posY, posZ), v);
|
||||
}
|
||||
|
||||
public V put(XYZAddressable xyz, V v) {
|
||||
return put(xyz.getX(), xyz.getY(), xyz.getZ(), v);
|
||||
}
|
||||
|
||||
public V computeIfAbsent(int posX, int posY, int posZ, @NotNull Compute3D<V> mappingFunction) {
|
||||
V v;
|
||||
|
||||
long key = pack(posX, posY, posZ);
|
||||
|
||||
if ((v = get(key)) == null) {
|
||||
V newValue;
|
||||
if ((newValue = mappingFunction.apply(posX, posY, posZ)) != null) {
|
||||
put(key, newValue);
|
||||
return newValue;
|
||||
}
|
||||
}
|
||||
|
||||
return v;
|
||||
}
|
||||
|
||||
public V computeIfAbsent(XYZAddressable xyz, @NotNull Compute3D<V> mappingFunction) {
|
||||
return computeIfAbsent(xyz.getX(), xyz.getY(), xyz.getZ(), mappingFunction);
|
||||
}
|
||||
|
||||
public V compute(int posX, int posY, int posZ, @NotNull Compute3DWithValue<V> remappingFunction) {
|
||||
return super.compute(pack(posX, posY, posZ), (k, v) -> remappingFunction.apply(posX, posY, posZ, v));
|
||||
}
|
||||
|
||||
public V compute(XYZAddressable xyz, @NotNull Compute3DWithValue<V> remappingFunction) {
|
||||
return compute(xyz.getX(), xyz.getY(), xyz.getZ(), remappingFunction);
|
||||
}
|
||||
|
||||
public V computeIfPresent(int posX, int posY, int posZ, @NotNull Compute3DWithValue<V> remappingFunction) {
|
||||
return super.computeIfPresent(pack(posX, posY, posZ), (k, v) -> remappingFunction.apply(posX, posY, posZ, v));
|
||||
}
|
||||
|
||||
public V computeIfPresent(XYZAddressable xyz, @NotNull Compute3DWithValue<V> remappingFunction) {
|
||||
return computeIfPresent(xyz.getX(), xyz.getY(), xyz.getZ(), remappingFunction);
|
||||
}
|
||||
|
||||
public V getOrDefault(int posX, int posY, int posZ, V defaultValue) {
|
||||
return super.getOrDefault(pack(posX, posY, posZ), defaultValue);
|
||||
}
|
||||
|
||||
public V getOrDefault(XYZAddressable xyz, V defaultValue) {
|
||||
return getOrDefault(xyz.getX(), xyz.getY(), xyz.getZ(), defaultValue);
|
||||
}
|
||||
|
||||
/// Unlike [java.util.Map#putIfAbsent], a key already mapped to `null` is treated as present and will not be
|
||||
/// overwritten.
|
||||
public V putIfAbsent(int posX, int posY, int posZ, V v) {
|
||||
return super.putIfAbsent(pack(posX, posY, posZ), v);
|
||||
}
|
||||
|
||||
public V putIfAbsent(XYZAddressable xyz, V v) {
|
||||
return putIfAbsent(xyz.getX(), xyz.getY(), xyz.getZ(), v);
|
||||
}
|
||||
|
||||
public boolean remove(int posX, int posY, int posZ, V value) {
|
||||
return super.remove(pack(posX, posY, posZ), value);
|
||||
}
|
||||
|
||||
public boolean remove(XYZAddressable xyz, V value) {
|
||||
return remove(xyz.getX(), xyz.getY(), xyz.getZ(), value);
|
||||
}
|
||||
|
||||
public V replace(int posX, int posY, int posZ, V value) {
|
||||
return super.replace(pack(posX, posY, posZ), value);
|
||||
}
|
||||
|
||||
public V replace(XYZAddressable xyz, V value) {
|
||||
return replace(xyz.getX(), xyz.getY(), xyz.getZ(), value);
|
||||
}
|
||||
|
||||
public boolean replace(int posX, int posY, int posZ, V oldValue, V newValue) {
|
||||
return super.replace(pack(posX, posY, posZ), oldValue, newValue);
|
||||
}
|
||||
|
||||
public boolean replace(XYZAddressable xyz, V oldValue, V newValue) {
|
||||
return replace(xyz.getX(), xyz.getY(), xyz.getZ(), oldValue, newValue);
|
||||
}
|
||||
|
||||
public V merge(int posX, int posY, int posZ, @NotNull V value,
|
||||
@NotNull BiFunction<? super V, ? super V, ? extends V> remappingFunction) {
|
||||
return super.merge(pack(posX, posY, posZ), value, remappingFunction);
|
||||
}
|
||||
|
||||
public V merge(XYZAddressable xyz, @NotNull V value,
|
||||
@NotNull BiFunction<? super V, ? super V, ? extends V> remappingFunction) {
|
||||
return merge(xyz.getX(), xyz.getY(), xyz.getZ(), value, remappingFunction);
|
||||
}
|
||||
|
||||
public void forEach(Consumer3DWithValue<V> consumer) {
|
||||
for (var e : this.fastEntryIterable()) {
|
||||
consumer.accept(e.getX(), e.getY(), e.getZ(), e.getValue());
|
||||
}
|
||||
}
|
||||
|
||||
public void replaceAll(@NotNull Compute3DWithValue<V> function) {
|
||||
var iter = Long2ObjectMaps.fastIterator(this);
|
||||
|
||||
while (iter.hasNext()) {
|
||||
var e = iter.next();
|
||||
|
||||
long key = e.getLongKey();
|
||||
V value = e.getValue();
|
||||
V newValue = function.apply(unpackX(key), unpackY(key), unpackZ(key), value);
|
||||
|
||||
if (newValue != value) {
|
||||
e.setValue(newValue);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public interface FastEntrySet3D<V> extends ObjectSet<Entry3D<V>> {
|
||||
|
||||
/**
|
||||
* Returns a fast iterator over this entry set; the iterator might return always the same entry instance,
|
||||
* suitably mutated.
|
||||
*
|
||||
* @return a fast iterator over this entry set; the iterator might return always the same
|
||||
* {@link java.util.Map.Entry} instance, suitably mutated.
|
||||
*/
|
||||
ObjectIterator<Entry3D<V>> fastIterator();
|
||||
|
||||
/**
|
||||
* Iterates quickly over this entry set; the iteration might happen always on the same entry instance, suitably
|
||||
* mutated.
|
||||
*
|
||||
* <p>
|
||||
*
|
||||
* This default implementation just delegates to {@link #forEach(Consumer)}.
|
||||
*
|
||||
* @param consumer a consumer that will by applied to the entries of this set; the entries might be represented
|
||||
* by the same entry instance, suitably mutated.
|
||||
* @since 8.1.0
|
||||
*/
|
||||
default void fastForEach(final Consumer<? super Entry3D<V>> consumer) {
|
||||
forEach(consumer);
|
||||
}
|
||||
}
|
||||
|
||||
private FastEntrySet3DImpl entrySet;
|
||||
|
||||
public FastEntrySet3D<V> fastEntrySet() {
|
||||
if (entrySet == null) entrySet = new FastEntrySet3DImpl();
|
||||
|
||||
return entrySet;
|
||||
}
|
||||
|
||||
public Iterable<Entry3D<V>> fastEntryIterable() {
|
||||
return () -> fastEntrySet().fastIterator();
|
||||
}
|
||||
|
||||
/// Unlike {@link #fastEntryIterable()}, each returned {@link Entry3D} is an independent instance, so the result
|
||||
/// is safe to buffer (e.g. via {@link Stream#sorted()} or {@link Stream#toList()}).
|
||||
public Iterator<Entry3D<V>> slowIterator() {
|
||||
return fastEntrySet().iterator();
|
||||
}
|
||||
|
||||
public Stream<Entry3D<V>> slowStream() {
|
||||
return StreamSupport.stream(
|
||||
Spliterators.spliterator(
|
||||
slowIterator(),
|
||||
size(),
|
||||
Spliterator.SIZED | Spliterator.NONNULL | Spliterator.DISTINCT),
|
||||
false);
|
||||
}
|
||||
|
||||
private class FastEntrySet3DImpl extends AbstractObjectSet<Entry3D<V>> implements FastEntrySet3D<V> {
|
||||
|
||||
@Override
|
||||
public ObjectIterator<Entry3D<V>> fastIterator() {
|
||||
Entry3D<V> entry = new Entry3D<>();
|
||||
|
||||
var iter = HashMap3D.this.long2ObjectEntrySet().fastIterator();
|
||||
|
||||
return new ObjectIterator<>() {
|
||||
|
||||
@Override
|
||||
public boolean hasNext() {
|
||||
return iter.hasNext();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Entry3D<V> next() {
|
||||
var e = iter.next();
|
||||
|
||||
entry.setKey(e.getLongKey());
|
||||
entry.setValue(e.getValue());
|
||||
|
||||
return entry;
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NotNull ObjectIterator<Entry3D<V>> iterator() {
|
||||
var iter = HashMap3D.this.long2ObjectEntrySet().fastIterator();
|
||||
|
||||
return new ObjectIterator<>() {
|
||||
|
||||
@Override
|
||||
public boolean hasNext() {
|
||||
return iter.hasNext();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Entry3D<V> next() {
|
||||
var e = iter.next();
|
||||
|
||||
return new Entry3D<>(e.getLongKey(), e.getValue());
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
@Override
|
||||
public int size() {
|
||||
return HashMap3D.this.size();
|
||||
}
|
||||
}
|
||||
|
||||
public static class Entry3D<T> extends BasicEntry<T> implements XYZAddressable {
|
||||
|
||||
public Entry3D() {}
|
||||
|
||||
/// @deprecated Use [#Entry3D(long, Object)] to avoid the long boxing.
|
||||
@Deprecated
|
||||
public Entry3D(Long key, T value) {
|
||||
super(key, value);
|
||||
}
|
||||
|
||||
public Entry3D(long key, T value) {
|
||||
super(key, value);
|
||||
}
|
||||
|
||||
public Entry3D(int posX, int posY, int posZ, T value) {
|
||||
super(pack(posX, posY, posZ), value);
|
||||
}
|
||||
|
||||
void setKey(long key) {
|
||||
super.key = key;
|
||||
}
|
||||
|
||||
@Override
|
||||
public T setValue(T value) {
|
||||
T old = super.value;
|
||||
super.value = value;
|
||||
return old;
|
||||
}
|
||||
|
||||
public final int getX() {
|
||||
return unpackX(getLongKey());
|
||||
}
|
||||
|
||||
public final int getY() {
|
||||
return unpackY(getLongKey());
|
||||
}
|
||||
|
||||
public final int getZ() {
|
||||
return unpackZ(getLongKey());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,113 @@
|
||||
package com.gtnewhorizon.gtnhlib.datastructs.space;
|
||||
|
||||
import static com.gtnewhorizon.gtnhlib.util.CoordinatePacker2D.packChunk;
|
||||
|
||||
import java.util.Iterator;
|
||||
import java.util.Spliterator;
|
||||
import java.util.Spliterators;
|
||||
import java.util.stream.Stream;
|
||||
import java.util.stream.StreamSupport;
|
||||
|
||||
import org.jetbrains.annotations.Contract;
|
||||
|
||||
import com.gtnewhorizon.gtnhlib.functional.Consumer2D;
|
||||
import com.gtnewhorizon.gtnhlib.space.MutableXZ;
|
||||
import com.gtnewhorizon.gtnhlib.space.XZAddressable;
|
||||
import com.gtnewhorizon.gtnhlib.util.CoordinatePacker2D;
|
||||
|
||||
import it.unimi.dsi.fastutil.longs.LongIterator;
|
||||
import it.unimi.dsi.fastutil.longs.LongOpenHashSet;
|
||||
|
||||
@SuppressWarnings("unused")
|
||||
public class HashSet2D extends LongOpenHashSet {
|
||||
|
||||
public boolean contains(int posX, int posZ) {
|
||||
return super.contains(packChunk(posX, posZ));
|
||||
}
|
||||
|
||||
public boolean contains(XZAddressable xyz) {
|
||||
return contains(xyz.getX(), xyz.getZ());
|
||||
}
|
||||
|
||||
public boolean remove(int posX, int posZ) {
|
||||
return super.remove(packChunk(posX, posZ));
|
||||
}
|
||||
|
||||
public boolean remove(XZAddressable xyz) {
|
||||
return remove(xyz.getX(), xyz.getZ());
|
||||
}
|
||||
|
||||
public boolean add(int posX, int posZ) {
|
||||
return super.add(packChunk(posX, posZ));
|
||||
}
|
||||
|
||||
public boolean add(XZAddressable xyz) {
|
||||
return add(xyz.getX(), xyz.getZ());
|
||||
}
|
||||
|
||||
public void forEach(Consumer2D consumer) {
|
||||
for (var e : this.fastEntryIterable()) {
|
||||
consumer.accept(e.getX(), e.getZ());
|
||||
}
|
||||
}
|
||||
|
||||
public Iterator<XZAddressable> slowIterator() {
|
||||
LongIterator iter = super.iterator();
|
||||
|
||||
return new Iterator<>() {
|
||||
|
||||
@Contract(pure = true)
|
||||
@Override
|
||||
public boolean hasNext() {
|
||||
return iter.hasNext();
|
||||
}
|
||||
|
||||
@Contract(mutates = "this")
|
||||
@Override
|
||||
public XZAddressable next() {
|
||||
return MutableXZ.unpack(iter.nextLong());
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
public Stream<XZAddressable> slowStream() {
|
||||
return StreamSupport.stream(
|
||||
Spliterators.spliterator(
|
||||
this.slowIterator(),
|
||||
size(),
|
||||
Spliterator.SIZED | Spliterator.NONNULL | Spliterator.DISTINCT),
|
||||
false);
|
||||
}
|
||||
|
||||
/// The returned iterator always yields the same mutated {@link XZAddressable} instance; do not retain the
|
||||
/// result of {@link Iterator#next()} past the following call.
|
||||
public Iterator<XZAddressable> fastIterator() {
|
||||
LongIterator iter = super.iterator();
|
||||
|
||||
MutableXZ pos = new MutableXZ();
|
||||
|
||||
return new Iterator<>() {
|
||||
|
||||
@Contract(pure = true)
|
||||
@Override
|
||||
public boolean hasNext() {
|
||||
return iter.hasNext();
|
||||
}
|
||||
|
||||
@Contract(mutates = "this")
|
||||
@Override
|
||||
public XZAddressable next() {
|
||||
long l = iter.nextLong();
|
||||
|
||||
pos.x = CoordinatePacker2D.unpackChunkX(l);
|
||||
pos.z = CoordinatePacker2D.unpackChunkZ(l);
|
||||
|
||||
return pos;
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
public Iterable<XZAddressable> fastEntryIterable() {
|
||||
return this::fastIterator;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,114 @@
|
||||
package com.gtnewhorizon.gtnhlib.datastructs.space;
|
||||
|
||||
import static com.gtnewhorizon.gtnhlib.util.CoordinatePacker.pack;
|
||||
|
||||
import java.util.Iterator;
|
||||
import java.util.Spliterator;
|
||||
import java.util.Spliterators;
|
||||
import java.util.stream.Stream;
|
||||
import java.util.stream.StreamSupport;
|
||||
|
||||
import org.jetbrains.annotations.Contract;
|
||||
|
||||
import com.gtnewhorizon.gtnhlib.functional.Consumer3D;
|
||||
import com.gtnewhorizon.gtnhlib.space.MutableXYZ;
|
||||
import com.gtnewhorizon.gtnhlib.space.XYZAddressable;
|
||||
import com.gtnewhorizon.gtnhlib.util.CoordinatePacker;
|
||||
|
||||
import it.unimi.dsi.fastutil.longs.LongIterator;
|
||||
import it.unimi.dsi.fastutil.longs.LongOpenHashSet;
|
||||
|
||||
@SuppressWarnings("unused")
|
||||
public class HashSet3D extends LongOpenHashSet {
|
||||
|
||||
public boolean contains(int posX, int posY, int posZ) {
|
||||
return super.contains(pack(posX, posY, posZ));
|
||||
}
|
||||
|
||||
public boolean contains(XYZAddressable xyz) {
|
||||
return contains(xyz.getX(), xyz.getY(), xyz.getZ());
|
||||
}
|
||||
|
||||
public boolean remove(int posX, int posY, int posZ) {
|
||||
return super.remove(pack(posX, posY, posZ));
|
||||
}
|
||||
|
||||
public boolean remove(XYZAddressable xyz) {
|
||||
return remove(xyz.getX(), xyz.getY(), xyz.getZ());
|
||||
}
|
||||
|
||||
public boolean add(int posX, int posY, int posZ) {
|
||||
return super.add(pack(posX, posY, posZ));
|
||||
}
|
||||
|
||||
public boolean add(XYZAddressable xyz) {
|
||||
return add(xyz.getX(), xyz.getY(), xyz.getZ());
|
||||
}
|
||||
|
||||
public void forEach(Consumer3D consumer) {
|
||||
for (var e : this.fastEntryIterable()) {
|
||||
consumer.accept(e.getX(), e.getY(), e.getZ());
|
||||
}
|
||||
}
|
||||
|
||||
public Iterator<XYZAddressable> slowIterator() {
|
||||
LongIterator iter = super.iterator();
|
||||
|
||||
return new Iterator<>() {
|
||||
|
||||
@Contract(pure = true)
|
||||
@Override
|
||||
public boolean hasNext() {
|
||||
return iter.hasNext();
|
||||
}
|
||||
|
||||
@Contract(mutates = "this")
|
||||
@Override
|
||||
public XYZAddressable next() {
|
||||
return MutableXYZ.unpack(iter.nextLong());
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
public Stream<XYZAddressable> slowStream() {
|
||||
return StreamSupport.stream(
|
||||
Spliterators.spliterator(
|
||||
this.slowIterator(),
|
||||
size(),
|
||||
Spliterator.SIZED | Spliterator.NONNULL | Spliterator.DISTINCT),
|
||||
false);
|
||||
}
|
||||
|
||||
/// The returned iterator always yields the same mutated {@link XYZAddressable} instance; do not retain the
|
||||
/// result of {@link Iterator#next()} past the following call.
|
||||
public Iterator<XYZAddressable> fastIterator() {
|
||||
LongIterator iter = super.iterator();
|
||||
|
||||
MutableXYZ pos = new MutableXYZ();
|
||||
|
||||
return new Iterator<>() {
|
||||
|
||||
@Contract(pure = true)
|
||||
@Override
|
||||
public boolean hasNext() {
|
||||
return iter.hasNext();
|
||||
}
|
||||
|
||||
@Contract(mutates = "this")
|
||||
@Override
|
||||
public XYZAddressable next() {
|
||||
long l = iter.nextLong();
|
||||
|
||||
pos.x = CoordinatePacker.unpackX(l);
|
||||
pos.y = CoordinatePacker.unpackY(l);
|
||||
pos.z = CoordinatePacker.unpackZ(l);
|
||||
|
||||
return pos;
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
public Iterable<XYZAddressable> fastEntryIterable() {
|
||||
return this::fastIterator;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
package com.gtnewhorizon.gtnhlib.functional;
|
||||
|
||||
@FunctionalInterface
|
||||
public interface Compute2D<V> {
|
||||
|
||||
V apply(int posX, int posZ);
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
package com.gtnewhorizon.gtnhlib.functional;
|
||||
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
@FunctionalInterface
|
||||
public interface Compute2DWithValue<V> {
|
||||
|
||||
/// @param currentValue the value currently mapped at (posX, posZ), or null if absent
|
||||
/// @return the new value to map, or null to remove the mapping
|
||||
@Nullable
|
||||
V apply(int posX, int posZ, @Nullable V currentValue);
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
package com.gtnewhorizon.gtnhlib.functional;
|
||||
|
||||
@FunctionalInterface
|
||||
public interface Compute3D<V> {
|
||||
|
||||
V apply(int posX, int posY, int posZ);
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
package com.gtnewhorizon.gtnhlib.functional;
|
||||
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
@FunctionalInterface
|
||||
public interface Compute3DWithValue<V> {
|
||||
|
||||
/// @param currentValue the value currently mapped at (posX, posY, posZ), or null if absent
|
||||
/// @return the new value to map, or null to remove the mapping
|
||||
@Nullable
|
||||
V apply(int posX, int posY, int posZ, @Nullable V currentValue);
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
package com.gtnewhorizon.gtnhlib.functional;
|
||||
|
||||
@FunctionalInterface
|
||||
public interface Consumer2D {
|
||||
|
||||
void accept(int posX, int posZ);
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
package com.gtnewhorizon.gtnhlib.functional;
|
||||
|
||||
@FunctionalInterface
|
||||
public interface Consumer2DWithValue<T> {
|
||||
|
||||
void accept(int posX, int posZ, T value);
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
package com.gtnewhorizon.gtnhlib.functional;
|
||||
|
||||
@FunctionalInterface
|
||||
public interface Consumer3D {
|
||||
|
||||
void accept(int posX, int posY, int posZ);
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
package com.gtnewhorizon.gtnhlib.functional;
|
||||
|
||||
@FunctionalInterface
|
||||
public interface Consumer3DWithValue<T> {
|
||||
|
||||
void accept(int posX, int posY, int posZ, T value);
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
package com.gtnewhorizon.gtnhlib.space;
|
||||
|
||||
import static com.gtnewhorizon.gtnhlib.util.CoordinatePacker.unpackX;
|
||||
import static com.gtnewhorizon.gtnhlib.util.CoordinatePacker.unpackY;
|
||||
import static com.gtnewhorizon.gtnhlib.util.CoordinatePacker.unpackZ;
|
||||
|
||||
import com.github.bsideup.jabel.Desugar;
|
||||
import com.gtnewhorizon.gtnhlib.hash.Fnv1a32;
|
||||
|
||||
@Desugar
|
||||
public record ImmutableXYZ(int x, int y, int z) implements XYZAddressable {
|
||||
|
||||
public static ImmutableXYZ unpack(long coord) {
|
||||
return new ImmutableXYZ(unpackX(coord), unpackY(coord), unpackZ(coord));
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getX() {
|
||||
return x;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getY() {
|
||||
return y;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getZ() {
|
||||
return z;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Fnv1a32.hashStep(Fnv1a32.hashStep(Fnv1a32.hashStep(Fnv1a32.initialState(), x), y), z);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
package com.gtnewhorizon.gtnhlib.space;
|
||||
|
||||
import com.github.bsideup.jabel.Desugar;
|
||||
import com.gtnewhorizon.gtnhlib.hash.Fnv1a32;
|
||||
import com.gtnewhorizon.gtnhlib.util.CoordinatePacker2D;
|
||||
|
||||
@Desugar
|
||||
public record ImmutableXZ(int x, int z) implements XZAddressable {
|
||||
|
||||
public static ImmutableXZ unpack(long coord) {
|
||||
return new ImmutableXZ(CoordinatePacker2D.unpackChunkX(coord), CoordinatePacker2D.unpackChunkZ(coord));
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getX() {
|
||||
return x;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getZ() {
|
||||
return z;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Fnv1a32.hashStep(Fnv1a32.hashStep(Fnv1a32.initialState(), x), z);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,56 @@
|
||||
package com.gtnewhorizon.gtnhlib.space;
|
||||
|
||||
import static com.gtnewhorizon.gtnhlib.util.CoordinatePacker.unpackX;
|
||||
import static com.gtnewhorizon.gtnhlib.util.CoordinatePacker.unpackY;
|
||||
import static com.gtnewhorizon.gtnhlib.util.CoordinatePacker.unpackZ;
|
||||
|
||||
import com.gtnewhorizon.gtnhlib.hash.Fnv1a32;
|
||||
|
||||
public class MutableXYZ implements XYZAddressable {
|
||||
|
||||
public int x, y, z;
|
||||
|
||||
public MutableXYZ() {}
|
||||
|
||||
public MutableXYZ(int x, int y, int z) {
|
||||
this.x = x;
|
||||
this.y = y;
|
||||
this.z = z;
|
||||
}
|
||||
|
||||
public static MutableXYZ unpack(long coord) {
|
||||
return new MutableXYZ(unpackX(coord), unpackY(coord), unpackZ(coord));
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getX() {
|
||||
return x;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getY() {
|
||||
return y;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getZ() {
|
||||
return z;
|
||||
}
|
||||
|
||||
@Override
|
||||
public final boolean equals(Object o) {
|
||||
if (!(o instanceof MutableXYZ that)) return false;
|
||||
|
||||
return x == that.x && y == that.y && z == that.z;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Fnv1a32.hashStep(Fnv1a32.hashStep(Fnv1a32.hashStep(Fnv1a32.initialState(), x), y), z);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "MutableXYZ{" + "x=" + x + ", y=" + y + ", z=" + z + '}';
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
package com.gtnewhorizon.gtnhlib.space;
|
||||
|
||||
import com.gtnewhorizon.gtnhlib.hash.Fnv1a32;
|
||||
import com.gtnewhorizon.gtnhlib.util.CoordinatePacker2D;
|
||||
|
||||
public class MutableXZ implements XZAddressable {
|
||||
|
||||
public int x, z;
|
||||
|
||||
public MutableXZ() {}
|
||||
|
||||
public MutableXZ(int x, int z) {
|
||||
this.x = x;
|
||||
this.z = z;
|
||||
}
|
||||
|
||||
public static MutableXZ unpack(long coord) {
|
||||
return new MutableXZ(CoordinatePacker2D.unpackChunkX(coord), CoordinatePacker2D.unpackChunkZ(coord));
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getX() {
|
||||
return x;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getZ() {
|
||||
return z;
|
||||
}
|
||||
|
||||
@Override
|
||||
public final boolean equals(Object o) {
|
||||
if (!(o instanceof MutableXZ mutableXZ)) return false;
|
||||
|
||||
return x == mutableXZ.x && z == mutableXZ.z;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Fnv1a32.hashStep(Fnv1a32.hashStep(Fnv1a32.initialState(), x), z);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "MutableXZ{" + "x=" + x + ", z=" + z + '}';
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
package com.gtnewhorizon.gtnhlib.space;
|
||||
|
||||
/// Something that exists at an X,Y,Z location.
|
||||
public interface XYZAddressable {
|
||||
|
||||
int getX();
|
||||
|
||||
int getY();
|
||||
|
||||
int getZ();
|
||||
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
package com.gtnewhorizon.gtnhlib.space;
|
||||
|
||||
/// Something that exists at an X,Z location.
|
||||
public interface XZAddressable {
|
||||
|
||||
int getX();
|
||||
|
||||
int getZ();
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
package com.gtnewhorizon.gtnhlib.util;
|
||||
|
||||
public class CoordinatePacker2D {
|
||||
|
||||
private static final long XZ_INT = 0xFFFFFFFFL;
|
||||
|
||||
public static long packChunk(int x, int z) {
|
||||
return (z & XZ_INT) << 32 | (x & XZ_INT);
|
||||
}
|
||||
|
||||
public static int unpackChunkX(long key) {
|
||||
return (int) key;
|
||||
}
|
||||
|
||||
public static int unpackChunkZ(long key) {
|
||||
return (int) (key >> 32);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user