Move SSD adjacency helper to typed Kotlin

ober

24f0256c2e8a18594a1410071a3ac823218603ec

diff --git a/.build.yml b/.build.yml
index 778d40e..77a0c81 100644
--- a/.build.yml
+++ b/.build.yml
@@ -5,7 +5,7 @@ packages:
   - make=4.4.1-r4
 sources:
   # Build dependency: full immutable commit, mirrored in dependencies.lock.json.
-  - "https://git.sr.ht/~lisp/jerboa#031db15b86a0d3f86caf64241531bd9be37013fe"
+  - "https://git.sr.ht/~lisp/jerboa#47ca11b0fc7b75ae33ecccf4fc22cbe4c9bcf8e0"
   # The second source is the build subject selected by the SourceHut submitter.
   - https://git.sr.ht/~lisp/jerboa-android
 tasks:
@@ -14,6 +14,6 @@ tasks:
       test "$(apk info -v chez-scheme)" = chez-scheme-10.3.0-r2
       test "$(apk info -v git)" = git-2.54.0-r0
       test "$(apk info -v make)" = make-4.4.1-r4
-      test "$(git -C ../jerboa rev-parse HEAD)" = 031db15b86a0d3f86caf64241531bd9be37013fe
-      test "$(git -C ../jerboa rev-parse 'HEAD^{tree}')" = 1c3786138585442c273e05a00daaa50c287af6d8
+      test "$(git -C ../jerboa rev-parse HEAD)" = 47ca11b0fc7b75ae33ecccf4fc22cbe4c9bcf8e0
+      test "$(git -C ../jerboa rev-parse 'HEAD^{tree}')" = 123466f97f77704fb16ccc1d98e6c522dbf24575
       JERBOA="chez --libdirs .:../jerboa/lib --script" make test
diff --git a/dependencies.lock.json b/dependencies.lock.json
index 09281ee..2c45d8c 100644
--- a/dependencies.lock.json
+++ b/dependencies.lock.json
@@ -11,8 +11,8 @@
   "generator_runtime": {
     "name": "jerboa",
     "repository": "https://git.sr.ht/~lisp/jerboa",
-    "commit": "031db15b86a0d3f86caf64241531bd9be37013fe",
-    "tree": "1c3786138585442c273e05a00daaa50c287af6d8"
+    "commit": "47ca11b0fc7b75ae33ecccf4fc22cbe4c9bcf8e0",
+    "tree": "123466f97f77704fb16ccc1d98e6c522dbf24575"
   },
   "assurance_tools": {
     "osv_scanner": {
diff --git a/scripts/verify-supply-chain.sh b/scripts/verify-supply-chain.sh
index 1db2a4a..48a67cc 100755
--- a/scripts/verify-supply-chain.sh
+++ b/scripts/verify-supply-chain.sh
@@ -3,8 +3,8 @@ set -eu
 
 repo=$(CDPATH= cd -- "$(dirname -- "$0")/.." && pwd -P)
 lock="$repo/dependencies.lock.json"
-jerboa_commit=031db15b86a0d3f86caf64241531bd9be37013fe # gitsafe:ignore
-jerboa_tree=1c3786138585442c273e05a00daaa50c287af6d8 # gitsafe:ignore
+jerboa_commit=47ca11b0fc7b75ae33ecccf4fc22cbe4c9bcf8e0 # gitsafe:ignore
+jerboa_tree=123466f97f77704fb16ccc1d98e6c522dbf24575 # gitsafe:ignore
 gradle_sha=20f1b1176237254a6fc204d8434196fa11a4cfb387567519c61556e8710aed78
 jdk_macos_sha=8fa1eff40bb637a33613b2ccb8b12c70dc3661cc22cf8e784943715769a05336
 jdk_linux_sha=d8afc263758141a66e0e3aafc321e783f7016696f4eaea067d340a269037d331
diff --git a/templates/ssd-review.ss b/templates/ssd-review.ss
index 7aa6aba..3586d65 100644
--- a/templates/ssd-review.ss
+++ b/templates/ssd-review.ss
@@ -32,7 +32,7 @@
                 SsdCell-h SsdCell-h-set!
                 SsdCell-detector
                 ssdCellCx ssdCellCy ssdCellRect
-                rectOverlapArea rectCenterInside iou)
+                rectOverlapArea rectCenterInside iou adjacent)
         (type Float32)
         (type FloatArray)
         (type Int32)
@@ -94,6 +94,10 @@
                  (<= cx (float-array-ref area (int32 2)))
                  (>= cy (float-array-ref area (int32 1)))
                  (<= cy (float-array-ref area (int32 3))))))
+        (def (maxFloat32 (a : Float32) (b : Float32)) : Float32
+          (if (> a b) a b))
+        (def (minFloat32 (a : Float32) (b : Float32)) : Float32
+          (if (< a b) a b))
         (def (iou (a : SsdCell) (b : SsdCell)) : Float32
           (let ((overlap (rectOverlapArea (ssdCellRect a) (ssdCellRect b))))
             (if (<= overlap (float32 0.0))
@@ -101,7 +105,61 @@
               (/ overlap
                  (- (+ (* (SsdCell-w a) (SsdCell-h a))
                        (* (SsdCell-w b) (SsdCell-h b)))
-                    overlap)))))))
+                    overlap)))))
+        (def (adjacent (a : SsdCell) (b : SsdCell)) : Bool
+          (let ((ax1 (SsdCell-x a))
+                (ay1 (SsdCell-y a))
+                (ax2 (+ (SsdCell-x a) (SsdCell-w a)))
+                (ay2 (+ (SsdCell-y a) (SsdCell-h a)))
+                (bx1 (SsdCell-x b))
+                (by1 (SsdCell-y b))
+                (bx2 (+ (SsdCell-x b) (SsdCell-w b)))
+                (by2 (+ (SsdCell-y b) (SsdCell-h b))))
+            (let ((yOverlap (maxFloat32
+                              (float32 0.0)
+                              (- (minFloat32 ay2 by2)
+                                 (maxFloat32 ay1 by1))))
+                  (xGap (maxFloat32
+                          (float32 0.0)
+                          (maxFloat32 (- bx1 ax2) (- ax1 bx2))))
+                  (xOverlap (maxFloat32
+                              (float32 0.0)
+                              (- (minFloat32 ax2 bx2)
+                                 (maxFloat32 ax1 bx1))))
+                  (yGap (maxFloat32
+                          (float32 0.0)
+                          (maxFloat32 (- by1 ay2) (- ay1 by2))))
+                  (minH (minFloat32 (SsdCell-h a) (SsdCell-h b)))
+                  (minW (minFloat32 (SsdCell-w a) (SsdCell-w b))))
+              (let ((horizontal
+                      (and (>= (* yOverlap (float32 2.0)) minH)
+                           (<= xGap
+                               (maxFloat32
+                                 (float32 4.0)
+                                 (float32
+                                   (float32-round->int32
+                                     (* minW (float32 0.25))))))
+                           (<= (float32-abs (- (SsdCell-h a) (SsdCell-h b)))
+                               (maxFloat32
+                                 (float32 5.0)
+                                 (float32
+                                   (float32-round->int32
+                                     (* minH (float32 0.35))))))))
+                    (vertical
+                      (and (>= (* xOverlap (float32 2.0)) minW)
+                           (<= yGap
+                               (maxFloat32
+                                 (float32 4.0)
+                                 (float32
+                                   (float32-round->int32
+                                     (* minH (float32 0.25))))))
+                           (<= (float32-abs (- (SsdCell-w a) (SsdCell-w b)))
+                               (maxFloat32
+                                 (float32 5.0)
+                                 (float32
+                                   (float32-round->int32
+                                     (* minW (float32 0.35)))))))))
+                (or horizontal vertical)))))))
 
     (typed-kotlin-file "com/sfb/ssdreview/Component.kt"
       (typed-library (com sfb ssdreview)
@@ -3036,30 +3094,6 @@
        "        return merged.mapIndexed { index, cell -> cell.copy(id = \"c%04d\".format(index + 1)) }.toMutableList()"
        "    }"
        ""
-       "    private fun adjacent(a: SsdCell, b: SsdCell): Boolean {"
-       "        val ax1 = a.x"
-       "        val ay1 = a.y"
-       "        val ax2 = a.x + a.w"
-       "        val ay2 = a.y + a.h"
-       "        val bx1 = b.x"
-       "        val by1 = b.y"
-       "        val bx2 = b.x + b.w"
-       "        val by2 = b.y + b.h"
-       "        val yOverlap = max(0f, min(ay2, by2) - max(ay1, by1))"
-       "        val xGap = max(0f, max(bx1 - ax2, ax1 - bx2))"
-       "        val xOverlap = max(0f, min(ax2, bx2) - max(ax1, bx1))"
-       "        val yGap = max(0f, max(by1 - ay2, ay1 - by2))"
-       "        val minH = min(a.h, b.h)"
-       "        val minW = min(a.w, b.w)"
-       "        val horizontal = yOverlap * 2f >= minH &&"
-       "            xGap <= max(4f, (minW * 0.25f).roundToInt().toFloat()) &&"
-       "            abs(a.h - b.h) <= max(5f, (minH * 0.35f).roundToInt().toFloat())"
-       "        val vertical = xOverlap * 2f >= minW &&"
-       "            yGap <= max(4f, (minH * 0.25f).roundToInt().toFloat()) &&"
-       "            abs(a.w - b.w) <= max(5f, (minW * 0.35f).roundToInt().toFloat())"
-       "        return horizontal || vertical"
-       "    }"
-       ""
        "    private fun grayscale(pixels: IntArray): IntArray = IntArray(pixels.size) { index ->"
        "        val pixel = pixels[index]"
        "        val r = (pixel shr 16) and 0xff"