| 1 | package net.minecraft.util;
|
| 2 |
|
| 3 | import com.google.common.annotations.VisibleForTesting;
|
| 4 | import java.util.AbstractList;
|
| 5 | import java.util.Iterator;
|
| 6 | import java.util.List;
|
| 7 | import java.util.NoSuchElementException;
|
| 8 | import java.util.Objects;
|
| 9 | import java.util.function.Consumer;
|
| 10 | import java.util.function.Predicate;
|
| 11 | import java.util.function.UnaryOperator;
|
| 12 | import org.jspecify.annotations.Nullable;
|
| 13 |
|
| 14 | public class ArrayListDeque<T> extends AbstractList<T> implements ListAndDeque<T> {
|
| 15 | private static final int MIN_GROWTH = 1;
|
| 16 | private @Nullable Object[] contents;
|
| 17 | private int head;
|
| 18 | private int size;
|
| 19 |
|
| 20 | public ArrayListDeque() {
|
| 21 | this(1);
|
| 22 | }
|
| 23 |
|
| 24 | public ArrayListDeque(final int capacity) {
|
| 25 | this.contents = new Object[capacity];
|
| 26 | this.head = 0;
|
| 27 | this.size = 0;
|
| 28 | }
|
| 29 |
|
| 30 | @Override
|
| 31 | public int size() {
|
| 32 | return this.size;
|
| 33 | }
|
| 34 |
|
| 35 | @VisibleForTesting
|
| 36 | public int capacity() {
|
| 37 | return this.contents.length;
|
| 38 | }
|
| 39 |
|
| 40 | private int getIndex(final int index) {
|
| 41 | return (index + this.head) % this.contents.length;
|
| 42 | }
|
| 43 |
|
| 44 | @Override
|
| 45 | public T get(final int index) {
|
| 46 | this.verifyIndexInRange(index);
|
| 47 | return this.getInner(this.getIndex(index));
|
| 48 | }
|
| 49 |
|
| 50 | private static void verifyIndexInRange(final int index, final int size) {
|
| 51 | if (index < 0 || index >= size) {
|
| 52 | throw new IndexOutOfBoundsException(index);
|
| 53 | }
|
| 54 | }
|
| 55 |
|
| 56 | private void verifyIndexInRange(final int index) {
|
| 57 | verifyIndexInRange(index, this.size);
|
| 58 | }
|
| 59 |
|
| 60 | private T getInner(final int innerIndex) {
|
| 61 | return (T)this.contents[innerIndex];
|
| 62 | }
|
| 63 |
|
| 64 | @Override
|
| 65 | public T set(final int index, final T element) {
|
| 66 | this.verifyIndexInRange(index);
|
| 67 | Objects.requireNonNull(element);
|
| 68 | int innerIndex = this.getIndex(index);
|
| 69 | T current = this.getInner(innerIndex);
|
| 70 | this.contents[innerIndex] = element;
|
| 71 | return current;
|
| 72 | }
|
| 73 |
|
| 74 | @Override
|
| 75 | public void add(final int index, final T element) {
|
| 76 | verifyIndexInRange(index, this.size + 1);
|
| 77 | Objects.requireNonNull(element);
|
| 78 | if (this.size == this.contents.length) {
|
| 79 | this.grow();
|
| 80 | }
|
| 81 |
|
| 82 | int internalIndex = this.getIndex(index);
|
| 83 | if (index == this.size) {
|
| 84 | this.contents[internalIndex] = element;
|
| 85 | } else if (index == 0) {
|
| 86 | this.head--;
|
| 87 | if (this.head < 0) {
|
| 88 | this.head = this.head + this.contents.length;
|
| 89 | }
|
| 90 |
|
| 91 | this.contents[this.getIndex(0)] = element;
|
| 92 | } else {
|
| 93 | for (int i = this.size - 1; i >= index; i--) {
|
| 94 | this.contents[this.getIndex(i + 1)] = this.contents[this.getIndex(i)];
|
| 95 | }
|
| 96 |
|
| 97 | this.contents[internalIndex] = element;
|
| 98 | }
|
| 99 |
|
| 100 | this.modCount++;
|
| 101 | this.size++;
|
| 102 | }
|
| 103 |
|
| 104 | private void grow() {
|
| 105 | int newLength = this.contents.length + Math.max(this.contents.length >> 1, 1);
|
| 106 | Object[] newContents = new Object[newLength];
|
| 107 | this.copyCount(newContents, this.size);
|
| 108 | this.head = 0;
|
| 109 | this.contents = newContents;
|
| 110 | }
|
| 111 |
|
| 112 | @Override
|
| 113 | public T remove(final int index) {
|
| 114 | this.verifyIndexInRange(index);
|
| 115 | int innerIndex = this.getIndex(index);
|
| 116 | T value = this.getInner(innerIndex);
|
| 117 | if (index == 0) {
|
| 118 | this.contents[innerIndex] = null;
|
| 119 | this.head++;
|
| 120 | } else if (index == this.size - 1) {
|
| 121 | this.contents[innerIndex] = null;
|
| 122 | } else {
|
| 123 | for (int i = index + 1; i < this.size; i++) {
|
| 124 | this.contents[this.getIndex(i - 1)] = this.get(i);
|
| 125 | }
|
| 126 |
|
| 127 | this.contents[this.getIndex(this.size - 1)] = null;
|
| 128 | }
|
| 129 |
|
| 130 | this.modCount++;
|
| 131 | this.size--;
|
| 132 | return value;
|
| 133 | }
|
| 134 |
|
| 135 | @Override
|
| 136 | public boolean removeIf(final Predicate<? super T> filter) {
|
| 137 | int removed = 0;
|
| 138 |
|
| 139 | for (int i = 0; i < this.size; i++) {
|
| 140 | T value = this.get(i);
|
| 141 | if (filter.test(value)) {
|
| 142 | removed++;
|
| 143 | } else if (removed != 0) {
|
| 144 | this.contents[this.getIndex(i - removed)] = value;
|
| 145 | this.contents[this.getIndex(i)] = null;
|
| 146 | }
|
| 147 | }
|
| 148 |
|
| 149 | this.modCount += removed;
|
| 150 | this.size -= removed;
|
| 151 | return removed != 0;
|
| 152 | }
|
| 153 |
|
| 154 | private void copyCount(final Object[] newContents, final int count) {
|
| 155 | for (int i = 0; i < count; i++) {
|
| 156 | newContents[i] = this.get(i);
|
| 157 | }
|
| 158 | }
|
| 159 |
|
| 160 | @Override
|
| 161 | public void replaceAll(final UnaryOperator<T> operator) {
|
| 162 | for (int i = 0; i < this.size; i++) {
|
| 163 | int index = this.getIndex(i);
|
| 164 | this.contents[index] = Objects.requireNonNull(operator.apply(this.getInner(i)));
|
| 165 | }
|
| 166 | }
|
| 167 |
|
| 168 | @Override
|
| 169 | public void forEach(final Consumer<? super T> action) {
|
| 170 | for (int i = 0; i < this.size; i++) {
|
| 171 | action.accept(this.get(i));
|
| 172 | }
|
| 173 | }
|
| 174 |
|
| 175 | @Override
|
| 176 | public void addFirst(final T value) {
|
| 177 | this.add(0, value);
|
| 178 | }
|
| 179 |
|
| 180 | @Override
|
| 181 | public void addLast(final T value) {
|
| 182 | this.add(this.size, value);
|
| 183 | }
|
| 184 |
|
| 185 | @Override
|
| 186 | public boolean offerFirst(final T value) {
|
| 187 | this.addFirst(value);
|
| 188 | return true;
|
| 189 | }
|
| 190 |
|
| 191 | @Override
|
| 192 | public boolean offerLast(final T value) {
|
| 193 | this.addLast(value);
|
| 194 | return true;
|
| 195 | }
|
| 196 |
|
| 197 | @Override
|
| 198 | public T removeFirst() {
|
| 199 | if (this.size == 0) {
|
| 200 | throw new NoSuchElementException();
|
| 201 | } else {
|
| 202 | return this.remove(0);
|
| 203 | }
|
| 204 | }
|
| 205 |
|
| 206 | @Override
|
| 207 | public T removeLast() {
|
| 208 | if (this.size == 0) {
|
| 209 | throw new NoSuchElementException();
|
| 210 | } else {
|
| 211 | return this.remove(this.size - 1);
|
| 212 | }
|
| 213 | }
|
| 214 |
|
| 215 | @Override
|
| 216 | public ListAndDeque<T> reversed() {
|
| 217 | return new ArrayListDeque.ReversedView(this);
|
| 218 | }
|
| 219 |
|
| 220 | @Override
|
| 221 | public @Nullable T pollFirst() {
|
| 222 | return this.size == 0 ? null : this.removeFirst();
|
| 223 | }
|
| 224 |
|
| 225 | @Override
|
| 226 | public @Nullable T pollLast() {
|
| 227 | return this.size == 0 ? null : this.removeLast();
|
| 228 | }
|
| 229 |
|
| 230 | @Override
|
| 231 | public T getFirst() {
|
| 232 | if (this.size == 0) {
|
| 233 | throw new NoSuchElementException();
|
| 234 | } else {
|
| 235 | return this.get(0);
|
| 236 | }
|
| 237 | }
|
| 238 |
|
| 239 | @Override
|
| 240 | public T getLast() {
|
| 241 | if (this.size == 0) {
|
| 242 | throw new NoSuchElementException();
|
| 243 | } else {
|
| 244 | return this.get(this.size - 1);
|
| 245 | }
|
| 246 | }
|
| 247 |
|
| 248 | @Override
|
| 249 | public @Nullable T peekFirst() {
|
| 250 | return this.size == 0 ? null : this.getFirst();
|
| 251 | }
|
| 252 |
|
| 253 | @Override
|
| 254 | public @Nullable T peekLast() {
|
| 255 | return this.size == 0 ? null : this.getLast();
|
| 256 | }
|
| 257 |
|
| 258 | @Override
|
| 259 | public boolean removeFirstOccurrence(final Object o) {
|
| 260 | for (int i = 0; i < this.size; i++) {
|
| 261 | T value = this.get(i);
|
| 262 | if (Objects.equals(o, value)) {
|
| 263 | this.remove(i);
|
| 264 | return true;
|
| 265 | }
|
| 266 | }
|
| 267 |
|
| 268 | return false;
|
| 269 | }
|
| 270 |
|
| 271 | @Override
|
| 272 | public boolean removeLastOccurrence(final Object o) {
|
| 273 | for (int i = this.size - 1; i >= 0; i--) {
|
| 274 | T value = this.get(i);
|
| 275 | if (Objects.equals(o, value)) {
|
| 276 | this.remove(i);
|
| 277 | return true;
|
| 278 | }
|
| 279 | }
|
| 280 |
|
| 281 | return false;
|
| 282 | }
|
| 283 |
|
| 284 | @Override
|
| 285 | public Iterator<T> descendingIterator() {
|
| 286 | return new ArrayListDeque.DescendingIterator();
|
| 287 | }
|
| 288 |
|
| 289 | private class DescendingIterator implements Iterator<T> {
|
| 290 | private int index = ArrayListDeque.this.size() - 1;
|
| 291 |
|
| 292 | public DescendingIterator() {
|
| 293 | }
|
| 294 |
|
| 295 | @Override
|
| 296 | public boolean hasNext() {
|
| 297 | return this.index >= 0;
|
| 298 | }
|
| 299 |
|
| 300 | @Override
|
| 301 | public T next() {
|
| 302 | return ArrayListDeque.this.get(this.index--);
|
| 303 | }
|
| 304 |
|
| 305 | @Override
|
| 306 | public void remove() {
|
| 307 | ArrayListDeque.this.remove(this.index + 1);
|
| 308 | }
|
| 309 | }
|
| 310 |
|
| 311 | private class ReversedView extends AbstractList<T> implements ListAndDeque<T> {
|
| 312 | private final ArrayListDeque<T> source;
|
| 313 |
|
| 314 | public ReversedView(final ArrayListDeque<T> source) {
|
| 315 | this.source = source;
|
| 316 | }
|
| 317 |
|
| 318 | @Override
|
| 319 | public ListAndDeque<T> reversed() {
|
| 320 | return this.source;
|
| 321 | }
|
| 322 |
|
| 323 | @Override
|
| 324 | public T getFirst() {
|
| 325 | return this.source.getLast();
|
| 326 | }
|
| 327 |
|
| 328 | @Override
|
| 329 | public T getLast() {
|
| 330 | return this.source.getFirst();
|
| 331 | }
|
| 332 |
|
| 333 | @Override
|
| 334 | public void addFirst(final T t) {
|
| 335 | this.source.addLast(t);
|
| 336 | }
|
| 337 |
|
| 338 | @Override
|
| 339 | public void addLast(final T t) {
|
| 340 | this.source.addFirst(t);
|
| 341 | }
|
| 342 |
|
| 343 | @Override
|
| 344 | public boolean offerFirst(final T t) {
|
| 345 | return this.source.offerLast(t);
|
| 346 | }
|
| 347 |
|
| 348 | @Override
|
| 349 | public boolean offerLast(final T t) {
|
| 350 | return this.source.offerFirst(t);
|
| 351 | }
|
| 352 |
|
| 353 | @Override
|
| 354 | public @Nullable T pollFirst() {
|
| 355 | return this.source.pollLast();
|
| 356 | }
|
| 357 |
|
| 358 | @Override
|
| 359 | public @Nullable T pollLast() {
|
| 360 | return this.source.pollFirst();
|
| 361 | }
|
| 362 |
|
| 363 | @Override
|
| 364 | public @Nullable T peekFirst() {
|
| 365 | return this.source.peekLast();
|
| 366 | }
|
| 367 |
|
| 368 | @Override
|
| 369 | public @Nullable T peekLast() {
|
| 370 | return this.source.peekFirst();
|
| 371 | }
|
| 372 |
|
| 373 | @Override
|
| 374 | public T removeFirst() {
|
| 375 | return this.source.removeLast();
|
| 376 | }
|
| 377 |
|
| 378 | @Override
|
| 379 | public T removeLast() {
|
| 380 | return this.source.removeFirst();
|
| 381 | }
|
| 382 |
|
| 383 | @Override
|
| 384 | public boolean removeFirstOccurrence(final Object o) {
|
| 385 | return this.source.removeLastOccurrence(o);
|
| 386 | }
|
| 387 |
|
| 388 | @Override
|
| 389 | public boolean removeLastOccurrence(final Object o) {
|
| 390 | return this.source.removeFirstOccurrence(o);
|
| 391 | }
|
| 392 |
|
| 393 | @Override
|
| 394 | public Iterator<T> descendingIterator() {
|
| 395 | return this.source.iterator();
|
| 396 | }
|
| 397 |
|
| 398 | @Override
|
| 399 | public int size() {
|
| 400 | return this.source.size();
|
| 401 | }
|
| 402 |
|
| 403 | @Override
|
| 404 | public boolean isEmpty() {
|
| 405 | return this.source.isEmpty();
|
| 406 | }
|
| 407 |
|
| 408 | @Override
|
| 409 | public boolean contains(final Object o) {
|
| 410 | return this.source.contains(o);
|
| 411 | }
|
| 412 |
|
| 413 | @Override
|
| 414 | public T get(final int index) {
|
| 415 | return this.source.get(this.reverseIndex(index));
|
| 416 | }
|
| 417 |
|
| 418 | @Override
|
| 419 | public T set(final int index, final T element) {
|
| 420 | return this.source.set(this.reverseIndex(index), element);
|
| 421 | }
|
| 422 |
|
| 423 | @Override
|
| 424 | public void add(final int index, final T element) {
|
| 425 | this.source.add(this.reverseIndex(index) + 1, element);
|
| 426 | }
|
| 427 |
|
| 428 | @Override
|
| 429 | public T remove(final int index) {
|
| 430 | return this.source.remove(this.reverseIndex(index));
|
| 431 | }
|
| 432 |
|
| 433 | @Override
|
| 434 | public int indexOf(final Object o) {
|
| 435 | return this.reverseIndex(this.source.lastIndexOf(o));
|
| 436 | }
|
| 437 |
|
| 438 | @Override
|
| 439 | public int lastIndexOf(final Object o) {
|
| 440 | return this.reverseIndex(this.source.indexOf(o));
|
| 441 | }
|
| 442 |
|
| 443 | @Override
|
| 444 | public List<T> subList(final int fromIndex, final int toIndex) {
|
| 445 | return this.source.subList(this.reverseIndex(toIndex) + 1, this.reverseIndex(fromIndex) + 1).reversed();
|
| 446 | }
|
| 447 |
|
| 448 | @Override
|
| 449 | public Iterator<T> iterator() {
|
| 450 | return this.source.descendingIterator();
|
| 451 | }
|
| 452 |
|
| 453 | @Override
|
| 454 | public void clear() {
|
| 455 | this.source.clear();
|
| 456 | }
|
| 457 |
|
| 458 | private int reverseIndex(final int index) {
|
| 459 | return index == -1 ? -1 : this.source.size() - 1 - index;
|
| 460 | }
|
| 461 | }
|
| 462 | }
|
| 463 |
|