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 9929 : BATiscand(BAT *b)
20 : {
21 9929 : if (ATOMtype(b->ttype) != TYPE_oid)
22 : return false;
23 9929 : if (complex_cand(b))
24 : return true;
25 9598 : if (b->ttype == TYPE_void && is_oid_nil(b->tseqbase))
26 : return false;
27 19196 : 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 9357 : newdensecand(oid first, oid last)
34 : {
35 9357 : if (last <= first)
36 0 : first = last = 0; /* empty range */
37 9357 : 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 113111 : BATmergecand(BAT *a, BAT *b)
47 : {
48 113111 : BAT *bn;
49 113111 : oid *restrict p, i;
50 113111 : struct canditer cia, cib;
51 :
52 113111 : BATcheck(a, NULL);
53 113111 : BATcheck(b, NULL);
54 :
55 113111 : canditer_init(&cia, NULL, a);
56 113037 : canditer_init(&cib, NULL, b);
57 :
58 : /* we can return a if b is empty (and v.v.) */
59 113322 : if (cia.ncand == 0) {
60 36780 : bn = canditer_slice(&cib, 0, cib.ncand);
61 36743 : goto doreturn;
62 : }
63 76542 : if (cib.ncand == 0) {
64 18318 : bn = canditer_slice(&cia, 0, cia.ncand);
65 18344 : goto doreturn;
66 : }
67 : /* we can return a if a fully covers b (and v.v) */
68 58224 : if (cia.tpe == cand_dense && cib.tpe == cand_dense) {
69 : /* both are dense */
70 17474 : 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 6676 : bn = newdensecand(cia.seq, cia.seq + cia.ncand < cib.seq + cib.ncand ? cib.seq + cib.ncand : cia.seq + cia.ncand);
74 6680 : goto doreturn;
75 : }
76 10798 : 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 2690 : bn = newdensecand(cib.seq, cia.seq + cia.ncand < cib.seq + cib.ncand ? cib.seq + cib.ncand : cia.seq + cia.ncand);
80 2691 : goto doreturn;
81 : }
82 : }
83 48858 : if (cia.tpe == cand_dense
84 10463 : && cia.seq <= cib.seq
85 4577 : && canditer_last(&cia) >= canditer_last(&cib)) {
86 220 : bn = canditer_slice(&cia, 0, cia.ncand);
87 220 : goto doreturn;
88 : }
89 48638 : if (cib.tpe == cand_dense
90 35715 : && cib.seq <= cia.seq
91 10318 : && canditer_last(&cib) >= canditer_last(&cia)) {
92 170 : bn = canditer_slice(&cib, 0, cib.ncand);
93 170 : goto doreturn;
94 : }
95 :
96 48473 : bn = COLnew(0, TYPE_oid, cia.ncand + cib.ncand, TRANSIENT);
97 48426 : if (bn == NULL)
98 0 : goto doreturn;
99 48426 : p = (oid *) Tloc(bn, 0);
100 48426 : if (cia.tpe == cand_dense && cib.tpe == cand_dense) {
101 : /* both lists are dense */
102 8111 : if (cia.seq > cib.seq) {
103 4226 : struct canditer ci;
104 4226 : ci = cia;
105 4226 : cia = cib;
106 4226 : cib = ci;
107 : }
108 : /* cia completely before cib */
109 8111 : assert(cia.seq + cia.ncand < cib.seq);
110 30932 : for (i = cia.seq; i < cia.seq + cia.ncand; i++)
111 22821 : *p++ = i;
112 : /* there is a gap */
113 27294 : for (i = cib.seq; i < cib.seq + cib.ncand; i++)
114 19183 : *p++ = i;
115 40315 : } else if (cia.tpe == cand_dense || cib.tpe == cand_dense) {
116 29533 : if (cib.tpe == cand_dense) {
117 27462 : struct canditer ci;
118 27462 : ci = cia;
119 27462 : cia = cib;
120 27462 : cib = ci;
121 : }
122 : /* only cia is dense */
123 :
124 : /* copy part of cib that's before the start of cia */
125 121830 : while (canditer_peek(&cib) < cia.seq) {
126 92289 : *p++ = canditer_next(&cib);
127 : }
128 : /* copy all of cia */
129 66827 : for (i = cia.seq; i < cia.seq + cia.ncand; i++)
130 37221 : *p++ = i;
131 : /* skip over part of cib that overlaps with cia */
132 29606 : canditer_setidx(&cib, canditer_search(&cib, cia.seq + cia.ncand, true));
133 : /* copy rest of cib */
134 115368 : while (cib.next < cib.ncand) {
135 85756 : *p++ = canditer_next(&cib);
136 : }
137 : } else {
138 : /* a and b are both not dense */
139 10782 : oid ao = canditer_next(&cia);
140 10776 : oid bo = canditer_next(&cib);
141 22555298 : while (!is_oid_nil(ao) && !is_oid_nil(bo)) {
142 22544512 : if (ao < bo) {
143 13050053 : *p++ = ao;
144 13050053 : ao = canditer_next(&cia);
145 9494459 : } else if (bo < ao) {
146 8460998 : *p++ = bo;
147 8460998 : bo = canditer_next(&cib);
148 : } else {
149 1033461 : *p++ = ao;
150 1033461 : ao = canditer_next(&cia);
151 1033463 : bo = canditer_next(&cib);
152 : }
153 : }
154 550658 : while (!is_oid_nil(ao)) {
155 539870 : *p++ = ao;
156 539870 : ao = canditer_next(&cia);
157 : }
158 414619 : while (!is_oid_nil(bo)) {
159 403830 : *p++ = bo;
160 403830 : bo = canditer_next(&cib);
161 : }
162 : }
163 :
164 : /* properties */
165 48492 : BATsetcount(bn, (BUN) (p - (oid *) Tloc(bn, 0)));
166 48469 : bn->trevsorted = BATcount(bn) <= 1;
167 48469 : bn->tsorted = true;
168 48469 : bn->tkey = true;
169 48469 : bn->tnil = false;
170 48469 : bn->tnonil = true;
171 48469 : bn = virtualize(bn);
172 113313 : doreturn:
173 113313 : 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 10 : while (!is_oid_nil(bo = canditer_next(&cib)) && bo < cia.seq + cia.ncand)
224 8 : *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 0 : BATdiffcand(BAT *a, BAT *b)
260 : {
261 0 : BAT *bn;
262 0 : oid *restrict p;
263 0 : struct canditer cia, cib;
264 :
265 0 : BATcheck(a, NULL);
266 0 : BATcheck(b, NULL);
267 :
268 0 : canditer_init(&cia, NULL, a);
269 0 : canditer_init(&cib, NULL, b);
270 :
271 0 : if (cia.ncand == 0) {
272 0 : bn = BATdense(0, 0, 0);
273 0 : goto doreturn;
274 : }
275 0 : if (cib.ncand == 0) {
276 0 : bn = canditer_slice(&cia, 0, cia.ncand);
277 0 : goto doreturn;
278 : }
279 :
280 0 : 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 0 : if (cia.tpe == cand_dense && cib.tpe == cand_dense) {
287 : /* both a and b are dense */
288 0 : if (cia.seq < cib.seq) {
289 : /* a starts before b */
290 0 : if (cia.seq + cia.ncand <= cib.seq + cib.ncand) {
291 : /* b overlaps with end of a */
292 0 : bn = canditer_slice(&cia, 0, cib.seq - cia.seq);
293 0 : goto doreturn;
294 : }
295 : /* b is subset of a */
296 0 : bn = canditer_slice2(&cia, 0, cib.seq - cia.seq,
297 : cib.seq + cib.ncand - cia.seq,
298 : cia.ncand);
299 0 : goto doreturn;
300 : } else {
301 : /* cia.seq >= cib.seq */
302 0 : if (cia.seq + cia.ncand > cib.seq + cib.ncand) {
303 : /* b overlaps with beginning of a */
304 0 : bn = canditer_slice(&cia, cib.seq + cib.ncand - cia.seq, cia.ncand);
305 0 : goto doreturn;
306 : }
307 : /* a is subset f b */
308 0 : bn = BATdense(0, 0, 0);
309 0 : goto doreturn;
310 : }
311 : }
312 0 : 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 0 : bn = canditer_slice2val(&cia, oid_nil, cib.seq,
317 0 : cib.seq + cib.ncand, oid_nil);
318 0 : goto doreturn;
319 : }
320 :
321 : /* b is not dense */
322 0 : bn = COLnew(0, TYPE_oid, BATcount(a), TRANSIENT);
323 0 : if (bn == NULL)
324 0 : goto doreturn;
325 0 : p = Tloc(bn, 0);
326 : /* find first position in b that is in range of a */
327 0 : 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 0 : oid ob = canditer_next(&cib);
332 0 : for (BUN i = 0; i < cia.ncand; i++) {
333 0 : oid oa = canditer_next(&cia);
334 0 : while (!is_oid_nil(ob) && ob < oa) {
335 0 : ob = canditer_next(&cib);
336 : }
337 0 : if (is_oid_nil(ob) || oa < ob)
338 0 : *p++ = oa;
339 : }
340 : }
341 :
342 : /* properties */
343 0 : BATsetcount(bn, (BUN) (p - (oid *) Tloc(bn, 0)));
344 0 : bn->trevsorted = BATcount(bn) <= 1;
345 0 : bn->tsorted = true;
346 0 : bn->tkey = true;
347 0 : bn->tnil = false;
348 0 : bn->tnonil = true;
349 0 : bn = virtualize(bn);
350 0 : doreturn:
351 0 : 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 980490 : binsearchcand(const oid *cand, BUN hi, oid o)
359 : {
360 980490 : BUN lo = 0;
361 :
362 980490 : if (o <= cand[lo])
363 : return 0;
364 492532 : if (o > cand[hi])
365 415706 : return hi + 1;
366 : /* loop invariant: cand[lo] < o <= cand[hi] */
367 542513 : while (hi > lo + 1) {
368 482606 : BUN mid = (lo + hi) / 2;
369 482606 : if (cand[mid] == o)
370 16919 : return mid;
371 465687 : if (cand[mid] < o)
372 : lo = mid;
373 : else
374 326902 : 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 23790 : count_mask_bits(const struct canditer *ci, BUN lo, BUN hi)
383 : {
384 23790 : BUN n;
385 23790 : assert(lo <= hi);
386 23790 : assert(ci->tpe == cand_mask);
387 23790 : if (lo == hi)
388 : return 0;
389 23790 : lo += ci->firstbit;
390 23790 : hi += ci->firstbit;
391 23790 : BUN loi = lo / 32;
392 23790 : BUN hii = hi / 32;
393 23790 : lo %= 32;
394 23790 : hi %= 32;
395 23790 : if (loi == hii)
396 5930 : return (BUN) candmask_pop((ci->mask[loi] & ((1U << hi) - 1)) >> lo);
397 17860 : n = (BUN) candmask_pop(ci->mask[loi++] >> lo);
398 3289319 : while (loi < hii)
399 3271459 : n += (BUN) candmask_pop(ci->mask[loi++]);
400 17860 : if (hi != 0)
401 3431 : n += (BUN) candmask_pop(ci->mask[loi] & ((1U << hi) - 1));
402 : return n;
403 : }
404 :
405 : /* initialize a candidate iterator */
406 : void
407 9558037 : canditer_init(struct canditer *ci, BAT *b, BAT *s)
408 : {
409 9558037 : assert(ci != NULL);
410 9558037 : BUN batcount = 0;
411 9558037 : oid hseq = 0;
412 9558037 : if (b) {
413 8730610 : MT_lock_set(&b->theaplock);
414 8746971 : batcount = BATcount(b);
415 8746971 : hseq = b->hseqbase;
416 8746971 : MT_lock_unset(&b->theaplock);
417 : }
418 :
419 9581689 : if (s == NULL) {
420 5189103 : 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 5189103 : *ci = (struct canditer) {
429 : .tpe = cand_dense,
430 : .seq = hseq,
431 : .hseq = hseq,
432 : .ncand = batcount,
433 : };
434 5189103 : return;
435 : }
436 :
437 4392586 : assert(ATOMtype(s->ttype) == TYPE_oid || s->ttype == TYPE_msk);
438 4392586 : assert(s->ttype == TYPE_msk|| s->tsorted);
439 4392586 : assert(s->ttype == TYPE_msk|| s->tkey);
440 4392586 : assert(s->ttype == TYPE_msk|| s->tnonil);
441 4392586 : assert(s->ttype == TYPE_void || s->tvheap == NULL);
442 :
443 4392586 : BUN cnt = BATcount(s);
444 :
445 4392586 : if (cnt == 0 || (b != NULL && batcount == 0)) {
446 : /* candidate list for empty BAT or empty candidate list */
447 1140428 : *ci = (struct canditer) {
448 : .tpe = cand_dense,
449 1140428 : .hseq = s->hseqbase,
450 : .s = s,
451 : };
452 1140428 : return;
453 : }
454 :
455 3252158 : *ci = (struct canditer) {
456 3252158 : .seq = s->tseqbase,
457 3252158 : .hseq = s->hseqbase,
458 : .s = s,
459 : };
460 :
461 3252158 : if (mask_cand(s)) {
462 23823 : ci->tpe = cand_mask;
463 23823 : ci->mask = (const uint32_t *) ccand_first(s);
464 23823 : ci->seq = s->tseqbase - (oid) CCAND(s)->firstbit;
465 23823 : ci->hseq = s->hseqbase;
466 23823 : ci->nvals = ccand_free(s) / sizeof(uint32_t);
467 23823 : cnt = ci->nvals * 32;
468 3228335 : } 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 3228335 : } else if (s->ttype == TYPE_void) {
475 2715842 : assert(!is_oid_nil(ci->seq));
476 2715842 : if (s->tvheap) {
477 88730 : assert(ccand_free(s) % SIZEOF_OID == 0);
478 88730 : ci->nvals = ccand_free(s) / SIZEOF_OID;
479 88730 : if (ci->nvals > 0) {
480 88709 : ci->tpe = cand_except;
481 88709 : 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 512493 : } else if (is_oid_nil(ci->seq)) {
490 480888 : ci->tpe = cand_materialized;
491 480888 : ci->oids = (const oid *) Tloc(s, 0);
492 480888 : ci->seq = ci->oids[0];
493 480888 : ci->nvals = cnt;
494 : } else {
495 : /* materialized dense: no exceptions */
496 : ci->tpe = cand_dense;
497 : }
498 3252158 : switch (ci->tpe) {
499 481066 : case cand_materialized:
500 481066 : if (b != NULL) {
501 396221 : BUN p = binsearchcand(ci->oids, cnt - 1U, hseq);
502 : /* p == cnt means candidate list is completely
503 : * before b */
504 396231 : ci->offset = p;
505 396231 : ci->oids += p;
506 396231 : cnt -= p;
507 396231 : if (cnt > 0) {
508 396209 : cnt = binsearchcand(ci->oids, cnt - 1U,
509 : hseq + batcount);
510 : /* cnt == 0 means candidate list is
511 : * completely after b */
512 : }
513 396204 : if (cnt == 0) {
514 : /* no overlap */
515 26 : *ci = (struct canditer) {
516 : .tpe = cand_dense,
517 : .s = s,
518 : };
519 26 : return;
520 : }
521 396200 : ci->seq = ci->oids[0];
522 396200 : ci->nvals = cnt;
523 396200 : if (ci->oids[cnt - 1U] - ci->seq == cnt - 1U) {
524 : /* actually dense */
525 75 : ci->tpe = cand_dense;
526 75 : ci->oids = NULL;
527 75 : ci->nvals = 0;
528 : }
529 : }
530 : break;
531 88718 : case cand_except:
532 : /* exceptions must all be within range of s */
533 88718 : assert(ci->oids[0] >= ci->seq);
534 88718 : assert(ci->oids[ci->nvals - 1U] < ci->seq + cnt + ci->nvals);
535 : /* prune exceptions at either end of range of s */
536 4026859 : while (ci->nvals > 0 && ci->oids[0] == ci->seq) {
537 3938141 : ci->nvals--;
538 3938141 : ci->oids++;
539 3938141 : ci->seq++;
540 : }
541 88718 : while (ci->nvals > 0 &&
542 87728 : ci->oids[ci->nvals - 1U] == ci->seq + cnt + ci->nvals - 1U)
543 0 : ci->nvals--;
544 88718 : if (b != NULL) {
545 76485 : if (ci->seq + cnt + ci->nvals <= hseq ||
546 76485 : 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 88718 : if (ci->nvals > 0) {
556 87718 : if (b == NULL)
557 : break;
558 75593 : BUN p;
559 75593 : p = binsearchcand(ci->oids, ci->nvals - 1U, hseq);
560 75593 : 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 75593 : assert(hseq > ci->seq || p == 0);
571 75593 : 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 75593 : 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 75593 : while (ci->nvals > 0 && ci->oids[0] == ci->seq) {
588 0 : ci->nvals--;
589 0 : ci->oids++;
590 0 : ci->seq++;
591 : }
592 75593 : while (ci->nvals > 0 &&
593 75593 : ci->oids[ci->nvals - 1U] == ci->seq + cnt + ci->nvals - 1U)
594 0 : ci->nvals--;
595 75593 : if (ci->nvals > 0)
596 : break;
597 : }
598 1000 : ci->tpe = cand_dense;
599 1000 : ci->oids = NULL;
600 : /* fall through */
601 2659500 : case cand_dense:
602 2659500 : if (b != NULL) {
603 2313143 : if (ci->seq + cnt <= hseq ||
604 2313143 : 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 2313143 : if (hseq > ci->seq) {
613 139 : cnt -= hseq - ci->seq;
614 139 : ci->offset += hseq - ci->seq;
615 139 : ci->seq = hseq;
616 : }
617 2313143 : if (ci->seq + cnt > hseq + batcount)
618 137 : cnt = hseq + batcount - ci->seq;
619 : }
620 : break;
621 23805 : case cand_mask:
622 23805 : assert(s->tseqbase != oid_nil);
623 23805 : if (b != NULL) {
624 9583 : if (ci->seq + cnt <= hseq ||
625 9583 : 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 9583 : 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 9583 : if (ci->seq + cnt > hseq + batcount) {
641 9571 : cnt = hseq + batcount - ci->seq;
642 : }
643 9583 : 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 23805 : 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 23805 : if (ci->firstbit == 0) {
660 53426 : while (cnt >= 32U && ci->mask[0] == 0) {
661 29620 : cnt -= 32U;
662 29620 : ci->mask++;
663 29620 : ci->nvals--;
664 : }
665 : }
666 : /* check whether there are any 1 bits in the last word */
667 23805 : if (cnt == 0 ||
668 23805 : (cnt < 32U - ci->firstbit &&
669 5942 : ((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 23805 : int i = candmask_lobit(ci->mask[0] >> ci->firstbit);
681 23805 : assert(i >= 0); /* there should be a set bit */
682 23805 : ci->firstbit += i;
683 23805 : cnt -= i;
684 23805 : if (mask_cand(s))
685 23801 : ci->mskoff = s->tseqbase - (oid) CCAND(s)->firstbit + (ci->mask - (const uint32_t *) ccand_first(s)) * 32U;
686 : else
687 4 : ci->mskoff = s->tseqbase + (ci->mask - (const uint32_t *) s->theap->base) * 32U;
688 23805 : ci->seq = ci->mskoff + ci->firstbit;
689 23805 : ci->nextbit = ci->firstbit;
690 : /* at this point we know that bit ci->firstbit is set
691 : * in ci->mask[0] */
692 23805 : ci->lastbit = (ci->firstbit + cnt - 1U) % 32U + 1U;
693 23805 : if (ci->lastbit < 32 &&
694 9567 : (ci->mask[ci->nvals - 1] & ((1U << ci->lastbit) - 1)) == 0) {
695 : /* last partial word is all zero */
696 197 : cnt -= ci->lastbit;
697 197 : ci->lastbit = 32;
698 197 : ci->nvals--;
699 : }
700 23805 : if (ci->lastbit == 32) {
701 : /* "remove" zero words at the end */
702 18027 : while (cnt >= 32 && ci->mask[ci->nvals - 1] == 0) {
703 3590 : ci->nvals--;
704 3590 : cnt -= 32;
705 : }
706 : }
707 23805 : ci->ncand = count_mask_bits(ci, 0, cnt);
708 23797 : return;
709 : }
710 3228332 : ci->ncand = cnt;
711 3228332 : ci->hseq += ci->offset;
712 : }
713 :
714 : /* return the next candidate without advancing */
715 : oid
716 2764012 : canditer_peek(struct canditer *ci)
717 : {
718 2764012 : oid o = oid_nil;
719 2764012 : if (ci->next == ci->ncand)
720 : return oid_nil;
721 2758441 : switch (ci->tpe) {
722 2481719 : case cand_dense:
723 2481719 : o = ci->seq + ci->next;
724 2481719 : break;
725 272752 : case cand_materialized:
726 272752 : assert(ci->next < ci->nvals);
727 272752 : o = ci->oids[ci->next];
728 272752 : break;
729 3970 : case cand_except:
730 3970 : o = ci->seq + ci->add + ci->next;
731 3976 : 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 1552 : canditer_prev(struct canditer *ci)
751 : {
752 1552 : if (ci->next == 0)
753 0 : return oid_nil;
754 1552 : switch (ci->tpe) {
755 1552 : case cand_dense:
756 1552 : 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 3929 : canditer_peekprev(struct canditer *ci)
785 : {
786 3929 : oid o = oid_nil;
787 :
788 3929 : if (ci->next == 0)
789 : return oid_nil;
790 3929 : switch (ci->tpe) {
791 3929 : case cand_dense:
792 3929 : 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 12568 : canditer_mask_next(const struct canditer *ci, oid o, bool next)
825 : {
826 12568 : if (o < ci->mskoff)
827 0 : return next ? ci->mskoff + ci->firstbit : oid_nil;
828 12568 : o -= ci->mskoff;
829 12568 : BUN p = o / 32;
830 12568 : o %= 32;
831 12568 : if (p >= ci->nvals || (p == ci->nvals - 1 && o >= ci->lastbit))
832 0 : return next ? oid_nil : canditer_last(ci);
833 12568 : if (next) {
834 12658 : while ((ci->mask[p] & (1U << o)) == 0) {
835 90 : if (++o == 32) {
836 2 : o = 0;
837 2 : if (++p == ci->nvals)
838 0 : return oid_nil;
839 : }
840 : }
841 12568 : 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 : } else {
850 0 : o--;
851 : }
852 : }
853 0 : if (p == 0 && o < ci->firstbit)
854 0 : return oid_nil;
855 : }
856 12568 : return ci->mskoff + 32 * p + o;
857 : }
858 :
859 : /* return the last candidate */
860 : oid
861 1312990 : canditer_last(const struct canditer *ci)
862 : {
863 1312990 : if (ci->ncand == 0)
864 0 : return oid_nil;
865 1312990 : switch (ci->tpe) {
866 989114 : case cand_dense:
867 989114 : return ci->seq + ci->ncand - 1;
868 250168 : case cand_materialized:
869 250168 : return ci->oids[ci->ncand - 1];
870 71635 : case cand_except:
871 71635 : return ci->seq + ci->ncand + ci->nvals - 1;
872 2071 : case cand_mask:
873 4124 : for (uint8_t i = ci->lastbit; i > 0; ) {
874 4126 : if (ci->mask[ci->nvals - 1] & (1U << --i))
875 2073 : return ci->mskoff + (ci->nvals - 1) * 32 + i;
876 : }
877 : break; /* cannot happen */
878 : }
879 0 : return oid_nil; /* cannot happen */
880 : }
881 :
882 : /* return the candidate at the given index */
883 : oid
884 321332155 : canditer_idx(const struct canditer *ci, BUN p)
885 : {
886 321332155 : if (p >= ci->ncand)
887 0 : return oid_nil;
888 321332155 : switch (ci->tpe) {
889 169860786 : case cand_dense:
890 169860786 : return ci->seq + p;
891 151257729 : case cand_materialized:
892 151257729 : return ci->oids[p];
893 213565 : case cand_except: {
894 213565 : oid o = ci->seq + p;
895 213565 : if (o < ci->oids[0])
896 : return o;
897 138695 : if (o + ci->nvals > ci->oids[ci->nvals - 1])
898 : return o + ci->nvals;
899 83990 : BUN lo = 0, hi = ci->nvals - 1;
900 542969 : while (hi - lo > 1) {
901 374989 : BUN mid = (hi + lo) / 2;
902 374989 : if (ci->oids[mid] - mid > o)
903 : hi = mid;
904 : else
905 256595 : lo = mid;
906 : }
907 83990 : return o + hi;
908 : }
909 75 : case cand_mask: {
910 75 : BUN x;
911 75 : if ((x = candmask_pop(ci->mask[0] >> ci->firstbit)) > p) {
912 24 : for (uint8_t i = ci->firstbit; ; i++) {
913 99 : if (ci->mask[0] & (1U << i)) {
914 99 : if (p == 0)
915 75 : return ci->mskoff + i;
916 24 : p--;
917 : }
918 : }
919 : }
920 0 : for (BUN n = 1; n < ci->nvals; n++) {
921 0 : uint32_t mask = ci->mask[n];
922 0 : p -= x;
923 0 : x = candmask_pop(mask);
924 0 : if (x > p) {
925 0 : for (uint8_t i = 0; ; i++) {
926 0 : if (mask & (1U << i)) {
927 0 : if (p == 0)
928 0 : return ci->mskoff + n * 32 + i;
929 0 : p--;
930 : }
931 : }
932 : }
933 : }
934 : break; /* cannot happen */
935 : }
936 : }
937 0 : return oid_nil; /* cannot happen */
938 : }
939 :
940 : /* set the index for the next candidate to be returned */
941 : void
942 683359 : canditer_setidx(struct canditer *ci, BUN p)
943 : {
944 683359 : if (p != ci->next) {
945 636344 : if (p >= ci->ncand) {
946 68338 : ci->next = ci->ncand;
947 68338 : switch (ci->tpe) {
948 0 : case cand_except:
949 0 : ci->add = ci->nvals;
950 0 : break;
951 0 : case cand_mask:
952 0 : ci->nextbit = ci->lastbit;
953 0 : ci->nextmsk = ci->nvals;
954 0 : if (ci->nextbit == 32)
955 0 : ci->nextbit = 0;
956 : else
957 0 : ci->nextmsk--;
958 : break;
959 : default:
960 : break;
961 : }
962 : } else {
963 568006 : ci->next = p;
964 568006 : switch (ci->tpe) {
965 2286 : case cand_except:
966 2286 : ci->add = canditer_idx(ci, p) - ci->seq - p;
967 2286 : break;
968 0 : case cand_mask: {
969 0 : oid o = canditer_idx(ci, p) - ci->mskoff;
970 0 : ci->nextmsk = o / 32;
971 0 : ci->nextbit = (uint8_t) (o % 32);
972 0 : break;
973 : }
974 : default:
975 : break;
976 : }
977 : }
978 : }
979 683359 : }
980 :
981 : /* reset */
982 : void
983 972092 : canditer_reset(struct canditer *ci)
984 : {
985 972092 : if (ci->tpe == cand_mask) {
986 0 : ci->nextbit = ci->firstbit;
987 0 : ci->nextmsk = 0;
988 : } else {
989 972092 : ci->add = 0;
990 : }
991 972092 : ci->next = 0;
992 972092 : }
993 :
994 : /* return index of given candidate if it occurs; if the candidate does
995 : * not occur, if next is set, return index of next larger candidate,
996 : * if next is not set, return BUN_NONE */
997 : BUN
998 1414812 : canditer_search(const struct canditer *ci, oid o, bool next)
999 : {
1000 1414812 : BUN p;
1001 :
1002 1414812 : switch (ci->tpe) {
1003 1301524 : case cand_dense:
1004 1301524 : if (o < ci->seq)
1005 1087 : return next ? 0 : BUN_NONE;
1006 1300437 : if (o >= ci->seq + ci->ncand)
1007 352021 : return next ? ci->ncand : BUN_NONE;
1008 948416 : return o - ci->seq;
1009 40101 : case cand_materialized:
1010 40101 : if (ci->nvals == 0)
1011 : return 0;
1012 40098 : p = binsearchcand(ci->oids, ci->nvals - 1, o);
1013 40130 : if (next || (p != ci->nvals && ci->oids[p] == o))
1014 39458 : return p;
1015 : break;
1016 73187 : case cand_except:
1017 73187 : if (o < ci->seq)
1018 0 : return next ? 0 : BUN_NONE;
1019 73187 : if (o >= ci->seq + ci->ncand + ci->nvals)
1020 876 : return next ? ci->ncand : BUN_NONE;
1021 72311 : p = binsearchcand(ci->oids, ci->nvals - 1, o);
1022 72311 : if (next || p == ci->nvals || ci->oids[p] != o)
1023 64991 : return o - ci->seq - p;
1024 : break;
1025 0 : case cand_mask:
1026 0 : if (o < ci->mskoff) {
1027 0 : return next ? 0 : BUN_NONE;
1028 : }
1029 0 : o -= ci->mskoff;
1030 0 : p = o / 32;
1031 0 : if (p >= ci->nvals)
1032 0 : return next ? ci->ncand : BUN_NONE;
1033 0 : o %= 32;
1034 0 : if (p == ci->nvals - 1 && o >= ci->lastbit)
1035 0 : return next ? ci->ncand : BUN_NONE;
1036 0 : if (next || ci->mask[p] & (1U << o))
1037 0 : return count_mask_bits(ci, 0, p * 32 + o) + !(ci->mask[p] & (1U << o));
1038 : break;
1039 : }
1040 : return BUN_NONE;
1041 : }
1042 :
1043 : static BAT *
1044 1408 : canditer_sliceval_mask(const struct canditer *ci, oid lo1, oid hi1, BUN cnt1,
1045 : oid lo2, oid hi2, BUN cnt2)
1046 : {
1047 1408 : assert(cnt2 == 0 || !is_oid_nil(lo2));
1048 1408 : assert(cnt2 == 0 || lo2 >= hi1);
1049 1408 : if (is_oid_nil(lo1) || lo1 < ci->mskoff + ci->firstbit)
1050 323 : lo1 = ci->mskoff + ci->firstbit;
1051 1408 : if (is_oid_nil(hi1) || hi1 >= ci->mskoff + (ci->nvals - 1) * 32 + ci->lastbit)
1052 792 : hi1 = ci->mskoff + (ci->nvals - 1) * 32 + ci->lastbit;
1053 :
1054 1408 : BAT *bn = COLnew(0, TYPE_oid, cnt1 + cnt2, TRANSIENT);
1055 1408 : if (bn == NULL)
1056 : return NULL;
1057 1408 : bn->tkey = true;
1058 :
1059 1408 : if (hi1 > ci->mskoff) {
1060 850 : if (lo1 < ci->mskoff)
1061 : lo1 = 0;
1062 : else
1063 850 : lo1 -= ci->mskoff;
1064 850 : hi1 -= ci->mskoff;
1065 6813 : for (oid o = lo1; o < hi1 && cnt1 > 0; o++) {
1066 5964 : if (ci->mask[o / 32] & (1U << (o % 32))) {
1067 5132 : if (BUNappend(bn, &(oid){o + ci->mskoff}, false) != GDK_SUCCEED) {
1068 0 : BBPreclaim(bn);
1069 0 : return NULL;
1070 : }
1071 5131 : cnt1--;
1072 : }
1073 : }
1074 : }
1075 1407 : if (cnt2 > 0) {
1076 66 : if (lo2 < ci->mskoff + ci->firstbit)
1077 : lo2 = ci->mskoff + ci->firstbit;
1078 66 : if (is_oid_nil(hi2) || hi2 >= ci->mskoff + (ci->nvals - 1) * 32 + ci->lastbit)
1079 66 : hi2 = ci->mskoff + (ci->nvals - 1) * 32 + ci->lastbit;
1080 :
1081 66 : lo2 -= ci->mskoff;
1082 66 : hi2 -= ci->mskoff;
1083 195 : for (oid o = lo2; o < hi2 && cnt2 > 0; o++) {
1084 129 : if (ci->mask[o / 32] & (1U << (o % 32))) {
1085 96 : if (BUNappend(bn, &(oid){o + ci->mskoff}, false) != GDK_SUCCEED) {
1086 0 : BBPreclaim(bn);
1087 0 : return NULL;
1088 : }
1089 96 : cnt2--;
1090 : }
1091 : }
1092 : }
1093 : return bn;
1094 : }
1095 :
1096 : /* return either an actual BATslice or a new BAT that contains the
1097 : * "virtual" slice of the input candidate list BAT; except, unlike
1098 : * BATslice, the hseqbase of the returned BAT is 0; note for cand_mask
1099 : * candidate iterators, the BUN values refer to number of 1 bits */
1100 : BAT *
1101 651397 : canditer_slice(const struct canditer *ci, BUN lo, BUN hi)
1102 : {
1103 651397 : BAT *bn;
1104 651397 : oid o;
1105 651397 : BUN add;
1106 :
1107 651397 : assert(ci != NULL);
1108 :
1109 651397 : if (lo >= ci->ncand || lo >= hi)
1110 212339 : return BATdense(0, 0, 0);
1111 439058 : if (hi > ci->ncand)
1112 : hi = ci->ncand;
1113 439058 : if (hi - lo == 1)
1114 363197 : return BATdense(0, canditer_idx(ci, lo), 1);
1115 75861 : switch (ci->tpe) {
1116 3774 : case cand_materialized:
1117 3774 : if (ci->s) {
1118 3774 : bn = BATslice(ci->s, lo + ci->offset, hi + ci->offset);
1119 3774 : BAThseqbase(bn, 0);
1120 3774 : return bn;
1121 : }
1122 0 : bn = COLnew(0, TYPE_oid, hi - lo, TRANSIENT);
1123 0 : if (bn == NULL)
1124 : return NULL;
1125 0 : BATsetcount(bn, hi - lo);
1126 0 : memcpy(Tloc(bn, 0), ci->oids + lo, (hi - lo) * sizeof(oid));
1127 0 : break;
1128 71800 : default: /* really: case cand_dense: */
1129 71800 : return BATdense(0, ci->seq + lo, hi - lo);
1130 284 : case cand_except:
1131 284 : o = canditer_idx(ci, lo);
1132 284 : add = o - ci->seq - lo;
1133 284 : assert(add <= ci->nvals);
1134 284 : if (add == ci->nvals || o + hi - lo < ci->oids[add]) {
1135 : /* after last exception or before next
1136 : * exception: return dense sequence */
1137 248 : return BATdense(0, o, hi - lo);
1138 : }
1139 36 : bn = COLnew(0, TYPE_oid, hi - lo, TRANSIENT);
1140 36 : if (bn == NULL)
1141 : return NULL;
1142 36 : BATsetcount(bn, hi - lo);
1143 265 : for (oid *dst = Tloc(bn, 0); lo < hi; lo++) {
1144 289 : while (add < ci->nvals && o == ci->oids[add]) {
1145 60 : o++;
1146 60 : add++;
1147 : }
1148 229 : *dst++ = o++;
1149 : }
1150 : break;
1151 3 : case cand_mask:
1152 3 : return canditer_sliceval_mask(ci, canditer_idx(ci, lo),
1153 : oid_nil, hi - lo,
1154 : oid_nil, oid_nil, 0);
1155 : }
1156 36 : bn->tsorted = true;
1157 36 : bn->trevsorted = BATcount(bn) <= 1;
1158 36 : bn->tkey = true;
1159 36 : bn->tseqbase = oid_nil;
1160 36 : bn->tnil = false;
1161 36 : bn->tnonil = true;
1162 36 : bn->tminpos = 0;
1163 36 : bn->tmaxpos = BATcount(bn) - 1;
1164 36 : return virtualize(bn);
1165 : }
1166 :
1167 : /* like the above, except the bounds are given by values instead of
1168 : * indexes */
1169 : BAT *
1170 477199 : canditer_sliceval(const struct canditer *ci, oid lo, oid hi)
1171 : {
1172 477199 : if (ci->tpe != cand_mask) {
1173 1427685 : return canditer_slice(
1174 : ci,
1175 475779 : is_oid_nil(lo) ? 0 : canditer_search(ci, lo, true),
1176 475860 : is_oid_nil(hi) ? ci->ncand : canditer_search(ci, hi, true));
1177 : }
1178 :
1179 1339 : return canditer_sliceval_mask(ci, lo, hi, ci->ncand,
1180 : oid_nil, oid_nil, 0);
1181 : }
1182 :
1183 : /* return the combination of two slices */
1184 : BAT *
1185 28208 : canditer_slice2(const struct canditer *ci, BUN lo1, BUN hi1, BUN lo2, BUN hi2)
1186 : {
1187 28208 : BAT *bn;
1188 28208 : oid o;
1189 28208 : BUN add;
1190 :
1191 28208 : assert(lo1 <= hi1);
1192 28208 : assert(lo2 <= hi2);
1193 28208 : assert(hi1 <= lo2 || (lo2 == 0 && hi2 == 0));
1194 :
1195 28208 : if (hi1 == lo2) /* consecutive slices: combine into one */
1196 7859 : return canditer_slice(ci, lo1, hi2);
1197 20349 : if (lo2 == hi2 || hi1 >= ci->ncand || lo2 >= ci->ncand) {
1198 : /* empty second slice */
1199 17311 : return canditer_slice(ci, lo1, hi1);
1200 : }
1201 3038 : if (lo1 == hi1) /* empty first slice */
1202 1272 : return canditer_slice(ci, lo2, hi2);
1203 1766 : if (lo1 >= ci->ncand) /* out of range */
1204 : return BATdense(0, 0, 0);
1205 :
1206 1766 : if (hi2 >= ci->ncand)
1207 : hi2 = ci->ncand;
1208 :
1209 1766 : bn = COLnew(0, TYPE_oid, hi1 - lo1 + hi2 - lo2, TRANSIENT);
1210 1767 : if (bn == NULL)
1211 : return NULL;
1212 1767 : BATsetcount(bn, hi1 - lo1 + hi2 - lo2);
1213 1766 : bn->tsorted = true;
1214 1766 : bn->trevsorted = BATcount(bn) <= 1;
1215 1766 : bn->tkey = true;
1216 1766 : bn->tseqbase = oid_nil;
1217 1766 : bn->tnil = false;
1218 1766 : bn->tnonil = true;
1219 :
1220 1766 : oid *dst = Tloc(bn, 0);
1221 :
1222 1766 : switch (ci->tpe) {
1223 1 : case cand_materialized:
1224 1 : memcpy(dst, ci->oids + lo1, (hi1 - lo1) * sizeof(oid));
1225 1 : memcpy(dst + hi1 - lo1, ci->oids + lo2, (hi2 - lo2) * sizeof(oid));
1226 1 : break;
1227 : case cand_dense:
1228 909721 : while (lo1 < hi1)
1229 907956 : *dst++ = ci->seq + lo1++;
1230 247629 : while (lo2 < hi2)
1231 245864 : *dst++ = ci->seq + lo2++;
1232 : break;
1233 0 : case cand_except:
1234 0 : o = canditer_idx(ci, lo1);
1235 0 : add = o - ci->seq - lo1;
1236 0 : assert(add <= ci->nvals);
1237 0 : if (add == ci->nvals) {
1238 : /* after last exception: return dense sequence */
1239 0 : while (lo1 < hi1)
1240 0 : *dst++ = ci->seq + add + lo1++;
1241 : } else {
1242 0 : while (lo1 < hi1) {
1243 0 : while (add < ci->nvals && o == ci->oids[add]) {
1244 0 : o++;
1245 0 : add++;
1246 : }
1247 0 : *dst++ = o++;
1248 0 : lo1++;
1249 : }
1250 : }
1251 0 : o = canditer_idx(ci, lo2);
1252 0 : add = o - ci->seq - lo2;
1253 0 : assert(add <= ci->nvals);
1254 0 : if (add == ci->nvals) {
1255 : /* after last exception: return dense sequence */
1256 0 : while (lo2 < hi2)
1257 0 : *dst++ = ci->seq + add + lo2++;
1258 : } else {
1259 0 : while (lo2 < hi2) {
1260 0 : while (add < ci->nvals && o == ci->oids[add]) {
1261 0 : o++;
1262 0 : add++;
1263 : }
1264 0 : *dst++ = o++;
1265 0 : lo2++;
1266 : }
1267 : }
1268 : break;
1269 0 : case cand_mask:
1270 0 : return canditer_sliceval_mask(ci, canditer_idx(ci, lo1),
1271 : oid_nil, hi1 - lo1,
1272 : canditer_idx(ci, lo2),
1273 : oid_nil, hi2 - lo2);
1274 : }
1275 1766 : return virtualize(bn);
1276 : }
1277 :
1278 : BAT *
1279 28250 : canditer_slice2val(const struct canditer *ci, oid lo1, oid hi1, oid lo2, oid hi2)
1280 : {
1281 28250 : if (ci->tpe != cand_mask) {
1282 112818 : return canditer_slice2(
1283 : ci,
1284 26388 : is_oid_nil(lo1) ? 0 : canditer_search(ci, lo1, true),
1285 28164 : is_oid_nil(hi1) ? ci->ncand : canditer_search(ci, hi1, true),
1286 28182 : is_oid_nil(lo2) ? 0 : canditer_search(ci, lo2, true),
1287 1853 : is_oid_nil(hi2) ? ci->ncand : canditer_search(ci, hi2, true));
1288 : }
1289 :
1290 66 : return canditer_sliceval_mask(ci, lo1, hi1, ci->ncand,
1291 66 : lo2, hi2, ci->ncand);
1292 : }
1293 :
1294 : BAT *
1295 0 : BATnegcands(BUN nr, BAT *odels)
1296 : {
1297 0 : const char *nme;
1298 0 : Heap *dels;
1299 0 : BUN lo, hi;
1300 0 : ccand_t *c;
1301 0 : BAT *bn;
1302 :
1303 0 : bn = BATdense(0, 0, nr);
1304 0 : if (bn == NULL)
1305 : return NULL;
1306 0 : if (BATcount(odels) == 0)
1307 : return bn;
1308 :
1309 0 : lo = SORTfndfirst(odels, &bn->tseqbase);
1310 0 : hi = SORTfndfirst(odels, &(oid) {bn->tseqbase + BATcount(bn)});
1311 0 : if (lo == hi)
1312 : return bn;
1313 :
1314 0 : nme = BBP_physical(bn->batCacheid);
1315 0 : if ((dels = GDKmalloc(sizeof(Heap))) == NULL){
1316 0 : BBPreclaim(bn);
1317 0 : return NULL;
1318 : }
1319 0 : *dels = (Heap) {
1320 0 : .farmid = BBPselectfarm(bn->batRole, bn->ttype, varheap),
1321 0 : .parentid = bn->batCacheid,
1322 : .dirty = true,
1323 : .refs = ATOMIC_VAR_INIT(1),
1324 : };
1325 0 : strconcat_len(dels->filename, sizeof(dels->filename),
1326 : nme, ".theap", NULL);
1327 :
1328 0 : if (dels->farmid < 0 ||
1329 0 : HEAPalloc(dels, hi - lo + (sizeof(ccand_t)/sizeof(oid)), sizeof(oid)) != GDK_SUCCEED) {
1330 0 : GDKfree(dels);
1331 0 : BBPreclaim(bn);
1332 0 : return NULL;
1333 : }
1334 0 : c = (ccand_t *) dels->base;
1335 0 : *c = (ccand_t) {
1336 : .type = CAND_NEGOID,
1337 : };
1338 0 : dels->free = sizeof(ccand_t) + sizeof(oid) * (hi - lo);
1339 0 : BATiter bi = bat_iterator(odels);
1340 0 : if (bi.type == TYPE_void) {
1341 0 : oid *r = (oid *) (dels->base + sizeof(ccand_t));
1342 0 : for (BUN x = lo; x < hi; x++)
1343 0 : r[x - lo] = x + odels->tseqbase;
1344 : } else {
1345 0 : oid *r = (oid *) (dels->base + sizeof(ccand_t));
1346 0 : memcpy(r, (const oid *) bi.base + lo, sizeof(oid) * (hi - lo));
1347 : }
1348 0 : bat_iterator_end(&bi);
1349 0 : assert(bn->tvheap == NULL);
1350 0 : bn->tvheap = dels;
1351 0 : BATsetcount(bn, bn->batCount - (hi - lo));
1352 0 : TRC_DEBUG(ALGO, "BATnegcands(cands=" ALGOBATFMT ","
1353 : "dels=" ALGOBATFMT ")\n",
1354 : ALGOBATPAR(bn),
1355 : ALGOBATPAR(odels));
1356 0 : TRC_DEBUG(ALGO, "nr=" BUNFMT ", odels=" ALGOBATFMT
1357 : " -> " ALGOBATFMT "\n",
1358 : nr, ALGOBATPAR(odels),
1359 : ALGOBATPAR(bn));
1360 : return bn;
1361 : }
1362 :
1363 : BAT *
1364 106140 : BATmaskedcands(oid hseq, BUN nr, BAT *masked, bool selected)
1365 : {
1366 106140 : const char *nme;
1367 106140 : Heap *msks;
1368 106140 : ccand_t *c;
1369 106140 : BUN nmask;
1370 106140 : BAT *bn;
1371 :
1372 106140 : assert(masked->ttype == TYPE_msk);
1373 :
1374 106140 : bn = COLnew(hseq, TYPE_void, 0, TRANSIENT);
1375 106140 : if (bn == NULL)
1376 : return NULL;
1377 106140 : BATtseqbase(bn, hseq);
1378 :
1379 106139 : if (BATcount(masked) == 0)
1380 : return bn;
1381 :
1382 106140 : nme = BBP_physical(bn->batCacheid);
1383 106140 : if ((msks = GDKmalloc(sizeof(Heap))) == NULL){
1384 0 : BBPreclaim(bn);
1385 0 : return NULL;
1386 : }
1387 212278 : *msks = (Heap) {
1388 106139 : .farmid = BBPselectfarm(bn->batRole, bn->ttype, varheap),
1389 106139 : .parentid = bn->batCacheid,
1390 : .dirty = true,
1391 : .refs = ATOMIC_VAR_INIT(1),
1392 : };
1393 106139 : strconcat_len(msks->filename, sizeof(msks->filename),
1394 : nme, ".theap", NULL);
1395 :
1396 106140 : nmask = (nr + 31) / 32;
1397 212279 : if (msks->farmid < 0 ||
1398 106140 : HEAPalloc(msks, nmask + (sizeof(ccand_t)/sizeof(uint32_t)), sizeof(uint32_t)) != GDK_SUCCEED) {
1399 0 : GDKfree(msks);
1400 0 : BBPreclaim(bn);
1401 0 : return NULL;
1402 : }
1403 106139 : c = (ccand_t *) msks->base;
1404 106139 : *c = (ccand_t) {
1405 : .type = CAND_MSK,
1406 : // .mask = true,
1407 : };
1408 106139 : msks->free = sizeof(ccand_t) + nmask * sizeof(uint32_t);
1409 106139 : uint32_t *r = (uint32_t*)(msks->base + sizeof(ccand_t));
1410 106139 : BATiter bi = bat_iterator(masked);
1411 106138 : if (selected) {
1412 106138 : if (nr <= bi.count)
1413 106138 : memcpy(r, bi.base, nmask * sizeof(uint32_t));
1414 : else
1415 0 : memcpy(r, bi.base, (bi.count + 31) / 32 * sizeof(uint32_t));
1416 : } else {
1417 0 : const uint32_t *s = (const uint32_t *) bi.base;
1418 0 : BUN nmask_ = (bi.count + 31) / 32;
1419 0 : for (BUN i = 0; i < nmask_; i++)
1420 0 : r[i] = ~s[i];
1421 : }
1422 106138 : if (nr > bi.count) {
1423 0 : BUN rest = bi.count & 31;
1424 0 : BUN nmask_ = (bi.count + 31) / 32;
1425 0 : if (rest > 0)
1426 0 : r[nmask_ -1] |= ((1U << (32 - rest)) - 1) << rest;
1427 0 : for (BUN j = nmask_; j < nmask; j++)
1428 0 : r[j] = ~0;
1429 : }
1430 106138 : bat_iterator_end(&bi);
1431 : /* make sure last word doesn't have any spurious bits set */
1432 106140 : BUN cnt = nr % 32;
1433 106140 : if (cnt > 0)
1434 101402 : r[nmask - 1] &= (1U << cnt) - 1;
1435 : cnt = 0;
1436 12416327 : for (BUN i = 0; i < nmask; i++) {
1437 12310187 : if (cnt == 0 && r[i] != 0)
1438 105145 : c->firstbit = candmask_lobit(r[i]) + i * 32;
1439 12310187 : cnt += candmask_pop(r[i]);
1440 : }
1441 106140 : if (cnt > 0) {
1442 105151 : assert(bn->tvheap == NULL);
1443 105151 : bn->tvheap = msks;
1444 105151 : bn->tseqbase += (oid) c->firstbit;
1445 : } else {
1446 : /* no point having a mask if it's empty */
1447 989 : HEAPfree(msks, true);
1448 988 : GDKfree(msks);
1449 : }
1450 106140 : BATsetcount(bn, cnt);
1451 106140 : TRC_DEBUG(ALGO, "hseq=" OIDFMT ", masked=" ALGOBATFMT ", selected=%s"
1452 : " -> " ALGOBATFMT "\n",
1453 : hseq, ALGOBATPAR(masked),
1454 : selected ? "true" : "false",
1455 : ALGOBATPAR(bn));
1456 106140 : assert(bn->tseqbase != oid_nil);
1457 : return bn;
1458 : }
1459 :
1460 : /* convert a masked candidate list to a positive or negative candidate list */
1461 : BAT *
1462 106795 : BATunmask(BAT *b)
1463 : {
1464 : // assert(!mask_cand(b) || CCAND(b)->mask); /* todo handle negmask case */
1465 106795 : BUN cnt;
1466 106795 : uint32_t rem;
1467 106795 : uint32_t val;
1468 106795 : const uint32_t *src;
1469 106795 : oid *dst;
1470 106795 : BUN n = 0;
1471 106795 : oid tseq = b->hseqbase;
1472 106795 : bool negcand = false;
1473 :
1474 106795 : BATiter bi = bat_iterator(b);
1475 106822 : if (mask_cand(b)) {
1476 102004 : cnt = ccand_free(b) / sizeof(uint32_t);
1477 102004 : rem = 0;
1478 102004 : src = (const uint32_t *) ccand_first(b);
1479 102004 : tseq = b->tseqbase;
1480 102004 : tseq -= (oid) CCAND(b)->firstbit;
1481 : /* create negative candidate list if more than half the
1482 : * bits are set */
1483 102004 : negcand = BATcount(b) > cnt * 16;
1484 : } else {
1485 4818 : cnt = bi.count / 32;
1486 4818 : rem = bi.count % 32;
1487 4818 : src = (const uint32_t *) bi.base;
1488 : }
1489 106822 : BAT *bn;
1490 :
1491 106822 : if (negcand) {
1492 88933 : bn = COLnew(b->hseqbase, TYPE_void, 0, TRANSIENT);
1493 88933 : if (bn == NULL) {
1494 0 : bat_iterator_end(&bi);
1495 0 : return NULL;
1496 : }
1497 88933 : Heap *dels;
1498 88933 : if ((dels = GDKmalloc(sizeof(Heap))) == NULL) {
1499 0 : BBPreclaim(bn);
1500 0 : bat_iterator_end(&bi);
1501 0 : return NULL;
1502 : }
1503 177865 : *dels = (Heap) {
1504 88935 : .farmid = BBPselectfarm(TRANSIENT, TYPE_void, varheap),
1505 88930 : .parentid = bn->batCacheid,
1506 : .dirty = true,
1507 : .refs = ATOMIC_VAR_INIT(1),
1508 : };
1509 88930 : strconcat_len(dels->filename, sizeof(dels->filename),
1510 88930 : BBP_physical(bn->batCacheid), ".theap", NULL);
1511 :
1512 177831 : if (dels->farmid < 0 ||
1513 88903 : HEAPalloc(dels, cnt * 32 - bi.count
1514 : + sizeof(ccand_t) / sizeof(oid), sizeof(oid)) != GDK_SUCCEED) {
1515 0 : GDKfree(dels);
1516 0 : BBPreclaim(bn);
1517 0 : bat_iterator_end(&bi);
1518 0 : return NULL;
1519 : }
1520 88928 : * (ccand_t *) dels->base = (ccand_t) {
1521 : .type = CAND_NEGOID,
1522 : };
1523 88928 : dst = (oid *) (dels->base + sizeof(ccand_t));
1524 9631039 : for (BUN p = 0, v = 0; p < cnt; p++, v += 32) {
1525 9542111 : if ((val = src[p]) == ~UINT32_C(0))
1526 7679140 : continue;
1527 60161080 : for (uint32_t i = 0; i < 32; i++) {
1528 58382046 : if ((val & (1U << i)) == 0) {
1529 49508703 : if (v + i >= b->batCount + n)
1530 : break;
1531 49424766 : dst[n++] = tseq + v + i;
1532 : }
1533 : }
1534 : }
1535 88928 : if (n == 0) {
1536 : /* didn't need it after all */
1537 19 : HEAPfree(dels, true);
1538 19 : GDKfree(dels);
1539 : } else {
1540 88909 : dels->free = sizeof(ccand_t) + n * sizeof(oid);
1541 88909 : dels->dirty = true;
1542 88909 : assert(bn->tvheap == NULL);
1543 88909 : bn->tvheap = dels;
1544 : }
1545 88928 : BATsetcount(bn, n=bi.count);
1546 88923 : bn->tseqbase = tseq;
1547 : } else {
1548 17889 : bn = COLnew(b->hseqbase, TYPE_oid, mask_cand(b) ? bi.count : 1024, TRANSIENT);
1549 17887 : if (bn == NULL) {
1550 0 : bat_iterator_end(&bi);
1551 0 : return NULL;
1552 : }
1553 17887 : dst = (oid *) Tloc(bn, 0);
1554 2123701 : for (BUN p = 0; p < cnt; p++) {
1555 2105814 : if ((val = src[p]) == 0)
1556 1481397 : continue;
1557 20600246 : for (uint32_t i = 0; i < 32; i++) {
1558 19975829 : if (val & (1U << i)) {
1559 18128143 : if (n == BATcapacity(bn)) {
1560 58 : BATsetcount(bn, n);
1561 58 : if (BATextend(bn, BATgrows(bn)) != GDK_SUCCEED) {
1562 0 : BBPreclaim(bn);
1563 0 : bat_iterator_end(&bi);
1564 0 : return NULL;
1565 : }
1566 58 : dst = (oid *) Tloc(bn, 0);
1567 : }
1568 18128143 : dst[n++] = tseq + p * 32 + i;
1569 : }
1570 : }
1571 : }
1572 : /* the last partial mask word */
1573 17887 : if (rem > 0 && (val = src[cnt]) != 0) {
1574 26165 : for (uint32_t i = 0; i < rem; i++) {
1575 23613 : if (val & (1U << i)) {
1576 901 : if (n == BATcapacity(bn)) {
1577 0 : BATsetcount(bn, n);
1578 0 : if (BATextend(bn, BATgrows(bn)) != GDK_SUCCEED) {
1579 0 : BBPreclaim(bn);
1580 0 : bat_iterator_end(&bi);
1581 0 : return NULL;
1582 : }
1583 0 : dst = (oid *) Tloc(bn, 0);
1584 : }
1585 901 : dst[n++] = tseq + cnt * 32 + i;
1586 : }
1587 : }
1588 : }
1589 17887 : BATsetcount(bn, n);
1590 : }
1591 106807 : bat_iterator_end(&bi);
1592 106818 : bn->tkey = true;
1593 106818 : bn->tsorted = true;
1594 106818 : bn->trevsorted = n <= 1;
1595 106818 : bn->tnil = false;
1596 106818 : bn->tnonil = true;
1597 106818 : bn = virtualize(bn);
1598 106801 : TRC_DEBUG(ALGO, ALGOBATFMT " -> " ALGOBATFMT "\n", ALGOBATPAR(b), ALGOBATPAR(bn));
1599 : return bn;
1600 : }
|