From 8a9acd5ae34dcf78dbddebc00571d828be63e4ff Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ren=C3=A9=20Kraneis?= Date: Wed, 4 Sep 2024 21:34:12 +0200 Subject: [PATCH] Add simple s Cursor variant of the DoesItVectorise example --- .../examples/DoesItVectoriseValue.java | 57 +++++++++++++++++++ 1 file changed, 57 insertions(+) create mode 100644 core/src/main/resources/examples/DoesItVectoriseValue.java diff --git a/core/src/main/resources/examples/DoesItVectoriseValue.java b/core/src/main/resources/examples/DoesItVectoriseValue.java new file mode 100644 index 00000000..f843a799 --- /dev/null +++ b/core/src/main/resources/examples/DoesItVectoriseValue.java @@ -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(); + } +}