Skip to content

Commit 2c62eb0

Browse files
committed
Vec
1 parent f09b790 commit 2c62eb0

File tree

4 files changed

+249
-33
lines changed

4 files changed

+249
-33
lines changed

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
<groupId>dev.mv</groupId>
88
<artifactId>utilsx</artifactId>
9-
<version>1.0</version>
9+
<version>2.1</version>
1010

1111
<properties>
1212
<maven.compiler.source>21</maven.compiler.source>
Lines changed: 2 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,11 @@
11
package dev.mv.utilsx;
22

3-
import dev.mv.utilsx.generic.Pair;
4-
import dev.mv.utilsx.sequence.Sequence;
5-
6-
import java.util.Arrays;
7-
import java.util.List;
8-
import java.util.Random;
3+
import dev.mv.utilsx.collection.Vec;
4+
import dev.mv.utilsx.generic.Option;
95

106
@SuppressWarnings("unchecked")
117
public class Test {
128
public static void main(String[] args) {
13-
Sequence<Pair<String, String>> seq = Sequence.once(new Pair<>("Hello", "world"));
14-
15-
String str = Sequence
16-
.random()
17-
.bind(65, 122)
18-
.ints()
19-
.take(10)
20-
.map(x -> (char) (int) x)
21-
.collect();
22-
23-
System.out.println(str);
24-
25-
List<List<Integer>> w = Sequence
26-
.random()
27-
.bind(0, 100)
28-
.ints()
29-
.take(10)
30-
.windows(3)
31-
.map(Arrays::asList)
32-
.collect();
33-
34-
//System.out.println(w);
35-
36-
List<Integer> l = Sequence.from(1, 2, 3).cycle().take(10).collect();
37-
//System.out.println(l);
389

39-
Sequence.random().bind(0, 20).ints();
4010
}
4111
}
Lines changed: 238 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,238 @@
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+
}

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

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

3+
import dev.mv.utilsx.collection.Vec;
34
import dev.mv.utilsx.generic.Option;
45
import dev.mv.utilsx.generic.Pair;
56
import dev.mv.utilsx.sequence.integer.IntOption;
67
import dev.mv.utilsx.sequence.integer.IntSequence;
8+
import org.jetbrains.annotations.NotNull;
79

810
import java.lang.reflect.Array;
911
import java.util.*;
@@ -13,6 +15,7 @@
1315
@FunctionalInterface
1416
public interface Sequence<T> extends Iterable<T> {
1517

18+
@NotNull
1619
Option<T> next();
1720

1821
@Override
@@ -235,6 +238,11 @@ else if (clazz.isArray()) {
235238
forEach(list::add);
236239
return (B) list.toArray((T[]) Array.newInstance(clazz.getComponentType(), list.size()));
237240
}
241+
else if (clazz.equals(Vec.class)) {
242+
Vec<T> vec = new Vec<>();
243+
forEach(vec::push);
244+
return (B) vec;
245+
}
238246
else if (clazz.equals(Set.class) || clazz.equals(HashSet.class)) {
239247
Set<T> set = new HashSet<>();
240248
forEach(set::add);

0 commit comments

Comments
 (0)