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