Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add simple Cursor variant of the DoesItVectorise example #391

Merged
merged 1 commit into from
Oct 13, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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();
}
}