LCOV - code coverage report
Current view: top level - gdk - gdk_cand.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 749 982 76.3 %
Date: 2024-12-19 20:05:57 Functions: 24 26 92.3 %

          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        8866 : BATiscand(BAT *b)
      20             : {
      21        8866 :         if (ATOMtype(b->ttype) != TYPE_oid)
      22             :                 return false;
      23        8866 :         if (complex_cand(b))
      24             :                 return true;
      25        8457 :         if (b->ttype == TYPE_void && is_oid_nil(b->tseqbase))
      26             :                 return false;
      27       16914 :         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        2770 : newdensecand(oid first, oid last)
      34             : {
      35        2770 :         if (last <= first)
      36           0 :                 first = last = 0; /* empty range */
      37        2770 :         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       81365 : BATmergecand(BAT *a, BAT *b)
      47             : {
      48       81365 :         BAT *bn;
      49       81365 :         oid *restrict p, i;
      50       81365 :         struct canditer cia, cib;
      51             : 
      52       81365 :         BATcheck(a, NULL);
      53       81365 :         BATcheck(b, NULL);
      54             : 
      55       81365 :         canditer_init(&cia, NULL, a);
      56       81355 :         canditer_init(&cib, NULL, b);
      57             : 
      58             :         /* we can return a if b is empty (and v.v.) */
      59       81362 :         if (cia.ncand == 0) {
      60       13011 :                 bn = canditer_slice(&cib, 0, cib.ncand);
      61       13011 :                 goto doreturn;
      62             :         }
      63       68351 :         if (cib.ncand == 0) {
      64       10959 :                 bn = canditer_slice(&cia, 0, cia.ncand);
      65       10959 :                 goto doreturn;
      66             :         }
      67             :         /* we can return a if a fully covers b (and v.v) */
      68       57392 :         if (cia.tpe == cand_dense && cib.tpe == cand_dense) {
      69             :                 /* both are dense */
      70       10984 :                 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        2331 :                         bn = newdensecand(cia.seq, cia.seq + cia.ncand < cib.seq + cib.ncand ? cib.seq + cib.ncand : cia.seq + cia.ncand);
      74        2333 :                         goto doreturn;
      75             :                 }
      76        8653 :                 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         432 :                         bn = newdensecand(cib.seq, cia.seq + cia.ncand < cib.seq + cib.ncand ? cib.seq + cib.ncand : cia.seq + cia.ncand);
      80         432 :                         goto doreturn;
      81             :                 }
      82             :         }
      83       54629 :         if (cia.tpe == cand_dense
      84       11411 :             && cia.seq <= cib.seq
      85        5035 :             && canditer_last(&cia) >= canditer_last(&cib)) {
      86         342 :                 bn = canditer_slice(&cia, 0, cia.ncand);
      87         342 :                 goto doreturn;
      88             :         }
      89       54287 :         if (cib.tpe == cand_dense
      90       38003 :             && cib.seq <= cia.seq
      91       10011 :             && canditer_last(&cib) >= canditer_last(&cia)) {
      92         186 :                 bn = canditer_slice(&cib, 0, cib.ncand);
      93         186 :                 goto doreturn;
      94             :         }
      95             : 
      96       54101 :         bn = COLnew(0, TYPE_oid, cia.ncand + cib.ncand, TRANSIENT);
      97       54101 :         if (bn == NULL)
      98           0 :                 goto doreturn;
      99       54101 :         p = (oid *) Tloc(bn, 0);
     100       54101 :         if (cia.tpe == cand_dense && cib.tpe == cand_dense) {
     101             :                 /* both lists are dense */
     102        8221 :                 if (cia.seq > cib.seq) {
     103        4097 :                         struct canditer ci;
     104        4097 :                         ci = cia;
     105        4097 :                         cia = cib;
     106        4097 :                         cib = ci;
     107             :                 }
     108             :                 /* cia completely before cib */
     109        8221 :                 assert(cia.seq + cia.ncand < cib.seq);
     110       31021 :                 for (i = cia.seq; i < cia.seq + cia.ncand; i++)
     111       22800 :                         *p++ = i;
     112             :                 /* there is a gap */
     113       27513 :                 for (i = cib.seq; i < cib.seq + cib.ncand; i++)
     114       19292 :                         *p++ = i;
     115       45880 :         } else if (cia.tpe == cand_dense || cib.tpe == cand_dense) {
     116       32444 :                 if (cib.tpe == cand_dense) {
     117       29596 :                         struct canditer ci;
     118       29596 :                         ci = cia;
     119       29596 :                         cia = cib;
     120       29596 :                         cib = ci;
     121             :                 }
     122             :                 /* only cia is dense */
     123             : 
     124             :                 /* copy part of cib that's before the start of cia */
     125      134119 :                 while (canditer_peek(&cib) < cia.seq) {
     126      101674 :                         *p++ = canditer_next(&cib);
     127             :                 }
     128             :                 /* copy all of cia */
     129       74198 :                 for (i = cia.seq; i < cia.seq + cia.ncand; i++)
     130       41760 :                         *p++ = i;
     131             :                 /* skip over part of cib that overlaps with cia */
     132       32438 :                 canditer_setidx(&cib, canditer_search(&cib, cia.seq + cia.ncand, true));
     133             :                 /* copy rest of cib */
     134      122558 :                 while (cib.next < cib.ncand) {
     135       90115 :                         *p++ = canditer_next(&cib);
     136             :                 }
     137             :         } else {
     138             :                 /* a and b are both not dense */
     139       13436 :                 oid ao = canditer_next(&cia);
     140       13435 :                 oid bo = canditer_next(&cib);
     141    24523754 :                 while (!is_oid_nil(ao) && !is_oid_nil(bo)) {
     142    24510319 :                         if (ao < bo) {
     143    13989474 :                                 *p++ = ao;
     144    13989474 :                                 ao = canditer_next(&cia);
     145    10520845 :                         } else if (bo < ao) {
     146     9459292 :                                 *p++ = bo;
     147     9459292 :                                 bo = canditer_next(&cib);
     148             :                         } else {
     149     1061553 :                                 *p++ = ao;
     150     1061553 :                                 ao = canditer_next(&cia);
     151     1061547 :                                 bo = canditer_next(&cib);
     152             :                         }
     153             :                 }
     154      563037 :                 while (!is_oid_nil(ao)) {
     155      549603 :                         *p++ = ao;
     156      549603 :                         ao = canditer_next(&cia);
     157             :                 }
     158      418792 :                 while (!is_oid_nil(bo)) {
     159      405358 :                         *p++ = bo;
     160      405358 :                         bo = canditer_next(&cib);
     161             :                 }
     162             :         }
     163             : 
     164             :         /* properties */
     165       54098 :         BATsetcount(bn, (BUN) (p - (oid *) Tloc(bn, 0)));
     166       54093 :         bn->trevsorted = BATcount(bn) <= 1;
     167       54093 :         bn->tsorted = true;
     168       54093 :         bn->tkey = true;
     169       54093 :         bn->tnil = false;
     170       54093 :         bn->tnonil = true;
     171       54093 :         bn = virtualize(bn);
     172       81363 :   doreturn:
     173       81363 :         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          16 : BATintersectcand(BAT *a, BAT *b)
     185             : {
     186          16 :         BAT *bn;
     187          16 :         oid *restrict p;
     188          16 :         struct canditer cia, cib;
     189             : 
     190          16 :         BATcheck(a, NULL);
     191          16 :         BATcheck(b, NULL);
     192             : 
     193          16 :         canditer_init(&cia, NULL, a);
     194          16 :         canditer_init(&cib, NULL, b);
     195             : 
     196          16 :         if (cia.ncand == 0 || cib.ncand == 0) {
     197           5 :                 bn = BATdense(0, 0, 0);
     198           5 :                 goto doreturn;
     199             :         }
     200             : 
     201          11 :         if (cia.tpe == cand_dense && cib.tpe == cand_dense) {
     202             :                 /* both lists are dense */
     203           5 :                 bn = newdensecand(MAX(cia.seq, cib.seq), MIN(cia.seq + cia.ncand, cib.seq + cib.ncand));
     204           5 :                 goto doreturn;
     205             :         }
     206             : 
     207           6 :         bn = COLnew(0, TYPE_oid, MIN(cia.ncand, cib.ncand), TRANSIENT);
     208           6 :         if (bn == NULL)
     209           0 :                 goto doreturn;
     210           6 :         p = (oid *) Tloc(bn, 0);
     211           6 :         if (cia.tpe == cand_dense || cib.tpe == cand_dense) {
     212           6 :                 if (cib.tpe == cand_dense) {
     213           6 :                         struct canditer ci;
     214           6 :                         ci = cia;
     215           6 :                         cia = cib;
     216           6 :                         cib = ci;
     217             :                 }
     218             :                 /* only cia is dense */
     219             : 
     220             :                 /* search for first value in cib that is contained in cia */
     221           6 :                 canditer_setidx(&cib, canditer_search(&cib, cia.seq, true));
     222           6 :                 oid bo;
     223          20 :                 while (!is_oid_nil(bo = canditer_next(&cib)) && bo < cia.seq + cia.ncand)
     224          14 :                         *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           6 :         BATsetcount(bn, (BUN) (p - (oid *) Tloc(bn, 0)));
     244           6 :         bn->trevsorted = BATcount(bn) <= 1;
     245           6 :         bn->tsorted = true;
     246           6 :         bn->tkey = true;
     247           6 :         bn->tnil = false;
     248           6 :         bn->tnonil = true;
     249           6 :         bn = virtualize(bn);
     250          16 :   doreturn:
     251          16 :         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        6123 : BATdiffcand(BAT *a, BAT *b)
     260             : {
     261        6123 :         BAT *bn;
     262        6123 :         oid *restrict p;
     263        6123 :         struct canditer cia, cib;
     264             : 
     265        6123 :         BATcheck(a, NULL);
     266        6123 :         BATcheck(b, NULL);
     267             : 
     268        6123 :         canditer_init(&cia, NULL, a);
     269        6123 :         canditer_init(&cib, NULL, b);
     270             : 
     271        6123 :         if (cia.ncand == 0) {
     272           0 :                 bn = BATdense(0, 0, 0);
     273           0 :                 goto doreturn;
     274             :         }
     275        6123 :         if (cib.ncand == 0) {
     276         457 :                 bn = canditer_slice(&cia, 0, cia.ncand);
     277         457 :                 goto doreturn;
     278             :         }
     279             : 
     280        5666 :         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        5666 :         if (cia.tpe == cand_dense && cib.tpe == cand_dense) {
     287             :                 /* both a and b are dense */
     288        2183 :                 if (cia.seq < cib.seq) {
     289             :                         /* a starts before b */
     290         672 :                         if (cia.seq + cia.ncand <= cib.seq + cib.ncand) {
     291             :                                 /* b overlaps with end of a */
     292          94 :                                 bn = canditer_slice(&cia, 0, cib.seq - cia.seq);
     293          94 :                                 goto doreturn;
     294             :                         }
     295             :                         /* b is subset of a */
     296         578 :                         bn = canditer_slice2(&cia, 0, cib.seq - cia.seq,
     297             :                                              cib.seq + cib.ncand - cia.seq,
     298             :                                              cia.ncand);
     299         578 :                         goto doreturn;
     300             :                 } else {
     301             :                         /* cia.seq >= cib.seq */
     302        1511 :                         if (cia.seq + cia.ncand > cib.seq + cib.ncand) {
     303             :                                 /* b overlaps with beginning of a */
     304          71 :                                 bn = canditer_slice(&cia, cib.seq + cib.ncand - cia.seq, cia.ncand);
     305          71 :                                 goto doreturn;
     306             :                         }
     307             :                         /* a is subset f b */
     308        1440 :                         bn = BATdense(0, 0, 0);
     309        1440 :                         goto doreturn;
     310             :                 }
     311             :         }
     312        3483 :         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        1003 :         bn = COLnew(0, TYPE_oid, BATcount(a), TRANSIENT);
     323        1003 :         if (bn == NULL)
     324           0 :                 goto doreturn;
     325        1003 :         p = Tloc(bn, 0);
     326             :         /* find first position in b that is in range of a */
     327        1003 :         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        1003 :                 oid ob = canditer_next(&cib);
     332     7692126 :                 for (BUN i = 0; i < cia.ncand; i++) {
     333     7691123 :                         oid oa = canditer_next(&cia);
     334    10328075 :                         while (!is_oid_nil(ob) && ob < oa) {
     335     2636286 :                                 ob = canditer_next(&cib);
     336             :                         }
     337     7691123 :                         if (is_oid_nil(ob) || oa < ob)
     338     5053848 :                                 *p++ = oa;
     339             :                 }
     340             :         }
     341             : 
     342             :         /* properties */
     343        1003 :         BATsetcount(bn, (BUN) (p - (oid *) Tloc(bn, 0)));
     344        1003 :         bn->trevsorted = BATcount(bn) <= 1;
     345        1003 :         bn->tsorted = true;
     346        1003 :         bn->tkey = true;
     347        1003 :         bn->tnil = false;
     348        1003 :         bn->tnonil = true;
     349        1003 :         bn = virtualize(bn);
     350        6123 :   doreturn:
     351        6123 :         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      935147 : binsearchcand(const oid *cand, BUN hi, oid o)
     359             : {
     360      935147 :         BUN lo = 0;
     361             : 
     362      935147 :         if (o <= cand[lo])
     363             :                 return 0;
     364      464476 :         if (o > cand[hi])
     365      411345 :                 return hi + 1;
     366             :         /* loop invariant: cand[lo] < o <= cand[hi] */
     367      291604 :         while (hi > lo + 1) {
     368      260112 :                 BUN mid = (lo + hi) / 2;
     369      260112 :                 if (cand[mid] == o)
     370       21639 :                         return mid;
     371      238473 :                 if (cand[mid] < o)
     372             :                         lo = mid;
     373             :                 else
     374      167929 :                         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       21820 : count_mask_bits(const struct canditer *ci, BUN lo, BUN hi)
     383             : {
     384       21820 :         BUN n;
     385       21820 :         assert(lo <= hi);
     386       21820 :         assert(ci->tpe == cand_mask);
     387       21820 :         if (lo == hi)
     388             :                 return 0;
     389       21820 :         lo += ci->firstbit;
     390       21820 :         hi += ci->firstbit;
     391       21820 :         BUN loi = lo / 32;
     392       21820 :         BUN hii = hi / 32;
     393       21820 :         lo %= 32;
     394       21820 :         hi %= 32;
     395       21820 :         if (loi == hii)
     396        1109 :                 return (BUN) candmask_pop((ci->mask[loi] & ((1U << hi) - 1)) >> lo);
     397       20711 :         n = (BUN) candmask_pop(ci->mask[loi++] >> lo);
     398     2326823 :         while (loi < hii)
     399     2306112 :                 n += (BUN) candmask_pop(ci->mask[loi++]);
     400       20711 :         if (hi != 0)
     401        6411 :                 n += (BUN) candmask_pop(ci->mask[loi] & ((1U << hi) - 1));
     402             :         return n;
     403             : }
     404             : 
     405             : /* initialize a candidate iterator */
     406             : void
     407     8143709 : canditer_init(struct canditer *ci, BAT *b, BAT *s)
     408             : {
     409     8143709 :         assert(ci != NULL);
     410     8143709 :         BUN batcount = 0;
     411     8143709 :         oid hseq = 0;
     412     8143709 :         if (b) {
     413     7475187 :                 MT_lock_set(&b->theaplock);
     414     7473497 :                 batcount = BATcount(b);
     415     7473497 :                 hseq = b->hseqbase;
     416     7473497 :                 MT_lock_unset(&b->theaplock);
     417             :         }
     418             : 
     419     8143242 :         if (s == NULL) {
     420     3964461 :                 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     3964461 :                 *ci = (struct canditer) {
     429             :                         .tpe = cand_dense,
     430             :                         .seq = hseq,
     431             :                         .hseq = hseq,
     432             :                         .ncand = batcount,
     433             :                 };
     434     3964461 :                 return;
     435             :         }
     436             : 
     437     4178781 :         assert(ATOMtype(s->ttype) == TYPE_oid || s->ttype == TYPE_msk);
     438     4178781 :         assert(s->ttype == TYPE_msk|| s->tsorted);
     439     4178781 :         assert(s->ttype == TYPE_msk|| s->tkey);
     440     4178781 :         assert(s->ttype == TYPE_msk|| s->tnonil);
     441     4178781 :         assert(s->ttype == TYPE_void || s->tvheap == NULL);
     442             : 
     443     4178781 :         BUN cnt = BATcount(s);
     444             : 
     445     4178781 :         if (cnt == 0 || (b != NULL && batcount == 0)) {
     446             :                 /* candidate list for empty BAT or empty candidate list */
     447      996435 :                 *ci = (struct canditer) {
     448             :                         .tpe = cand_dense,
     449      996435 :                         .hseq = s->hseqbase,
     450             :                         .s = s,
     451             :                 };
     452      996435 :                 return;
     453             :         }
     454             : 
     455     3182346 :         *ci = (struct canditer) {
     456     3182346 :                 .seq = s->tseqbase,
     457     3182346 :                 .hseq = s->hseqbase,
     458             :                 .s = s,
     459             :         };
     460             : 
     461     3182346 :         if (mask_cand(s)) {
     462       21822 :                 ci->tpe = cand_mask;
     463       21822 :                 ci->mask = (const uint32_t *) ccand_first(s);
     464       21822 :                 ci->seq = s->tseqbase - (oid) CCAND(s)->firstbit;
     465       21822 :                 ci->hseq = s->hseqbase;
     466       21822 :                 ci->nvals = ccand_free(s) / sizeof(uint32_t);
     467       21822 :                 cnt = ci->nvals * 32;
     468     3160524 :         } 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     3160524 :         } else if (s->ttype == TYPE_void) {
     475     2612859 :                 assert(!is_oid_nil(ci->seq));
     476     2612859 :                 if (s->tvheap) {
     477       73731 :                         assert(ccand_free(s) % SIZEOF_OID == 0);
     478       73731 :                         ci->nvals = ccand_free(s) / SIZEOF_OID;
     479       73731 :                         if (ci->nvals > 0) {
     480       73731 :                                 ci->tpe = cand_except;
     481       73731 :                                 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      547665 :         } else if (is_oid_nil(ci->seq)) {
     490      511701 :                 ci->tpe = cand_materialized;
     491      511701 :                 ci->oids = (const oid *) Tloc(s, 0);
     492      511701 :                 ci->seq = ci->oids[0];
     493      511701 :                 ci->nvals = cnt;
     494             :         } else {
     495             :                 /* materialized dense: no exceptions */
     496             :                 ci->tpe = cand_dense;
     497             :         }
     498     3182346 :         switch (ci->tpe) {
     499      511702 :         case cand_materialized:
     500      511702 :                 if (b != NULL) {
     501      392114 :                         BUN p = binsearchcand(ci->oids, cnt - 1U, hseq);
     502             :                         /* p == cnt means candidate list is completely
     503             :                          * before b */
     504      392113 :                         ci->offset = p;
     505      392113 :                         ci->oids += p;
     506      392113 :                         cnt -= p;
     507      392113 :                         if (cnt > 0) {
     508      392113 :                                 cnt = binsearchcand(ci->oids, cnt  - 1U,
     509             :                                                     hseq + batcount);
     510             :                                 /* cnt == 0 means candidate list is
     511             :                                  * completely after b */
     512             :                         }
     513      392115 :                         if (cnt == 0) {
     514             :                                 /* no overlap */
     515           0 :                                 *ci = (struct canditer) {
     516             :                                         .tpe = cand_dense,
     517             :                                         .s = s,
     518             :                                 };
     519           0 :                                 return;
     520             :                         }
     521      392115 :                         ci->seq = ci->oids[0];
     522      392115 :                         ci->nvals = cnt;
     523      392115 :                         if (ci->oids[cnt - 1U] - ci->seq == cnt - 1U) {
     524             :                                 /* actually dense */
     525          58 :                                 ci->tpe = cand_dense;
     526          58 :                                 ci->oids = NULL;
     527          58 :                                 ci->nvals = 0;
     528             :                         }
     529             :                 }
     530             :                 break;
     531       73731 :         case cand_except:
     532             :                 /* exceptions must all be within range of s */
     533       73731 :                 assert(ci->oids[0] >= ci->seq);
     534       73731 :                 assert(ci->oids[ci->nvals - 1U] < ci->seq + cnt + ci->nvals);
     535             :                 /* prune exceptions at either end of range of s */
     536     2735367 :                 while (ci->nvals > 0 && ci->oids[0] == ci->seq) {
     537     2661636 :                         ci->nvals--;
     538     2661636 :                         ci->oids++;
     539     2661636 :                         ci->seq++;
     540             :                 }
     541       73945 :                 while (ci->nvals > 0 &&
     542       72156 :                        ci->oids[ci->nvals - 1U] == ci->seq + cnt + ci->nvals - 1U)
     543         214 :                         ci->nvals--;
     544       73731 :                 if (b != NULL) {
     545       69340 :                         if (ci->seq + cnt + ci->nvals <= hseq ||
     546       69340 :                             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       73731 :                 if (ci->nvals > 0) {
     556       71942 :                         if (b == NULL)
     557             :                                 break;
     558       67774 :                         BUN p;
     559       67774 :                         p = binsearchcand(ci->oids, ci->nvals - 1U, hseq);
     560       67774 :                         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       67774 :                         assert(hseq > ci->seq || p == 0);
     571       67774 :                         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       67774 :                         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       67774 :                         while (ci->nvals > 0 && ci->oids[0] == ci->seq) {
     588           0 :                                 ci->nvals--;
     589           0 :                                 ci->oids++;
     590           0 :                                 ci->seq++;
     591             :                         }
     592       67774 :                         while (ci->nvals > 0 &&
     593       67774 :                                ci->oids[ci->nvals - 1U] == ci->seq + cnt + ci->nvals - 1U)
     594           0 :                                 ci->nvals--;
     595       67774 :                         if (ci->nvals > 0)
     596             :                                 break;
     597             :                 }
     598        1789 :                 ci->tpe = cand_dense;
     599        1789 :                 ci->oids = NULL;
     600             :                 /* fall through */
     601     2576886 :         case cand_dense:
     602     2576886 :                 if (b != NULL) {
     603     2250456 :                         if (ci->seq + cnt <= hseq ||
     604     2250456 :                             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     2250456 :                         if (hseq > ci->seq) {
     613           0 :                                 cnt -= hseq - ci->seq;
     614           0 :                                 ci->offset += hseq - ci->seq;
     615           0 :                                 ci->seq = hseq;
     616             :                         }
     617     2250456 :                         if (ci->seq + cnt > hseq + batcount)
     618           0 :                                 cnt = hseq + batcount - ci->seq;
     619             :                 }
     620             :                 break;
     621       21818 :         case cand_mask:
     622       21818 :                 assert(s->tseqbase != oid_nil);
     623       21818 :                 if (b != NULL) {
     624        7986 :                         if (ci->seq + cnt <= hseq ||
     625        7986 :                             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        7986 :                         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        7986 :                         if (ci->seq + cnt > hseq + batcount) {
     641        7704 :                                 cnt = hseq + batcount - ci->seq;
     642             :                         }
     643        7986 :                         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       21818 :                 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       21818 :                 if (ci->firstbit == 0) {
     660       57616 :                         while (cnt >= 32U && ci->mask[0] == 0) {
     661       35797 :                                 cnt -= 32U;
     662       35797 :                                 ci->mask++;
     663       35797 :                                 ci->nvals--;
     664             :                         }
     665             :                 }
     666             :                 /* check whether there are any 1 bits in the last word */
     667       21818 :                 if (cnt == 0 ||
     668       21818 :                     (cnt < 32U - ci->firstbit &&
     669        1109 :                      ((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       21818 :                 int i = candmask_lobit(ci->mask[0] >> ci->firstbit);
     681       21818 :                 assert(i >= 0);      /* there should be a set bit */
     682       21818 :                 ci->firstbit += i;
     683       21818 :                 cnt -= i;
     684       21818 :                 if (mask_cand(s))
     685       21820 :                         ci->mskoff = s->tseqbase - (oid) CCAND(s)->firstbit + (ci->mask - (const uint32_t *) ccand_first(s)) * 32U;
     686             :                 else
     687           0 :                         ci->mskoff = s->tseqbase + (ci->mask - (const uint32_t *) s->theap->base) * 32U;
     688       21818 :                 ci->seq = ci->mskoff + ci->firstbit;
     689       21818 :                 ci->nextbit = ci->firstbit;
     690             :                 /* at this point we know that bit ci->firstbit is set
     691             :                  * in ci->mask[0] */
     692       21818 :                 ci->lastbit = (ci->firstbit + cnt - 1U) % 32U + 1U;
     693       21818 :                 if (ci->lastbit < 32 &&
     694        7704 :                     (ci->mask[ci->nvals - 1] & ((1U << ci->lastbit) - 1)) == 0) {
     695             :                         /* last partial word is all zero */
     696         184 :                         cnt -= ci->lastbit;
     697         184 :                         ci->lastbit = 32;
     698         184 :                         ci->nvals--;
     699             :                 }
     700       21818 :                 if (ci->lastbit == 32) {
     701             :                         /* "remove" zero words at the end */
     702       22263 :                         while (cnt >= 32 && ci->mask[ci->nvals - 1] == 0) {
     703        7963 :                                 ci->nvals--;
     704        7963 :                                 cnt -= 32;
     705             :                         }
     706             :                 }
     707       21818 :                 ci->ncand = count_mask_bits(ci, 0, cnt);
     708       21821 :                 return;
     709             :         }
     710     3160529 :         ci->ncand = cnt;
     711     3160529 :         ci->hseq += ci->offset;
     712             : }
     713             : 
     714             : /* return the next candidate without advancing */
     715             : oid
     716     2538091 : canditer_peek(struct canditer *ci)
     717             : {
     718     2538091 :         oid o = oid_nil;
     719     2538091 :         if (ci->next == ci->ncand)
     720             :                 return oid_nil;
     721     2531681 :         switch (ci->tpe) {
     722     2267681 :         case cand_dense:
     723     2267681 :                 o = ci->seq + ci->next;
     724     2267681 :                 break;
     725      260024 :         case cand_materialized:
     726      260024 :                 assert(ci->next < ci->nvals);
     727      260024 :                 o = ci->oids[ci->next];
     728      260024 :                 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         911 : canditer_prev(struct canditer *ci)
     751             : {
     752         911 :         if (ci->next == 0)
     753           0 :                 return oid_nil;
     754         911 :         switch (ci->tpe) {
     755         911 :         case cand_dense:
     756         911 :                 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        3131 : canditer_peekprev(struct canditer *ci)
     785             : {
     786        3131 :         oid o = oid_nil;
     787             : 
     788        3131 :         if (ci->next == 0)
     789             :                 return oid_nil;
     790        3131 :         switch (ci->tpe) {
     791        3131 :         case cand_dense:
     792        3131 :                 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     1326688 : canditer_last(const struct canditer *ci)
     863             : {
     864     1326688 :         if (ci->ncand == 0)
     865           0 :                 return oid_nil;
     866     1326688 :         switch (ci->tpe) {
     867      999621 :         case cand_dense:
     868      999621 :                 return ci->seq + ci->ncand - 1;
     869      260391 :         case cand_materialized:
     870      260391 :                 return ci->oids[ci->ncand - 1];
     871       63608 :         case cand_except:
     872       63608 :                 return ci->seq + ci->ncand + ci->nvals - 1;
     873        3068 :         case cand_mask:
     874       15471 :                 for (uint8_t i = ci->lastbit; i > 0; ) {
     875       15471 :                         if (ci->mask[ci->nvals - 1] & (1U << --i))
     876        3068 :                                 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   358552594 : canditer_idx(const struct canditer *ci, BUN p)
     886             : {
     887   358552594 :         if (p >= ci->ncand)
     888           0 :                 return oid_nil;
     889   358552594 :         switch (ci->tpe) {
     890   173071705 :         case cand_dense:
     891   173071705 :                 return ci->seq + p;
     892   185277641 :         case cand_materialized:
     893   185277641 :                 return ci->oids[p];
     894      203170 :         case cand_except: {
     895      203170 :                 oid o = ci->seq + p;
     896      203170 :                 if (o < ci->oids[0])
     897             :                         return o;
     898      153256 :                 if (o + ci->nvals > ci->oids[ci->nvals - 1])
     899             :                         return o + ci->nvals;
     900       81159 :                 BUN lo = 0, hi = ci->nvals - 1;
     901      558045 :                 while (hi - lo > 1) {
     902      395727 :                         BUN mid = (hi + lo) / 2;
     903      395727 :                         if (ci->oids[mid] - mid > o)
     904             :                                 hi = mid;
     905             :                         else
     906      255851 :                                 lo = mid;
     907             :                 }
     908       81159 :                 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      630152 : canditer_setidx(struct canditer *ci, BUN p)
     944             : {
     945      630152 :         if (p != ci->next) {
     946      593330 :                 if (p >= ci->ncand) {
     947       54986 :                         ci->next = ci->ncand;
     948       54986 :                         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      538344 :                         ci->next = p;
     965      538344 :                         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      630152 : }
     981             : 
     982             : /* reset */
     983             : void
     984      682841 : canditer_reset(struct canditer *ci)
     985             : {
     986      682841 :         if (ci->tpe == cand_mask) {
     987           0 :                 ci->nextbit = ci->firstbit;
     988           0 :                 ci->nextmsk = 0;
     989             :         } else {
     990      682841 :                 ci->add = 0;
     991             :         }
     992      682841 :         ci->next = 0;
     993      682841 : }
     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     1056203 : canditer_search(const struct canditer *ci, oid o, bool next)
    1000             : {
    1001     1056203 :         BUN p;
    1002             : 
    1003     1056203 :         switch (ci->tpe) {
    1004      972121 :         case cand_dense:
    1005      972121 :                 if (o < ci->seq)
    1006         638 :                         return next ? 0 : BUN_NONE;
    1007      971483 :                 if (o >= ci->seq + ci->ncand)
    1008      129742 :                         return next ? ci->ncand : BUN_NONE;
    1009      841741 :                 return o - ci->seq;
    1010       54394 :         case cand_materialized:
    1011       54394 :                 if (ci->nvals == 0)
    1012             :                         return 0;
    1013       54394 :                 p = binsearchcand(ci->oids, ci->nvals - 1, o);
    1014       54392 :                 if (next || (p != ci->nvals && ci->oids[p] == o))
    1015       54147 :                         return p;
    1016             :                 break;
    1017       29688 :         case cand_except:
    1018       29688 :                 if (o < ci->seq)
    1019           0 :                         return next ? 0 : BUN_NONE;
    1020       29688 :                 if (o >= ci->seq + ci->ncand + ci->nvals)
    1021         929 :                         return next ? ci->ncand : BUN_NONE;
    1022       28759 :                 p = binsearchcand(ci->oids, ci->nvals - 1, o);
    1023       28759 :                 if (next || p == ci->nvals || ci->oids[p] != o)
    1024       28604 :                         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         769 : canditer_sliceval_mask(const struct canditer *ci, oid lo1, oid hi1, BUN cnt1,
    1046             :                        oid lo2, oid hi2, BUN cnt2)
    1047             : {
    1048         769 :         assert(cnt2 == 0 || !is_oid_nil(lo2));
    1049         769 :         assert(cnt2 == 0 || lo2 >= hi1);
    1050         769 :         if (is_oid_nil(lo1) || lo1 < ci->mskoff + ci->firstbit)
    1051         180 :                 lo1 = ci->mskoff + ci->firstbit;
    1052         769 :         if (is_oid_nil(hi1) || hi1 >= ci->mskoff + (ci->nvals - 1) * 32 + ci->lastbit)
    1053         600 :                 hi1 = ci->mskoff + (ci->nvals - 1) * 32 + ci->lastbit;
    1054             : 
    1055         769 :         BAT *bn = COLnew(0, TYPE_oid, cnt1 + cnt2, TRANSIENT);
    1056         769 :         if (bn == NULL)
    1057             :                 return NULL;
    1058         769 :         bn->tkey = true;
    1059             : 
    1060         769 :         if (hi1 > ci->mskoff) {
    1061         625 :                 if (lo1 < ci->mskoff)
    1062             :                         lo1 = 0;
    1063             :                 else
    1064         625 :                         lo1 -= ci->mskoff;
    1065         625 :                 hi1 -= ci->mskoff;
    1066      146858 :                 for (oid o = lo1; o < hi1 && cnt1 > 0; o++) {
    1067      146233 :                         if (ci->mask[o / 32] & (1U << (o % 32))) {
    1068      116941 :                                 if (BUNappend(bn, &(oid){o + ci->mskoff}, false) != GDK_SUCCEED) {
    1069           0 :                                         BBPreclaim(bn);
    1070           0 :                                         return NULL;
    1071             :                                 }
    1072      116941 :                                 cnt1--;
    1073             :                         }
    1074             :                 }
    1075             :         }
    1076         769 :         if (cnt2 > 0) {
    1077          28 :                 if (lo2 < ci->mskoff + ci->firstbit)
    1078             :                         lo2 = ci->mskoff + ci->firstbit;
    1079          28 :                 if (is_oid_nil(hi2) || hi2 >= ci->mskoff + (ci->nvals - 1) * 32 + ci->lastbit)
    1080          28 :                         hi2 = ci->mskoff + (ci->nvals - 1) * 32 + ci->lastbit;
    1081             : 
    1082          28 :                 lo2 -= ci->mskoff;
    1083          28 :                 hi2 -= ci->mskoff;
    1084          96 :                 for (oid o = lo2; o < hi2 && cnt2 > 0; o++) {
    1085          68 :                         if (ci->mask[o / 32] & (1U << (o % 32))) {
    1086          62 :                                 if (BUNappend(bn, &(oid){o + ci->mskoff}, false) != GDK_SUCCEED) {
    1087           0 :                                         BBPreclaim(bn);
    1088           0 :                                         return NULL;
    1089             :                                 }
    1090          62 :                                 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      452637 : canditer_slice(const struct canditer *ci, BUN lo, BUN hi)
    1103             : {
    1104      452637 :         BAT *bn;
    1105      452637 :         oid o;
    1106      452637 :         BUN add;
    1107             : 
    1108      452637 :         assert(ci != NULL);
    1109             : 
    1110      452637 :         if (lo >= ci->ncand || lo >= hi)
    1111       67296 :                 return BATdense(0, 0, 0);
    1112      385341 :         if (hi > ci->ncand)
    1113             :                 hi = ci->ncand;
    1114      385341 :         if (hi - lo == 1)
    1115      328618 :                 return BATdense(0, canditer_idx(ci, lo), 1);
    1116       56723 :         switch (ci->tpe) {
    1117        5552 :         case cand_materialized:
    1118        5552 :                 if (ci->s) {
    1119        5552 :                         bn = BATslice(ci->s, lo + ci->offset, hi + ci->offset);
    1120        5552 :                         BAThseqbase(bn, 0);
    1121        5552 :                         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       50881 :         default: /* really: case cand_dense: */
    1130       50881 :                 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      329135 : canditer_sliceval(const struct canditer *ci, oid lo, oid hi)
    1172             : {
    1173      329135 :         if (ci->tpe != cand_mask) {
    1174      985186 :                 return canditer_slice(
    1175             :                         ci,
    1176      328395 :                         is_oid_nil(lo) ? 0 : canditer_search(ci, lo, true),
    1177      328397 :                         is_oid_nil(hi) ? ci->ncand : canditer_search(ci, hi, true));
    1178             :         }
    1179             : 
    1180         738 :         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       32216 : canditer_slice2(const struct canditer *ci, BUN lo1, BUN hi1, BUN lo2, BUN hi2)
    1187             : {
    1188       32216 :         BAT *bn;
    1189       32216 :         oid o;
    1190       32216 :         BUN add;
    1191             : 
    1192       32216 :         assert(lo1 <= hi1);
    1193       32216 :         assert(lo2 <= hi2);
    1194       32216 :         assert(hi1 <= lo2 || (lo2 == 0 && hi2 == 0));
    1195             : 
    1196       32216 :         if (hi1 == lo2)         /* consecutive slices: combine into one */
    1197         597 :                 return canditer_slice(ci, lo1, hi2);
    1198       31619 :         if (lo2 == hi2 || hi1 >= ci->ncand || lo2 >= ci->ncand) {
    1199             :                 /* empty second slice */
    1200       24671 :                 return canditer_slice(ci, lo1, hi1);
    1201             :         }
    1202        6948 :         if (lo1 == hi1)         /* empty first slice */
    1203        1870 :                 return canditer_slice(ci, lo2, hi2);
    1204        5078 :         if (lo1 >= ci->ncand)     /* out of range */
    1205             :                 return BATdense(0, 0, 0);
    1206             : 
    1207        5078 :         if (hi2 >= ci->ncand)
    1208             :                 hi2 = ci->ncand;
    1209             : 
    1210        5078 :         bn = COLnew(0, TYPE_oid, hi1 - lo1 + hi2 - lo2, TRANSIENT);
    1211        5078 :         if (bn == NULL)
    1212             :                 return NULL;
    1213        5078 :         BATsetcount(bn, hi1 - lo1 + hi2 - lo2);
    1214        5078 :         bn->tsorted = true;
    1215        5078 :         bn->trevsorted = BATcount(bn) <= 1;
    1216        5078 :         bn->tkey = true;
    1217        5078 :         bn->tseqbase = oid_nil;
    1218        5078 :         bn->tnil = false;
    1219        5078 :         bn->tnonil = true;
    1220             : 
    1221        5078 :         oid *dst = Tloc(bn, 0);
    1222             : 
    1223        5078 :         switch (ci->tpe) {
    1224        2426 :         case cand_materialized:
    1225        2426 :                 memcpy(dst, ci->oids + lo1, (hi1 - lo1) * sizeof(oid));
    1226        2426 :                 memcpy(dst + hi1 - lo1, ci->oids + lo2, (hi2 - lo2) * sizeof(oid));
    1227        2426 :                 break;
    1228             :         case cand_dense:
    1229      956869 :                 while (lo1 < hi1)
    1230      954218 :                         *dst++ = ci->seq + lo1++;
    1231      289392 :                 while (lo2 < hi2)
    1232      286741 :                         *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        5078 :         return virtualize(bn);
    1277             : }
    1278             : 
    1279             : BAT *
    1280       31666 : canditer_slice2val(const struct canditer *ci, oid lo1, oid hi1, oid lo2, oid hi2)
    1281             : {
    1282       31666 :         if (ci->tpe != cand_mask) {
    1283      124072 :                 return canditer_slice2(
    1284             :                         ci,
    1285       28896 :                         is_oid_nil(lo1) ? 0 : canditer_search(ci, lo1, true),
    1286       31638 :                         is_oid_nil(hi1) ? ci->ncand : canditer_search(ci, hi1, true),
    1287       31638 :                         is_oid_nil(lo2) ? 0 : canditer_search(ci, lo2, true),
    1288         262 :                         is_oid_nil(hi2) ? ci->ncand : canditer_search(ci, hi2, true));
    1289             :         }
    1290             : 
    1291          28 :         return canditer_sliceval_mask(ci, lo1, hi1, ci->ncand,
    1292          28 :                                       lo2, hi2, ci->ncand);
    1293             : }
    1294             : 
    1295             : BAT *
    1296        3161 : BATnegcands2(oid tseq, BUN nr, BAT *odels)
    1297             : {
    1298        3161 :         const char *nme;
    1299        3161 :         Heap *dels;
    1300        3161 :         BUN lo, hi;
    1301        3161 :         ccand_t *c;
    1302        3161 :         BAT *bn;
    1303             : 
    1304        3161 :         bn = BATdense(0, tseq, nr);
    1305        3161 :         if (bn == NULL)
    1306             :                 return NULL;
    1307        3161 :         if (BATcount(odels) == 0)
    1308        2767 :                 goto doreturn;
    1309             : 
    1310         394 :         lo = SORTfndfirst(odels, &bn->tseqbase);
    1311         394 :         hi = SORTfndfirst(odels, &(oid) {bn->tseqbase + BATcount(bn)});
    1312         394 :         if (lo == hi)
    1313             :                 return bn;
    1314         394 :         if (lo + nr == hi) {
    1315         134 :                 BATsetcount(bn, 0);
    1316         134 :                 goto doreturn;
    1317             :         }
    1318             : 
    1319         260 :         nme = BBP_physical(bn->batCacheid);
    1320         260 :         if ((dels = GDKmalloc(sizeof(Heap))) == NULL){
    1321           0 :                 BBPreclaim(bn);
    1322           0 :                 return NULL;
    1323             :         }
    1324         520 :         *dels = (Heap) {
    1325         260 :                 .farmid = BBPselectfarm(bn->batRole, bn->ttype, varheap),
    1326         260 :                 .parentid = bn->batCacheid,
    1327             :                 .dirty = true,
    1328             :                 .refs = ATOMIC_VAR_INIT(1),
    1329             :         };
    1330         260 :         strconcat_len(dels->filename, sizeof(dels->filename),
    1331             :                       nme, ".theap", NULL);
    1332             : 
    1333         520 :         if (dels->farmid < 0 ||
    1334         260 :             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         260 :         c = (ccand_t *) dels->base;
    1340         260 :         *c = (ccand_t) {
    1341             :                 .type = CAND_NEGOID,
    1342             :         };
    1343         260 :         dels->free = sizeof(ccand_t) + sizeof(oid) * (hi - lo);
    1344         260 :         BATiter bi = bat_iterator(odels);
    1345         260 :         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         119 :                 oid *r = (oid *) (dels->base + sizeof(ccand_t));
    1351         119 :                 memcpy(r, (const oid *) bi.base + lo, sizeof(oid) * (hi - lo));
    1352             :         }
    1353         260 :         bat_iterator_end(&bi);
    1354         260 :         assert(bn->tvheap == NULL);
    1355         260 :         bn->tvheap = dels;
    1356         260 :         BATsetcount(bn, bn->batCount - (hi - lo));
    1357        3161 :   doreturn:
    1358        3161 :         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           0 : BATnegcands(BUN nr, BAT *odels)
    1367             : {
    1368           0 :         return BATnegcands2(0, nr, odels);
    1369             : }
    1370             : 
    1371             : BAT *
    1372      103017 : BATmaskedcands(oid hseq, BUN nr, BAT *masked, bool selected)
    1373             : {
    1374      103017 :         const char *nme;
    1375      103017 :         Heap *msks;
    1376      103017 :         ccand_t *c;
    1377      103017 :         BUN nmask;
    1378      103017 :         BAT *bn;
    1379             : 
    1380      103017 :         assert(masked->ttype == TYPE_msk);
    1381             : 
    1382      103017 :         bn = COLnew(hseq, TYPE_void, 0, TRANSIENT);
    1383      103017 :         if (bn == NULL)
    1384             :                 return NULL;
    1385      103017 :         BATtseqbase(bn, hseq);
    1386             : 
    1387      103017 :         if (BATcount(masked) == 0)
    1388             :                 return bn;
    1389             : 
    1390      103017 :         nme = BBP_physical(bn->batCacheid);
    1391      103017 :         if ((msks = GDKmalloc(sizeof(Heap))) == NULL){
    1392           0 :                 BBPreclaim(bn);
    1393           0 :                 return NULL;
    1394             :         }
    1395      206032 :         *msks = (Heap) {
    1396      103016 :                 .farmid = BBPselectfarm(bn->batRole, bn->ttype, varheap),
    1397      103016 :                 .parentid = bn->batCacheid,
    1398             :                 .dirty = true,
    1399             :                 .refs = ATOMIC_VAR_INIT(1),
    1400             :         };
    1401      103016 :         strconcat_len(msks->filename, sizeof(msks->filename),
    1402             :                       nme, ".theap", NULL);
    1403             : 
    1404      103017 :         nmask = (nr + 31) / 32;
    1405      206034 :         if (msks->farmid < 0 ||
    1406      103017 :             HEAPalloc(msks, nmask + (sizeof(ccand_t)/sizeof(uint32_t)), sizeof(uint32_t)) != GDK_SUCCEED) {
    1407           0 :                 GDKfree(msks);
    1408           0 :                 BBPreclaim(bn);
    1409           0 :                 return NULL;
    1410             :         }
    1411      103017 :         c = (ccand_t *) msks->base;
    1412      103017 :         *c = (ccand_t) {
    1413             :                 .type = CAND_MSK,
    1414             : //              .mask = true,
    1415             :         };
    1416      103017 :         msks->free = sizeof(ccand_t) + nmask * sizeof(uint32_t);
    1417      103017 :         uint32_t *r = (uint32_t*)(msks->base + sizeof(ccand_t));
    1418      103017 :         BATiter bi = bat_iterator(masked);
    1419      103016 :         if (selected) {
    1420      103016 :                 if (nr <= bi.count)
    1421      103016 :                         memcpy(r, bi.base, nmask * sizeof(uint32_t));
    1422             :                 else
    1423           0 :                         memcpy(r, bi.base, (bi.count + 31) / 32 * sizeof(uint32_t));
    1424             :         } else {
    1425           0 :                 const uint32_t *s = (const uint32_t *) bi.base;
    1426           0 :                 BUN nmask_ = (bi.count + 31) / 32;
    1427           0 :                 for (BUN i = 0; i < nmask_; i++)
    1428           0 :                         r[i] = ~s[i];
    1429             :         }
    1430      103016 :         if (nr > bi.count) {
    1431           0 :                 BUN rest = bi.count & 31;
    1432           0 :                 BUN nmask_ = (bi.count + 31) / 32;
    1433           0 :                 if (rest > 0)
    1434           0 :                         r[nmask_ -1] |= ((1U << (32 - rest)) - 1) << rest;
    1435           0 :                 for (BUN j = nmask_; j < nmask; j++)
    1436           0 :                         r[j] = ~0;
    1437             :         }
    1438      103016 :         bat_iterator_end(&bi);
    1439             :         /* make sure last word doesn't have any spurious bits set */
    1440      103017 :         BUN cnt = nr % 32;
    1441      103017 :         if (cnt > 0)
    1442      101700 :                 r[nmask - 1] &= (1U << cnt) - 1;
    1443             :         cnt = 0;
    1444    11687207 :         for (BUN i = 0; i < nmask; i++) {
    1445    11584190 :                 if (cnt == 0 && r[i] != 0)
    1446      102730 :                         c->firstbit = candmask_lobit(r[i]) + i * 32;
    1447    11584190 :                 cnt += candmask_pop(r[i]);
    1448             :         }
    1449      103017 :         if (cnt > 0) {
    1450      102736 :                 assert(bn->tvheap == NULL);
    1451      102736 :                 bn->tvheap = msks;
    1452      102736 :                 bn->tseqbase += (oid) c->firstbit;
    1453             :         } else {
    1454             :                 /* no point having a mask if it's empty */
    1455         281 :                 HEAPfree(msks, true);
    1456         281 :                 GDKfree(msks);
    1457             :         }
    1458      103017 :         BATsetcount(bn, cnt);
    1459      103017 :         TRC_DEBUG(ALGO, "hseq=" OIDFMT ", masked=" ALGOBATFMT ", selected=%s"
    1460             :                   " -> " ALGOBATFMT "\n",
    1461             :                   hseq, ALGOBATPAR(masked),
    1462             :                   selected ? "true" : "false",
    1463             :                   ALGOBATPAR(bn));
    1464      103017 :         assert(bn->tseqbase != oid_nil);
    1465             :         return bn;
    1466             : }
    1467             : 
    1468             : /* convert a masked candidate list to a positive or negative candidate list */
    1469             : BAT *
    1470       99834 : BATunmask(BAT *b)
    1471             : {
    1472             : //      assert(!mask_cand(b) || CCAND(b)->mask); /* todo handle negmask case */
    1473       99834 :         BUN cnt;
    1474       99834 :         uint32_t rem;
    1475       99834 :         uint32_t val;
    1476       99834 :         const uint32_t *src;
    1477       99834 :         oid *dst;
    1478       99834 :         BUN n = 0;
    1479       99834 :         oid tseq = b->hseqbase;
    1480       99834 :         bool negcand = false;
    1481             : 
    1482       99834 :         BATiter bi = bat_iterator(b);
    1483       99833 :         if (mask_cand(b)) {
    1484       95389 :                 cnt = ccand_free(b) / sizeof(uint32_t);
    1485       95389 :                 rem = 0;
    1486       95389 :                 src = (const uint32_t *) ccand_first(b);
    1487       95389 :                 tseq = b->tseqbase;
    1488       95389 :                 tseq -= (oid) CCAND(b)->firstbit;
    1489             :                 /* create negative candidate list if more than half the
    1490             :                  * bits are set */
    1491       95389 :                 negcand = BATcount(b) > cnt * 16;
    1492             :         } else {
    1493        4444 :                 cnt = bi.count / 32;
    1494        4444 :                 rem = bi.count % 32;
    1495        4444 :                 src = (const uint32_t *) bi.base;
    1496             :         }
    1497       99833 :         BAT *bn;
    1498             : 
    1499       99833 :         if (negcand) {
    1500       78421 :                 bn = COLnew(b->hseqbase, TYPE_void, 0, TRANSIENT);
    1501       78421 :                 if (bn == NULL) {
    1502           0 :                         bat_iterator_end(&bi);
    1503           0 :                         return NULL;
    1504             :                 }
    1505       78421 :                 Heap *dels;
    1506       78421 :                 if ((dels = GDKmalloc(sizeof(Heap))) == NULL) {
    1507           0 :                         BBPreclaim(bn);
    1508           0 :                         bat_iterator_end(&bi);
    1509           0 :                         return NULL;
    1510             :                 }
    1511      156842 :                 *dels = (Heap) {
    1512       78421 :                         .farmid = BBPselectfarm(TRANSIENT, TYPE_void, varheap),
    1513       78421 :                         .parentid = bn->batCacheid,
    1514             :                         .dirty = true,
    1515             :                         .refs = ATOMIC_VAR_INIT(1),
    1516             :                 };
    1517       78421 :                 strconcat_len(dels->filename, sizeof(dels->filename),
    1518       78421 :                               BBP_physical(bn->batCacheid), ".theap", NULL);
    1519             : 
    1520      156843 :                 if (dels->farmid < 0 ||
    1521       78421 :                     HEAPalloc(dels, cnt * 32 - bi.count
    1522             :                               + sizeof(ccand_t) / sizeof(oid), sizeof(oid)) != GDK_SUCCEED) {
    1523           0 :                         GDKfree(dels);
    1524           0 :                         BBPreclaim(bn);
    1525           0 :                         bat_iterator_end(&bi);
    1526           0 :                         return NULL;
    1527             :                 }
    1528       78422 :                 * (ccand_t *) dels->base = (ccand_t) {
    1529             :                         .type = CAND_NEGOID,
    1530             :                 };
    1531       78422 :                 dst = (oid *) (dels->base + sizeof(ccand_t));
    1532     7276726 :                 for (BUN p = 0, v = 0; p < cnt; p++, v += 32) {
    1533     7198304 :                         if ((val = src[p]) == ~UINT32_C(0))
    1534     5568117 :                                 continue;
    1535    52448468 :                         for (uint32_t i = 0; i < 32; i++) {
    1536    50896276 :                                 if ((val & (1U << i)) == 0) {
    1537    45027077 :                                         if (v + i >= b->batCount + n)
    1538             :                                                 break;
    1539    44949082 :                                         dst[n++] = tseq + v + i;
    1540             :                                 }
    1541             :                         }
    1542             :                 }
    1543       78422 :                 if (n == 0) {
    1544             :                         /* didn't need it after all */
    1545          10 :                         HEAPfree(dels, true);
    1546          10 :                         GDKfree(dels);
    1547             :                 } else {
    1548       78412 :                         dels->free = sizeof(ccand_t) + n * sizeof(oid);
    1549       78412 :                         dels->dirty = true;
    1550       78412 :                         assert(bn->tvheap == NULL);
    1551       78412 :                         bn->tvheap = dels;
    1552             :                 }
    1553       78422 :                 BATsetcount(bn, n=bi.count);
    1554       78422 :                 bn->tseqbase = tseq;
    1555             :         } else {
    1556       21412 :                 bn = COLnew(b->hseqbase, TYPE_oid, mask_cand(b) ? bi.count : 1024, TRANSIENT);
    1557       21412 :                 if (bn == NULL) {
    1558           0 :                         bat_iterator_end(&bi);
    1559           0 :                         return NULL;
    1560             :                 }
    1561       21412 :                 dst = (oid *) Tloc(bn, 0);
    1562     2679979 :                 for (BUN p = 0; p < cnt; p++) {
    1563     2658567 :                         if ((val = src[p]) == 0)
    1564     1888729 :                                 continue;
    1565    25395391 :                         for (uint32_t i = 0; i < 32; i++) {
    1566    24625553 :                                 if (val & (1U << i)) {
    1567    21468841 :                                         if (n == BATcapacity(bn)) {
    1568          78 :                                                 BATsetcount(bn, n);
    1569          78 :                                                 if (BATextend(bn, BATgrows(bn)) != GDK_SUCCEED) {
    1570           0 :                                                         BBPreclaim(bn);
    1571           0 :                                                         bat_iterator_end(&bi);
    1572           0 :                                                         return NULL;
    1573             :                                                 }
    1574          78 :                                                 dst = (oid *) Tloc(bn, 0);
    1575             :                                         }
    1576    21468841 :                                         dst[n++] = tseq + p * 32 + i;
    1577             :                                 }
    1578             :                         }
    1579             :                 }
    1580             :                 /* the last partial mask word */
    1581       21412 :                 if (rem > 0 && (val = src[cnt]) != 0) {
    1582       22274 :                         for (uint32_t i = 0; i < rem; i++) {
    1583       20002 :                                 if (val & (1U << i)) {
    1584        1046 :                                         if (n == BATcapacity(bn)) {
    1585           0 :                                                 BATsetcount(bn, n);
    1586           0 :                                                 if (BATextend(bn, BATgrows(bn)) != GDK_SUCCEED) {
    1587           0 :                                                         BBPreclaim(bn);
    1588           0 :                                                         bat_iterator_end(&bi);
    1589           0 :                                                         return NULL;
    1590             :                                                 }
    1591           0 :                                                 dst = (oid *) Tloc(bn, 0);
    1592             :                                         }
    1593        1046 :                                         dst[n++] = tseq + cnt * 32 + i;
    1594             :                                 }
    1595             :                         }
    1596             :                 }
    1597       21412 :                 BATsetcount(bn, n);
    1598             :         }
    1599       99834 :         bat_iterator_end(&bi);
    1600       99834 :         bn->tkey = true;
    1601       99834 :         bn->tsorted = true;
    1602       99834 :         bn->trevsorted = n <= 1;
    1603       99834 :         bn->tnil = false;
    1604       99834 :         bn->tnonil = true;
    1605       99834 :         bn = virtualize(bn);
    1606       99834 :         TRC_DEBUG(ALGO, ALGOBATFMT " -> " ALGOBATFMT "\n", ALGOBATPAR(b), ALGOBATPAR(bn));
    1607             :         return bn;
    1608             : }

Generated by: LCOV version 1.14