-
-
Notifications
You must be signed in to change notification settings - Fork 444
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #391 from rkraneis/rk-valhalla-examples
Add simple Cursor variant of the DoesItVectorise example
- Loading branch information
Showing
1 changed file
with
57 additions
and
0 deletions.
There are no files selected for viewing
57 changes: 57 additions & 0 deletions
57
core/src/main/resources/examples/DoesItVectoriseValue.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |