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 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302
|
/*
# C-Code for searching and merging
# S3 atomic 64bit integers for R
# (c) 2011 Jens Oehlschägel
# Licence: GPL2
# Provided 'as is', use at your own risk
# Created: 2011-12-11
# Last changed: 2011-12-11
*/
#include <R.h>
#include <Rdefines.h>
//#include <Rinternals.h>
#include "integer64.h"
#include "bsearch.h"
void R_Busy (int which);
SEXP r_ram_integer64_nacount(
SEXP x_
)
{
int i,n = LENGTH(x_);
ValueT *x = (ValueT *) REAL(x_);
SEXP ret_;
PROTECT( ret_ = allocVector(INTSXP, 1) );
int ret = 0;
if (n){
R_Busy(1);
for(i=0;i<n;i++)
if (x[i]==NA_INTEGER64)
ret++;
}
INTEGER(ret_)[0]=ret;
R_Busy(0);
UNPROTECT(1);
return ret_;
}
SEXP r_ram_integer64_issorted_asc(
SEXP x_
)
{
int i,n = LENGTH(x_);
ValueT *x = (ValueT *) REAL(x_);
SEXP ret_;
PROTECT( ret_ = allocVector(LGLSXP, 1) );
Rboolean ret = TRUE;
if (n){
R_Busy(1);
for(i=1;i<n;i++)
if (x[i]<x[i-1]){
ret = FALSE;
goto wrapup;
}
}
wrapup:
INTEGER(ret_)[0]=ret;
R_Busy(0);
UNPROTECT(1);
return ret_;
}
SEXP r_ram_integer64_sortnut(
SEXP sorted_ /* somehow sorted table vector */
, SEXP ret_
)
{
int i,lasti,ities,nties=0,nunique=0,n = LENGTH(sorted_);
ValueT *sorted = (ValueT *) REAL(sorted_);
PROTECT( ret_ = allocVector(INTSXP, 2) );
if (n){
R_Busy(1);
nunique=1;
lasti = 0;
for(i=1;i<n;i++){
if (sorted[i]!=sorted[lasti]){
ities = i - lasti;
if (ities>1)
nties += ities;
nunique++;
lasti=i;
}
}
if (lasti<(n-1))
nties += n - lasti;
R_Busy(0);
}
INTEGER(ret_)[0]=nunique;
INTEGER(ret_)[1]=nties;
UNPROTECT(1);
return ret_;
}
SEXP r_ram_integer64_ordernut(
SEXP table_
, SEXP order_
, SEXP ret_
)
{
int i,lasti,ities,nties=0,nunique=0,n = LENGTH(table_);
ValueT *table;
table = (ValueT *) REAL(table_);
IndexT *index = INTEGER(order_);
PROTECT( ret_ = allocVector(INTSXP, 2) );
if (n){
R_Busy(1);
nunique=1;
lasti = 0;
for(i=1;i<n;i++){
if (table[index[i]-1]!=table[index[lasti]-1]){
ities = i - lasti;
if (ities>1)
nties += ities;
nunique++;
lasti=i;
}
}
if (lasti<(n-1))
nties += n - lasti;
R_Busy(0);
}
INTEGER(ret_)[0]=nunique;
INTEGER(ret_)[1]=nties;
UNPROTECT(1);
return ret_;
}
SEXP r_ram_integer64_sortfin_asc(
SEXP x_ /* data vector */
, SEXP sorted_ /* sorted table vector */
, SEXP method_
, SEXP ret_
)
{
int i,n = LENGTH(x_);
int pos,nt = LENGTH(sorted_);
int n1 = nt-1;
int method = asInteger(method_);
ValueT *data;
data = (ValueT *) REAL(x_);
ValueT *sorted;
sorted = (ValueT *) REAL(sorted_);
int *ret = LOGICAL(ret_);
R_Busy(1);
DEBUG_INIT
switch (method){
case 1:{
for(i=0;i<n;i++)
ret[i] = integer64_bsearch_asc_EQ(sorted, 0, n1, data[i])<0 ? FALSE : TRUE;
break;
}
case 2:{
pos = 0;
for(i=0;i<n;i++){
pos = integer64_lsearch_asc_GE(sorted, pos, n1, data[i]);
if (pos>n1){
for (;i<n;i++)
ret[i] = FALSE;
}else{
ret[i] = data[i]==sorted[pos] ? TRUE : FALSE;
}
}
break;
}
case 3:{
pos = 0;
for (i=0;i<n;i++){
while(LESS(sorted[pos], data[i])){
pos++;
if (pos==nt){
for (;i<n;i++)
ret[i] = FALSE;
goto wrapup;
}
}
ret[i] = data[i]==sorted[pos] ? TRUE : FALSE;
}
break;
}
default:
method=0;
}
wrapup:
R_Busy(0);
if (method==0)
error("unimplemented method");
return ret_;
}
SEXP r_ram_integer64_orderfin_asc(
SEXP x_ /* data vector */
, SEXP table_ /* table vector */
, SEXP order_ /* order vector that makes table_ sorted */
, SEXP method_
, SEXP ret_
)
{
int i,n = LENGTH(x_);
int pos,nt = LENGTH(table_);
int n1 = nt-1;
int method = asInteger(method_);
ValueT *data;
data = (ValueT *) REAL(x_);
ValueT *table;
table = (ValueT *) REAL(table_);
IndexT *index = INTEGER(order_);
int *ret = LOGICAL(ret_);
R_Busy(1);
DEBUG_INIT
for(i=0;i<nt;i++)
index[i]--;
switch (method){
case 1:{
for(i=0;i<n;i++){
ret[i] = integer64_bosearch_asc_EQ(table, index, 0, n1, data[i])<0 ? FALSE : TRUE;
}
break;
}
case 2:{
pos = 0;
for(i=0;i<n;i++){
pos = integer64_losearch_asc_GE(table, index, pos, n1, data[i]);
if (pos>n1){
for (;i<n;i++)
ret[i] = FALSE;
}else{
ret[i] = data[i]==table[index[pos]] ? TRUE : FALSE;
}
}
break;
}
case 3:{
pos = 0;
for (i=0;i<n;i++){
while(LESS(table[index[pos]], data[i])){
pos++;
if (pos==nt){
for (;i<n;i++)
ret[i] = FALSE;
goto wrapup;
}
}
ret[i] = data[i]==table[index[pos]] ? TRUE : FALSE;
}
break;
}
default:
method=0;
}
wrapup:
for(i=0;i<nt;i++)
index[i]++;
R_Busy(0);
if (method==0)
error("unimplemented method");
return ret_;
}
SEXP r_ram_integer64_orderpos_asc(
SEXP x_ /* data vector */
, SEXP table_ /* table vector */
, SEXP order_ /* order vector that makes table_ sorted */
, SEXP nomatch_
, SEXP method_
, SEXP ret_
)
{
int i,n = LENGTH(x_);
int pos,nt = LENGTH(table_);
int n1 = nt-1;
int method = asInteger(method_);
int nomatch = asInteger(nomatch_);
ValueT *data;
data = (ValueT *) REAL(x_);
ValueT *table;
table = (ValueT *) REAL(table_);
IndexT *index = INTEGER(order_);
int *ret = INTEGER(ret_);
R_Busy(1);
DEBUG_INIT
for(i=0;i<nt;i++)
index[i]--;
switch (method){
case 1:{
for(i=0;i<n;i++){
pos = integer64_bosearch_asc_EQ(table, index, 0, n1, data[i]);
ret[i] = pos<0 ? nomatch : index[pos]+ 1;
}
break;
}
case 2:{
pos = 0;
for(i=0;i<n;i++){
pos = integer64_losearch_asc_GE(table, index, pos, n1, data[i]);
if (pos>n1){
for (;i<n;i++)
ret[i] = nomatch;
}else{
ret[i] = data[i]==table[index[pos]] ? index[pos]+1 : nomatch;
}
}
break;
}
case 3:{
pos = 0;
for (i=0;i<n;i++){
while(LESS(table[index[pos]], data[i])){
pos++;
if (pos==nt){
for (;i<n;i++)
ret[i] = nomatch;
goto wrapup;
}
}
ret[i] = data[i]==table[index[pos]] ? index[pos]+1 : nomatch;
}
break;
}
default:
method=0;
}
wrapup:
for(i=0;i<nt;i++)
index[i]++;
R_Busy(0);
if (method==0)
error("unimplemented method");
return ret_;
}
/* 1= simple binary search of unsorted in sorted
2= double exponential search of sorted in sorted
3= merge-search of sorted in sorted
*/
SEXP r_ram_integer64_sortorderpos_asc(
SEXP x_ /* data vector */
, SEXP sorted_ /* sorted table vector */
, SEXP order_ /* order vector that makes table_ sorted */
, SEXP nomatch_
, SEXP method_
, SEXP ret_
)
{
int i,n = LENGTH(x_);
int pos,nt = LENGTH(sorted_);
int n1 = nt-1;
int method = asInteger(method_);
int nomatch = asInteger(nomatch_);
ValueT *data;
data = (ValueT *) REAL(x_);
ValueT *sorted;
sorted = (ValueT *) REAL(sorted_);
IndexT *index = INTEGER(order_);
int *ret = INTEGER(ret_);
R_Busy(1);
DEBUG_INIT
switch (method){
case 1:{
for(i=0;i<n;i++){
pos = integer64_bsearch_asc_EQ(sorted, 0, n1, data[i]);
ret[i] = pos<0 ? nomatch : index[pos];
}
break;
}
case 2:{
pos = 0;
for(i=0;i<n;i++){
pos = integer64_lsearch_asc_GE(sorted, pos, n1, data[i]);
if (pos>n1){
for (;i<n;i++)
ret[i] = nomatch;
}else{
ret[i] = data[i]==sorted[pos] ? index[pos] : nomatch;
}
}
break;
}
case 3:{
pos = 0;
for (i=0;i<n;i++){
while(LESS(sorted[pos], data[i])){
pos++;
if (pos==nt){
for (;i<n;i++)
ret[i] = nomatch;
goto wrapup;
}
}
ret[i] = data[i]==sorted[pos] ? index[pos] : nomatch;
}
break;
}
default:
method=0;
}
wrapup:
R_Busy(0);
if (method==0)
error("unimplemented method");
return ret_;
}
SEXP r_ram_integer64_sortuni_asc(
SEXP sorted_ /* somehow sorted table vector */
, SEXP ret_
)
{
int i,pos,n = LENGTH(sorted_);
ValueT *sorted = (ValueT *) REAL(sorted_);
ValueT * ret = (ValueT *) REAL(ret_);
if (n){
R_Busy(1);
pos = 0;
ret[0] = sorted[0];
for(i=1;i<n;i++)
if (sorted[i]!=ret[pos])
ret[++pos] = sorted[i];
R_Busy(0);
}
return ret_;
}
SEXP r_ram_integer64_sortorderuni_asc(
SEXP table_ /* table vector */
, SEXP sorted_ /* somehow sorted table vector */
, SEXP order_ /* sorted table vector */
, SEXP ret_
)
{
int i,pos,n = LENGTH(table_);
ValueT *table = (ValueT *) REAL(table_);
ValueT *sorted = (ValueT *) REAL(sorted_);
IndexT *index = INTEGER(order_);
ValueT * ret = (ValueT *) REAL(ret_);
ValueT lastval;
if (n){
R_Busy(1);
IndexT nbitflags = n/BITS_INTEGER64+(n%BITS_INTEGER64 ? 1 : 0);
ValueT *bitflags;
bitflags = (ValueT *) R_alloc(nbitflags, sizeof(ValueT));
for (i=0;i<nbitflags;i++)
bitflags[i]=0;
lastval = sorted[0];
bitflags[(index[0]-1)/BITS_INTEGER64] |= (RIGHTBIT_INTEGER64 << ((index[0]-1) % BITS_INTEGER64));
for(i=1;i<n;i++)
if (sorted[i]!=lastval){
bitflags[(index[i]-1)/BITS_INTEGER64] |= (RIGHTBIT_INTEGER64 << ((index[i]-1) % BITS_INTEGER64));
lastval = sorted[i];
}
pos = 0;
for(i=0;i<n;i++)
if ((bitflags[i/BITS_INTEGER64] & (RIGHTBIT_INTEGER64 << (i % BITS_INTEGER64))))
ret[pos++] = table[i];
R_Busy(0);
}
return ret_;
}
SEXP r_ram_integer64_orderuni_asc(
SEXP table_ /* sorted table vector */
, SEXP order_ /* sorted table vector */
, SEXP keep_order_ /* sorted table vector */
, SEXP ret_
)
{
int i,pos,n = LENGTH(table_);
ValueT *table = (ValueT *) REAL(table_);
IndexT *index = INTEGER(order_);
ValueT * ret = (ValueT *) REAL(ret_);
ValueT val, lastval;
if (n){
R_Busy(1);
if (asLogical(keep_order_)){
IndexT nbitflags = n/BITS_INTEGER64+(n%BITS_INTEGER64 ? 1 : 0);
ValueT *bitflags;
bitflags = (ValueT *) R_alloc(nbitflags, sizeof(ValueT));
for (i=0;i<nbitflags;i++)
bitflags[i]=0;
lastval = table[index[0]-1];
bitflags[(index[0]-1)/BITS_INTEGER64] |= (RIGHTBIT_INTEGER64 << ((index[0]-1) % BITS_INTEGER64));
for(i=1;i<n;i++){
pos = index[i]-1;
if (table[pos]!=lastval){
bitflags[pos/BITS_INTEGER64] |= (RIGHTBIT_INTEGER64 << (pos % BITS_INTEGER64));
lastval = table[pos];
}
}
pos = 0;
for(i=0;i<n;i++)
if ((bitflags[i/BITS_INTEGER64] & (RIGHTBIT_INTEGER64 << (i % BITS_INTEGER64))))
ret[pos++] = table[i];
}else{
lastval = table[index[0]-1];
ret[0] = lastval;
pos=1;
for(i=1;i<n;i++){
val = table[index[i]-1];
if (val!=lastval){
ret[pos++] = val;
lastval = val;
}
}
}
R_Busy(0);
}
return ret_;
}
SEXP r_ram_integer64_sortorderupo_asc(
SEXP sorted_ /* somehow sorted table vector */
, SEXP order_ /* sorted table vector */
, SEXP keep_order_ /* sorted table vector */
, SEXP ret_
)
{
int i,pos,n = LENGTH(sorted_);
ValueT *sorted = (ValueT *) REAL(sorted_);
IndexT *index = INTEGER(order_);
IndexT *ret = INTEGER(ret_);
ValueT lastval;
if (n){
R_Busy(1);
if (asLogical(keep_order_)){
IndexT nbitflags = n/BITS_INTEGER64+(n%BITS_INTEGER64 ? 1 : 0);
ValueT *bitflags;
bitflags = (ValueT *) R_alloc(nbitflags, sizeof(ValueT));
for (i=0;i<nbitflags;i++)
bitflags[i]=0;
lastval = sorted[0];
bitflags[(index[0]-1)/BITS_INTEGER64] |= (RIGHTBIT_INTEGER64 << ((index[0]-1) % BITS_INTEGER64));
for(i=1;i<n;i++)
if (sorted[i]!=lastval){
bitflags[(index[i]-1)/BITS_INTEGER64] |= (RIGHTBIT_INTEGER64 << ((index[i]-1) % BITS_INTEGER64));
lastval = sorted[i];
}
pos = 0;
for(i=0;i<n;i++)
if ((bitflags[i/BITS_INTEGER64] & (RIGHTBIT_INTEGER64 << (i % BITS_INTEGER64))))
ret[pos++] = i+1;
}else{
ret[0] = index[0];
pos=1;
for(i=1;i<n;i++){
if (sorted[i]!=sorted[i-1]){
ret[pos++] = index[i];
}
}
}
R_Busy(0);
}
return ret_;
}
SEXP r_ram_integer64_orderupo_asc(
SEXP table_ /* sorted table vector */
, SEXP order_ /* sorted table vector */
, SEXP keep_order_ /* sorted table vector */
, SEXP ret_
)
{
int i,pos,n = LENGTH(table_);
ValueT *table = (ValueT *) REAL(table_);
IndexT *index = INTEGER(order_);
IndexT * ret = INTEGER(ret_);
ValueT lastval;
if (n){
R_Busy(1);
if (asLogical(keep_order_)){
IndexT nbitflags = n/BITS_INTEGER64+(n%BITS_INTEGER64 ? 1 : 0);
ValueT *bitflags;
bitflags = (ValueT *) R_alloc(nbitflags, sizeof(ValueT));
for (i=0;i<nbitflags;i++)
bitflags[i]=0;
lastval = table[index[0]-1];
bitflags[(index[0]-1)/BITS_INTEGER64] |= (RIGHTBIT_INTEGER64 << ((index[0]-1) % BITS_INTEGER64));
for(i=1;i<n;i++){
pos = index[i]-1;
if (table[pos]!=lastval){
bitflags[pos/BITS_INTEGER64] |= (RIGHTBIT_INTEGER64 << (pos % BITS_INTEGER64));
lastval = table[pos];
}
}
pos = 0;
for(i=0;i<n;i++)
if ((bitflags[i/BITS_INTEGER64] & (RIGHTBIT_INTEGER64 << (i % BITS_INTEGER64))))
ret[pos++] = i+1;
}else{
ret[0] = index[0];
pos=1;
for(i=1;i<n;i++){
if ((table[index[i]-1])!=(table[index[i-1]-1])){
ret[pos++] = index[i];
}
}
}
R_Busy(0);
}
return ret_;
}
SEXP r_ram_integer64_sorttab_asc(
SEXP sorted_ /* somehow sorted table vector */
, SEXP ret_
)
{
int i,pos,n = LENGTH(sorted_);
ValueT *sorted = (ValueT *) REAL(sorted_);
IndexT * ret = INTEGER(ret_);
if (n){
R_Busy(1);
pos = 0;
ret[0] = 1;
for(i=1;i<n;i++){
if (sorted[i]!=sorted[i-1])
ret[++pos] = 1;
else
ret[pos]++;
}
R_Busy(0);
}
return ret_;
}
SEXP r_ram_integer64_ordertab_asc(
SEXP table_ /* sorted table vector */
, SEXP order_ /* sorted table vector */
, SEXP denormalize_ /* sorted table vector */
, SEXP keep_order_
, SEXP ret_
)
{
int i,j,pos,n = LENGTH(table_);
ValueT *table = (ValueT *) REAL(table_);
IndexT *index = INTEGER(order_);
IndexT * ret = INTEGER(ret_);
int cnt;
if (n){
PROTECT(ret_); /* because of R_Busy wee need PROTECT, according to Thomas Kalibera */
R_Busy(1);
if (asLogical(denormalize_)){
j = 0;
cnt=1;
pos = index[j]-1;
for(i=1;i<n;i++){
if (table[pos]!=table[index[i]-1]){
for (;j<i;j++)
ret[index[j]-1] = cnt;
cnt = 1;
j = i;
pos = index[j]-1;
}else{
cnt++;
}
}
for (;j<i;j++)
ret[index[j]-1] = cnt;
}else if (asLogical(keep_order_)){
pos = index[0]-1;
ret[pos] = 1;
for(i=1;i<n;i++){
j = index[i]-1;
if (table[pos]!=table[j]){
pos = j;
ret[pos] = 1;
}else{
ret[pos]++;
ret[j]=0;
}
}
pos = 0;
for(i=0;i<n;i++)
if (ret[i])
ret[pos++] = ret[i];
SET_LENGTH(ret_, pos);
}else{
j = 0;
ret[j] = 1;
pos = index[j]-1;
for(i=1;i<n;i++){
if (table[index[i]-1]!=table[pos]){
pos = index[i]-1;
ret[++j] = 1;
}else{
ret[j]++;
}
}
}
R_Busy(0);
UNPROTECT(1);
}
return ret_;
}
SEXP r_ram_integer64_sortordertab_asc(
SEXP sorted_ /* somehow sorted table vector */
, SEXP order_ /* sorted table vector */
, SEXP denormalize_
, SEXP ret_
)
{
int i,pos,n = LENGTH(sorted_);
ValueT *sorted = (ValueT *) REAL(sorted_);
IndexT *index = INTEGER(order_);
IndexT * ret = INTEGER(ret_);
int cnt;
if (n){
PROTECT(ret_); /* because of R_Busy wee need PROTECT, according to Thomas Kalibera */
R_Busy(1);
if (asLogical(denormalize_)){
pos = 0;
cnt = 1;
for(i=1;i<n;i++){
if (sorted[i]!=sorted[pos]){
for (;pos<i;pos++)
ret[index[pos]-1] = cnt;
cnt = 1;
pos = i;
}else{
cnt++;
}
}
for (;pos<i;pos++)
ret[index[pos]-1] = cnt;
}else{
pos = index[0]-1;
ret[pos] = 1;
for(i=1;i<n;i++){
if (sorted[i]!=sorted[i-1]){
pos = index[i]-1;
ret[pos] = 1;
}else{
ret[pos]++;
ret[index[i]-1]=0;
}
}
pos = 0;
for(i=0;i<n;i++)
if (ret[i])
ret[pos++] = ret[i];
SET_LENGTH(ret_, pos);
}
R_Busy(0);
UNPROTECT(1);
}
return ret_;
}
// with na_skip_num==0 this is the proper version doing proper star schema modelling:
// NAs receive a key value like all other values, such that they can be joined with the dimension table
// with na_skip_num==na_count this is the sick version needed for as.factor/as.ordered:
// NAs are propagated and can not be joined with the dimension table / NAs are not in levels
// this breaks all previous consistent modelling
SEXP r_ram_integer64_orderkey_asc(
SEXP table_ /* sorted table vector */
, SEXP order_ /* sorted table vector */
, SEXP na_skip_num_ /* number of NAs to be skiped before keying, must be 0 or na_count */
, SEXP ret_
)
{
int i,j,pos,n = LENGTH(table_);
ValueT *table = (ValueT *) REAL(table_);
IndexT *index = INTEGER(order_);
IndexT na_skip_num = asInteger(na_skip_num_);
IndexT * ret = INTEGER(ret_);
IndexT key;
if (n){
R_Busy(1);
for (i=0;i<na_skip_num;i++)
ret[index[i]-1] = NA_INTEGER;
if (na_skip_num<n){
key = 1;
pos = index[na_skip_num]-1;
ret[pos]= key;
for(i=na_skip_num+1;i<n;i++){
j = index[i]-1;
if (table[pos]!=table[j]){
pos = j;
key++;
}
ret[j] = key;
}
}
R_Busy(0);
}
return ret_;
}
SEXP r_ram_integer64_sortorderkey_asc(
SEXP sorted_ /* somehow sorted table vector */
, SEXP order_ /* sorted table vector */
, SEXP na_skip_num_ /* number of NAs to be skiped before keying, must be 0 or na_count */
, SEXP ret_
)
{
int i,n = LENGTH(sorted_);
ValueT *sorted = (ValueT *) REAL(sorted_);
IndexT *index = INTEGER(order_);
IndexT na_skip_num = asInteger(na_skip_num_);
IndexT * ret = INTEGER(ret_);
IndexT key;
if (n){
R_Busy(1);
for (i=0;i<na_skip_num;i++)
ret[index[i]-1] = NA_INTEGER;
if (na_skip_num<n){
key = 1;
ret[index[na_skip_num]-1]= key;
for(i=na_skip_num+1;i<n;i++){
if (sorted[i]!=sorted[i-1]){
key++;
}
ret[index[i]-1] = key;
}
}
R_Busy(0);
}
return ret_;
}
SEXP r_ram_integer64_orderrnk_asc(
SEXP table_ /* sorted table vector */
, SEXP order_ /* sorted table vector */
, SEXP nacount_
, SEXP ret_
)
{
int i,j,pos,n = LENGTH(table_);
ValueT *table = (ValueT *) REAL(table_);
IndexT *index = INTEGER(order_);
double * ret = REAL(ret_);
double avgrank;
int nacount = asInteger(nacount_);
int lasti;
if (n){
R_Busy(1);
for (i=0;i<nacount;i++)
ret[index[i]-1] = NA_REAL;
index += nacount;
n -= nacount;
lasti = 0;
pos = index[0]-1;
for(i=1;i<n;i++){
j = index[i]-1;
if (table[pos]!=table[j]){
pos = j;
avgrank = (lasti + 1 + i)/2.0;
for (j=i-1;j>=lasti;j--)
ret[index[j]-1] = avgrank;
lasti = i;
}
}
avgrank = (lasti + 1 + i)/2.0;
for (j=i-1;j>=lasti;j--)
ret[index[j]-1] = avgrank;
R_Busy(0);
}
return ret_;
}
SEXP r_ram_integer64_sortorderrnk_asc(
SEXP sorted_ /* somehow sorted table vector */
, SEXP order_ /* sorted table vector */
, SEXP nacount_
, SEXP ret_
)
{
int i,j,n = LENGTH(sorted_);
ValueT *sorted = (ValueT *) REAL(sorted_);
IndexT *index = INTEGER(order_);
double * ret = REAL(ret_);
double avgrank;
int nacount = asInteger(nacount_);
int lasti;
if (n){
R_Busy(1);
for (i=0;i<nacount;i++)
ret[index[i]-1] = NA_REAL;
index += nacount;
sorted += nacount;
n -= nacount;
lasti = 0;
for(i=1;i<n;i++){
if (sorted[i]!=sorted[i-1]){
avgrank = (lasti + 1 + i)/2.0;
for (j=i-1;j>=lasti;j--)
ret[index[j]-1] = avgrank;
lasti = i;
}
}
avgrank = (lasti + 1 + i)/2.0;
for (j=i-1;j>=lasti;j--)
ret[index[j]-1] = avgrank;
R_Busy(0);
}
return ret_;
}
SEXP r_ram_integer64_orderdup_asc(
SEXP table_ /* sorted table vector */
, SEXP order_ /* sorted table vector */
, SEXP method_
, SEXP ret_
)
{
int i,pos,n = LENGTH(table_);
ValueT *table = (ValueT *) REAL(table_);
IndexT *index = INTEGER(order_);
int method = asInteger(method_);
int * ret = LOGICAL(ret_);
ValueT lastval;
if (n){
R_Busy(1);
switch (method){
case 1:{
for (i=0;i<n;i++)
ret[i]=TRUE;
lastval = table[index[0]-1];
ret[index[0]-1] = FALSE;
for(i=1;i<n;i++){
pos = index[i]-1;
if (table[pos]!=lastval){
ret[pos] = FALSE;
lastval = table[pos];
}
}
break;
}
case 2:{
IndexT nbitflags = n/BITS_INTEGER64+(n%BITS_INTEGER64 ? 1 : 0);
ValueT *bitflags;
bitflags = (ValueT *) R_alloc(nbitflags, sizeof(ValueT));
for (i=0;i<nbitflags;i++)
bitflags[i]=0;
lastval = table[index[0]-1];
bitflags[(index[0]-1)/BITS_INTEGER64] |= (RIGHTBIT_INTEGER64 << ((index[0]-1) % BITS_INTEGER64));
for(i=1;i<n;i++){
pos = index[i]-1;
if (table[pos]!=lastval){
bitflags[pos/BITS_INTEGER64] |= (RIGHTBIT_INTEGER64 << (pos % BITS_INTEGER64));
lastval = table[pos];
}
}
for(i=0;i<n;i++)
ret[i] = ((bitflags[i/BITS_INTEGER64] & (RIGHTBIT_INTEGER64 << (i % BITS_INTEGER64)))) ? FALSE : TRUE;
break;
}
default:
method=0;
}
R_Busy(0);
}
if (method==0)
error("unimplemented method");
return ret_;
}
SEXP r_ram_integer64_sortorderdup_asc(
SEXP sorted_ /* somehow sorted table vector */
, SEXP order_ /* sorted table vector */
, SEXP method_
, SEXP ret_
)
{
int i,n = LENGTH(sorted_);
ValueT *sorted = (ValueT *) REAL(sorted_);
IndexT *index = INTEGER(order_);
int method = asInteger(method_);
int * ret = LOGICAL(ret_);
if (n){
R_Busy(1);
switch (method){
case 1:{
for (i=0;i<n;i++)
ret[i]=TRUE;
ret[index[0]-1] = FALSE;
for(i=1;i<n;i++){
if (sorted[i]!=sorted[i-1]){
ret[index[i]-1] = FALSE;
}
}
break;
}
case 2:{
IndexT nbitflags = n/BITS_INTEGER64+(n%BITS_INTEGER64 ? 1 : 0);
ValueT *bitflags;
bitflags = (ValueT *) R_alloc(nbitflags, sizeof(ValueT));
for (i=0;i<nbitflags;i++)
bitflags[i]=0;
bitflags[(index[0]-1)/BITS_INTEGER64] |= (RIGHTBIT_INTEGER64 << ((index[0]-1) % BITS_INTEGER64));
for(i=1;i<n;i++){
if (sorted[i]!=sorted[i-1]){
bitflags[(index[i]-1)/BITS_INTEGER64] |= (RIGHTBIT_INTEGER64 << ((index[i]-1) % BITS_INTEGER64));
}
}
for(i=0;i<n;i++)
ret[i] = ((bitflags[i/BITS_INTEGER64] & (RIGHTBIT_INTEGER64 << (i % BITS_INTEGER64)))) ? FALSE : TRUE;
break;
}
default:
method=0;
}
R_Busy(0);
}
if (method==0)
error("unimplemented method");
return ret_;
}
/* experimental: all origpos at which we have ties */
SEXP r_ram_integer64_sortordertie_asc(
SEXP sorted_ /* somehow sorted table vector */
, SEXP order_ /* sorted table vector */
, SEXP ret_
)
{
int i,j,n = LENGTH(sorted_);
ValueT *sorted = (ValueT *) REAL(sorted_);
IndexT *index = INTEGER(order_);
IndexT * ret = INTEGER(ret_);
if (n){
R_Busy(1);
IndexT nbitflags = n/BITS_INTEGER64+(n%BITS_INTEGER64 ? 1 : 0);
ValueT *bitflags;
bitflags = (ValueT *) R_alloc(nbitflags, sizeof(ValueT));
for (i=0;i<nbitflags;i++)
bitflags[i]=0;
j = 0;
for(i=1;i<n;i++)
if (sorted[i]!=sorted[j]){
if (i>j+1){
for (;j<i;j++)
bitflags[(index[j]-1)/BITS_INTEGER64] |= (RIGHTBIT_INTEGER64 << ((index[j]-1) % BITS_INTEGER64));
}else{
j = i;
}
}
if (i>j+1){
for (;j<i;j++)
bitflags[(index[j]-1)/BITS_INTEGER64] |= (RIGHTBIT_INTEGER64 << ((index[j]-1) % BITS_INTEGER64));
}
j = 0;
for(i=0;i<n;i++)
if ((bitflags[i/BITS_INTEGER64] & (RIGHTBIT_INTEGER64 << (i % BITS_INTEGER64))))
ret[j++] = i+1;
R_Busy(0);
}
return ret_;
}
SEXP r_ram_integer64_ordertie_asc(
SEXP table_ /* sorted table vector */
, SEXP order_ /* sorted table vector */
, SEXP ret_
)
{
int i,j,pos,n = LENGTH(table_);
ValueT *table = (ValueT *) REAL(table_);
IndexT *index = INTEGER(order_);
IndexT * ret = INTEGER(ret_);
if (n){
R_Busy(1);
IndexT nbitflags = n/BITS_INTEGER64+(n%BITS_INTEGER64 ? 1 : 0);
ValueT *bitflags;
bitflags = (ValueT *) R_alloc(nbitflags, sizeof(ValueT));
for (i=0;i<nbitflags;i++)
bitflags[i]=0;
j = 0;
pos = index[0]-1;
for(i=1;i<n;i++)
if (table[index[i]-1]!=table[pos]){
if (i>j+1){
for (;j<i;j++)
bitflags[(index[j]-1)/BITS_INTEGER64] |= (RIGHTBIT_INTEGER64 << ((index[j]-1) % BITS_INTEGER64));
}else{
j = i;
}
pos = index[i]-1;
}
if (i>j+1){
for (;j<i;j++)
bitflags[(index[j]-1)/BITS_INTEGER64] |= (RIGHTBIT_INTEGER64 << ((index[j]-1) % BITS_INTEGER64));
}
j = 0;
for(i=0;i<n;i++)
if ((bitflags[i/BITS_INTEGER64] & (RIGHTBIT_INTEGER64 << (i % BITS_INTEGER64))))
ret[j++] = i+1;
R_Busy(0);
}
return ret_;
}
SEXP r_ram_integer64_sortsrt(
SEXP x_ /* sorted data vector */
, SEXP na_count_ /* logical scalar */
, SEXP na_last_ /* logical scalar */
, SEXP decreasing_ /* logical scalar */
, SEXP ret_ /* logical scalar */
)
{
R_Busy(1);
DEBUG_INIT
int i,j,l,r,n = LENGTH(x_);
Rboolean na_count = asInteger(na_count_);
Rboolean na_last = asLogical(na_last_);
Rboolean decreasing = asLogical(decreasing_);
ValueT *sorted;
sorted = (ValueT *) REAL(x_);
ValueT *ret;
ret = (ValueT *) REAL(ret_);
if (na_last){
for (i=0,j=n-na_count;i<na_count;i++,j++)
ret[j] = sorted[i];
}else{
for (i=0,j=0;i<na_count;i++,j++)
ret[j] = sorted[i];
ret += na_count;
}
sorted += na_count;
n = n - na_count;
if (decreasing){
for(l=n-2,r=n-1,j=0;l>=0;l--)
if (sorted[l]!=sorted[r]){
for (i=l+1;i<=r;i++,j++)
ret[j] = sorted[i];
r=l;
}
for (i=l+1;i<=r;i++,j++)
ret[j] = sorted[i];
}else{
for (i=0,j=0;i<n;i++,j++)
ret[j] = sorted[i];
}
R_Busy(0);
return ret_;
}
SEXP r_ram_integer64_sortorderord(
SEXP x_ /* sorted data vector */
, SEXP index_ /* index vector */
, SEXP na_count_ /* logical scalar */
, SEXP na_last_ /* logical scalar */
, SEXP decreasing_ /* logical scalar */
, SEXP ret_ /* logical scalar */
)
{
R_Busy(1);
DEBUG_INIT
int i,j,l,r,n = LENGTH(x_);
Rboolean na_count = asInteger(na_count_);
Rboolean na_last = asLogical(na_last_);
Rboolean decreasing = asLogical(decreasing_);
ValueT *sorted;
sorted = (ValueT *) REAL(x_);
IndexT *index = INTEGER(index_);
IndexT *ret = INTEGER(ret_);
if (na_last){
for (i=0,j=n-na_count;i<na_count;i++,j++)
ret[j] = index[i];
}else{
for (i=0,j=0;i<na_count;i++,j++)
ret[j] = index[i];
ret += na_count;
}
index += na_count;
n = n - na_count;
if (decreasing){
sorted += na_count;
for(l=n-2,r=n-1,j=0;l>=0;l--)
if (sorted[l]!=sorted[r]){
for (i=l+1;i<=r;i++,j++)
ret[j] = index[i];
r=l;
}
for (i=l+1;i<=r;i++,j++)
ret[j] = index[i];
}else{
for (i=0,j=0;i<n;i++,j++)
ret[j] = index[i];
}
R_Busy(0);
return ret_;
}
SEXP r_ram_integer64_orderord(
SEXP x_ /* sorted data vector */
, SEXP index_ /* index vector */
, SEXP na_count_ /* logical scalar */
, SEXP na_last_ /* logical scalar */
, SEXP decreasing_ /* logical scalar */
, SEXP ret_ /* logical scalar */
)
{
R_Busy(1);
DEBUG_INIT
int i,j,l,r,n = LENGTH(x_);
Rboolean na_count = asInteger(na_count_);
Rboolean na_last = asLogical(na_last_);
Rboolean decreasing = asLogical(decreasing_);
ValueT *data;
data = (ValueT *) REAL(x_);
IndexT *index = INTEGER(index_);
IndexT *ret = INTEGER(ret_);
if (na_last){
for (i=0,j=n-na_count;i<na_count;i++,j++)
ret[j] = index[i];
}else{
for (i=0,j=0;i<na_count;i++,j++)
ret[j] = index[i];
ret += na_count;
}
index += na_count;
n = n - na_count;
if (decreasing){
data += na_count;
for(l=n-2,r=n-1,j=0;l>=0;l--)
if (data[index[l]]!=data[index[r]]){
for (i=l+1;i<=r;i++,j++)
ret[j] = index[i];
r=l;
}
for (i=l+1;i<=r;i++,j++)
ret[j] = index[i];
}else{
for (i=0,j=0;i<n;i++,j++)
ret[j] = index[i];
}
R_Busy(0);
return ret_;
}
|