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 10015 : BATiscand(BAT *b)
20 : {
21 10015 : if (ATOMtype(b->ttype) != TYPE_oid)
22 : return false;
23 10015 : if (complex_cand(b))
24 : return true;
25 9600 : if (b->ttype == TYPE_void && is_oid_nil(b->tseqbase))
26 : return false;
27 19199 : 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 10288 : newdensecand(oid first, oid last)
34 : {
35 10288 : if (last <= first)
36 0 : first = last = 0; /* empty range */
37 10288 : 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 114693 : BATmergecand(BAT *a, BAT *b)
47 : {
48 114693 : BAT *bn;
49 114693 : oid *restrict p, i;
50 114693 : struct canditer cia, cib;
51 :
52 114693 : BATcheck(a, NULL);
53 114693 : BATcheck(b, NULL);
54 :
55 114693 : canditer_init(&cia, NULL, a);
56 114598 : canditer_init(&cib, NULL, b);
57 :
58 : /* we can return a if b is empty (and v.v.) */
59 114831 : if (cia.ncand == 0) {
60 36588 : bn = canditer_slice(&cib, 0, cib.ncand);
61 36572 : goto doreturn;
62 : }
63 78243 : if (cib.ncand == 0) {
64 18894 : bn = canditer_slice(&cia, 0, cia.ncand);
65 18894 : goto doreturn;
66 : }
67 : /* we can return a if a fully covers b (and v.v) */
68 59349 : if (cia.tpe == cand_dense && cib.tpe == cand_dense) {
69 : /* both are dense */
70 18199 : 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 7635 : bn = newdensecand(cia.seq, cia.seq + cia.ncand < cib.seq + cib.ncand ? cib.seq + cib.ncand : cia.seq + cia.ncand);
74 7648 : goto doreturn;
75 : }
76 10564 : 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 2656 : bn = newdensecand(cib.seq, cia.seq + cia.ncand < cib.seq + cib.ncand ? cib.seq + cib.ncand : cia.seq + cia.ncand);
80 2658 : goto doreturn;
81 : }
82 : }
83 49058 : if (cia.tpe == cand_dense
84 10099 : && cia.seq <= cib.seq
85 4387 : && canditer_last(&cia) >= canditer_last(&cib)) {
86 220 : bn = canditer_slice(&cia, 0, cia.ncand);
87 220 : goto doreturn;
88 : }
89 48836 : if (cib.tpe == cand_dense
90 36152 : && cib.seq <= cia.seq
91 10358 : && canditer_last(&cib) >= canditer_last(&cia)) {
92 172 : bn = canditer_slice(&cib, 0, cib.ncand);
93 172 : goto doreturn;
94 : }
95 :
96 48666 : bn = COLnew(0, TYPE_oid, cia.ncand + cib.ncand, TRANSIENT);
97 48637 : if (bn == NULL)
98 0 : goto doreturn;
99 48637 : p = (oid *) Tloc(bn, 0);
100 48637 : if (cia.tpe == cand_dense && cib.tpe == cand_dense) {
101 : /* both lists are dense */
102 7913 : if (cia.seq > cib.seq) {
103 4197 : struct canditer ci;
104 4197 : ci = cia;
105 4197 : cia = cib;
106 4197 : cib = ci;
107 : }
108 : /* cia completely before cib */
109 7913 : assert(cia.seq + cia.ncand < cib.seq);
110 30297 : for (i = cia.seq; i < cia.seq + cia.ncand; i++)
111 22384 : *p++ = i;
112 : /* there is a gap */
113 26741 : for (i = cib.seq; i < cib.seq + cib.ncand; i++)
114 18828 : *p++ = i;
115 40724 : } else if (cia.tpe == cand_dense || cib.tpe == cand_dense) {
116 30013 : if (cib.tpe == cand_dense) {
117 28102 : struct canditer ci;
118 28102 : ci = cia;
119 28102 : cia = cib;
120 28102 : cib = ci;
121 : }
122 : /* only cia is dense */
123 :
124 : /* copy part of cib that's before the start of cia */
125 124321 : while (canditer_peek(&cib) < cia.seq) {
126 94338 : *p++ = canditer_next(&cib);
127 : }
128 : /* copy all of cia */
129 68145 : for (i = cia.seq; i < cia.seq + cia.ncand; i++)
130 38075 : *p++ = i;
131 : /* skip over part of cib that overlaps with cia */
132 30070 : canditer_setidx(&cib, canditer_search(&cib, cia.seq + cia.ncand, true));
133 : /* copy rest of cib */
134 117096 : while (cib.next < cib.ncand) {
135 87028 : *p++ = canditer_next(&cib);
136 : }
137 : } else {
138 : /* a and b are both not dense */
139 10711 : oid ao = canditer_next(&cia);
140 10706 : oid bo = canditer_next(&cib);
141 21657720 : while (!is_oid_nil(ao) && !is_oid_nil(bo)) {
142 21647011 : if (ao < bo) {
143 12443060 : *p++ = ao;
144 12443060 : ao = canditer_next(&cia);
145 9203951 : } else if (bo < ao) {
146 8171155 : *p++ = bo;
147 8171155 : bo = canditer_next(&cib);
148 : } else {
149 1032796 : *p++ = ao;
150 1032796 : ao = canditer_next(&cia);
151 1032805 : bo = canditer_next(&cib);
152 : }
153 : }
154 541569 : while (!is_oid_nil(ao)) {
155 530859 : *p++ = ao;
156 530859 : ao = canditer_next(&cia);
157 : }
158 415166 : while (!is_oid_nil(bo)) {
159 404455 : *p++ = bo;
160 404455 : bo = canditer_next(&cib);
161 : }
162 : }
163 :
164 : /* properties */
165 48680 : BATsetcount(bn, (BUN) (p - (oid *) Tloc(bn, 0)));
166 48622 : bn->trevsorted = BATcount(bn) <= 1;
167 48622 : bn->tsorted = true;
168 48622 : bn->tkey = true;
169 48622 : bn->tnil = false;
170 48622 : bn->tnonil = true;
171 48622 : bn = virtualize(bn);
172 114814 : doreturn:
173 114814 : 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 9 : BATintersectcand(BAT *a, BAT *b)
185 : {
186 9 : BAT *bn;
187 9 : oid *restrict p;
188 9 : struct canditer cia, cib;
189 :
190 9 : BATcheck(a, NULL);
191 9 : BATcheck(b, NULL);
192 :
193 9 : canditer_init(&cia, NULL, a);
194 9 : canditer_init(&cib, NULL, b);
195 :
196 9 : if (cia.ncand == 0 || cib.ncand == 0) {
197 2 : bn = BATdense(0, 0, 0);
198 2 : goto doreturn;
199 : }
200 :
201 7 : if (cia.tpe == cand_dense && cib.tpe == cand_dense) {
202 : /* both lists are dense */
203 0 : bn = newdensecand(MAX(cia.seq, cib.seq), MIN(cia.seq + cia.ncand, cib.seq + cib.ncand));
204 0 : goto doreturn;
205 : }
206 :
207 7 : bn = COLnew(0, TYPE_oid, MIN(cia.ncand, cib.ncand), TRANSIENT);
208 7 : if (bn == NULL)
209 0 : goto doreturn;
210 7 : p = (oid *) Tloc(bn, 0);
211 7 : if (cia.tpe == cand_dense || cib.tpe == cand_dense) {
212 7 : if (cib.tpe == cand_dense) {
213 7 : struct canditer ci;
214 7 : ci = cia;
215 7 : cia = cib;
216 7 : cib = ci;
217 : }
218 : /* only cia is dense */
219 :
220 : /* search for first value in cib that is contained in cia */
221 7 : canditer_setidx(&cib, canditer_search(&cib, cia.seq, true));
222 7 : oid bo;
223 33 : while (!is_oid_nil(bo = canditer_next(&cib)) && bo < cia.seq + cia.ncand)
224 26 : *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 7 : BATsetcount(bn, (BUN) (p - (oid *) Tloc(bn, 0)));
244 7 : bn->trevsorted = BATcount(bn) <= 1;
245 7 : bn->tsorted = true;
246 7 : bn->tkey = true;
247 7 : bn->tnil = false;
248 7 : bn->tnonil = true;
249 7 : bn = virtualize(bn);
250 9 : doreturn:
251 9 : 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 7678 : BATdiffcand(BAT *a, BAT *b)
260 : {
261 7678 : BAT *bn;
262 7678 : oid *restrict p;
263 7678 : struct canditer cia, cib;
264 :
265 7678 : BATcheck(a, NULL);
266 7678 : BATcheck(b, NULL);
267 :
268 7678 : canditer_init(&cia, NULL, a);
269 7680 : canditer_init(&cib, NULL, b);
270 :
271 7682 : if (cia.ncand == 0) {
272 0 : bn = BATdense(0, 0, 0);
273 0 : goto doreturn;
274 : }
275 7682 : if (cib.ncand == 0) {
276 660 : bn = canditer_slice(&cia, 0, cia.ncand);
277 660 : goto doreturn;
278 : }
279 :
280 7022 : 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 7018 : if (cia.tpe == cand_dense && cib.tpe == cand_dense) {
287 : /* both a and b are dense */
288 3395 : if (cia.seq < cib.seq) {
289 : /* a starts before b */
290 663 : if (cia.seq + cia.ncand <= cib.seq + cib.ncand) {
291 : /* b overlaps with end of a */
292 89 : bn = canditer_slice(&cia, 0, cib.seq - cia.seq);
293 89 : goto doreturn;
294 : }
295 : /* b is subset of a */
296 574 : bn = canditer_slice2(&cia, 0, cib.seq - cia.seq,
297 : cib.seq + cib.ncand - cia.seq,
298 : cia.ncand);
299 573 : goto doreturn;
300 : } else {
301 : /* cia.seq >= cib.seq */
302 2732 : if (cia.seq + cia.ncand > cib.seq + cib.ncand) {
303 : /* b overlaps with beginning of a */
304 198 : bn = canditer_slice(&cia, cib.seq + cib.ncand - cia.seq, cia.ncand);
305 198 : goto doreturn;
306 : }
307 : /* a is subset f b */
308 2534 : bn = BATdense(0, 0, 0);
309 2534 : goto doreturn;
310 : }
311 : }
312 3623 : 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 4956 : bn = canditer_slice2val(&cia, oid_nil, cib.seq,
317 2477 : cib.seq + cib.ncand, oid_nil);
318 2479 : goto doreturn;
319 : }
320 :
321 : /* b is not dense */
322 1146 : bn = COLnew(0, TYPE_oid, BATcount(a), TRANSIENT);
323 1147 : if (bn == NULL)
324 0 : goto doreturn;
325 1147 : p = Tloc(bn, 0);
326 : /* find first position in b that is in range of a */
327 1147 : 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 1144 : oid ob = canditer_next(&cib);
332 7180371 : for (BUN i = 0; i < cia.ncand; i++) {
333 7179225 : oid oa = canditer_next(&cia);
334 9647706 : while (!is_oid_nil(ob) && ob < oa) {
335 2469804 : ob = canditer_next(&cib);
336 : }
337 7179227 : if (is_oid_nil(ob) || oa < ob)
338 4730576 : *p++ = oa;
339 : }
340 : }
341 :
342 : /* properties */
343 1147 : BATsetcount(bn, (BUN) (p - (oid *) Tloc(bn, 0)));
344 1148 : bn->trevsorted = BATcount(bn) <= 1;
345 1148 : bn->tsorted = true;
346 1148 : bn->tkey = true;
347 1148 : bn->tnil = false;
348 1148 : bn->tnonil = true;
349 1148 : bn = virtualize(bn);
350 7681 : doreturn:
351 7681 : 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 924137 : binsearchcand(const oid *cand, BUN hi, oid o)
359 : {
360 924137 : BUN lo = 0;
361 :
362 924137 : if (o <= cand[lo])
363 : return 0;
364 457809 : if (o > cand[hi])
365 406086 : return hi + 1;
366 : /* loop invariant: cand[lo] < o <= cand[hi] */
367 287213 : while (hi > lo + 1) {
368 256083 : BUN mid = (lo + hi) / 2;
369 256083 : if (cand[mid] == o)
370 20593 : return mid;
371 235490 : if (cand[mid] < o)
372 : lo = mid;
373 : else
374 165748 : 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 30426 : count_mask_bits(const struct canditer *ci, BUN lo, BUN hi)
383 : {
384 30426 : BUN n;
385 30426 : assert(lo <= hi);
386 30426 : assert(ci->tpe == cand_mask);
387 30426 : if (lo == hi)
388 : return 0;
389 30426 : lo += ci->firstbit;
390 30426 : hi += ci->firstbit;
391 30426 : BUN loi = lo / 32;
392 30426 : BUN hii = hi / 32;
393 30426 : lo %= 32;
394 30426 : hi %= 32;
395 30426 : if (loi == hii)
396 5964 : return (BUN) candmask_pop((ci->mask[loi] & ((1U << hi) - 1)) >> lo);
397 24462 : n = (BUN) candmask_pop(ci->mask[loi++] >> lo);
398 3499683 : while (loi < hii)
399 3475221 : n += (BUN) candmask_pop(ci->mask[loi++]);
400 24462 : if (hi != 0)
401 2978 : n += (BUN) candmask_pop(ci->mask[loi] & ((1U << hi) - 1));
402 : return n;
403 : }
404 :
405 : /* initialize a candidate iterator */
406 : void
407 10034949 : canditer_init(struct canditer *ci, BAT *b, BAT *s)
408 : {
409 10034949 : assert(ci != NULL);
410 10034949 : BUN batcount = 0;
411 10034949 : oid hseq = 0;
412 10034949 : if (b) {
413 9141896 : MT_lock_set(&b->theaplock);
414 9153119 : batcount = BATcount(b);
415 9153119 : hseq = b->hseqbase;
416 9153119 : MT_lock_unset(&b->theaplock);
417 : }
418 :
419 10059117 : if (s == NULL) {
420 5556511 : 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 5556511 : *ci = (struct canditer) {
429 : .tpe = cand_dense,
430 : .seq = hseq,
431 : .hseq = hseq,
432 : .ncand = batcount,
433 : };
434 5556511 : return;
435 : }
436 :
437 4502606 : assert(ATOMtype(s->ttype) == TYPE_oid || s->ttype == TYPE_msk);
438 4502606 : assert(s->ttype == TYPE_msk|| s->tsorted);
439 4502606 : assert(s->ttype == TYPE_msk|| s->tkey);
440 4502606 : assert(s->ttype == TYPE_msk|| s->tnonil);
441 4502606 : assert(s->ttype == TYPE_void || s->tvheap == NULL);
442 :
443 4502606 : BUN cnt = BATcount(s);
444 :
445 4502606 : if (cnt == 0 || (b != NULL && batcount == 0)) {
446 : /* candidate list for empty BAT or empty candidate list */
447 1188396 : *ci = (struct canditer) {
448 : .tpe = cand_dense,
449 1188396 : .hseq = s->hseqbase,
450 : .s = s,
451 : };
452 1188396 : return;
453 : }
454 :
455 3314210 : *ci = (struct canditer) {
456 3314210 : .seq = s->tseqbase,
457 3314210 : .hseq = s->hseqbase,
458 : .s = s,
459 : };
460 :
461 3314210 : if (mask_cand(s)) {
462 30455 : ci->tpe = cand_mask;
463 30455 : ci->mask = (const uint32_t *) ccand_first(s);
464 30455 : ci->seq = s->tseqbase - (oid) CCAND(s)->firstbit;
465 30455 : ci->hseq = s->hseqbase;
466 30455 : ci->nvals = ccand_free(s) / sizeof(uint32_t);
467 30455 : cnt = ci->nvals * 32;
468 3283755 : } 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 3283755 : } else if (s->ttype == TYPE_void) {
475 2764615 : assert(!is_oid_nil(ci->seq));
476 2764615 : if (s->tvheap) {
477 86140 : assert(ccand_free(s) % SIZEOF_OID == 0);
478 86140 : ci->nvals = ccand_free(s) / SIZEOF_OID;
479 86140 : if (ci->nvals > 0) {
480 86143 : ci->tpe = cand_except;
481 86143 : 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 519140 : } else if (is_oid_nil(ci->seq)) {
490 481295 : ci->tpe = cand_materialized;
491 481295 : ci->oids = (const oid *) Tloc(s, 0);
492 481295 : ci->seq = ci->oids[0];
493 481295 : ci->nvals = cnt;
494 : } else {
495 : /* materialized dense: no exceptions */
496 : ci->tpe = cand_dense;
497 : }
498 3314210 : switch (ci->tpe) {
499 481325 : case cand_materialized:
500 481325 : if (b != NULL) {
501 387818 : BUN p = binsearchcand(ci->oids, cnt - 1U, hseq);
502 : /* p == cnt means candidate list is completely
503 : * before b */
504 387816 : ci->offset = p;
505 387816 : ci->oids += p;
506 387816 : cnt -= p;
507 387816 : if (cnt > 0) {
508 387794 : cnt = binsearchcand(ci->oids, cnt - 1U,
509 : hseq + batcount);
510 : /* cnt == 0 means candidate list is
511 : * completely after b */
512 : }
513 387793 : if (cnt == 0) {
514 : /* no overlap */
515 26 : *ci = (struct canditer) {
516 : .tpe = cand_dense,
517 : .s = s,
518 : };
519 26 : return;
520 : }
521 387789 : ci->seq = ci->oids[0];
522 387789 : ci->nvals = cnt;
523 387789 : if (ci->oids[cnt - 1U] - ci->seq == cnt - 1U) {
524 : /* actually dense */
525 65 : ci->tpe = cand_dense;
526 65 : ci->oids = NULL;
527 65 : ci->nvals = 0;
528 : }
529 : }
530 : break;
531 86155 : case cand_except:
532 : /* exceptions must all be within range of s */
533 86155 : assert(ci->oids[0] >= ci->seq);
534 86155 : assert(ci->oids[ci->nvals - 1U] < ci->seq + cnt + ci->nvals);
535 : /* prune exceptions at either end of range of s */
536 3565068 : while (ci->nvals > 0 && ci->oids[0] == ci->seq) {
537 3478913 : ci->nvals--;
538 3478913 : ci->oids++;
539 3478913 : ci->seq++;
540 : }
541 86309 : while (ci->nvals > 0 &&
542 83432 : ci->oids[ci->nvals - 1U] == ci->seq + cnt + ci->nvals - 1U)
543 154 : ci->nvals--;
544 86155 : if (b != NULL) {
545 70438 : if (ci->seq + cnt + ci->nvals <= hseq ||
546 70438 : 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 86155 : if (ci->nvals > 0) {
556 83273 : if (b == NULL)
557 : break;
558 67760 : BUN p;
559 67760 : p = binsearchcand(ci->oids, ci->nvals - 1U, hseq);
560 67760 : 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 67760 : assert(hseq > ci->seq || p == 0);
571 67760 : 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 67760 : 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 67760 : while (ci->nvals > 0 && ci->oids[0] == ci->seq) {
588 0 : ci->nvals--;
589 0 : ci->oids++;
590 0 : ci->seq++;
591 : }
592 67760 : while (ci->nvals > 0 &&
593 67760 : ci->oids[ci->nvals - 1U] == ci->seq + cnt + ci->nvals - 1U)
594 0 : ci->nvals--;
595 67760 : if (ci->nvals > 0)
596 : break;
597 : }
598 2882 : ci->tpe = cand_dense;
599 2882 : ci->oids = NULL;
600 : /* fall through */
601 2719163 : case cand_dense:
602 2719163 : if (b != NULL) {
603 2352965 : if (ci->seq + cnt <= hseq ||
604 2352965 : 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 2352965 : if (hseq > ci->seq) {
613 66 : cnt -= hseq - ci->seq;
614 66 : ci->offset += hseq - ci->seq;
615 66 : ci->seq = hseq;
616 : }
617 2352965 : if (ci->seq + cnt > hseq + batcount)
618 64 : cnt = hseq + batcount - ci->seq;
619 : }
620 : break;
621 30430 : case cand_mask:
622 30430 : assert(s->tseqbase != oid_nil);
623 30430 : if (b != NULL) {
624 10404 : if (ci->seq + cnt <= hseq ||
625 10404 : 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 10404 : 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 10404 : if (ci->seq + cnt > hseq + batcount) {
641 9333 : cnt = hseq + batcount - ci->seq;
642 : }
643 10404 : 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 30430 : 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 30430 : if (ci->firstbit == 0) {
660 77165 : while (cnt >= 32U && ci->mask[0] == 0) {
661 46732 : cnt -= 32U;
662 46732 : ci->mask++;
663 46732 : ci->nvals--;
664 : }
665 : }
666 : /* check whether there are any 1 bits in the last word */
667 30430 : if (cnt == 0 ||
668 30430 : (cnt < 32U - ci->firstbit &&
669 5966 : ((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 30430 : int i = candmask_lobit(ci->mask[0] >> ci->firstbit);
681 30430 : assert(i >= 0); /* there should be a set bit */
682 30430 : ci->firstbit += i;
683 30430 : cnt -= i;
684 30430 : if (mask_cand(s))
685 30422 : ci->mskoff = s->tseqbase - (oid) CCAND(s)->firstbit + (ci->mask - (const uint32_t *) ccand_first(s)) * 32U;
686 : else
687 8 : ci->mskoff = s->tseqbase + (ci->mask - (const uint32_t *) s->theap->base) * 32U;
688 30430 : ci->seq = ci->mskoff + ci->firstbit;
689 30430 : ci->nextbit = ci->firstbit;
690 : /* at this point we know that bit ci->firstbit is set
691 : * in ci->mask[0] */
692 30430 : ci->lastbit = (ci->firstbit + cnt - 1U) % 32U + 1U;
693 30430 : if (ci->lastbit < 32 &&
694 9333 : (ci->mask[ci->nvals - 1] & ((1U << ci->lastbit) - 1)) == 0) {
695 : /* last partial word is all zero */
696 386 : cnt -= ci->lastbit;
697 386 : ci->lastbit = 32;
698 386 : ci->nvals--;
699 : }
700 30430 : if (ci->lastbit == 32) {
701 : /* "remove" zero words at the end */
702 35750 : while (cnt >= 32 && ci->mask[ci->nvals - 1] == 0) {
703 14265 : ci->nvals--;
704 14265 : cnt -= 32;
705 : }
706 : }
707 30430 : ci->ncand = count_mask_bits(ci, 0, cnt);
708 30420 : return;
709 : }
710 3283751 : ci->ncand = cnt;
711 3283751 : ci->hseq += ci->offset;
712 : }
713 :
714 : /* return the next candidate without advancing */
715 : oid
716 2496812 : canditer_peek(struct canditer *ci)
717 : {
718 2496812 : oid o = oid_nil;
719 2496812 : if (ci->next == ci->ncand)
720 : return oid_nil;
721 2490806 : switch (ci->tpe) {
722 2240129 : case cand_dense:
723 2240129 : o = ci->seq + ci->next;
724 2240129 : break;
725 246701 : case cand_materialized:
726 246701 : assert(ci->next < ci->nvals);
727 246701 : o = ci->oids[ci->next];
728 246701 : 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 1682 : canditer_prev(struct canditer *ci)
751 : {
752 1682 : if (ci->next == 0)
753 0 : return oid_nil;
754 1682 : switch (ci->tpe) {
755 1682 : case cand_dense:
756 1682 : 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 4172 : canditer_peekprev(struct canditer *ci)
785 : {
786 4172 : oid o = oid_nil;
787 :
788 4172 : if (ci->next == 0)
789 : return oid_nil;
790 4172 : switch (ci->tpe) {
791 4172 : case cand_dense:
792 4172 : 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 264354 : canditer_last(const struct canditer *ci)
863 : {
864 264354 : if (ci->ncand == 0)
865 0 : return oid_nil;
866 264354 : switch (ci->tpe) {
867 236697 : case cand_dense:
868 236697 : return ci->seq + ci->ncand - 1;
869 22313 : case cand_materialized:
870 22313 : return ci->oids[ci->ncand - 1];
871 4446 : case cand_except:
872 4446 : return ci->seq + ci->ncand + ci->nvals - 1;
873 898 : case cand_mask:
874 3896 : for (uint8_t i = ci->lastbit; i > 0; ) {
875 3896 : if (ci->mask[ci->nvals - 1] & (1U << --i))
876 898 : 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 315240418 : canditer_idx(const struct canditer *ci, BUN p)
886 : {
887 315240418 : if (p >= ci->ncand)
888 0 : return oid_nil;
889 315240418 : switch (ci->tpe) {
890 171701257 : case cand_dense:
891 171701257 : return ci->seq + p;
892 143307847 : case cand_materialized:
893 143307847 : return ci->oids[p];
894 231239 : case cand_except: {
895 231239 : oid o = ci->seq + p;
896 231239 : if (o < ci->oids[0])
897 : return o;
898 171017 : if (o + ci->nvals > ci->oids[ci->nvals - 1])
899 : return o + ci->nvals;
900 114293 : BUN lo = 0, hi = ci->nvals - 1;
901 795052 : while (hi - lo > 1) {
902 566466 : BUN mid = (hi + lo) / 2;
903 566466 : if (ci->oids[mid] - mid > o)
904 : hi = mid;
905 : else
906 348667 : lo = mid;
907 : }
908 114293 : return o + hi;
909 : }
910 75 : case cand_mask: {
911 75 : BUN x;
912 75 : if ((x = candmask_pop(ci->mask[0] >> ci->firstbit)) > p) {
913 24 : for (uint8_t i = ci->firstbit; ; i++) {
914 99 : if (ci->mask[0] & (1U << i)) {
915 99 : if (p == 0)
916 75 : return ci->mskoff + i;
917 24 : 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 652137 : canditer_setidx(struct canditer *ci, BUN p)
944 : {
945 652137 : if (p != ci->next) {
946 616141 : if (p >= ci->ncand) {
947 61728 : ci->next = ci->ncand;
948 61728 : 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 554413 : ci->next = p;
965 554413 : 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 652137 : }
981 :
982 : /* reset */
983 : void
984 791399 : canditer_reset(struct canditer *ci)
985 : {
986 791399 : if (ci->tpe == cand_mask) {
987 0 : ci->nextbit = ci->firstbit;
988 0 : ci->nextmsk = 0;
989 : } else {
990 791399 : ci->add = 0;
991 : }
992 791399 : ci->next = 0;
993 791399 : }
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 1186705 : canditer_search(const struct canditer *ci, oid o, bool next)
1000 : {
1001 1186705 : BUN p;
1002 :
1003 1186705 : switch (ci->tpe) {
1004 1105076 : case cand_dense:
1005 1105076 : if (o < ci->seq)
1006 968 : return next ? 0 : BUN_NONE;
1007 1104108 : if (o >= ci->seq + ci->ncand)
1008 198303 : return next ? ci->ncand : BUN_NONE;
1009 905805 : return o - ci->seq;
1010 52036 : case cand_materialized:
1011 52036 : if (ci->nvals == 0)
1012 : return 0;
1013 52039 : p = binsearchcand(ci->oids, ci->nvals - 1, o);
1014 52077 : if (next || (p != ci->nvals && ci->oids[p] == o))
1015 51833 : return p;
1016 : break;
1017 29593 : case cand_except:
1018 29593 : if (o < ci->seq)
1019 0 : return next ? 0 : BUN_NONE;
1020 29593 : if (o >= ci->seq + ci->ncand + ci->nvals)
1021 919 : return next ? ci->ncand : BUN_NONE;
1022 28674 : p = binsearchcand(ci->oids, ci->nvals - 1, o);
1023 28674 : if (next || p == ci->nvals || ci->oids[p] != o)
1024 28519 : 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 1300 : canditer_sliceval_mask(const struct canditer *ci, oid lo1, oid hi1, BUN cnt1,
1046 : oid lo2, oid hi2, BUN cnt2)
1047 : {
1048 1300 : assert(cnt2 == 0 || !is_oid_nil(lo2));
1049 1300 : assert(cnt2 == 0 || lo2 >= hi1);
1050 1300 : if (is_oid_nil(lo1) || lo1 < ci->mskoff + ci->firstbit)
1051 373 : lo1 = ci->mskoff + ci->firstbit;
1052 1300 : if (is_oid_nil(hi1) || hi1 >= ci->mskoff + (ci->nvals - 1) * 32 + ci->lastbit)
1053 1081 : hi1 = ci->mskoff + (ci->nvals - 1) * 32 + ci->lastbit;
1054 :
1055 1300 : BAT *bn = COLnew(0, TYPE_oid, cnt1 + cnt2, TRANSIENT);
1056 1301 : if (bn == NULL)
1057 : return NULL;
1058 1301 : bn->tkey = true;
1059 :
1060 1301 : if (hi1 > ci->mskoff) {
1061 1098 : if (lo1 < ci->mskoff)
1062 : lo1 = 0;
1063 : else
1064 1098 : lo1 -= ci->mskoff;
1065 1098 : hi1 -= ci->mskoff;
1066 117188 : for (oid o = lo1; o < hi1 && cnt1 > 0; o++) {
1067 116090 : if (ci->mask[o / 32] & (1U << (o % 32))) {
1068 102206 : if (BUNappend(bn, &(oid){o + ci->mskoff}, false) != GDK_SUCCEED) {
1069 0 : BBPreclaim(bn);
1070 0 : return NULL;
1071 : }
1072 102206 : cnt1--;
1073 : }
1074 : }
1075 : }
1076 1301 : if (cnt2 > 0) {
1077 209 : if (lo2 < ci->mskoff + ci->firstbit)
1078 : lo2 = ci->mskoff + ci->firstbit;
1079 209 : if (is_oid_nil(hi2) || hi2 >= ci->mskoff + (ci->nvals - 1) * 32 + ci->lastbit)
1080 209 : hi2 = ci->mskoff + (ci->nvals - 1) * 32 + ci->lastbit;
1081 :
1082 209 : lo2 -= ci->mskoff;
1083 209 : hi2 -= ci->mskoff;
1084 281 : for (oid o = lo2; o < hi2 && cnt2 > 0; o++) {
1085 72 : if (ci->mask[o / 32] & (1U << (o % 32))) {
1086 56 : if (BUNappend(bn, &(oid){o + ci->mskoff}, false) != GDK_SUCCEED) {
1087 0 : BBPreclaim(bn);
1088 0 : return NULL;
1089 : }
1090 56 : 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 567306 : canditer_slice(const struct canditer *ci, BUN lo, BUN hi)
1103 : {
1104 567306 : BAT *bn;
1105 567306 : oid o;
1106 567306 : BUN add;
1107 :
1108 567306 : assert(ci != NULL);
1109 :
1110 567306 : if (lo >= ci->ncand || lo >= hi)
1111 124439 : return BATdense(0, 0, 0);
1112 442867 : if (hi > ci->ncand)
1113 : hi = ci->ncand;
1114 442867 : if (hi - lo == 1)
1115 362835 : return BATdense(0, canditer_idx(ci, lo), 1);
1116 80032 : switch (ci->tpe) {
1117 4356 : case cand_materialized:
1118 4356 : if (ci->s) {
1119 4356 : bn = BATslice(ci->s, lo + ci->offset, hi + ci->offset);
1120 4357 : BAThseqbase(bn, 0);
1121 4357 : 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 75388 : default: /* really: case cand_dense: */
1130 75388 : return BATdense(0, ci->seq + lo, hi - lo);
1131 285 : case cand_except:
1132 285 : o = canditer_idx(ci, lo);
1133 285 : add = o - ci->seq - lo;
1134 285 : assert(add <= ci->nvals);
1135 285 : if (add == ci->nvals || o + hi - lo < ci->oids[add]) {
1136 : /* after last exception or before next
1137 : * exception: return dense sequence */
1138 241 : 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 372483 : canditer_sliceval(const struct canditer *ci, oid lo, oid hi)
1172 : {
1173 372483 : if (ci->tpe != cand_mask) {
1174 1114691 : return canditer_slice(
1175 : ci,
1176 371471 : is_oid_nil(lo) ? 0 : canditer_search(ci, lo, true),
1177 371395 : is_oid_nil(hi) ? ci->ncand : canditer_search(ci, hi, true));
1178 : }
1179 :
1180 1088 : 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 41504 : canditer_slice2(const struct canditer *ci, BUN lo1, BUN hi1, BUN lo2, BUN hi2)
1187 : {
1188 41504 : BAT *bn;
1189 41504 : oid o;
1190 41504 : BUN add;
1191 :
1192 41504 : assert(lo1 <= hi1);
1193 41504 : assert(lo2 <= hi2);
1194 41504 : assert(hi1 <= lo2 || (lo2 == 0 && hi2 == 0));
1195 :
1196 41504 : if (hi1 == lo2) /* consecutive slices: combine into one */
1197 2817 : return canditer_slice(ci, lo1, hi2);
1198 38687 : if (lo2 == hi2 || hi1 >= ci->ncand || lo2 >= ci->ncand) {
1199 : /* empty second slice */
1200 30918 : return canditer_slice(ci, lo1, hi1);
1201 : }
1202 7769 : if (lo1 == hi1) /* empty first slice */
1203 3026 : return canditer_slice(ci, lo2, hi2);
1204 4743 : if (lo1 >= ci->ncand) /* out of range */
1205 : return BATdense(0, 0, 0);
1206 :
1207 4743 : if (hi2 >= ci->ncand)
1208 : hi2 = ci->ncand;
1209 :
1210 4743 : bn = COLnew(0, TYPE_oid, hi1 - lo1 + hi2 - lo2, TRANSIENT);
1211 4750 : if (bn == NULL)
1212 : return NULL;
1213 4750 : BATsetcount(bn, hi1 - lo1 + hi2 - lo2);
1214 4748 : bn->tsorted = true;
1215 4748 : bn->trevsorted = BATcount(bn) <= 1;
1216 4748 : bn->tkey = true;
1217 4748 : bn->tseqbase = oid_nil;
1218 4748 : bn->tnil = false;
1219 4748 : bn->tnonil = true;
1220 :
1221 4748 : oid *dst = Tloc(bn, 0);
1222 :
1223 4748 : switch (ci->tpe) {
1224 2423 : case cand_materialized:
1225 2423 : memcpy(dst, ci->oids + lo1, (hi1 - lo1) * sizeof(oid));
1226 2423 : memcpy(dst + hi1 - lo1, ci->oids + lo2, (hi2 - lo2) * sizeof(oid));
1227 2423 : break;
1228 : case cand_dense:
1229 943088 : while (lo1 < hi1)
1230 940764 : *dst++ = ci->seq + lo1++;
1231 275499 : while (lo2 < hi2)
1232 273175 : *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 4748 : return virtualize(bn);
1277 : }
1278 :
1279 : BAT *
1280 41147 : canditer_slice2val(const struct canditer *ci, oid lo1, oid hi1, oid lo2, oid hi2)
1281 : {
1282 41147 : if (ci->tpe != cand_mask) {
1283 161381 : return canditer_slice2(
1284 : ci,
1285 36243 : is_oid_nil(lo1) ? 0 : canditer_search(ci, lo1, true),
1286 40947 : is_oid_nil(hi1) ? ci->ncand : canditer_search(ci, hi1, true),
1287 40965 : is_oid_nil(lo2) ? 0 : canditer_search(ci, lo2, true),
1288 2250 : is_oid_nil(hi2) ? ci->ncand : canditer_search(ci, hi2, true));
1289 : }
1290 :
1291 209 : return canditer_sliceval_mask(ci, lo1, hi1, ci->ncand,
1292 209 : lo2, hi2, ci->ncand);
1293 : }
1294 :
1295 : BAT *
1296 3142 : BATnegcands(oid tseq, BUN nr, BAT *odels)
1297 : {
1298 3142 : const char *nme;
1299 3142 : Heap *dels;
1300 3142 : BUN lo, hi;
1301 3142 : ccand_t *c;
1302 3142 : BAT *bn;
1303 :
1304 3142 : bn = BATdense(0, tseq, nr);
1305 3143 : if (bn == NULL)
1306 : return NULL;
1307 3143 : if (BATcount(odels) == 0)
1308 2772 : goto doreturn;
1309 :
1310 371 : lo = SORTfndfirst(odels, &bn->tseqbase);
1311 371 : hi = SORTfndfirst(odels, &(oid) {bn->tseqbase + BATcount(bn)});
1312 371 : if (lo == hi)
1313 : return bn;
1314 371 : if (lo + nr == hi) {
1315 134 : BATsetcount(bn, 0);
1316 134 : goto doreturn;
1317 : }
1318 :
1319 237 : nme = BBP_physical(bn->batCacheid);
1320 237 : if ((dels = GDKmalloc(sizeof(Heap))) == NULL){
1321 0 : BBPreclaim(bn);
1322 0 : return NULL;
1323 : }
1324 474 : *dels = (Heap) {
1325 237 : .farmid = BBPselectfarm(bn->batRole, bn->ttype, varheap),
1326 237 : .parentid = bn->batCacheid,
1327 : .dirty = true,
1328 : .refs = ATOMIC_VAR_INIT(1),
1329 : };
1330 237 : strconcat_len(dels->filename, sizeof(dels->filename),
1331 : nme, ".theap", NULL);
1332 :
1333 474 : if (dels->farmid < 0 ||
1334 237 : 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 237 : c = (ccand_t *) dels->base;
1340 237 : *c = (ccand_t) {
1341 : .type = CAND_NEGOID,
1342 : };
1343 237 : dels->free = sizeof(ccand_t) + sizeof(oid) * (hi - lo);
1344 237 : BATiter bi = bat_iterator(odels);
1345 237 : if (bi.type == TYPE_void) {
1346 115 : oid *r = (oid *) (dels->base + sizeof(ccand_t));
1347 93569 : for (BUN x = lo; x < hi; x++)
1348 93454 : r[x - lo] = x + odels->tseqbase;
1349 : } else {
1350 122 : oid *r = (oid *) (dels->base + sizeof(ccand_t));
1351 122 : memcpy(r, (const oid *) bi.base + lo, sizeof(oid) * (hi - lo));
1352 : }
1353 237 : bat_iterator_end(&bi);
1354 237 : assert(bn->tvheap == NULL);
1355 237 : bn->tvheap = dels;
1356 237 : BATsetcount(bn, bn->batCount - (hi - lo));
1357 3143 : doreturn:
1358 3143 : 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 105774 : BATmaskedcands(oid hseq, BUN nr, BAT *masked, bool selected)
1367 : {
1368 105774 : const char *nme;
1369 105774 : Heap *msks;
1370 105774 : ccand_t *c;
1371 105774 : BUN nmask;
1372 105774 : BAT *bn;
1373 :
1374 105774 : assert(masked->ttype == TYPE_msk);
1375 :
1376 105774 : bn = COLnew(hseq, TYPE_void, 0, TRANSIENT);
1377 105774 : if (bn == NULL)
1378 : return NULL;
1379 105774 : BATtseqbase(bn, hseq);
1380 :
1381 105773 : if (BATcount(masked) == 0)
1382 : return bn;
1383 :
1384 105773 : nme = BBP_physical(bn->batCacheid);
1385 105773 : if ((msks = GDKmalloc(sizeof(Heap))) == NULL){
1386 0 : BBPreclaim(bn);
1387 0 : return NULL;
1388 : }
1389 211547 : *msks = (Heap) {
1390 105774 : .farmid = BBPselectfarm(bn->batRole, bn->ttype, varheap),
1391 105773 : .parentid = bn->batCacheid,
1392 : .dirty = true,
1393 : .refs = ATOMIC_VAR_INIT(1),
1394 : };
1395 105773 : strconcat_len(msks->filename, sizeof(msks->filename),
1396 : nme, ".theap", NULL);
1397 :
1398 105774 : nmask = (nr + 31) / 32;
1399 211548 : if (msks->farmid < 0 ||
1400 105774 : HEAPalloc(msks, nmask + (sizeof(ccand_t)/sizeof(uint32_t)), sizeof(uint32_t)) != GDK_SUCCEED) {
1401 0 : GDKfree(msks);
1402 0 : BBPreclaim(bn);
1403 0 : return NULL;
1404 : }
1405 105774 : c = (ccand_t *) msks->base;
1406 105774 : *c = (ccand_t) {
1407 : .type = CAND_MSK,
1408 : // .mask = true,
1409 : };
1410 105774 : msks->free = sizeof(ccand_t) + nmask * sizeof(uint32_t);
1411 105774 : uint32_t *r = (uint32_t*)(msks->base + sizeof(ccand_t));
1412 105774 : BATiter bi = bat_iterator(masked);
1413 105771 : if (selected) {
1414 105771 : if (nr <= bi.count)
1415 105771 : memcpy(r, bi.base, nmask * sizeof(uint32_t));
1416 : else
1417 0 : memcpy(r, bi.base, (bi.count + 31) / 32 * sizeof(uint32_t));
1418 : } else {
1419 0 : const uint32_t *s = (const uint32_t *) bi.base;
1420 0 : BUN nmask_ = (bi.count + 31) / 32;
1421 0 : for (BUN i = 0; i < nmask_; i++)
1422 0 : r[i] = ~s[i];
1423 : }
1424 105771 : if (nr > bi.count) {
1425 0 : BUN rest = bi.count & 31;
1426 0 : BUN nmask_ = (bi.count + 31) / 32;
1427 0 : if (rest > 0)
1428 0 : r[nmask_ -1] |= ((1U << (32 - rest)) - 1) << rest;
1429 0 : for (BUN j = nmask_; j < nmask; j++)
1430 0 : r[j] = ~0;
1431 : }
1432 105771 : bat_iterator_end(&bi);
1433 : /* make sure last word doesn't have any spurious bits set */
1434 105773 : BUN cnt = nr % 32;
1435 105773 : if (cnt > 0)
1436 103113 : r[nmask - 1] &= (1U << cnt) - 1;
1437 : cnt = 0;
1438 12675764 : for (BUN i = 0; i < nmask; i++) {
1439 12569991 : if (cnt == 0 && r[i] != 0)
1440 104448 : c->firstbit = candmask_lobit(r[i]) + i * 32;
1441 12569991 : cnt += candmask_pop(r[i]);
1442 : }
1443 105773 : if (cnt > 0) {
1444 104456 : assert(bn->tvheap == NULL);
1445 104456 : bn->tvheap = msks;
1446 104456 : bn->tseqbase += (oid) c->firstbit;
1447 : } else {
1448 : /* no point having a mask if it's empty */
1449 1317 : HEAPfree(msks, true);
1450 1317 : GDKfree(msks);
1451 : }
1452 105773 : BATsetcount(bn, cnt);
1453 105772 : TRC_DEBUG(ALGO, "hseq=" OIDFMT ", masked=" ALGOBATFMT ", selected=%s"
1454 : " -> " ALGOBATFMT "\n",
1455 : hseq, ALGOBATPAR(masked),
1456 : selected ? "true" : "false",
1457 : ALGOBATPAR(bn));
1458 105772 : assert(bn->tseqbase != oid_nil);
1459 : return bn;
1460 : }
1461 :
1462 : /* convert a masked candidate list to a positive or negative candidate list */
1463 : BAT *
1464 108155 : BATunmask(BAT *b)
1465 : {
1466 : // assert(!mask_cand(b) || CCAND(b)->mask); /* todo handle negmask case */
1467 108155 : BUN cnt;
1468 108155 : uint32_t rem;
1469 108155 : uint32_t val;
1470 108155 : const uint32_t *src;
1471 108155 : oid *dst;
1472 108155 : BUN n = 0;
1473 108155 : oid tseq = b->hseqbase;
1474 108155 : bool negcand = false;
1475 :
1476 108155 : BATiter bi = bat_iterator(b);
1477 108211 : if (mask_cand(b)) {
1478 103818 : cnt = ccand_free(b) / sizeof(uint32_t);
1479 103818 : rem = 0;
1480 103818 : src = (const uint32_t *) ccand_first(b);
1481 103818 : tseq = b->tseqbase;
1482 103818 : tseq -= (oid) CCAND(b)->firstbit;
1483 : /* create negative candidate list if more than half the
1484 : * bits are set */
1485 103818 : negcand = BATcount(b) > cnt * 16;
1486 : } else {
1487 4393 : cnt = bi.count / 32;
1488 4393 : rem = bi.count % 32;
1489 4393 : src = (const uint32_t *) bi.base;
1490 : }
1491 108211 : BAT *bn;
1492 :
1493 108211 : if (negcand) {
1494 85849 : bn = COLnew(b->hseqbase, TYPE_void, 0, TRANSIENT);
1495 85844 : if (bn == NULL) {
1496 0 : bat_iterator_end(&bi);
1497 0 : return NULL;
1498 : }
1499 85844 : Heap *dels;
1500 85844 : if ((dels = GDKmalloc(sizeof(Heap))) == NULL) {
1501 0 : BBPreclaim(bn);
1502 0 : bat_iterator_end(&bi);
1503 0 : return NULL;
1504 : }
1505 171676 : *dels = (Heap) {
1506 85839 : .farmid = BBPselectfarm(TRANSIENT, TYPE_void, varheap),
1507 85837 : .parentid = bn->batCacheid,
1508 : .dirty = true,
1509 : .refs = ATOMIC_VAR_INIT(1),
1510 : };
1511 85837 : strconcat_len(dels->filename, sizeof(dels->filename),
1512 85837 : BBP_physical(bn->batCacheid), ".theap", NULL);
1513 :
1514 171623 : if (dels->farmid < 0 ||
1515 85799 : HEAPalloc(dels, cnt * 32 - bi.count
1516 : + sizeof(ccand_t) / sizeof(oid), sizeof(oid)) != GDK_SUCCEED) {
1517 0 : GDKfree(dels);
1518 0 : BBPreclaim(bn);
1519 0 : bat_iterator_end(&bi);
1520 0 : return NULL;
1521 : }
1522 85824 : * (ccand_t *) dels->base = (ccand_t) {
1523 : .type = CAND_NEGOID,
1524 : };
1525 85824 : dst = (oid *) (dels->base + sizeof(ccand_t));
1526 9682134 : for (BUN p = 0, v = 0; p < cnt; p++, v += 32) {
1527 9596310 : if ((val = src[p]) == ~UINT32_C(0))
1528 7945420 : continue;
1529 53084489 : for (uint32_t i = 0; i < 32; i++) {
1530 51517790 : if ((val & (1U << i)) == 0) {
1531 45520187 : if (v + i >= b->batCount + n)
1532 : break;
1533 45435996 : dst[n++] = tseq + v + i;
1534 : }
1535 : }
1536 : }
1537 85824 : if (n == 0) {
1538 : /* didn't need it after all */
1539 29 : HEAPfree(dels, true);
1540 29 : GDKfree(dels);
1541 : } else {
1542 85795 : dels->free = sizeof(ccand_t) + n * sizeof(oid);
1543 85795 : dels->dirty = true;
1544 85795 : assert(bn->tvheap == NULL);
1545 85795 : bn->tvheap = dels;
1546 : }
1547 85824 : BATsetcount(bn, n=bi.count);
1548 85833 : bn->tseqbase = tseq;
1549 : } else {
1550 22362 : bn = COLnew(b->hseqbase, TYPE_oid, mask_cand(b) ? bi.count : 1024, TRANSIENT);
1551 22364 : if (bn == NULL) {
1552 0 : bat_iterator_end(&bi);
1553 0 : return NULL;
1554 : }
1555 22364 : dst = (oid *) Tloc(bn, 0);
1556 2685204 : for (BUN p = 0; p < cnt; p++) {
1557 2662838 : if ((val = src[p]) == 0)
1558 1887310 : continue;
1559 25571779 : for (uint32_t i = 0; i < 32; i++) {
1560 24796249 : if (val & (1U << i)) {
1561 21580330 : if (n == BATcapacity(bn)) {
1562 78 : BATsetcount(bn, n);
1563 80 : if (BATextend(bn, BATgrows(bn)) != GDK_SUCCEED) {
1564 0 : BBPreclaim(bn);
1565 0 : bat_iterator_end(&bi);
1566 0 : return NULL;
1567 : }
1568 80 : dst = (oid *) Tloc(bn, 0);
1569 : }
1570 21580332 : dst[n++] = tseq + p * 32 + i;
1571 : }
1572 : }
1573 : }
1574 : /* the last partial mask word */
1575 22366 : if (rem > 0 && (val = src[cnt]) != 0) {
1576 22058 : for (uint32_t i = 0; i < rem; i++) {
1577 19724 : if (val & (1U << i)) {
1578 1182 : if (n == BATcapacity(bn)) {
1579 0 : BATsetcount(bn, n);
1580 0 : if (BATextend(bn, BATgrows(bn)) != GDK_SUCCEED) {
1581 0 : BBPreclaim(bn);
1582 0 : bat_iterator_end(&bi);
1583 0 : return NULL;
1584 : }
1585 0 : dst = (oid *) Tloc(bn, 0);
1586 : }
1587 1182 : dst[n++] = tseq + cnt * 32 + i;
1588 : }
1589 : }
1590 : }
1591 22366 : BATsetcount(bn, n);
1592 : }
1593 108194 : bat_iterator_end(&bi);
1594 108217 : bn->tkey = true;
1595 108217 : bn->tsorted = true;
1596 108217 : bn->trevsorted = n <= 1;
1597 108217 : bn->tnil = false;
1598 108217 : bn->tnonil = true;
1599 108217 : bn = virtualize(bn);
1600 108193 : TRC_DEBUG(ALGO, ALGOBATFMT " -> " ALGOBATFMT "\n", ALGOBATPAR(b), ALGOBATPAR(bn));
1601 : return bn;
1602 : }
|