Changeset: 05642b70844a for MonetDB
URL: http://dev.monetdb.org/hg/MonetDB?cmd=changeset;node=05642b70844a
Modified Files:
        gdk/gdk_sample.c
        monetdb5/modules/mal/sample.c
Branch: stratified_sampling
Log Message:

reduce redundant code in weighted sampling


diffs (90 lines):

diff --git a/gdk/gdk_sample.c b/gdk/gdk_sample.c
--- a/gdk/gdk_sample.c
+++ b/gdk/gdk_sample.c
@@ -276,26 +276,18 @@ BATweightedsample(BAT *b, BUN n, BAT *w)
        cnt = BATcount(b);
 
        sample = COLnew(0, TYPE_oid, n, TRANSIENT);
+       if(!sample)
+               goto bailout;
 
-       if(sample == NULL) {
-               if(weights_are_cast)//if weights where converted, delete 
converted BAT
-                       BBPunfix(weights->batCacheid);
-               return NULL;
-       }
        if(n == 0) {
                if(weights_are_cast)
                        BBPunfix(weights->batCacheid);
                return sample;
        }
 
-
        keys = (dbl*) GDKmalloc(sizeof(dbl)*n);
-       if(keys == NULL) {
-               if(weights_are_cast)
-                       BBPunfix(weights->batCacheid);
-               BBPunfix(sample->batCacheid);
-               return NULL;
-       }
+       if(!keys)
+               goto bailout;
 
        oids = (oid *) Tloc(sample, 0);
        w_ptr = (dbl*) Tloc(weights, 0);
@@ -315,12 +307,8 @@ BATweightedsample(BAT *b, BUN n, BAT *w)
                if(w_ptr[j] == 0.0)
                        continue;
                if(w_ptr[j] < 0.0) {
-                       BBPunfix(sample->batCacheid);
-                       GDKfree(keys);
-                       if(weights_are_cast)
-                               BBPunfix(weights->batCacheid);
                        GDKerror("BATsample: w contains negative weights\n");
-                       return NULL;
+                       goto bailout;
                }
                oids[i] = (oid)(j+minoid);
                keys[i] = pow(mtwist_drand(mt_rng),1.0/w_ptr[j]);//TODO cast 
1.0 to dbl?
@@ -328,12 +316,8 @@ BATweightedsample(BAT *b, BUN n, BAT *w)
        }
 
        if(i < n) {
-               BBPunfix(sample->batCacheid);
-               GDKfree(keys);
-               if(weights_are_cast)
-                       BBPunfix(weights->batCacheid);
                GDKerror("BATsample: sample size bigger than number of non-zero 
weights\n");
-               return NULL;
+               goto bailout;
        }
 
        heapify(compKeysGT, SWAP3);
@@ -373,6 +357,15 @@ BATweightedsample(BAT *b, BUN n, BAT *w)
                sample->tseqbase = *(oid *) Tloc(sample, 0);
 
        return sample;
+
+  bailout:
+    if(weights_are_cast && weights)//if weights where converted, delete 
converted BAT
+       BBPunfix(weights->batCacheid);
+    if(keys)
+       GDKfree(keys);
+    if(sample)
+       BBPunfix(sample->batCacheid);
+    return NULL;
 }
 
 
diff --git a/monetdb5/modules/mal/sample.c b/monetdb5/modules/mal/sample.c
--- a/monetdb5/modules/mal/sample.c
+++ b/monetdb5/modules/mal/sample.c
@@ -132,7 +132,7 @@ SAMPLEweighted_dbl(bat *r, bat *b, dbl *
        double pr = *p;
        lng s;
 
-       if ( pr < 0.0 || pr > 1.0 ) {
+       if ( pr < 0.0 || pr > 1.0 ) { /* the sql parser guarantees this */
                throw(MAL, "sample.subweighted", ILLEGAL_ARGUMENT
                                " p should be between 0 and 1.0" );
        } else if (pr == 0) {/* special case */
_______________________________________________
checkin-list mailing list
checkin-list@monetdb.org
https://www.monetdb.org/mailman/listinfo/checkin-list

Reply via email to