|
| 1 | +package github.kasuminova.novaeng.common.util; |
| 2 | + |
| 3 | +import github.kasuminova.novaeng.NovaEngineeringCore; |
| 4 | +import it.unimi.dsi.fastutil.objects.ObjectArrayList; |
| 5 | +import it.unimi.dsi.fastutil.objects.ObjectOpenHashSet; |
| 6 | +import org.jspecify.annotations.NonNull; |
| 7 | + |
| 8 | +import java.util.AbstractList; |
| 9 | +import java.util.Collection; |
| 10 | +import java.util.List; |
| 11 | +import java.util.Objects; |
| 12 | +import java.util.RandomAccess; |
| 13 | +import java.util.Set; |
| 14 | + |
| 15 | +public class SetList<T> extends AbstractList<T> implements RandomAccess { |
| 16 | + |
| 17 | + private final List<T> list = new ObjectArrayList<>(); |
| 18 | + private final Set<T> set = new ObjectOpenHashSet<>(); |
| 19 | + |
| 20 | + @Override |
| 21 | + public T get(int index) { |
| 22 | + return list.get(index); |
| 23 | + } |
| 24 | + |
| 25 | + @Override |
| 26 | + public int size() { |
| 27 | + return list.size(); |
| 28 | + } |
| 29 | + |
| 30 | + @Override |
| 31 | + public boolean contains(Object o) { |
| 32 | + return set.contains(o); |
| 33 | + } |
| 34 | + |
| 35 | + @Override |
| 36 | + public boolean add(T element) { |
| 37 | + if (!set.add(element)) { |
| 38 | + warnDuplicate(element); |
| 39 | + return false; |
| 40 | + } |
| 41 | + list.add(element); |
| 42 | + modCount++; |
| 43 | + return true; |
| 44 | + } |
| 45 | + |
| 46 | + @Override |
| 47 | + public void add(int index, T element) { |
| 48 | + if (!set.add(element)) { |
| 49 | + warnDuplicate(element); |
| 50 | + return; |
| 51 | + } |
| 52 | + list.add(index, element); |
| 53 | + modCount++; |
| 54 | + } |
| 55 | + |
| 56 | + @Override |
| 57 | + public boolean addAll(@NonNull Collection<? extends T> collection) { |
| 58 | + return addAll(list.size(), collection); |
| 59 | + } |
| 60 | + |
| 61 | + @Override |
| 62 | + public boolean addAll(int index, @NonNull Collection<? extends T> collection) { |
| 63 | + int insertIndex = index; |
| 64 | + boolean changed = false; |
| 65 | + for (T element : collection) { |
| 66 | + if (set.add(element)) { |
| 67 | + list.add(insertIndex, element); |
| 68 | + insertIndex++; |
| 69 | + changed = true; |
| 70 | + } else { |
| 71 | + warnDuplicate(element); |
| 72 | + } |
| 73 | + } |
| 74 | + if (changed) { |
| 75 | + modCount++; |
| 76 | + } |
| 77 | + return changed; |
| 78 | + } |
| 79 | + |
| 80 | + @Override |
| 81 | + public T set(int index, T element) { |
| 82 | + T oldValue = list.get(index); |
| 83 | + if (Objects.equals(oldValue, element)) { |
| 84 | + return oldValue; |
| 85 | + } |
| 86 | + |
| 87 | + int existingIndex = list.indexOf(element); |
| 88 | + if (existingIndex >= 0) { |
| 89 | + warnDuplicate(element); |
| 90 | + list.remove(existingIndex); |
| 91 | + if (existingIndex < index) { |
| 92 | + index--; |
| 93 | + } |
| 94 | + list.set(index, element); |
| 95 | + set.remove(oldValue); |
| 96 | + modCount++; |
| 97 | + return oldValue; |
| 98 | + } |
| 99 | + |
| 100 | + list.set(index, element); |
| 101 | + set.remove(oldValue); |
| 102 | + set.add(element); |
| 103 | + modCount++; |
| 104 | + return oldValue; |
| 105 | + } |
| 106 | + |
| 107 | + @Override |
| 108 | + public T remove(int index) { |
| 109 | + T removed = list.remove(index); |
| 110 | + set.remove(removed); |
| 111 | + modCount++; |
| 112 | + return removed; |
| 113 | + } |
| 114 | + |
| 115 | + @Override |
| 116 | + public boolean remove(Object o) { |
| 117 | + int index = list.indexOf(o); |
| 118 | + if (index < 0) { |
| 119 | + return false; |
| 120 | + } |
| 121 | + remove(index); |
| 122 | + return true; |
| 123 | + } |
| 124 | + |
| 125 | + @Override |
| 126 | + public void clear() { |
| 127 | + if (list.isEmpty()) { |
| 128 | + return; |
| 129 | + } |
| 130 | + list.clear(); |
| 131 | + set.clear(); |
| 132 | + modCount++; |
| 133 | + } |
| 134 | + |
| 135 | + @Override |
| 136 | + public int indexOf(Object o) { |
| 137 | + return list.indexOf(o); |
| 138 | + } |
| 139 | + |
| 140 | + @Override |
| 141 | + public int lastIndexOf(Object o) { |
| 142 | + return list.lastIndexOf(o); |
| 143 | + } |
| 144 | + |
| 145 | + private void warnDuplicate(T element) { |
| 146 | + NovaEngineeringCore.log.warn("[{}]SetList detected duplicate element: {}", Thread.currentThread().getName(), element); |
| 147 | + } |
| 148 | +} |
0 commit comments