mirror of
https://github.com/Samsuik/Sakura.git
synced 2025-12-21 07:49:29 +00:00
118 lines
4.2 KiB
Diff
118 lines
4.2 KiB
Diff
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
|
|
From: Samsuik <kfian294ma4@gmail.com>
|
|
Date: Thu, 14 Oct 2021 19:16:49 +0100
|
|
Subject: [PATCH] Copy EntityList methods to BasicEntityList
|
|
|
|
|
|
diff --git a/src/main/java/io/papermc/paper/world/ChunkEntitySlices.java b/src/main/java/io/papermc/paper/world/ChunkEntitySlices.java
|
|
index 7e8dc9e8f381abfdcce2746edc93122d623622d1..2d79633d86007c7d40eecf5f9271fa3f351b72b5 100644
|
|
--- a/src/main/java/io/papermc/paper/world/ChunkEntitySlices.java
|
|
+++ b/src/main/java/io/papermc/paper/world/ChunkEntitySlices.java
|
|
@@ -25,6 +25,8 @@ import java.util.Iterator;
|
|
import java.util.List;
|
|
import java.util.function.Predicate;
|
|
|
|
+import it.unimi.dsi.fastutil.ints.Int2IntOpenHashMap; // Sakura
|
|
+
|
|
public final class ChunkEntitySlices {
|
|
|
|
protected final int minSection;
|
|
@@ -303,6 +305,13 @@ public final class ChunkEntitySlices {
|
|
|
|
protected static final class BasicEntityList<E extends Entity> {
|
|
|
|
+ // Sakura start - copy entitylist methods across
|
|
+ protected final Int2IntOpenHashMap entityToIndex = new Int2IntOpenHashMap(2, 0.8f);
|
|
+ {
|
|
+ this.entityToIndex.defaultReturnValue(Integer.MIN_VALUE);
|
|
+ }
|
|
+ // Sakura end
|
|
+
|
|
protected static final Entity[] EMPTY = new Entity[0];
|
|
protected static final int DEFAULT_CAPACITY = 4;
|
|
|
|
@@ -325,55 +334,52 @@ public final class ChunkEntitySlices {
|
|
return this.size;
|
|
}
|
|
|
|
- private void resize() {
|
|
- if (this.storage == EMPTY) {
|
|
- this.storage = (E[])new Entity[DEFAULT_CAPACITY];
|
|
- } else {
|
|
- this.storage = Arrays.copyOf(this.storage, this.storage.length * 2);
|
|
- }
|
|
- }
|
|
-
|
|
+ // Sakura start - copy entitylist methods across
|
|
public void add(final E entity) {
|
|
- final int idx = this.size++;
|
|
- if (idx >= this.storage.length) {
|
|
- this.resize();
|
|
- this.storage[idx] = entity;
|
|
- } else {
|
|
- this.storage[idx] = entity;
|
|
+ final int count = this.size;
|
|
+ final int currIndex = this.entityToIndex.putIfAbsent(entity.getId(), count);
|
|
+
|
|
+ if (currIndex != Integer.MIN_VALUE) {
|
|
+ return; // already in this list
|
|
}
|
|
- }
|
|
|
|
- public int indexOf(final E entity) {
|
|
- final E[] storage = this.storage;
|
|
+ E[] list = this.storage;
|
|
|
|
- for (int i = 0, len = Math.min(this.storage.length, this.size); i < len; ++i) {
|
|
- if (storage[i] == entity) {
|
|
- return i;
|
|
- }
|
|
+ if (list.length == count) {
|
|
+ // resize required
|
|
+ list = this.storage = Arrays.copyOf(list, (int)Math.max(4L, count * 2L)); // overflow results in negative
|
|
}
|
|
|
|
- return -1;
|
|
+ list[count] = entity;
|
|
+ this.size = count + 1;
|
|
+ }
|
|
+
|
|
+ public int indexOf(final E entity) {
|
|
+ return this.entityToIndex.getOrDefault(entity.getId(), -1);
|
|
}
|
|
|
|
public boolean remove(final E entity) {
|
|
- final int idx = this.indexOf(entity);
|
|
- if (idx == -1) {
|
|
+ final int index = this.entityToIndex.remove(entity.getId());
|
|
+ if (index == Integer.MIN_VALUE) {
|
|
return false;
|
|
}
|
|
|
|
- final int size = --this.size;
|
|
- final E[] storage = this.storage;
|
|
- if (idx != size) {
|
|
- System.arraycopy(storage, idx + 1, storage, idx, size - idx);
|
|
+ // move the entity at the end to this index
|
|
+ final int endIndex = --this.size;
|
|
+ final E end = this.storage[endIndex];
|
|
+ if (index != endIndex) {
|
|
+ // not empty after this call
|
|
+ this.entityToIndex.put(end.getId(), index); // update index
|
|
}
|
|
-
|
|
- storage[size] = null;
|
|
+ this.storage[index] = end;
|
|
+ this.storage[endIndex] = null;
|
|
|
|
return true;
|
|
}
|
|
|
|
public boolean has(final E entity) {
|
|
- return this.indexOf(entity) != -1;
|
|
+ return this.entityToIndex.containsKey(entity.getId());
|
|
+ // Sakura end
|
|
}
|
|
}
|
|
|