initial commit

This commit is contained in:
00asdf 2023-04-18 04:36:04 +02:00
commit e49993f88a
8 changed files with 2092 additions and 0 deletions

31
.gitignore vendored Normal file
View File

@ -0,0 +1,31 @@
### IntelliJ IDEA ###
out/
!**/src/main/**/out/
!**/src/test/**/out/
### Eclipse ###
.apt_generated
.classpath
.factorypath
.project
.settings
.springBeans
.sts4-cache
bin/
!**/src/main/**/bin/
!**/src/test/**/bin/
### NetBeans ###
/nbproject/private/
/nbbuild/
/dist/
/nbdist/
/.nb-gradle/
### VS Code ###
.vscode/
### Mac OS ###
.DS_Store
.idea

11
GeneralUtils.iml Normal file
View File

@ -0,0 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>

View File

@ -0,0 +1,162 @@
package dev.asdf00.general.utils.list;
import dev.asdf00.general.utils.list.internal.AbstractBaseList;
import dev.asdf00.general.utils.list.internal.ByteList;
public abstract class PrimitiveList<T> extends AbstractBaseList<T> {
public static <T> PrimitiveList<T> create(Class<T> type) {
return create(type, 8);
}
public static <T> PrimitiveList<T> create(Class<T> boxedType, int initial) {
if (Byte.class == boxedType) {
return (PrimitiveList<T>) new ByteList(initial);
} else if (Short.class.equals(boxedType)) {
return null;
} else if (Integer.class.equals(boxedType)) {
return null;
} else if (Long.class.equals(boxedType)) {
return null;
} else if (Float.class.equals(boxedType)) {
return null;
} else if (Double.class.equals(boxedType)) {
return null;
} else if (Character.class.equals(boxedType)) {
return null;
} else if (Boolean.class.equals(boxedType)) {
return null;
} else {
throw new IllegalArgumentException("%s is not a boxed type!".formatted(boxedType));
}
}
public void addByte(byte value) {
throw new UnsupportedOperationException("this list is not of type Byte!");
}
public void addShort(short value) {
throw new UnsupportedOperationException("this list is not of type Short!");
}
public void addInt(int value) {
throw new UnsupportedOperationException("this list is not of type Integer!");
}
public void addLong(long value) {
throw new UnsupportedOperationException("this list is not of type Long!");
}
public void addFloat(float value) {
throw new UnsupportedOperationException("this list is not of type Float!");
}
public void addDouble(double value) {
throw new UnsupportedOperationException("this list is not of type Double!");
}
public void addChar(char value) {
throw new UnsupportedOperationException("this list is not of type Character!");
}
public void addBoolean(boolean value) {
throw new UnsupportedOperationException("this list is not of type Boolean!");
}
public byte getByte(int index) {
throw new UnsupportedOperationException("this list is not of type Byte!");
}
public short getShort(int index) {
throw new UnsupportedOperationException("this list is not of type Short!");
}
public int getInt(int index) {
throw new UnsupportedOperationException("this list is not of type Integer!");
}
public long getLong(int index) {
throw new UnsupportedOperationException("this list is not of type Long!");
}
public float getFloat(int index) {
throw new UnsupportedOperationException("this list is not of type Float!");
}
public double getDouble(int index) {
throw new UnsupportedOperationException("this list is not of type Double!");
}
public char getChar(int index) {
throw new UnsupportedOperationException("this list is not of type Character!");
}
public boolean getBoolean(int index) {
throw new UnsupportedOperationException("this list is not of type Boolean!");
}
public byte[] toByteArray() {
throw new UnsupportedOperationException("this list is not of type Byte!");
}
public short[] toShortArray() {
throw new UnsupportedOperationException("this list is not of type Short!");
}
public int[] toIntArray() {
throw new UnsupportedOperationException("this list is not of type Integer!");
}
public long[] toLongArray() {
throw new UnsupportedOperationException("this list is not of type Long!");
}
public float[] toFloatArray() {
throw new UnsupportedOperationException("this list is not of type Float!");
}
public double[] toDoubleArray() {
throw new UnsupportedOperationException("this list is not of type Double!");
}
public char[] toCharArray() {
throw new UnsupportedOperationException("this list is not of type Character!");
}
public boolean[] toBooleanArray() {
throw new UnsupportedOperationException("this list is not of type Boolean!");
}
public byte[] finalizeAsByteArray() {
throw new UnsupportedOperationException("this list is not of type Byte!");
}
public short[] finalizeAsShortArray() {
throw new UnsupportedOperationException("this list is not of type Short!");
}
public int[] finalizeAsIntArray() {
throw new UnsupportedOperationException("this list is not of type Integer!");
}
public long[] finalizeAsLongArray() {
throw new UnsupportedOperationException("this list is not of type Long!");
}
public float[] finalizeAsFloatArray() {
throw new UnsupportedOperationException("this list is not of type Float!");
}
public double[] finalizeAsDoubleArray() {
throw new UnsupportedOperationException("this list is not of type Double!");
}
public char[] finalizeAsCharArray() {
throw new UnsupportedOperationException("this list is not of type Character!");
}
public boolean[] finalizeAsBooleanArray() {
throw new UnsupportedOperationException("this list is not of type Boolean!");
}
public abstract boolean isFinalized();
}

View File

@ -0,0 +1,489 @@
package dev.asdf00.general.utils.list.internal;
import sun.misc.Unsafe;
import java.lang.reflect.Field;
import java.util.*;
public abstract class AbstractBaseList<T> implements List<T> {
static final Unsafe UNSAFE;
static final long OFFSET_LEN;
static {
try {
Field f = Unsafe.class.getDeclaredField("theUnsafe");
f.setAccessible(true);
UNSAFE = (Unsafe) f.get(null);
// OFFSET_LEN = sizeof(mark-word) + sizeof(klass-ptr)
OFFSET_LEN = UNSAFE.addressSize() + (UNSAFE.addressSize() == 8 && System.getProperty("java.vm.compressedOopsMode") == null ? 8 : 4);
} catch (NoSuchFieldException | IllegalAccessException e) {
throw new RuntimeException(e);
}
}
protected int size;
protected int modCnt;
@Override
public Object[] toArray() {
return toArray(Object[]::new);
}
@Override
public <T1> T1[] toArray(T1[] a) {
return toArray(a, 0, size);
}
@Override
public int size() {
return size();
}
@Override
public boolean isEmpty() {
return size() < 1;
}
@Override
public boolean contains(Object o) {
return indexOf(o) >= 0;
}
@Override
public Iterator<T> iterator() {
return listIterator();
}
@Override
public boolean containsAll(Collection<?> c) {
for (Object o : c) {
if (!contains(o)) {
return false;
}
}
return true;
}
@Override
public T remove(int index) {
Objects.checkIndex(index, size);
return remove(index, 0, size);
}
@Override
public boolean remove(Object o) {
int index = indexOf(o);
if (index >= 0) {
remove(index);
return true;
}
return false;
}
@Override
public boolean removeAll(Collection<?> c) {
boolean changed = false;
for (Object o : c) {
changed = remove(o) || changed;
}
return changed;
}
@Override
public boolean retainAll(Collection<?> c) {
boolean changed = false;
ListIterator<T> itr = listIterator();
while (itr.hasNext()) {
if (c.contains(itr.next())) {
itr.remove();
changed = true;
}
}
return changed;
}
@Override
public int indexOf(Object o) {
return indexOf(o, 0, size);
}
@Override
public int lastIndexOf(Object o) {
return lastIndexOf(o, 0, size);
}
@Override
public T set(int index, T element) {
Objects.checkIndex(index, size);
return directSet(index, element);
}
@Override
public boolean add(T t) {
directSet(size++, t);
return true;
}
@Override
public ListIterator<T> listIterator() {
return new ListItr<>(this, 0);
}
@Override
public ListIterator<T> listIterator(int index) {
Objects.checkIndex(index, size);
return new ListItr<>(this, index);
}
@Override
public List<T> subList(int fromIndex, int toIndex) {
Objects.checkIndex(fromIndex, size);
Objects.checkIndex(fromIndex, toIndex);
int len = (toIndex - fromIndex) + 1;
if (len < 0) {
throw new IllegalArgumentException();
}
return new SubList<>(this, fromIndex, len);
}
protected abstract <T1> T1[] toArray(T1[] a, int start, int len);
protected abstract T remove(Object o, int start, int len);
protected abstract int indexOf(Object o, int start, int len);
protected abstract int lastIndexOf(Object o, int start, int len);
protected abstract T directSet(int index, T element);
protected abstract void removeRange(int start, int len);
private static class ListItr<E> implements ListIterator<E> {
private final AbstractBaseList<E> base;
private int modCnt;
private int cur;
private int last;
public ListItr(AbstractBaseList base, int start) {
this.base = base;
modCnt = base.modCnt;
cur = start;
last = -1;
}
@Override
public boolean hasNext() {
return cur < base.size;
}
@Override
public E next() {
checkConcurrentModification();
if (!hasNext()) {
throw new IndexOutOfBoundsException();
}
last = cur;
return base.get(cur++);
}
@Override
public boolean hasPrevious() {
return cur > 1;
}
@Override
public E previous() {
checkConcurrentModification();
if (!hasPrevious()) {
throw new IndexOutOfBoundsException();
}
last = --cur;
return base.get(cur);
}
@Override
public int nextIndex() {
return cur;
}
@Override
public int previousIndex() {
return cur - 1;
}
@Override
public void remove() {
checkConcurrentModification();
Objects.checkIndex(cur, base.size);
if (last == -1) {
throw new IllegalStateException();
}
modCnt++;
base.remove(--cur);
last = -1;
}
@Override
public void set(E e) {
checkConcurrentModification();
Objects.checkIndex(cur, base.size);
if (last == -1) {
throw new IllegalStateException();
}
modCnt++;
base.set(cur, e);
last = -1;
}
@Override
public void add(E e) {
checkConcurrentModification();
Objects.checkIndex(cur, base.size);
if (last == -1) {
throw new IllegalStateException();
}
modCnt++;
base.add(cur, e);
last = -1;
}
private void checkConcurrentModification() {
if (base.modCnt != modCnt) {
throw new ConcurrentModificationException();
}
}
}
private static class SubList<E> extends AbstractBaseList<E> {
private final AbstractBaseList<E> base;
private final int start;
public SubList(AbstractBaseList<E> base, int start, int size) {
this.base = base;
this.start = start;
this.size = size;
modCnt = base.modCnt;
}
@Override
public boolean addAll(Collection<? extends E> c) {
for (E e : c) {
add(e);
}
return c.size() > 0;
}
@Override
public boolean addAll(int index, Collection<? extends E> c) {
Objects.checkIndex(index, size);
int i = 0;
for (E e : c) {
add(i, e);
}
return false;
}
@Override
public void clear() {
checkConcurrentModification();
modCnt++;
base.removeRange(start, size);
size = 0;
}
@Override
public E get(int index) {
checkConcurrentModification();
Objects.checkIndex(index, size);
return base.get(start + index);
}
@Override
public void add(int index, E element) {
checkConcurrentModification();
Objects.checkIndex(index, size);
modCnt++;
base.add(start + index, element);
}
@Override
public ListIterator<E> listIterator() {
return new SubListItr<>(base, start, start, start + (size - 1));
}
@Override
public ListIterator<E> listIterator(int index) {
Objects.checkIndex(index, size);
return new SubListItr<>(base, start + index, start, start + (size - 1));
}
@Override
public List<E> subList(int fromIndex, int toIndex) {
checkConcurrentModification();
Objects.checkIndex(fromIndex, size);
Objects.checkIndex(toIndex, size);
int len = (toIndex - fromIndex) + 1;
if (len < 0) {
throw new IllegalArgumentException();
}
return new SubList<>(base, start + fromIndex, len);
}
@Override
protected <T1> T1[] toArray(T1[] a, int start, int len) {
checkConcurrentModification();
modCnt++;
return base.toArray(a, this.start + start, len);
}
@Override
protected E remove(Object o, int start, int len) {
checkConcurrentModification();
modCnt++;
return base.remove(o, this.start + start, len);
}
@Override
protected int indexOf(Object o, int start, int len) {
checkConcurrentModification();
return base.indexOf(o, this.start + start, len);
}
@Override
protected int lastIndexOf(Object o, int start, int len) {
checkConcurrentModification();
return base.lastIndexOf(o, this.start + start, len);
}
@Override
protected E directSet(int index, E element) {
checkConcurrentModification();
modCnt++;
return base.directSet(index, element);
}
@Override
protected void removeRange(int start, int len) {
throw new UnsupportedOperationException();
}
private void checkConcurrentModification() {
if (base.modCnt != modCnt) {
throw new ConcurrentModificationException();
}
}
private static class SubListItr<TYPE> implements ListIterator<TYPE> {
private final AbstractBaseList<TYPE> base;
private int modCnt;
private int cur;
private int lower;
private int higher;
private int last;
public SubListItr(AbstractBaseList<TYPE> base, int start, int lower, int higher) {
this.base = base;
modCnt = base.modCnt;
cur = start;
this.lower = lower;
this.higher = higher;
last = -1;
}
@Override
public boolean hasNext() {
return cur <= higher;
}
@Override
public TYPE next() {
checkConcurrentModification();
if (!hasNext()) {
throw new IndexOutOfBoundsException();
}
last = cur;
return base.get(cur++);
}
@Override
public boolean hasPrevious() {
return cur > lower;
}
@Override
public TYPE previous() {
checkConcurrentModification();
if (!hasPrevious()) {
throw new IndexOutOfBoundsException();
}
last = --cur;
return base.get(cur);
}
@Override
public int nextIndex() {
return cur - lower;
}
@Override
public int previousIndex() {
return cur - lower - 1;
}
@Override
public void remove() {
checkConcurrentModification();
rangeCheck();
if (last == -1) {
throw new IllegalStateException();
}
modCnt++;
base.remove(last);
last = -1;
}
@Override
public void set(TYPE element) {
checkConcurrentModification();
rangeCheck();
if (last == -1) {
throw new IllegalStateException();
}
modCnt++;
base.set(last, element);
last = -1;
}
@Override
public void add(TYPE element) {
checkConcurrentModification();
rangeCheck();
if (last == -1) {
throw new IllegalStateException();
}
modCnt++;
base.add(last, element);
last = -1;
}
private void checkConcurrentModification() {
if (base.modCnt != modCnt) {
throw new ConcurrentModificationException();
}
}
private void rangeCheck() {
if (cur < lower || cur > higher) {
throw new IndexOutOfBoundsException();
}
}
}
}
}

View File

@ -0,0 +1,97 @@
package dev.asdf00.general.utils.list.internal;
import dev.asdf00.general.utils.list.PrimitiveList;
import java.util.Collection;
public class ByteList extends PrimitiveList<Boolean> {
private byte[] data;
public ByteList(int initial) {
}
@Override
public boolean isFinalized() {
return data == null;
}
@Override
protected <T1> T1[] toArray(T1[] a, int start, int len) {
return null;
}
@Override
protected Boolean remove(Object o, int start, int len) {
return null;
}
@Override
protected int indexOf(Object o, int start, int len) {
return 0;
}
@Override
protected int lastIndexOf(Object o, int start, int len) {
return 0;
}
@Override
protected Boolean directSet(int index, Boolean element) {
return null;
}
@Override
protected void removeRange(int start, int len) {
}
@Override
public boolean addAll(Collection<? extends Boolean> c) {
return false;
}
@Override
public boolean addAll(int index, Collection<? extends Boolean> c) {
return false;
}
@Override
public void clear() {
}
@Override
public Boolean get(int index) {
return null;
}
@Override
public void add(int index, Boolean element) {
}
@Override
public void addByte(byte value) {
}
@Override
public byte getByte(int index) {
return 0;
}
@Override
public byte[] toByteArray() {
return new byte[0];
}
@Override
public byte[] finalizeAsByteArray() {
byte[] res = data;
data = null;
AbstractBaseList.UNSAFE.putInt(data, AbstractBaseList.OFFSET_LEN, size);
return res;
}
}

View File

@ -0,0 +1,79 @@
package dev.asdf00.general.utils.listOld;
import dev.asdf00.general.utils.listOld.internal.PrimitiveListImpl;
import sun.misc.Unsafe;
import java.lang.reflect.Field;
import java.util.List;
import java.util.RandomAccess;
public abstract class PrimitiveList<T> implements List<T>, RandomAccess {
private static final Unsafe UNSAFE;
static {
try {
Field f = Unsafe.class.getDeclaredField("theUnsafe");
f.setAccessible(true);
UNSAFE = (Unsafe) f.get(null);
} catch (NoSuchFieldException | IllegalAccessException e) {
throw new RuntimeException(e);
}
}
public static boolean isSupportedOnCurrentJVMConfiguration() {
return UNSAFE.arrayIndexScale(byte[].class) == 1 && UNSAFE.arrayIndexScale(short[].class) == 2 &&
UNSAFE.arrayIndexScale(int[].class) == 4 && UNSAFE.arrayIndexScale(long[].class) == 8 &&
UNSAFE.arrayIndexScale(float[].class) == 4 && UNSAFE.arrayIndexScale(double[].class) == 8;
}
public static <T> PrimitiveList<T> create(Class<T> boxedType) throws IllegalArgumentException, UnsupportedOperationException {
return PrimitiveListImpl.create(boxedType);
}
public abstract byte[] toByteArray();
public abstract short[] toShortArray();
public abstract int[] toIntArray();
public abstract long[] toLongArray();
public abstract float[] toFloatArray();
public abstract double[] toDoubleArray();
public abstract char[] toCharArray();
public abstract boolean[] toBooleanArray();
public abstract byte getByte(int index);
public abstract short getShort(int index);
public abstract int getInt(int index);
public abstract long getLong(int index);
public abstract float getFloat(int index);
public abstract double getDouble(int index);
public abstract char getChar(int index);
public abstract boolean getBoolean(int index);
public abstract byte[] finalizeAsByteArray();
public abstract short[] finalizeAsShortArray();
public abstract int[] finalizeAsIntArray();
public abstract long[] finalizeAsLongArray();
public abstract float[] finalizeAsFloatArray();
public abstract double[] finalizeAsDoubleArray();
public abstract boolean isFinalized();
}

File diff suppressed because it is too large Load Diff

5
src/module-info.java Normal file
View File

@ -0,0 +1,5 @@
module GeneralUtils {
requires jdk.unsupported;
exports dev.asdf00.general.utils.listOld;
exports dev.asdf00.general.utils.list;
}