[GRASS-SVN] r70686 - grass/trunk/lib/calc

svn_grass at osgeo.org svn_grass at osgeo.org
Sun Feb 26 13:19:08 PST 2017


Author: mmetz
Date: 2017-02-26 13:19:08 -0800 (Sun, 26 Feb 2017)
New Revision: 70686

Modified:
   grass/trunk/lib/calc/xnmedian.c
Log:
calclib: optimize nmedian

Modified: grass/trunk/lib/calc/xnmedian.c
===================================================================
--- grass/trunk/lib/calc/xnmedian.c	2017-02-26 14:14:52 UTC (rev 70685)
+++ grass/trunk/lib/calc/xnmedian.c	2017-02-26 21:19:08 UTC (rev 70686)
@@ -68,8 +68,8 @@
 	    CELL *res = args[0];
 	    CELL **argv = (CELL **) &args[1];
 	    CELL *a = array;
-	    CELL *a1;
-	    CELL *a2;
+	    CELL a1;
+	    CELL *resc;
 
 	    for (i = 0; i < columns; i++) {
 		int n = 0;
@@ -80,13 +80,18 @@
 		    a[n++] = argv[j][i];
 		}
 
+		resc = &res[i];
+
 		if (!n)
-		    SET_NULL_C(&res[i]);
+		    SET_NULL_C(resc);
 		else {
 		    qsort(a, n, sizeof(CELL), icmp);
-		    a1 = &a[(n - 1) / 2];
-		    a2 = &a[n / 2];
-		    res[i] = (*a1 + *a2) / 2;
+		    *resc = a[n / 2];
+		    if ((n & 1) == 0) {
+			a1 = a[(n - 1) / 2];
+			if (*resc != a1)
+			    *resc = (*resc + a1) / 2;
+		    }
 		}
 	    }
 
@@ -97,8 +102,8 @@
 	    FCELL *res = args[0];
 	    FCELL **argv = (FCELL **) &args[1];
 	    FCELL *a = array;
-	    FCELL *a1;
-	    FCELL *a2;
+	    FCELL a1;
+	    FCELL *resc;
 
 	    for (i = 0; i < columns; i++) {
 		int n = 0;
@@ -109,13 +114,18 @@
 		    a[n++] = argv[j][i];
 		}
 
+		resc = &res[i];
+
 		if (!n)
-		    SET_NULL_F(&res[i]);
+		    SET_NULL_F(resc);
 		else {
 		    qsort(a, n, sizeof(FCELL), fcmp);
-		    a1 = &a[(n - 1) / 2];
-		    a2 = &a[n / 2];
-		    res[i] = (*a1 + *a2) / 2;
+		    *resc = a[n / 2];
+		    if ((n & 1) == 0) {
+			a1 = a[(n - 1) / 2];
+			if (*resc != a1)
+			    *resc = (*resc + a1) / 2;
+		    }
 		}
 	    }
 
@@ -126,8 +136,8 @@
 	    DCELL *res = args[0];
 	    DCELL **argv = (DCELL **) &args[1];
 	    DCELL *a = array;
-	    DCELL *a1;
-	    DCELL *a2;
+	    DCELL a1;
+	    DCELL *resc;
 
 	    for (i = 0; i < columns; i++) {
 		int n = 0;
@@ -138,13 +148,18 @@
 		    a[n++] = argv[j][i];
 		}
 
+		resc = &res[i];
+
 		if (!n)
-		    SET_NULL_D(&res[i]);
+		    SET_NULL_D(resc);
 		else {
 		    qsort(a, n, sizeof(DCELL), dcmp);
-		    a1 = &a[(n - 1) / 2];
-		    a2 = &a[n / 2];
-		    res[i] = (*a1 + *a2) / 2;
+		    *resc = a[n / 2];
+		    if ((n & 1) == 0) {
+			a1 = a[(n - 1) / 2];
+			if (*resc != a1)
+			    *resc = (*resc + a1) / 2;
+		    }
 		}
 	    }
 



More information about the grass-commit mailing list