Skip to content

Commit

Permalink
Replace useages with Vec
Browse files Browse the repository at this point in the history
  • Loading branch information
mqxf committed Jan 21, 2024
1 parent 2c62eb0 commit b5eb482
Show file tree
Hide file tree
Showing 15 changed files with 109 additions and 72 deletions.
10 changes: 10 additions & 0 deletions src/main/java/dev/mv/utilsx/Test.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,16 @@
@SuppressWarnings("unchecked")
public class Test {
public static void main(String[] args) {
Vec<Integer> vec = new Vec<>();

vec.append(new Integer[]{ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 });
System.out.println(vec);

vec = vec.iter().filterMap(i -> {
if (i > 7) return Option.none();
return Option.some(i * 2);
}).collect();

System.out.println(vec);
}
}
29 changes: 27 additions & 2 deletions src/main/java/dev/mv/utilsx/UtilsX.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import dev.mv.utilsx.check.Match;
import dev.mv.utilsx.check.MatchReturn;
import dev.mv.utilsx.collection.Vec;
import dev.mv.utilsx.generic.Option;
import dev.mv.utilsx.misc.ClassFinder;
import dev.mv.utilsx.nullHandler.NullHandler;
Expand Down Expand Up @@ -225,6 +226,20 @@ public static <T> List<T> merge(List<T>... lists) {
return mergedList;
}

/**
* Merge multiple vecs into one vec, returns the merged vec as an {@link Vec}.
*
* @param vecs The vecs to be merged.
* @return {@link Vec} instance with all the merged vecs.
*/
public static <T> Vec<T> merge(Vec<T>... vecs) {
Vec<T> mergedVec = new Vec<>();
for (Vec<T> vec : vecs) {
mergedVec.append(vec);
}
return mergedVec;
}

/**
* Creates a null check instance on the object, allowing you to execute certain
* code only if the object is null and other code only if it is not null.
Expand Down Expand Up @@ -391,9 +406,9 @@ public static <T> T[] repeat(T t, int times) {
* Get all the classes in the current JVM classPath with a filter on the full name of the class.
*
* @param filter the filter, true to add the class, false to not add.
* @return a {@link List} with all the classes as {@link Class<?>} objects.
* @return a {@link Vec} with all the classes as {@link Class<?>} objects.
*/
public static List<Class<?>> getAllClasses(Predicate<String> filter) {
public static Vec<Class<?>> getAllClasses(Predicate<String> filter) {
try {
ClassLoader loader = ClassLoader.getSystemClassLoader();
return ClassFinder.findAllClasses().filter(filter).filterMap(name -> {
Expand Down Expand Up @@ -488,6 +503,16 @@ public static <T> T random(List<T> list) {
return list.get(random.nextInt(list.size()));
}

/**
* Returns a random element from the given vec.
*
* @param vec The vec to get a random element from.
* @return A random element from the vec.
*/
public static <T> T random(Vec<T> vec) {
return vec.get(random.nextInt(vec.len()));
}

/**
* Returns a random element from the given array.
*
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/dev/mv/utilsx/buffer/DynamicByteBuffer.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import dev.mv.utilsx.ArrayUtils;
import dev.mv.utilsx.ByteUtils;
import dev.mv.utilsx.collection.Vec;
import dev.mv.utilsx.sequence.Sequence;

import java.nio.charset.Charset;
Expand Down Expand Up @@ -560,7 +561,7 @@ public DynamicByteBuffer[] splitInto(int amount) {

public byte[][] splitIntoBytes(int amount) {
DynamicByteBuffer[] parts = splitInto(amount);
return Sequence.from(Arrays.asList(parts)).map(DynamicByteBuffer::array).collect();
return new Vec<>(parts).iter().map(DynamicByteBuffer::array).collect();
}

public DynamicByteBuffer merge(DynamicByteBuffer other) {
Expand Down
25 changes: 21 additions & 4 deletions src/main/java/dev/mv/utilsx/collection/Vec.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,15 @@ public class Vec<T> implements RandomAccess, Iterable<T> {
transient T[] elements;
int length;

public Vec(T... ignore) {
this(0, ignore);
public Vec(T... init) {
elements = init;
length = init.length;
}

public Vec(int capacity, T... ignore) {
elements = (T[]) Array.newInstance(ignore.getClass().componentType(), capacity);
public static <T> Vec<T> withCapacity(int capacity, T... ignore) {
Vec<T> vec = new Vec<>();
vec.elements = (T[]) Array.newInstance(ignore.getClass().componentType(), capacity);
return vec;
}

private void grow(int minAmount) {
Expand Down Expand Up @@ -108,6 +111,20 @@ public T pop() {
return element;
}

public T popFirst() {
if (length == 0) return null;
length--;
T element = elements[0];
System.arraycopy(elements, 1, elements, 0, length);
elements[length] = null;
return element;
}

public void clear() {
elements = Arrays.copyOf(elements, 0);
length = 0;
}

public T get(int index) {
if (index >= length) throw new IndexOutOfBoundsException("Index " + index + " is out of bounds for length " + length + "!");
return elements[index];
Expand Down
7 changes: 3 additions & 4 deletions src/main/java/dev/mv/utilsx/futures/Signal.java
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
package dev.mv.utilsx.futures;

import dev.mv.utilsx.collection.Vec;
import dev.mv.utilsx.generic.Null;

import java.time.Duration;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;

public class Signal implements Wake {

private final Mutex<Boolean> ready = new Mutex<>(false);
private final CondVar condition = new CondVar(ready);
private final List<Waker> wakers = new ArrayList<>(1);
private final Vec<Waker> wakers = new Vec<>();
private final AtomicInteger waiting = new AtomicInteger(0);

public boolean ready() {
Expand Down Expand Up @@ -77,7 +76,7 @@ public Poll<Null> poll(Context context) {
signal.ready.lock();
if (signal.ready.getLocked()) return new Poll<>(Null.INSTANCE);
if (!started) {
signal.wakers.add(context.waker());
signal.wakers.push(context.waker());
}
return new Poll<>();
} finally {
Expand Down
11 changes: 7 additions & 4 deletions src/main/java/dev/mv/utilsx/misc/ClassFinder.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
package dev.mv.utilsx.misc;

import dev.mv.utilsx.UtilsX;
import dev.mv.utilsx.collection.Vec;
import dev.mv.utilsx.sequence.Sequence;

import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.*;
import java.util.Enumeration;
import java.util.HashSet;
import java.util.Set;
import java.util.jar.Attributes;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;
Expand All @@ -17,11 +20,11 @@
@SuppressWarnings("unchecked")
public class ClassFinder {

private static List<String> foundClasses = null;
private static Vec<String> foundClasses = null;


public static Sequence<String> findAllClasses() {
if (foundClasses != null) return Sequence.from(foundClasses);
if (foundClasses != null) return foundClasses.iterCopied();
try {
Set<File> files = new HashSet<>();
Set<String> classes = new HashSet<>();
Expand All @@ -40,7 +43,7 @@ public static Sequence<String> findAllClasses() {
findClasses(files, classes);
foundClasses = Sequence.from(classes)
.filter(name -> !UtilsX.containsAny(name, "module-info", "package-info", "META-INF")).collect();
return Sequence.from(foundClasses);
return foundClasses.iterCopied();
} catch (Exception e) {
throw new RuntimeException(e);
}
Expand Down
13 changes: 5 additions & 8 deletions src/main/java/dev/mv/utilsx/sequence/Cycle.java
Original file line number Diff line number Diff line change
@@ -1,19 +1,16 @@
package dev.mv.utilsx.sequence;

import dev.mv.utilsx.collection.Vec;
import dev.mv.utilsx.generic.Option;

import java.util.ArrayList;
import java.util.List;

public class Cycle<T> implements Sequence<T> {
private Sequence<T> parent;
private List<T> ts;
private Vec<T> ts;
private boolean filled;
private int idx;

Cycle(Sequence<T> parent) {
this.parent = parent;
ts = new ArrayList<>();
ts = new Vec<>();
}

@Override
Expand All @@ -25,11 +22,11 @@ public Option<T> next() {
filled = true;
return Option.some(ts.get(idx++));
}
ts.add(next.getUnchecked());
ts.push(next.getUnchecked());
return next;
}

if (idx >= ts.size()) {
if (idx >= ts.len()) {
idx = 0;
}

Expand Down
12 changes: 5 additions & 7 deletions src/main/java/dev/mv/utilsx/sequence/MultiPeek.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
package dev.mv.utilsx.sequence;

import dev.mv.utilsx.collection.Vec;
import dev.mv.utilsx.generic.Option;

import java.util.ArrayList;
import java.util.List;

public class MultiPeek<T> implements Sequence<T> {

private Sequence<T> parent;
private List<T> peeked = new ArrayList<>();
private Vec<T> peeked = new Vec<>();
private int current = 0;

public MultiPeek(Sequence<T> parent) {
Expand All @@ -19,19 +17,19 @@ public MultiPeek(Sequence<T> parent) {
public Option<T> next() {
if (!peeked.isEmpty()) {
current--;
return Option.some(peeked.removeFirst());
return Option.some(peeked.popFirst());
}
return parent.next();
}

public Option<T> peek() {
if (peeked.size() > current) {
if (peeked.len() > current) {
return Option.some(peeked.get(current++));
}
var next = parent.next();
if (next.isNone()) return Option.none();
current++;
peeked.add(next.getUnchecked());
peeked.push(next.getUnchecked());
return next;
}

Expand Down
10 changes: 4 additions & 6 deletions src/main/java/dev/mv/utilsx/sequence/PutBackN.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
package dev.mv.utilsx.sequence;

import dev.mv.utilsx.collection.Vec;
import dev.mv.utilsx.generic.Option;

import java.util.ArrayList;
import java.util.List;

public class PutBackN<T> implements Sequence<T> {

private Sequence<T> parent;
private List<T> putBack = new ArrayList<>();;
private Vec<T> putBack = new Vec<>();;

public PutBackN(Sequence<T> parent) {
this.parent = parent;
Expand All @@ -17,13 +15,13 @@ public PutBackN(Sequence<T> parent) {
@Override
public Option<T> next() {
if (!putBack.isEmpty()) {
return Option.some(putBack.removeLast());
return Option.some(putBack.pop());
}
return next();
}

public void putBack(T value) {
putBack.add(value);
putBack.push(value);
}

}
6 changes: 3 additions & 3 deletions src/main/java/dev/mv/utilsx/sequence/Sequence.java
Original file line number Diff line number Diff line change
Expand Up @@ -234,9 +234,9 @@ default <B> B collect(B... ignore) {
return (B) list;
}
else if (clazz.isArray()) {
List<T> list = new ArrayList<>();
forEach(list::add);
return (B) list.toArray((T[]) Array.newInstance(clazz.getComponentType(), list.size()));
Vec<T> vec = new Vec<>();
forEach(vec::push);
return (B) vec.toArray();
}
else if (clazz.equals(Vec.class)) {
Vec<T> vec = new Vec<>();
Expand Down
14 changes: 5 additions & 9 deletions src/main/java/dev/mv/utilsx/sequence/integer/IntCycle.java
Original file line number Diff line number Diff line change
@@ -1,20 +1,16 @@
package dev.mv.utilsx.sequence.integer;

import dev.mv.utilsx.generic.Option;
import dev.mv.utilsx.sequence.Sequence;

import java.util.ArrayList;
import java.util.List;
import dev.mv.utilsx.collection.Vec;

public class IntCycle implements IntSequence{
private IntSequence parent;
private List<Integer> ts;
private Vec<Integer> ts;
private boolean filled;
private int idx;

public IntCycle(IntSequence parent) {
this.parent = parent;
ts = new ArrayList<>();
ts = new Vec<>();
}

@Override
Expand All @@ -26,11 +22,11 @@ public IntOption next() {
filled = true;
return IntOption.some(ts.get(idx++));
}
ts.add(next.getUnchecked());
ts.push(next.getUnchecked());
return next;
}

if (idx >= ts.size()) {
if (idx >= ts.len()) {
idx = 0;
}

Expand Down
Loading

0 comments on commit b5eb482

Please sign in to comment.