Move SSD duplicate-cell merge to typed Kotlin

ober

73223d0403ab36b87efb4cc7bb15ca8de8ea5d0c

diff --git a/.build.yml b/.build.yml
index 5abf74b..8db2d89 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#bc870112a90a626c20a6795579d205fed1b59518"
+  - "https://git.sr.ht/~lisp/jerboa#1388ed603520d35b9228fbdc525294ae496e3635"
   # 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)" = bc870112a90a626c20a6795579d205fed1b59518
-      test "$(git -C ../jerboa rev-parse 'HEAD^{tree}')" = e510a741cc68717b998f02980153702e61bda563
+      test "$(git -C ../jerboa rev-parse HEAD)" = 1388ed603520d35b9228fbdc525294ae496e3635
+      test "$(git -C ../jerboa rev-parse 'HEAD^{tree}')" = 14389e832902111f5a8434d25f3ed6744f1f0112
       JERBOA="chez --libdirs .:../jerboa/lib --script" make test
diff --git a/dependencies.lock.json b/dependencies.lock.json
index c861938..6631f5e 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": "bc870112a90a626c20a6795579d205fed1b59518",
-    "tree": "e510a741cc68717b998f02980153702e61bda563"
+    "commit": "1388ed603520d35b9228fbdc525294ae496e3635",
+    "tree": "14389e832902111f5a8434d25f3ed6744f1f0112"
   },
   "assurance_tools": {
     "osv_scanner": {
diff --git a/scripts/verify-supply-chain.sh b/scripts/verify-supply-chain.sh
index 7925f02..9eb4bd8 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=bc870112a90a626c20a6795579d205fed1b59518 # gitsafe:ignore
-jerboa_tree=e510a741cc68717b998f02980153702e61bda563 # gitsafe:ignore
+jerboa_commit=1388ed603520d35b9228fbdc525294ae496e3635 # gitsafe:ignore
+jerboa_tree=14389e832902111f5a8434d25f3ed6744f1f0112 # 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 c13d708..d28f5de 100644
--- a/templates/ssd-review.ss
+++ b/templates/ssd-review.ss
@@ -33,7 +33,7 @@
                 SsdCell-detector
                 ssdCellCx ssdCellCy ssdCellRect
                 rectOverlapArea rectCenterInside iou adjacent
-                bboxForCells)
+                bboxForCells mergeDuplicateCellsSorted)
         (type Float32)
         (type FloatArray)
         (type Int32)
@@ -179,7 +179,59 @@
                 (for/fold ((y2 (+ (SsdCell-y first) (SsdCell-h first))))
                           ((i (in-range (int32 1) (list-size cells))))
                   (let ((cell (list-ref cells i)))
-                    (maxFloat32 y2 (+ (SsdCell-y cell) (SsdCell-h cell)))))))))))
+                    (maxFloat32 y2 (+ (SsdCell-y cell) (SsdCell-h cell)))))))))
+        (def (ssdCellGeneratedId (index : Int32)) : String
+          (string-append
+            "c"
+            (string-pad-start (int32->string index) (int32 4) #\0)))
+        (def (ssdCellWithId (cell : SsdCell) (id : String)) : SsdCell
+          (make-SsdCell id
+                        (SsdCell-x cell)
+                        (SsdCell-y cell)
+                        (SsdCell-w cell)
+                        (SsdCell-h cell)
+                        (SsdCell-detector cell)))
+        (def (duplicateCellIndex (merged : (MutableList SsdCell))
+                                 (cell : SsdCell)) : Int32
+          (for/fold ((found (int32 -1)))
+                    ((i (in-range (int32 0) (list-size merged))))
+            (if (>= found (int32 0))
+              found
+              (if (>= (iou cell (list-ref merged i)) (float32 0.55))
+                i
+                found))))
+        (def (preferReplacementCell (existing : SsdCell) (candidate : SsdCell)) : Bool
+          (and (not (equal? (SsdCell-detector existing) "android-color"))
+               (equal? (SsdCell-detector candidate) "android-color")))
+        (def (renumberCells (cells : (MutableList SsdCell))) : (MutableList SsdCell)
+          (let ((out (mutable-list-empty SsdCell)))
+            (begin
+              (for/fold ((ignored (int32 0)))
+                        ((i (in-range (int32 0) (list-size cells))))
+                (begin
+                  (mutable-list-add!
+                    out
+                    (ssdCellWithId
+                      (list-ref cells i)
+                      (ssdCellGeneratedId (+ i (int32 1)))))
+                  ignored))
+              out)))
+        (def (mergeDuplicateCellsSorted (cells : (List SsdCell))) : (MutableList SsdCell)
+          (let ((merged (mutable-list-empty SsdCell)))
+            (begin
+              (for/fold ((ignored (int32 0)))
+                        ((i (in-range (int32 0) (list-size cells))))
+                (let ((cell (list-ref cells i)))
+                  (let ((duplicateIndex (duplicateCellIndex merged cell)))
+                    (begin
+                      (if (>= duplicateIndex (int32 0))
+                        (let ((existing (list-ref merged duplicateIndex)))
+                          (if (preferReplacementCell existing cell)
+                            (mutable-list-set! merged duplicateIndex cell)
+                            (mutable-list-set! merged duplicateIndex existing)))
+                        (mutable-list-add! merged cell))
+                      ignored))))
+              (renumberCells merged))))))
 
     (typed-kotlin-file "com/sfb/ssdreview/Component.kt"
       (typed-library (com sfb ssdreview)
@@ -3608,20 +3660,8 @@
        "            .mapNotNull { contourCell(it, edge, width, height) }"
        "    }"
        ""
-       "    private fun mergeDuplicateCells(cells: List<SsdCell>): MutableList<SsdCell> {"
-       "        val merged = mutableListOf<SsdCell>()"
-       "        for (cell in cells.sortedWith(compareBy<SsdCell> { it.y }.thenBy { it.x })) {"
-       "            val duplicateIndex = merged.indexOfFirst { iou(cell, it) >= 0.55f }"
-       "            if (duplicateIndex >= 0) {"
-       "                if (merged[duplicateIndex].detector != \"android-color\" && cell.detector == \"android-color\") {"
-       "                    merged[duplicateIndex] = cell"
-       "                }"
-       "            } else {"
-       "                merged.add(cell)"
-       "            }"
-       "        }"
-       "        return merged.mapIndexed { index, cell -> cell.copy(id = \"c%04d\".format(index + 1)) }.toMutableList()"
-       "    }"
+       "    private fun mergeDuplicateCells(cells: List<SsdCell>): MutableList<SsdCell> ="
+       "        mergeDuplicateCellsSorted(cells.sortedWith(compareBy<SsdCell> { it.y }.thenBy { it.x }))"
        ""
        "}"
        ))