|
| 1 | +package dev.mv.utilsx.collection; |
| 2 | + |
| 3 | +import dev.mv.utilsx.ArrayUtils; |
| 4 | +import dev.mv.utilsx.generic.Option; |
| 5 | +import dev.mv.utilsx.sequence.Sequence; |
| 6 | +import org.jetbrains.annotations.NotNull; |
| 7 | + |
| 8 | +import java.lang.reflect.Array; |
| 9 | +import java.util.Arrays; |
| 10 | +import java.util.Collection; |
| 11 | +import java.util.Iterator; |
| 12 | +import java.util.RandomAccess; |
| 13 | +import java.util.function.Consumer; |
| 14 | + |
| 15 | +public class Vec<T> implements RandomAccess, Iterable<T> { |
| 16 | + |
| 17 | + transient T[] elements; |
| 18 | + int length; |
| 19 | + |
| 20 | + public Vec(T... ignore) { |
| 21 | + this(0, ignore); |
| 22 | + } |
| 23 | + |
| 24 | + public Vec(int capacity, T... ignore) { |
| 25 | + elements = (T[]) Array.newInstance(ignore.getClass().componentType(), capacity); |
| 26 | + } |
| 27 | + |
| 28 | + private void grow(int minAmount) { |
| 29 | + if (elements.length < 3) { |
| 30 | + elements = Arrays.copyOf(elements, Math.max(minAmount + 3, 5)); |
| 31 | + return; |
| 32 | + } |
| 33 | + int initialLength = elements.length; |
| 34 | + int newLength = (int) (initialLength * ArrayUtils.phi); |
| 35 | + while (newLength - initialLength < minAmount) { |
| 36 | + newLength = (int) (newLength * ArrayUtils.phi); |
| 37 | + } |
| 38 | + elements = Arrays.copyOf(elements, newLength); |
| 39 | + } |
| 40 | + |
| 41 | + public int capacity() { |
| 42 | + return elements.length; |
| 43 | + } |
| 44 | + |
| 45 | + public int len() { |
| 46 | + return length; |
| 47 | + } |
| 48 | + |
| 49 | + public boolean isEmpty() { |
| 50 | + return length == 0; |
| 51 | + } |
| 52 | + |
| 53 | + public void reserve(int additional) { |
| 54 | + if (length + additional > elements.length) grow(additional); |
| 55 | + } |
| 56 | + |
| 57 | + public void reserveExact(int additional) { |
| 58 | + if (length + additional > elements.length) elements = Arrays.copyOf(elements, length + additional); |
| 59 | + |
| 60 | + } |
| 61 | + |
| 62 | + public void shrinkToFit() { |
| 63 | + truncate(length); |
| 64 | + } |
| 65 | + |
| 66 | + public void truncate(int length) { |
| 67 | + if (length < this.length) elements = Arrays.copyOf(elements, length); |
| 68 | + |
| 69 | + } |
| 70 | + |
| 71 | + public void insert(int index, T element) { |
| 72 | + if (length == elements.length) grow(1); |
| 73 | + |
| 74 | + |
| 75 | + if (index < length) { |
| 76 | + System.arraycopy(elements, index, elements, index + 1, length - index); |
| 77 | + length++; |
| 78 | + } |
| 79 | + else if (index > length) { |
| 80 | + reserve(index - length); |
| 81 | + length = index + 1; |
| 82 | + } |
| 83 | + else { |
| 84 | + length++; |
| 85 | + } |
| 86 | + |
| 87 | + elements[index] = element; |
| 88 | + } |
| 89 | + |
| 90 | + public T remove(int index) { |
| 91 | + if (index >= length) throw new IndexOutOfBoundsException("Index " + index + " is out of bounds for length " + length + "!"); |
| 92 | + T element = elements[index]; |
| 93 | + System.arraycopy(elements, index + 1, elements, index, length - index - 1); |
| 94 | + return element; |
| 95 | + } |
| 96 | + |
| 97 | + public void push(T element) { |
| 98 | + if (length == elements.length) grow(1); |
| 99 | + elements[length] = element; |
| 100 | + length++; |
| 101 | + } |
| 102 | + |
| 103 | + public T pop() { |
| 104 | + if (length == 0) return null; |
| 105 | + length--; |
| 106 | + T element = elements[length]; |
| 107 | + elements[length] = null; |
| 108 | + return element; |
| 109 | + } |
| 110 | + |
| 111 | + public T get(int index) { |
| 112 | + if (index >= length) throw new IndexOutOfBoundsException("Index " + index + " is out of bounds for length " + length + "!"); |
| 113 | + return elements[index]; |
| 114 | + } |
| 115 | + |
| 116 | + public void append(Vec<T> other) { |
| 117 | + reserve(other.length); |
| 118 | + System.arraycopy(other.elements, 0, elements, length, other.length); |
| 119 | + length += other.length; |
| 120 | + } |
| 121 | + |
| 122 | + public void append(T[] other) { |
| 123 | + reserve(other.length); |
| 124 | + System.arraycopy(other, 0, elements, length, other.length); |
| 125 | + length += other.length; |
| 126 | + } |
| 127 | + |
| 128 | + public void append(Collection<T> other) { |
| 129 | + reserve(other.size()); |
| 130 | + System.arraycopy(other.toArray(Arrays.copyOf(elements, 0)), 0, elements, length, other.size()); |
| 131 | + length += other.size(); |
| 132 | + } |
| 133 | + |
| 134 | + public T[] toArray() { |
| 135 | + return Arrays.copyOf(elements, length); |
| 136 | + } |
| 137 | + |
| 138 | + public Sequence<T> iter() { |
| 139 | + return new Iter(this); |
| 140 | + } |
| 141 | + |
| 142 | + public Sequence<T> iterCopied() { |
| 143 | + return new IterCopied<>(toArray()); |
| 144 | + } |
| 145 | + |
| 146 | + public Sequence<T> drain() { |
| 147 | + return drain(0, length); |
| 148 | + } |
| 149 | + |
| 150 | + public Sequence<T> drain(int start, int end) { |
| 151 | + if (start > end) throw new IndexOutOfBoundsException("Start cannot be bigger than end"); |
| 152 | + if (end > length) throw new IndexOutOfBoundsException("End index " + end + " is out of bounds for length " + length + "!"); |
| 153 | + T[] drained = Arrays.copyOfRange(elements, start, end); |
| 154 | + System.arraycopy(elements, end, elements, start, length - end); |
| 155 | + Arrays.fill(elements, start + length - end, length, null); |
| 156 | + length -= end - start; |
| 157 | + return new IterCopied<>(drained); |
| 158 | + } |
| 159 | + |
| 160 | + @NotNull |
| 161 | + @Override |
| 162 | + public Iterator<T> iterator() { |
| 163 | + return iter().iterator(); |
| 164 | + } |
| 165 | + |
| 166 | + @Override |
| 167 | + public void forEach(Consumer<? super T> action) { |
| 168 | + Iterable.super.forEach(action); |
| 169 | + } |
| 170 | + |
| 171 | + @Override |
| 172 | + public boolean equals(Object obj) { |
| 173 | + if (obj instanceof Vec vec) { |
| 174 | + if (length != vec.length) return false; |
| 175 | + for (int i = 0; i < length; i++) { |
| 176 | + if (elements[i] == null) { |
| 177 | + if (vec.elements[i] == null) continue; |
| 178 | + return false; |
| 179 | + } |
| 180 | + if (!elements[i].equals(vec.elements[i])) return false; |
| 181 | + } |
| 182 | + return true; |
| 183 | + } |
| 184 | + return false; |
| 185 | + } |
| 186 | + |
| 187 | + @Override |
| 188 | + public Object clone() { |
| 189 | + Vec<T> clone = new Vec<>(Arrays.copyOf(elements, 0)); |
| 190 | + clone.elements = Arrays.copyOf(elements, length); |
| 191 | + clone.length = length; |
| 192 | + return clone; |
| 193 | + } |
| 194 | + |
| 195 | + @Override |
| 196 | + public String toString() { |
| 197 | + StringBuilder builder = new StringBuilder("["); |
| 198 | + for (int i = 0; i < length; i++) { |
| 199 | + builder.append(elements[i]); |
| 200 | + if (i + 1 < length) builder.append(", "); |
| 201 | + } |
| 202 | + builder.append("]"); |
| 203 | + return builder.toString(); |
| 204 | + } |
| 205 | + |
| 206 | + public static class Iter<T> implements Sequence<T> { |
| 207 | + private int index = 0; |
| 208 | + private Vec<T> parent; |
| 209 | + private Iter(Vec<T> parent) { |
| 210 | + this.parent = parent; |
| 211 | + } |
| 212 | + |
| 213 | + @Override |
| 214 | + public Option<T> next() { |
| 215 | + if (index < parent.length) { |
| 216 | + return Option.some(parent.get(index++)); |
| 217 | + } |
| 218 | + return Option.none(); |
| 219 | + } |
| 220 | + } |
| 221 | + |
| 222 | + public static class IterCopied<T> implements Sequence<T> { |
| 223 | + private int index = 0; |
| 224 | + private T[] parent; |
| 225 | + |
| 226 | + private IterCopied(T[] elements) { |
| 227 | + parent = elements; |
| 228 | + } |
| 229 | + |
| 230 | + @Override |
| 231 | + public Option<T> next() { |
| 232 | + if (index < parent.length) { |
| 233 | + return Option.some(parent[index++]); |
| 234 | + } |
| 235 | + return Option.none(); |
| 236 | + } |
| 237 | + } |
| 238 | +} |
0 commit comments