| 1 | package net.minecraft.util;
|
| 2 |
|
| 3 | import it.unimi.dsi.fastutil.objects.ObjectArrays;
|
| 4 | import java.util.AbstractSet;
|
| 5 | import java.util.Arrays;
|
| 6 | import java.util.Comparator;
|
| 7 | import java.util.Iterator;
|
| 8 | import java.util.NoSuchElementException;
|
| 9 | import org.jspecify.annotations.Nullable;
|
| 10 |
|
| 11 | public class SortedArraySet<T> extends AbstractSet<T> {
|
| 12 | private static final int DEFAULT_INITIAL_CAPACITY = 10;
|
| 13 | private final Comparator<T> comparator;
|
| 14 | private T[] contents;
|
| 15 | private int size;
|
| 16 |
|
| 17 | private SortedArraySet(final int initialCapacity, final Comparator<T> comparator) {
|
| 18 | this.comparator = comparator;
|
| 19 | if (initialCapacity < 0) {
|
| 20 | throw new IllegalArgumentException("Initial capacity (" + initialCapacity + ") is negative");
|
| 21 | }
|
| 22 |
|
| 23 | this.contents = (T[])castRawArray(new Object[initialCapacity]);
|
| 24 | }
|
| 25 |
|
| 26 | public static <T extends Comparable<T>> SortedArraySet<T> create() {
|
| 27 | return create(10);
|
| 28 | }
|
| 29 |
|
| 30 | public static <T extends Comparable<T>> SortedArraySet<T> create(final int initialCapacity) {
|
| 31 | return new SortedArraySet<>(initialCapacity, Comparator.naturalOrder());
|
| 32 | }
|
| 33 |
|
| 34 | public static <T> SortedArraySet<T> create(final Comparator<T> comparator) {
|
| 35 | return create(comparator, 10);
|
| 36 | }
|
| 37 |
|
| 38 | public static <T> SortedArraySet<T> create(final Comparator<T> comparator, final int initialCapacity) {
|
| 39 | return new SortedArraySet<>(initialCapacity, comparator);
|
| 40 | }
|
| 41 |
|
| 42 | private static <T> T[] castRawArray(final Object[] array) {
|
| 43 | return (T[])array;
|
| 44 | }
|
| 45 |
|
| 46 | private int findIndex(final T t) {
|
| 47 | return Arrays.binarySearch(this.contents, 0, this.size, t, this.comparator);
|
| 48 | }
|
| 49 |
|
| 50 | private static int getInsertionPosition(final int position) {
|
| 51 | return -position - 1;
|
| 52 | }
|
| 53 |
|
| 54 | @Override
|
| 55 | public boolean add(final T t) {
|
| 56 | int position = this.findIndex(t);
|
| 57 | if (position >= 0) {
|
| 58 | return false;
|
| 59 | }
|
| 60 |
|
| 61 | int pos = getInsertionPosition(position);
|
| 62 | this.addInternal(t, pos);
|
| 63 | return true;
|
| 64 | }
|
| 65 |
|
| 66 | private void grow(int capacity) {
|
| 67 | if (capacity > this.contents.length) {
|
| 68 | if (this.contents != ObjectArrays.DEFAULT_EMPTY_ARRAY) {
|
| 69 | capacity = Util.growByHalf(this.contents.length, capacity);
|
| 70 | } else if (capacity < 10) {
|
| 71 | capacity = 10;
|
| 72 | }
|
| 73 |
|
| 74 | Object[] t = new Object[capacity];
|
| 75 | System.arraycopy(this.contents, 0, t, 0, this.size);
|
| 76 | this.contents = (T[])castRawArray(t);
|
| 77 | }
|
| 78 | }
|
| 79 |
|
| 80 | private void addInternal(final T t, final int pos) {
|
| 81 | this.grow(this.size + 1);
|
| 82 | if (pos != this.size) {
|
| 83 | System.arraycopy(this.contents, pos, this.contents, pos + 1, this.size - pos);
|
| 84 | }
|
| 85 |
|
| 86 | this.contents[pos] = t;
|
| 87 | this.size++;
|
| 88 | }
|
| 89 |
|
| 90 | private void removeInternal(final int position) {
|
| 91 | this.size--;
|
| 92 | if (position != this.size) {
|
| 93 | System.arraycopy(this.contents, position + 1, this.contents, position, this.size - position);
|
| 94 | }
|
| 95 |
|
| 96 | this.contents[this.size] = null;
|
| 97 | }
|
| 98 |
|
| 99 | private T getInternal(final int position) {
|
| 100 | return this.contents[position];
|
| 101 | }
|
| 102 |
|
| 103 | public T addOrGet(final T t) {
|
| 104 | int position = this.findIndex(t);
|
| 105 | if (position >= 0) {
|
| 106 | return this.getInternal(position);
|
| 107 | }
|
| 108 |
|
| 109 | this.addInternal(t, getInsertionPosition(position));
|
| 110 | return t;
|
| 111 | }
|
| 112 |
|
| 113 | @Override
|
| 114 | public boolean remove(final Object o) {
|
| 115 | int position = this.findIndex((T)o);
|
| 116 | if (position >= 0) {
|
| 117 | this.removeInternal(position);
|
| 118 | return true;
|
| 119 | } else {
|
| 120 | return false;
|
| 121 | }
|
| 122 | }
|
| 123 |
|
| 124 | public @Nullable T get(final T t) {
|
| 125 | int position = this.findIndex(t);
|
| 126 | return position >= 0 ? this.getInternal(position) : null;
|
| 127 | }
|
| 128 |
|
| 129 | public T first() {
|
| 130 | return this.getInternal(0);
|
| 131 | }
|
| 132 |
|
| 133 | public T last() {
|
| 134 | return this.getInternal(this.size - 1);
|
| 135 | }
|
| 136 |
|
| 137 | @Override
|
| 138 | public boolean contains(final Object o) {
|
| 139 | int result = this.findIndex((T)o);
|
| 140 | return result >= 0;
|
| 141 | }
|
| 142 |
|
| 143 | @Override
|
| 144 | public Iterator<T> iterator() {
|
| 145 | return new SortedArraySet.ArrayIterator();
|
| 146 | }
|
| 147 |
|
| 148 | @Override
|
| 149 | public int size() {
|
| 150 | return this.size;
|
| 151 | }
|
| 152 |
|
| 153 | @Override
|
| 154 | public Object[] toArray() {
|
| 155 | return Arrays.copyOf(this.contents, this.size, Object[].class);
|
| 156 | }
|
| 157 |
|
| 158 | @Override
|
| 159 | public <U> U[] toArray(final U[] a) {
|
| 160 | if (a.length < this.size) {
|
| 161 | return (U[])Arrays.copyOf(this.contents, this.size, (Class<? extends T[]>)a.getClass());
|
| 162 | }
|
| 163 |
|
| 164 | System.arraycopy(this.contents, 0, a, 0, this.size);
|
| 165 | if (a.length > this.size) {
|
| 166 | a[this.size] = null;
|
| 167 | }
|
| 168 |
|
| 169 | return a;
|
| 170 | }
|
| 171 |
|
| 172 | @Override
|
| 173 | public void clear() {
|
| 174 | Arrays.fill(this.contents, 0, this.size, null);
|
| 175 | this.size = 0;
|
| 176 | }
|
| 177 |
|
| 178 | @Override
|
| 179 | public boolean equals(final Object o) {
|
| 180 | if (this == o) {
|
| 181 | return true;
|
| 182 | } else {
|
| 183 | return o instanceof SortedArraySet<?> that && this.comparator.equals(that.comparator)
|
| 184 | ? this.size == that.size && Arrays.equals(this.contents, that.contents)
|
| 185 | : super.equals(o);
|
| 186 | }
|
| 187 | }
|
| 188 |
|
| 189 | private class ArrayIterator implements Iterator<T> {
|
| 190 | private int index;
|
| 191 | private int last = -1;
|
| 192 |
|
| 193 | @Override
|
| 194 | public boolean hasNext() {
|
| 195 | return this.index < SortedArraySet.this.size;
|
| 196 | }
|
| 197 |
|
| 198 | @Override
|
| 199 | public T next() {
|
| 200 | if (this.index >= SortedArraySet.this.size) {
|
| 201 | throw new NoSuchElementException();
|
| 202 | }
|
| 203 |
|
| 204 | this.last = this.index++;
|
| 205 | return SortedArraySet.this.contents[this.last];
|
| 206 | }
|
| 207 |
|
| 208 | @Override
|
| 209 | public void remove() {
|
| 210 | if (this.last == -1) {
|
| 211 | throw new IllegalStateException();
|
| 212 | }
|
| 213 |
|
| 214 | SortedArraySet.this.removeInternal(this.last);
|
| 215 | this.index--;
|
| 216 | this.last = -1;
|
| 217 | }
|
| 218 | }
|
| 219 | }
|
| 220 |
|