22
33import com .ishland .vmp .common .util .SimpleObjectPool ;
44import io .papermc .paper .util .MCUtil ;
5- import it .unimi .dsi .fastutil .Hash ;
65import it .unimi .dsi .fastutil .longs .Long2ObjectFunction ;
76import it .unimi .dsi .fastutil .longs .Long2ObjectMap ;
87import it .unimi .dsi .fastutil .longs .Long2ObjectOpenHashMap ;
98import it .unimi .dsi .fastutil .longs .LongArrayList ;
109import it .unimi .dsi .fastutil .longs .LongComparators ;
1110import it .unimi .dsi .fastutil .longs .LongIterator ;
12- import it .unimi .dsi .fastutil .objects .Object2IntOpenCustomHashMap ;
13- import it .unimi .dsi .fastutil .objects .Object2LongOpenCustomHashMap ;
14- import it .unimi .dsi .fastutil .objects .ObjectLinkedOpenCustomHashSet ;
1511import it .unimi .dsi .fastutil .objects .Reference2IntOpenHashMap ;
1612import it .unimi .dsi .fastutil .objects .Reference2LongOpenHashMap ;
1713import it .unimi .dsi .fastutil .objects .ReferenceLinkedOpenHashSet ;
@@ -24,7 +20,15 @@ public class AreaMap<T> {
2420
2521 private static final Object [] EMPTY = new Object [0 ];
2622
27- private final SimpleObjectPool <RawObjectLinkedOpenIdentityHashSet <T >> pooledHashSets = new SimpleObjectPool <>(unused -> new RawObjectLinkedOpenIdentityHashSet <>(), RawObjectLinkedOpenIdentityHashSet ::clear , 8192 );
23+ private final SimpleObjectPool <RawObjectLinkedOpenIdentityHashSet <T >> pooledHashSets =
24+ new SimpleObjectPool <>(unused -> new RawObjectLinkedOpenIdentityHashSet <>(),
25+ ReferenceLinkedOpenHashSet ::clear ,
26+ ts -> {
27+ ts .clear ();
28+ ts .trim (4 );
29+ },
30+ 8192
31+ );
2832 private final Long2ObjectFunction <RawObjectLinkedOpenIdentityHashSet <T >> allocHashSet = unused -> pooledHashSets .alloc ();
2933 private final Long2ObjectOpenHashMap <RawObjectLinkedOpenIdentityHashSet <T >> map = new Long2ObjectOpenHashMap <>();
3034 private final Reference2IntOpenHashMap <T > viewDistances = new Reference2IntOpenHashMap <>();
@@ -69,12 +73,7 @@ public void add(T object, int x, int z, int rawViewDistance) {
6973 set .add (MCUtil .getCoordinateKey (xx , zz ));
7074 }
7175 }
72- set .sort (LongComparators .asLongComparator (Comparator .comparingLong (l -> chebyshevDistance (x , z , MCUtil .getCoordinateX (l ), MCUtil .getCoordinateZ (l )))));
73- final LongIterator iterator = set .iterator ();
74- while (iterator .hasNext ()) {
75- final long pos = iterator .nextLong ();
76- addListener .accept (object , MCUtil .getCoordinateX (pos ), MCUtil .getCoordinateZ (pos ));
77- }
76+ notifyListenersSorted (object , x , z , addListener , set );
7877 } else {
7978 for (int xx = x - viewDistance ; xx <= x + viewDistance ; xx ++) {
8079 for (int zz = z - viewDistance ; zz <= z + viewDistance ; zz ++) {
@@ -103,12 +102,7 @@ public void remove(T object) {
103102 set .add (MCUtil .getCoordinateKey (xx , zz ));
104103 }
105104 }
106- set .sort (LongComparators .asLongComparator (Comparator .comparingLong (l -> chebyshevDistance (x , z , MCUtil .getCoordinateX (l ), MCUtil .getCoordinateZ (l )))));
107- final LongIterator iterator = set .iterator ();
108- while (iterator .hasNext ()) {
109- final long pos = iterator .nextLong ();
110- removeListener .accept (object , MCUtil .getCoordinateX (pos ), MCUtil .getCoordinateZ (pos ));
111- }
105+ notifyListenersSorted (object , x , z , removeListener , set );
112106 } else {
113107 for (int xx = x - viewDistance ; xx <= x + viewDistance ; xx ++) {
114108 for (int zz = z - viewDistance ; zz <= z + viewDistance ; zz ++) {
@@ -145,23 +139,18 @@ private void updateAdds(T object, int oldX, int oldZ, int oldViewDistance, int n
145139 if (this .sortListenerCalls && addListener != null ) {
146140 final int length = 2 * newViewDistance + 1 ;
147141 LongArrayList set = new LongArrayList (length * length );
148- for (int xx = newX - newViewDistance ; xx <= newX + newViewDistance ; xx ++) {
149- for (int zz = newZ - newViewDistance ; zz <= newZ + newViewDistance ; zz ++) {
142+ for (int xx = newX - newViewDistance ; xx <= newX + newViewDistance ; xx ++) {
143+ for (int zz = newZ - newViewDistance ; zz <= newZ + newViewDistance ; zz ++) {
150144 if (!isInRange (xLower , xHigher , zLower , zHigher , xx , zz )) {
151145 add0 (xx , zz , object , false );
152146 set .add (MCUtil .getCoordinateKey (xx , zz ));
153147 }
154148 }
155149 }
156- set .sort (LongComparators .asLongComparator (Comparator .comparingLong (l -> chebyshevDistance (newX , newZ , MCUtil .getCoordinateX (l ), MCUtil .getCoordinateZ (l )))));
157- final LongIterator iterator = set .iterator ();
158- while (iterator .hasNext ()) {
159- final long pos = iterator .nextLong ();
160- addListener .accept (object , MCUtil .getCoordinateX (pos ), MCUtil .getCoordinateZ (pos ));
161- }
150+ notifyListenersSorted (object , newX , newZ , addListener , set );
162151 } else {
163- for (int xx = newX - newViewDistance ; xx <= newX + newViewDistance ; xx ++) {
164- for (int zz = newZ - newViewDistance ; zz <= newZ + newViewDistance ; zz ++) {
152+ for (int xx = newX - newViewDistance ; xx <= newX + newViewDistance ; xx ++) {
153+ for (int zz = newZ - newViewDistance ; zz <= newZ + newViewDistance ; zz ++) {
165154 if (!isInRange (xLower , xHigher , zLower , zHigher , xx , zz )) {
166155 add0 (xx , zz , object , true );
167156 }
@@ -180,23 +169,18 @@ private void updateRemovals(T object, int oldX, int oldZ, int oldViewDistance, i
180169 if (this .sortListenerCalls && removeListener != null ) {
181170 final int length = 2 * oldViewDistance + 1 ;
182171 LongArrayList set = new LongArrayList (length * length );
183- for (int xx = oldX - oldViewDistance ; xx <= oldX + oldViewDistance ; xx ++) {
184- for (int zz = oldZ - oldViewDistance ; zz <= oldZ + oldViewDistance ; zz ++) {
172+ for (int xx = oldX - oldViewDistance ; xx <= oldX + oldViewDistance ; xx ++) {
173+ for (int zz = oldZ - oldViewDistance ; zz <= oldZ + oldViewDistance ; zz ++) {
185174 if (!isInRange (xLower , xHigher , zLower , zHigher , xx , zz )) {
186175 remove0 (xx , zz , object , false );
187176 set .add (MCUtil .getCoordinateKey (xx , zz ));
188177 }
189178 }
190179 }
191- set .sort (LongComparators .asLongComparator (Comparator .comparingLong (l -> chebyshevDistance (newX , newZ , MCUtil .getCoordinateX (l ), MCUtil .getCoordinateZ (l )))));
192- final LongIterator iterator = set .iterator ();
193- while (iterator .hasNext ()) {
194- final long pos = iterator .nextLong ();
195- removeListener .accept (object , MCUtil .getCoordinateX (pos ), MCUtil .getCoordinateZ (pos ));
196- }
180+ notifyListenersSorted (object , newX , newZ , removeListener , set );
197181 } else {
198- for (int xx = oldX - oldViewDistance ; xx <= oldX + oldViewDistance ; xx ++) {
199- for (int zz = oldZ - oldViewDistance ; zz <= oldZ + oldViewDistance ; zz ++) {
182+ for (int xx = oldX - oldViewDistance ; xx <= oldX + oldViewDistance ; xx ++) {
183+ for (int zz = oldZ - oldViewDistance ; zz <= oldZ + oldViewDistance ; zz ++) {
200184 if (!isInRange (xLower , xHigher , zLower , zHigher , xx , zz )) {
201185 remove0 (xx , zz , object , true );
202186 }
@@ -225,6 +209,15 @@ private void remove0(int xx, int zz, T object, boolean notifyListeners) {
225209 if (notifyListeners && this .removeListener != null ) this .removeListener .accept (object , xx , zz );
226210 }
227211
212+ private void notifyListenersSorted (T object , int x , int z , Listener <T > addListener , LongArrayList set ) {
213+ set .sort (LongComparators .asLongComparator (Comparator .comparingLong (l -> chebyshevDistance (x , z , MCUtil .getCoordinateX (l ), MCUtil .getCoordinateZ (l )))));
214+ final LongIterator iterator = set .iterator ();
215+ while (iterator .hasNext ()) {
216+ final long pos = iterator .nextLong ();
217+ addListener .accept (object , MCUtil .getCoordinateX (pos ), MCUtil .getCoordinateZ (pos ));
218+ }
219+ }
220+
228221 private static boolean isInRange (int xLower , int xHigher , int zLower , int zHigher , int x , int z ) {
229222 return x >= xLower && x <= xHigher && z >= zLower && z <= zHigher ;
230223 }
0 commit comments