Skip to content

Commit b5eb482

Browse files
committed
Replace useages with Vec
1 parent 2c62eb0 commit b5eb482

File tree

15 files changed

+109
-72
lines changed

15 files changed

+109
-72
lines changed

src/main/java/dev/mv/utilsx/Test.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,16 @@
66
@SuppressWarnings("unchecked")
77
public class Test {
88
public static void main(String[] args) {
9+
Vec<Integer> vec = new Vec<>();
910

11+
vec.append(new Integer[]{ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 });
12+
System.out.println(vec);
13+
14+
vec = vec.iter().filterMap(i -> {
15+
if (i > 7) return Option.none();
16+
return Option.some(i * 2);
17+
}).collect();
18+
19+
System.out.println(vec);
1020
}
1121
}

src/main/java/dev/mv/utilsx/UtilsX.java

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import dev.mv.utilsx.check.Match;
44
import dev.mv.utilsx.check.MatchReturn;
5+
import dev.mv.utilsx.collection.Vec;
56
import dev.mv.utilsx.generic.Option;
67
import dev.mv.utilsx.misc.ClassFinder;
78
import dev.mv.utilsx.nullHandler.NullHandler;
@@ -225,6 +226,20 @@ public static <T> List<T> merge(List<T>... lists) {
225226
return mergedList;
226227
}
227228

229+
/**
230+
* Merge multiple vecs into one vec, returns the merged vec as an {@link Vec}.
231+
*
232+
* @param vecs The vecs to be merged.
233+
* @return {@link Vec} instance with all the merged vecs.
234+
*/
235+
public static <T> Vec<T> merge(Vec<T>... vecs) {
236+
Vec<T> mergedVec = new Vec<>();
237+
for (Vec<T> vec : vecs) {
238+
mergedVec.append(vec);
239+
}
240+
return mergedVec;
241+
}
242+
228243
/**
229244
* Creates a null check instance on the object, allowing you to execute certain
230245
* code only if the object is null and other code only if it is not null.
@@ -391,9 +406,9 @@ public static <T> T[] repeat(T t, int times) {
391406
* Get all the classes in the current JVM classPath with a filter on the full name of the class.
392407
*
393408
* @param filter the filter, true to add the class, false to not add.
394-
* @return a {@link List} with all the classes as {@link Class<?>} objects.
409+
* @return a {@link Vec} with all the classes as {@link Class<?>} objects.
395410
*/
396-
public static List<Class<?>> getAllClasses(Predicate<String> filter) {
411+
public static Vec<Class<?>> getAllClasses(Predicate<String> filter) {
397412
try {
398413
ClassLoader loader = ClassLoader.getSystemClassLoader();
399414
return ClassFinder.findAllClasses().filter(filter).filterMap(name -> {
@@ -488,6 +503,16 @@ public static <T> T random(List<T> list) {
488503
return list.get(random.nextInt(list.size()));
489504
}
490505

506+
/**
507+
* Returns a random element from the given vec.
508+
*
509+
* @param vec The vec to get a random element from.
510+
* @return A random element from the vec.
511+
*/
512+
public static <T> T random(Vec<T> vec) {
513+
return vec.get(random.nextInt(vec.len()));
514+
}
515+
491516
/**
492517
* Returns a random element from the given array.
493518
*

src/main/java/dev/mv/utilsx/buffer/DynamicByteBuffer.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import dev.mv.utilsx.ArrayUtils;
44
import dev.mv.utilsx.ByteUtils;
5+
import dev.mv.utilsx.collection.Vec;
56
import dev.mv.utilsx.sequence.Sequence;
67

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

561562
public byte[][] splitIntoBytes(int amount) {
562563
DynamicByteBuffer[] parts = splitInto(amount);
563-
return Sequence.from(Arrays.asList(parts)).map(DynamicByteBuffer::array).collect();
564+
return new Vec<>(parts).iter().map(DynamicByteBuffer::array).collect();
564565
}
565566

566567
public DynamicByteBuffer merge(DynamicByteBuffer other) {

src/main/java/dev/mv/utilsx/collection/Vec.java

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,15 @@ public class Vec<T> implements RandomAccess, Iterable<T> {
1717
transient T[] elements;
1818
int length;
1919

20-
public Vec(T... ignore) {
21-
this(0, ignore);
20+
public Vec(T... init) {
21+
elements = init;
22+
length = init.length;
2223
}
2324

24-
public Vec(int capacity, T... ignore) {
25-
elements = (T[]) Array.newInstance(ignore.getClass().componentType(), capacity);
25+
public static <T> Vec<T> withCapacity(int capacity, T... ignore) {
26+
Vec<T> vec = new Vec<>();
27+
vec.elements = (T[]) Array.newInstance(ignore.getClass().componentType(), capacity);
28+
return vec;
2629
}
2730

2831
private void grow(int minAmount) {
@@ -108,6 +111,20 @@ public T pop() {
108111
return element;
109112
}
110113

114+
public T popFirst() {
115+
if (length == 0) return null;
116+
length--;
117+
T element = elements[0];
118+
System.arraycopy(elements, 1, elements, 0, length);
119+
elements[length] = null;
120+
return element;
121+
}
122+
123+
public void clear() {
124+
elements = Arrays.copyOf(elements, 0);
125+
length = 0;
126+
}
127+
111128
public T get(int index) {
112129
if (index >= length) throw new IndexOutOfBoundsException("Index " + index + " is out of bounds for length " + length + "!");
113130
return elements[index];

src/main/java/dev/mv/utilsx/futures/Signal.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,16 @@
11
package dev.mv.utilsx.futures;
22

3+
import dev.mv.utilsx.collection.Vec;
34
import dev.mv.utilsx.generic.Null;
45

56
import java.time.Duration;
6-
import java.util.ArrayList;
7-
import java.util.List;
87
import java.util.concurrent.atomic.AtomicInteger;
98

109
public class Signal implements Wake {
1110

1211
private final Mutex<Boolean> ready = new Mutex<>(false);
1312
private final CondVar condition = new CondVar(ready);
14-
private final List<Waker> wakers = new ArrayList<>(1);
13+
private final Vec<Waker> wakers = new Vec<>();
1514
private final AtomicInteger waiting = new AtomicInteger(0);
1615

1716
public boolean ready() {
@@ -77,7 +76,7 @@ public Poll<Null> poll(Context context) {
7776
signal.ready.lock();
7877
if (signal.ready.getLocked()) return new Poll<>(Null.INSTANCE);
7978
if (!started) {
80-
signal.wakers.add(context.waker());
79+
signal.wakers.push(context.waker());
8180
}
8281
return new Poll<>();
8382
} finally {

src/main/java/dev/mv/utilsx/misc/ClassFinder.java

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,17 @@
11
package dev.mv.utilsx.misc;
22

33
import dev.mv.utilsx.UtilsX;
4+
import dev.mv.utilsx.collection.Vec;
45
import dev.mv.utilsx.sequence.Sequence;
56

67
import java.io.File;
78
import java.io.IOException;
89
import java.net.MalformedURLException;
910
import java.net.URISyntaxException;
1011
import java.net.URL;
11-
import java.util.*;
12+
import java.util.Enumeration;
13+
import java.util.HashSet;
14+
import java.util.Set;
1215
import java.util.jar.Attributes;
1316
import java.util.jar.JarEntry;
1417
import java.util.jar.JarFile;
@@ -17,11 +20,11 @@
1720
@SuppressWarnings("unchecked")
1821
public class ClassFinder {
1922

20-
private static List<String> foundClasses = null;
23+
private static Vec<String> foundClasses = null;
2124

2225

2326
public static Sequence<String> findAllClasses() {
24-
if (foundClasses != null) return Sequence.from(foundClasses);
27+
if (foundClasses != null) return foundClasses.iterCopied();
2528
try {
2629
Set<File> files = new HashSet<>();
2730
Set<String> classes = new HashSet<>();
@@ -40,7 +43,7 @@ public static Sequence<String> findAllClasses() {
4043
findClasses(files, classes);
4144
foundClasses = Sequence.from(classes)
4245
.filter(name -> !UtilsX.containsAny(name, "module-info", "package-info", "META-INF")).collect();
43-
return Sequence.from(foundClasses);
46+
return foundClasses.iterCopied();
4447
} catch (Exception e) {
4548
throw new RuntimeException(e);
4649
}

src/main/java/dev/mv/utilsx/sequence/Cycle.java

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,16 @@
11
package dev.mv.utilsx.sequence;
22

3+
import dev.mv.utilsx.collection.Vec;
34
import dev.mv.utilsx.generic.Option;
4-
5-
import java.util.ArrayList;
6-
import java.util.List;
7-
85
public class Cycle<T> implements Sequence<T> {
96
private Sequence<T> parent;
10-
private List<T> ts;
7+
private Vec<T> ts;
118
private boolean filled;
129
private int idx;
1310

1411
Cycle(Sequence<T> parent) {
1512
this.parent = parent;
16-
ts = new ArrayList<>();
13+
ts = new Vec<>();
1714
}
1815

1916
@Override
@@ -25,11 +22,11 @@ public Option<T> next() {
2522
filled = true;
2623
return Option.some(ts.get(idx++));
2724
}
28-
ts.add(next.getUnchecked());
25+
ts.push(next.getUnchecked());
2926
return next;
3027
}
3128

32-
if (idx >= ts.size()) {
29+
if (idx >= ts.len()) {
3330
idx = 0;
3431
}
3532

src/main/java/dev/mv/utilsx/sequence/MultiPeek.java

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,12 @@
11
package dev.mv.utilsx.sequence;
22

3+
import dev.mv.utilsx.collection.Vec;
34
import dev.mv.utilsx.generic.Option;
45

5-
import java.util.ArrayList;
6-
import java.util.List;
7-
86
public class MultiPeek<T> implements Sequence<T> {
97

108
private Sequence<T> parent;
11-
private List<T> peeked = new ArrayList<>();
9+
private Vec<T> peeked = new Vec<>();
1210
private int current = 0;
1311

1412
public MultiPeek(Sequence<T> parent) {
@@ -19,19 +17,19 @@ public MultiPeek(Sequence<T> parent) {
1917
public Option<T> next() {
2018
if (!peeked.isEmpty()) {
2119
current--;
22-
return Option.some(peeked.removeFirst());
20+
return Option.some(peeked.popFirst());
2321
}
2422
return parent.next();
2523
}
2624

2725
public Option<T> peek() {
28-
if (peeked.size() > current) {
26+
if (peeked.len() > current) {
2927
return Option.some(peeked.get(current++));
3028
}
3129
var next = parent.next();
3230
if (next.isNone()) return Option.none();
3331
current++;
34-
peeked.add(next.getUnchecked());
32+
peeked.push(next.getUnchecked());
3533
return next;
3634
}
3735

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,12 @@
11
package dev.mv.utilsx.sequence;
22

3+
import dev.mv.utilsx.collection.Vec;
34
import dev.mv.utilsx.generic.Option;
45

5-
import java.util.ArrayList;
6-
import java.util.List;
7-
86
public class PutBackN<T> implements Sequence<T> {
97

108
private Sequence<T> parent;
11-
private List<T> putBack = new ArrayList<>();;
9+
private Vec<T> putBack = new Vec<>();;
1210

1311
public PutBackN(Sequence<T> parent) {
1412
this.parent = parent;
@@ -17,13 +15,13 @@ public PutBackN(Sequence<T> parent) {
1715
@Override
1816
public Option<T> next() {
1917
if (!putBack.isEmpty()) {
20-
return Option.some(putBack.removeLast());
18+
return Option.some(putBack.pop());
2119
}
2220
return next();
2321
}
2422

2523
public void putBack(T value) {
26-
putBack.add(value);
24+
putBack.push(value);
2725
}
2826

2927
}

src/main/java/dev/mv/utilsx/sequence/Sequence.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -234,9 +234,9 @@ default <B> B collect(B... ignore) {
234234
return (B) list;
235235
}
236236
else if (clazz.isArray()) {
237-
List<T> list = new ArrayList<>();
238-
forEach(list::add);
239-
return (B) list.toArray((T[]) Array.newInstance(clazz.getComponentType(), list.size()));
237+
Vec<T> vec = new Vec<>();
238+
forEach(vec::push);
239+
return (B) vec.toArray();
240240
}
241241
else if (clazz.equals(Vec.class)) {
242242
Vec<T> vec = new Vec<>();

0 commit comments

Comments
 (0)