LCOV - code coverage report
Current view: top level - gdk - gdk_cand.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 756 980 77.1 %
Date: 2024-10-04 20:04:04 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 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             : #include "gdk_cand.h"
      17             : 
      18             : bool
      19        8591 : BATiscand(BAT *b)
      20             : {
      21        8591 :         if (ATOMtype(b->ttype) != TYPE_oid)
      22             :                 return false;
      23        8591 :         if (complex_cand(b))
      24             :                 return true;
      25        8173 :         if (b->ttype == TYPE_void && is_oid_nil(b->tseqbase))
      26             :                 return false;
      27       16346 :         return b->tsorted && BATtkey(b);
      28             : }
      29             : 
      30             : /* create a new, dense candidate list with values from `first' up to,
      31             :  * but not including, `last' */
      32             : static inline BAT *
      33        2805 : newdensecand(oid first, oid last)
      34             : {
      35        2805 :         if (last <= first)
      36           0 :                 first = last = 0; /* empty range */
      37        2805 :         return BATdense(0, first, last - first);
      38             : }
      39             : 
      40             : /* merge two candidate lists and produce a new one
      41             :  *
      42             :  * candidate lists are VOID-headed BATs with an OID tail which is
      43             :  * sorted and unique.
      44             :  */
      45             : BAT *
      46       80353 : BATmergecand(BAT *a, BAT *b)
      47             : {
      48       80353 :         BAT *bn;
      49       80353 :         oid *restrict p, i;
      50       80353 :         struct canditer cia, cib;
      51             : 
      52       80353 :         BATcheck(a, NULL);
      53       80353 :         BATcheck(b, NULL);
      54             : 
      55       80353 :         canditer_init(&cia, NULL, a);
      56       80347 :         canditer_init(&cib, NULL, b);
      57             : 
      58             :         /* we can return a if b is empty (and v.v.) */
      59       80352 :         if (cia.ncand == 0) {
      60       12581 :                 bn = canditer_slice(&cib, 0, cib.ncand);
      61       12581 :                 goto doreturn;
      62             :         }
      63       67771 :         if (cib.ncand == 0) {
      64       10753 :                 bn = canditer_slice(&cia, 0, cia.ncand);
      65       10752 :                 goto doreturn;
      66             :         }
      67             :         /* we can return a if a fully covers b (and v.v) */
      68       57018 :         if (cia.tpe == cand_dense && cib.tpe == cand_dense) {
      69             :                 /* both are dense */
      70       10797 :                 if (cia.seq <= cib.seq && cib.seq <= cia.seq + cia.ncand) {
      71             :                         /* partial overlap starting with a, or b is
      72             :                          * smack bang after a */
      73        2365 :                         bn = newdensecand(cia.seq, cia.seq + cia.ncand < cib.seq + cib.ncand ? cib.seq + cib.ncand : cia.seq + cia.ncand);
      74        2365 :                         goto doreturn;
      75             :                 }
      76        8432 :                 if (cib.seq <= cia.seq && cia.seq <= cib.seq + cib.ncand) {
      77             :                         /* partial overlap starting with b, or a is
      78             :                          * smack bang after b */
      79         431 :                         bn = newdensecand(cib.seq, cia.seq + cia.ncand < cib.seq + cib.ncand ? cib.seq + cib.ncand : cia.seq + cia.ncand);
      80         431 :                         goto doreturn;
      81             :                 }
      82             :         }
      83       54222 :         if (cia.tpe == cand_dense
      84       11002 :             && cia.seq <= cib.seq
      85        4864 :             && canditer_last(&cia) >= canditer_last(&cib)) {
      86         342 :                 bn = canditer_slice(&cia, 0, cia.ncand);
      87         342 :                 goto doreturn;
      88             :         }
      89       53880 :         if (cib.tpe == cand_dense
      90       37922 :             && cib.seq <= cia.seq
      91        9987 :             && canditer_last(&cib) >= canditer_last(&cia)) {
      92         186 :                 bn = canditer_slice(&cib, 0, cib.ncand);
      93         186 :                 goto doreturn;
      94             :         }
      95             : 
      96       53693 :         bn = COLnew(0, TYPE_oid, cia.ncand + cib.ncand, TRANSIENT);
      97       53689 :         if (bn == NULL)
      98           0 :                 goto doreturn;
      99       53689 :         p = (oid *) Tloc(bn, 0);
     100       53689 :         if (cia.tpe == cand_dense && cib.tpe == cand_dense) {
     101             :                 /* both lists are dense */
     102        8001 :                 if (cia.seq > cib.seq) {
     103        4029 :                         struct canditer ci;
     104        4029 :                         ci = cia;
     105        4029 :                         cia = cib;
     106        4029 :                         cib = ci;
     107             :                 }
     108             :                 /* cia completely before cib */
     109        8001 :                 assert(cia.seq + cia.ncand < cib.seq);
     110       30580 :                 for (i = cia.seq; i < cia.seq + cia.ncand; i++)
     111       22579 :                         *p++ = i;
     112             :                 /* there is a gap */
     113       27072 :                 for (i = cib.seq; i < cib.seq + cib.ncand; i++)
     114       19071 :                         *p++ = i;
     115       45688 :         } else if (cia.tpe == cand_dense || cib.tpe == cand_dense) {
     116       32392 :                 if (cib.tpe == cand_dense) {
     117       29736 :                         struct canditer ci;
     118       29736 :                         ci = cia;
     119       29736 :                         cia = cib;
     120       29736 :                         cib = ci;
     121             :                 }
     122             :                 /* only cia is dense */
     123             : 
     124             :                 /* copy part of cib that's before the start of cia */
     125      132472 :                 while (canditer_peek(&cib) < cia.seq) {
     126      100079 :                         *p++ = canditer_next(&cib);
     127             :                 }
     128             :                 /* copy all of cia */
     129       74804 :                 for (i = cia.seq; i < cia.seq + cia.ncand; i++)
     130       42413 :                         *p++ = i;
     131             :                 /* skip over part of cib that overlaps with cia */
     132       32391 :                 canditer_setidx(&cib, canditer_search(&cib, cia.seq + cia.ncand, true));
     133             :                 /* copy rest of cib */
     134      123232 :                 while (cib.next < cib.ncand) {
     135       90844 :                         *p++ = canditer_next(&cib);
     136             :                 }
     137             :         } else {
     138             :                 /* a and b are both not dense */
     139       13296 :                 oid ao = canditer_next(&cia);
     140       13298 :                 oid bo = canditer_next(&cib);
     141    24146499 :                 while (!is_oid_nil(ao) && !is_oid_nil(bo)) {
     142    24133202 :                         if (ao < bo) {
     143    14169905 :                                 *p++ = ao;
     144    14169905 :                                 ao = canditer_next(&cia);
     145     9963297 :                         } else if (bo < ao) {
     146     8901098 :                                 *p++ = bo;
     147     8901098 :                                 bo = canditer_next(&cib);
     148             :                         } else {
     149     1062199 :                                 *p++ = ao;
     150     1062199 :                                 ao = canditer_next(&cia);
     151     1062197 :                                 bo = canditer_next(&cib);
     152             :                         }
     153             :                 }
     154      574046 :                 while (!is_oid_nil(ao)) {
     155      560749 :                         *p++ = ao;
     156      560749 :                         ao = canditer_next(&cia);
     157             :                 }
     158      418711 :                 while (!is_oid_nil(bo)) {
     159      405415 :                         *p++ = bo;
     160      405415 :                         bo = canditer_next(&cib);
     161             :                 }
     162             :         }
     163             : 
     164             :         /* properties */
     165       53687 :         BATsetcount(bn, (BUN) (p - (oid *) Tloc(bn, 0)));
     166       53684 :         bn->trevsorted = BATcount(bn) <= 1;
     167       53684 :         bn->tsorted = true;
     168       53684 :         bn->tkey = true;
     169       53684 :         bn->tnil = false;
     170       53684 :         bn->tnonil = true;
     171       53684 :         bn = virtualize(bn);
     172       80348 :   doreturn:
     173       80348 :         TRC_DEBUG(ALGO, ALGOBATFMT "," ALGOBATFMT " -> " ALGOOPTBATFMT "\n",
     174             :                   ALGOBATPAR(a), ALGOBATPAR(b), ALGOOPTBATPAR(bn));
     175             :         return bn;
     176             : }
     177             : 
     178             : /* intersect two candidate lists and produce a new one
     179             :  *
     180             :  * candidate lists are VOID-headed BATs with an OID tail which is
     181             :  * sorted and unique.
     182             :  */
     183             : BAT *
     184          34 : BATintersectcand(BAT *a, BAT *b)
     185             : {
     186          34 :         BAT *bn;
     187          34 :         oid *restrict p;
     188          34 :         struct canditer cia, cib;
     189             : 
     190          34 :         BATcheck(a, NULL);
     191          34 :         BATcheck(b, NULL);
     192             : 
     193          34 :         canditer_init(&cia, NULL, a);
     194          34 :         canditer_init(&cib, NULL, b);
     195             : 
     196          34 :         if (cia.ncand == 0 || cib.ncand == 0) {
     197           7 :                 bn = BATdense(0, 0, 0);
     198           7 :                 goto doreturn;
     199             :         }
     200             : 
     201          27 :         if (cia.tpe == cand_dense && cib.tpe == cand_dense) {
     202             :                 /* both lists are dense */
     203           9 :                 bn = newdensecand(MAX(cia.seq, cib.seq), MIN(cia.seq + cia.ncand, cib.seq + cib.ncand));
     204           9 :                 goto doreturn;
     205             :         }
     206             : 
     207          18 :         bn = COLnew(0, TYPE_oid, MIN(cia.ncand, cib.ncand), TRANSIENT);
     208          18 :         if (bn == NULL)
     209           0 :                 goto doreturn;
     210          18 :         p = (oid *) Tloc(bn, 0);
     211          18 :         if (cia.tpe == cand_dense || cib.tpe == cand_dense) {
     212          18 :                 if (cib.tpe == cand_dense) {
     213          18 :                         struct canditer ci;
     214          18 :                         ci = cia;
     215          18 :                         cia = cib;
     216          18 :                         cib = ci;
     217             :                 }
     218             :                 /* only cia is dense */
     219             : 
     220             :                 /* search for first value in cib that is contained in cia */
     221          18 :                 canditer_setidx(&cib, canditer_search(&cib, cia.seq, true));
     222          18 :                 oid bo;
     223          62 :                 while (!is_oid_nil(bo = canditer_next(&cib)) && bo < cia.seq + cia.ncand)
     224          44 :                         *p++ = bo;
     225             :         } else {
     226             :                 /* a and b are both not dense */
     227           0 :                 oid ao = canditer_next(&cia);
     228           0 :                 oid bo = canditer_next(&cib);
     229           0 :                 while (!is_oid_nil(ao) && !is_oid_nil(bo)) {
     230           0 :                         if (ao < bo)
     231           0 :                                 ao = canditer_next(&cia);
     232           0 :                         else if (bo < ao)
     233           0 :                                 bo = canditer_next(&cib);
     234             :                         else {
     235           0 :                                 *p++ = ao;
     236           0 :                                 ao = canditer_next(&cia);
     237           0 :                                 bo = canditer_next(&cib);
     238             :                         }
     239             :                 }
     240             :         }
     241             : 
     242             :         /* properties */
     243          18 :         BATsetcount(bn, (BUN) (p - (oid *) Tloc(bn, 0)));
     244          18 :         bn->trevsorted = BATcount(bn) <= 1;
     245          18 :         bn->tsorted = true;
     246          18 :         bn->tkey = true;
     247          18 :         bn->tnil = false;
     248          18 :         bn->tnonil = true;
     249          18 :         bn = virtualize(bn);
     250          34 :   doreturn:
     251          34 :         TRC_DEBUG(ALGO, ALGOBATFMT "," ALGOBATFMT " -> " ALGOOPTBATFMT "\n",
     252             :                   ALGOBATPAR(a), ALGOBATPAR(b), ALGOOPTBATPAR(bn));
     253             :         return bn;
     254             : }
     255             : 
     256             : /* calculate the difference of two candidate lists and produce a new one
     257             :  */
     258             : BAT *
     259        5871 : BATdiffcand(BAT *a, BAT *b)
     260             : {
     261        5871 :         BAT *bn;
     262        5871 :         oid *restrict p;
     263        5871 :         struct canditer cia, cib;
     264             : 
     265        5871 :         BATcheck(a, NULL);
     266        5871 :         BATcheck(b, NULL);
     267             : 
     268        5871 :         canditer_init(&cia, NULL, a);
     269        5871 :         canditer_init(&cib, NULL, b);
     270             : 
     271        5871 :         if (cia.ncand == 0) {
     272           0 :                 bn = BATdense(0, 0, 0);
     273           0 :                 goto doreturn;
     274             :         }
     275        5871 :         if (cib.ncand == 0) {
     276         467 :                 bn = canditer_slice(&cia, 0, cia.ncand);
     277         467 :                 goto doreturn;
     278             :         }
     279             : 
     280        5404 :         if (cib.seq > canditer_last(&cia) || canditer_last(&cib) < cia.seq) {
     281             :                 /* no overlap, return a */
     282           0 :                 bn = canditer_slice(&cia, 0, cia.ncand);
     283           0 :                 goto doreturn;
     284             :         }
     285             : 
     286        5403 :         if (cia.tpe == cand_dense && cib.tpe == cand_dense) {
     287             :                 /* both a and b are dense */
     288        1940 :                 if (cia.seq < cib.seq) {
     289             :                         /* a starts before b */
     290         669 :                         if (cia.seq + cia.ncand <= cib.seq + cib.ncand) {
     291             :                                 /* b overlaps with end of a */
     292          95 :                                 bn = canditer_slice(&cia, 0, cib.seq - cia.seq);
     293          95 :                                 goto doreturn;
     294             :                         }
     295             :                         /* b is subset of a */
     296         574 :                         bn = canditer_slice2(&cia, 0, cib.seq - cia.seq,
     297             :                                              cib.seq + cib.ncand - cia.seq,
     298             :                                              cia.ncand);
     299         574 :                         goto doreturn;
     300             :                 } else {
     301             :                         /* cia.seq >= cib.seq */
     302        1271 :                         if (cia.seq + cia.ncand > cib.seq + cib.ncand) {
     303             :                                 /* b overlaps with beginning of a */
     304          72 :                                 bn = canditer_slice(&cia, cib.seq + cib.ncand - cia.seq, cia.ncand);
     305          72 :                                 goto doreturn;
     306             :                         }
     307             :                         /* a is subset f b */
     308        1199 :                         bn = BATdense(0, 0, 0);
     309        1200 :                         goto doreturn;
     310             :                 }
     311             :         }
     312        3463 :         if (cib.tpe == cand_dense) {
     313             :                 /* b is dense and a is not: we can copy the part of a
     314             :                  * that is before the start of b and the part of a
     315             :                  * that is after the end of b */
     316        4960 :                 bn = canditer_slice2val(&cia, oid_nil, cib.seq,
     317        2480 :                                         cib.seq + cib.ncand, oid_nil);
     318        2480 :                 goto doreturn;
     319             :         }
     320             : 
     321             :         /* b is not dense */
     322         983 :         bn = COLnew(0, TYPE_oid, BATcount(a), TRANSIENT);
     323         983 :         if (bn == NULL)
     324           0 :                 goto doreturn;
     325         983 :         p = Tloc(bn, 0);
     326             :         /* find first position in b that is in range of a */
     327         983 :         canditer_setidx(&cib, canditer_search(&cib, cia.seq, true));
     328             :         {
     329             :                 /* because we may jump over this declaration, we put
     330             :                  * it inside a block */
     331         983 :                 oid ob = canditer_next(&cib);
     332     7589662 :                 for (BUN i = 0; i < cia.ncand; i++) {
     333     7588679 :                         oid oa = canditer_next(&cia);
     334    10202514 :                         while (!is_oid_nil(ob) && ob < oa) {
     335     2613267 :                                 ob = canditer_next(&cib);
     336             :                         }
     337     7588679 :                         if (is_oid_nil(ob) || oa < ob)
     338     4978694 :                                 *p++ = oa;
     339             :                 }
     340             :         }
     341             : 
     342             :         /* properties */
     343         983 :         BATsetcount(bn, (BUN) (p - (oid *) Tloc(bn, 0)));
     344         983 :         bn->trevsorted = BATcount(bn) <= 1;
     345         983 :         bn->tsorted = true;
     346         983 :         bn->tkey = true;
     347         983 :         bn->tnil = false;
     348         983 :         bn->tnonil = true;
     349         983 :         bn = virtualize(bn);
     350        5871 :   doreturn:
     351        5871 :         TRC_DEBUG(ALGO, ALGOBATFMT "," ALGOBATFMT " -> " ALGOOPTBATFMT "\n",
     352             :                   ALGOBATPAR(a), ALGOBATPAR(b), ALGOOPTBATPAR(bn));
     353             :         return bn;
     354             : }
     355             : 
     356             : /* return offset of first value in cand that is >= o */
     357             : static inline BUN
     358      927316 : binsearchcand(const oid *cand, BUN hi, oid o)
     359             : {
     360      927316 :         BUN lo = 0;
     361             : 
     362      927316 :         if (o <= cand[lo])
     363             :                 return 0;
     364      459039 :         if (o > cand[hi])
     365      408325 :                 return hi + 1;
     366             :         /* loop invariant: cand[lo] < o <= cand[hi] */
     367      275303 :         while (hi > lo + 1) {
     368      246132 :                 BUN mid = (lo + hi) / 2;
     369      246132 :                 if (cand[mid] == o)
     370       21543 :                         return mid;
     371      224589 :                 if (cand[mid] < o)
     372             :                         lo = mid;
     373             :                 else
     374      159263 :                         hi = mid;
     375             :         }
     376             :         return hi;
     377             : }
     378             : 
     379             : /* count number of 1 bits in ci->mask between bit positions lo
     380             :  * (inclusive) and hi (not inclusive) */
     381             : static BUN
     382       26148 : count_mask_bits(const struct canditer *ci, BUN lo, BUN hi)
     383             : {
     384       26148 :         BUN n;
     385       26148 :         assert(lo <= hi);
     386       26148 :         assert(ci->tpe == cand_mask);
     387       26148 :         if (lo == hi)
     388             :                 return 0;
     389       26148 :         lo += ci->firstbit;
     390       26148 :         hi += ci->firstbit;
     391       26148 :         BUN loi = lo / 32;
     392       26148 :         BUN hii = hi / 32;
     393       26148 :         lo %= 32;
     394       26148 :         hi %= 32;
     395       26148 :         if (loi == hii)
     396        1097 :                 return (BUN) candmask_pop((ci->mask[loi] & ((1U << hi) - 1)) >> lo);
     397       25051 :         n = (BUN) candmask_pop(ci->mask[loi++] >> lo);
     398     3900137 :         while (loi < hii)
     399     3875086 :                 n += (BUN) candmask_pop(ci->mask[loi++]);
     400       25051 :         if (hi != 0)
     401        6539 :                 n += (BUN) candmask_pop(ci->mask[loi] & ((1U << hi) - 1));
     402             :         return n;
     403             : }
     404             : 
     405             : /* initialize a candidate iterator */
     406             : void
     407     7970245 : canditer_init(struct canditer *ci, BAT *b, BAT *s)
     408             : {
     409     7970245 :         assert(ci != NULL);
     410     7970245 :         BUN batcount = 0;
     411     7970245 :         oid hseq = 0;
     412     7970245 :         if (b) {
     413     7324817 :                 MT_lock_set(&b->theaplock);
     414     7322912 :                 batcount = BATcount(b);
     415     7322912 :                 hseq = b->hseqbase;
     416     7322912 :                 MT_lock_unset(&b->theaplock);
     417             :         }
     418             : 
     419     7970260 :         if (s == NULL) {
     420     3860819 :                 if (b == NULL) {
     421             :                         /* trivially empty candidate list */
     422           0 :                         *ci = (struct canditer) {
     423             :                                 .tpe = cand_dense,
     424             :                         };
     425           0 :                         return;
     426             :                 }
     427             :                 /* every row is a candidate */
     428     3860819 :                 *ci = (struct canditer) {
     429             :                         .tpe = cand_dense,
     430             :                         .seq = hseq,
     431             :                         .hseq = hseq,
     432             :                         .ncand = batcount,
     433             :                 };
     434     3860819 :                 return;
     435             :         }
     436             : 
     437     4109441 :         assert(ATOMtype(s->ttype) == TYPE_oid || s->ttype == TYPE_msk);
     438     4109441 :         assert(s->ttype == TYPE_msk|| s->tsorted);
     439     4109441 :         assert(s->ttype == TYPE_msk|| s->tkey);
     440     4109441 :         assert(s->ttype == TYPE_msk|| s->tnonil);
     441     4109441 :         assert(s->ttype == TYPE_void || s->tvheap == NULL);
     442             : 
     443     4109441 :         BUN cnt = BATcount(s);
     444             : 
     445     4109441 :         if (cnt == 0 || (b != NULL && batcount == 0)) {
     446             :                 /* candidate list for empty BAT or empty candidate list */
     447      950755 :                 *ci = (struct canditer) {
     448             :                         .tpe = cand_dense,
     449      950755 :                         .hseq = s->hseqbase,
     450             :                         .s = s,
     451             :                 };
     452      950755 :                 return;
     453             :         }
     454             : 
     455     3158686 :         *ci = (struct canditer) {
     456     3158686 :                 .seq = s->tseqbase,
     457     3158686 :                 .hseq = s->hseqbase,
     458             :                 .s = s,
     459             :         };
     460             : 
     461     3158686 :         if (mask_cand(s)) {
     462       26150 :                 ci->tpe = cand_mask;
     463       26150 :                 ci->mask = (const uint32_t *) ccand_first(s);
     464       26150 :                 ci->seq = s->tseqbase - (oid) CCAND(s)->firstbit;
     465       26150 :                 ci->hseq = s->hseqbase;
     466       26150 :                 ci->nvals = ccand_free(s) / sizeof(uint32_t);
     467       26150 :                 cnt = ci->nvals * 32;
     468     3132536 :         } else if (s->ttype == TYPE_msk) {
     469           0 :                 assert(0);
     470             :                 ci->tpe = cand_mask;
     471             :                 ci->mask = (const uint32_t *) s->theap->base;
     472             :                 ci->seq = s->hseqbase;
     473             :                 ci->nvals = (cnt + 31U) / 32U;
     474     3132536 :         } else if (s->ttype == TYPE_void) {
     475     2608033 :                 assert(!is_oid_nil(ci->seq));
     476     2608033 :                 if (s->tvheap) {
     477       91300 :                         assert(ccand_free(s) % SIZEOF_OID == 0);
     478       91300 :                         ci->nvals = ccand_free(s) / SIZEOF_OID;
     479       91300 :                         if (ci->nvals > 0) {
     480       91300 :                                 ci->tpe = cand_except;
     481       91300 :                                 ci->oids = (const oid *) ccand_first(s);
     482             :                         } else {
     483             :                                 /* why the vheap? */
     484             :                                 ci->tpe = cand_dense;
     485             :                         }
     486             :                 } else {
     487             :                         ci->tpe = cand_dense;
     488             :                 }
     489      524503 :         } else if (is_oid_nil(ci->seq)) {
     490      488911 :                 ci->tpe = cand_materialized;
     491      488911 :                 ci->oids = (const oid *) Tloc(s, 0);
     492      488911 :                 ci->seq = ci->oids[0];
     493      488911 :                 ci->nvals = cnt;
     494             :         } else {
     495             :                 /* materialized dense: no exceptions */
     496             :                 ci->tpe = cand_dense;
     497             :         }
     498     3158686 :         switch (ci->tpe) {
     499      488911 :         case cand_materialized:
     500      488911 :                 if (b != NULL) {
     501      389705 :                         BUN p = binsearchcand(ci->oids, cnt - 1U, hseq);
     502             :                         /* p == cnt means candidate list is completely
     503             :                          * before b */
     504      389705 :                         ci->offset = p;
     505      389705 :                         ci->oids += p;
     506      389705 :                         cnt -= p;
     507      389705 :                         if (cnt > 0) {
     508      389695 :                                 cnt = binsearchcand(ci->oids, cnt  - 1U,
     509             :                                                     hseq + batcount);
     510             :                                 /* cnt == 0 means candidate list is
     511             :                                  * completely after b */
     512             :                         }
     513      389695 :                         if (cnt == 0) {
     514             :                                 /* no overlap */
     515          10 :                                 *ci = (struct canditer) {
     516             :                                         .tpe = cand_dense,
     517             :                                         .s = s,
     518             :                                 };
     519          10 :                                 return;
     520             :                         }
     521      389695 :                         ci->seq = ci->oids[0];
     522      389695 :                         ci->nvals = cnt;
     523      389695 :                         if (ci->oids[cnt - 1U] - ci->seq == cnt - 1U) {
     524             :                                 /* actually dense */
     525          61 :                                 ci->tpe = cand_dense;
     526          61 :                                 ci->oids = NULL;
     527          61 :                                 ci->nvals = 0;
     528             :                         }
     529             :                 }
     530             :                 break;
     531       91297 :         case cand_except:
     532             :                 /* exceptions must all be within range of s */
     533       91297 :                 assert(ci->oids[0] >= ci->seq);
     534       91297 :                 assert(ci->oids[ci->nvals - 1U] < ci->seq + cnt + ci->nvals);
     535             :                 /* prune exceptions at either end of range of s */
     536     3601859 :                 while (ci->nvals > 0 && ci->oids[0] == ci->seq) {
     537     3510562 :                         ci->nvals--;
     538     3510562 :                         ci->oids++;
     539     3510562 :                         ci->seq++;
     540             :                 }
     541       91495 :                 while (ci->nvals > 0 &&
     542       88513 :                        ci->oids[ci->nvals - 1U] == ci->seq + cnt + ci->nvals - 1U)
     543         198 :                         ci->nvals--;
     544       91297 :                 if (b != NULL) {
     545       70574 :                         if (ci->seq + cnt + ci->nvals <= hseq ||
     546       70574 :                             ci->seq >= hseq + batcount) {
     547             :                                 /* candidate list does not overlap with b */
     548           0 :                                 *ci = (struct canditer) {
     549             :                                         .tpe = cand_dense,
     550             :                                         .s = s,
     551             :                                 };
     552           0 :                                 return;
     553             :                         }
     554             :                 }
     555       91297 :                 if (ci->nvals > 0) {
     556       88312 :                         if (b == NULL)
     557             :                                 break;
     558       67896 :                         BUN p;
     559       67896 :                         p = binsearchcand(ci->oids, ci->nvals - 1U, hseq);
     560       67896 :                         if (p == ci->nvals) {
     561             :                                 /* all exceptions before start of b */
     562           0 :                                 ci->offset = hseq - ci->seq - ci->nvals;
     563           0 :                                 cnt = ci->seq + cnt + ci->nvals - hseq;
     564           0 :                                 ci->seq = hseq;
     565           0 :                                 ci->nvals = 0;
     566           0 :                                 ci->tpe = cand_dense;
     567           0 :                                 ci->oids = NULL;
     568           0 :                                 break;
     569             :                         }
     570       67896 :                         assert(hseq > ci->seq || p == 0);
     571       67896 :                         if (hseq > ci->seq) {
     572             :                                 /* skip candidates, possibly including
     573             :                                  * exceptions */
     574           0 :                                 ci->oids += p;
     575           0 :                                 ci->nvals -= p;
     576           0 :                                 p = hseq - ci->seq - p;
     577           0 :                                 cnt -= p;
     578           0 :                                 ci->offset += p;
     579           0 :                                 ci->seq = hseq;
     580             :                         }
     581       67896 :                         if (ci->seq + cnt + ci->nvals > hseq + batcount) {
     582           0 :                                 p = binsearchcand(ci->oids, ci->nvals - 1U,
     583             :                                                   hseq + batcount);
     584           0 :                                 ci->nvals = p;
     585           0 :                                 cnt = hseq + batcount - ci->seq - ci->nvals;
     586             :                         }
     587       67896 :                         while (ci->nvals > 0 && ci->oids[0] == ci->seq) {
     588           0 :                                 ci->nvals--;
     589           0 :                                 ci->oids++;
     590           0 :                                 ci->seq++;
     591             :                         }
     592       67896 :                         while (ci->nvals > 0 &&
     593       67896 :                                ci->oids[ci->nvals - 1U] == ci->seq + cnt + ci->nvals - 1U)
     594           0 :                                 ci->nvals--;
     595       67896 :                         if (ci->nvals > 0)
     596             :                                 break;
     597             :                 }
     598        2985 :                 ci->tpe = cand_dense;
     599        2985 :                 ci->oids = NULL;
     600             :                 /* fall through */
     601     2555311 :         case cand_dense:
     602     2555311 :                 if (b != NULL) {
     603     2230577 :                         if (ci->seq + cnt <= hseq ||
     604     2230577 :                             ci->seq >= hseq + batcount) {
     605             :                                 /* no overlap */
     606           0 :                                 *ci = (struct canditer) {
     607             :                                         .tpe = cand_dense,
     608             :                                         .s = s,
     609             :                                 };
     610           0 :                                 return;
     611             :                         }
     612     2230577 :                         if (hseq > ci->seq) {
     613          41 :                                 cnt -= hseq - ci->seq;
     614          41 :                                 ci->offset += hseq - ci->seq;
     615          41 :                                 ci->seq = hseq;
     616             :                         }
     617     2230577 :                         if (ci->seq + cnt > hseq + batcount)
     618          40 :                                 cnt = hseq + batcount - ci->seq;
     619             :                 }
     620             :                 break;
     621       26148 :         case cand_mask:
     622       26148 :                 assert(s->tseqbase != oid_nil);
     623       26148 :                 if (b != NULL) {
     624        8124 :                         if (ci->seq + cnt <= hseq ||
     625        8124 :                             ci->seq >= hseq + batcount) {
     626             :                                 /* no overlap */
     627           0 :                                 *ci = (struct canditer) {
     628             :                                         .tpe = cand_dense,
     629             :                                         .s = s,
     630             :                                 };
     631           0 :                                 return;
     632             :                         }
     633        8124 :                         if (hseq > ci->seq) {
     634           0 :                                 cnt = hseq - ci->seq;
     635           0 :                                 ci->mask += cnt / 32U;
     636           0 :                                 ci->firstbit = (uint8_t) (cnt % 32U);
     637           0 :                                 cnt = BATcount(s) - cnt;
     638           0 :                                 ci->seq = hseq;
     639             :                         }
     640        8124 :                         if (ci->seq + cnt > hseq + batcount) {
     641        7835 :                                 cnt = hseq + batcount - ci->seq;
     642             :                         }
     643        8124 :                         ci->nvals = (ci->firstbit + cnt + 31U) / 32U;
     644             :                 }
     645             :                 /* if first value is only partially used, check
     646             :                  * whether there are any 1 bits in the used part */
     647       26148 :                 if (ci->firstbit > 0 && (ci->mask[0] >> ci->firstbit) == 0) {
     648           0 :                         if (cnt <= 32U - ci->firstbit) {
     649             :                                 cnt = 0;
     650             :                                 /* returns below */
     651             :                         } else {
     652           0 :                                 cnt -= 32U - ci->firstbit;
     653           0 :                                 ci->firstbit = 0;
     654           0 :                                 ci->mask++;
     655           0 :                                 ci->nvals--;
     656             :                         }
     657             :                 }
     658             :                 /* skip over any zero mask words that are used completely */
     659       26148 :                 if (ci->firstbit == 0) {
     660       67048 :                         while (cnt >= 32U && ci->mask[0] == 0) {
     661       40900 :                                 cnt -= 32U;
     662       40900 :                                 ci->mask++;
     663       40900 :                                 ci->nvals--;
     664             :                         }
     665             :                 }
     666             :                 /* check whether there are any 1 bits in the last word */
     667       26148 :                 if (cnt == 0 ||
     668       26148 :                     (cnt < 32U - ci->firstbit &&
     669        1097 :                      ((ci->mask[0] >> ci->firstbit) & ((1U << cnt) - 1U)) == 0)) {
     670             :                         /* no one bits in the whole relevant portion
     671             :                          * of the bat */
     672           0 :                         *ci = (struct canditer) {
     673             :                                 .tpe = cand_dense,
     674             :                                 .s = s,
     675             :                         };
     676           0 :                         return;
     677             :                 }
     678             :                 /* here we know there are 1 bits in the first mask
     679             :                  * word */
     680       26148 :                 int i = candmask_lobit(ci->mask[0] >> ci->firstbit);
     681       26148 :                 assert(i >= 0);      /* there should be a set bit */
     682       26148 :                 ci->firstbit += i;
     683       26148 :                 cnt -= i;
     684       26148 :                 if (mask_cand(s))
     685       26145 :                         ci->mskoff = s->tseqbase - (oid) CCAND(s)->firstbit + (ci->mask - (const uint32_t *) ccand_first(s)) * 32U;
     686             :                 else
     687           3 :                         ci->mskoff = s->tseqbase + (ci->mask - (const uint32_t *) s->theap->base) * 32U;
     688       26148 :                 ci->seq = ci->mskoff + ci->firstbit;
     689       26148 :                 ci->nextbit = ci->firstbit;
     690             :                 /* at this point we know that bit ci->firstbit is set
     691             :                  * in ci->mask[0] */
     692       26148 :                 ci->lastbit = (ci->firstbit + cnt - 1U) % 32U + 1U;
     693       26148 :                 if (ci->lastbit < 32 &&
     694        7833 :                     (ci->mask[ci->nvals - 1] & ((1U << ci->lastbit) - 1)) == 0) {
     695             :                         /* last partial word is all zero */
     696         198 :                         cnt -= ci->lastbit;
     697         198 :                         ci->lastbit = 32;
     698         198 :                         ci->nvals--;
     699             :                 }
     700       26148 :                 if (ci->lastbit == 32) {
     701             :                         /* "remove" zero words at the end */
     702       26755 :                         while (cnt >= 32 && ci->mask[ci->nvals - 1] == 0) {
     703        8242 :                                 ci->nvals--;
     704        8242 :                                 cnt -= 32;
     705             :                         }
     706             :                 }
     707       26148 :                 ci->ncand = count_mask_bits(ci, 0, cnt);
     708       26146 :                 return;
     709             :         }
     710     3132528 :         ci->ncand = cnt;
     711     3132528 :         ci->hseq += ci->offset;
     712             : }
     713             : 
     714             : /* return the next candidate without advancing */
     715             : oid
     716     2531390 : canditer_peek(struct canditer *ci)
     717             : {
     718     2531390 :         oid o = oid_nil;
     719     2531390 :         if (ci->next == ci->ncand)
     720             :                 return oid_nil;
     721     2525044 :         switch (ci->tpe) {
     722     2268134 :         case cand_dense:
     723     2268134 :                 o = ci->seq + ci->next;
     724     2268134 :                 break;
     725      252934 :         case cand_materialized:
     726      252934 :                 assert(ci->next < ci->nvals);
     727      252934 :                 o = ci->oids[ci->next];
     728      252934 :                 break;
     729        3976 :         case cand_except:
     730        3976 :                 o = ci->seq + ci->add + ci->next;
     731        3982 :                 while (ci->add < ci->nvals && o == ci->oids[ci->add]) {
     732           6 :                         ci->add++;
     733           6 :                         o++;
     734             :                 }
     735             :                 break;
     736             :         case cand_mask:
     737           0 :                 while ((ci->mask[ci->nextmsk] >> ci->nextbit) == 0) {
     738           0 :                         ci->nextmsk++;
     739           0 :                         ci->nextbit = 0;
     740             :                 }
     741           0 :                 ci->nextbit += candmask_lobit(ci->mask[ci->nextmsk] >> ci->nextbit);
     742           0 :                 o = ci->mskoff + ci->nextmsk * 32 + ci->nextbit;
     743           0 :                 break;
     744             :         }
     745             :         return o;
     746             : }
     747             : 
     748             : /* return the previous candidate */
     749             : oid
     750         893 : canditer_prev(struct canditer *ci)
     751             : {
     752         893 :         if (ci->next == 0)
     753           0 :                 return oid_nil;
     754         893 :         switch (ci->tpe) {
     755         893 :         case cand_dense:
     756         893 :                 return ci->seq + --ci->next;
     757           0 :         case cand_materialized:
     758           0 :                 return ci->oids[--ci->next];
     759             :         case cand_except:
     760             :                 break;
     761           0 :         case cand_mask:
     762           0 :                 for (;;) {
     763           0 :                         if (ci->nextbit == 0) {
     764           0 :                                 ci->nextbit = 32;
     765           0 :                                 while (ci->mask[--ci->nextmsk] == 0)
     766             :                                         ;
     767             :                         }
     768           0 :                         if (ci->mask[ci->nextmsk] & (1U << --ci->nextbit))
     769             :                                 break;
     770             :                 }
     771           0 :                 ci->next--;
     772           0 :                 return ci->mskoff + ci->nextmsk * 32 + ci->nextbit;
     773             :         }
     774           0 :         oid o = ci->seq + ci->add + --ci->next;
     775           0 :         while (ci->add > 0 && o == ci->oids[ci->add - 1]) {
     776           0 :                 ci->add--;
     777           0 :                 o--;
     778             :         }
     779             :         return o;
     780             : }
     781             : 
     782             : /* return the previous candidate without retreating */
     783             : oid
     784        3105 : canditer_peekprev(struct canditer *ci)
     785             : {
     786        3105 :         oid o = oid_nil;
     787             : 
     788        3105 :         if (ci->next == 0)
     789             :                 return oid_nil;
     790        3105 :         switch (ci->tpe) {
     791        3105 :         case cand_dense:
     792        3105 :                 return ci->seq + ci->next - 1;
     793           0 :         case cand_materialized:
     794           0 :                 return ci->oids[ci->next - 1];
     795           0 :         case cand_except:
     796           0 :                 o = ci->seq + ci->add + ci->next - 1;
     797           0 :                 while (ci->add > 0 && o == ci->oids[ci->add - 1]) {
     798           0 :                         ci->add--;
     799           0 :                         o--;
     800             :                 }
     801             :                 break;
     802           0 :         case cand_mask:
     803           0 :                 do {
     804           0 :                         if (ci->nextbit == 0) {
     805           0 :                                 ci->nextbit = 32;
     806           0 :                                 while (ci->mask[--ci->nextmsk] == 0)
     807             :                                         ;
     808             :                         }
     809           0 :                 } while ((ci->mask[ci->nextmsk] & (1U << --ci->nextbit)) == 0);
     810           0 :                 o = ci->mskoff + ci->nextmsk * 32 + ci->nextbit;
     811           0 :                 if (++ci->nextbit == 32) {
     812           0 :                         ci->nextbit = 0;
     813           0 :                         ci->nextmsk++;
     814             :                 }
     815             :                 break;
     816             :         }
     817             :         return o;
     818             : }
     819             : 
     820             : /* if o is a candidate, return it, else return the next candidate from
     821             :  * the cand_mask iterator ci after (if next is set) or before (if next
     822             :  * is unset) o; if there are no more candidates, return oid_nil */
     823             : oid
     824           0 : canditer_mask_next(const struct canditer *ci, oid o, bool next)
     825             : {
     826           0 :         if (o < ci->mskoff)
     827           0 :                 return next ? ci->mskoff + ci->firstbit : oid_nil;
     828           0 :         o -= ci->mskoff;
     829           0 :         BUN p = o / 32;
     830           0 :         o %= 32;
     831           0 :         if (p >= ci->nvals || (p == ci->nvals - 1 && o >= ci->lastbit))
     832           0 :                 return next ? oid_nil : canditer_last(ci);
     833           0 :         if (next) {
     834           0 :                 while ((ci->mask[p] & (1U << o)) == 0) {
     835           0 :                         if (++o == 32) {
     836           0 :                                 o = 0;
     837           0 :                                 if (++p == ci->nvals)
     838           0 :                                         return oid_nil;
     839             :                         }
     840             :                 }
     841           0 :                 if (p == ci->nvals - 1 && o >= ci->lastbit)
     842           0 :                         return oid_nil;
     843             :         } else {
     844           0 :                 while ((ci->mask[p] & (1U << o)) == 0) {
     845           0 :                         if (o == 0) {
     846           0 :                                 o = 31;
     847           0 :                                 if (p == 0)
     848           0 :                                         return oid_nil;
     849           0 :                                 p--;
     850             :                         } else {
     851           0 :                                 o--;
     852             :                         }
     853             :                 }
     854           0 :                 if (p == 0 && o < ci->firstbit)
     855           0 :                         return oid_nil;
     856             :         }
     857           0 :         return ci->mskoff + 32 * p + o;
     858             : }
     859             : 
     860             : /* return the last candidate */
     861             : oid
     862      246273 : canditer_last(const struct canditer *ci)
     863             : {
     864      246273 :         if (ci->ncand == 0)
     865           0 :                 return oid_nil;
     866      246273 :         switch (ci->tpe) {
     867      219061 :         case cand_dense:
     868      219061 :                 return ci->seq + ci->ncand - 1;
     869       22179 :         case cand_materialized:
     870       22179 :                 return ci->oids[ci->ncand - 1];
     871        4407 :         case cand_except:
     872        4407 :                 return ci->seq + ci->ncand + ci->nvals - 1;
     873         626 :         case cand_mask:
     874       10728 :                 for (uint8_t i = ci->lastbit; i > 0; ) {
     875       10728 :                         if (ci->mask[ci->nvals - 1] & (1U << --i))
     876         626 :                                 return ci->mskoff + (ci->nvals - 1) * 32 + i;
     877             :                 }
     878             :                 break;          /* cannot happen */
     879             :         }
     880           0 :         return oid_nil;         /* cannot happen */
     881             : }
     882             : 
     883             : /* return the candidate at the given index */
     884             : oid
     885   341412850 : canditer_idx(const struct canditer *ci, BUN p)
     886             : {
     887   341412850 :         if (p >= ci->ncand)
     888           0 :                 return oid_nil;
     889   341412850 :         switch (ci->tpe) {
     890   172626794 :         case cand_dense:
     891   172626794 :                 return ci->seq + p;
     892   168572014 :         case cand_materialized:
     893   168572014 :                 return ci->oids[p];
     894      213964 :         case cand_except: {
     895      213964 :                 oid o = ci->seq + p;
     896      213964 :                 if (o < ci->oids[0])
     897             :                         return o;
     898      147613 :                 if (o + ci->nvals > ci->oids[ci->nvals - 1])
     899             :                         return o + ci->nvals;
     900       79277 :                 BUN lo = 0, hi = ci->nvals - 1;
     901      545192 :                 while (hi - lo > 1) {
     902      386638 :                         BUN mid = (hi + lo) / 2;
     903      386638 :                         if (ci->oids[mid] - mid > o)
     904             :                                 hi = mid;
     905             :                         else
     906      247607 :                                 lo = mid;
     907             :                 }
     908       79277 :                 return o + hi;
     909             :         }
     910          78 :         case cand_mask: {
     911          78 :                 BUN x;
     912          78 :                 if ((x = candmask_pop(ci->mask[0] >> ci->firstbit)) > p) {
     913          26 :                         for (uint8_t i = ci->firstbit; ; i++) {
     914         104 :                                 if (ci->mask[0] & (1U << i)) {
     915         104 :                                         if (p == 0)
     916          78 :                                                 return ci->mskoff + i;
     917          26 :                                         p--;
     918             :                                 }
     919             :                         }
     920             :                 }
     921           0 :                 for (BUN n = 1; n < ci->nvals; n++) {
     922           0 :                         uint32_t mask = ci->mask[n];
     923           0 :                         p -= x;
     924           0 :                         x = candmask_pop(mask);
     925           0 :                         if (x > p) {
     926           0 :                                 for (uint8_t i = 0; ; i++) {
     927           0 :                                         if (mask & (1U << i)) {
     928           0 :                                                 if (p == 0)
     929           0 :                                                         return ci->mskoff + n * 32 + i;
     930           0 :                                                 p--;
     931             :                                         }
     932             :                                 }
     933             :                         }
     934             :                 }
     935             :                 break;          /* cannot happen */
     936             :         }
     937             :         }
     938           0 :         return oid_nil;         /* cannot happen */
     939             : }
     940             : 
     941             : /* set the index for the next candidate to be returned */
     942             : void
     943      626951 : canditer_setidx(struct canditer *ci, BUN p)
     944             : {
     945      626951 :         if (p != ci->next) {
     946      589955 :                 if (p >= ci->ncand) {
     947       52572 :                         ci->next = ci->ncand;
     948       52572 :                         switch (ci->tpe) {
     949           0 :                         case cand_except:
     950           0 :                                 ci->add = ci->nvals;
     951           0 :                                 break;
     952           0 :                         case cand_mask:
     953           0 :                                 ci->nextbit = ci->lastbit;
     954           0 :                                 ci->nextmsk = ci->nvals;
     955           0 :                                 if (ci->nextbit == 32)
     956           0 :                                         ci->nextbit = 0;
     957             :                                 else
     958           0 :                                         ci->nextmsk--;
     959             :                                 break;
     960             :                         default:
     961             :                                 break;
     962             :                         }
     963             :                 } else {
     964      537383 :                         ci->next = p;
     965      537383 :                         switch (ci->tpe) {
     966        2316 :                         case cand_except:
     967        2316 :                                 ci->add = canditer_idx(ci, p) - ci->seq - p;
     968        2316 :                                 break;
     969           0 :                         case cand_mask: {
     970           0 :                                 oid o = canditer_idx(ci, p) - ci->mskoff;
     971           0 :                                 ci->nextmsk = o / 32;
     972           0 :                                 ci->nextbit = (uint8_t) (o % 32);
     973           0 :                                 break;
     974             :                         }
     975             :                         default:
     976             :                                 break;
     977             :                         }
     978             :                 }
     979             :         }
     980      626951 : }
     981             : 
     982             : /* reset */
     983             : void
     984      682090 : canditer_reset(struct canditer *ci)
     985             : {
     986      682090 :         if (ci->tpe == cand_mask) {
     987           0 :                 ci->nextbit = ci->firstbit;
     988           0 :                 ci->nextmsk = 0;
     989             :         } else {
     990      682090 :                 ci->add = 0;
     991             :         }
     992      682090 :         ci->next = 0;
     993      682090 : }
     994             : 
     995             : /* return index of given candidate if it occurs; if the candidate does
     996             :  * not occur, if next is set, return index of next larger candidate,
     997             :  * if next is not set, return BUN_NONE */
     998             : BUN
     999     1048887 : canditer_search(const struct canditer *ci, oid o, bool next)
    1000             : {
    1001     1048887 :         BUN p;
    1002             : 
    1003     1048887 :         switch (ci->tpe) {
    1004      967940 :         case cand_dense:
    1005      967940 :                 if (o < ci->seq)
    1006         606 :                         return next ? 0 : BUN_NONE;
    1007      967334 :                 if (o >= ci->seq + ci->ncand)
    1008      126763 :                         return next ? ci->ncand : BUN_NONE;
    1009      840571 :                 return o - ci->seq;
    1010       54331 :         case cand_materialized:
    1011       54331 :                 if (ci->nvals == 0)
    1012             :                         return 0;
    1013       54331 :                 p = binsearchcand(ci->oids, ci->nvals - 1, o);
    1014       54326 :                 if (next || (p != ci->nvals && ci->oids[p] == o))
    1015       54081 :                         return p;
    1016             :                 break;
    1017       26616 :         case cand_except:
    1018       26616 :                 if (o < ci->seq)
    1019           0 :                         return next ? 0 : BUN_NONE;
    1020       26616 :                 if (o >= ci->seq + ci->ncand + ci->nvals)
    1021         928 :                         return next ? ci->ncand : BUN_NONE;
    1022       25688 :                 p = binsearchcand(ci->oids, ci->nvals - 1, o);
    1023       25688 :                 if (next || p == ci->nvals || ci->oids[p] != o)
    1024       25533 :                         return o - ci->seq - p;
    1025             :                 break;
    1026           0 :         case cand_mask:
    1027           0 :                 if (o < ci->mskoff) {
    1028           0 :                         return next ? 0 : BUN_NONE;
    1029             :                 }
    1030           0 :                 o -= ci->mskoff;
    1031           0 :                 p = o / 32;
    1032           0 :                 if (p >= ci->nvals)
    1033           0 :                         return next ? ci->ncand : BUN_NONE;
    1034           0 :                 o %= 32;
    1035           0 :                 if (p == ci->nvals - 1 && o >= ci->lastbit)
    1036           0 :                         return next ? ci->ncand : BUN_NONE;
    1037           0 :                 if (next || ci->mask[p] & (1U << o))
    1038           0 :                         return count_mask_bits(ci, 0, p * 32 + o) + !(ci->mask[p] & (1U << o));
    1039             :                 break;
    1040             :         }
    1041             :         return BUN_NONE;
    1042             : }
    1043             : 
    1044             : static BAT *
    1045         781 : canditer_sliceval_mask(const struct canditer *ci, oid lo1, oid hi1, BUN cnt1,
    1046             :                        oid lo2, oid hi2, BUN cnt2)
    1047             : {
    1048         781 :         assert(cnt2 == 0 || !is_oid_nil(lo2));
    1049         781 :         assert(cnt2 == 0 || lo2 >= hi1);
    1050         781 :         if (is_oid_nil(lo1) || lo1 < ci->mskoff + ci->firstbit)
    1051         195 :                 lo1 = ci->mskoff + ci->firstbit;
    1052         781 :         if (is_oid_nil(hi1) || hi1 >= ci->mskoff + (ci->nvals - 1) * 32 + ci->lastbit)
    1053         606 :                 hi1 = ci->mskoff + (ci->nvals - 1) * 32 + ci->lastbit;
    1054             : 
    1055         781 :         BAT *bn = COLnew(0, TYPE_oid, cnt1 + cnt2, TRANSIENT);
    1056         781 :         if (bn == NULL)
    1057             :                 return NULL;
    1058         781 :         bn->tkey = true;
    1059             : 
    1060         781 :         if (hi1 > ci->mskoff) {
    1061         629 :                 if (lo1 < ci->mskoff)
    1062             :                         lo1 = 0;
    1063             :                 else
    1064         629 :                         lo1 -= ci->mskoff;
    1065         629 :                 hi1 -= ci->mskoff;
    1066      149941 :                 for (oid o = lo1; o < hi1 && cnt1 > 0; o++) {
    1067      149312 :                         if (ci->mask[o / 32] & (1U << (o % 32))) {
    1068      119874 :                                 if (BUNappend(bn, &(oid){o + ci->mskoff}, false) != GDK_SUCCEED) {
    1069           0 :                                         BBPreclaim(bn);
    1070           0 :                                         return NULL;
    1071             :                                 }
    1072      119874 :                                 cnt1--;
    1073             :                         }
    1074             :                 }
    1075             :         }
    1076         781 :         if (cnt2 > 0) {
    1077          26 :                 if (lo2 < ci->mskoff + ci->firstbit)
    1078             :                         lo2 = ci->mskoff + ci->firstbit;
    1079          26 :                 if (is_oid_nil(hi2) || hi2 >= ci->mskoff + (ci->nvals - 1) * 32 + ci->lastbit)
    1080          26 :                         hi2 = ci->mskoff + (ci->nvals - 1) * 32 + ci->lastbit;
    1081             : 
    1082          26 :                 lo2 -= ci->mskoff;
    1083          26 :                 hi2 -= ci->mskoff;
    1084          88 :                 for (oid o = lo2; o < hi2 && cnt2 > 0; o++) {
    1085          62 :                         if (ci->mask[o / 32] & (1U << (o % 32))) {
    1086          58 :                                 if (BUNappend(bn, &(oid){o + ci->mskoff}, false) != GDK_SUCCEED) {
    1087           0 :                                         BBPreclaim(bn);
    1088           0 :                                         return NULL;
    1089             :                                 }
    1090          58 :                                 cnt2--;
    1091             :                         }
    1092             :                 }
    1093             :         }
    1094             :         return bn;
    1095             : }
    1096             : 
    1097             : /* return either an actual BATslice or a new BAT that contains the
    1098             :  * "virtual" slice of the input candidate list BAT; except, unlike
    1099             :  * BATslice, the hseqbase of the returned BAT is 0; note for cand_mask
    1100             :  * candidate iterators, the BUN values refer to number of 1 bits */
    1101             : BAT *
    1102      448885 : canditer_slice(const struct canditer *ci, BUN lo, BUN hi)
    1103             : {
    1104      448885 :         BAT *bn;
    1105      448885 :         oid o;
    1106      448885 :         BUN add;
    1107             : 
    1108      448885 :         assert(ci != NULL);
    1109             : 
    1110      448885 :         if (lo >= ci->ncand || lo >= hi)
    1111       61190 :                 return BATdense(0, 0, 0);
    1112      387695 :         if (hi > ci->ncand)
    1113             :                 hi = ci->ncand;
    1114      387695 :         if (hi - lo == 1)
    1115      331949 :                 return BATdense(0, canditer_idx(ci, lo), 1);
    1116       55746 :         switch (ci->tpe) {
    1117        5380 :         case cand_materialized:
    1118        5380 :                 if (ci->s) {
    1119        5380 :                         bn = BATslice(ci->s, lo + ci->offset, hi + ci->offset);
    1120        5380 :                         BAThseqbase(bn, 0);
    1121        5380 :                         return bn;
    1122             :                 }
    1123           0 :                 bn = COLnew(0, TYPE_oid, hi - lo, TRANSIENT);
    1124           0 :                 if (bn == NULL)
    1125             :                         return NULL;
    1126           0 :                 BATsetcount(bn, hi - lo);
    1127           0 :                 memcpy(Tloc(bn, 0), ci->oids + lo, (hi - lo) * sizeof(oid));
    1128           0 :                 break;
    1129       50076 :         default: /* really: case cand_dense: */
    1130       50076 :                 return BATdense(0, ci->seq + lo, hi - lo);
    1131         287 :         case cand_except:
    1132         287 :                 o = canditer_idx(ci, lo);
    1133         287 :                 add = o - ci->seq - lo;
    1134         287 :                 assert(add <= ci->nvals);
    1135         287 :                 if (add == ci->nvals || o + hi - lo < ci->oids[add]) {
    1136             :                         /* after last exception or before next
    1137             :                          * exception: return dense sequence */
    1138         243 :                         return BATdense(0, o, hi - lo);
    1139             :                 }
    1140          44 :                 bn = COLnew(0, TYPE_oid, hi - lo, TRANSIENT);
    1141          44 :                 if (bn == NULL)
    1142             :                         return NULL;
    1143          44 :                 BATsetcount(bn, hi - lo);
    1144         392 :                 for (oid *dst = Tloc(bn, 0); lo < hi; lo++) {
    1145         501 :                         while (add < ci->nvals && o == ci->oids[add]) {
    1146         153 :                                 o++;
    1147         153 :                                 add++;
    1148             :                         }
    1149         348 :                         *dst++ = o++;
    1150             :                 }
    1151             :                 break;
    1152           3 :         case cand_mask:
    1153           3 :                 return canditer_sliceval_mask(ci, canditer_idx(ci, lo),
    1154             :                                               oid_nil, hi - lo,
    1155             :                                               oid_nil, oid_nil, 0);
    1156             :         }
    1157          44 :         bn->tsorted = true;
    1158          44 :         bn->trevsorted = BATcount(bn) <= 1;
    1159          44 :         bn->tkey = true;
    1160          44 :         bn->tseqbase = oid_nil;
    1161          44 :         bn->tnil = false;
    1162          44 :         bn->tnonil = true;
    1163          44 :         bn->tminpos = 0;
    1164          44 :         bn->tmaxpos = BATcount(bn) - 1;
    1165          44 :         return virtualize(bn);
    1166             : }
    1167             : 
    1168             : /* like the above, except the bounds are given by values instead of
    1169             :  * indexes */
    1170             : BAT *
    1171      328425 : canditer_sliceval(const struct canditer *ci, oid lo, oid hi)
    1172             : {
    1173      328425 :         if (ci->tpe != cand_mask) {
    1174      982998 :                 return canditer_slice(
    1175             :                         ci,
    1176      327671 :                         is_oid_nil(lo) ? 0 : canditer_search(ci, lo, true),
    1177      327673 :                         is_oid_nil(hi) ? ci->ncand : canditer_search(ci, hi, true));
    1178             :         }
    1179             : 
    1180         752 :         return canditer_sliceval_mask(ci, lo, hi, ci->ncand,
    1181             :                                       oid_nil, oid_nil, 0);
    1182             : }
    1183             : 
    1184             : /* return the combination of two slices */
    1185             : BAT *
    1186       31930 : canditer_slice2(const struct canditer *ci, BUN lo1, BUN hi1, BUN lo2, BUN hi2)
    1187             : {
    1188       31930 :         BAT *bn;
    1189       31930 :         oid o;
    1190       31930 :         BUN add;
    1191             : 
    1192       31930 :         assert(lo1 <= hi1);
    1193       31930 :         assert(lo2 <= hi2);
    1194       31930 :         assert(hi1 <= lo2 || (lo2 == 0 && hi2 == 0));
    1195             : 
    1196       31930 :         if (hi1 == lo2)         /* consecutive slices: combine into one */
    1197         573 :                 return canditer_slice(ci, lo1, hi2);
    1198       31357 :         if (lo2 == hi2 || hi1 >= ci->ncand || lo2 >= ci->ncand) {
    1199             :                 /* empty second slice */
    1200       23333 :                 return canditer_slice(ci, lo1, hi1);
    1201             :         }
    1202        8024 :         if (lo1 == hi1)         /* empty first slice */
    1203        2952 :                 return canditer_slice(ci, lo2, hi2);
    1204        5072 :         if (lo1 >= ci->ncand)     /* out of range */
    1205             :                 return BATdense(0, 0, 0);
    1206             : 
    1207        5072 :         if (hi2 >= ci->ncand)
    1208             :                 hi2 = ci->ncand;
    1209             : 
    1210        5072 :         bn = COLnew(0, TYPE_oid, hi1 - lo1 + hi2 - lo2, TRANSIENT);
    1211        5072 :         if (bn == NULL)
    1212             :                 return NULL;
    1213        5072 :         BATsetcount(bn, hi1 - lo1 + hi2 - lo2);
    1214        5072 :         bn->tsorted = true;
    1215        5072 :         bn->trevsorted = BATcount(bn) <= 1;
    1216        5072 :         bn->tkey = true;
    1217        5072 :         bn->tseqbase = oid_nil;
    1218        5072 :         bn->tnil = false;
    1219        5072 :         bn->tnonil = true;
    1220             : 
    1221        5072 :         oid *dst = Tloc(bn, 0);
    1222             : 
    1223        5072 :         switch (ci->tpe) {
    1224        2424 :         case cand_materialized:
    1225        2424 :                 memcpy(dst, ci->oids + lo1, (hi1 - lo1) * sizeof(oid));
    1226        2424 :                 memcpy(dst + hi1 - lo1, ci->oids + lo2, (hi2 - lo2) * sizeof(oid));
    1227        2424 :                 break;
    1228             :         case cand_dense:
    1229      956756 :                 while (lo1 < hi1)
    1230      954109 :                         *dst++ = ci->seq + lo1++;
    1231      289249 :                 while (lo2 < hi2)
    1232      286602 :                         *dst++ = ci->seq + lo2++;
    1233             :                 break;
    1234           1 :         case cand_except:
    1235           1 :                 o = canditer_idx(ci, lo1);
    1236           1 :                 add = o - ci->seq - lo1;
    1237           1 :                 assert(add <= ci->nvals);
    1238           1 :                 if (add == ci->nvals) {
    1239             :                         /* after last exception: return dense sequence */
    1240           0 :                         while (lo1 < hi1)
    1241           0 :                                 *dst++ = ci->seq + add + lo1++;
    1242             :                 } else {
    1243           2 :                         while (lo1 < hi1) {
    1244           1 :                                 while (add < ci->nvals && o == ci->oids[add]) {
    1245           0 :                                         o++;
    1246           0 :                                         add++;
    1247             :                                 }
    1248           1 :                                 *dst++ = o++;
    1249           1 :                                 lo1++;
    1250             :                         }
    1251             :                 }
    1252           1 :                 o = canditer_idx(ci, lo2);
    1253           1 :                 add = o - ci->seq - lo2;
    1254           1 :                 assert(add <= ci->nvals);
    1255           1 :                 if (add == ci->nvals) {
    1256             :                         /* after last exception: return dense sequence */
    1257           0 :                         while (lo2 < hi2)
    1258           0 :                                 *dst++ = ci->seq + add + lo2++;
    1259             :                 } else {
    1260           4 :                         while (lo2 < hi2) {
    1261           6 :                                 while (add < ci->nvals && o == ci->oids[add]) {
    1262           3 :                                         o++;
    1263           3 :                                         add++;
    1264             :                                 }
    1265           3 :                                 *dst++ = o++;
    1266           3 :                                 lo2++;
    1267             :                         }
    1268             :                 }
    1269             :                 break;
    1270           0 :         case cand_mask:
    1271           0 :                 return canditer_sliceval_mask(ci, canditer_idx(ci, lo1),
    1272             :                                               oid_nil, hi1 - lo1,
    1273             :                                               canditer_idx(ci, lo2),
    1274             :                                               oid_nil, hi2 - lo2);
    1275             :         }
    1276        5072 :         return virtualize(bn);
    1277             : }
    1278             : 
    1279             : BAT *
    1280       31382 : canditer_slice2val(const struct canditer *ci, oid lo1, oid hi1, oid lo2, oid hi2)
    1281             : {
    1282       31382 :         if (ci->tpe != cand_mask) {
    1283      122944 :                 return canditer_slice2(
    1284             :                         ci,
    1285       26704 :                         is_oid_nil(lo1) ? 0 : canditer_search(ci, lo1, true),
    1286       31356 :                         is_oid_nil(hi1) ? ci->ncand : canditer_search(ci, hi1, true),
    1287       31356 :                         is_oid_nil(lo2) ? 0 : canditer_search(ci, lo2, true),
    1288        2172 :                         is_oid_nil(hi2) ? ci->ncand : canditer_search(ci, hi2, true));
    1289             :         }
    1290             : 
    1291          26 :         return canditer_sliceval_mask(ci, lo1, hi1, ci->ncand,
    1292          26 :                                       lo2, hi2, ci->ncand);
    1293             : }
    1294             : 
    1295             : BAT *
    1296        3160 : BATnegcands(oid tseq, BUN nr, BAT *odels)
    1297             : {
    1298        3160 :         const char *nme;
    1299        3160 :         Heap *dels;
    1300        3160 :         BUN lo, hi;
    1301        3160 :         ccand_t *c;
    1302        3160 :         BAT *bn;
    1303             : 
    1304        3160 :         bn = BATdense(0, tseq, nr);
    1305        3160 :         if (bn == NULL)
    1306             :                 return NULL;
    1307        3160 :         if (BATcount(odels) == 0)
    1308        2767 :                 goto doreturn;
    1309             : 
    1310         393 :         lo = SORTfndfirst(odels, &bn->tseqbase);
    1311         393 :         hi = SORTfndfirst(odels, &(oid) {bn->tseqbase + BATcount(bn)});
    1312         393 :         if (lo == hi)
    1313             :                 return bn;
    1314         393 :         if (lo + nr == hi) {
    1315         134 :                 BATsetcount(bn, 0);
    1316         134 :                 goto doreturn;
    1317             :         }
    1318             : 
    1319         259 :         nme = BBP_physical(bn->batCacheid);
    1320         259 :         if ((dels = GDKmalloc(sizeof(Heap))) == NULL){
    1321           0 :                 BBPreclaim(bn);
    1322           0 :                 return NULL;
    1323             :         }
    1324         518 :         *dels = (Heap) {
    1325         259 :                 .farmid = BBPselectfarm(bn->batRole, bn->ttype, varheap),
    1326         259 :                 .parentid = bn->batCacheid,
    1327             :                 .dirty = true,
    1328             :                 .refs = ATOMIC_VAR_INIT(1),
    1329             :         };
    1330         259 :         strconcat_len(dels->filename, sizeof(dels->filename),
    1331             :                       nme, ".theap", NULL);
    1332             : 
    1333         518 :         if (dels->farmid < 0 ||
    1334         259 :             HEAPalloc(dels, hi - lo + (sizeof(ccand_t)/sizeof(oid)), sizeof(oid)) != GDK_SUCCEED) {
    1335           0 :                 GDKfree(dels);
    1336           0 :                 BBPreclaim(bn);
    1337           0 :                 return NULL;
    1338             :         }
    1339         259 :         c = (ccand_t *) dels->base;
    1340         259 :         *c = (ccand_t) {
    1341             :                 .type = CAND_NEGOID,
    1342             :         };
    1343         259 :         dels->free = sizeof(ccand_t) + sizeof(oid) * (hi - lo);
    1344         259 :         BATiter bi = bat_iterator(odels);
    1345         259 :         if (bi.type == TYPE_void) {
    1346         141 :                 oid *r = (oid *) (dels->base + sizeof(ccand_t));
    1347       93621 :                 for (BUN x = lo; x < hi; x++)
    1348       93480 :                         r[x - lo] = x + odels->tseqbase;
    1349             :         } else {
    1350         118 :                 oid *r = (oid *) (dels->base + sizeof(ccand_t));
    1351         118 :                 memcpy(r, (const oid *) bi.base + lo, sizeof(oid) * (hi - lo));
    1352             :         }
    1353         259 :         bat_iterator_end(&bi);
    1354         259 :         assert(bn->tvheap == NULL);
    1355         259 :         bn->tvheap = dels;
    1356         259 :         BATsetcount(bn, bn->batCount - (hi - lo));
    1357        3160 :   doreturn:
    1358        3160 :         TRC_DEBUG(ALGO, "nr=" BUNFMT ", odels=" ALGOBATFMT
    1359             :                   " -> " ALGOBATFMT "\n",
    1360             :                   nr, ALGOBATPAR(odels),
    1361             :                   ALGOBATPAR(bn));
    1362             :         return bn;
    1363             : }
    1364             : 
    1365             : BAT *
    1366      103769 : BATmaskedcands(oid hseq, BUN nr, BAT *masked, bool selected)
    1367             : {
    1368      103769 :         const char *nme;
    1369      103769 :         Heap *msks;
    1370      103769 :         ccand_t *c;
    1371      103769 :         BUN nmask;
    1372      103769 :         BAT *bn;
    1373             : 
    1374      103769 :         assert(masked->ttype == TYPE_msk);
    1375             : 
    1376      103769 :         bn = COLnew(hseq, TYPE_void, 0, TRANSIENT);
    1377      103769 :         if (bn == NULL)
    1378             :                 return NULL;
    1379      103769 :         BATtseqbase(bn, hseq);
    1380             : 
    1381      103769 :         if (BATcount(masked) == 0)
    1382             :                 return bn;
    1383             : 
    1384      103769 :         nme = BBP_physical(bn->batCacheid);
    1385      103769 :         if ((msks = GDKmalloc(sizeof(Heap))) == NULL){
    1386           0 :                 BBPreclaim(bn);
    1387           0 :                 return NULL;
    1388             :         }
    1389      207536 :         *msks = (Heap) {
    1390      103768 :                 .farmid = BBPselectfarm(bn->batRole, bn->ttype, varheap),
    1391      103768 :                 .parentid = bn->batCacheid,
    1392             :                 .dirty = true,
    1393             :                 .refs = ATOMIC_VAR_INIT(1),
    1394             :         };
    1395      103768 :         strconcat_len(msks->filename, sizeof(msks->filename),
    1396             :                       nme, ".theap", NULL);
    1397             : 
    1398      103769 :         nmask = (nr + 31) / 32;
    1399      207538 :         if (msks->farmid < 0 ||
    1400      103769 :             HEAPalloc(msks, nmask + (sizeof(ccand_t)/sizeof(uint32_t)), sizeof(uint32_t)) != GDK_SUCCEED) {
    1401           0 :                 GDKfree(msks);
    1402           0 :                 BBPreclaim(bn);
    1403           0 :                 return NULL;
    1404             :         }
    1405      103769 :         c = (ccand_t *) msks->base;
    1406      103769 :         *c = (ccand_t) {
    1407             :                 .type = CAND_MSK,
    1408             : //              .mask = true,
    1409             :         };
    1410      103769 :         msks->free = sizeof(ccand_t) + nmask * sizeof(uint32_t);
    1411      103769 :         uint32_t *r = (uint32_t*)(msks->base + sizeof(ccand_t));
    1412      103769 :         BATiter bi = bat_iterator(masked);
    1413      103769 :         if (selected) {
    1414      103769 :                 if (nr <= bi.count)
    1415      103769 :                         memcpy(r, bi.base, nmask * sizeof(uint32_t));
    1416             :                 else
    1417           0 :                         memcpy(r, bi.base, (bi.count + 31) / 32 * sizeof(uint32_t));
    1418             :         } else {
    1419           0 :                 const uint32_t *s = (const uint32_t *) bi.base;
    1420           0 :                 BUN nmask_ = (bi.count + 31) / 32;
    1421           0 :                 for (BUN i = 0; i < nmask_; i++)
    1422           0 :                         r[i] = ~s[i];
    1423             :         }
    1424      103769 :         if (nr > bi.count) {
    1425           0 :                 BUN rest = bi.count & 31;
    1426           0 :                 BUN nmask_ = (bi.count + 31) / 32;
    1427           0 :                 if (rest > 0)
    1428           0 :                         r[nmask_ -1] |= ((1U << (32 - rest)) - 1) << rest;
    1429           0 :                 for (BUN j = nmask_; j < nmask; j++)
    1430           0 :                         r[j] = ~0;
    1431             :         }
    1432      103769 :         bat_iterator_end(&bi);
    1433             :         /* make sure last word doesn't have any spurious bits set */
    1434      103768 :         BUN cnt = nr % 32;
    1435      103768 :         if (cnt > 0)
    1436      102506 :                 r[nmask - 1] &= (1U << cnt) - 1;
    1437             :         cnt = 0;
    1438    13060076 :         for (BUN i = 0; i < nmask; i++) {
    1439    12956308 :                 if (cnt == 0 && r[i] != 0)
    1440      103431 :                         c->firstbit = candmask_lobit(r[i]) + i * 32;
    1441    12956308 :                 cnt += candmask_pop(r[i]);
    1442             :         }
    1443      103768 :         if (cnt > 0) {
    1444      103440 :                 assert(bn->tvheap == NULL);
    1445      103440 :                 bn->tvheap = msks;
    1446      103440 :                 bn->tseqbase += (oid) c->firstbit;
    1447             :         } else {
    1448             :                 /* no point having a mask if it's empty */
    1449         328 :                 HEAPfree(msks, true);
    1450         328 :                 GDKfree(msks);
    1451             :         }
    1452      103768 :         BATsetcount(bn, cnt);
    1453      103768 :         TRC_DEBUG(ALGO, "hseq=" OIDFMT ", masked=" ALGOBATFMT ", selected=%s"
    1454             :                   " -> " ALGOBATFMT "\n",
    1455             :                   hseq, ALGOBATPAR(masked),
    1456             :                   selected ? "true" : "false",
    1457             :                   ALGOBATPAR(bn));
    1458      103768 :         assert(bn->tseqbase != oid_nil);
    1459             :         return bn;
    1460             : }
    1461             : 
    1462             : /* convert a masked candidate list to a positive or negative candidate list */
    1463             : BAT *
    1464      112528 : BATunmask(BAT *b)
    1465             : {
    1466             : //      assert(!mask_cand(b) || CCAND(b)->mask); /* todo handle negmask case */
    1467      112528 :         BUN cnt;
    1468      112528 :         uint32_t rem;
    1469      112528 :         uint32_t val;
    1470      112528 :         const uint32_t *src;
    1471      112528 :         oid *dst;
    1472      112528 :         BUN n = 0;
    1473      112528 :         oid tseq = b->hseqbase;
    1474      112528 :         bool negcand = false;
    1475             : 
    1476      112528 :         BATiter bi = bat_iterator(b);
    1477      112526 :         if (mask_cand(b)) {
    1478      108171 :                 cnt = ccand_free(b) / sizeof(uint32_t);
    1479      108171 :                 rem = 0;
    1480      108171 :                 src = (const uint32_t *) ccand_first(b);
    1481      108171 :                 tseq = b->tseqbase;
    1482      108171 :                 tseq -= (oid) CCAND(b)->firstbit;
    1483             :                 /* create negative candidate list if more than half the
    1484             :                  * bits are set */
    1485      108171 :                 negcand = BATcount(b) > cnt * 16;
    1486             :         } else {
    1487        4355 :                 cnt = bi.count / 32;
    1488        4355 :                 rem = bi.count % 32;
    1489        4355 :                 src = (const uint32_t *) bi.base;
    1490             :         }
    1491      112526 :         BAT *bn;
    1492             : 
    1493      112526 :         if (negcand) {
    1494       91199 :                 bn = COLnew(b->hseqbase, TYPE_void, 0, TRANSIENT);
    1495       91200 :                 if (bn == NULL) {
    1496           0 :                         bat_iterator_end(&bi);
    1497           0 :                         return NULL;
    1498             :                 }
    1499       91200 :                 Heap *dels;
    1500       91200 :                 if ((dels = GDKmalloc(sizeof(Heap))) == NULL) {
    1501           0 :                         BBPreclaim(bn);
    1502           0 :                         bat_iterator_end(&bi);
    1503           0 :                         return NULL;
    1504             :                 }
    1505      182392 :                 *dels = (Heap) {
    1506       91196 :                         .farmid = BBPselectfarm(TRANSIENT, TYPE_void, varheap),
    1507       91196 :                         .parentid = bn->batCacheid,
    1508             :                         .dirty = true,
    1509             :                         .refs = ATOMIC_VAR_INIT(1),
    1510             :                 };
    1511       91196 :                 strconcat_len(dels->filename, sizeof(dels->filename),
    1512       91196 :                               BBP_physical(bn->batCacheid), ".theap", NULL);
    1513             : 
    1514      182395 :                 if (dels->farmid < 0 ||
    1515       91198 :                     HEAPalloc(dels, cnt * 32 - bi.count
    1516             :                               + sizeof(ccand_t) / sizeof(oid), sizeof(oid)) != GDK_SUCCEED) {
    1517           0 :                         GDKfree(dels);
    1518           0 :                         BBPreclaim(bn);
    1519           0 :                         bat_iterator_end(&bi);
    1520           0 :                         return NULL;
    1521             :                 }
    1522       91197 :                 * (ccand_t *) dels->base = (ccand_t) {
    1523             :                         .type = CAND_NEGOID,
    1524             :                 };
    1525       91197 :                 dst = (oid *) (dels->base + sizeof(ccand_t));
    1526    11057464 :                 for (BUN p = 0, v = 0; p < cnt; p++, v += 32) {
    1527    10966267 :                         if ((val = src[p]) == ~UINT32_C(0))
    1528     9295531 :                                 continue;
    1529    53594281 :                         for (uint32_t i = 0; i < 32; i++) {
    1530    52014570 :                                 if ((val & (1U << i)) == 0) {
    1531    45877450 :                                         if (v + i >= b->batCount + n)
    1532             :                                                 break;
    1533    45786425 :                                         dst[n++] = tseq + v + i;
    1534             :                                 }
    1535             :                         }
    1536             :                 }
    1537       91197 :                 if (n == 0) {
    1538             :                         /* didn't need it after all */
    1539          10 :                         HEAPfree(dels, true);
    1540          10 :                         GDKfree(dels);
    1541             :                 } else {
    1542       91187 :                         dels->free = sizeof(ccand_t) + n * sizeof(oid);
    1543       91187 :                         dels->dirty = true;
    1544       91187 :                         assert(bn->tvheap == NULL);
    1545       91187 :                         bn->tvheap = dels;
    1546             :                 }
    1547       91197 :                 BATsetcount(bn, n=bi.count);
    1548       91198 :                 bn->tseqbase = tseq;
    1549             :         } else {
    1550       21327 :                 bn = COLnew(b->hseqbase, TYPE_oid, mask_cand(b) ? bi.count : 1024, TRANSIENT);
    1551       21329 :                 if (bn == NULL) {
    1552           0 :                         bat_iterator_end(&bi);
    1553           0 :                         return NULL;
    1554             :                 }
    1555       21329 :                 dst = (oid *) Tloc(bn, 0);
    1556     2684808 :                 for (BUN p = 0; p < cnt; p++) {
    1557     2663480 :                         if ((val = src[p]) == 0)
    1558     1891308 :                                 continue;
    1559    25472812 :                         for (uint32_t i = 0; i < 32; i++) {
    1560    24700641 :                                 if (val & (1U << i)) {
    1561    21521067 :                                         if (n == BATcapacity(bn)) {
    1562          81 :                                                 BATsetcount(bn, n);
    1563          80 :                                                 if (BATextend(bn, BATgrows(bn)) != GDK_SUCCEED) {
    1564           0 :                                                         BBPreclaim(bn);
    1565           0 :                                                         bat_iterator_end(&bi);
    1566           0 :                                                         return NULL;
    1567             :                                                 }
    1568          80 :                                                 dst = (oid *) Tloc(bn, 0);
    1569             :                                         }
    1570    21521066 :                                         dst[n++] = tseq + p * 32 + i;
    1571             :                                 }
    1572             :                         }
    1573             :                 }
    1574             :                 /* the last partial mask word */
    1575       21328 :                 if (rem > 0 && (val = src[cnt]) != 0) {
    1576       21777 :                         for (uint32_t i = 0; i < rem; i++) {
    1577       19472 :                                 if (val & (1U << i)) {
    1578        1172 :                                         if (n == BATcapacity(bn)) {
    1579           0 :                                                 BATsetcount(bn, n);
    1580           0 :                                                 if (BATextend(bn, BATgrows(bn)) != GDK_SUCCEED) {
    1581           0 :                                                         BBPreclaim(bn);
    1582           0 :                                                         bat_iterator_end(&bi);
    1583           0 :                                                         return NULL;
    1584             :                                                 }
    1585           0 :                                                 dst = (oid *) Tloc(bn, 0);
    1586             :                                         }
    1587        1172 :                                         dst[n++] = tseq + cnt * 32 + i;
    1588             :                                 }
    1589             :                         }
    1590             :                 }
    1591       21328 :                 BATsetcount(bn, n);
    1592             :         }
    1593      112526 :         bat_iterator_end(&bi);
    1594      112529 :         bn->tkey = true;
    1595      112529 :         bn->tsorted = true;
    1596      112529 :         bn->trevsorted = n <= 1;
    1597      112529 :         bn->tnil = false;
    1598      112529 :         bn->tnonil = true;
    1599      112529 :         bn = virtualize(bn);
    1600      112528 :         TRC_DEBUG(ALGO, ALGOBATFMT " -> " ALGOBATFMT "\n", ALGOBATPAR(b), ALGOBATPAR(bn));
    1601             :         return bn;
    1602             : }

Generated by: LCOV version 1.14