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 10498 : BATiscand(BAT *b)
20 : {
21 10498 : if (ATOMtype(b->ttype) != TYPE_oid)
22 : return false;
23 10498 : if (complex_cand(b))
24 : return true;
25 10090 : if (b->ttype == TYPE_void && is_oid_nil(b->tseqbase))
26 : return false;
27 20179 : 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 10270 : newdensecand(oid first, oid last)
34 : {
35 10270 : if (last <= first)
36 0 : first = last = 0; /* empty range */
37 10270 : 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 116388 : BATmergecand(BAT *a, BAT *b)
47 : {
48 116388 : BAT *bn;
49 116388 : oid *restrict p, i;
50 116388 : struct canditer cia, cib;
51 :
52 116388 : BATcheck(a, NULL);
53 116388 : BATcheck(b, NULL);
54 :
55 116388 : canditer_init(&cia, NULL, a);
56 116270 : canditer_init(&cib, NULL, b);
57 :
58 : /* we can return a if b is empty (and v.v.) */
59 116476 : if (cia.ncand == 0) {
60 37544 : bn = canditer_slice(&cib, 0, cib.ncand);
61 37525 : goto doreturn;
62 : }
63 78932 : if (cib.ncand == 0) {
64 19304 : bn = canditer_slice(&cia, 0, cia.ncand);
65 19315 : goto doreturn;
66 : }
67 : /* we can return a if a fully covers b (and v.v) */
68 59628 : if (cia.tpe == cand_dense && cib.tpe == cand_dense) {
69 : /* both are dense */
70 18422 : 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 7622 : bn = newdensecand(cia.seq, cia.seq + cia.ncand < cib.seq + cib.ncand ? cib.seq + cib.ncand : cia.seq + cia.ncand);
74 7631 : goto doreturn;
75 : }
76 10800 : 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 2663 : bn = newdensecand(cib.seq, cia.seq + cia.ncand < cib.seq + cib.ncand ? cib.seq + cib.ncand : cia.seq + cia.ncand);
80 2666 : goto doreturn;
81 : }
82 : }
83 49343 : if (cia.tpe == cand_dense
84 10506 : && cia.seq <= cib.seq
85 4557 : && canditer_last(&cia) >= canditer_last(&cib)) {
86 220 : bn = canditer_slice(&cia, 0, cia.ncand);
87 220 : goto doreturn;
88 : }
89 49124 : if (cib.tpe == cand_dense
90 36176 : && cib.seq <= cia.seq
91 10384 : && canditer_last(&cib) >= canditer_last(&cia)) {
92 172 : bn = canditer_slice(&cib, 0, cib.ncand);
93 172 : goto doreturn;
94 : }
95 :
96 48955 : bn = COLnew(0, TYPE_oid, cia.ncand + cib.ncand, TRANSIENT);
97 48946 : if (bn == NULL)
98 0 : goto doreturn;
99 48946 : p = (oid *) Tloc(bn, 0);
100 48946 : if (cia.tpe == cand_dense && cib.tpe == cand_dense) {
101 : /* both lists are dense */
102 8138 : if (cia.seq > cib.seq) {
103 4267 : struct canditer ci;
104 4267 : ci = cia;
105 4267 : cia = cib;
106 4267 : cib = ci;
107 : }
108 : /* cia completely before cib */
109 8138 : assert(cia.seq + cia.ncand < cib.seq);
110 30762 : for (i = cia.seq; i < cia.seq + cia.ncand; i++)
111 22624 : *p++ = i;
112 : /* there is a gap */
113 27194 : for (i = cib.seq; i < cib.seq + cib.ncand; i++)
114 19056 : *p++ = i;
115 40808 : } else if (cia.tpe == cand_dense || cib.tpe == cand_dense) {
116 30012 : if (cib.tpe == cand_dense) {
117 27919 : struct canditer ci;
118 27919 : ci = cia;
119 27919 : cia = cib;
120 27919 : cib = ci;
121 : }
122 : /* only cia is dense */
123 :
124 : /* copy part of cib that's before the start of cia */
125 124478 : while (canditer_peek(&cib) < cia.seq) {
126 94458 : *p++ = canditer_next(&cib);
127 : }
128 : /* copy all of cia */
129 67428 : for (i = cia.seq; i < cia.seq + cia.ncand; i++)
130 37363 : *p++ = i;
131 : /* skip over part of cib that overlaps with cia */
132 30065 : canditer_setidx(&cib, canditer_search(&cib, cia.seq + cia.ncand, true));
133 : /* copy rest of cib */
134 115389 : while (cib.next < cib.ncand) {
135 85294 : *p++ = canditer_next(&cib);
136 : }
137 : } else {
138 : /* a and b are both not dense */
139 10796 : oid ao = canditer_next(&cia);
140 10791 : oid bo = canditer_next(&cib);
141 21126896 : while (!is_oid_nil(ao) && !is_oid_nil(bo)) {
142 21116100 : if (ao < bo) {
143 11699667 : *p++ = ao;
144 11699667 : ao = canditer_next(&cia);
145 9416433 : } else if (bo < ao) {
146 8384036 : *p++ = bo;
147 8384036 : bo = canditer_next(&cib);
148 : } else {
149 1032397 : *p++ = ao;
150 1032397 : ao = canditer_next(&cia);
151 1032393 : bo = canditer_next(&cib);
152 : }
153 : }
154 523607 : while (!is_oid_nil(ao)) {
155 512807 : *p++ = ao;
156 512807 : ao = canditer_next(&cia);
157 : }
158 415167 : while (!is_oid_nil(bo)) {
159 404368 : *p++ = bo;
160 404368 : bo = canditer_next(&cib);
161 : }
162 : }
163 :
164 : /* properties */
165 49001 : BATsetcount(bn, (BUN) (p - (oid *) Tloc(bn, 0)));
166 48985 : bn->trevsorted = BATcount(bn) <= 1;
167 48985 : bn->tsorted = true;
168 48985 : bn->tkey = true;
169 48985 : bn->tnil = false;
170 48985 : bn->tnonil = true;
171 48985 : bn = virtualize(bn);
172 116498 : doreturn:
173 116498 : 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 4 : BATintersectcand(BAT *a, BAT *b)
185 : {
186 4 : BAT *bn;
187 4 : oid *restrict p;
188 4 : struct canditer cia, cib;
189 :
190 4 : BATcheck(a, NULL);
191 4 : BATcheck(b, NULL);
192 :
193 4 : canditer_init(&cia, NULL, a);
194 4 : canditer_init(&cib, NULL, b);
195 :
196 4 : if (cia.ncand == 0 || cib.ncand == 0) {
197 2 : bn = BATdense(0, 0, 0);
198 2 : goto doreturn;
199 : }
200 :
201 2 : 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 2 : bn = COLnew(0, TYPE_oid, MIN(cia.ncand, cib.ncand), TRANSIENT);
208 2 : if (bn == NULL)
209 0 : goto doreturn;
210 2 : p = (oid *) Tloc(bn, 0);
211 2 : if (cia.tpe == cand_dense || cib.tpe == cand_dense) {
212 2 : if (cib.tpe == cand_dense) {
213 2 : struct canditer ci;
214 2 : ci = cia;
215 2 : cia = cib;
216 2 : cib = ci;
217 : }
218 : /* only cia is dense */
219 :
220 : /* search for first value in cib that is contained in cia */
221 2 : canditer_setidx(&cib, canditer_search(&cib, cia.seq, true));
222 2 : oid bo;
223 12 : while (!is_oid_nil(bo = canditer_next(&cib)) && bo < cia.seq + cia.ncand)
224 10 : *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 2 : BATsetcount(bn, (BUN) (p - (oid *) Tloc(bn, 0)));
244 2 : bn->trevsorted = BATcount(bn) <= 1;
245 2 : bn->tsorted = true;
246 2 : bn->tkey = true;
247 2 : bn->tnil = false;
248 2 : bn->tnonil = true;
249 2 : bn = virtualize(bn);
250 4 : doreturn:
251 4 : 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 8355 : BATdiffcand(BAT *a, BAT *b)
260 : {
261 8355 : BAT *bn;
262 8355 : oid *restrict p;
263 8355 : struct canditer cia, cib;
264 :
265 8355 : BATcheck(a, NULL);
266 8355 : BATcheck(b, NULL);
267 :
268 8355 : canditer_init(&cia, NULL, a);
269 8357 : canditer_init(&cib, NULL, b);
270 :
271 8357 : if (cia.ncand == 0) {
272 0 : bn = BATdense(0, 0, 0);
273 0 : goto doreturn;
274 : }
275 8357 : if (cib.ncand == 0) {
276 703 : bn = canditer_slice(&cia, 0, cia.ncand);
277 706 : goto doreturn;
278 : }
279 :
280 7654 : 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 7653 : if (cia.tpe == cand_dense && cib.tpe == cand_dense) {
287 : /* both a and b are dense */
288 4002 : if (cia.seq < cib.seq) {
289 : /* a starts before b */
290 680 : if (cia.seq + cia.ncand <= cib.seq + cib.ncand) {
291 : /* b overlaps with end of a */
292 98 : bn = canditer_slice(&cia, 0, cib.seq - cia.seq);
293 98 : goto doreturn;
294 : }
295 : /* b is subset of a */
296 582 : bn = canditer_slice2(&cia, 0, cib.seq - cia.seq,
297 : cib.seq + cib.ncand - cia.seq,
298 : cia.ncand);
299 582 : goto doreturn;
300 : } else {
301 : /* cia.seq >= cib.seq */
302 3322 : if (cia.seq + cia.ncand > cib.seq + cib.ncand) {
303 : /* b overlaps with beginning of a */
304 197 : bn = canditer_slice(&cia, cib.seq + cib.ncand - cia.seq, cia.ncand);
305 198 : goto doreturn;
306 : }
307 : /* a is subset f b */
308 3125 : bn = BATdense(0, 0, 0);
309 3126 : goto doreturn;
310 : }
311 : }
312 3651 : 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 4958 : bn = canditer_slice2val(&cia, oid_nil, cib.seq,
317 2478 : cib.seq + cib.ncand, oid_nil);
318 2480 : goto doreturn;
319 : }
320 :
321 : /* b is not dense */
322 1173 : bn = COLnew(0, TYPE_oid, BATcount(a), TRANSIENT);
323 1173 : if (bn == NULL)
324 0 : goto doreturn;
325 1173 : p = Tloc(bn, 0);
326 : /* find first position in b that is in range of a */
327 1173 : 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 1172 : oid ob = canditer_next(&cib);
332 7070882 : for (BUN i = 0; i < cia.ncand; i++) {
333 7069713 : oid oa = canditer_next(&cia);
334 9489870 : while (!is_oid_nil(ob) && ob < oa) {
335 2423278 : ob = canditer_next(&cib);
336 : }
337 7069710 : if (is_oid_nil(ob) || oa < ob)
338 4673866 : *p++ = oa;
339 : }
340 : }
341 :
342 : /* properties */
343 1170 : BATsetcount(bn, (BUN) (p - (oid *) Tloc(bn, 0)));
344 1171 : bn->trevsorted = BATcount(bn) <= 1;
345 1171 : bn->tsorted = true;
346 1171 : bn->tkey = true;
347 1171 : bn->tnil = false;
348 1171 : bn->tnonil = true;
349 1171 : bn = virtualize(bn);
350 8361 : doreturn:
351 8361 : 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 928157 : binsearchcand(const oid *cand, BUN hi, oid o)
359 : {
360 928157 : BUN lo = 0;
361 :
362 928157 : if (o <= cand[lo])
363 : return 0;
364 458545 : if (o > cand[hi])
365 409090 : return hi + 1;
366 : /* loop invariant: cand[lo] < o <= cand[hi] */
367 271751 : while (hi > lo + 1) {
368 242924 : BUN mid = (lo + hi) / 2;
369 242924 : if (cand[mid] == o)
370 20628 : return mid;
371 222296 : if (cand[mid] < o)
372 : lo = mid;
373 : else
374 157930 : 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 27456 : count_mask_bits(const struct canditer *ci, BUN lo, BUN hi)
383 : {
384 27456 : BUN n;
385 27456 : assert(lo <= hi);
386 27456 : assert(ci->tpe == cand_mask);
387 27456 : if (lo == hi)
388 : return 0;
389 27456 : lo += ci->firstbit;
390 27456 : hi += ci->firstbit;
391 27456 : BUN loi = lo / 32;
392 27456 : BUN hii = hi / 32;
393 27456 : lo %= 32;
394 27456 : hi %= 32;
395 27456 : if (loi == hii)
396 5979 : return (BUN) candmask_pop((ci->mask[loi] & ((1U << hi) - 1)) >> lo);
397 21477 : n = (BUN) candmask_pop(ci->mask[loi++] >> lo);
398 2219612 : while (loi < hii)
399 2198135 : n += (BUN) candmask_pop(ci->mask[loi++]);
400 21477 : if (hi != 0)
401 2938 : n += (BUN) candmask_pop(ci->mask[loi] & ((1U << hi) - 1));
402 : return n;
403 : }
404 :
405 : /* initialize a candidate iterator */
406 : void
407 10501246 : canditer_init(struct canditer *ci, BAT *b, BAT *s)
408 : {
409 10501246 : assert(ci != NULL);
410 10501246 : BUN batcount = 0;
411 10501246 : oid hseq = 0;
412 10501246 : if (b) {
413 9550157 : MT_lock_set(&b->theaplock);
414 9559461 : batcount = BATcount(b);
415 9559461 : hseq = b->hseqbase;
416 9559461 : MT_lock_unset(&b->theaplock);
417 : }
418 :
419 10522041 : if (s == NULL) {
420 5857812 : 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 5857812 : *ci = (struct canditer) {
429 : .tpe = cand_dense,
430 : .seq = hseq,
431 : .hseq = hseq,
432 : .ncand = batcount,
433 : };
434 5857812 : return;
435 : }
436 :
437 4664229 : assert(ATOMtype(s->ttype) == TYPE_oid || s->ttype == TYPE_msk);
438 4664229 : assert(s->ttype == TYPE_msk|| s->tsorted);
439 4664229 : assert(s->ttype == TYPE_msk|| s->tkey);
440 4664229 : assert(s->ttype == TYPE_msk|| s->tnonil);
441 4664229 : assert(s->ttype == TYPE_void || s->tvheap == NULL);
442 :
443 4664229 : BUN cnt = BATcount(s);
444 :
445 4664229 : if (cnt == 0 || (b != NULL && batcount == 0)) {
446 : /* candidate list for empty BAT or empty candidate list */
447 1298457 : *ci = (struct canditer) {
448 : .tpe = cand_dense,
449 1298457 : .hseq = s->hseqbase,
450 : .s = s,
451 : };
452 1298457 : return;
453 : }
454 :
455 3365772 : *ci = (struct canditer) {
456 3365772 : .seq = s->tseqbase,
457 3365772 : .hseq = s->hseqbase,
458 : .s = s,
459 : };
460 :
461 3365772 : if (mask_cand(s)) {
462 27511 : ci->tpe = cand_mask;
463 27511 : ci->mask = (const uint32_t *) ccand_first(s);
464 27511 : ci->seq = s->tseqbase - (oid) CCAND(s)->firstbit;
465 27511 : ci->hseq = s->hseqbase;
466 27511 : ci->nvals = ccand_free(s) / sizeof(uint32_t);
467 27511 : cnt = ci->nvals * 32;
468 3338261 : } 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 3338261 : } else if (s->ttype == TYPE_void) {
475 2800402 : assert(!is_oid_nil(ci->seq));
476 2800402 : if (s->tvheap) {
477 74242 : assert(ccand_free(s) % SIZEOF_OID == 0);
478 74242 : ci->nvals = ccand_free(s) / SIZEOF_OID;
479 74242 : if (ci->nvals > 0) {
480 74220 : ci->tpe = cand_except;
481 74220 : 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 537859 : } else if (is_oid_nil(ci->seq)) {
490 499279 : ci->tpe = cand_materialized;
491 499279 : ci->oids = (const oid *) Tloc(s, 0);
492 499279 : ci->seq = ci->oids[0];
493 499279 : ci->nvals = cnt;
494 : } else {
495 : /* materialized dense: no exceptions */
496 : ci->tpe = cand_dense;
497 : }
498 3365772 : switch (ci->tpe) {
499 499466 : case cand_materialized:
500 499466 : if (b != NULL) {
501 391254 : BUN p = binsearchcand(ci->oids, cnt - 1U, hseq);
502 : /* p == cnt means candidate list is completely
503 : * before b */
504 391254 : ci->offset = p;
505 391254 : ci->oids += p;
506 391254 : cnt -= p;
507 391254 : if (cnt > 0) {
508 391254 : cnt = binsearchcand(ci->oids, cnt - 1U,
509 : hseq + batcount);
510 : /* cnt == 0 means candidate list is
511 : * completely after b */
512 : }
513 391240 : if (cnt == 0) {
514 : /* no overlap */
515 0 : *ci = (struct canditer) {
516 : .tpe = cand_dense,
517 : .s = s,
518 : };
519 0 : return;
520 : }
521 391240 : ci->seq = ci->oids[0];
522 391240 : ci->nvals = cnt;
523 391240 : if (ci->oids[cnt - 1U] - ci->seq == cnt - 1U) {
524 : /* actually dense */
525 60 : ci->tpe = cand_dense;
526 60 : ci->oids = NULL;
527 60 : ci->nvals = 0;
528 : }
529 : }
530 : break;
531 74210 : case cand_except:
532 : /* exceptions must all be within range of s */
533 74210 : assert(ci->oids[0] >= ci->seq);
534 74210 : assert(ci->oids[ci->nvals - 1U] < ci->seq + cnt + ci->nvals);
535 : /* prune exceptions at either end of range of s */
536 2752389 : while (ci->nvals > 0 && ci->oids[0] == ci->seq) {
537 2678179 : ci->nvals--;
538 2678179 : ci->oids++;
539 2678179 : ci->seq++;
540 : }
541 74433 : while (ci->nvals > 0 &&
542 72677 : ci->oids[ci->nvals - 1U] == ci->seq + cnt + ci->nvals - 1U)
543 223 : ci->nvals--;
544 74210 : if (b != NULL) {
545 69311 : if (ci->seq + cnt + ci->nvals <= hseq ||
546 69311 : 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 74210 : if (ci->nvals > 0) {
556 72439 : if (b == NULL)
557 : break;
558 67745 : BUN p;
559 67745 : p = binsearchcand(ci->oids, ci->nvals - 1U, hseq);
560 67745 : 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 67745 : assert(hseq > ci->seq || p == 0);
571 67745 : 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 67745 : 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 67745 : while (ci->nvals > 0 && ci->oids[0] == ci->seq) {
588 0 : ci->nvals--;
589 0 : ci->oids++;
590 0 : ci->seq++;
591 : }
592 67745 : while (ci->nvals > 0 &&
593 67745 : ci->oids[ci->nvals - 1U] == ci->seq + cnt + ci->nvals - 1U)
594 0 : ci->nvals--;
595 67745 : if (ci->nvals > 0)
596 : break;
597 : }
598 1771 : ci->tpe = cand_dense;
599 1771 : ci->oids = NULL;
600 : /* fall through */
601 2766323 : case cand_dense:
602 2766323 : if (b != NULL) {
603 2395900 : if (ci->seq + cnt <= hseq ||
604 2395900 : 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 2395900 : if (hseq > ci->seq) {
613 0 : cnt -= hseq - ci->seq;
614 0 : ci->offset += hseq - ci->seq;
615 0 : ci->seq = hseq;
616 : }
617 2395900 : if (ci->seq + cnt > hseq + batcount)
618 0 : cnt = hseq + batcount - ci->seq;
619 : }
620 : break;
621 27472 : case cand_mask:
622 27472 : assert(s->tseqbase != oid_nil);
623 27472 : if (b != NULL) {
624 10286 : if (ci->seq + cnt <= hseq ||
625 10286 : 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 10286 : 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 10286 : if (ci->seq + cnt > hseq + batcount) {
641 9322 : cnt = hseq + batcount - ci->seq;
642 : }
643 10286 : 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 27472 : 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 27472 : if (ci->firstbit == 0) {
660 69546 : while (cnt >= 32U && ci->mask[0] == 0) {
661 42065 : cnt -= 32U;
662 42065 : ci->mask++;
663 42065 : ci->nvals--;
664 : }
665 : }
666 : /* check whether there are any 1 bits in the last word */
667 27472 : if (cnt == 0 ||
668 27472 : (cnt < 32U - ci->firstbit &&
669 5992 : ((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 27472 : int i = candmask_lobit(ci->mask[0] >> ci->firstbit);
681 27472 : assert(i >= 0); /* there should be a set bit */
682 27472 : ci->firstbit += i;
683 27472 : cnt -= i;
684 27472 : if (mask_cand(s))
685 27473 : 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 27472 : ci->seq = ci->mskoff + ci->firstbit;
689 27472 : ci->nextbit = ci->firstbit;
690 : /* at this point we know that bit ci->firstbit is set
691 : * in ci->mask[0] */
692 27472 : ci->lastbit = (ci->firstbit + cnt - 1U) % 32U + 1U;
693 27472 : if (ci->lastbit < 32 &&
694 9313 : (ci->mask[ci->nvals - 1] & ((1U << ci->lastbit) - 1)) == 0) {
695 : /* last partial word is all zero */
696 394 : cnt -= ci->lastbit;
697 394 : ci->lastbit = 32;
698 394 : ci->nvals--;
699 : }
700 27472 : if (ci->lastbit == 32) {
701 : /* "remove" zero words at the end */
702 34311 : while (cnt >= 32 && ci->mask[ci->nvals - 1] == 0) {
703 15758 : ci->nvals--;
704 15758 : cnt -= 32;
705 : }
706 : }
707 27472 : ci->ncand = count_mask_bits(ci, 0, cnt);
708 27479 : return;
709 : }
710 3338286 : ci->ncand = cnt;
711 3338286 : ci->hseq += ci->offset;
712 : }
713 :
714 : /* return the next candidate without advancing */
715 : oid
716 2431021 : canditer_peek(struct canditer *ci)
717 : {
718 2431021 : oid o = oid_nil;
719 2431021 : if (ci->next == ci->ncand)
720 : return oid_nil;
721 2424951 : switch (ci->tpe) {
722 2176699 : case cand_dense:
723 2176699 : o = ci->seq + ci->next;
724 2176699 : break;
725 244276 : case cand_materialized:
726 244276 : assert(ci->next < ci->nvals);
727 244276 : o = ci->oids[ci->next];
728 244276 : 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 2125 : canditer_prev(struct canditer *ci)
751 : {
752 2125 : if (ci->next == 0)
753 0 : return oid_nil;
754 2125 : switch (ci->tpe) {
755 2125 : case cand_dense:
756 2125 : 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 4891 : canditer_peekprev(struct canditer *ci)
785 : {
786 4891 : oid o = oid_nil;
787 :
788 4891 : if (ci->next == 0)
789 : return oid_nil;
790 4891 : switch (ci->tpe) {
791 4891 : case cand_dense:
792 4891 : 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 1355301 : canditer_last(const struct canditer *ci)
863 : {
864 1355301 : if (ci->ncand == 0)
865 0 : return oid_nil;
866 1355301 : switch (ci->tpe) {
867 1028413 : case cand_dense:
868 1028413 : return ci->seq + ci->ncand - 1;
869 259818 : case cand_materialized:
870 259818 : return ci->oids[ci->ncand - 1];
871 63569 : case cand_except:
872 63569 : return ci->seq + ci->ncand + ci->nvals - 1;
873 3495 : case cand_mask:
874 9902 : for (uint8_t i = ci->lastbit; i > 0; ) {
875 9908 : if (ci->mask[ci->nvals - 1] & (1U << --i))
876 3501 : 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 316956467 : canditer_idx(const struct canditer *ci, BUN p)
886 : {
887 316956467 : if (p >= ci->ncand)
888 0 : return oid_nil;
889 316956467 : switch (ci->tpe) {
890 172833840 : case cand_dense:
891 172833840 : return ci->seq + p;
892 143887835 : case cand_materialized:
893 143887835 : return ci->oids[p];
894 234717 : case cand_except: {
895 234717 : oid o = ci->seq + p;
896 234717 : if (o < ci->oids[0])
897 : return o;
898 187332 : if (o + ci->nvals > ci->oids[ci->nvals - 1])
899 : return o + ci->nvals;
900 129356 : BUN lo = 0, hi = ci->nvals - 1;
901 947857 : while (hi - lo > 1) {
902 689145 : BUN mid = (hi + lo) / 2;
903 689145 : if (ci->oids[mid] - mid > o)
904 : hi = mid;
905 : else
906 386338 : lo = mid;
907 : }
908 129356 : 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 643353 : canditer_setidx(struct canditer *ci, BUN p)
944 : {
945 643353 : if (p != ci->next) {
946 607621 : if (p >= ci->ncand) {
947 64581 : ci->next = ci->ncand;
948 64581 : 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 543040 : ci->next = p;
965 543040 : 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 643353 : }
981 :
982 : /* reset */
983 : void
984 789439 : canditer_reset(struct canditer *ci)
985 : {
986 789439 : if (ci->tpe == cand_mask) {
987 0 : ci->nextbit = ci->firstbit;
988 0 : ci->nextmsk = 0;
989 : } else {
990 789439 : ci->add = 0;
991 : }
992 789439 : ci->next = 0;
993 789439 : }
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 1194578 : canditer_search(const struct canditer *ci, oid o, bool next)
1000 : {
1001 1194578 : BUN p;
1002 :
1003 1194578 : switch (ci->tpe) {
1004 1115834 : case cand_dense:
1005 1115834 : if (o < ci->seq)
1006 993 : return next ? 0 : BUN_NONE;
1007 1114841 : if (o >= ci->seq + ci->ncand)
1008 204599 : return next ? ci->ncand : BUN_NONE;
1009 910242 : return o - ci->seq;
1010 52002 : case cand_materialized:
1011 52002 : if (ci->nvals == 0)
1012 : return 0;
1013 51993 : p = binsearchcand(ci->oids, ci->nvals - 1, o);
1014 52065 : if (next || (p != ci->nvals && ci->oids[p] == o))
1015 51820 : return p;
1016 : break;
1017 26742 : case cand_except:
1018 26742 : if (o < ci->seq)
1019 0 : return next ? 0 : BUN_NONE;
1020 26742 : if (o >= ci->seq + ci->ncand + ci->nvals)
1021 933 : return next ? ci->ncand : BUN_NONE;
1022 25809 : p = binsearchcand(ci->oids, ci->nvals - 1, o);
1023 25809 : if (next || p == ci->nvals || ci->oids[p] != o)
1024 25654 : 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 1285 : canditer_sliceval_mask(const struct canditer *ci, oid lo1, oid hi1, BUN cnt1,
1046 : oid lo2, oid hi2, BUN cnt2)
1047 : {
1048 1285 : assert(cnt2 == 0 || !is_oid_nil(lo2));
1049 1285 : assert(cnt2 == 0 || lo2 >= hi1);
1050 1285 : if (is_oid_nil(lo1) || lo1 < ci->mskoff + ci->firstbit)
1051 359 : lo1 = ci->mskoff + ci->firstbit;
1052 1285 : if (is_oid_nil(hi1) || hi1 >= ci->mskoff + (ci->nvals - 1) * 32 + ci->lastbit)
1053 1075 : hi1 = ci->mskoff + (ci->nvals - 1) * 32 + ci->lastbit;
1054 :
1055 1285 : BAT *bn = COLnew(0, TYPE_oid, cnt1 + cnt2, TRANSIENT);
1056 1287 : if (bn == NULL)
1057 : return NULL;
1058 1287 : bn->tkey = true;
1059 :
1060 1287 : if (hi1 > ci->mskoff) {
1061 1093 : if (lo1 < ci->mskoff)
1062 : lo1 = 0;
1063 : else
1064 1093 : lo1 -= ci->mskoff;
1065 1093 : hi1 -= ci->mskoff;
1066 118068 : for (oid o = lo1; o < hi1 && cnt1 > 0; o++) {
1067 116975 : if (ci->mask[o / 32] & (1U << (o % 32))) {
1068 104833 : if (BUNappend(bn, &(oid){o + ci->mskoff}, false) != GDK_SUCCEED) {
1069 0 : BBPreclaim(bn);
1070 0 : return NULL;
1071 : }
1072 104833 : cnt1--;
1073 : }
1074 : }
1075 : }
1076 1287 : 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 579155 : canditer_slice(const struct canditer *ci, BUN lo, BUN hi)
1103 : {
1104 579155 : BAT *bn;
1105 579155 : oid o;
1106 579155 : BUN add;
1107 :
1108 579155 : assert(ci != NULL);
1109 :
1110 579155 : if (lo >= ci->ncand || lo >= hi)
1111 137918 : return BATdense(0, 0, 0);
1112 441237 : if (hi > ci->ncand)
1113 : hi = ci->ncand;
1114 441237 : if (hi - lo == 1)
1115 358173 : return BATdense(0, canditer_idx(ci, lo), 1);
1116 83064 : switch (ci->tpe) {
1117 4668 : case cand_materialized:
1118 4668 : if (ci->s) {
1119 4668 : bn = BATslice(ci->s, lo + ci->offset, hi + ci->offset);
1120 4666 : BAThseqbase(bn, 0);
1121 4666 : 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 78106 : default: /* really: case cand_dense: */
1130 78106 : return BATdense(0, ci->seq + lo, hi - lo);
1131 287 : case cand_except:
1132 287 : o = canditer_idx(ci, lo);
1133 287 : add = o - ci->seq - lo;
1134 287 : assert(add <= ci->nvals);
1135 287 : if (add == ci->nvals || o + hi - lo < ci->oids[add]) {
1136 : /* after last exception or before next
1137 : * exception: return dense sequence */
1138 243 : return BATdense(0, o, hi - lo);
1139 : }
1140 44 : bn = COLnew(0, TYPE_oid, hi - lo, TRANSIENT);
1141 44 : if (bn == NULL)
1142 : return NULL;
1143 44 : BATsetcount(bn, hi - lo);
1144 392 : for (oid *dst = Tloc(bn, 0); lo < hi; lo++) {
1145 501 : while (add < ci->nvals && o == ci->oids[add]) {
1146 153 : o++;
1147 153 : add++;
1148 : }
1149 348 : *dst++ = o++;
1150 : }
1151 : break;
1152 3 : case cand_mask:
1153 3 : return canditer_sliceval_mask(ci, canditer_idx(ci, lo),
1154 : oid_nil, hi - lo,
1155 : oid_nil, oid_nil, 0);
1156 : }
1157 44 : bn->tsorted = true;
1158 44 : bn->trevsorted = BATcount(bn) <= 1;
1159 44 : bn->tkey = true;
1160 44 : bn->tseqbase = oid_nil;
1161 44 : bn->tnil = false;
1162 44 : bn->tnonil = true;
1163 44 : bn->tminpos = 0;
1164 44 : bn->tmaxpos = BATcount(bn) - 1;
1165 44 : return virtualize(bn);
1166 : }
1167 :
1168 : /* like the above, except the bounds are given by values instead of
1169 : * indexes */
1170 : BAT *
1171 376574 : canditer_sliceval(const struct canditer *ci, oid lo, oid hi)
1172 : {
1173 376574 : if (ci->tpe != cand_mask) {
1174 1126496 : return canditer_slice(
1175 : ci,
1176 375391 : is_oid_nil(lo) ? 0 : canditer_search(ci, lo, true),
1177 375499 : is_oid_nil(hi) ? ci->ncand : canditer_search(ci, hi, true));
1178 : }
1179 :
1180 1075 : 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 42726 : canditer_slice2(const struct canditer *ci, BUN lo1, BUN hi1, BUN lo2, BUN hi2)
1187 : {
1188 42726 : BAT *bn;
1189 42726 : oid o;
1190 42726 : BUN add;
1191 :
1192 42726 : assert(lo1 <= hi1);
1193 42726 : assert(lo2 <= hi2);
1194 42726 : assert(hi1 <= lo2 || (lo2 == 0 && hi2 == 0));
1195 :
1196 42726 : if (hi1 == lo2) /* consecutive slices: combine into one */
1197 2962 : return canditer_slice(ci, lo1, hi2);
1198 39764 : if (lo2 == hi2 || hi1 >= ci->ncand || lo2 >= ci->ncand) {
1199 : /* empty second slice */
1200 33102 : return canditer_slice(ci, lo1, hi1);
1201 : }
1202 6662 : if (lo1 == hi1) /* empty first slice */
1203 1901 : return canditer_slice(ci, lo2, hi2);
1204 4761 : if (lo1 >= ci->ncand) /* out of range */
1205 : return BATdense(0, 0, 0);
1206 :
1207 4761 : if (hi2 >= ci->ncand)
1208 : hi2 = ci->ncand;
1209 :
1210 4761 : bn = COLnew(0, TYPE_oid, hi1 - lo1 + hi2 - lo2, TRANSIENT);
1211 4764 : if (bn == NULL)
1212 : return NULL;
1213 4764 : BATsetcount(bn, hi1 - lo1 + hi2 - lo2);
1214 4761 : bn->tsorted = true;
1215 4761 : bn->trevsorted = BATcount(bn) <= 1;
1216 4761 : bn->tkey = true;
1217 4761 : bn->tseqbase = oid_nil;
1218 4761 : bn->tnil = false;
1219 4761 : bn->tnonil = true;
1220 :
1221 4761 : oid *dst = Tloc(bn, 0);
1222 :
1223 4761 : 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 943207 : while (lo1 < hi1)
1230 940870 : *dst++ = ci->seq + lo1++;
1231 275693 : while (lo2 < hi2)
1232 273356 : *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 4761 : return virtualize(bn);
1277 : }
1278 :
1279 : BAT *
1280 42350 : canditer_slice2val(const struct canditer *ci, oid lo1, oid hi1, oid lo2, oid hi2)
1281 : {
1282 42350 : if (ci->tpe != cand_mask) {
1283 166182 : return canditer_slice2(
1284 : ci,
1285 39352 : is_oid_nil(lo1) ? 0 : canditer_search(ci, lo1, true),
1286 42153 : is_oid_nil(hi1) ? ci->ncand : canditer_search(ci, hi1, true),
1287 42147 : is_oid_nil(lo2) ? 0 : canditer_search(ci, lo2, true),
1288 357 : is_oid_nil(hi2) ? ci->ncand : canditer_search(ci, hi2, true));
1289 : }
1290 :
1291 207 : return canditer_sliceval_mask(ci, lo1, hi1, ci->ncand,
1292 207 : lo2, hi2, ci->ncand);
1293 : }
1294 :
1295 : BAT *
1296 3209 : BATnegcands2(oid tseq, BUN nr, BAT *odels)
1297 : {
1298 3209 : const char *nme;
1299 3209 : Heap *dels;
1300 3209 : BUN lo, hi;
1301 3209 : ccand_t *c;
1302 3209 : BAT *bn;
1303 :
1304 3209 : bn = BATdense(0, tseq, nr);
1305 3210 : if (bn == NULL)
1306 : return NULL;
1307 3210 : if (BATcount(odels) == 0)
1308 2785 : goto doreturn;
1309 :
1310 425 : lo = SORTfndfirst(odels, &bn->tseqbase);
1311 425 : hi = SORTfndfirst(odels, &(oid) {bn->tseqbase + BATcount(bn)});
1312 425 : if (lo == hi)
1313 : return bn;
1314 425 : if (lo + nr == hi) {
1315 135 : BATsetcount(bn, 0);
1316 136 : goto doreturn;
1317 : }
1318 :
1319 290 : nme = BBP_physical(bn->batCacheid);
1320 290 : if ((dels = GDKmalloc(sizeof(Heap))) == NULL){
1321 0 : BBPreclaim(bn);
1322 0 : return NULL;
1323 : }
1324 580 : *dels = (Heap) {
1325 290 : .farmid = BBPselectfarm(bn->batRole, bn->ttype, varheap),
1326 290 : .parentid = bn->batCacheid,
1327 : .dirty = true,
1328 : .refs = ATOMIC_VAR_INIT(1),
1329 : };
1330 290 : strconcat_len(dels->filename, sizeof(dels->filename),
1331 : nme, ".theap", NULL);
1332 :
1333 579 : if (dels->farmid < 0 ||
1334 289 : 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 290 : c = (ccand_t *) dels->base;
1340 290 : *c = (ccand_t) {
1341 : .type = CAND_NEGOID,
1342 : };
1343 290 : dels->free = sizeof(ccand_t) + sizeof(oid) * (hi - lo);
1344 290 : BATiter bi = bat_iterator(odels);
1345 289 : if (bi.type == TYPE_void) {
1346 165 : oid *r = (oid *) (dels->base + sizeof(ccand_t));
1347 93671 : for (BUN x = lo; x < hi; x++)
1348 93506 : r[x - lo] = x + odels->tseqbase;
1349 : } else {
1350 124 : oid *r = (oid *) (dels->base + sizeof(ccand_t));
1351 124 : memcpy(r, (const oid *) bi.base + lo, sizeof(oid) * (hi - lo));
1352 : }
1353 289 : bat_iterator_end(&bi);
1354 290 : assert(bn->tvheap == NULL);
1355 290 : bn->tvheap = dels;
1356 290 : BATsetcount(bn, bn->batCount - (hi - lo));
1357 3211 : doreturn:
1358 3211 : TRC_DEBUG(ALGO, "nr=" BUNFMT ", odels=" ALGOBATFMT
1359 : " -> " ALGOBATFMT "\n",
1360 : nr, ALGOBATPAR(odels),
1361 : ALGOBATPAR(bn));
1362 : return bn;
1363 : }
1364 :
1365 : BAT *
1366 0 : BATnegcands(BUN nr, BAT *odels)
1367 : {
1368 0 : return BATnegcands2(0, nr, odels);
1369 : }
1370 :
1371 : BAT *
1372 104971 : BATmaskedcands(oid hseq, BUN nr, BAT *masked, bool selected)
1373 : {
1374 104971 : const char *nme;
1375 104971 : Heap *msks;
1376 104971 : ccand_t *c;
1377 104971 : BUN nmask;
1378 104971 : BAT *bn;
1379 :
1380 104971 : assert(masked->ttype == TYPE_msk);
1381 :
1382 104971 : bn = COLnew(hseq, TYPE_void, 0, TRANSIENT);
1383 104972 : if (bn == NULL)
1384 : return NULL;
1385 104972 : BATtseqbase(bn, hseq);
1386 :
1387 104971 : if (BATcount(masked) == 0)
1388 : return bn;
1389 :
1390 104972 : nme = BBP_physical(bn->batCacheid);
1391 104972 : if ((msks = GDKmalloc(sizeof(Heap))) == NULL){
1392 0 : BBPreclaim(bn);
1393 0 : return NULL;
1394 : }
1395 209944 : *msks = (Heap) {
1396 104972 : .farmid = BBPselectfarm(bn->batRole, bn->ttype, varheap),
1397 104972 : .parentid = bn->batCacheid,
1398 : .dirty = true,
1399 : .refs = ATOMIC_VAR_INIT(1),
1400 : };
1401 104972 : strconcat_len(msks->filename, sizeof(msks->filename),
1402 : nme, ".theap", NULL);
1403 :
1404 104971 : nmask = (nr + 31) / 32;
1405 209942 : if (msks->farmid < 0 ||
1406 104971 : HEAPalloc(msks, nmask + (sizeof(ccand_t)/sizeof(uint32_t)), sizeof(uint32_t)) != GDK_SUCCEED) {
1407 0 : GDKfree(msks);
1408 0 : BBPreclaim(bn);
1409 0 : return NULL;
1410 : }
1411 104971 : c = (ccand_t *) msks->base;
1412 104971 : *c = (ccand_t) {
1413 : .type = CAND_MSK,
1414 : // .mask = true,
1415 : };
1416 104971 : msks->free = sizeof(ccand_t) + nmask * sizeof(uint32_t);
1417 104971 : uint32_t *r = (uint32_t*)(msks->base + sizeof(ccand_t));
1418 104971 : BATiter bi = bat_iterator(masked);
1419 104970 : if (selected) {
1420 104970 : if (nr <= bi.count)
1421 104970 : memcpy(r, bi.base, nmask * sizeof(uint32_t));
1422 : else
1423 0 : memcpy(r, bi.base, (bi.count + 31) / 32 * sizeof(uint32_t));
1424 : } else {
1425 0 : const uint32_t *s = (const uint32_t *) bi.base;
1426 0 : BUN nmask_ = (bi.count + 31) / 32;
1427 0 : for (BUN i = 0; i < nmask_; i++)
1428 0 : r[i] = ~s[i];
1429 : }
1430 104970 : if (nr > bi.count) {
1431 0 : BUN rest = bi.count & 31;
1432 0 : BUN nmask_ = (bi.count + 31) / 32;
1433 0 : if (rest > 0)
1434 0 : r[nmask_ -1] |= ((1U << (32 - rest)) - 1) << rest;
1435 0 : for (BUN j = nmask_; j < nmask; j++)
1436 0 : r[j] = ~0;
1437 : }
1438 104970 : bat_iterator_end(&bi);
1439 : /* make sure last word doesn't have any spurious bits set */
1440 104970 : BUN cnt = nr % 32;
1441 104970 : if (cnt > 0)
1442 102343 : r[nmask - 1] &= (1U << cnt) - 1;
1443 : cnt = 0;
1444 11402817 : for (BUN i = 0; i < nmask; i++) {
1445 11297847 : if (cnt == 0 && r[i] != 0)
1446 103713 : c->firstbit = candmask_lobit(r[i]) + i * 32;
1447 11297847 : cnt += candmask_pop(r[i]);
1448 : }
1449 104970 : if (cnt > 0) {
1450 103722 : assert(bn->tvheap == NULL);
1451 103722 : bn->tvheap = msks;
1452 103722 : bn->tseqbase += (oid) c->firstbit;
1453 : } else {
1454 : /* no point having a mask if it's empty */
1455 1248 : HEAPfree(msks, true);
1456 1250 : GDKfree(msks);
1457 : }
1458 104972 : BATsetcount(bn, cnt);
1459 104969 : TRC_DEBUG(ALGO, "hseq=" OIDFMT ", masked=" ALGOBATFMT ", selected=%s"
1460 : " -> " ALGOBATFMT "\n",
1461 : hseq, ALGOBATPAR(masked),
1462 : selected ? "true" : "false",
1463 : ALGOBATPAR(bn));
1464 104969 : assert(bn->tseqbase != oid_nil);
1465 : return bn;
1466 : }
1467 :
1468 : /* convert a masked candidate list to a positive or negative candidate list */
1469 : BAT *
1470 99274 : BATunmask(BAT *b)
1471 : {
1472 : // assert(!mask_cand(b) || CCAND(b)->mask); /* todo handle negmask case */
1473 99274 : BUN cnt;
1474 99274 : uint32_t rem;
1475 99274 : uint32_t val;
1476 99274 : const uint32_t *src;
1477 99274 : oid *dst;
1478 99274 : BUN n = 0;
1479 99274 : oid tseq = b->hseqbase;
1480 99274 : bool negcand = false;
1481 :
1482 99274 : BATiter bi = bat_iterator(b);
1483 99316 : if (mask_cand(b)) {
1484 95039 : cnt = ccand_free(b) / sizeof(uint32_t);
1485 95039 : rem = 0;
1486 95039 : src = (const uint32_t *) ccand_first(b);
1487 95039 : tseq = b->tseqbase;
1488 95039 : tseq -= (oid) CCAND(b)->firstbit;
1489 : /* create negative candidate list if more than half the
1490 : * bits are set */
1491 95039 : negcand = BATcount(b) > cnt * 16;
1492 : } else {
1493 4277 : cnt = bi.count / 32;
1494 4277 : rem = bi.count % 32;
1495 4277 : src = (const uint32_t *) bi.base;
1496 : }
1497 99316 : BAT *bn;
1498 :
1499 99316 : if (negcand) {
1500 77147 : bn = COLnew(b->hseqbase, TYPE_void, 0, TRANSIENT);
1501 77149 : if (bn == NULL) {
1502 0 : bat_iterator_end(&bi);
1503 0 : return NULL;
1504 : }
1505 77149 : Heap *dels;
1506 77149 : if ((dels = GDKmalloc(sizeof(Heap))) == NULL) {
1507 0 : BBPreclaim(bn);
1508 0 : bat_iterator_end(&bi);
1509 0 : return NULL;
1510 : }
1511 154287 : *dels = (Heap) {
1512 77146 : .farmid = BBPselectfarm(TRANSIENT, TYPE_void, varheap),
1513 77141 : .parentid = bn->batCacheid,
1514 : .dirty = true,
1515 : .refs = ATOMIC_VAR_INIT(1),
1516 : };
1517 77141 : strconcat_len(dels->filename, sizeof(dels->filename),
1518 77141 : BBP_physical(bn->batCacheid), ".theap", NULL);
1519 :
1520 154238 : if (dels->farmid < 0 ||
1521 77107 : HEAPalloc(dels, cnt * 32 - bi.count
1522 : + sizeof(ccand_t) / sizeof(oid), sizeof(oid)) != GDK_SUCCEED) {
1523 0 : GDKfree(dels);
1524 0 : BBPreclaim(bn);
1525 0 : bat_iterator_end(&bi);
1526 0 : return NULL;
1527 : }
1528 77131 : * (ccand_t *) dels->base = (ccand_t) {
1529 : .type = CAND_NEGOID,
1530 : };
1531 77131 : dst = (oid *) (dels->base + sizeof(ccand_t));
1532 7002478 : for (BUN p = 0, v = 0; p < cnt; p++, v += 32) {
1533 6925347 : if ((val = src[p]) == ~UINT32_C(0))
1534 5309898 : continue;
1535 52028472 : for (uint32_t i = 0; i < 32; i++) {
1536 50488769 : if ((val & (1U << i)) == 0) {
1537 44701640 : if (v + i >= b->batCount + n)
1538 : break;
1539 44625894 : dst[n++] = tseq + v + i;
1540 : }
1541 : }
1542 : }
1543 77131 : if (n == 0) {
1544 : /* didn't need it after all */
1545 28 : HEAPfree(dels, true);
1546 29 : GDKfree(dels);
1547 : } else {
1548 77103 : dels->free = sizeof(ccand_t) + n * sizeof(oid);
1549 77103 : dels->dirty = true;
1550 77103 : assert(bn->tvheap == NULL);
1551 77103 : bn->tvheap = dels;
1552 : }
1553 77132 : BATsetcount(bn, n=bi.count);
1554 77138 : bn->tseqbase = tseq;
1555 : } else {
1556 22169 : bn = COLnew(b->hseqbase, TYPE_oid, mask_cand(b) ? bi.count : 1024, TRANSIENT);
1557 22181 : if (bn == NULL) {
1558 0 : bat_iterator_end(&bi);
1559 0 : return NULL;
1560 : }
1561 22181 : dst = (oid *) Tloc(bn, 0);
1562 2676769 : for (BUN p = 0; p < cnt; p++) {
1563 2654587 : if ((val = src[p]) == 0)
1564 1881214 : continue;
1565 25502877 : for (uint32_t i = 0; i < 32; i++) {
1566 24729503 : if (val & (1U << i)) {
1567 21535236 : if (n == BATcapacity(bn)) {
1568 77 : BATsetcount(bn, n);
1569 78 : if (BATextend(bn, BATgrows(bn)) != GDK_SUCCEED) {
1570 0 : BBPreclaim(bn);
1571 0 : bat_iterator_end(&bi);
1572 0 : return NULL;
1573 : }
1574 78 : dst = (oid *) Tloc(bn, 0);
1575 : }
1576 21535237 : dst[n++] = tseq + p * 32 + i;
1577 : }
1578 : }
1579 : }
1580 : /* the last partial mask word */
1581 22182 : if (rem > 0 && (val = src[cnt]) != 0) {
1582 21274 : for (uint32_t i = 0; i < rem; i++) {
1583 19106 : if (val & (1U << i)) {
1584 1046 : if (n == BATcapacity(bn)) {
1585 0 : BATsetcount(bn, n);
1586 0 : if (BATextend(bn, BATgrows(bn)) != GDK_SUCCEED) {
1587 0 : BBPreclaim(bn);
1588 0 : bat_iterator_end(&bi);
1589 0 : return NULL;
1590 : }
1591 0 : dst = (oid *) Tloc(bn, 0);
1592 : }
1593 1046 : dst[n++] = tseq + cnt * 32 + i;
1594 : }
1595 : }
1596 : }
1597 22182 : BATsetcount(bn, n);
1598 : }
1599 99317 : bat_iterator_end(&bi);
1600 99332 : bn->tkey = true;
1601 99332 : bn->tsorted = true;
1602 99332 : bn->trevsorted = n <= 1;
1603 99332 : bn->tnil = false;
1604 99332 : bn->tnonil = true;
1605 99332 : bn = virtualize(bn);
1606 99316 : TRC_DEBUG(ALGO, ALGOBATFMT " -> " ALGOBATFMT "\n", ALGOBATPAR(b), ALGOBATPAR(bn));
1607 : return bn;
1608 : }
|