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 7982 : BATiscand(BAT *b)
20 : {
21 7982 : if (ATOMtype(b->ttype) != TYPE_oid)
22 : return false;
23 7982 : if (complex_cand(b))
24 : return true;
25 7802 : if (b->ttype == TYPE_void && is_oid_nil(b->tseqbase))
26 : return false;
27 15604 : 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 2665 : newdensecand(oid first, oid last)
34 : {
35 2665 : if (last <= first)
36 0 : first = last = 0; /* empty range */
37 2665 : 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 79326 : BATmergecand(BAT *a, BAT *b)
47 : {
48 79326 : BAT *bn;
49 79326 : oid *restrict p, i;
50 79326 : struct canditer cia, cib;
51 :
52 79326 : BATcheck(a, NULL);
53 79326 : BATcheck(b, NULL);
54 :
55 79326 : canditer_init(&cia, NULL, a);
56 79317 : canditer_init(&cib, NULL, b);
57 :
58 : /* we can return a if b is empty (and v.v.) */
59 79324 : if (cia.ncand == 0) {
60 12509 : bn = canditer_slice(&cib, 0, cib.ncand);
61 12508 : goto doreturn;
62 : }
63 66815 : if (cib.ncand == 0) {
64 10590 : bn = canditer_slice(&cia, 0, cia.ncand);
65 10591 : goto doreturn;
66 : }
67 : /* we can return a if a fully covers b (and v.v) */
68 56225 : if (cia.tpe == cand_dense && cib.tpe == cand_dense) {
69 : /* both are dense */
70 10630 : 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 2292 : bn = newdensecand(cia.seq, cia.seq + cia.ncand < cib.seq + cib.ncand ? cib.seq + cib.ncand : cia.seq + cia.ncand);
74 2294 : goto doreturn;
75 : }
76 8338 : 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 362 : bn = newdensecand(cib.seq, cia.seq + cia.ncand < cib.seq + cib.ncand ? cib.seq + cib.ncand : cia.seq + cia.ncand);
80 362 : goto doreturn;
81 : }
82 : }
83 53571 : if (cia.tpe == cand_dense
84 10857 : && cia.seq <= cib.seq
85 4851 : && canditer_last(&cia) >= canditer_last(&cib)) {
86 342 : bn = canditer_slice(&cia, 0, cia.ncand);
87 342 : goto doreturn;
88 : }
89 53229 : if (cib.tpe == cand_dense
90 37441 : && cib.seq <= cia.seq
91 9953 : && canditer_last(&cib) >= canditer_last(&cia)) {
92 186 : bn = canditer_slice(&cib, 0, cib.ncand);
93 186 : goto doreturn;
94 : }
95 :
96 53043 : bn = COLnew(0, TYPE_oid, cia.ncand + cib.ncand, TRANSIENT);
97 53038 : if (bn == NULL)
98 0 : goto doreturn;
99 53038 : p = (oid *) Tloc(bn, 0);
100 53038 : if (cia.tpe == cand_dense && cib.tpe == cand_dense) {
101 : /* both lists are dense */
102 7977 : if (cia.seq > cib.seq) {
103 4013 : struct canditer ci;
104 4013 : ci = cia;
105 4013 : cia = cib;
106 4013 : cib = ci;
107 : }
108 : /* cia completely before cib */
109 7977 : assert(cia.seq + cia.ncand < cib.seq);
110 30513 : for (i = cia.seq; i < cia.seq + cia.ncand; i++)
111 22536 : *p++ = i;
112 : /* there is a gap */
113 26973 : for (i = cib.seq; i < cib.seq + cib.ncand; i++)
114 18996 : *p++ = i;
115 45061 : } else if (cia.tpe == cand_dense || cib.tpe == cand_dense) {
116 31812 : if (cib.tpe == cand_dense) {
117 29278 : struct canditer ci;
118 29278 : ci = cia;
119 29278 : cia = cib;
120 29278 : cib = ci;
121 : }
122 : /* only cia is dense */
123 :
124 : /* copy part of cib that's before the start of cia */
125 123830 : while (canditer_peek(&cib) < cia.seq) {
126 92010 : *p++ = canditer_next(&cib);
127 : }
128 : /* copy all of cia */
129 73354 : for (i = cia.seq; i < cia.seq + cia.ncand; i++)
130 41534 : *p++ = i;
131 : /* skip over part of cib that overlaps with cia */
132 31820 : canditer_setidx(&cib, canditer_search(&cib, cia.seq + cia.ncand, true));
133 : /* copy rest of cib */
134 118043 : while (cib.next < cib.ncand) {
135 86223 : *p++ = canditer_next(&cib);
136 : }
137 : } else {
138 : /* a and b are both not dense */
139 13249 : oid ao = canditer_next(&cia);
140 13250 : oid bo = canditer_next(&cib);
141 23937580 : while (!is_oid_nil(ao) && !is_oid_nil(bo)) {
142 23924333 : if (ao < bo) {
143 13932867 : *p++ = ao;
144 13932867 : ao = canditer_next(&cia);
145 9991466 : } else if (bo < ao) {
146 8929828 : *p++ = bo;
147 8929828 : bo = canditer_next(&cib);
148 : } else {
149 1061638 : *p++ = ao;
150 1061638 : ao = canditer_next(&cia);
151 1061600 : bo = canditer_next(&cib);
152 : }
153 : }
154 572088 : while (!is_oid_nil(ao)) {
155 558844 : *p++ = ao;
156 558844 : ao = canditer_next(&cia);
157 : }
158 418605 : while (!is_oid_nil(bo)) {
159 405361 : *p++ = bo;
160 405361 : bo = canditer_next(&cib);
161 : }
162 : }
163 :
164 : /* properties */
165 53040 : BATsetcount(bn, (BUN) (p - (oid *) Tloc(bn, 0)));
166 53037 : bn->trevsorted = BATcount(bn) <= 1;
167 53037 : bn->tsorted = true;
168 53037 : bn->tkey = true;
169 53037 : bn->tnil = false;
170 53037 : bn->tnonil = true;
171 53037 : bn = virtualize(bn);
172 79328 : doreturn:
173 79328 : TRC_DEBUG(ALGO, ALGOBATFMT "," ALGOBATFMT " -> " ALGOOPTBATFMT "\n",
174 : ALGOBATPAR(a), ALGOBATPAR(b), ALGOOPTBATPAR(bn));
175 : return bn;
176 : }
177 :
178 : /* intersect two candidate lists and produce a new one
179 : *
180 : * candidate lists are VOID-headed BATs with an OID tail which is
181 : * sorted and unique.
182 : */
183 : BAT *
184 34 : BATintersectcand(BAT *a, BAT *b)
185 : {
186 34 : BAT *bn;
187 34 : oid *restrict p;
188 34 : struct canditer cia, cib;
189 :
190 34 : BATcheck(a, NULL);
191 34 : BATcheck(b, NULL);
192 :
193 34 : canditer_init(&cia, NULL, a);
194 34 : canditer_init(&cib, NULL, b);
195 :
196 34 : if (cia.ncand == 0 || cib.ncand == 0) {
197 7 : bn = BATdense(0, 0, 0);
198 7 : goto doreturn;
199 : }
200 :
201 27 : if (cia.tpe == cand_dense && cib.tpe == cand_dense) {
202 : /* both lists are dense */
203 9 : bn = newdensecand(MAX(cia.seq, cib.seq), MIN(cia.seq + cia.ncand, cib.seq + cib.ncand));
204 9 : goto doreturn;
205 : }
206 :
207 18 : bn = COLnew(0, TYPE_oid, MIN(cia.ncand, cib.ncand), TRANSIENT);
208 18 : if (bn == NULL)
209 0 : goto doreturn;
210 18 : p = (oid *) Tloc(bn, 0);
211 18 : if (cia.tpe == cand_dense || cib.tpe == cand_dense) {
212 18 : if (cib.tpe == cand_dense) {
213 18 : struct canditer ci;
214 18 : ci = cia;
215 18 : cia = cib;
216 18 : cib = ci;
217 : }
218 : /* only cia is dense */
219 :
220 : /* search for first value in cib that is contained in cia */
221 18 : canditer_setidx(&cib, canditer_search(&cib, cia.seq, true));
222 18 : oid bo;
223 62 : while (!is_oid_nil(bo = canditer_next(&cib)) && bo < cia.seq + cia.ncand)
224 44 : *p++ = bo;
225 : } else {
226 : /* a and b are both not dense */
227 0 : oid ao = canditer_next(&cia);
228 0 : oid bo = canditer_next(&cib);
229 0 : while (!is_oid_nil(ao) && !is_oid_nil(bo)) {
230 0 : if (ao < bo)
231 0 : ao = canditer_next(&cia);
232 0 : else if (bo < ao)
233 0 : bo = canditer_next(&cib);
234 : else {
235 0 : *p++ = ao;
236 0 : ao = canditer_next(&cia);
237 0 : bo = canditer_next(&cib);
238 : }
239 : }
240 : }
241 :
242 : /* properties */
243 18 : BATsetcount(bn, (BUN) (p - (oid *) Tloc(bn, 0)));
244 18 : bn->trevsorted = BATcount(bn) <= 1;
245 18 : bn->tsorted = true;
246 18 : bn->tkey = true;
247 18 : bn->tnil = false;
248 18 : bn->tnonil = true;
249 18 : bn = virtualize(bn);
250 34 : doreturn:
251 34 : TRC_DEBUG(ALGO, ALGOBATFMT "," ALGOBATFMT " -> " ALGOOPTBATFMT "\n",
252 : ALGOBATPAR(a), ALGOBATPAR(b), ALGOOPTBATPAR(bn));
253 : return bn;
254 : }
255 :
256 : /* calculate the difference of two candidate lists and produce a new one
257 : */
258 : BAT *
259 5319 : BATdiffcand(BAT *a, BAT *b)
260 : {
261 5319 : BAT *bn;
262 5319 : oid *restrict p;
263 5319 : struct canditer cia, cib;
264 :
265 5319 : BATcheck(a, NULL);
266 5319 : BATcheck(b, NULL);
267 :
268 5319 : canditer_init(&cia, NULL, a);
269 5319 : canditer_init(&cib, NULL, b);
270 :
271 5319 : if (cia.ncand == 0) {
272 0 : bn = BATdense(0, 0, 0);
273 0 : goto doreturn;
274 : }
275 5319 : if (cib.ncand == 0) {
276 451 : bn = canditer_slice(&cia, 0, cia.ncand);
277 451 : goto doreturn;
278 : }
279 :
280 4868 : 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 4868 : if (cia.tpe == cand_dense && cib.tpe == cand_dense) {
287 : /* both a and b are dense */
288 1791 : if (cia.seq < cib.seq) {
289 : /* a starts before b */
290 665 : if (cia.seq + cia.ncand <= cib.seq + cib.ncand) {
291 : /* b overlaps with end of a */
292 87 : bn = canditer_slice(&cia, 0, cib.seq - cia.seq);
293 87 : 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 1126 : if (cia.seq + cia.ncand > cib.seq + cib.ncand) {
303 : /* b overlaps with beginning of a */
304 48 : bn = canditer_slice(&cia, cib.seq + cib.ncand - cia.seq, cia.ncand);
305 48 : goto doreturn;
306 : }
307 : /* a is subset f b */
308 1078 : bn = BATdense(0, 0, 0);
309 1078 : goto doreturn;
310 : }
311 : }
312 3077 : 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 2480 : bn = canditer_slice2val(&cia, oid_nil, cib.seq,
317 : cib.seq + cib.ncand, oid_nil);
318 2480 : goto doreturn;
319 : }
320 :
321 : /* b is not dense */
322 597 : bn = COLnew(0, TYPE_oid, BATcount(a), TRANSIENT);
323 597 : if (bn == NULL)
324 0 : goto doreturn;
325 597 : p = Tloc(bn, 0);
326 : /* find first position in b that is in range of a */
327 597 : 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 597 : oid ob = canditer_next(&cib);
332 7558670 : for (BUN i = 0; i < cia.ncand; i++) {
333 7558073 : oid oa = canditer_next(&cia);
334 10073195 : while (!is_oid_nil(ob) && ob < oa) {
335 2516451 : ob = canditer_next(&cib);
336 : }
337 7558073 : if (is_oid_nil(ob) || oa < ob)
338 5045576 : *p++ = oa;
339 : }
340 : }
341 :
342 : /* properties */
343 597 : BATsetcount(bn, (BUN) (p - (oid *) Tloc(bn, 0)));
344 597 : bn->trevsorted = BATcount(bn) <= 1;
345 597 : bn->tsorted = true;
346 597 : bn->tkey = true;
347 597 : bn->tnil = false;
348 597 : bn->tnonil = true;
349 597 : bn = virtualize(bn);
350 5319 : doreturn:
351 5319 : 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 870914 : binsearchcand(const oid *cand, BUN hi, oid o)
359 : {
360 870914 : BUN lo = 0;
361 :
362 870914 : if (o <= cand[lo])
363 : return 0;
364 428157 : if (o > cand[hi])
365 389695 : return hi + 1;
366 : /* loop invariant: cand[lo] < o <= cand[hi] */
367 178464 : while (hi > lo + 1) {
368 157554 : BUN mid = (lo + hi) / 2;
369 157554 : if (cand[mid] == o)
370 17552 : return mid;
371 140002 : if (cand[mid] < o)
372 : lo = mid;
373 : else
374 100876 : 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 1646 : count_mask_bits(const struct canditer *ci, BUN lo, BUN hi)
383 : {
384 1646 : BUN n;
385 1646 : assert(lo <= hi);
386 1646 : assert(ci->tpe == cand_mask);
387 1646 : if (lo == hi)
388 : return 0;
389 1646 : lo += ci->firstbit;
390 1646 : hi += ci->firstbit;
391 1646 : BUN loi = lo / 32;
392 1646 : BUN hii = hi / 32;
393 1646 : lo %= 32;
394 1646 : hi %= 32;
395 1646 : if (loi == hii)
396 151 : return (BUN) candmask_pop((ci->mask[loi] & ((1U << hi) - 1)) >> lo);
397 1495 : n = (BUN) candmask_pop(ci->mask[loi++] >> lo);
398 1975370 : while (loi < hii)
399 1973875 : n += (BUN) candmask_pop(ci->mask[loi++]);
400 1495 : if (hi != 0)
401 189 : n += (BUN) candmask_pop(ci->mask[loi] & ((1U << hi) - 1));
402 : return n;
403 : }
404 :
405 : /* initialize a candidate iterator */
406 : void
407 7759834 : canditer_init(struct canditer *ci, BAT *b, BAT *s)
408 : {
409 7759834 : assert(ci != NULL);
410 7759834 : BUN batcount = 0;
411 7759834 : oid hseq = 0;
412 7759834 : if (b) {
413 7145287 : MT_lock_set(&b->theaplock);
414 7143402 : batcount = BATcount(b);
415 7143402 : hseq = b->hseqbase;
416 7143402 : MT_lock_unset(&b->theaplock);
417 : }
418 :
419 7759142 : if (s == NULL) {
420 3709902 : 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 3709902 : *ci = (struct canditer) {
429 : .tpe = cand_dense,
430 : .seq = hseq,
431 : .hseq = hseq,
432 : .ncand = batcount,
433 : };
434 3709902 : return;
435 : }
436 :
437 4049240 : assert(ATOMtype(s->ttype) == TYPE_oid || s->ttype == TYPE_msk);
438 4049240 : assert(s->ttype == TYPE_msk|| s->tsorted);
439 4049240 : assert(s->ttype == TYPE_msk|| s->tkey);
440 4049240 : assert(s->ttype == TYPE_msk|| s->tnonil);
441 4049240 : assert(s->ttype == TYPE_void || s->tvheap == NULL);
442 :
443 4049240 : BUN cnt = BATcount(s);
444 :
445 4049240 : if (cnt == 0 || (b != NULL && batcount == 0)) {
446 : /* candidate list for empty BAT or empty candidate list */
447 962376 : *ci = (struct canditer) {
448 : .tpe = cand_dense,
449 962376 : .hseq = s->hseqbase,
450 : .s = s,
451 : };
452 962376 : return;
453 : }
454 :
455 3086864 : *ci = (struct canditer) {
456 3086864 : .seq = s->tseqbase,
457 3086864 : .hseq = s->hseqbase,
458 : .s = s,
459 : };
460 :
461 3086864 : if (mask_cand(s)) {
462 1646 : ci->tpe = cand_mask;
463 1646 : ci->mask = (const uint32_t *) ccand_first(s);
464 1646 : ci->seq = s->tseqbase - (oid) CCAND(s)->firstbit;
465 1646 : ci->hseq = s->hseqbase;
466 1646 : ci->nvals = ccand_free(s) / sizeof(uint32_t);
467 1646 : cnt = ci->nvals * 32;
468 3085218 : } 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 3085218 : } else if (s->ttype == TYPE_void) {
475 2563683 : assert(!is_oid_nil(ci->seq));
476 2563683 : if (s->tvheap) {
477 60498 : assert(ccand_free(s) % SIZEOF_OID == 0);
478 60498 : ci->nvals = ccand_free(s) / SIZEOF_OID;
479 60498 : if (ci->nvals > 0) {
480 60498 : ci->tpe = cand_except;
481 60498 : 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 521535 : } else if (is_oid_nil(ci->seq)) {
490 487312 : ci->tpe = cand_materialized;
491 487312 : ci->oids = (const oid *) Tloc(s, 0);
492 487312 : ci->seq = ci->oids[0];
493 487312 : ci->nvals = cnt;
494 : } else {
495 : /* materialized dense: no exceptions */
496 : ci->tpe = cand_dense;
497 : }
498 3086864 : switch (ci->tpe) {
499 487305 : case cand_materialized:
500 487305 : if (b != NULL) {
501 375965 : BUN p = binsearchcand(ci->oids, cnt - 1U, hseq);
502 : /* p == cnt means candidate list is completely
503 : * before b */
504 375966 : ci->offset = p;
505 375966 : ci->oids += p;
506 375966 : cnt -= p;
507 375966 : if (cnt > 0) {
508 375966 : cnt = binsearchcand(ci->oids, cnt - 1U,
509 : hseq + batcount);
510 : /* cnt == 0 means candidate list is
511 : * completely after b */
512 : }
513 375965 : if (cnt == 0) {
514 : /* no overlap */
515 0 : *ci = (struct canditer) {
516 : .tpe = cand_dense,
517 : .s = s,
518 : };
519 0 : return;
520 : }
521 375965 : ci->seq = ci->oids[0];
522 375965 : ci->nvals = cnt;
523 375965 : 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 60498 : case cand_except:
532 : /* exceptions must all be within range of s */
533 60498 : assert(ci->oids[0] >= ci->seq);
534 60498 : assert(ci->oids[ci->nvals - 1U] < ci->seq + cnt + ci->nvals);
535 : /* prune exceptions at either end of range of s */
536 91491 : while (ci->nvals > 0 && ci->oids[0] == ci->seq) {
537 30993 : ci->nvals--;
538 30993 : ci->oids++;
539 30993 : ci->seq++;
540 : }
541 60696 : while (ci->nvals > 0 &&
542 59755 : ci->oids[ci->nvals - 1U] == ci->seq + cnt + ci->nvals - 1U)
543 198 : ci->nvals--;
544 60498 : if (b != NULL) {
545 60000 : if (ci->seq + cnt + ci->nvals <= hseq ||
546 60000 : 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 60498 : if (ci->nvals > 0) {
556 59557 : if (b == NULL)
557 : break;
558 59180 : BUN p;
559 59180 : p = binsearchcand(ci->oids, ci->nvals - 1U, hseq);
560 59180 : 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 59180 : assert(hseq > ci->seq || p == 0);
571 59180 : 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 59180 : 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 59180 : while (ci->nvals > 0 && ci->oids[0] == ci->seq) {
588 0 : ci->nvals--;
589 0 : ci->oids++;
590 0 : ci->seq++;
591 : }
592 59180 : while (ci->nvals > 0 &&
593 59180 : ci->oids[ci->nvals - 1U] == ci->seq + cnt + ci->nvals - 1U)
594 0 : ci->nvals--;
595 59180 : if (ci->nvals > 0)
596 : break;
597 : }
598 941 : ci->tpe = cand_dense;
599 941 : ci->oids = NULL;
600 : /* fall through */
601 2538357 : case cand_dense:
602 2538357 : if (b != NULL) {
603 2217594 : if (ci->seq + cnt <= hseq ||
604 2217594 : 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 2217594 : if (hseq > ci->seq) {
613 0 : cnt -= hseq - ci->seq;
614 0 : ci->offset += hseq - ci->seq;
615 0 : ci->seq = hseq;
616 : }
617 2217594 : if (ci->seq + cnt > hseq + batcount)
618 0 : cnt = hseq + batcount - ci->seq;
619 : }
620 : break;
621 1645 : case cand_mask:
622 1645 : assert(s->tseqbase != oid_nil);
623 1645 : if (b != NULL) {
624 344 : if (ci->seq + cnt <= hseq ||
625 344 : 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 344 : 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 344 : if (ci->seq + cnt > hseq + batcount) {
641 340 : cnt = hseq + batcount - ci->seq;
642 : }
643 344 : 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 1645 : 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 1645 : if (ci->firstbit == 0) {
660 1838 : while (cnt >= 32U && ci->mask[0] == 0) {
661 192 : cnt -= 32U;
662 192 : ci->mask++;
663 192 : ci->nvals--;
664 : }
665 : }
666 : /* check whether there are any 1 bits in the last word */
667 1645 : if (cnt == 0 ||
668 1645 : (cnt < 32U - ci->firstbit &&
669 151 : ((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 1645 : int i = candmask_lobit(ci->mask[0] >> ci->firstbit);
681 1645 : assert(i >= 0); /* there should be a set bit */
682 1645 : ci->firstbit += i;
683 1645 : cnt -= i;
684 1645 : if (mask_cand(s))
685 1646 : 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 1645 : ci->seq = ci->mskoff + ci->firstbit;
689 1645 : ci->nextbit = ci->firstbit;
690 : /* at this point we know that bit ci->firstbit is set
691 : * in ci->mask[0] */
692 1645 : ci->lastbit = (ci->firstbit + cnt - 1U) % 32U + 1U;
693 1645 : if (ci->lastbit < 32 &&
694 340 : (ci->mask[ci->nvals - 1] & ((1U << ci->lastbit) - 1)) == 0) {
695 : /* last partial word is all zero */
696 0 : cnt -= ci->lastbit;
697 0 : ci->lastbit = 32;
698 0 : ci->nvals--;
699 : }
700 1645 : if (ci->lastbit == 32) {
701 : /* "remove" zero words at the end */
702 1306 : while (cnt >= 32 && ci->mask[ci->nvals - 1] == 0) {
703 0 : ci->nvals--;
704 0 : cnt -= 32;
705 : }
706 : }
707 1645 : ci->ncand = count_mask_bits(ci, 0, cnt);
708 1646 : return;
709 : default:
710 0 : MT_UNREACHABLE();
711 : }
712 3085219 : ci->ncand = cnt;
713 3085219 : ci->hseq += ci->offset;
714 : }
715 :
716 : /* return the next candidate without advancing */
717 : oid
718 1761408 : canditer_peek(const struct canditer *ci)
719 : {
720 1761408 : oid o = oid_nil;
721 1761408 : if (ci->next == ci->ncand)
722 6056 : return oid_nil;
723 1755352 : switch (ci->tpe) {
724 1505992 : case cand_dense:
725 1505992 : o = ci->seq + ci->next;
726 1505992 : break;
727 245381 : case cand_materialized:
728 245381 : assert(ci->next < ci->nvals);
729 245381 : o = ci->oids[ci->next];
730 245381 : break;
731 3979 : case cand_except:
732 3979 : o = ci->seq + ci->add + ci->next;
733 3989 : for (oid a = ci->add; a < ci->nvals && o == ci->oids[a]; a++)
734 10 : o++;
735 : break;
736 0 : case cand_mask: {
737 0 : BUN m = ci->nextmsk;
738 0 : uint8_t b = ci->nextbit;
739 0 : while ((ci->mask[m] >> b) == 0) {
740 0 : m++;
741 0 : b = 0;
742 : }
743 0 : b += candmask_lobit(ci->mask[m] >> b);
744 0 : o = ci->mskoff + m * 32 + b;
745 0 : break;
746 : }
747 : default:
748 0 : MT_UNREACHABLE();
749 : }
750 : return o;
751 : }
752 :
753 : /* return the previous candidate */
754 : oid
755 850 : canditer_prev(struct canditer *ci)
756 : {
757 850 : if (ci->next == 0)
758 0 : return oid_nil;
759 850 : switch (ci->tpe) {
760 850 : case cand_dense:
761 850 : return ci->seq + --ci->next;
762 0 : case cand_materialized:
763 0 : return ci->oids[--ci->next];
764 : case cand_except:
765 0 : break;
766 0 : case cand_mask:
767 0 : for (;;) {
768 0 : if (ci->nextbit == 0) {
769 0 : ci->nextbit = 32;
770 0 : while (ci->mask[--ci->nextmsk] == 0)
771 : ;
772 : }
773 0 : if (ci->mask[ci->nextmsk] & (1U << --ci->nextbit))
774 : break;
775 : }
776 0 : ci->next--;
777 0 : return ci->mskoff + ci->nextmsk * 32 + ci->nextbit;
778 : default:
779 0 : MT_UNREACHABLE();
780 : }
781 0 : oid o = ci->seq + ci->add + --ci->next;
782 0 : while (ci->add > 0 && o == ci->oids[ci->add - 1]) {
783 0 : ci->add--;
784 0 : o--;
785 : }
786 : return o;
787 : }
788 :
789 : /* return the previous candidate without retreating */
790 : oid
791 2808 : canditer_peekprev(const struct canditer *ci)
792 : {
793 2808 : oid o = oid_nil;
794 :
795 2808 : if (ci->next == 0)
796 0 : return oid_nil;
797 2808 : switch (ci->tpe) {
798 2808 : case cand_dense:
799 2808 : return ci->seq + ci->next - 1;
800 0 : case cand_materialized:
801 0 : return ci->oids[ci->next - 1];
802 0 : case cand_except:
803 0 : o = ci->seq + ci->add + ci->next - 1;
804 0 : for (oid a = ci->add; a > 0 && o == ci->oids[a - 1]; a--)
805 0 : o--;
806 : break;
807 0 : case cand_mask: {
808 0 : BUN m = ci->nextmsk;
809 0 : uint8_t b = ci->nextbit;
810 0 : do {
811 0 : if (b == 0) {
812 0 : b = 32;
813 0 : while (ci->mask[--m] == 0)
814 : ;
815 : }
816 0 : } while ((ci->mask[m] & (1U << --b)) == 0);
817 0 : o = ci->mskoff + m * 32 + b;
818 0 : if (++b == 32) {
819 : b = 0;
820 : m++;
821 : }
822 : break;
823 : }
824 : default:
825 0 : MT_UNREACHABLE();
826 : }
827 : return o;
828 : }
829 :
830 : /* if o is a candidate, return it, else return the next candidate from
831 : * the cand_mask iterator ci after (if next is set) or before (if next
832 : * is unset) o; if there are no more candidates, return oid_nil */
833 : oid
834 0 : canditer_mask_next(const struct canditer *ci, oid o, bool next)
835 : {
836 0 : if (o < ci->mskoff)
837 0 : return next ? ci->mskoff + ci->firstbit : oid_nil;
838 0 : o -= ci->mskoff;
839 0 : BUN p = o / 32;
840 0 : o %= 32;
841 0 : if (p >= ci->nvals || (p == ci->nvals - 1 && o >= ci->lastbit))
842 0 : return next ? oid_nil : canditer_last(ci);
843 0 : if (next) {
844 0 : while ((ci->mask[p] & (1U << o)) == 0) {
845 0 : if (++o == 32) {
846 0 : o = 0;
847 0 : if (++p == ci->nvals)
848 0 : return oid_nil;
849 : }
850 : }
851 0 : if (p == ci->nvals - 1 && o >= ci->lastbit)
852 0 : return oid_nil;
853 : } else {
854 0 : while ((ci->mask[p] & (1U << o)) == 0) {
855 0 : if (o == 0) {
856 0 : o = 31;
857 0 : if (p == 0)
858 0 : return oid_nil;
859 0 : p--;
860 : } else {
861 0 : o--;
862 : }
863 : }
864 0 : if (p == 0 && o < ci->firstbit)
865 0 : return oid_nil;
866 : }
867 0 : return ci->mskoff + 32 * p + o;
868 : }
869 :
870 : /* return the last candidate */
871 : oid
872 233848 : canditer_last(const struct canditer *ci)
873 : {
874 233848 : if (ci->ncand == 0)
875 0 : return oid_nil;
876 233848 : switch (ci->tpe) {
877 213451 : case cand_dense:
878 213451 : return ci->seq + ci->ncand - 1;
879 18457 : case cand_materialized:
880 18457 : return ci->oids[ci->ncand - 1];
881 1939 : case cand_except:
882 1939 : return ci->seq + ci->ncand + ci->nvals - 1;
883 1 : case cand_mask:
884 1 : for (uint8_t i = ci->lastbit; i > 0; ) {
885 1 : if (ci->mask[ci->nvals - 1] & (1U << --i))
886 1 : return ci->mskoff + (ci->nvals - 1) * 32 + i;
887 : }
888 0 : break; /* cannot happen */
889 : default:
890 0 : MT_UNREACHABLE();
891 : }
892 0 : return oid_nil; /* cannot happen */
893 : }
894 :
895 : /* return the candidate at the given index */
896 : oid
897 337561137 : canditer_idx(const struct canditer *ci, BUN p)
898 : {
899 337561137 : if (p >= ci->ncand)
900 0 : return oid_nil;
901 337561137 : switch (ci->tpe) {
902 171750130 : case cand_dense:
903 171750130 : return ci->seq + p;
904 165778203 : case cand_materialized:
905 165778203 : return ci->oids[p];
906 32726 : case cand_except: {
907 32726 : oid o = ci->seq + p;
908 32726 : if (o < ci->oids[0])
909 : return o;
910 17360 : if (o + ci->nvals > ci->oids[ci->nvals - 1])
911 : return o + ci->nvals;
912 12929 : BUN lo = 0, hi = ci->nvals - 1;
913 104602 : while (hi - lo > 1) {
914 78744 : BUN mid = (hi + lo) / 2;
915 78744 : if (ci->oids[mid] - mid > o)
916 : hi = mid;
917 : else
918 44370 : lo = mid;
919 : }
920 12929 : return o + hi;
921 : }
922 78 : case cand_mask: {
923 78 : BUN x;
924 78 : if ((x = candmask_pop(ci->mask[0] >> ci->firstbit)) > p) {
925 26 : for (uint8_t i = ci->firstbit; ; i++) {
926 104 : if (ci->mask[0] & (1U << i)) {
927 104 : if (p == 0)
928 78 : return ci->mskoff + i;
929 26 : p--;
930 : }
931 : }
932 : }
933 0 : for (BUN n = 1; n < ci->nvals; n++) {
934 0 : uint32_t mask = ci->mask[n];
935 0 : p -= x;
936 0 : x = candmask_pop(mask);
937 0 : if (x > p) {
938 0 : for (uint8_t i = 0; ; i++) {
939 0 : if (mask & (1U << i)) {
940 0 : if (p == 0)
941 0 : return ci->mskoff + n * 32 + i;
942 0 : p--;
943 : }
944 : }
945 : }
946 : }
947 0 : break; /* cannot happen */
948 : }
949 : default:
950 0 : MT_UNREACHABLE();
951 : }
952 0 : return oid_nil; /* cannot happen */
953 : }
954 :
955 : /* set the index for the next candidate to be returned */
956 : void
957 424868 : canditer_setidx(struct canditer *ci, BUN p)
958 : {
959 424868 : if (p != ci->next) {
960 394802 : if (p >= ci->ncand) {
961 47526 : ci->next = ci->ncand;
962 47526 : switch (ci->tpe) {
963 0 : case cand_except:
964 0 : ci->add = ci->nvals;
965 0 : break;
966 0 : case cand_mask:
967 0 : ci->nextbit = ci->lastbit;
968 0 : ci->nextmsk = ci->nvals;
969 0 : if (ci->nextbit == 32)
970 0 : ci->nextbit = 0;
971 : else
972 0 : ci->nextmsk--;
973 : break;
974 : default:
975 : break;
976 : }
977 : } else {
978 347276 : ci->next = p;
979 347276 : switch (ci->tpe) {
980 2318 : case cand_except:
981 2318 : ci->add = canditer_idx(ci, p) - ci->seq - p;
982 2318 : break;
983 0 : case cand_mask: {
984 0 : oid o = canditer_idx(ci, p) - ci->mskoff;
985 0 : ci->nextmsk = o / 32;
986 0 : ci->nextbit = (uint8_t) (o % 32);
987 0 : break;
988 : }
989 : default:
990 : break;
991 : }
992 : }
993 : }
994 424868 : }
995 :
996 : /* reset */
997 : void
998 549373 : canditer_reset(struct canditer *ci)
999 : {
1000 549373 : if (ci->tpe == cand_mask) {
1001 0 : ci->nextbit = ci->firstbit;
1002 0 : ci->nextmsk = 0;
1003 : } else {
1004 549373 : ci->add = 0;
1005 : }
1006 549373 : ci->next = 0;
1007 549373 : }
1008 :
1009 : /* return index of given candidate if it occurs; if the candidate does
1010 : * not occur, if next is set, return index of next larger candidate,
1011 : * if next is not set, return BUN_NONE */
1012 : BUN
1013 963664 : canditer_search(const struct canditer *ci, oid o, bool next)
1014 : {
1015 963664 : BUN p;
1016 :
1017 963664 : switch (ci->tpe) {
1018 902932 : case cand_dense:
1019 902932 : if (o < ci->seq)
1020 550 : return next ? 0 : BUN_NONE;
1021 902382 : if (o >= ci->seq + ci->ncand)
1022 116514 : return next ? ci->ncand : BUN_NONE;
1023 785868 : return o - ci->seq;
1024 49579 : case cand_materialized:
1025 49579 : if (ci->nvals == 0)
1026 : return 0;
1027 49579 : p = binsearchcand(ci->oids, ci->nvals - 1, o);
1028 49576 : if (next || (p != ci->nvals && ci->oids[p] == o))
1029 49331 : return p;
1030 : break;
1031 11153 : case cand_except:
1032 11153 : if (o < ci->seq)
1033 0 : return next ? 0 : BUN_NONE;
1034 11153 : if (o >= ci->seq + ci->ncand + ci->nvals)
1035 927 : return next ? ci->ncand : BUN_NONE;
1036 10226 : p = binsearchcand(ci->oids, ci->nvals - 1, o);
1037 10226 : if (next || p == ci->nvals || ci->oids[p] != o)
1038 10071 : return o - ci->seq - p;
1039 : break;
1040 0 : case cand_mask:
1041 0 : if (o < ci->mskoff) {
1042 0 : return next ? 0 : BUN_NONE;
1043 : }
1044 0 : o -= ci->mskoff;
1045 0 : p = o / 32;
1046 0 : if (p >= ci->nvals)
1047 0 : return next ? ci->ncand : BUN_NONE;
1048 0 : o %= 32;
1049 0 : if (p == ci->nvals - 1 && o >= ci->lastbit)
1050 0 : return next ? ci->ncand : BUN_NONE;
1051 0 : if (next || ci->mask[p] & (1U << o))
1052 0 : return count_mask_bits(ci, 0, p * 32 + o) + !(ci->mask[p] & (1U << o));
1053 : break;
1054 : default:
1055 0 : MT_UNREACHABLE();
1056 : }
1057 : return BUN_NONE;
1058 : }
1059 :
1060 : static BAT *
1061 156 : canditer_sliceval_mask(const struct canditer *ci, oid lo1, oid hi1, BUN cnt1,
1062 : oid lo2, oid hi2, BUN cnt2)
1063 : {
1064 156 : assert(cnt2 == 0 || !is_oid_nil(lo2));
1065 156 : assert(cnt2 == 0 || lo2 >= hi1);
1066 156 : if (is_oid_nil(lo1) || lo1 < ci->mskoff + ci->firstbit)
1067 68 : lo1 = ci->mskoff + ci->firstbit;
1068 156 : if (is_oid_nil(hi1) || hi1 >= ci->mskoff + (ci->nvals - 1) * 32 + ci->lastbit)
1069 143 : hi1 = ci->mskoff + (ci->nvals - 1) * 32 + ci->lastbit;
1070 :
1071 156 : BAT *bn = COLnew(0, TYPE_oid, cnt1 + cnt2, TRANSIENT);
1072 156 : if (bn == NULL)
1073 : return NULL;
1074 156 : bn->tkey = true;
1075 :
1076 156 : if (hi1 > ci->mskoff) {
1077 156 : if (lo1 < ci->mskoff)
1078 : lo1 = 0;
1079 : else
1080 156 : lo1 -= ci->mskoff;
1081 156 : hi1 -= ci->mskoff;
1082 896 : for (oid o = lo1; o < hi1 && cnt1 > 0; o++) {
1083 740 : if (ci->mask[o / 32] & (1U << (o % 32))) {
1084 300 : if (BUNappend(bn, &(oid){o + ci->mskoff}, false) != GDK_SUCCEED) {
1085 0 : BBPreclaim(bn);
1086 0 : return NULL;
1087 : }
1088 300 : cnt1--;
1089 : }
1090 : }
1091 : }
1092 156 : if (cnt2 > 0) {
1093 2 : if (lo2 < ci->mskoff + ci->firstbit)
1094 : lo2 = ci->mskoff + ci->firstbit;
1095 2 : if (is_oid_nil(hi2) || hi2 >= ci->mskoff + (ci->nvals - 1) * 32 + ci->lastbit)
1096 2 : hi2 = ci->mskoff + (ci->nvals - 1) * 32 + ci->lastbit;
1097 :
1098 2 : lo2 -= ci->mskoff;
1099 2 : hi2 -= ci->mskoff;
1100 8 : for (oid o = lo2; o < hi2 && cnt2 > 0; o++) {
1101 6 : if (ci->mask[o / 32] & (1U << (o % 32))) {
1102 4 : if (BUNappend(bn, &(oid){o + ci->mskoff}, false) != GDK_SUCCEED) {
1103 0 : BBPreclaim(bn);
1104 0 : return NULL;
1105 : }
1106 4 : cnt2--;
1107 : }
1108 : }
1109 : }
1110 : return bn;
1111 : }
1112 :
1113 : /* return either an actual BATslice or a new BAT that contains the
1114 : * "virtual" slice of the input candidate list BAT; except, unlike
1115 : * BATslice, the hseqbase of the returned BAT is 0; note for cand_mask
1116 : * candidate iterators, the BUN values refer to number of 1 bits */
1117 : BAT *
1118 439504 : canditer_slice(const struct canditer *ci, BUN lo, BUN hi)
1119 : {
1120 439504 : BAT *bn;
1121 439504 : oid o;
1122 439504 : BUN add;
1123 :
1124 439504 : assert(ci != NULL);
1125 :
1126 439504 : if (lo >= ci->ncand || lo >= hi)
1127 63724 : return BATdense(0, 0, 0);
1128 375780 : if (hi > ci->ncand)
1129 : hi = ci->ncand;
1130 375780 : if (hi - lo == 1)
1131 321496 : return BATdense(0, canditer_idx(ci, lo), 1);
1132 54284 : switch (ci->tpe) {
1133 5147 : case cand_materialized:
1134 5147 : if (ci->s) {
1135 5147 : bn = BATslice(ci->s, lo + ci->offset, hi + ci->offset);
1136 5147 : BAThseqbase(bn, 0);
1137 5147 : return bn;
1138 : }
1139 0 : bn = COLnew(0, TYPE_oid, hi - lo, TRANSIENT);
1140 0 : if (bn == NULL)
1141 : return NULL;
1142 0 : BATsetcount(bn, hi - lo);
1143 0 : memcpy(Tloc(bn, 0), ci->oids + lo, (hi - lo) * sizeof(oid));
1144 0 : break;
1145 48849 : case cand_dense:
1146 48849 : return BATdense(0, ci->seq + lo, hi - lo);
1147 285 : case cand_except:
1148 285 : o = canditer_idx(ci, lo);
1149 285 : add = o - ci->seq - lo;
1150 285 : assert(add <= ci->nvals);
1151 285 : if (add == ci->nvals || o + hi - lo < ci->oids[add]) {
1152 : /* after last exception or before next
1153 : * exception: return dense sequence */
1154 243 : return BATdense(0, o, hi - lo);
1155 : }
1156 42 : bn = COLnew(0, TYPE_oid, hi - lo, TRANSIENT);
1157 42 : if (bn == NULL)
1158 : return NULL;
1159 42 : BATsetcount(bn, hi - lo);
1160 320 : for (oid *dst = Tloc(bn, 0); lo < hi; lo++) {
1161 373 : while (add < ci->nvals && o == ci->oids[add]) {
1162 95 : o++;
1163 95 : add++;
1164 : }
1165 278 : *dst++ = o++;
1166 : }
1167 : break;
1168 3 : case cand_mask:
1169 3 : return canditer_sliceval_mask(ci, canditer_idx(ci, lo),
1170 : oid_nil, hi - lo,
1171 : oid_nil, oid_nil, 0);
1172 : default:
1173 0 : MT_UNREACHABLE();
1174 : }
1175 42 : bn->tsorted = true;
1176 42 : bn->trevsorted = BATcount(bn) <= 1;
1177 42 : bn->tkey = true;
1178 42 : bn->tseqbase = oid_nil;
1179 42 : bn->tnil = false;
1180 42 : bn->tnonil = true;
1181 42 : bn->tminpos = 0;
1182 42 : bn->tmaxpos = BATcount(bn) - 1;
1183 42 : return virtualize(bn);
1184 : }
1185 :
1186 : /* like the above, except the bounds are given by values instead of
1187 : * indexes */
1188 : BAT *
1189 320945 : canditer_sliceval(const struct canditer *ci, oid lo, oid hi)
1190 : {
1191 320945 : if (ci->tpe != cand_mask) {
1192 962380 : return canditer_slice(
1193 : ci,
1194 320792 : is_oid_nil(lo) ? 0 : canditer_search(ci, lo, true),
1195 320794 : is_oid_nil(hi) ? ci->ncand : canditer_search(ci, hi, true));
1196 : }
1197 :
1198 151 : return canditer_sliceval_mask(ci, lo, hi, ci->ncand,
1199 : oid_nil, oid_nil, 0);
1200 : }
1201 :
1202 : /* return the combination of two slices */
1203 : BAT *
1204 30815 : canditer_slice2(const struct canditer *ci, BUN lo1, BUN hi1, BUN lo2, BUN hi2)
1205 : {
1206 30815 : BAT *bn;
1207 30815 : oid o;
1208 30815 : BUN add;
1209 :
1210 30815 : assert(lo1 <= hi1);
1211 30815 : assert(lo2 <= hi2);
1212 30815 : assert(hi1 <= lo2 || (lo2 == 0 && hi2 == 0));
1213 :
1214 30815 : if (hi1 == lo2) /* consecutive slices: combine into one */
1215 578 : return canditer_slice(ci, lo1, hi2);
1216 30237 : if (lo2 == hi2 || hi1 >= ci->ncand || lo2 >= ci->ncand) {
1217 : /* empty second slice */
1218 23270 : return canditer_slice(ci, lo1, hi1);
1219 : }
1220 6967 : if (lo1 == hi1) /* empty first slice */
1221 1894 : return canditer_slice(ci, lo2, hi2);
1222 5073 : if (lo1 >= ci->ncand) /* out of range */
1223 : return BATdense(0, 0, 0);
1224 :
1225 5073 : if (hi2 >= ci->ncand)
1226 : hi2 = ci->ncand;
1227 :
1228 5073 : bn = COLnew(0, TYPE_oid, hi1 - lo1 + hi2 - lo2, TRANSIENT);
1229 5073 : if (bn == NULL)
1230 : return NULL;
1231 5073 : BATsetcount(bn, hi1 - lo1 + hi2 - lo2);
1232 5073 : bn->tsorted = true;
1233 5073 : bn->trevsorted = BATcount(bn) <= 1;
1234 5073 : bn->tkey = true;
1235 5073 : bn->tseqbase = oid_nil;
1236 5073 : bn->tnil = false;
1237 5073 : bn->tnonil = true;
1238 :
1239 5073 : oid *dst = Tloc(bn, 0);
1240 :
1241 5073 : switch (ci->tpe) {
1242 2426 : case cand_materialized:
1243 2426 : memcpy(dst, ci->oids + lo1, (hi1 - lo1) * sizeof(oid));
1244 2426 : memcpy(dst + hi1 - lo1, ci->oids + lo2, (hi2 - lo2) * sizeof(oid));
1245 2426 : break;
1246 : case cand_dense:
1247 956830 : while (lo1 < hi1)
1248 954184 : *dst++ = ci->seq + lo1++;
1249 288052 : while (lo2 < hi2)
1250 285406 : *dst++ = ci->seq + lo2++;
1251 : break;
1252 1 : case cand_except:
1253 1 : o = canditer_idx(ci, lo1);
1254 1 : add = o - ci->seq - lo1;
1255 1 : assert(add <= ci->nvals);
1256 1 : if (add == ci->nvals) {
1257 : /* after last exception: return dense sequence */
1258 0 : while (lo1 < hi1)
1259 0 : *dst++ = ci->seq + add + lo1++;
1260 : } else {
1261 2 : while (lo1 < hi1) {
1262 1 : while (add < ci->nvals && o == ci->oids[add]) {
1263 0 : o++;
1264 0 : add++;
1265 : }
1266 1 : *dst++ = o++;
1267 1 : lo1++;
1268 : }
1269 : }
1270 1 : o = canditer_idx(ci, lo2);
1271 1 : add = o - ci->seq - lo2;
1272 1 : assert(add <= ci->nvals);
1273 1 : if (add == ci->nvals) {
1274 : /* after last exception: return dense sequence */
1275 0 : while (lo2 < hi2)
1276 0 : *dst++ = ci->seq + add + lo2++;
1277 : } else {
1278 4 : while (lo2 < hi2) {
1279 6 : while (add < ci->nvals && o == ci->oids[add]) {
1280 3 : o++;
1281 3 : add++;
1282 : }
1283 3 : *dst++ = o++;
1284 3 : lo2++;
1285 : }
1286 : }
1287 : break;
1288 0 : case cand_mask:
1289 0 : return canditer_sliceval_mask(ci, canditer_idx(ci, lo1),
1290 : oid_nil, hi1 - lo1,
1291 : canditer_idx(ci, lo2),
1292 : oid_nil, hi2 - lo2);
1293 : default:
1294 0 : MT_UNREACHABLE();
1295 : }
1296 5073 : return virtualize(bn);
1297 : }
1298 :
1299 : BAT *
1300 30239 : canditer_slice2val(const struct canditer *ci, oid lo1, oid hi1, oid lo2, oid hi2)
1301 : {
1302 30239 : if (ci->tpe != cand_mask) {
1303 118468 : return canditer_slice2(
1304 : ci,
1305 27542 : is_oid_nil(lo1) ? 0 : canditer_search(ci, lo1, true),
1306 30237 : is_oid_nil(hi1) ? ci->ncand : canditer_search(ci, hi1, true),
1307 30237 : is_oid_nil(lo2) ? 0 : canditer_search(ci, lo2, true),
1308 215 : is_oid_nil(hi2) ? ci->ncand : canditer_search(ci, hi2, true));
1309 : }
1310 :
1311 2 : return canditer_sliceval_mask(ci, lo1, hi1, ci->ncand,
1312 2 : lo2, hi2, ci->ncand);
1313 : }
1314 :
1315 : BAT *
1316 3152 : BATnegcands(oid tseq, BUN nr, BAT *odels)
1317 : {
1318 3152 : const char *nme;
1319 3152 : Heap *dels;
1320 3152 : BUN lo, hi;
1321 3152 : ccand_t *c;
1322 3152 : BAT *bn;
1323 :
1324 3152 : bn = BATdense(0, tseq, nr);
1325 3152 : if (bn == NULL)
1326 : return NULL;
1327 3152 : if (BATcount(odels) == 0)
1328 2759 : goto doreturn;
1329 :
1330 393 : lo = SORTfndfirst(odels, &bn->tseqbase);
1331 393 : hi = SORTfndfirst(odels, &(oid) {bn->tseqbase + BATcount(bn)});
1332 393 : if (lo == hi)
1333 : return bn;
1334 393 : if (lo + nr == hi) {
1335 134 : BATsetcount(bn, 0);
1336 134 : goto doreturn;
1337 : }
1338 :
1339 259 : nme = BBP_physical(bn->batCacheid);
1340 259 : if ((dels = GDKmalloc(sizeof(Heap))) == NULL){
1341 0 : BBPreclaim(bn);
1342 0 : return NULL;
1343 : }
1344 518 : *dels = (Heap) {
1345 259 : .farmid = BBPselectfarm(bn->batRole, bn->ttype, varheap),
1346 259 : .parentid = bn->batCacheid,
1347 : .dirty = true,
1348 : .refs = ATOMIC_VAR_INIT(1),
1349 : };
1350 259 : strconcat_len(dels->filename, sizeof(dels->filename),
1351 : nme, ".theap", NULL);
1352 :
1353 518 : if (dels->farmid < 0 ||
1354 259 : HEAPalloc(dels, hi - lo + (sizeof(ccand_t)/sizeof(oid)), sizeof(oid)) != GDK_SUCCEED) {
1355 0 : GDKfree(dels);
1356 0 : BBPreclaim(bn);
1357 0 : return NULL;
1358 : }
1359 259 : c = (ccand_t *) dels->base;
1360 259 : *c = (ccand_t) {
1361 : .type = CAND_NEGOID,
1362 : };
1363 259 : dels->free = sizeof(ccand_t) + sizeof(oid) * (hi - lo);
1364 259 : BATiter bi = bat_iterator(odels);
1365 259 : if (bi.type == TYPE_void) {
1366 141 : oid *r = (oid *) (dels->base + sizeof(ccand_t));
1367 93621 : for (BUN x = lo; x < hi; x++)
1368 93480 : r[x - lo] = x + odels->tseqbase;
1369 : } else {
1370 118 : oid *r = (oid *) (dels->base + sizeof(ccand_t));
1371 118 : memcpy(r, (const oid *) bi.base + lo, sizeof(oid) * (hi - lo));
1372 : }
1373 259 : bat_iterator_end(&bi);
1374 259 : assert(bn->tvheap == NULL);
1375 259 : bn->tvheap = dels;
1376 259 : BATsetcount(bn, bn->batCount - (hi - lo));
1377 3152 : doreturn:
1378 3152 : TRC_DEBUG(ALGO, "nr=" BUNFMT ", odels=" ALGOBATFMT
1379 : " -> " ALGOBATFMT "\n",
1380 : nr, ALGOBATPAR(odels),
1381 : ALGOBATPAR(bn));
1382 : return bn;
1383 : }
1384 :
1385 : BAT *
1386 78299 : BATmaskedcands(oid hseq, BUN nr, BAT *masked, bool selected)
1387 : {
1388 78299 : const char *nme;
1389 78299 : Heap *msks;
1390 78299 : ccand_t *c;
1391 78299 : BUN nmask;
1392 78299 : BAT *bn;
1393 :
1394 78299 : assert(masked->ttype == TYPE_msk);
1395 :
1396 78299 : bn = COLnew(hseq, TYPE_void, 0, TRANSIENT);
1397 78299 : if (bn == NULL)
1398 : return NULL;
1399 78299 : BATtseqbase(bn, hseq);
1400 :
1401 78299 : if (BATcount(masked) == 0)
1402 : return bn;
1403 :
1404 78299 : nme = BBP_physical(bn->batCacheid);
1405 78299 : if ((msks = GDKmalloc(sizeof(Heap))) == NULL){
1406 0 : BBPreclaim(bn);
1407 0 : return NULL;
1408 : }
1409 156598 : *msks = (Heap) {
1410 78299 : .farmid = BBPselectfarm(bn->batRole, bn->ttype, varheap),
1411 78299 : .parentid = bn->batCacheid,
1412 : .dirty = true,
1413 : .refs = ATOMIC_VAR_INIT(1),
1414 : };
1415 78299 : strconcat_len(msks->filename, sizeof(msks->filename),
1416 : nme, ".theap", NULL);
1417 :
1418 78299 : nmask = (nr + 31) / 32;
1419 156598 : if (msks->farmid < 0 ||
1420 78299 : HEAPalloc(msks, nmask + (sizeof(ccand_t)/sizeof(uint32_t)), sizeof(uint32_t)) != GDK_SUCCEED) {
1421 0 : GDKfree(msks);
1422 0 : BBPreclaim(bn);
1423 0 : return NULL;
1424 : }
1425 78299 : c = (ccand_t *) msks->base;
1426 78299 : *c = (ccand_t) {
1427 : .type = CAND_MSK,
1428 : // .mask = true,
1429 : };
1430 78299 : msks->free = sizeof(ccand_t) + nmask * sizeof(uint32_t);
1431 78299 : uint32_t *r = (uint32_t*)(msks->base + sizeof(ccand_t));
1432 78299 : BATiter bi = bat_iterator(masked);
1433 78299 : if (selected) {
1434 78299 : if (nr <= bi.count)
1435 78299 : memcpy(r, bi.base, nmask * sizeof(uint32_t));
1436 : else
1437 0 : memcpy(r, bi.base, (bi.count + 31) / 32 * sizeof(uint32_t));
1438 : } else {
1439 0 : const uint32_t *s = (const uint32_t *) bi.base;
1440 0 : BUN nmask_ = (bi.count + 31) / 32;
1441 0 : for (BUN i = 0; i < nmask_; i++)
1442 0 : r[i] = ~s[i];
1443 : }
1444 78299 : if (nr > bi.count) {
1445 0 : BUN rest = bi.count & 31;
1446 0 : BUN nmask_ = (bi.count + 31) / 32;
1447 0 : if (rest > 0)
1448 0 : r[nmask_ -1] |= ((1U << (32 - rest)) - 1) << rest;
1449 0 : for (BUN j = nmask_; j < nmask; j++)
1450 0 : r[j] = ~0;
1451 : }
1452 78299 : bat_iterator_end(&bi);
1453 : /* make sure last word doesn't have any spurious bits set */
1454 78299 : BUN cnt = nr % 32;
1455 78299 : if (cnt > 0)
1456 76418 : r[nmask - 1] &= (1U << cnt) - 1;
1457 : cnt = 0;
1458 10221347 : for (BUN i = 0; i < nmask; i++) {
1459 10143048 : if (cnt == 0 && r[i] != 0)
1460 78273 : c->firstbit = candmask_lobit(r[i]) + i * 32;
1461 10143048 : cnt += candmask_pop(r[i]);
1462 : }
1463 78299 : if (cnt > 0) {
1464 78273 : assert(bn->tvheap == NULL);
1465 78273 : bn->tvheap = msks;
1466 78273 : bn->tseqbase += (oid) c->firstbit;
1467 : } else {
1468 : /* no point having a mask if it's empty */
1469 26 : HEAPfree(msks, true);
1470 26 : GDKfree(msks);
1471 : }
1472 78299 : BATsetcount(bn, cnt);
1473 78299 : TRC_DEBUG(ALGO, "hseq=" OIDFMT ", masked=" ALGOBATFMT ", selected=%s"
1474 : " -> " ALGOBATFMT "\n",
1475 : hseq, ALGOBATPAR(masked),
1476 : selected ? "true" : "false",
1477 : ALGOBATPAR(bn));
1478 78299 : assert(bn->tseqbase != oid_nil);
1479 : return bn;
1480 : }
1481 :
1482 : /* convert a masked candidate list to a positive or negative candidate list */
1483 : BAT *
1484 80774 : BATunmask(BAT *b)
1485 : {
1486 : // assert(!mask_cand(b) || CCAND(b)->mask); /* todo handle negmask case */
1487 80774 : BUN cnt;
1488 80774 : uint32_t rem;
1489 80774 : uint32_t val;
1490 80774 : const uint32_t *src;
1491 80774 : oid *dst;
1492 80774 : BUN n = 0;
1493 80774 : oid tseq = b->hseqbase;
1494 80774 : bool negcand = false;
1495 :
1496 80774 : BATiter bi = bat_iterator(b);
1497 80774 : if (mask_cand(b)) {
1498 77111 : cnt = ccand_free(b) / sizeof(uint32_t);
1499 77111 : rem = 0;
1500 77111 : src = (const uint32_t *) ccand_first(b);
1501 77111 : tseq = b->tseqbase;
1502 77111 : tseq -= (oid) CCAND(b)->firstbit;
1503 : /* create negative candidate list if more than half the
1504 : * bits are set */
1505 77111 : negcand = BATcount(b) > cnt * 16;
1506 : } else {
1507 3663 : cnt = bi.count / 32;
1508 3663 : rem = bi.count % 32;
1509 3663 : src = (const uint32_t *) bi.base;
1510 : }
1511 80774 : BAT *bn;
1512 :
1513 80774 : if (negcand) {
1514 65350 : bn = COLnew(b->hseqbase, TYPE_void, 0, TRANSIENT);
1515 65350 : if (bn == NULL) {
1516 0 : bat_iterator_end(&bi);
1517 0 : return NULL;
1518 : }
1519 65350 : Heap *dels;
1520 65350 : if ((dels = GDKmalloc(sizeof(Heap))) == NULL) {
1521 0 : BBPreclaim(bn);
1522 0 : bat_iterator_end(&bi);
1523 0 : return NULL;
1524 : }
1525 130700 : *dels = (Heap) {
1526 65350 : .farmid = BBPselectfarm(TRANSIENT, TYPE_void, varheap),
1527 65350 : .parentid = bn->batCacheid,
1528 : .dirty = true,
1529 : .refs = ATOMIC_VAR_INIT(1),
1530 : };
1531 65350 : strconcat_len(dels->filename, sizeof(dels->filename),
1532 65350 : BBP_physical(bn->batCacheid), ".theap", NULL);
1533 :
1534 130700 : if (dels->farmid < 0 ||
1535 65350 : HEAPalloc(dels, cnt * 32 - bi.count
1536 : + sizeof(ccand_t) / sizeof(oid), sizeof(oid)) != GDK_SUCCEED) {
1537 0 : GDKfree(dels);
1538 0 : BBPreclaim(bn);
1539 0 : bat_iterator_end(&bi);
1540 0 : return NULL;
1541 : }
1542 65350 : * (ccand_t *) dels->base = (ccand_t) {
1543 : .type = CAND_NEGOID,
1544 : };
1545 65350 : dst = (oid *) (dels->base + sizeof(ccand_t));
1546 6662552 : for (BUN p = 0, v = 0; p < cnt; p++, v += 32) {
1547 6597202 : if ((val = src[p]) == ~UINT32_C(0))
1548 5221276 : continue;
1549 44274517 : for (uint32_t i = 0; i < 32; i++) {
1550 42962137 : if ((val & (1U << i)) == 0) {
1551 39706156 : if (v + i >= b->batCount + n)
1552 : break;
1553 39642610 : dst[n++] = tseq + v + i;
1554 : }
1555 : }
1556 : }
1557 65350 : if (n == 0) {
1558 : /* didn't need it after all */
1559 0 : HEAPfree(dels, true);
1560 0 : GDKfree(dels);
1561 : } else {
1562 65350 : dels->free = sizeof(ccand_t) + n * sizeof(oid);
1563 65350 : dels->dirty = true;
1564 65350 : assert(bn->tvheap == NULL);
1565 65350 : bn->tvheap = dels;
1566 : }
1567 65350 : BATsetcount(bn, n=bi.count);
1568 65350 : bn->tseqbase = tseq;
1569 : } else {
1570 15424 : bn = COLnew(b->hseqbase, TYPE_oid, mask_cand(b) ? bi.count : 1024, TRANSIENT);
1571 15424 : if (bn == NULL) {
1572 0 : bat_iterator_end(&bi);
1573 0 : return NULL;
1574 : }
1575 15424 : dst = (oid *) Tloc(bn, 0);
1576 1842228 : for (BUN p = 0; p < cnt; p++) {
1577 1826804 : if ((val = src[p]) == 0)
1578 1249811 : continue;
1579 19040769 : for (uint32_t i = 0; i < 32; i++) {
1580 18463776 : if (val & (1U << i)) {
1581 16719198 : if (n == BATcapacity(bn)) {
1582 36 : BATsetcount(bn, n);
1583 36 : if (BATextend(bn, BATgrows(bn)) != GDK_SUCCEED) {
1584 0 : BBPreclaim(bn);
1585 0 : bat_iterator_end(&bi);
1586 0 : return NULL;
1587 : }
1588 36 : dst = (oid *) Tloc(bn, 0);
1589 : }
1590 16719198 : dst[n++] = tseq + p * 32 + i;
1591 : }
1592 : }
1593 : }
1594 : /* the last partial mask word */
1595 15424 : if (rem > 0 && (val = src[cnt]) != 0) {
1596 18563 : for (uint32_t i = 0; i < rem; i++) {
1597 16459 : if (val & (1U << i)) {
1598 770 : if (n == BATcapacity(bn)) {
1599 0 : BATsetcount(bn, n);
1600 0 : if (BATextend(bn, BATgrows(bn)) != GDK_SUCCEED) {
1601 0 : BBPreclaim(bn);
1602 0 : bat_iterator_end(&bi);
1603 0 : return NULL;
1604 : }
1605 0 : dst = (oid *) Tloc(bn, 0);
1606 : }
1607 770 : dst[n++] = tseq + cnt * 32 + i;
1608 : }
1609 : }
1610 : }
1611 15424 : BATsetcount(bn, n);
1612 : }
1613 80774 : bat_iterator_end(&bi);
1614 80774 : bn->tkey = true;
1615 80774 : bn->tsorted = true;
1616 80774 : bn->trevsorted = n <= 1;
1617 80774 : bn->tnil = false;
1618 80774 : bn->tnonil = true;
1619 80774 : bn = virtualize(bn);
1620 80774 : TRC_DEBUG(ALGO, ALGOBATFMT " -> " ALGOBATFMT "\n", ALGOBATPAR(b), ALGOBATPAR(bn));
1621 : return bn;
1622 : }
|