LCOV - code coverage report
Current view: top level - gdk - gdk_cand.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 764 994 76.9 %
Date: 2024-12-20 21:24:02 Functions: 24 25 96.0 %

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

Generated by: LCOV version 1.14