changeset 1899:ea88b91acf1a

Optimize the map block matching inner loop.
author Matti Hamalainen <ccr@tnsp.org>
date Mon, 06 Nov 2017 04:25:30 +0200
parents 9da7e7526bde
children ed98d2fe4bca
files mapsearch.c
diffstat 1 files changed, 7 insertions(+), 22 deletions(-) [+]
line wrap: on
line diff
--- a/mapsearch.c	Mon Nov 06 04:05:49 2017 +0200
+++ b/mapsearch.c	Mon Nov 06 04:25:30 2017 +0200
@@ -75,7 +75,7 @@
 {
     char *map;
     int mx, my, wx, wy;
-    double accuracy;
+    int accuracy;
     BOOL centered;
 } MAPMatch;
 
@@ -773,35 +773,20 @@
 }
 
 
-double mapMatchBlock(const MapBlock *map, const MapBlock *match, const int ox, const int oy, const BOOL hardDrop)
+int mapMatchBlock(const MapBlock *map, const MapBlock *match, const int ox, const int oy)
 {
-    int n, k;
-
-    n = k = 0;
     for (int y = 0; y < match->height; y++)
     {
         unsigned char *dp = match->data + (y * match->scansize);
         unsigned char *sp = map->data + ((y + oy) * map->scansize) + ox;
         for (int x = 0; x < match->width; x++)
         {
-            const int
-                c2 = sp[x],
-                c1 = dp[x];
-
-            k++;
-
-            if (c1 == 0 || c1 == c2)
-                n++;
-            else
-            if (hardDrop)
+            if (dp[x] != 0 && dp[x] != sp[x])
                 return -1;
         }
     }
 
-    if (k > 0)
-        return ((double) n * 100.0f) / (double) k;
-    else
-        return 0.0f;
+    return 100;
 }
 
 
@@ -1025,8 +1010,8 @@
         for (int ox = 0; ox < info->map->width - pattern->width; ox++)
         {
             // Check for match
-            double accuracy = mapMatchBlock(info->map, pattern, ox, oy, TRUE);
-            if (accuracy > 99.5f)
+            int accuracy = mapMatchBlock(info->map, pattern, ox, oy);
+            if (accuracy > 99)
             {
                 // Okay, add the match to our list
                 MAPMatch *match = &matches[nmatches++];
@@ -1063,7 +1048,7 @@
     {
         MAPMatch *match = &matches[n];
         char *vstr = th_strdup_printf(
-            "[%1.2f,%d,\"%s\",%d,%d,%d,%d]%s",
+            "[%d,%d,\"%s\",%d,%d,%d,%d]%s",
             match->accuracy,
             match->centered,
             match->map,