LCOV - code coverage report
Current view: top level - gdk - gdk_cand.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 765 994 77.0 %
Date: 2025-03-26 20:06:40 Functions: 24 25 96.0 %

          Line data    Source code
       1             : /*
       2             :  * SPDX-License-Identifier: MPL-2.0
       3             :  *
       4             :  * This Source Code Form is subject to the terms of the Mozilla Public
       5             :  * License, v. 2.0.  If a copy of the MPL was not distributed with this
       6             :  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
       7             :  *
       8             :  * Copyright 2024, 2025 MonetDB Foundation;
       9             :  * Copyright August 2008 - 2023 MonetDB B.V.;
      10             :  * Copyright 1997 - July 2008 CWI.
      11             :  */
      12             : 
      13             : #include "monetdb_config.h"
      14             : #include "gdk.h"
      15             : #include "gdk_private.h"
      16             : 
      17             : bool
      18        9676 : BATiscand(BAT *b)
      19             : {
      20        9676 :         if (ATOMtype(b->ttype) != TYPE_oid)
      21             :                 return false;
      22        9676 :         if (complex_cand(b))
      23             :                 return true;
      24        9170 :         if (b->ttype == TYPE_void && is_oid_nil(b->tseqbase))
      25             :                 return false;
      26       18340 :         return b->tsorted && BATtkey(b);
      27             : }
      28             : 
      29             : /* create a new, dense candidate list with values from `first' up to,
      30             :  * but not including, `last' */
      31             : static inline BAT *
      32        2936 : newdensecand(oid first, oid last)
      33             : {
      34        2936 :         if (last <= first)
      35           0 :                 first = last = 0; /* empty range */
      36        2936 :         return BATdense(0, first, last - first);
      37             : }
      38             : 
      39             : /* merge two candidate lists and produce a new one
      40             :  *
      41             :  * candidate lists are VOID-headed BATs with an OID tail which is
      42             :  * sorted and unique.
      43             :  */
      44             : BAT *
      45       78677 : BATmergecand(BAT *a, BAT *b)
      46             : {
      47       78677 :         BAT *bn;
      48       78677 :         oid *restrict p, i;
      49       78677 :         struct canditer cia, cib;
      50             : 
      51       78677 :         BATcheck(a, NULL);
      52       78677 :         BATcheck(b, NULL);
      53             : 
      54       78677 :         canditer_init(&cia, NULL, a);
      55       78675 :         canditer_init(&cib, NULL, b);
      56             : 
      57             :         /* we can return a if b is empty (and v.v.) */
      58       78676 :         if (cia.ncand == 0) {
      59       12965 :                 bn = canditer_slice(&cib, 0, cib.ncand);
      60       12964 :                 goto doreturn;
      61             :         }
      62       65711 :         if (cib.ncand == 0) {
      63       10956 :                 bn = canditer_slice(&cia, 0, cia.ncand);
      64       10957 :                 goto doreturn;
      65             :         }
      66             :         /* we can return a if a fully covers b (and v.v) */
      67       54755 :         if (cia.tpe == cand_dense && cib.tpe == cand_dense) {
      68             :                 /* both are dense */
      69       11024 :                 if (cia.seq <= cib.seq && cib.seq <= cia.seq + cia.ncand) {
      70             :                         /* partial overlap starting with a, or b is
      71             :                          * smack bang after a */
      72        2370 :                         bn = newdensecand(cia.seq, cia.seq + cia.ncand < cib.seq + cib.ncand ? cib.seq + cib.ncand : cia.seq + cia.ncand);
      73        2370 :                         goto doreturn;
      74             :                 }
      75        8654 :                 if (cib.seq <= cia.seq && cia.seq <= cib.seq + cib.ncand) {
      76             :                         /* partial overlap starting with b, or a is
      77             :                          * smack bang after b */
      78         557 :                         bn = newdensecand(cib.seq, cia.seq + cia.ncand < cib.seq + cib.ncand ? cib.seq + cib.ncand : cia.seq + cia.ncand);
      79         557 :                         goto doreturn;
      80             :                 }
      81             :         }
      82       51828 :         if (cia.tpe == cand_dense
      83       11218 :             && cia.seq <= cib.seq
      84        4871 :             && canditer_last(&cia) >= canditer_last(&cib)) {
      85         339 :                 bn = canditer_slice(&cia, 0, cia.ncand);
      86         339 :                 goto doreturn;
      87             :         }
      88       51489 :         if (cib.tpe == cand_dense
      89       38163 :             && cib.seq <= cia.seq
      90       10184 :             && canditer_last(&cib) >= canditer_last(&cia)) {
      91         185 :                 bn = canditer_slice(&cib, 0, cib.ncand);
      92         185 :                 goto doreturn;
      93             :         }
      94             : 
      95       51304 :         bn = COLnew(0, TYPE_oid, cia.ncand + cib.ncand, TRANSIENT);
      96       51302 :         if (bn == NULL)
      97           0 :                 goto doreturn;
      98       51302 :         p = (oid *) Tloc(bn, 0);
      99       51302 :         if (cia.tpe == cand_dense && cib.tpe == cand_dense) {
     100             :                 /* both lists are dense */
     101        8097 :                 if (cia.seq > cib.seq) {
     102        4113 :                         struct canditer ci;
     103        4113 :                         ci = cia;
     104        4113 :                         cia = cib;
     105        4113 :                         cib = ci;
     106             :                 }
     107             :                 /* cia completely before cib */
     108        8097 :                 assert(cia.seq + cia.ncand < cib.seq);
     109       30772 :                 for (i = cia.seq; i < cia.seq + cia.ncand; i++)
     110       22675 :                         *p++ = i;
     111             :                 /* there is a gap */
     112       27270 :                 for (i = cib.seq; i < cib.seq + cib.ncand; i++)
     113       19173 :                         *p++ = i;
     114       43205 :         } else if (cia.tpe == cand_dense || cib.tpe == cand_dense) {
     115       32661 :                 if (cib.tpe == cand_dense) {
     116       29881 :                         struct canditer ci;
     117       29881 :                         ci = cia;
     118       29881 :                         cia = cib;
     119       29881 :                         cib = ci;
     120             :                 }
     121             :                 /* only cia is dense */
     122             : 
     123             :                 /* copy part of cib that's before the start of cia */
     124      133021 :                 while (canditer_peek(&cib) < cia.seq) {
     125      100360 :                         *p++ = canditer_next(&cib);
     126             :                 }
     127             :                 /* copy all of cia */
     128       75381 :                 for (i = cia.seq; i < cia.seq + cia.ncand; i++)
     129       42720 :                         *p++ = i;
     130             :                 /* skip over part of cib that overlaps with cia */
     131       32661 :                 canditer_setidx(&cib, canditer_search(&cib, cia.seq + cia.ncand, true));
     132             :                 /* copy rest of cib */
     133      123790 :                 while (cib.next < cib.ncand) {
     134       91128 :                         *p++ = canditer_next(&cib);
     135             :                 }
     136             :         } else {
     137             :                 /* a and b are both not dense */
     138       10544 :                 oid ao = canditer_next(&cia);
     139       10544 :                 oid bo = canditer_next(&cib);
     140    24515891 :                 while (!is_oid_nil(ao) && !is_oid_nil(bo)) {
     141    24505347 :                         if (ao < bo) {
     142    14641914 :                                 *p++ = ao;
     143    14641914 :                                 ao = canditer_next(&cia);
     144     9863433 :                         } else if (bo < ao) {
     145     9139429 :                                 *p++ = bo;
     146     9139429 :                                 bo = canditer_next(&cib);
     147             :                         } else {
     148      724004 :                                 *p++ = ao;
     149      724004 :                                 ao = canditer_next(&cia);
     150      724004 :                                 bo = canditer_next(&cib);
     151             :                         }
     152             :                 }
     153      575811 :                 while (!is_oid_nil(ao)) {
     154      565267 :                         *p++ = ao;
     155      565267 :                         ao = canditer_next(&cia);
     156             :                 }
     157      416223 :                 while (!is_oid_nil(bo)) {
     158      405679 :                         *p++ = bo;
     159      405679 :                         bo = canditer_next(&cib);
     160             :                 }
     161             :         }
     162             : 
     163             :         /* properties */
     164       51302 :         BATsetcount(bn, (BUN) (p - (oid *) Tloc(bn, 0)));
     165       51303 :         bn->trevsorted = BATcount(bn) <= 1;
     166       51303 :         bn->tsorted = true;
     167       51303 :         bn->tkey = true;
     168       51303 :         bn->tnil = false;
     169       51303 :         bn->tnonil = true;
     170       51303 :         bn = virtualize(bn);
     171       78677 :   doreturn:
     172       78677 :         TRC_DEBUG(ALGO, ALGOBATFMT "," ALGOBATFMT " -> " ALGOOPTBATFMT "\n",
     173             :                   ALGOBATPAR(a), ALGOBATPAR(b), ALGOOPTBATPAR(bn));
     174             :         return bn;
     175             : }
     176             : 
     177             : /* intersect two candidate lists and produce a new one
     178             :  *
     179             :  * candidate lists are VOID-headed BATs with an OID tail which is
     180             :  * sorted and unique.
     181             :  */
     182             : BAT *
     183          34 : BATintersectcand(BAT *a, BAT *b)
     184             : {
     185          34 :         BAT *bn;
     186          34 :         oid *restrict p;
     187          34 :         struct canditer cia, cib;
     188             : 
     189          34 :         BATcheck(a, NULL);
     190          34 :         BATcheck(b, NULL);
     191             : 
     192          34 :         canditer_init(&cia, NULL, a);
     193          34 :         canditer_init(&cib, NULL, b);
     194             : 
     195          34 :         if (cia.ncand == 0 || cib.ncand == 0) {
     196           7 :                 bn = BATdense(0, 0, 0);
     197           7 :                 goto doreturn;
     198             :         }
     199             : 
     200          27 :         if (cia.tpe == cand_dense && cib.tpe == cand_dense) {
     201             :                 /* both lists are dense */
     202           9 :                 bn = newdensecand(MAX(cia.seq, cib.seq), MIN(cia.seq + cia.ncand, cib.seq + cib.ncand));
     203           9 :                 goto doreturn;
     204             :         }
     205             : 
     206          18 :         bn = COLnew(0, TYPE_oid, MIN(cia.ncand, cib.ncand), TRANSIENT);
     207          18 :         if (bn == NULL)
     208           0 :                 goto doreturn;
     209          18 :         p = (oid *) Tloc(bn, 0);
     210          18 :         if (cia.tpe == cand_dense || cib.tpe == cand_dense) {
     211          18 :                 if (cib.tpe == cand_dense) {
     212          18 :                         struct canditer ci;
     213          18 :                         ci = cia;
     214          18 :                         cia = cib;
     215          18 :                         cib = ci;
     216             :                 }
     217             :                 /* only cia is dense */
     218             : 
     219             :                 /* search for first value in cib that is contained in cia */
     220          18 :                 canditer_setidx(&cib, canditer_search(&cib, cia.seq, true));
     221          18 :                 oid bo;
     222          62 :                 while (!is_oid_nil(bo = canditer_next(&cib)) && bo < cia.seq + cia.ncand)
     223          44 :                         *p++ = bo;
     224             :         } else {
     225             :                 /* a and b are both not dense */
     226           0 :                 oid ao = canditer_next(&cia);
     227           0 :                 oid bo = canditer_next(&cib);
     228           0 :                 while (!is_oid_nil(ao) && !is_oid_nil(bo)) {
     229           0 :                         if (ao < bo)
     230           0 :                                 ao = canditer_next(&cia);
     231           0 :                         else if (bo < ao)
     232           0 :                                 bo = canditer_next(&cib);
     233             :                         else {
     234           0 :                                 *p++ = ao;
     235           0 :                                 ao = canditer_next(&cia);
     236           0 :                                 bo = canditer_next(&cib);
     237             :                         }
     238             :                 }
     239             :         }
     240             : 
     241             :         /* properties */
     242          18 :         BATsetcount(bn, (BUN) (p - (oid *) Tloc(bn, 0)));
     243          18 :         bn->trevsorted = BATcount(bn) <= 1;
     244          18 :         bn->tsorted = true;
     245          18 :         bn->tkey = true;
     246          18 :         bn->tnil = false;
     247          18 :         bn->tnonil = true;
     248          18 :         bn = virtualize(bn);
     249          34 :   doreturn:
     250          34 :         TRC_DEBUG(ALGO, ALGOBATFMT "," ALGOBATFMT " -> " ALGOOPTBATFMT "\n",
     251             :                   ALGOBATPAR(a), ALGOBATPAR(b), ALGOOPTBATPAR(bn));
     252             :         return bn;
     253             : }
     254             : 
     255             : /* calculate the difference of two candidate lists and produce a new one
     256             :  */
     257             : BAT *
     258        2980 : BATdiffcand(BAT *a, BAT *b)
     259             : {
     260        2980 :         BAT *bn;
     261        2980 :         oid *restrict p;
     262        2980 :         struct canditer cia, cib;
     263             : 
     264        2980 :         BATcheck(a, NULL);
     265        2980 :         BATcheck(b, NULL);
     266             : 
     267        2980 :         canditer_init(&cia, NULL, a);
     268        2980 :         canditer_init(&cib, NULL, b);
     269             : 
     270        2980 :         if (cia.ncand == 0) {
     271           0 :                 bn = BATdense(0, 0, 0);
     272           0 :                 goto doreturn;
     273             :         }
     274        2980 :         if (cib.ncand == 0) {
     275         397 :                 bn = canditer_slice(&cia, 0, cia.ncand);
     276         397 :                 goto doreturn;
     277             :         }
     278             : 
     279        2583 :         if (cib.seq > canditer_last(&cia) || canditer_last(&cib) < cia.seq) {
     280             :                 /* no overlap, return a */
     281           0 :                 bn = canditer_slice(&cia, 0, cia.ncand);
     282           0 :                 goto doreturn;
     283             :         }
     284             : 
     285        2583 :         if (cia.tpe == cand_dense && cib.tpe == cand_dense) {
     286             :                 /* both a and b are dense */
     287        1291 :                 if (cia.seq < cib.seq) {
     288             :                         /* a starts before b */
     289         139 :                         if (cia.seq + cia.ncand <= cib.seq + cib.ncand) {
     290             :                                 /* b overlaps with end of a */
     291          94 :                                 bn = canditer_slice(&cia, 0, cib.seq - cia.seq);
     292          94 :                                 goto doreturn;
     293             :                         }
     294             :                         /* b is subset of a */
     295          45 :                         bn = canditer_slice2(&cia, 0, cib.seq - cia.seq,
     296             :                                              cib.seq + cib.ncand - cia.seq,
     297             :                                              cia.ncand);
     298          45 :                         goto doreturn;
     299             :                 } else {
     300             :                         /* cia.seq >= cib.seq */
     301        1152 :                         if (cia.seq + cia.ncand > cib.seq + cib.ncand) {
     302             :                                 /* b overlaps with beginning of a */
     303          71 :                                 bn = canditer_slice(&cia, cib.seq + cib.ncand - cia.seq, cia.ncand);
     304          71 :                                 goto doreturn;
     305             :                         }
     306             :                         /* a is subset f b */
     307        1081 :                         bn = BATdense(0, 0, 0);
     308        1081 :                         goto doreturn;
     309             :                 }
     310             :         }
     311        1292 :         if (cib.tpe == cand_dense) {
     312             :                 /* b is dense and a is not: we can copy the part of a
     313             :                  * that is before the start of b and the part of a
     314             :                  * that is after the end of b */
     315         300 :                 bn = canditer_slice2val(&cia, oid_nil, cib.seq,
     316             :                                         cib.seq + cib.ncand, oid_nil);
     317         300 :                 goto doreturn;
     318             :         }
     319             : 
     320             :         /* b is not dense */
     321         992 :         bn = COLnew(0, TYPE_oid, BATcount(a), TRANSIENT);
     322         992 :         if (bn == NULL)
     323           0 :                 goto doreturn;
     324         992 :         p = Tloc(bn, 0);
     325             :         /* find first position in b that is in range of a */
     326         992 :         canditer_setidx(&cib, canditer_search(&cib, cia.seq, true));
     327             :         {
     328             :                 /* because we may jump over this declaration, we put
     329             :                  * it inside a block */
     330         992 :                 oid ob = canditer_next(&cib);
     331     7922910 :                 for (BUN i = 0; i < cia.ncand; i++) {
     332     7921918 :                         oid oa = canditer_next(&cia);
     333    10704382 :                         while (!is_oid_nil(ob) && ob < oa) {
     334     2781856 :                                 ob = canditer_next(&cib);
     335             :                         }
     336     7921918 :                         if (is_oid_nil(ob) || oa < ob)
     337     5138734 :                                 *p++ = oa;
     338             :                 }
     339             :         }
     340             : 
     341             :         /* properties */
     342         992 :         BATsetcount(bn, (BUN) (p - (oid *) Tloc(bn, 0)));
     343         992 :         bn->trevsorted = BATcount(bn) <= 1;
     344         992 :         bn->tsorted = true;
     345         992 :         bn->tkey = true;
     346         992 :         bn->tnil = false;
     347         992 :         bn->tnonil = true;
     348         992 :         bn = virtualize(bn);
     349        2980 :   doreturn:
     350        2980 :         TRC_DEBUG(ALGO, ALGOBATFMT "," ALGOBATFMT " -> " ALGOOPTBATFMT "\n",
     351             :                   ALGOBATPAR(a), ALGOBATPAR(b), ALGOOPTBATPAR(bn));
     352             :         return bn;
     353             : }
     354             : 
     355             : /* return offset of first value in cand that is >= o */
     356             : static inline BUN
     357      941492 : binsearchcand(const oid *cand, BUN hi, oid o)
     358             : {
     359      941492 :         BUN lo = 0;
     360             : 
     361      941492 :         if (o <= cand[lo])
     362             :                 return 0;
     363      451051 :         if (o > cand[hi])
     364      404751 :                 return hi + 1;
     365             :         /* loop invariant: cand[lo] < o <= cand[hi] */
     366      251089 :         while (hi > lo + 1) {
     367      219547 :                 BUN mid = (lo + hi) / 2;
     368      219547 :                 if (cand[mid] == o)
     369       14758 :                         return mid;
     370      204789 :                 if (cand[mid] < o)
     371             :                         lo = mid;
     372             :                 else
     373      145627 :                         hi = mid;
     374             :         }
     375             :         return hi;
     376             : }
     377             : 
     378             : /* count number of 1 bits in ci->mask between bit positions lo
     379             :  * (inclusive) and hi (not inclusive) */
     380             : static BUN
     381       31809 : count_mask_bits(const struct canditer *ci, BUN lo, BUN hi)
     382             : {
     383       31809 :         BUN n;
     384       31809 :         assert(lo <= hi);
     385       31809 :         assert(ci->tpe == cand_mask);
     386       31809 :         if (lo == hi)
     387             :                 return 0;
     388       31809 :         lo += ci->firstbit;
     389       31809 :         hi += ci->firstbit;
     390       31809 :         BUN loi = lo / 32;
     391       31809 :         BUN hii = hi / 32;
     392       31809 :         lo %= 32;
     393       31809 :         hi %= 32;
     394       31809 :         if (loi == hii)
     395        1119 :                 return (BUN) candmask_pop((ci->mask[loi] & ((1U << hi) - 1)) >> lo);
     396       30690 :         n = (BUN) candmask_pop(ci->mask[loi++] >> lo);
     397     2418212 :         while (loi < hii)
     398     2387522 :                 n += (BUN) candmask_pop(ci->mask[loi++]);
     399       30690 :         if (hi != 0)
     400        9650 :                 n += (BUN) candmask_pop(ci->mask[loi] & ((1U << hi) - 1));
     401             :         return n;
     402             : }
     403             : 
     404             : /* initialize a candidate iterator */
     405             : void
     406     8322486 : canditer_init(struct canditer *ci, BAT *b, BAT *s)
     407             : {
     408     8322486 :         assert(ci != NULL);
     409     8322486 :         BUN batcount = 0;
     410     8322486 :         oid hseq = 0;
     411     8322486 :         if (b) {
     412     7661486 :                 MT_lock_set(&b->theaplock);
     413     7658971 :                 batcount = BATcount(b);
     414     7658971 :                 hseq = b->hseqbase;
     415     7658971 :                 MT_lock_unset(&b->theaplock);
     416             :         }
     417             : 
     418     8322137 :         if (s == NULL) {
     419     4081486 :                 if (b == NULL) {
     420             :                         /* trivially empty candidate list */
     421           0 :                         *ci = (struct canditer) {
     422             :                                 .tpe = cand_dense,
     423             :                         };
     424           0 :                         return;
     425             :                 }
     426             :                 /* every row is a candidate */
     427     4081486 :                 *ci = (struct canditer) {
     428             :                         .tpe = cand_dense,
     429             :                         .seq = hseq,
     430             :                         .hseq = hseq,
     431             :                         .ncand = batcount,
     432             :                 };
     433     4081486 :                 return;
     434             :         }
     435             : 
     436     4240651 :         assert(ATOMtype(s->ttype) == TYPE_oid || s->ttype == TYPE_msk);
     437     4240651 :         assert(s->ttype == TYPE_msk|| s->tsorted);
     438     4240651 :         assert(s->ttype == TYPE_msk|| s->tkey);
     439     4240651 :         assert(s->ttype == TYPE_msk|| s->tnonil);
     440     4240651 :         assert(s->ttype == TYPE_void || s->tvheap == NULL);
     441             : 
     442     4240651 :         BUN cnt = BATcount(s);
     443             : 
     444     4240651 :         if (cnt == 0 || (b != NULL && batcount == 0)) {
     445             :                 /* candidate list for empty BAT or empty candidate list */
     446     1014102 :                 *ci = (struct canditer) {
     447             :                         .tpe = cand_dense,
     448     1014102 :                         .hseq = s->hseqbase,
     449             :                         .s = s,
     450             :                 };
     451     1014102 :                 return;
     452             :         }
     453             : 
     454     3226549 :         *ci = (struct canditer) {
     455     3226549 :                 .seq = s->tseqbase,
     456     3226549 :                 .hseq = s->hseqbase,
     457             :                 .s = s,
     458             :         };
     459             : 
     460     3226549 :         if (mask_cand(s)) {
     461       31807 :                 ci->tpe = cand_mask;
     462       31807 :                 ci->mask = (const uint32_t *) ccand_first(s);
     463       31807 :                 ci->seq = s->tseqbase - (oid) CCAND(s)->firstbit;
     464       31807 :                 ci->hseq = s->hseqbase;
     465       31807 :                 ci->nvals = ccand_free(s) / sizeof(uint32_t);
     466       31807 :                 cnt = ci->nvals * 32;
     467     3194742 :         } else if (s->ttype == TYPE_msk) {
     468           0 :                 assert(0);
     469             :                 ci->tpe = cand_mask;
     470             :                 ci->mask = (const uint32_t *) s->theap->base;
     471             :                 ci->seq = s->hseqbase;
     472             :                 ci->nvals = (cnt + 31U) / 32U;
     473     3194742 :         } else if (s->ttype == TYPE_void) {
     474     2674853 :                 assert(!is_oid_nil(ci->seq));
     475     2674853 :                 if (s->tvheap) {
     476       94038 :                         assert(ccand_free(s) % SIZEOF_OID == 0);
     477       94038 :                         ci->nvals = ccand_free(s) / SIZEOF_OID;
     478       94038 :                         if (ci->nvals > 0) {
     479       94038 :                                 ci->tpe = cand_except;
     480       94038 :                                 ci->oids = (const oid *) ccand_first(s);
     481             :                         } else {
     482             :                                 /* why the vheap? */
     483             :                                 ci->tpe = cand_dense;
     484             :                         }
     485             :                 } else {
     486             :                         ci->tpe = cand_dense;
     487             :                 }
     488      519889 :         } else if (is_oid_nil(ci->seq)) {
     489      486690 :                 ci->tpe = cand_materialized;
     490      486690 :                 ci->oids = (const oid *) Tloc(s, 0);
     491      486690 :                 ci->seq = ci->oids[0];
     492      486690 :                 ci->nvals = cnt;
     493             :         } else {
     494             :                 /* materialized dense: no exceptions */
     495             :                 ci->tpe = cand_dense;
     496             :         }
     497     3226549 :         switch (ci->tpe) {
     498      486684 :         case cand_materialized:
     499      486684 :                 if (b != NULL) {
     500      380782 :                         BUN p = binsearchcand(ci->oids, cnt - 1U, hseq);
     501             :                         /* p == cnt means candidate list is completely
     502             :                          * before b */
     503      380783 :                         ci->offset = p;
     504      380783 :                         ci->oids += p;
     505      380783 :                         cnt -= p;
     506      380783 :                         if (cnt > 0) {
     507      380783 :                                 cnt = binsearchcand(ci->oids, cnt  - 1U,
     508             :                                                     hseq + batcount);
     509             :                                 /* cnt == 0 means candidate list is
     510             :                                  * completely after b */
     511             :                         }
     512      380783 :                         if (cnt == 0) {
     513             :                                 /* no overlap */
     514           0 :                                 *ci = (struct canditer) {
     515             :                                         .tpe = cand_dense,
     516             :                                         .s = s,
     517             :                                 };
     518           0 :                                 return;
     519             :                         }
     520      380783 :                         ci->seq = ci->oids[0];
     521      380783 :                         ci->nvals = cnt;
     522      380783 :                         if (ci->oids[cnt - 1U] - ci->seq == cnt - 1U) {
     523             :                                 /* actually dense */
     524          57 :                                 ci->tpe = cand_dense;
     525          57 :                                 ci->oids = NULL;
     526          57 :                                 ci->nvals = 0;
     527             :                         }
     528             :                 }
     529             :                 break;
     530       94037 :         case cand_except:
     531             :                 /* exceptions must all be within range of s */
     532       94037 :                 assert(ci->oids[0] >= ci->seq);
     533       94037 :                 assert(ci->oids[ci->nvals - 1U] < ci->seq + cnt + ci->nvals);
     534             :                 /* prune exceptions at either end of range of s */
     535     3659783 :                 while (ci->nvals > 0 && ci->oids[0] == ci->seq) {
     536     3565746 :                         ci->nvals--;
     537     3565746 :                         ci->oids++;
     538     3565746 :                         ci->seq++;
     539             :                 }
     540       94237 :                 while (ci->nvals > 0 &&
     541       92969 :                        ci->oids[ci->nvals - 1U] == ci->seq + cnt + ci->nvals - 1U)
     542         200 :                         ci->nvals--;
     543       94037 :                 if (b != NULL) {
     544       87862 :                         if (ci->seq + cnt + ci->nvals <= hseq ||
     545       87862 :                             ci->seq >= hseq + batcount) {
     546             :                                 /* candidate list does not overlap with b */
     547           0 :                                 *ci = (struct canditer) {
     548             :                                         .tpe = cand_dense,
     549             :                                         .s = s,
     550             :                                 };
     551           0 :                                 return;
     552             :                         }
     553             :                 }
     554       94037 :                 if (ci->nvals > 0) {
     555       92768 :                         if (b == NULL)
     556             :                                 break;
     557       86958 :                         BUN p;
     558       86958 :                         p = binsearchcand(ci->oids, ci->nvals - 1U, hseq);
     559       86958 :                         if (p == ci->nvals) {
     560             :                                 /* all exceptions before start of b */
     561           0 :                                 ci->offset = hseq - ci->seq - ci->nvals;
     562           0 :                                 cnt = ci->seq + cnt + ci->nvals - hseq;
     563           0 :                                 ci->seq = hseq;
     564           0 :                                 ci->nvals = 0;
     565           0 :                                 ci->tpe = cand_dense;
     566           0 :                                 ci->oids = NULL;
     567           0 :                                 break;
     568             :                         }
     569       86958 :                         assert(hseq > ci->seq || p == 0);
     570       86958 :                         if (hseq > ci->seq) {
     571             :                                 /* skip candidates, possibly including
     572             :                                  * exceptions */
     573           0 :                                 ci->oids += p;
     574           0 :                                 ci->nvals -= p;
     575           0 :                                 p = hseq - ci->seq - p;
     576           0 :                                 cnt -= p;
     577           0 :                                 ci->offset += p;
     578           0 :                                 ci->seq = hseq;
     579             :                         }
     580       86958 :                         if (ci->seq + cnt + ci->nvals > hseq + batcount) {
     581           0 :                                 p = binsearchcand(ci->oids, ci->nvals - 1U,
     582             :                                                   hseq + batcount);
     583           0 :                                 ci->nvals = p;
     584           0 :                                 cnt = hseq + batcount - ci->seq - ci->nvals;
     585             :                         }
     586       86958 :                         while (ci->nvals > 0 && ci->oids[0] == ci->seq) {
     587           0 :                                 ci->nvals--;
     588           0 :                                 ci->oids++;
     589           0 :                                 ci->seq++;
     590             :                         }
     591       86958 :                         while (ci->nvals > 0 &&
     592       86958 :                                ci->oids[ci->nvals - 1U] == ci->seq + cnt + ci->nvals - 1U)
     593           0 :                                 ci->nvals--;
     594       86958 :                         if (ci->nvals > 0)
     595             :                                 break;
     596             :                 }
     597        1269 :                 ci->tpe = cand_dense;
     598        1269 :                 ci->oids = NULL;
     599             :                 /* fall through */
     600     2615290 :         case cand_dense:
     601     2615290 :                 if (b != NULL) {
     602     2291741 :                         if (ci->seq + cnt <= hseq ||
     603     2291741 :                             ci->seq >= hseq + batcount) {
     604             :                                 /* no overlap */
     605           0 :                                 *ci = (struct canditer) {
     606             :                                         .tpe = cand_dense,
     607             :                                         .s = s,
     608             :                                 };
     609           0 :                                 return;
     610             :                         }
     611     2291741 :                         if (hseq > ci->seq) {
     612           0 :                                 cnt -= hseq - ci->seq;
     613           0 :                                 ci->offset += hseq - ci->seq;
     614           0 :                                 ci->seq = hseq;
     615             :                         }
     616     2291741 :                         if (ci->seq + cnt > hseq + batcount)
     617           0 :                                 cnt = hseq + batcount - ci->seq;
     618             :                 }
     619             :                 break;
     620       31807 :         case cand_mask:
     621       31807 :                 assert(s->tseqbase != oid_nil);
     622       31807 :                 if (b != NULL) {
     623       11317 :                         if (ci->seq + cnt <= hseq ||
     624       11317 :                             ci->seq >= hseq + batcount) {
     625             :                                 /* no overlap */
     626           0 :                                 *ci = (struct canditer) {
     627             :                                         .tpe = cand_dense,
     628             :                                         .s = s,
     629             :                                 };
     630           0 :                                 return;
     631             :                         }
     632       11317 :                         if (hseq > ci->seq) {
     633           0 :                                 cnt = hseq - ci->seq;
     634           0 :                                 ci->mask += cnt / 32U;
     635           0 :                                 ci->firstbit = (uint8_t) (cnt % 32U);
     636           0 :                                 cnt = BATcount(s) - cnt;
     637           0 :                                 ci->seq = hseq;
     638             :                         }
     639       11317 :                         if (ci->seq + cnt > hseq + batcount) {
     640       10968 :                                 cnt = hseq + batcount - ci->seq;
     641             :                         }
     642       11317 :                         ci->nvals = (ci->firstbit + cnt + 31U) / 32U;
     643             :                 }
     644             :                 /* if first value is only partially used, check
     645             :                  * whether there are any 1 bits in the used part */
     646       31807 :                 if (ci->firstbit > 0 && (ci->mask[0] >> ci->firstbit) == 0) {
     647           0 :                         if (cnt <= 32U - ci->firstbit) {
     648             :                                 cnt = 0;
     649             :                                 /* returns below */
     650             :                         } else {
     651           0 :                                 cnt -= 32U - ci->firstbit;
     652           0 :                                 ci->firstbit = 0;
     653           0 :                                 ci->mask++;
     654           0 :                                 ci->nvals--;
     655             :                         }
     656             :                 }
     657             :                 /* skip over any zero mask words that are used completely */
     658       31807 :                 if (ci->firstbit == 0) {
     659       74206 :                         while (cnt >= 32U && ci->mask[0] == 0) {
     660       42399 :                                 cnt -= 32U;
     661       42399 :                                 ci->mask++;
     662       42399 :                                 ci->nvals--;
     663             :                         }
     664             :                 }
     665             :                 /* check whether there are any 1 bits in the last word */
     666       31807 :                 if (cnt == 0 ||
     667       31807 :                     (cnt < 32U - ci->firstbit &&
     668        1119 :                      ((ci->mask[0] >> ci->firstbit) & ((1U << cnt) - 1U)) == 0)) {
     669             :                         /* no one bits in the whole relevant portion
     670             :                          * of the bat */
     671           0 :                         *ci = (struct canditer) {
     672             :                                 .tpe = cand_dense,
     673             :                                 .s = s,
     674             :                         };
     675           0 :                         return;
     676             :                 }
     677             :                 /* here we know there are 1 bits in the first mask
     678             :                  * word */
     679       31807 :                 int i = candmask_lobit(ci->mask[0] >> ci->firstbit);
     680       31807 :                 assert(i >= 0);      /* there should be a set bit */
     681       31807 :                 ci->firstbit += i;
     682       31807 :                 cnt -= i;
     683       31807 :                 if (mask_cand(s))
     684       31807 :                         ci->mskoff = s->tseqbase - (oid) CCAND(s)->firstbit + (ci->mask - (const uint32_t *) ccand_first(s)) * 32U;
     685             :                 else
     686           0 :                         ci->mskoff = s->tseqbase + (ci->mask - (const uint32_t *) s->theap->base) * 32U;
     687       31807 :                 ci->seq = ci->mskoff + ci->firstbit;
     688       31807 :                 ci->nextbit = ci->firstbit;
     689             :                 /* at this point we know that bit ci->firstbit is set
     690             :                  * in ci->mask[0] */
     691       31807 :                 ci->lastbit = (ci->firstbit + cnt - 1U) % 32U + 1U;
     692       31807 :                 if (ci->lastbit < 32 &&
     693       10968 :                     (ci->mask[ci->nvals - 1] & ((1U << ci->lastbit) - 1)) == 0) {
     694             :                         /* last partial word is all zero */
     695         198 :                         cnt -= ci->lastbit;
     696         198 :                         ci->lastbit = 32;
     697         198 :                         ci->nvals--;
     698             :                 }
     699       31807 :                 if (ci->lastbit == 32) {
     700             :                         /* "remove" zero words at the end */
     701       29124 :                         while (cnt >= 32 && ci->mask[ci->nvals - 1] == 0) {
     702        8088 :                                 ci->nvals--;
     703        8088 :                                 cnt -= 32;
     704             :                         }
     705             :                 }
     706       31807 :                 ci->ncand = count_mask_bits(ci, 0, cnt);
     707       31807 :                 return;
     708             :         default:
     709           0 :                 MT_UNREACHABLE();
     710             :         }
     711     3194743 :         ci->ncand = cnt;
     712     3194743 :         ci->hseq += ci->offset;
     713             : }
     714             : 
     715             : /* return the next candidate without advancing */
     716             : oid
     717     2309842 : canditer_peek(const struct canditer *ci)
     718             : {
     719     2309842 :         oid o = oid_nil;
     720     2309842 :         if (ci->next == ci->ncand)
     721        6308 :                 return oid_nil;
     722     2303534 :         switch (ci->tpe) {
     723     1993001 :         case cand_dense:
     724     1993001 :                 o = ci->seq + ci->next;
     725     1993001 :                 break;
     726      291138 :         case cand_materialized:
     727      291138 :                 assert(ci->next < ci->nvals);
     728      291138 :                 o = ci->oids[ci->next];
     729      291138 :                 break;
     730       19395 :         case cand_except:
     731       19395 :                 o = ci->seq + ci->add + ci->next;
     732       19437 :                 for (oid a = ci->add; a < ci->nvals && o == ci->oids[a]; a++)
     733          42 :                         o++;
     734             :                 break;
     735           0 :         case cand_mask: {
     736           0 :                 BUN m = ci->nextmsk;
     737           0 :                 uint8_t b = ci->nextbit;
     738           0 :                 while ((ci->mask[m] >> b) == 0) {
     739           0 :                         m++;
     740           0 :                         b = 0;
     741             :                 }
     742           0 :                 b += candmask_lobit(ci->mask[m] >> b);
     743           0 :                 o = ci->mskoff + m * 32 + b;
     744           0 :                 break;
     745             :         }
     746             :         default:
     747           0 :                 MT_UNREACHABLE();
     748             :         }
     749             :         return o;
     750             : }
     751             : 
     752             : /* return the previous candidate */
     753             : oid
     754         877 : canditer_prev(struct canditer *ci)
     755             : {
     756         877 :         if (ci->next == 0)
     757           0 :                 return oid_nil;
     758         877 :         switch (ci->tpe) {
     759         877 :         case cand_dense:
     760         877 :                 return ci->seq + --ci->next;
     761           0 :         case cand_materialized:
     762           0 :                 return ci->oids[--ci->next];
     763             :         case cand_except:
     764           0 :                 break;
     765           0 :         case cand_mask:
     766           0 :                 for (;;) {
     767           0 :                         if (ci->nextbit == 0) {
     768           0 :                                 ci->nextbit = 32;
     769           0 :                                 while (ci->mask[--ci->nextmsk] == 0)
     770             :                                         ;
     771             :                         }
     772           0 :                         if (ci->mask[ci->nextmsk] & (1U << --ci->nextbit))
     773             :                                 break;
     774             :                 }
     775           0 :                 ci->next--;
     776           0 :                 return ci->mskoff + ci->nextmsk * 32 + ci->nextbit;
     777             :         default:
     778           0 :                 MT_UNREACHABLE();
     779             :         }
     780           0 :         oid o = ci->seq + ci->add + --ci->next;
     781           0 :         while (ci->add > 0 && o == ci->oids[ci->add - 1]) {
     782           0 :                 ci->add--;
     783           0 :                 o--;
     784             :         }
     785             :         return o;
     786             : }
     787             : 
     788             : /* return the previous candidate without retreating */
     789             : oid
     790        3091 : canditer_peekprev(const struct canditer *ci)
     791             : {
     792        3091 :         oid o = oid_nil;
     793             : 
     794        3091 :         if (ci->next == 0)
     795           0 :                 return oid_nil;
     796        3091 :         switch (ci->tpe) {
     797        3091 :         case cand_dense:
     798        3091 :                 return ci->seq + ci->next - 1;
     799           0 :         case cand_materialized:
     800           0 :                 return ci->oids[ci->next - 1];
     801           0 :         case cand_except:
     802           0 :                 o = ci->seq + ci->add + ci->next - 1;
     803           0 :                 for (oid a = ci->add; a > 0 && o == ci->oids[a - 1]; a--)
     804           0 :                         o--;
     805             :                 break;
     806           0 :         case cand_mask: {
     807           0 :                 BUN m = ci->nextmsk;
     808           0 :                 uint8_t b = ci->nextbit;
     809           0 :                 do {
     810           0 :                         if (b == 0) {
     811           0 :                                 b = 32;
     812           0 :                                 while (ci->mask[--m] == 0)
     813             :                                         ;
     814             :                         }
     815           0 :                 } while ((ci->mask[m] & (1U << --b)) == 0);
     816           0 :                 o = ci->mskoff + m * 32 + b;
     817           0 :                 if (++b == 32) {
     818             :                         b = 0;
     819             :                         m++;
     820             :                 }
     821             :                 break;
     822             :         }
     823             :         default:
     824           0 :                 MT_UNREACHABLE();
     825             :         }
     826             :         return o;
     827             : }
     828             : 
     829             : /* if o is a candidate, return it, else return the next candidate from
     830             :  * the cand_mask iterator ci after (if next is set) or before (if next
     831             :  * is unset) o; if there are no more candidates, return oid_nil */
     832             : oid
     833           0 : canditer_mask_next(const struct canditer *ci, oid o, bool next)
     834             : {
     835           0 :         if (o < ci->mskoff)
     836           0 :                 return next ? ci->mskoff + ci->firstbit : oid_nil;
     837           0 :         o -= ci->mskoff;
     838           0 :         BUN p = o / 32;
     839           0 :         o %= 32;
     840           0 :         if (p >= ci->nvals || (p == ci->nvals - 1 && o >= ci->lastbit))
     841           0 :                 return next ? oid_nil : canditer_last(ci);
     842           0 :         if (next) {
     843           0 :                 while ((ci->mask[p] & (1U << o)) == 0) {
     844           0 :                         if (++o == 32) {
     845           0 :                                 o = 0;
     846           0 :                                 if (++p == ci->nvals)
     847           0 :                                         return oid_nil;
     848             :                         }
     849             :                 }
     850           0 :                 if (p == ci->nvals - 1 && o >= ci->lastbit)
     851           0 :                         return oid_nil;
     852             :         } else {
     853           0 :                 while ((ci->mask[p] & (1U << o)) == 0) {
     854           0 :                         if (o == 0) {
     855           0 :                                 o = 31;
     856           0 :                                 if (p == 0)
     857           0 :                                         return oid_nil;
     858           0 :                                 p--;
     859             :                         } else {
     860           0 :                                 o--;
     861             :                         }
     862             :                 }
     863           0 :                 if (p == 0 && o < ci->firstbit)
     864           0 :                         return oid_nil;
     865             :         }
     866           0 :         return ci->mskoff + 32 * p + o;
     867             : }
     868             : 
     869             : /* return the last candidate */
     870             : oid
     871      239241 : canditer_last(const struct canditer *ci)
     872             : {
     873      239241 :         if (ci->ncand == 0)
     874           0 :                 return oid_nil;
     875      239241 :         switch (ci->tpe) {
     876      214578 :         case cand_dense:
     877      214578 :                 return ci->seq + ci->ncand - 1;
     878       17266 :         case cand_materialized:
     879       17266 :                 return ci->oids[ci->ncand - 1];
     880        6360 :         case cand_except:
     881        6360 :                 return ci->seq + ci->ncand + ci->nvals - 1;
     882        1037 :         case cand_mask:
     883       15013 :                 for (uint8_t i = ci->lastbit; i > 0; ) {
     884       15013 :                         if (ci->mask[ci->nvals - 1] & (1U << --i))
     885        1037 :                                 return ci->mskoff + (ci->nvals - 1) * 32 + i;
     886             :                 }
     887           0 :                 break;          /* cannot happen */
     888             :         default:
     889           0 :                 MT_UNREACHABLE();
     890             :         }
     891           0 :         return oid_nil;         /* cannot happen */
     892             : }
     893             : 
     894             : /* return the candidate at the given index */
     895             : oid
     896   366878750 : canditer_idx(const struct canditer *ci, BUN p)
     897             : {
     898   366878750 :         if (p >= ci->ncand)
     899           0 :                 return oid_nil;
     900   366878750 :         switch (ci->tpe) {
     901   175026049 :         case cand_dense:
     902   175026049 :                 return ci->seq + p;
     903   191551835 :         case cand_materialized:
     904   191551835 :                 return ci->oids[p];
     905      300768 :         case cand_except: {
     906      300768 :                 oid o = ci->seq + p;
     907      300768 :                 if (o < ci->oids[0])
     908             :                         return o;
     909      204353 :                 if (o + ci->nvals > ci->oids[ci->nvals - 1])
     910             :                         return o + ci->nvals;
     911      108094 :                 BUN lo = 0, hi = ci->nvals - 1;
     912      729971 :                 while (hi - lo > 1) {
     913      513783 :                         BUN mid = (hi + lo) / 2;
     914      513783 :                         if (ci->oids[mid] - mid > o)
     915             :                                 hi = mid;
     916             :                         else
     917      340688 :                                 lo = mid;
     918             :                 }
     919      108094 :                 return o + hi;
     920             :         }
     921          98 :         case cand_mask: {
     922          98 :                 BUN x;
     923          98 :                 if ((x = candmask_pop(ci->mask[0] >> ci->firstbit)) > p) {
     924          26 :                         for (uint8_t i = ci->firstbit; ; i++) {
     925         112 :                                 if (ci->mask[0] & (1U << i)) {
     926         112 :                                         if (p == 0)
     927          86 :                                                 return ci->mskoff + i;
     928          26 :                                         p--;
     929             :                                 }
     930             :                         }
     931             :                 }
     932          63 :                 for (BUN n = 1; n < ci->nvals; n++) {
     933          63 :                         uint32_t mask = ci->mask[n];
     934          63 :                         p -= x;
     935          63 :                         x = candmask_pop(mask);
     936          63 :                         if (x > p) {
     937         122 :                                 for (uint8_t i = 0; ; i++) {
     938         134 :                                         if (mask & (1U << i)) {
     939         134 :                                                 if (p == 0)
     940          12 :                                                         return ci->mskoff + n * 32 + i;
     941         122 :                                                 p--;
     942             :                                         }
     943             :                                 }
     944             :                         }
     945             :                 }
     946           0 :                 break;          /* cannot happen */
     947             :         }
     948             :         default:
     949           0 :                 MT_UNREACHABLE();
     950             :         }
     951           0 :         return oid_nil;         /* cannot happen */
     952             : }
     953             : 
     954             : /* set the index for the next candidate to be returned */
     955             : void
     956      510326 : canditer_setidx(struct canditer *ci, BUN p)
     957             : {
     958      510326 :         if (p != ci->next) {
     959      473385 :                 if (p >= ci->ncand) {
     960       52170 :                         ci->next = ci->ncand;
     961       52170 :                         switch (ci->tpe) {
     962          28 :                         case cand_except:
     963          28 :                                 ci->add = ci->nvals;
     964          28 :                                 break;
     965           0 :                         case cand_mask:
     966           0 :                                 ci->nextbit = ci->lastbit;
     967           0 :                                 ci->nextmsk = ci->nvals;
     968           0 :                                 if (ci->nextbit == 32)
     969           0 :                                         ci->nextbit = 0;
     970             :                                 else
     971           0 :                                         ci->nextmsk--;
     972             :                                 break;
     973             :                         default:
     974             :                                 break;
     975             :                         }
     976             :                 } else {
     977      421215 :                         ci->next = p;
     978      421215 :                         switch (ci->tpe) {
     979        2442 :                         case cand_except:
     980        2442 :                                 ci->add = canditer_idx(ci, p) - ci->seq - p;
     981        2442 :                                 break;
     982           0 :                         case cand_mask: {
     983           0 :                                 oid o = canditer_idx(ci, p) - ci->mskoff;
     984           0 :                                 ci->nextmsk = o / 32;
     985           0 :                                 ci->nextbit = (uint8_t) (o % 32);
     986           0 :                                 break;
     987             :                         }
     988             :                         default:
     989             :                                 break;
     990             :                         }
     991             :                 }
     992             :         }
     993      510326 : }
     994             : 
     995             : /* reset */
     996             : void
     997      563573 : canditer_reset(struct canditer *ci)
     998             : {
     999      563573 :         if (ci->tpe == cand_mask) {
    1000           0 :                 ci->nextbit = ci->firstbit;
    1001           0 :                 ci->nextmsk = 0;
    1002             :         } else {
    1003      563573 :                 ci->add = 0;
    1004             :         }
    1005      563573 :         ci->next = 0;
    1006      563573 : }
    1007             : 
    1008             : /* return index of given candidate if it occurs; if the candidate does
    1009             :  * not occur, if next is set, return index of next larger candidate,
    1010             :  * if next is not set, return BUN_NONE */
    1011             : BUN
    1012     1059550 : canditer_search(const struct canditer *ci, oid o, bool next)
    1013             : {
    1014     1059550 :         BUN p;
    1015             : 
    1016     1059550 :         switch (ci->tpe) {
    1017      965613 :         case cand_dense:
    1018      965613 :                 if (o < ci->seq)
    1019         692 :                         return next ? 0 : BUN_NONE;
    1020      964921 :                 if (o >= ci->seq + ci->ncand)
    1021      127087 :                         return next ? ci->ncand : BUN_NONE;
    1022      837834 :                 return o - ci->seq;
    1023       47386 :         case cand_materialized:
    1024       47386 :                 if (ci->nvals == 0)
    1025             :                         return 0;
    1026       47386 :                 p = binsearchcand(ci->oids, ci->nvals - 1, o);
    1027       47383 :                 if (next || (p != ci->nvals && ci->oids[p] == o))
    1028       47226 :                         return p;
    1029             :                 break;
    1030       46551 :         case cand_except:
    1031       46551 :                 if (o < ci->seq)
    1032           0 :                         return next ? 0 : BUN_NONE;
    1033       46551 :                 if (o >= ci->seq + ci->ncand + ci->nvals)
    1034         967 :                         return next ? ci->ncand : BUN_NONE;
    1035       45584 :                 p = binsearchcand(ci->oids, ci->nvals - 1, o);
    1036       45584 :                 if (next || p == ci->nvals || ci->oids[p] != o)
    1037       45429 :                         return o - ci->seq - p;
    1038             :                 break;
    1039           0 :         case cand_mask:
    1040           0 :                 if (o < ci->mskoff) {
    1041           0 :                         return next ? 0 : BUN_NONE;
    1042             :                 }
    1043           0 :                 o -= ci->mskoff;
    1044           0 :                 p = o / 32;
    1045           0 :                 if (p >= ci->nvals)
    1046           0 :                         return next ? ci->ncand : BUN_NONE;
    1047           0 :                 o %= 32;
    1048           0 :                 if (p == ci->nvals - 1 && o >= ci->lastbit)
    1049           0 :                         return next ? ci->ncand : BUN_NONE;
    1050           0 :                 if (next || ci->mask[p] & (1U << o))
    1051           0 :                         return count_mask_bits(ci, 0, p * 32 + o) + !(ci->mask[p] & (1U << o));
    1052             :                 break;
    1053             :         default:
    1054           0 :                 MT_UNREACHABLE();
    1055             :         }
    1056             :         return BUN_NONE;
    1057             : }
    1058             : 
    1059             : static BAT *
    1060        1249 : canditer_sliceval_mask(const struct canditer *ci, oid lo1, oid hi1, BUN cnt1,
    1061             :                        oid lo2, oid hi2, BUN cnt2)
    1062             : {
    1063        1249 :         assert(cnt2 == 0 || !is_oid_nil(lo2));
    1064        1249 :         assert(cnt2 == 0 || lo2 >= hi1);
    1065        1249 :         if (is_oid_nil(lo1) || lo1 < ci->mskoff + ci->firstbit)
    1066         310 :                 lo1 = ci->mskoff + ci->firstbit;
    1067        1249 :         if (is_oid_nil(hi1) || hi1 >= ci->mskoff + (ci->nvals - 1) * 32 + ci->lastbit)
    1068         987 :                 hi1 = ci->mskoff + (ci->nvals - 1) * 32 + ci->lastbit;
    1069             : 
    1070        1249 :         BAT *bn = COLnew(0, TYPE_oid, cnt1 + cnt2, TRANSIENT);
    1071        1249 :         if (bn == NULL)
    1072             :                 return NULL;
    1073        1249 :         bn->tkey = true;
    1074             : 
    1075        1249 :         if (hi1 > ci->mskoff) {
    1076        1092 :                 if (lo1 < ci->mskoff)
    1077             :                         lo1 = 0;
    1078             :                 else
    1079        1092 :                         lo1 -= ci->mskoff;
    1080        1092 :                 hi1 -= ci->mskoff;
    1081      290925 :                 for (oid o = lo1; o < hi1 && cnt1 > 0; o++) {
    1082      289833 :                         if (ci->mask[o / 32] & (1U << (o % 32))) {
    1083      249484 :                                 if (BUNappend(bn, &(oid){o + ci->mskoff}, false) != GDK_SUCCEED) {
    1084           0 :                                         BBPreclaim(bn);
    1085           0 :                                         return NULL;
    1086             :                                 }
    1087      249484 :                                 cnt1--;
    1088             :                         }
    1089             :                 }
    1090             :         }
    1091        1249 :         if (cnt2 > 0) {
    1092          98 :                 if (lo2 < ci->mskoff + ci->firstbit)
    1093             :                         lo2 = ci->mskoff + ci->firstbit;
    1094          98 :                 if (is_oid_nil(hi2) || hi2 >= ci->mskoff + (ci->nvals - 1) * 32 + ci->lastbit)
    1095          98 :                         hi2 = ci->mskoff + (ci->nvals - 1) * 32 + ci->lastbit;
    1096             : 
    1097          98 :                 lo2 -= ci->mskoff;
    1098          98 :                 hi2 -= ci->mskoff;
    1099         814 :                 for (oid o = lo2; o < hi2 && cnt2 > 0; o++) {
    1100         716 :                         if (ci->mask[o / 32] & (1U << (o % 32))) {
    1101          70 :                                 if (BUNappend(bn, &(oid){o + ci->mskoff}, false) != GDK_SUCCEED) {
    1102           0 :                                         BBPreclaim(bn);
    1103           0 :                                         return NULL;
    1104             :                                 }
    1105          70 :                                 cnt2--;
    1106             :                         }
    1107             :                 }
    1108             :         }
    1109             :         return bn;
    1110             : }
    1111             : 
    1112             : /* return either an actual BATslice or a new BAT that contains the
    1113             :  * "virtual" slice of the input candidate list BAT; except, unlike
    1114             :  * BATslice, the hseqbase of the returned BAT is 0; note for cand_mask
    1115             :  * candidate iterators, the BUN values refer to number of 1 bits */
    1116             : BAT *
    1117      465903 : canditer_slice(const struct canditer *ci, BUN lo, BUN hi)
    1118             : {
    1119      465903 :         BAT *bn;
    1120      465903 :         oid o;
    1121      465903 :         BUN add;
    1122             : 
    1123      465903 :         assert(ci != NULL);
    1124             : 
    1125      465903 :         if (lo >= ci->ncand || lo >= hi)
    1126       67761 :                 return BATdense(0, 0, 0);
    1127      398142 :         if (hi > ci->ncand)
    1128             :                 hi = ci->ncand;
    1129      398142 :         if (hi - lo == 1)
    1130      340548 :                 return BATdense(0, canditer_idx(ci, lo), 1);
    1131       57594 :         switch (ci->tpe) {
    1132        5448 :         case cand_materialized:
    1133        5448 :                 if (ci->s) {
    1134        5448 :                         bn = BATslice(ci->s, lo + ci->offset, hi + ci->offset);
    1135        5449 :                         BAThseqbase(bn, 0);
    1136        5449 :                         return bn;
    1137             :                 }
    1138           0 :                 bn = COLnew(0, TYPE_oid, hi - lo, TRANSIENT);
    1139           0 :                 if (bn == NULL)
    1140             :                         return NULL;
    1141           0 :                 BATsetcount(bn, hi - lo);
    1142           0 :                 memcpy(Tloc(bn, 0), ci->oids + lo, (hi - lo) * sizeof(oid));
    1143           0 :                 break;
    1144       51848 :         case cand_dense:
    1145       51848 :                 return BATdense(0, ci->seq + lo, hi - lo);
    1146         287 :         case cand_except:
    1147         287 :                 o = canditer_idx(ci, lo);
    1148         287 :                 add = o - ci->seq - lo;
    1149         287 :                 assert(add <= ci->nvals);
    1150         287 :                 if (add == ci->nvals || o + hi - lo < ci->oids[add]) {
    1151             :                         /* after last exception or before next
    1152             :                          * exception: return dense sequence */
    1153         243 :                         return BATdense(0, o, hi - lo);
    1154             :                 }
    1155          44 :                 bn = COLnew(0, TYPE_oid, hi - lo, TRANSIENT);
    1156          44 :                 if (bn == NULL)
    1157             :                         return NULL;
    1158          44 :                 BATsetcount(bn, hi - lo);
    1159         390 :                 for (oid *dst = Tloc(bn, 0); lo < hi; lo++) {
    1160         500 :                         while (add < ci->nvals && o == ci->oids[add]) {
    1161         154 :                                 o++;
    1162         154 :                                 add++;
    1163             :                         }
    1164         346 :                         *dst++ = o++;
    1165             :                 }
    1166             :                 break;
    1167          11 :         case cand_mask:
    1168          11 :                 return canditer_sliceval_mask(ci, canditer_idx(ci, lo),
    1169             :                                               oid_nil, hi - lo,
    1170             :                                               oid_nil, oid_nil, 0);
    1171             :         default:
    1172           0 :                 MT_UNREACHABLE();
    1173             :         }
    1174          44 :         bn->tsorted = true;
    1175          44 :         bn->trevsorted = BATcount(bn) <= 1;
    1176          44 :         bn->tkey = true;
    1177          44 :         bn->tseqbase = oid_nil;
    1178          44 :         bn->tnil = false;
    1179          44 :         bn->tnonil = true;
    1180          44 :         bn->tminpos = 0;
    1181          44 :         bn->tmaxpos = BATcount(bn) - 1;
    1182          44 :         return virtualize(bn);
    1183             : }
    1184             : 
    1185             : /* like the above, except the bounds are given by values instead of
    1186             :  * indexes */
    1187             : BAT *
    1188      341194 : canditer_sliceval(const struct canditer *ci, oid lo, oid hi)
    1189             : {
    1190      341194 :         if (ci->tpe != cand_mask) {
    1191     1020163 :                 return canditer_slice(
    1192             :                         ci,
    1193      340055 :                         is_oid_nil(lo) ? 0 : canditer_search(ci, lo, true),
    1194      340054 :                         is_oid_nil(hi) ? ci->ncand : canditer_search(ci, hi, true));
    1195             :         }
    1196             : 
    1197        1140 :         return canditer_sliceval_mask(ci, lo, hi, ci->ncand,
    1198             :                                       oid_nil, oid_nil, 0);
    1199             : }
    1200             : 
    1201             : /* return the combination of two slices */
    1202             : BAT *
    1203       29980 : canditer_slice2(const struct canditer *ci, BUN lo1, BUN hi1, BUN lo2, BUN hi2)
    1204             : {
    1205       29980 :         BAT *bn;
    1206       29980 :         oid o;
    1207       29980 :         BUN add;
    1208             : 
    1209       29980 :         assert(lo1 <= hi1);
    1210       29980 :         assert(lo2 <= hi2);
    1211       29980 :         assert(hi1 <= lo2 || (lo2 == 0 && hi2 == 0));
    1212             : 
    1213       29980 :         if (hi1 == lo2)         /* consecutive slices: combine into one */
    1214         607 :                 return canditer_slice(ci, lo1, hi2);
    1215       29373 :         if (lo2 == hi2 || hi1 >= ci->ncand || lo2 >= ci->ncand) {
    1216             :                 /* empty second slice */
    1217       24814 :                 return canditer_slice(ci, lo1, hi1);
    1218             :         }
    1219        4559 :         if (lo1 == hi1)         /* empty first slice */
    1220        1999 :                 return canditer_slice(ci, lo2, hi2);
    1221        2560 :         if (lo1 >= ci->ncand)     /* out of range */
    1222             :                 return BATdense(0, 0, 0);
    1223             : 
    1224        2560 :         if (hi2 >= ci->ncand)
    1225             :                 hi2 = ci->ncand;
    1226             : 
    1227        2560 :         bn = COLnew(0, TYPE_oid, hi1 - lo1 + hi2 - lo2, TRANSIENT);
    1228        2560 :         if (bn == NULL)
    1229             :                 return NULL;
    1230        2560 :         BATsetcount(bn, hi1 - lo1 + hi2 - lo2);
    1231        2560 :         bn->tsorted = true;
    1232        2560 :         bn->trevsorted = BATcount(bn) <= 1;
    1233        2560 :         bn->tkey = true;
    1234        2560 :         bn->tseqbase = oid_nil;
    1235        2560 :         bn->tnil = false;
    1236        2560 :         bn->tnonil = true;
    1237             : 
    1238        2560 :         oid *dst = Tloc(bn, 0);
    1239             : 
    1240        2560 :         switch (ci->tpe) {
    1241         233 :         case cand_materialized:
    1242         233 :                 memcpy(dst, ci->oids + lo1, (hi1 - lo1) * sizeof(oid));
    1243         233 :                 memcpy(dst + hi1 - lo1, ci->oids + lo2, (hi2 - lo2) * sizeof(oid));
    1244         233 :                 break;
    1245             :         case cand_dense:
    1246      926694 :                 while (lo1 < hi1)
    1247      924368 :                         *dst++ = ci->seq + lo1++;
    1248      259789 :                 while (lo2 < hi2)
    1249      257463 :                         *dst++ = ci->seq + lo2++;
    1250             :                 break;
    1251           1 :         case cand_except:
    1252           1 :                 o = canditer_idx(ci, lo1);
    1253           1 :                 add = o - ci->seq - lo1;
    1254           1 :                 assert(add <= ci->nvals);
    1255           1 :                 if (add == ci->nvals) {
    1256             :                         /* after last exception: return dense sequence */
    1257           0 :                         while (lo1 < hi1)
    1258           0 :                                 *dst++ = ci->seq + add + lo1++;
    1259             :                 } else {
    1260           2 :                         while (lo1 < hi1) {
    1261           1 :                                 while (add < ci->nvals && o == ci->oids[add]) {
    1262           0 :                                         o++;
    1263           0 :                                         add++;
    1264             :                                 }
    1265           1 :                                 *dst++ = o++;
    1266           1 :                                 lo1++;
    1267             :                         }
    1268             :                 }
    1269           1 :                 o = canditer_idx(ci, lo2);
    1270           1 :                 add = o - ci->seq - lo2;
    1271           1 :                 assert(add <= ci->nvals);
    1272           1 :                 if (add == ci->nvals) {
    1273             :                         /* after last exception: return dense sequence */
    1274           0 :                         while (lo2 < hi2)
    1275           0 :                                 *dst++ = ci->seq + add + lo2++;
    1276             :                 } else {
    1277           4 :                         while (lo2 < hi2) {
    1278           6 :                                 while (add < ci->nvals && o == ci->oids[add]) {
    1279           3 :                                         o++;
    1280           3 :                                         add++;
    1281             :                                 }
    1282           3 :                                 *dst++ = o++;
    1283           3 :                                 lo2++;
    1284             :                         }
    1285             :                 }
    1286             :                 break;
    1287           0 :         case cand_mask:
    1288           0 :                 return canditer_sliceval_mask(ci, canditer_idx(ci, lo1),
    1289             :                                               oid_nil, hi1 - lo1,
    1290             :                                               canditer_idx(ci, lo2),
    1291             :                                               oid_nil, hi2 - lo2);
    1292             :         default:
    1293           0 :                 MT_UNREACHABLE();
    1294             :         }
    1295        2560 :         return virtualize(bn);
    1296             : }
    1297             : 
    1298             : BAT *
    1299       30033 : canditer_slice2val(const struct canditer *ci, oid lo1, oid hi1, oid lo2, oid hi2)
    1300             : {
    1301       30033 :         if (ci->tpe != cand_mask) {
    1302      119504 :                 return canditer_slice2(
    1303             :                         ci,
    1304       29426 :                         is_oid_nil(lo1) ? 0 : canditer_search(ci, lo1, true),
    1305       29935 :                         is_oid_nil(hi1) ? ci->ncand : canditer_search(ci, hi1, true),
    1306       29936 :                         is_oid_nil(lo2) ? 0 : canditer_search(ci, lo2, true),
    1307         272 :                         is_oid_nil(hi2) ? ci->ncand : canditer_search(ci, hi2, true));
    1308             :         }
    1309             : 
    1310          98 :         return canditer_sliceval_mask(ci, lo1, hi1, ci->ncand,
    1311          98 :                                       lo2, hi2, ci->ncand);
    1312             : }
    1313             : 
    1314             : BAT *
    1315        3226 : BATnegcands(oid tseq, BUN nr, BAT *odels)
    1316             : {
    1317        3226 :         const char *nme;
    1318        3226 :         Heap *dels;
    1319        3226 :         BUN lo, hi;
    1320        3226 :         ccand_t *c;
    1321        3226 :         BAT *bn;
    1322             : 
    1323        3226 :         bn = BATdense(0, tseq, nr);
    1324        3226 :         if (bn == NULL)
    1325             :                 return NULL;
    1326        3226 :         if (BATcount(odels) == 0)
    1327        2799 :                 goto doreturn;
    1328             : 
    1329         427 :         lo = SORTfndfirst(odels, &bn->tseqbase);
    1330         427 :         hi = SORTfndfirst(odels, &(oid) {bn->tseqbase + BATcount(bn)});
    1331         427 :         if (lo == hi)
    1332             :                 return bn;
    1333         427 :         if (lo + nr == hi) {
    1334         166 :                 BATsetcount(bn, 0);
    1335         166 :                 goto doreturn;
    1336             :         }
    1337             : 
    1338         261 :         nme = BBP_physical(bn->batCacheid);
    1339         261 :         if ((dels = GDKmalloc(sizeof(Heap))) == NULL){
    1340           0 :                 BBPreclaim(bn);
    1341           0 :                 return NULL;
    1342             :         }
    1343         522 :         *dels = (Heap) {
    1344         261 :                 .farmid = BBPselectfarm(bn->batRole, bn->ttype, varheap),
    1345         261 :                 .parentid = bn->batCacheid,
    1346             :                 .dirty = true,
    1347             :                 .refs = ATOMIC_VAR_INIT(1),
    1348             :         };
    1349         261 :         strconcat_len(dels->filename, sizeof(dels->filename),
    1350             :                       nme, ".theap", NULL);
    1351             : 
    1352         522 :         if (dels->farmid < 0 ||
    1353         261 :             HEAPalloc(dels, hi - lo + (sizeof(ccand_t)/sizeof(oid)), sizeof(oid)) != GDK_SUCCEED) {
    1354           0 :                 GDKfree(dels);
    1355           0 :                 BBPreclaim(bn);
    1356           0 :                 return NULL;
    1357             :         }
    1358         261 :         c = (ccand_t *) dels->base;
    1359         261 :         *c = (ccand_t) {
    1360             :                 .type = CAND_NEGOID,
    1361             :         };
    1362         261 :         dels->free = sizeof(ccand_t) + sizeof(oid) * (hi - lo);
    1363         261 :         BATiter bi = bat_iterator(odels);
    1364         261 :         if (bi.type == TYPE_void) {
    1365         143 :                 oid *r = (oid *) (dels->base + sizeof(ccand_t));
    1366       93625 :                 for (BUN x = lo; x < hi; x++)
    1367       93482 :                         r[x - lo] = x + odels->tseqbase;
    1368             :         } else {
    1369         118 :                 oid *r = (oid *) (dels->base + sizeof(ccand_t));
    1370         118 :                 memcpy(r, (const oid *) bi.base + lo, sizeof(oid) * (hi - lo));
    1371             :         }
    1372         261 :         bat_iterator_end(&bi);
    1373         261 :         assert(bn->tvheap == NULL);
    1374         261 :         bn->tvheap = dels;
    1375         261 :         BATsetcount(bn, bn->batCount - (hi - lo));
    1376        3226 :   doreturn:
    1377        3226 :         TRC_DEBUG(ALGO, "nr=" BUNFMT ", odels=" ALGOBATFMT
    1378             :                   " -> " ALGOBATFMT "\n",
    1379             :                   nr, ALGOBATPAR(odels),
    1380             :                   ALGOBATPAR(bn));
    1381             :         return bn;
    1382             : }
    1383             : 
    1384             : BAT *
    1385      124363 : BATmaskedcands(oid hseq, BUN nr, BAT *masked, bool selected)
    1386             : {
    1387      124363 :         const char *nme;
    1388      124363 :         Heap *msks;
    1389      124363 :         ccand_t *c;
    1390      124363 :         BUN nmask;
    1391      124363 :         BAT *bn;
    1392             : 
    1393      124363 :         assert(masked->ttype == TYPE_msk);
    1394             : 
    1395      124363 :         bn = COLnew(hseq, TYPE_void, 0, TRANSIENT);
    1396      124363 :         if (bn == NULL)
    1397             :                 return NULL;
    1398      124363 :         BATtseqbase(bn, hseq);
    1399             : 
    1400      124363 :         if (BATcount(masked) == 0)
    1401             :                 return bn;
    1402             : 
    1403      124363 :         nme = BBP_physical(bn->batCacheid);
    1404      124363 :         if ((msks = GDKmalloc(sizeof(Heap))) == NULL){
    1405           0 :                 BBPreclaim(bn);
    1406           0 :                 return NULL;
    1407             :         }
    1408      248726 :         *msks = (Heap) {
    1409      124363 :                 .farmid = BBPselectfarm(bn->batRole, bn->ttype, varheap),
    1410      124363 :                 .parentid = bn->batCacheid,
    1411             :                 .dirty = true,
    1412             :                 .refs = ATOMIC_VAR_INIT(1),
    1413             :         };
    1414      124363 :         strconcat_len(msks->filename, sizeof(msks->filename),
    1415             :                       nme, ".theap", NULL);
    1416             : 
    1417      124363 :         nmask = (nr + 31) / 32;
    1418      248726 :         if (msks->farmid < 0 ||
    1419      124363 :             HEAPalloc(msks, nmask + (sizeof(ccand_t)/sizeof(uint32_t)), sizeof(uint32_t)) != GDK_SUCCEED) {
    1420           0 :                 GDKfree(msks);
    1421           0 :                 BBPreclaim(bn);
    1422           0 :                 return NULL;
    1423             :         }
    1424      124363 :         c = (ccand_t *) msks->base;
    1425      124363 :         *c = (ccand_t) {
    1426             :                 .type = CAND_MSK,
    1427             : //              .mask = true,
    1428             :         };
    1429      124363 :         msks->free = sizeof(ccand_t) + nmask * sizeof(uint32_t);
    1430      124363 :         uint32_t *r = (uint32_t*)(msks->base + sizeof(ccand_t));
    1431      124363 :         BATiter bi = bat_iterator(masked);
    1432      124363 :         if (selected) {
    1433      124319 :                 if (nr <= bi.count)
    1434      124319 :                         memcpy(r, bi.base, nmask * sizeof(uint32_t));
    1435             :                 else
    1436           0 :                         memcpy(r, bi.base, (bi.count + 31) / 32 * sizeof(uint32_t));
    1437             :         } else {
    1438          44 :                 const uint32_t *s = (const uint32_t *) bi.base;
    1439          44 :                 BUN nmask_ = (bi.count + 31) / 32;
    1440        6498 :                 for (BUN i = 0; i < nmask_; i++)
    1441        6454 :                         r[i] = ~s[i];
    1442             :         }
    1443      124363 :         if (nr > bi.count) {
    1444           0 :                 BUN rest = bi.count & 31;
    1445           0 :                 BUN nmask_ = (bi.count + 31) / 32;
    1446           0 :                 if (rest > 0)
    1447           0 :                         r[nmask_ -1] |= ((1U << (32 - rest)) - 1) << rest;
    1448           0 :                 for (BUN j = nmask_; j < nmask; j++)
    1449           0 :                         r[j] = ~0;
    1450             :         }
    1451      124363 :         bat_iterator_end(&bi);
    1452             :         /* make sure last word doesn't have any spurious bits set */
    1453      124363 :         BUN cnt = nr % 32;
    1454      124363 :         if (cnt > 0)
    1455      122316 :                 r[nmask - 1] &= (1U << cnt) - 1;
    1456             :         cnt = 0;
    1457    12535034 :         for (BUN i = 0; i < nmask; i++) {
    1458    12410671 :                 if (cnt == 0 && r[i] != 0)
    1459      124022 :                         c->firstbit = candmask_lobit(r[i]) + i * 32;
    1460    12410671 :                 cnt += candmask_pop(r[i]);
    1461             :         }
    1462      124363 :         if (cnt > 0) {
    1463      124024 :                 assert(bn->tvheap == NULL);
    1464      124024 :                 bn->tvheap = msks;
    1465      124024 :                 bn->tseqbase += (oid) c->firstbit;
    1466             :         } else {
    1467             :                 /* no point having a mask if it's empty */
    1468         339 :                 HEAPfree(msks, true);
    1469         339 :                 GDKfree(msks);
    1470             :         }
    1471      124363 :         BATsetcount(bn, cnt);
    1472      124363 :         TRC_DEBUG(ALGO, "hseq=" OIDFMT ", masked=" ALGOBATFMT ", selected=%s"
    1473             :                   " -> " ALGOBATFMT "\n",
    1474             :                   hseq, ALGOBATPAR(masked),
    1475             :                   selected ? "true" : "false",
    1476             :                   ALGOBATPAR(bn));
    1477      124363 :         assert(bn->tseqbase != oid_nil);
    1478             :         return bn;
    1479             : }
    1480             : 
    1481             : /* convert a masked candidate list to a positive or negative candidate list */
    1482             : BAT *
    1483      118688 : BATunmask(BAT *b)
    1484             : {
    1485             : //      assert(!mask_cand(b) || CCAND(b)->mask); /* todo handle negmask case */
    1486      118688 :         BUN cnt;
    1487      118688 :         uint32_t rem;
    1488      118688 :         uint32_t val;
    1489      118688 :         const uint32_t *src;
    1490      118688 :         oid *dst;
    1491      118688 :         BUN n = 0;
    1492      118688 :         oid tseq = b->hseqbase;
    1493      118688 :         bool negcand = false;
    1494             : 
    1495      118688 :         BATiter bi = bat_iterator(b);
    1496      118687 :         if (mask_cand(b)) {
    1497      113399 :                 cnt = ccand_free(b) / sizeof(uint32_t);
    1498      113399 :                 rem = 0;
    1499      113399 :                 src = (const uint32_t *) ccand_first(b);
    1500      113399 :                 tseq = b->tseqbase;
    1501      113399 :                 tseq -= (oid) CCAND(b)->firstbit;
    1502             :                 /* create negative candidate list if more than half the
    1503             :                  * bits are set */
    1504      113399 :                 negcand = BATcount(b) > cnt * 16;
    1505             :         } else {
    1506        5288 :                 cnt = bi.count / 32;
    1507        5288 :                 rem = bi.count % 32;
    1508        5288 :                 src = (const uint32_t *) bi.base;
    1509             :         }
    1510      118687 :         BAT *bn;
    1511             : 
    1512      118687 :         if (negcand) {
    1513       96456 :                 bn = COLnew(b->hseqbase, TYPE_void, 0, TRANSIENT);
    1514       96456 :                 if (bn == NULL) {
    1515           0 :                         bat_iterator_end(&bi);
    1516           0 :                         return NULL;
    1517             :                 }
    1518       96456 :                 Heap *dels;
    1519       96456 :                 if ((dels = GDKmalloc(sizeof(Heap))) == NULL) {
    1520           0 :                         BBPreclaim(bn);
    1521           0 :                         bat_iterator_end(&bi);
    1522           0 :                         return NULL;
    1523             :                 }
    1524      192912 :                 *dels = (Heap) {
    1525       96456 :                         .farmid = BBPselectfarm(TRANSIENT, TYPE_void, varheap),
    1526       96456 :                         .parentid = bn->batCacheid,
    1527             :                         .dirty = true,
    1528             :                         .refs = ATOMIC_VAR_INIT(1),
    1529             :                 };
    1530       96456 :                 strconcat_len(dels->filename, sizeof(dels->filename),
    1531       96456 :                               BBP_physical(bn->batCacheid), ".theap", NULL);
    1532             : 
    1533      192911 :                 if (dels->farmid < 0 ||
    1534       96456 :                     HEAPalloc(dels, cnt * 32 - bi.count
    1535             :                               + sizeof(ccand_t) / sizeof(oid), sizeof(oid)) != GDK_SUCCEED) {
    1536           0 :                         GDKfree(dels);
    1537           0 :                         BBPreclaim(bn);
    1538           0 :                         bat_iterator_end(&bi);
    1539           0 :                         return NULL;
    1540             :                 }
    1541       96455 :                 * (ccand_t *) dels->base = (ccand_t) {
    1542             :                         .type = CAND_NEGOID,
    1543             :                 };
    1544       96455 :                 dst = (oid *) (dels->base + sizeof(ccand_t));
    1545     8065857 :                 for (BUN p = 0, v = 0; p < cnt; p++, v += 32) {
    1546     7969402 :                         if ((val = src[p]) == ~UINT32_C(0))
    1547     6056544 :                                 continue;
    1548    61544506 :                         for (uint32_t i = 0; i < 32; i++) {
    1549    59727625 :                                 if ((val & (1U << i)) == 0) {
    1550    49595847 :                                         if (v + i >= b->batCount + n)
    1551             :                                                 break;
    1552    49499870 :                                         dst[n++] = tseq + v + i;
    1553             :                                 }
    1554             :                         }
    1555             :                 }
    1556       96455 :                 if (n == 0) {
    1557             :                         /* didn't need it after all */
    1558          10 :                         HEAPfree(dels, true);
    1559          10 :                         GDKfree(dels);
    1560             :                 } else {
    1561       96445 :                         dels->free = sizeof(ccand_t) + n * sizeof(oid);
    1562       96445 :                         dels->dirty = true;
    1563       96445 :                         assert(bn->tvheap == NULL);
    1564       96445 :                         bn->tvheap = dels;
    1565             :                 }
    1566       96455 :                 BATsetcount(bn, n=bi.count);
    1567       96456 :                 bn->tseqbase = tseq;
    1568             :         } else {
    1569       22231 :                 bn = COLnew(b->hseqbase, TYPE_oid, mask_cand(b) ? bi.count : 1024, TRANSIENT);
    1570       22231 :                 if (bn == NULL) {
    1571           0 :                         bat_iterator_end(&bi);
    1572           0 :                         return NULL;
    1573             :                 }
    1574       22231 :                 dst = (oid *) Tloc(bn, 0);
    1575     2698850 :                 for (BUN p = 0; p < cnt; p++) {
    1576     2676618 :                         if ((val = src[p]) == 0)
    1577     1901724 :                                 continue;
    1578    25561316 :                         for (uint32_t i = 0; i < 32; i++) {
    1579    24786421 :                                 if (val & (1U << i)) {
    1580    21508621 :                                         if (n == BATcapacity(bn)) {
    1581          79 :                                                 BATsetcount(bn, n);
    1582          80 :                                                 if (BATextend(bn, BATgrows(bn)) != GDK_SUCCEED) {
    1583           0 :                                                         BBPreclaim(bn);
    1584           0 :                                                         bat_iterator_end(&bi);
    1585           0 :                                                         return NULL;
    1586             :                                                 }
    1587          80 :                                                 dst = (oid *) Tloc(bn, 0);
    1588             :                                         }
    1589    21508622 :                                         dst[n++] = tseq + p * 32 + i;
    1590             :                                 }
    1591             :                         }
    1592             :                 }
    1593             :                 /* the last partial mask word */
    1594       22232 :                 if (rem > 0 && (val = src[cnt]) != 0) {
    1595       26957 :                         for (uint32_t i = 0; i < rem; i++) {
    1596       24017 :                                 if (val & (1U << i)) {
    1597        1393 :                                         if (n == BATcapacity(bn)) {
    1598           0 :                                                 BATsetcount(bn, n);
    1599           0 :                                                 if (BATextend(bn, BATgrows(bn)) != GDK_SUCCEED) {
    1600           0 :                                                         BBPreclaim(bn);
    1601           0 :                                                         bat_iterator_end(&bi);
    1602           0 :                                                         return NULL;
    1603             :                                                 }
    1604           0 :                                                 dst = (oid *) Tloc(bn, 0);
    1605             :                                         }
    1606        1393 :                                         dst[n++] = tseq + cnt * 32 + i;
    1607             :                                 }
    1608             :                         }
    1609             :                 }
    1610       22232 :                 BATsetcount(bn, n);
    1611             :         }
    1612      118688 :         bat_iterator_end(&bi);
    1613      118687 :         bn->tkey = true;
    1614      118687 :         bn->tsorted = true;
    1615      118687 :         bn->trevsorted = n <= 1;
    1616      118687 :         bn->tnil = false;
    1617      118687 :         bn->tnonil = true;
    1618      118687 :         bn = virtualize(bn);
    1619      118688 :         TRC_DEBUG(ALGO, ALGOBATFMT " -> " ALGOBATFMT "\n", ALGOBATPAR(b), ALGOBATPAR(bn));
    1620             :         return bn;
    1621             : }

Generated by: LCOV version 1.14