From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 From: NONPLAYT <76615486+NONPLAYT@users.noreply.github.com> Date: Wed, 9 Jul 2025 03:37:51 +0300 Subject: [PATCH] Rewrite ReferenceList diff --git a/src/main/java/ca/spottedleaf/moonrise/common/list/ReferenceList.java b/src/main/java/ca/spottedleaf/moonrise/common/list/ReferenceList.java index 2e876b918672e8ef3b5197b7e6b1597247fdeaa1..94232d9ab2b1b064c032d9710734f6fbcdb52e10 100644 --- a/src/main/java/ca/spottedleaf/moonrise/common/list/ReferenceList.java +++ b/src/main/java/ca/spottedleaf/moonrise/common/list/ReferenceList.java @@ -1,142 +1,26 @@ package ca.spottedleaf.moonrise.common.list; -import it.unimi.dsi.fastutil.objects.Reference2IntOpenHashMap; -import java.util.Arrays; -import java.util.Iterator; -import java.util.NoSuchElementException; +// DivineMC start - Rewrite ReferenceList +import it.unimi.dsi.fastutil.objects.ReferenceArrayList; +import it.unimi.dsi.fastutil.objects.ReferenceLists; -public final class ReferenceList implements Iterable { - - private static final Object[] EMPTY_LIST = new Object[0]; - - private final Reference2IntOpenHashMap referenceToIndex; - private E[] references; - private int count; - - public ReferenceList() { - this((E[])EMPTY_LIST); - } - - public ReferenceList(final E[] referenceArray) { - this.references = referenceArray; - this.referenceToIndex = new Reference2IntOpenHashMap<>(2, 0.8f); - this.referenceToIndex.defaultReturnValue(Integer.MIN_VALUE); - } - - private ReferenceList(final E[] references, final int count, final Reference2IntOpenHashMap referenceToIndex) { - this.references = references; - this.count = count; - this.referenceToIndex = referenceToIndex; - } - - public ReferenceList copy() { - return new ReferenceList<>(this.references.clone(), this.count, this.referenceToIndex.clone()); - } - - public int size() { - return this.count; - } - - public boolean contains(final E obj) { - return this.referenceToIndex.containsKey(obj); +public class ReferenceList extends ReferenceLists.SynchronizedList { + public ReferenceList(E[] elements) { + super(new RefListInner<>(elements)); } - public boolean remove(final E obj) { - final int index = this.referenceToIndex.removeInt(obj); - if (index == Integer.MIN_VALUE) { - return false; - } - - // move the object at the end to this index - final int endIndex = --this.count; - final E end = (E)this.references[endIndex]; - if (index != endIndex) { - // not empty after this call - this.referenceToIndex.put(end, index); // update index - } - this.references[index] = end; - this.references[endIndex] = null; - - return true; + public synchronized E[] getRawDataUnchecked() { + return ((RefListInner) this.list).getRawDataUnchecked(); } - public boolean add(final E obj) { - final int count = this.count; - final int currIndex = this.referenceToIndex.putIfAbsent(obj, count); - - if (currIndex != Integer.MIN_VALUE) { - return false; // already in this list + public static class RefListInner extends ReferenceArrayList { + public RefListInner(A[] elements) { + super(elements, true); } - E[] list = this.references; - - if (list.length == count) { - // resize required - list = this.references = Arrays.copyOf(list, (int)Math.max(4L, count * 2L)); // overflow results in negative + public A[] getRawDataUnchecked() { + return this.a; } - - list[count] = obj; - this.count = count + 1; - - return true; - } - - public E getChecked(final int index) { - if (index < 0 || index >= this.count) { - throw new IndexOutOfBoundsException("Index: " + index + " is out of bounds, size: " + this.count); - } - return this.references[index]; - } - - public E getUnchecked(final int index) { - return this.references[index]; - } - - public Object[] getRawData() { - return this.references; - } - - public E[] getRawDataUnchecked() { - return this.references; - } - - public void clear() { - this.referenceToIndex.clear(); - Arrays.fill(this.references, 0, this.count, null); - this.count = 0; - } - - @Override - public Iterator iterator() { - return new Iterator<>() { - private E lastRet; - private int current; - - @Override - public boolean hasNext() { - return this.current < ReferenceList.this.count; - } - - @Override - public E next() { - if (this.current >= ReferenceList.this.count) { - throw new NoSuchElementException(); - } - return this.lastRet = ReferenceList.this.references[this.current++]; - } - - @Override - public void remove() { - final E lastRet = this.lastRet; - - if (lastRet == null) { - throw new IllegalStateException(); - } - this.lastRet = null; - - ReferenceList.this.remove(lastRet); - --this.current; - } - }; } } +// DivineMC end - Rewrite ReferenceList