-
Notifications
You must be signed in to change notification settings - Fork 985
/
ijoin.c
732 lines (699 loc) · 24.9 KB
/
ijoin.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
#include "data.table.h"
#include <Rdefines.h>
#include <time.h>
// #include <signal.h> // the debugging machinery + breakpoint aidee
// TODO: rewrite/simplify logic -- took me ages to understand what I wrote!!
// TODO: benchmark and parallelise slow regions
// TODO: implement 'lookup' for 'gaps' and 'overlaps' arguments
SEXP lookup(SEXP ux, SEXP xlen, SEXP indices, SEXP gaps, SEXP overlaps, SEXP multArg, SEXP typeArg, SEXP verbose) {
SEXP vv, tt, lookup, type_lookup;
R_len_t *idx,*count,*type_count,xrows=INTEGER(xlen)[0],uxrows=LENGTH(VECTOR_ELT(ux, 0)),uxcols=LENGTH(ux);
int *from = (int *)INTEGER(VECTOR_ELT(indices, 0));
int *to = (int *)INTEGER(VECTOR_ELT(indices, 1));
clock_t pass1, pass2, pass3, start;
enum {ALL, FIRST, LAST} mult = ALL;
enum {ANY, WITHIN, START, END, EQUAL} type = ANY;
if (!strcmp(CHAR(STRING_ELT(multArg, 0)), "all")) mult = ALL;
else if (!strcmp(CHAR(STRING_ELT(multArg, 0)), "first")) mult = FIRST;
else if (!strcmp(CHAR(STRING_ELT(multArg, 0)), "last")) mult = LAST;
else error(_("Internal error: invalid value for 'mult'; this should have been caught before. please report to data.table issue tracker")); // # nocov
if (!strcmp(CHAR(STRING_ELT(typeArg, 0)), "any")) type = ANY;
else if (!strcmp(CHAR(STRING_ELT(typeArg, 0)), "within")) type = WITHIN;
else if (!strcmp(CHAR(STRING_ELT(typeArg, 0)), "start")) type = START;
else if (!strcmp(CHAR(STRING_ELT(typeArg, 0)), "end")) type = END;
else if (!strcmp(CHAR(STRING_ELT(typeArg, 0)), "equal")) type = EQUAL;
else error(_("Internal error: invalid value for 'type'; this should have been caught before. please report to data.table issue tracker")); // # nocov
// For reference: uxcols-1 = type_count, uxcols-2 = count, uxcols-3 = type_lookup, uxcols-4 = lookup
// first pass: calculate lengths first
start = clock();
count = (int *)INTEGER(VECTOR_ELT(ux, uxcols-2));
type_count = (int *)INTEGER(VECTOR_ELT(ux, uxcols-1));
switch (mult) {
case FIRST:
switch(type) {
case EQUAL:
for (int i=0; i<xrows; ++i) {
count[from[i]-1]++; count[to[i]-1]++;
type_count[from[i]-1]++; type_count[to[i]-1]++;
}
break;
case START: case END: case ANY: case WITHIN:
for (int i=0; i<xrows; ++i) {
for (int j=from[i]; j<=to[i]; ++j) {
count[j-1]++;
}
}
if (type != WITHIN) {
for (int i=0; i<uxrows; ++i) // TODO: this allocation can be avoided if we take care of FIRST/LAST accordingly in 'overlaps'
if (count[i]) type_count[i] = 1;
}
break;
default: error(_("Internal error: unknown type in mult=%d in lookup: %d"), mult, type); // #nocov
}
break;
case LAST :
switch (type) {
case ANY:
for (int i=0; i<xrows; ++i) {
for (int j=from[i]; j<=to[i]; ++j) {
count[j-1]++;
if (from[i]==j && !type_count[j-1]) type_count[j-1]++;
}
}
break;
case EQUAL:
for (int i=0; i<xrows; ++i) {
count[from[i]-1]++; count[to[i]-1]++;
type_count[from[i]-1]++; type_count[to[i]-1]++;
}
break;
case START: case END: case WITHIN:
for (int i=0; i<xrows; ++i) {
for (int j=from[i]; j<=to[i]; ++j) {
count[j-1]++;
}
}
if (type != WITHIN) {
for (int i=0; i<uxrows; ++i) // TODO: this allocation can be avoided if we take care of FIRST/LAST accordingly in 'overlaps'
if (count[i]) type_count[i] = 1;
}
break;
}
break;
case ALL :
switch (type) {
case START: case END:
for (int i=0; i<xrows; ++i) {
for (int j=from[i]; j<=to[i]; ++j) {
count[j-1]++; type_count[j-1]++; // alternatively, we could simply do with type_count=count ?
}
}
break;
case EQUAL:
for (int i=0; i<xrows; ++i) {
count[from[i]-1]++; count[to[i]-1]++;
type_count[from[i]-1]++; type_count[to[i]-1]++;
}
break;
case ANY :
for (int i=0; i<xrows; ++i) {
const int k = from[i];
for (int j=from[i]; j<=to[i]; ++j) {
count[j-1]++;
if (k==j) type_count[j-1]++;
}
}
break;
case WITHIN :
for (int i=0; i<xrows; ++i) {
for (int j=from[i]; j<=to[i]; ++j) {
count[j-1]++;
}
}
break;
default: error(_("Internal error: unknown type in mult=%d in lookup: %d"), mult, type); // #nocov
}
break;
default: error(_("Internal error: unknown mult in lookup: %d"), mult); // #nocov
}
pass1 = clock() - start;
if (LOGICAL(verbose)[0])
Rprintf(_("First pass on calculating lengths in lookup ... done in %8.3f seconds\n"), 1.0*(pass1)/CLOCKS_PER_SEC);
// second pass: allocate vectors
start = clock();
lookup = VECTOR_ELT(ux, uxcols-4);
type_lookup = VECTOR_ELT(ux, uxcols-3);
for (int i=0; i<uxrows; ++i) {
SET_VECTOR_ELT(lookup, i, vv=allocVector(INTSXP, count[i]));
if (type != WITHIN) {
SET_VECTOR_ELT(type_lookup, i, vv=allocVector(INTSXP, type_count[i]));
}
}
pass2 = clock() - start;
if (LOGICAL(verbose)[0])
Rprintf(_("Second pass on allocation in lookup ... done in %8.3f seconds\n"), 1.0*(pass2)/CLOCKS_PER_SEC);
// generate lookup
start = clock();
idx = Calloc(uxrows, R_len_t); // resets bits, =0
switch (type) {
case ANY: case START: case END: case WITHIN:
for (int i=0; i<xrows; ++i) {
for (int j=from[i]; j<=to[i]; ++j) {
vv = VECTOR_ELT(lookup, j-1); // cache misses - memory efficiency? but 'lookups' are tiny - takes 0.036s on A.thaliana GFF for entire process)
INTEGER(vv)[idx[j-1]++] = i+1;
}
}
break;
case EQUAL:
for (int i=0; i<xrows; ++i) {
INTEGER(VECTOR_ELT(lookup, from[i]-1))[idx[from[i]-1]++] = i+1;
INTEGER(VECTOR_ELT(lookup, to[i]-1))[idx[to[i]-1]++] = i+1;
}
break;
default: error(_("Internal error: unknown type lookup should have been caught earlier: %d"), type); // #nocov
}
Free(idx);
// generate type_lookup
if (type != WITHIN) {
switch (mult) {
case FIRST :
for (int i=0; i<uxrows; ++i) {
if (!count[i]) continue;
vv = VECTOR_ELT(lookup, i);
tt = VECTOR_ELT(type_lookup, i);
if (length(tt) && length(vv)) { // length check added by Matt to avoid SEGV in #2767
INTEGER(tt)[0] = INTEGER(vv)[0];
}
}
break;
case LAST :
for (int i=0; i<uxrows; ++i) {
if (!count[i]) continue;
vv = VECTOR_ELT(lookup, i);
tt = VECTOR_ELT(type_lookup, i);
if (length(tt) && length(vv)>=count[i]) { // length check added by Matt to avoid SEGV in #2767
INTEGER(tt)[0] = INTEGER(vv)[count[i]-1];
}
}
case ALL :
switch (type) {
case START: case END: case EQUAL:
for (int i=0; i<uxrows; ++i)
SET_VECTOR_ELT(type_lookup, i, VECTOR_ELT(lookup, i));
break;
case ANY :
for (int i=0; i<uxrows; ++i) {
vv = VECTOR_ELT(lookup, i);
tt = VECTOR_ELT(type_lookup, i);
int k=0;
for (int j=count[i]-type_count[i]; j<count[i]; ++j)
INTEGER(tt)[k++] = INTEGER(vv)[j];
}
break;
case WITHIN :
// for (int i=0; i<uxrows; ++i) {
// vv = VECTOR_ELT(lookup, i);
// tt = VECTOR_ELT(type_lookup, i);
// for (int j=0; j<type_count[i]; ++j)
// INTEGER(tt)[j] = INTEGER(vv)[j];
// }
break; // #nocov
default: error(_("Internal error: unknown type in mult=%d in lookup should have been caught earlier: %d"), mult, type); // #nocov
}
break;
default: error(_("Internal error: unknown mult in lookup: %d"), mult); // #nocov
}
}
pass3 = clock() - start;
if (LOGICAL(verbose)[0])
Rprintf(_("Final step in generating lookup ... done in %8.3f seconds\n"), 1.0*(pass3)/CLOCKS_PER_SEC);
return(R_NilValue);
}
SEXP overlaps(SEXP ux, SEXP imatches, SEXP multArg, SEXP typeArg, SEXP nomatchArg, SEXP verbose) {
R_len_t uxcols=LENGTH(ux),rows=length(VECTOR_ELT(imatches,0));
int nomatch = INTEGER(nomatchArg)[0], totlen=0, thislen;
int *from = (int *)INTEGER(VECTOR_ELT(imatches, 0));
int *to = (int *)INTEGER(VECTOR_ELT(imatches, 1));
int *count = (int *)INTEGER(VECTOR_ELT(ux, uxcols-2));
int *type_count = (int *)INTEGER(VECTOR_ELT(ux, uxcols-1));
SEXP lookup = VECTOR_ELT(ux, uxcols-4);
SEXP type_lookup = VECTOR_ELT(ux, uxcols-3);
SEXP ans, f1__, f2__, tmp1, tmp2;
clock_t end1, end2, start;
enum {ALL, FIRST, LAST} mult = ALL;
enum {ANY, WITHIN, START, END, EQUAL} type = ANY;
// raise(SIGINT);
if (!strcmp(CHAR(STRING_ELT(multArg, 0)), "all")) mult = ALL;
else if (!strcmp(CHAR(STRING_ELT(multArg, 0)), "first")) mult = FIRST;
else if (!strcmp(CHAR(STRING_ELT(multArg, 0)), "last")) mult = LAST;
else error(_("Internal error: invalid value for 'mult'; this should have been caught before. please report to data.table issue tracker")); // # nocov
if (!strcmp(CHAR(STRING_ELT(typeArg, 0)), "any")) type = ANY;
else if (!strcmp(CHAR(STRING_ELT(typeArg, 0)), "within")) type = WITHIN;
else if (!strcmp(CHAR(STRING_ELT(typeArg, 0)), "start")) type = START;
else if (!strcmp(CHAR(STRING_ELT(typeArg, 0)), "end")) type = END;
else if (!strcmp(CHAR(STRING_ELT(typeArg, 0)), "equal")) type = EQUAL;
else error(_("Internal error: invalid value for 'type'; this should have been caught before. please report to data.table issue tracker")); // # nocov
// As a first pass get the final length, so that we can allocate up-front and not deal with Calloc + Realloc + size calculation hassle
// Checked the time for this loop on realisitc data (81m reads) and took 0.27 seconds! No excuses ;).
start = clock();
if (mult == ALL) {
totlen=0;
switch (type) {
case START: case END:
for (int i=0; i<rows; ++i)
totlen += (from[i] > 0 && type_count[from[i]-1]) ? type_count[from[i]-1] : 1;
break;
case EQUAL:
for (int i=0; i<rows; ++i) {
const int len=totlen;
int wlen=0, j=0, m=0;
const int k = (from[i]>0) ? from[i] : 1;
if (k == to[i]) {
wlen = count[k-1];
} else if (k < to[i]) {
tmp1 = VECTOR_ELT(lookup, k-1);
tmp2 = VECTOR_ELT(type_lookup, to[i]-1);
while (j<count[k-1] && m<type_count[to[i]-1]) {
if ( INTEGER(tmp1)[j] == INTEGER(tmp2)[m] ) {
++wlen; ++j; ++m;
} else if ( INTEGER(tmp1)[j] > INTEGER(tmp2)[m] ) {
++m;
} else ++j;
}
}
totlen += wlen;
if (len == totlen)
++totlen;
}
break;
case ANY:
for (int i=0; i<rows; ++i) {
const int len = totlen;
// k = (from[i] > 0) ? from[i] : 1;
const int k = from[i];
if (k<=to[i])
totlen += count[k-1];
for (int j=k+1; j<=to[i]; ++j)
totlen += type_count[j-1];
if (len == totlen)
++totlen;
}
break;
case WITHIN:
for (int i=0; i<rows; ++i) {
const int len=totlen;
int j=0, m=0;
const int k = from[i];
if (k > 0) {
if (k == to[i]) {
totlen += count[k-1];
} else if (k < to[i]) {
tmp1 = VECTOR_ELT(lookup, k-1);
tmp2 = VECTOR_ELT(lookup, to[i]-1);
while (j<count[k-1] && m<count[to[i]-1]) {
if ( INTEGER(tmp1)[j] == INTEGER(tmp2)[m] ) {
++totlen; ++j; ++m;
} else if ( INTEGER(tmp1)[j] > INTEGER(tmp2)[m] ) {
++m;
} else ++j;
}
}
}
if (len == totlen)
++totlen;
}
break;
default: error(_("Internal error: unknown type in mult=ALL in overlaps: %d"), type); // #nocov
}
} else totlen = rows;
end1 = clock() - start;
if (LOGICAL(verbose)[0])
Rprintf(_("First pass on calculating lengths in overlaps ... done in %8.3f seconds\n"), 1.0*(end1)/CLOCKS_PER_SEC);
// ans[0] is the the position of 'query' and ans[1] is that of 'subject'
// allocate f1__ and f2__ and assign 'nomatch' to f2__
ans = PROTECT(allocVector(VECSXP, 2));
SET_VECTOR_ELT(ans, 0, f1__=allocVector(INTSXP, totlen));
SET_VECTOR_ELT(ans, 1, f2__=allocVector(INTSXP, totlen));
thislen=0;
start = clock();
// switching mult=ALL,FIRST,LAST separately to
// - enhance performance for special cases, and
// - easy to fix any bugs in the future
switch (mult) {
case ALL:
switch (type) {
case START : case END :
for (int i=0; i<rows; ++i) {
const int len = thislen;
if (from[i] > 0) {
const int k = from[i];
tmp2 = VECTOR_ELT(type_lookup, k-1);
for (int j=0; j<type_count[k-1]; ++j) {
INTEGER(f1__)[thislen] = i+1;
INTEGER(f2__)[thislen] = INTEGER(tmp2)[j];
++thislen;
}
}
if (len == thislen) {
INTEGER(f1__)[thislen] = i+1;
INTEGER(f2__)[thislen] = nomatch;
++thislen;
}
}
break;
case EQUAL :
for (int i=0; i<rows; ++i) {
const int len = thislen;
if (from[i] > 0 && to[i] > 0) {
const int k = from[i];
if (k == to[i]) {
tmp1 = VECTOR_ELT(lookup, k-1);
tmp2 = VECTOR_ELT(type_lookup, to[i]-1);
for (int j=0; j<count[k-1]; ++j) {
INTEGER(f1__)[thislen] = i+1;
INTEGER(f2__)[thislen] = INTEGER(tmp1)[j];
++thislen;
}
} else if (k < to[i]) {
int j=0, m=0;
tmp1 = VECTOR_ELT(lookup, k-1);
tmp2 = VECTOR_ELT(type_lookup, to[i]-1);
while (j<count[k-1] && m<type_count[to[i]-1]) {
if ( INTEGER(tmp1)[j] == INTEGER(tmp2)[m] ) {
INTEGER(f1__)[thislen] = i+1;
INTEGER(f2__)[thislen] = INTEGER(tmp1)[j];
++thislen; ++j; ++m;
} else if ( INTEGER(tmp1)[j] > INTEGER(tmp2)[m] ) {
++m;
} else ++j;
}
}
}
if (len == thislen) {
INTEGER(f1__)[thislen] = i+1;
INTEGER(f2__)[thislen] = nomatch;
++thislen;
}
}
break;
case ANY :
for (int i=0; i<rows; ++i) {
const int len = thislen;
// k = (from[i]>0) ? from[i] : 1;
const int k = from[i];
if (k<=to[i]) {
tmp1 = VECTOR_ELT(lookup, k-1);
for (int m=0; m<count[k-1]; ++m) {
INTEGER(f1__)[thislen] = i+1;
INTEGER(f2__)[thislen] = INTEGER(tmp1)[m];
++thislen;
}
}
for (int j=k+1; j<=to[i]; ++j) {
tmp2 = VECTOR_ELT(type_lookup, j-1);
for (int m=0; m<type_count[j-1]; ++m) {
INTEGER(f1__)[thislen] = i+1;
INTEGER(f2__)[thislen] = INTEGER(tmp2)[m];
++thislen;
}
}
// dint go through any loops above
if (len == thislen) {
INTEGER(f1__)[thislen] = i+1;
INTEGER(f2__)[thislen] = nomatch;
++thislen;
}
}
break;
case WITHIN :
for (int i=0; i<rows; ++i) {
const int len = thislen;
const int k=from[i];
if (k > 0) {
if (k == to[i]) {
tmp1 = VECTOR_ELT(lookup, k-1);
for (int j=0; j<count[k-1]; ++j) {
INTEGER(f1__)[thislen] = i+1;
INTEGER(f2__)[thislen] = INTEGER(tmp1)[j];
++thislen;
}
} else if (k < to[i]) {
int j=0, m=0;
tmp1 = VECTOR_ELT(lookup, k-1);
tmp2 = VECTOR_ELT(lookup, to[i]-1);
while (j<count[k-1] && m<count[to[i]-1]) {
if ( INTEGER(tmp1)[j] == INTEGER(tmp2)[m] ) {
INTEGER(f1__)[thislen] = i+1;
INTEGER(f2__)[thislen] = INTEGER(tmp1)[j];
++thislen; ++j; ++m;
} else if ( INTEGER(tmp1)[j] > INTEGER(tmp2)[m] ) {
++m;
} else ++j;
}
}
}
if (len == thislen) {
INTEGER(f1__)[thislen] = i+1;
INTEGER(f2__)[thislen] = nomatch;
++thislen;
}
}
break;
default: error(_("Internal error: unknown type in mult=%d in overlaps: %d"), mult, type); // #nocov
}
break;
case FIRST:
switch (type) {
case START: case END:
for (int i=0; i<rows; ++i) {
const int len = thislen;
INTEGER(f1__)[thislen] = i+1;
const int k = (from[i]>0) ? from[i] : 1;
if (k <= to[i]) { // count[k-1] is equal to type_count[k-1] and will always be >0, so no length check necessary.
tmp1 = VECTOR_ELT(lookup, k-1);
INTEGER(f2__)[thislen] = INTEGER(tmp1)[0];
++thislen;
}
if (len == thislen) {
INTEGER(f2__)[thislen] = nomatch;
++thislen;
}
}
break;
case EQUAL :
for (int i=0; i<rows; ++i) {
const int len = thislen;
INTEGER(f1__)[thislen] = i+1;
if (from[i] > 0 && to[i] > 0) {
const int k = from[i];
if (k == to[i]) {
tmp1 = VECTOR_ELT(lookup, k-1);
INTEGER(f2__)[thislen] = INTEGER(tmp1)[0];
++thislen;
} else if (k < to[i]) {
int j=0, m=0;
tmp1 = VECTOR_ELT(lookup, k-1);
tmp2 = VECTOR_ELT(type_lookup, to[i]-1);
while (j<count[k-1] && m<type_count[to[i]-1]) {
if ( INTEGER(tmp1)[j] == INTEGER(tmp2)[m] ) {
INTEGER(f2__)[thislen] = INTEGER(tmp1)[j];
++thislen; ++j; ++m;
break;
} else if ( INTEGER(tmp1)[j] > INTEGER(tmp2)[m] ) {
++m;
} else ++j;
}
}
}
if (len == thislen) {
INTEGER(f2__)[thislen] = nomatch;
++thislen;
}
}
break;
case ANY:
for (int i=0; i<rows; ++i) {
const int len = thislen;
INTEGER(f1__)[thislen] = i+1;
// k = (from[i]>0) ? from[i] : 1;
const int k = from[i];
for (int j=k; j<=to[i]; ++j) {
if (type_count[j-1]) {
tmp2 = VECTOR_ELT(type_lookup, j-1);
INTEGER(f2__)[thislen] = INTEGER(tmp2)[0];
++thislen;
break;
}
}
if (len == thislen) {
INTEGER(f2__)[thislen] = nomatch;
++thislen;
}
}
break;
case WITHIN:
for (int i=0; i<rows; ++i) {
const int len = thislen;
INTEGER(f1__)[thislen] = i+1;
const int k = from[i];
if (k > 0) {
if (k == to[i] && count[k-1]) {
tmp1 = VECTOR_ELT(lookup, k-1);
INTEGER(f2__)[thislen] = INTEGER(tmp1)[0];
++thislen;
} else if (k < to[i]) {
int j=0, m=0;
tmp1 = VECTOR_ELT(lookup, k-1);
tmp2 = VECTOR_ELT(lookup, to[i]-1);
while (j<count[k-1] && m<count[to[i]-1]) {
if ( INTEGER(tmp1)[j] == INTEGER(tmp2)[m] ) {
INTEGER(f2__)[thislen] = INTEGER(tmp1)[j];
++thislen; ++j; ++m;
break;
} else if ( INTEGER(tmp1)[j] > INTEGER(tmp2)[m] ) {
++m;;
} else ++j;
}
}
}
if (len == thislen) {
INTEGER(f2__)[thislen] = nomatch;
++thislen;
}
}
break;
default: error(_("Internal error: unknown type in mult=%d in overlaps: %d"), mult, type); // #nocov
}
break;
case LAST:
switch (type) {
case START: case END:
for (int i=0; i<rows; ++i) {
const int len = thislen;
INTEGER(f1__)[thislen] = i+1;
const int k = (from[i]>0) ? from[i] : 1;
if (k <= to[i]) { // count[k-1] is equal to type_count[k-1] and will always be >0, so no length check necessary.
tmp1 = VECTOR_ELT(lookup, k-1);
INTEGER(f2__)[thislen] = INTEGER(tmp1)[count[k-1]-1];
++thislen;
}
if (len == thislen) {
INTEGER(f2__)[thislen] = nomatch;
++thislen;
}
}
break;
case EQUAL :
// Debugging reference for future-me
// R -d lldb
// run -f file.R
// breakpoint set -f ijoin.c -l 591
// c (hit enter to break at line 591)
// n (next line)
// p val # for native C objects
// call Rf_PrintValue(val) # for SEXP objects, to print whole vector/vals
for (int i=0; i<rows; ++i) {
const int len = thislen;
INTEGER(f1__)[thislen] = i+1;
if (from[i] > 0 && to[i] > 0) {
const int k = from[i];
if (k == to[i]) {
tmp1 = VECTOR_ELT(lookup, k-1);
INTEGER(f2__)[thislen] = INTEGER(tmp1)[count[k-1]-1];
++thislen;
} else if (k < to[i]) {
tmp1 = VECTOR_ELT(lookup, k-1);
tmp2 = VECTOR_ELT(type_lookup, to[i]-1);
int j=count[k-1]-1, m=type_count[to[i]-1]-1; // bug fix, k=from[i] but should be to[i]
while (j>=0 && m>=0) {
if ( INTEGER(tmp1)[j] == INTEGER(tmp2)[m] ) {
INTEGER(f2__)[thislen] = INTEGER(tmp1)[j];
++thislen; --j; --m;
break;
} else if ( INTEGER(tmp1)[j] < INTEGER(tmp2)[m] ) {
--m;
} else --j;
}
}
}
if (len == thislen) {
INTEGER(f2__)[thislen] = nomatch;
++thislen;
}
}
break;
// OLD logic for 'any,last' which had to check for maximum for each 'i'. Better logic below.
// for 'first' we need to just get the minimum of first non-zero-length element, but not the same case for 'last'.
// We've to loop over from[i]:to[i] and get maximum of all tmp2 values (each is of length 1 already conveniently set uo) in that range
// case ANY:
// for (int i=0; i<rows; ++i) {
// len = thislen;
// INTEGER(f1__)[thislen] = i+1;
// INTEGER(f2__)[thislen] = 0;
// // k = (from[i]>0) ? from[i] : 1;
// k = from[i];
// for (int j=k; j<=to[i]; ++j) {
// if (type_count[j-1]) {
// tmp2 = VECTOR_ELT(type_lookup, j-1);
// INTEGER(f2__)[thislen] = (INTEGER(f2__)[thislen] < INTEGER(tmp2)[type_count[j-1]-1]) ? INTEGER(tmp2)[type_count[j-1]-1] : INTEGER(f2__)[thislen];
// }
// }
// if (INTEGER(f2__)[thislen] == 0)
// INTEGER(f2__)[thislen] = nomatch;
// ++thislen;
// }
// break;
case ANY:
for (int i=0; i<rows; ++i) {
const int len = thislen;
INTEGER(f1__)[thislen] = i+1;
// k = (from[i]>0) ? from[i] : 1;
const int k = from[i];
if (k <= to[i]) {
if (k==to[i] && count[k-1]) {
tmp1 = VECTOR_ELT(lookup, k-1);
INTEGER(f2__)[thislen] = INTEGER(tmp1)[count[k-1]-1];
++thislen;
} else {
for (int j=to[i]; j>k; --j) {
if (type_count[j-1]) {
tmp2 = VECTOR_ELT(type_lookup, j-1);
INTEGER(f2__)[thislen] = INTEGER(tmp2)[0]; // tmp2 will be length 1
++thislen; break;
}
}
if (len == thislen && count[k-1]) {
tmp1 = VECTOR_ELT(lookup, k-1);
INTEGER(f2__)[thislen] = INTEGER(tmp1)[count[k-1]-1];
++thislen;
}
}
}
if (len == thislen) {
INTEGER(f2__)[thislen] = nomatch;
++thislen;
}
}
break;
case WITHIN:
for (int i=0; i<rows; ++i) {
const int len = thislen;
INTEGER(f1__)[thislen] = i+1;
const int k = from[i];
if (k > 0) {
if (k == to[i] && count[k-1]) {
tmp1 = VECTOR_ELT(lookup, k-1);
INTEGER(f2__)[thislen] = INTEGER(tmp1)[count[k-1]-1];
++thislen;
} else if (k < to[i]) {
tmp1 = VECTOR_ELT(lookup, k-1);
tmp2 = VECTOR_ELT(lookup, to[i]-1);
int j=count[k-1]-1, m=count[to[i]-1]-1;
while (j>=0 && m>=0) {
if ( INTEGER(tmp1)[j] == INTEGER(tmp2)[m] ) {
INTEGER(f2__)[thislen] = INTEGER(tmp1)[j];
++thislen; --j; --m;
break;
} else if ( INTEGER(tmp1)[j] < INTEGER(tmp2)[m] ) {
--m;
} else --j;
}
}
}
if (len == thislen) {
INTEGER(f2__)[thislen] = nomatch;
++thislen;
}
}
break;
default: error(_("Internal error: unknown type in mult=%d in overlaps: %d"), mult, type); // #nocov
}
break;
default: error(_("Internal error: unknown mult in overlaps: %d"), mult); // #nocov
}
end2 = clock() - start;
if (LOGICAL(verbose)[0])
Rprintf(_("Final step, fetching indices in overlaps ... done in %8.3f seconds\n"), 1.0*(end2)/CLOCKS_PER_SEC);
UNPROTECT(1);
return(ans);
}