Changeset: 9f16bb98c14a for MonetDB
URL: http://dev.monetdb.org/hg/MonetDB?cmd=changeset;node=9f16bb98c14a
Modified Files:
        gdk/gdk_aggr.c
Branch: default
Log Message:

Extended BATgroupmin/BATgroupmax to work without group BAT parameter.
If the group BAT parameter is NULL, the whole input BAT is considered
a single group with group is 0.


diffs (300 lines):

diff --git a/gdk/gdk_aggr.c b/gdk/gdk_aggr.c
--- a/gdk/gdk_aggr.c
+++ b/gdk/gdk_aggr.c
@@ -1732,32 +1732,55 @@ BATgroupsize(BAT *b, BAT *g, BAT *e, BAT
 #define AGGR_CMP(TYPE, OP)                                             \
        do {                                                            \
                const TYPE *vals = (const TYPE *) Tloc(b, BUNfirst(b)); \
-               for (;;) {                                              \
+               if (g && BATtdense(g)) {                                \
+                       /* single element groups */                     \
                        if (cand) {                                     \
-                               if (cand == candend)                    \
-                                       break;                          \
-                               i = *cand++ - b->hseqbase;              \
-                               if (i >= end)                           \
-                                       break;                          \
+                               while (cand < candend) {                \
+                                       i = *cand++ - b->hseqbase;      \
+                                       if (i >= end)                   \
+                                               break;                  \
+                                       if (!skip_nils ||               \
+                                           vals[i] != TYPE##_nil) {    \
+                                               oids[i] = i + b->hseqbase; \
+                                               nils--;                 \
+                                       }                               \
+                               }                                       \
                        } else {                                        \
-                               i = start++;                            \
-                               if (i == end)                           \
-                                       break;                          \
+                               for (i = start; i < end; i++) {         \
+                                       if (!skip_nils ||               \
+                                           vals[i] != TYPE##_nil) {    \
+                                               oids[i] = i + b->hseqbase; \
+                                               nils--;                 \
+                                       }                               \
+                               }                                       \
                        }                                               \
-                       if (gids == NULL ||                             \
-                           (gids[i] >= min && gids[i] <= max)) {       \
-                               if (gids)                               \
-                                       gid = gids[i] - min;            \
-                               else                                    \
-                                       gid = (oid) i;                  \
-                               if (!skip_nils || vals[i] != TYPE##_nil) { \
-                                       if (oids[gid] == oid_nil) {     \
-                                               oids[gid] = i + b->hseqbase; \
-                                               nils--;                 \
-                                       } else if (vals[oids[gid] - 
b->hseqbase] != TYPE##_nil && \
-                                                  (vals[i] == TYPE##_nil || \
-                                                   OP(vals[i], vals[oids[gid] 
- b->hseqbase]))) \
-                                               oids[gid] = i + b->hseqbase; \
+               } else {                                                \
+                       gid = 0; /* in case gids == NULL */             \
+                       for (;;) {                                      \
+                               if (cand) {                             \
+                                       if (cand == candend)            \
+                                               break;                  \
+                                       i = *cand++ - b->hseqbase;      \
+                                       if (i >= end)                   \
+                                               break;                  \
+                               } else {                                \
+                                       i = start++;                    \
+                                       if (i == end)                   \
+                                               break;                  \
+                               }                                       \
+                               if (gids == NULL ||                     \
+                                   (gids[i] >= min && gids[i] <= max)) { \
+                                       if (gids)                       \
+                                               gid = gids[i] - min;    \
+                                       if (!skip_nils || vals[i] != 
TYPE##_nil) { \
+                                               if (oids[gid] == oid_nil) { \
+                                                       oids[gid] = i + 
b->hseqbase; \
+                                                       nils--;         \
+                                               } else if (vals[oids[gid] - 
b->hseqbase] != TYPE##_nil && \
+                                                          (vals[i] == 
TYPE##_nil || \
+                                                           OP(vals[i], 
vals[oids[gid] - b->hseqbase]))) \
+                                                       oids[gid] = i + 
b->hseqbase; \
+                                       }                               \
                                }                                       \
                        }                                               \
                }                                                       \
@@ -1800,13 +1823,9 @@ BATgroupmin(BAT *b, BAT *g, BAT *e, BAT 
                GDKerror("BATgroupmin: %s\n", err);
                return NULL;
        }
-       if (g == NULL) {
-               GDKerror("BATgroupmin: b and g must be aligned\n");
-               return NULL;
-       }
 
        if (BATcount(b) == 0 || ngrp == 0) {
-               /* trivial: no products, so return bat aligned with g
+               /* trivial: no minimums, so return bat aligned with g
                 * with nil in the tail */
                bn = BATconstant(TYPE_oid, &oid_nil, ngrp);
                BATseqbase(bn, ngrp == 0 ? 0 : min);
@@ -1821,7 +1840,7 @@ BATgroupmin(BAT *b, BAT *g, BAT *e, BAT 
        for (i = 0; i < ngrp; i++)
                oids[i] = oid_nil;
 
-       if (BATtdense(g))
+       if (g == NULL || BATtdense(g))
                gids = NULL;
        else
                gids = (const oid *) Tloc(g, BUNfirst(g) + start);
@@ -1858,35 +1877,59 @@ BATgroupmin(BAT *b, BAT *g, BAT *e, BAT 
        default:
                bi = bat_iterator(b);
 
-               for (;;) {
+               if (g && BATtdense(g)) {
+                       /* single element groups */
                        if (cand) {
-                               if (cand == candend)
-                                       break;
-                               i = *cand++ - b->hseqbase;
-                               if (i >= end)
-                                       break;
+                               while (cand < candend) {
+                                       i = *cand++ - b->hseqbase;
+                                       if (i >= end)
+                                               break;
+                                       if (!skip_nils ||
+                                           (*atomcmp)(BUNtail(bi, i + 
BUNfirst(b)), nil) != 0) {
+                                               oids[i] = i + b->hseqbase;
+                                               nils--;
+                                       }
+                               }
                        } else {
-                               i = start++;
-                               if (i == end)
-                                       break;
+                               for (i = start; i < end; i++) {
+                                       if (!skip_nils ||
+                                           (*atomcmp)(BUNtail(bi, i + 
BUNfirst(b)), nil) != 0) {
+                                               oids[i] = i + b->hseqbase;
+                                               nils--;
+                                       }
+                               }
                        }
-                       if (gids == NULL ||
-                           (gids[i] >= min && gids[i] <= max)) {
-                               const void *v = BUNtail(bi, i + BUNfirst(b));
-                               if (gids)
-                                       gid = gids[i] - min;
-                               else
-                                       gid = (oid) i;
-                               if (!skip_nils || (*atomcmp)(v, nil) != 0) {
-                                       if (oids[gid] == oid_nil) {
-                                               oids[gid] = i + b->hseqbase;
-                                               nils--;
-                                       } else {
-                                               const void *g = BUNtail(bi, 
(BUN) (oids[gid] - b->hseqbase) + BUNfirst(b));
-                                               if ((*atomcmp)(g, nil) != 0 &&
-                                                  ((*atomcmp)(v, nil) == 0 ||
-                                                   LT((*atomcmp)(v, g), 0)))
+               } else {
+                       gid = 0; /* in case gids == NULL */
+                       for (;;) {
+                               if (cand) {
+                                       if (cand == candend)
+                                               break;
+                                       i = *cand++ - b->hseqbase;
+                                       if (i >= end)
+                                               break;
+                               } else {
+                                       i = start++;
+                                       if (i == end)
+                                               break;
+                               }
+                               if (gids == NULL ||
+                                   (gids[i] >= min && gids[i] <= max)) {
+                                       const void *v = BUNtail(bi, i + 
BUNfirst(b));
+                                       if (gids)
+                                               gid = gids[i] - min;
+                                       if (!skip_nils ||
+                                           (*atomcmp)(v, nil) != 0) {
+                                               if (oids[gid] == oid_nil) {
                                                        oids[gid] = i + 
b->hseqbase;
+                                                       nils--;
+                                               } else {
+                                                       const void *g = 
BUNtail(bi, (BUN) (oids[gid] - b->hseqbase) + BUNfirst(b));
+                                                       if ((*atomcmp)(g, nil) 
!= 0 &&
+                                                           ((*atomcmp)(v, nil) 
== 0 ||
+                                                            LT((*atomcmp)(v, 
g), 0)))
+                                                               oids[gid] = i + 
b->hseqbase;
+                                               }
                                        }
                                }
                        }
@@ -1941,13 +1984,9 @@ BATgroupmax(BAT *b, BAT *g, BAT *e, BAT 
                GDKerror("BATgroupmax: %s\n", err);
                return NULL;
        }
-       if (g == NULL) {
-               GDKerror("BATgroupmax: b and g must be aligned\n");
-               return NULL;
-       }
 
        if (BATcount(b) == 0 || ngrp == 0) {
-               /* trivial: no products, so return bat aligned with g
+               /* trivial: no maximums, so return bat aligned with g
                 * with nil in the tail */
                bn = BATconstant(TYPE_oid, &oid_nil, ngrp);
                BATseqbase(bn, ngrp == 0 ? 0 : min);
@@ -1962,7 +2001,7 @@ BATgroupmax(BAT *b, BAT *g, BAT *e, BAT 
        for (i = 0; i < ngrp; i++)
                oids[i] = oid_nil;
 
-       if (BATtdense(g))
+       if (g == NULL || BATtdense(g))
                gids = NULL;
        else
                gids = (const oid *) Tloc(g, BUNfirst(g) + start);
@@ -1999,35 +2038,59 @@ BATgroupmax(BAT *b, BAT *g, BAT *e, BAT 
        default:
                bi = bat_iterator(b);
 
-               for (;;) {
+               if (g && BATtdense(g)) {
+                       /* single element groups */
                        if (cand) {
-                               if (cand == candend)
-                                       break;
-                               i = *cand++ - b->hseqbase;
-                               if (i >= end)
-                                       break;
+                               while (cand < candend) {
+                                       i = *cand++ - b->hseqbase;
+                                       if (i >= end)
+                                               break;
+                                       if (!skip_nils ||
+                                           (*atomcmp)(BUNtail(bi, i + 
BUNfirst(b)), nil) != 0) {
+                                               oids[i] = i + b->hseqbase;
+                                               nils--;
+                                       }
+                               }
                        } else {
-                               i = start++;
-                               if (i == end)
-                                       break;
+                               for (i = start; i < end; i++) {
+                                       if (!skip_nils ||
+                                           (*atomcmp)(BUNtail(bi, i + 
BUNfirst(b)), nil) != 0) {
+                                               oids[i] = i + b->hseqbase;
+                                               nils--;
+                                       }
+                               }
                        }
-                       if (gids == NULL ||
-                           (gids[i] >= min && gids[i] <= max)) {
-                               const void *v = BUNtail(bi, i + BUNfirst(b));
-                               if (gids)
-                                       gid = gids[i] - min;
-                               else
-                                       gid = (oid) i;
-                               if (!skip_nils || (*atomcmp)(v, nil) != 0) {
-                                       if (oids[gid] == oid_nil) {
-                                               oids[gid] = i + b->hseqbase;
-                                               nils--;
-                                       } else {
-                                               const void *g = BUNtail(bi, 
(BUN) (oids[gid] - b->hseqbase) + BUNfirst(b));
-                                               if ((*atomcmp)(g, nil) != 0 &&
-                                                  ((*atomcmp)(v, nil) == 0 ||
-                                                   GT((*atomcmp)(v, g), 0)))
+               } else {
+                       gid = 0; /* in case gids == NULL */
+                       for (;;) {
+                               if (cand) {
+                                       if (cand == candend)
+                                               break;
+                                       i = *cand++ - b->hseqbase;
+                                       if (i >= end)
+                                               break;
+                               } else {
+                                       i = start++;
+                                       if (i == end)
+                                               break;
+                               }
+                               if (gids == NULL ||
+                                   (gids[i] >= min && gids[i] <= max)) {
+                                       const void *v = BUNtail(bi, i + 
BUNfirst(b));
+                                       if (gids)
+                                               gid = gids[i] - min;
+                                       if (!skip_nils ||
+                                           (*atomcmp)(v, nil) != 0) {
+                                               if (oids[gid] == oid_nil) {
                                                        oids[gid] = i + 
b->hseqbase;
+                                                       nils--;
+                                               } else {
+                                                       const void *g = 
BUNtail(bi, (BUN) (oids[gid] - b->hseqbase) + BUNfirst(b));
+                                                       if ((*atomcmp)(g, nil) 
!= 0 &&
+                                                           ((*atomcmp)(v, nil) 
== 0 ||
+                                                            GT((*atomcmp)(v, 
g), 0)))
+                                                               oids[gid] = i + 
b->hseqbase;
+                                               }
                                        }
                                }
                        }
_______________________________________________
checkin-list mailing list
[email protected]
http://mail.monetdb.org/mailman/listinfo/checkin-list

Reply via email to