Skip to content

Commit 03c2796

Browse files
Merge pull request #391 from rkraneis/rk-valhalla-examples
Add simple Cursor variant of the DoesItVectorise example
2 parents 8234257 + 8a9acd5 commit 03c2796

File tree

1 file changed

+57
-0
lines changed

1 file changed

+57
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
public class DoesItVectoriseValue
2+
{
3+
public DoesItVectoriseValue()
4+
{
5+
int[] array = new int[1024];
6+
7+
for (int i = 0; i < 1_000_000; i++)
8+
{
9+
incrementArray(array, 1);
10+
}
11+
12+
for (int i = 0; i < array.length; i++)
13+
{
14+
System.out.println(array[i]);
15+
}
16+
}
17+
18+
public void incrementArray(int[] array, int constant)
19+
{
20+
int length = array.length;
21+
22+
for (Cursor c = Cursor.of(length); c.canAdvance(); c = c.advance())
23+
{
24+
array[c.position] += constant;
25+
}
26+
}
27+
28+
public value record Cursor(int position, int length)
29+
{
30+
public Cursor {
31+
if (length < 0 || position > length)
32+
{
33+
throw new IllegalArgumentException();
34+
}
35+
}
36+
37+
public static Cursor of(int length)
38+
{
39+
return new Cursor(0, length);
40+
}
41+
42+
public boolean canAdvance()
43+
{
44+
return position < length;
45+
}
46+
47+
public Cursor advance()
48+
{
49+
return new Cursor(position + 1, length);
50+
}
51+
}
52+
53+
public static void main(String[] args)
54+
{
55+
new DoesItVectoriseValue();
56+
}
57+
}

0 commit comments

Comments
 (0)