Skip to content

Commit

Permalink
Merge pull request #391 from rkraneis/rk-valhalla-examples
Browse files Browse the repository at this point in the history
Add simple Cursor variant of the DoesItVectorise example
  • Loading branch information
chriswhocodes authored Oct 13, 2024
2 parents 8234257 + 8a9acd5 commit 03c2796
Showing 1 changed file with 57 additions and 0 deletions.
57 changes: 57 additions & 0 deletions core/src/main/resources/examples/DoesItVectoriseValue.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
public class DoesItVectoriseValue
{
public DoesItVectoriseValue()
{
int[] array = new int[1024];

for (int i = 0; i < 1_000_000; i++)
{
incrementArray(array, 1);
}

for (int i = 0; i < array.length; i++)
{
System.out.println(array[i]);
}
}

public void incrementArray(int[] array, int constant)
{
int length = array.length;

for (Cursor c = Cursor.of(length); c.canAdvance(); c = c.advance())
{
array[c.position] += constant;
}
}

public value record Cursor(int position, int length)
{
public Cursor {
if (length < 0 || position > length)
{
throw new IllegalArgumentException();
}
}

public static Cursor of(int length)
{
return new Cursor(0, length);
}

public boolean canAdvance()
{
return position < length;
}

public Cursor advance()
{
return new Cursor(position + 1, length);
}
}

public static void main(String[] args)
{
new DoesItVectoriseValue();
}
}

0 comments on commit 03c2796

Please sign in to comment.