From b5eb4826eb3387eb99861c81469615b8c241d6f0 Mon Sep 17 00:00:00 2001 From: Maxim Savenkov Date: Sun, 21 Jan 2024 23:11:54 +0100 Subject: [PATCH] Replace useages with Vec --- src/main/java/dev/mv/utilsx/Test.java | 10 +++++++ src/main/java/dev/mv/utilsx/UtilsX.java | 29 +++++++++++++++++-- .../mv/utilsx/buffer/DynamicByteBuffer.java | 3 +- .../java/dev/mv/utilsx/collection/Vec.java | 25 +++++++++++++--- .../java/dev/mv/utilsx/futures/Signal.java | 7 ++--- .../java/dev/mv/utilsx/misc/ClassFinder.java | 11 ++++--- .../java/dev/mv/utilsx/sequence/Cycle.java | 13 ++++----- .../dev/mv/utilsx/sequence/MultiPeek.java | 12 ++++---- .../java/dev/mv/utilsx/sequence/PutBackN.java | 10 +++---- .../java/dev/mv/utilsx/sequence/Sequence.java | 6 ++-- .../mv/utilsx/sequence/integer/IntCycle.java | 14 ++++----- .../{IntMultiPeak.java => IntMultiPeek.java} | 18 +++++------- .../{IntPeakable.java => IntPeekable.java} | 4 +-- .../utilsx/sequence/integer/IntPutBackN.java | 11 +++---- .../utilsx/sequence/integer/IntSequence.java | 8 ++--- 15 files changed, 109 insertions(+), 72 deletions(-) rename src/main/java/dev/mv/utilsx/sequence/integer/{IntMultiPeak.java => IntMultiPeek.java} (58%) rename src/main/java/dev/mv/utilsx/sequence/integer/{IntPeakable.java => IntPeekable.java} (82%) diff --git a/src/main/java/dev/mv/utilsx/Test.java b/src/main/java/dev/mv/utilsx/Test.java index 0c353f0..79eddfb 100644 --- a/src/main/java/dev/mv/utilsx/Test.java +++ b/src/main/java/dev/mv/utilsx/Test.java @@ -6,6 +6,16 @@ @SuppressWarnings("unchecked") public class Test { public static void main(String[] args) { + Vec 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); } } diff --git a/src/main/java/dev/mv/utilsx/UtilsX.java b/src/main/java/dev/mv/utilsx/UtilsX.java index f4d2247..75b37ff 100644 --- a/src/main/java/dev/mv/utilsx/UtilsX.java +++ b/src/main/java/dev/mv/utilsx/UtilsX.java @@ -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; @@ -225,6 +226,20 @@ public static List merge(List... 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 Vec merge(Vec... vecs) { + Vec mergedVec = new Vec<>(); + for (Vec 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. @@ -391,9 +406,9 @@ public static 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> getAllClasses(Predicate filter) { + public static Vec> getAllClasses(Predicate filter) { try { ClassLoader loader = ClassLoader.getSystemClassLoader(); return ClassFinder.findAllClasses().filter(filter).filterMap(name -> { @@ -488,6 +503,16 @@ public static T random(List 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 random(Vec vec) { + return vec.get(random.nextInt(vec.len())); + } + /** * Returns a random element from the given array. * diff --git a/src/main/java/dev/mv/utilsx/buffer/DynamicByteBuffer.java b/src/main/java/dev/mv/utilsx/buffer/DynamicByteBuffer.java index 8387ff8..1772f82 100644 --- a/src/main/java/dev/mv/utilsx/buffer/DynamicByteBuffer.java +++ b/src/main/java/dev/mv/utilsx/buffer/DynamicByteBuffer.java @@ -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; @@ -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) { diff --git a/src/main/java/dev/mv/utilsx/collection/Vec.java b/src/main/java/dev/mv/utilsx/collection/Vec.java index bb00351..1671def 100644 --- a/src/main/java/dev/mv/utilsx/collection/Vec.java +++ b/src/main/java/dev/mv/utilsx/collection/Vec.java @@ -17,12 +17,15 @@ public class Vec implements RandomAccess, Iterable { 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 Vec withCapacity(int capacity, T... ignore) { + Vec vec = new Vec<>(); + vec.elements = (T[]) Array.newInstance(ignore.getClass().componentType(), capacity); + return vec; } private void grow(int minAmount) { @@ -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]; diff --git a/src/main/java/dev/mv/utilsx/futures/Signal.java b/src/main/java/dev/mv/utilsx/futures/Signal.java index 091dcf6..4672f54 100644 --- a/src/main/java/dev/mv/utilsx/futures/Signal.java +++ b/src/main/java/dev/mv/utilsx/futures/Signal.java @@ -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 ready = new Mutex<>(false); private final CondVar condition = new CondVar(ready); - private final List wakers = new ArrayList<>(1); + private final Vec wakers = new Vec<>(); private final AtomicInteger waiting = new AtomicInteger(0); public boolean ready() { @@ -77,7 +76,7 @@ public Poll 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 { diff --git a/src/main/java/dev/mv/utilsx/misc/ClassFinder.java b/src/main/java/dev/mv/utilsx/misc/ClassFinder.java index 568eae2..85e1c4f 100644 --- a/src/main/java/dev/mv/utilsx/misc/ClassFinder.java +++ b/src/main/java/dev/mv/utilsx/misc/ClassFinder.java @@ -1,6 +1,7 @@ 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; @@ -8,7 +9,9 @@ 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; @@ -17,11 +20,11 @@ @SuppressWarnings("unchecked") public class ClassFinder { - private static List foundClasses = null; + private static Vec foundClasses = null; public static Sequence findAllClasses() { - if (foundClasses != null) return Sequence.from(foundClasses); + if (foundClasses != null) return foundClasses.iterCopied(); try { Set files = new HashSet<>(); Set classes = new HashSet<>(); @@ -40,7 +43,7 @@ public static Sequence 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); } diff --git a/src/main/java/dev/mv/utilsx/sequence/Cycle.java b/src/main/java/dev/mv/utilsx/sequence/Cycle.java index 52ab5bb..c845fbe 100644 --- a/src/main/java/dev/mv/utilsx/sequence/Cycle.java +++ b/src/main/java/dev/mv/utilsx/sequence/Cycle.java @@ -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 implements Sequence { private Sequence parent; - private List ts; + private Vec ts; private boolean filled; private int idx; Cycle(Sequence parent) { this.parent = parent; - ts = new ArrayList<>(); + ts = new Vec<>(); } @Override @@ -25,11 +22,11 @@ public Option 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; } diff --git a/src/main/java/dev/mv/utilsx/sequence/MultiPeek.java b/src/main/java/dev/mv/utilsx/sequence/MultiPeek.java index 8402ef4..4d4b8ec 100644 --- a/src/main/java/dev/mv/utilsx/sequence/MultiPeek.java +++ b/src/main/java/dev/mv/utilsx/sequence/MultiPeek.java @@ -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 implements Sequence { private Sequence parent; - private List peeked = new ArrayList<>(); + private Vec peeked = new Vec<>(); private int current = 0; public MultiPeek(Sequence parent) { @@ -19,19 +17,19 @@ public MultiPeek(Sequence parent) { public Option next() { if (!peeked.isEmpty()) { current--; - return Option.some(peeked.removeFirst()); + return Option.some(peeked.popFirst()); } return parent.next(); } public Option 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; } diff --git a/src/main/java/dev/mv/utilsx/sequence/PutBackN.java b/src/main/java/dev/mv/utilsx/sequence/PutBackN.java index ab65791..2bccd37 100644 --- a/src/main/java/dev/mv/utilsx/sequence/PutBackN.java +++ b/src/main/java/dev/mv/utilsx/sequence/PutBackN.java @@ -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 implements Sequence { private Sequence parent; - private List putBack = new ArrayList<>();; + private Vec putBack = new Vec<>();; public PutBackN(Sequence parent) { this.parent = parent; @@ -17,13 +15,13 @@ public PutBackN(Sequence parent) { @Override public Option 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); } } diff --git a/src/main/java/dev/mv/utilsx/sequence/Sequence.java b/src/main/java/dev/mv/utilsx/sequence/Sequence.java index 5ec92a9..bb482df 100644 --- a/src/main/java/dev/mv/utilsx/sequence/Sequence.java +++ b/src/main/java/dev/mv/utilsx/sequence/Sequence.java @@ -234,9 +234,9 @@ default B collect(B... ignore) { return (B) list; } else if (clazz.isArray()) { - List list = new ArrayList<>(); - forEach(list::add); - return (B) list.toArray((T[]) Array.newInstance(clazz.getComponentType(), list.size())); + Vec vec = new Vec<>(); + forEach(vec::push); + return (B) vec.toArray(); } else if (clazz.equals(Vec.class)) { Vec vec = new Vec<>(); diff --git a/src/main/java/dev/mv/utilsx/sequence/integer/IntCycle.java b/src/main/java/dev/mv/utilsx/sequence/integer/IntCycle.java index ffb407c..4f91a09 100644 --- a/src/main/java/dev/mv/utilsx/sequence/integer/IntCycle.java +++ b/src/main/java/dev/mv/utilsx/sequence/integer/IntCycle.java @@ -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 ts; + private Vec ts; private boolean filled; private int idx; public IntCycle(IntSequence parent) { this.parent = parent; - ts = new ArrayList<>(); + ts = new Vec<>(); } @Override @@ -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; } diff --git a/src/main/java/dev/mv/utilsx/sequence/integer/IntMultiPeak.java b/src/main/java/dev/mv/utilsx/sequence/integer/IntMultiPeek.java similarity index 58% rename from src/main/java/dev/mv/utilsx/sequence/integer/IntMultiPeak.java rename to src/main/java/dev/mv/utilsx/sequence/integer/IntMultiPeek.java index 5c6d0e9..a00fa39 100644 --- a/src/main/java/dev/mv/utilsx/sequence/integer/IntMultiPeak.java +++ b/src/main/java/dev/mv/utilsx/sequence/integer/IntMultiPeek.java @@ -1,28 +1,24 @@ package dev.mv.utilsx.sequence.integer; -import dev.mv.utilsx.generic.Option; -import dev.mv.utilsx.sequence.Sequence; +import dev.mv.utilsx.collection.Vec; -import java.util.ArrayList; -import java.util.List; - -public class IntMultiPeak implements IntSequence{ +public class IntMultiPeek implements IntSequence{ private IntSequence parent; - private List peeked = new ArrayList<>(); + private Vec peeked = new Vec<>(); private int current = 0; - public IntMultiPeak(IntSequence parent) { + public IntMultiPeek(IntSequence parent) { this.parent = parent; } public IntOption peek() { - if (peeked.size() > current) { + if (peeked.len() > current) { return IntOption.some(peeked.get(current++)); } var next = parent.next(); if (next.isNone()) return IntOption.none(); current++; - peeked.add(next.getUnchecked()); + peeked.push(next.getUnchecked()); return next; } @@ -34,7 +30,7 @@ public void resetPeek() { public IntOption next() { if (!peeked.isEmpty()) { current--; - return IntOption.some(peeked.removeFirst()); + return IntOption.some(peeked.popFirst()); } return parent.next(); } diff --git a/src/main/java/dev/mv/utilsx/sequence/integer/IntPeakable.java b/src/main/java/dev/mv/utilsx/sequence/integer/IntPeekable.java similarity index 82% rename from src/main/java/dev/mv/utilsx/sequence/integer/IntPeakable.java rename to src/main/java/dev/mv/utilsx/sequence/integer/IntPeekable.java index 661c461..51b51de 100644 --- a/src/main/java/dev/mv/utilsx/sequence/integer/IntPeakable.java +++ b/src/main/java/dev/mv/utilsx/sequence/integer/IntPeekable.java @@ -1,10 +1,10 @@ package dev.mv.utilsx.sequence.integer; -public class IntPeakable implements IntSequence{ +public class IntPeekable implements IntSequence{ private IntSequence parent; private IntOption buffer; - public IntPeakable(IntSequence parent) { + public IntPeekable(IntSequence parent) { this.parent = parent; } diff --git a/src/main/java/dev/mv/utilsx/sequence/integer/IntPutBackN.java b/src/main/java/dev/mv/utilsx/sequence/integer/IntPutBackN.java index 44f0efa..f6185ff 100644 --- a/src/main/java/dev/mv/utilsx/sequence/integer/IntPutBackN.java +++ b/src/main/java/dev/mv/utilsx/sequence/integer/IntPutBackN.java @@ -1,13 +1,10 @@ package dev.mv.utilsx.sequence.integer; -import dev.mv.utilsx.generic.Option; - -import java.util.ArrayList; -import java.util.List; +import dev.mv.utilsx.collection.Vec; public class IntPutBackN implements IntSequence{ private IntSequence parent; - private List putBack = new ArrayList<>(); + private Vec putBack = new Vec<>(); public IntPutBackN(IntSequence parent) { this.parent = parent; @@ -16,12 +13,12 @@ public IntPutBackN(IntSequence parent) { @Override public IntOption next() { if (!putBack.isEmpty()) { - return IntOption.some(putBack.removeLast()); + return IntOption.some(putBack.pop()); } return next(); } public void putBack(int value) { - putBack.add(value); + putBack.push(value); } } diff --git a/src/main/java/dev/mv/utilsx/sequence/integer/IntSequence.java b/src/main/java/dev/mv/utilsx/sequence/integer/IntSequence.java index 2c9860d..75c24e7 100644 --- a/src/main/java/dev/mv/utilsx/sequence/integer/IntSequence.java +++ b/src/main/java/dev/mv/utilsx/sequence/integer/IntSequence.java @@ -71,12 +71,12 @@ default IntWindows windows(int size) { return new IntWindows(this, size); } - default IntPeakable peak() { - return new IntPeakable(this); + default IntPeekable peekable() { + return new IntPeekable(this); } - default IntMultiPeak multiPeak() { - return new IntMultiPeak(this); + default IntMultiPeek multiPeek() { + return new IntMultiPeek(this); } default IntMapWhile mapWhile(IntFunction> mapper) {