From f8f1a67a61bf9db057b55466dcad1d049d542cc6 Mon Sep 17 00:00:00 2001 From: mike Date: Mon, 12 Jan 2026 07:07:49 +0100 Subject: [PATCH] introduce bitloops --- src/main/java/puzzle/SwedishGenerator.java | 38 ++----------------- .../java/puzzle/SwedishGeneratorTest.java | 31 +++++++++++++++ 2 files changed, 34 insertions(+), 35 deletions(-) diff --git a/src/main/java/puzzle/SwedishGenerator.java b/src/main/java/puzzle/SwedishGenerator.java index cfb9927..18a4325 100644 --- a/src/main/java/puzzle/SwedishGenerator.java +++ b/src/main/java/puzzle/SwedishGenerator.java @@ -175,7 +175,7 @@ public record SwedishGenerator(Rng rng) { this.hi = hi; } static Grid createEmpty() { return new Grid(new byte[SIZE], X, X); } - int digitAt(int index) { return g[index] - 48; } + int digitAt(int index) { return g[index] & ~48; } public static int r(int offset) { return offset & 7; } public static int c(int offset) { return offset >>> 3; } static int offset(int r, int c) { return r | (c << 3); } @@ -344,38 +344,7 @@ public record SwedishGenerator(Rng rng) { } } - static int intersectSorted(int[] a, int aLen, int[] b, int bLen, int[] out) { - if (aLen == 0 || bLen == 0) return 0; - if (aLen < bLen >>> 4) { - int k = 0; - for (int i = 0; i < aLen; i++) { - int x = a[i]; - if (Arrays.binarySearch(b, 0, bLen, x) >= 0) out[k++] = x; - } - return k; - } - if (bLen < aLen >>> 4) { - int k = 0; - for (int i = 0; i < bLen; i++) { - int y = b[i]; - if (Arrays.binarySearch(a, 0, aLen, y) >= 0) out[k++] = y; - } - return k; - } - int i = 0, j = 0, k = 0, x, y; - while (i < aLen && j < bLen) { - x = a[i]; - y = b[j]; - if (x == y) { - out[k++] = x; - i++; - j++; - } else if (x < y) i++; - else j++; - } - return k; - } - + static record Slot(int key, long packedPos) { static final int BIT_FOR_DIR = 3; @@ -428,7 +397,7 @@ public record SwedishGenerator(Rng rng) { for (int i = 0; i < 65; i += 64) { for (long bits = (i == 0 ? lo_cl : hi_cl); bits != X; bits &= bits - 1) { - int clueIdx = i + Long.numberOfTrailingZeros(bits); + int clueIdx = i | Long.numberOfTrailingZeros(bits); var d = grid.digitAt(clueIdx); var nbrs16 = OFFSETS[d]; long packed = nbrs16.path()[clueIdx]; @@ -778,7 +747,6 @@ public record SwedishGenerator(Rng rng) { val multiThreaded = Thread.currentThread().getName().contains("pool"); val grid = mask.deepCopyGrid(); val used = new Bit1029(); - // val assigned = new HashMap(); long[] assigned = new long[BIGG]; val ctx = CTX.get(); diff --git a/src/test/java/puzzle/SwedishGeneratorTest.java b/src/test/java/puzzle/SwedishGeneratorTest.java index 134d9f5..e6b6a26 100644 --- a/src/test/java/puzzle/SwedishGeneratorTest.java +++ b/src/test/java/puzzle/SwedishGeneratorTest.java @@ -161,6 +161,37 @@ public class SwedishGeneratorTest { assertTrue(Slot.horiz(2)); // right assertFalse(Slot.horiz(3)); // down } + static int intersectSorted(int[] a, int aLen, int[] b, int bLen, int[] out) { + if (aLen == 0 || bLen == 0) return 0; + if (aLen < bLen >>> 4) { + int k = 0; + for (int i = 0; i < aLen; i++) { + int x = a[i]; + if (Arrays.binarySearch(b, 0, bLen, x) >= 0) out[k++] = x; + } + return k; + } + if (bLen < aLen >>> 4) { + int k = 0; + for (int i = 0; i < bLen; i++) { + int y = b[i]; + if (Arrays.binarySearch(a, 0, aLen, y) >= 0) out[k++] = y; + } + return k; + } + int i = 0, j = 0, k = 0, x, y; + while (i < aLen && j < bLen) { + x = a[i]; + y = b[j]; + if (x == y) { + out[k++] = x; + i++; + j++; + } else if (x < y) i++; + else j++; + } + return k; + } @Test void testIntersectSorted() {