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
|
// R specific swig components
/*
Vectors
*/
%fragment("StdVectorTraits","header",fragment="StdSequenceTraits")
%{
namespace swig {
// vectors of doubles
template <>
struct traits_from_ptr<std::vector<double> > {
static SEXP from (std::vector<double > *val, int owner = 0) {
SEXP result;
PROTECT(result = Rf_allocVector(REALSXP, val->size()));
for (unsigned pos = 0; pos < val->size(); pos++)
{
NUMERIC_POINTER(result)[pos] = ((*val)[pos]);
}
UNPROTECT(1);
return(result);
}
};
// vectors of floats
template <>
struct traits_from_ptr<std::vector<float> > {
static SEXP from (std::vector<float > *val, int owner = 0) {
SEXP result;
PROTECT(result = Rf_allocVector(REALSXP, val->size()));
for (unsigned pos = 0; pos < val->size(); pos++)
{
NUMERIC_POINTER(result)[pos] = ((*val)[pos]);
}
UNPROTECT(1);
return(result);
}
};
// vectors of unsigned 8bit int
template <>
struct traits_from_ptr<std::vector<unsigned char> > {
static SEXP from (std::vector<unsigned char > *val, int owner = 0) {
SEXP result;
PROTECT(result = Rf_allocVector(INTSXP, val->size()));
for (unsigned pos = 0; pos < val->size(); pos++)
{
INTEGER_POINTER(result)[pos] = ((*val)[pos]);
}
UNPROTECT(1);
return(result);
}
};
// vectors of 8bit int
template <>
struct traits_from_ptr<std::vector<signed char> > {
static SEXP from (std::vector<signed char > *val, int owner = 0) {
SEXP result;
PROTECT(result = Rf_allocVector(INTSXP, val->size()));
for (unsigned pos = 0; pos < val->size(); pos++)
{
INTEGER_POINTER(result)[pos] = ((*val)[pos]);
}
UNPROTECT(1);
return(result);
}
};
// vectors of unsigned 16bit int
template <>
struct traits_from_ptr<std::vector<unsigned short int> > {
static SEXP from (std::vector<unsigned short int > *val, int owner = 0) {
SEXP result;
PROTECT(result = Rf_allocVector(INTSXP, val->size()));
for (unsigned pos = 0; pos < val->size(); pos++)
{
INTEGER_POINTER(result)[pos] = ((*val)[pos]);
}
UNPROTECT(1);
return(result);
}
};
// vectors of 16bit int
template <>
struct traits_from_ptr<std::vector<short int> > {
static SEXP from (std::vector<short int > *val, int owner = 0) {
SEXP result;
PROTECT(result = Rf_allocVector(INTSXP, val->size()));
for (unsigned pos = 0; pos < val->size(); pos++)
{
INTEGER_POINTER(result)[pos] = ((*val)[pos]);
}
UNPROTECT(1);
return(result);
}
};
// vectors of 32 bit unsigned int
template <>
struct traits_from_ptr<std::vector<unsigned int> > {
static SEXP from (std::vector<unsigned int> *val, int owner = 0) {
SEXP result;
PROTECT(result = Rf_allocVector(INTSXP, val->size()));
for (unsigned pos = 0; pos < val->size(); pos++)
{
INTEGER_POINTER(result)[pos] = ((*val)[pos]);
}
UNPROTECT(1);
return(result);
}
};
// vectors of 32bit int
template <>
struct traits_from_ptr<std::vector<int> > {
static SEXP from (std::vector<int > *val, int owner = 0) {
SEXP result;
PROTECT(result = Rf_allocVector(INTSXP, val->size()));
for (unsigned pos = 0; pos < val->size(); pos++)
{
INTEGER_POINTER(result)[pos] = ((*val)[pos]);
}
UNPROTECT(1);
return(result);
}
};
// vectors of 64 bit unsigned int
#if defined(SWIGWORDSIZE64)
template <>
struct traits_from_ptr<std::vector<unsigned long int> > {
static SEXP from (std::vector<unsigned long int> *val, int owner = 0) {
SEXP result;
PROTECT(result = Rf_allocVector(INTSXP, val->size()));
for (unsigned pos = 0; pos < val->size(); pos++)
{
INTEGER_POINTER(result)[pos] = ((*val)[pos]);
}
UNPROTECT(1);
return(result);
}
};
// vectors of 64 bit int
template <>
struct traits_from_ptr<std::vector<long int> > {
static SEXP from (std::vector<long int> *val, int owner = 0) {
SEXP result;
PROTECT(result = Rf_allocVector(INTSXP, val->size()));
for (unsigned pos = 0; pos < val->size(); pos++)
{
INTEGER_POINTER(result)[pos] = ((*val)[pos]);
}
UNPROTECT(1);
return(result);
}
};
#else
template <>
struct traits_from_ptr<std::vector<unsigned long long int> > {
static SEXP from (std::vector<unsigned long long int> *val, int owner = 0) {
SEXP result;
PROTECT(result = Rf_allocVector(INTSXP, val->size()));
for (unsigned pos = 0; pos < val->size(); pos++)
{
INTEGER_POINTER(result)[pos] = ((*val)[pos]);
}
UNPROTECT(1);
return(result);
}
};
// vectors of 64 bit int
template <>
struct traits_from_ptr<std::vector<long long int> > {
static SEXP from (std::vector<long long int> *val, int owner = 0) {
SEXP result;
PROTECT(result = Rf_allocVector(INTSXP, val->size()));
for (unsigned pos = 0; pos < val->size(); pos++)
{
INTEGER_POINTER(result)[pos] = ((*val)[pos]);
}
UNPROTECT(1);
return(result);
}
};
#endif
// vectors of bool
template <>
struct traits_from_ptr<std::vector<bool> > {
static SEXP from (std::vector<bool> *val, int owner = 0) {
SEXP result;
PROTECT(result = Rf_allocVector(LGLSXP, val->size()));
for (unsigned pos = 0; pos < val->size(); pos++)
{
LOGICAL_POINTER(result)[pos] = ((*val)[pos]);
}
UNPROTECT(1);
return(result);
}
};
// vectors of strings
template <>
struct traits_from_ptr<std::vector<std::basic_string<char> > > {
static SEXP from (std::vector<std::basic_string<char> > *val, int owner = 0) {
SEXP result;
PROTECT(result = Rf_allocVector(STRSXP, val->size()));
for (unsigned pos = 0; pos < val->size(); pos++)
{
CHARACTER_POINTER(result)[pos] = Rf_mkChar(((*val)[pos]).c_str());
}
UNPROTECT(1);
return(result);
}
};
// catch all that does everything with vectors
template <typename T>
struct traits_from_ptr< std::vector< T > > {
static SEXP from (std::vector< T > *val, int owner = 0) {
return SWIG_R_NewPointerObj(val, type_info< std::vector< T > >(), owner);
}
};
/////////////////////////////////////////////////
template <>
struct traits_asptr < std::vector<double> > {
static int asptr(SEXP obj, std::vector<double> **val) {
std::vector<double> *p;
// not sure how to check the size of the SEXP obj is correct
unsigned int sexpsz = Rf_length(obj);
p = new std::vector<double>(sexpsz);
double *S = NUMERIC_POINTER(obj);
for (unsigned pos = 0; pos < p->size(); pos++)
{
(*p)[pos] = static_cast<double>(S[pos]);
}
int res = SWIG_OK;
if (SWIG_IsOK(res)) {
if (val) *val = p;
}
return res;
}
};
template <>
struct traits_asptr < std::vector<float> > {
static int asptr(SEXP obj, std::vector<float> **val) {
std::vector<float> *p;
// not sure how to check the size of the SEXP obj is correct
unsigned int sexpsz = Rf_length(obj);
p = new std::vector<float>(sexpsz);
double *S = NUMERIC_POINTER(obj);
for (unsigned pos = 0; pos < p->size(); pos++)
{
(*p)[pos] = static_cast<double>(S[pos]);
}
int res = SWIG_OK;
if (SWIG_IsOK(res)) {
if (val) *val = p;
}
return res;
}
};
// 8 bit integer types
template <>
struct traits_asptr < std::vector<unsigned char> > {
static int asptr(SEXP obj, std::vector<unsigned char> **val) {
std::vector<unsigned char> *p;
unsigned int sexpsz = Rf_length(obj);
p = new std::vector<unsigned char>(sexpsz);
SEXP coerced;
PROTECT(coerced = Rf_coerceVector(obj, INTSXP));
int *S = INTEGER_POINTER(coerced);
for (unsigned pos = 0; pos < p->size(); pos++)
{
(*p)[pos] = static_cast<unsigned char>(S[pos]);
}
int res = SWIG_OK;
if (SWIG_IsOK(res)) {
if (val) *val = p;
}
UNPROTECT(1);
return res;
}
};
template <>
struct traits_asptr < std::vector<signed char> > {
static int asptr(SEXP obj, std::vector<signed char> **val) {
std::vector<signed char> *p;
// not sure how to check the size of the SEXP obj is correct
int sexpsz = Rf_length(obj);
p = new std::vector<signed char>(sexpsz);
SEXP coerced;
PROTECT(coerced = Rf_coerceVector(obj, INTSXP));
int *S = INTEGER_POINTER(coerced);
for (unsigned pos = 0; pos < p->size(); pos++)
{
(*p)[pos] = static_cast<signed char>(S[pos]);
}
int res = SWIG_OK;
if (SWIG_IsOK(res)) {
if (val) *val = p;
}
UNPROTECT(1);
return res;
}
};
// 16 bit integer types
template <>
struct traits_asptr < std::vector<unsigned short int> > {
static int asptr(SEXP obj, std::vector<unsigned short int> **val) {
std::vector<unsigned short int> *p;
unsigned int sexpsz = Rf_length(obj);
p = new std::vector<unsigned short int>(sexpsz);
SEXP coerced;
PROTECT(coerced = Rf_coerceVector(obj, INTSXP));
int *S = INTEGER_POINTER(coerced);
for (unsigned pos = 0; pos < p->size(); pos++)
{
(*p)[pos] = static_cast<unsigned short int>(S[pos]);
}
int res = SWIG_OK;
if (SWIG_IsOK(res)) {
if (val) *val = p;
}
UNPROTECT(1);
return res;
}
};
template <>
struct traits_asptr < std::vector<short int> > {
static int asptr(SEXP obj, std::vector<short int> **val) {
std::vector<short int> *p;
// not sure how to check the size of the SEXP obj is correct
int sexpsz = Rf_length(obj);
p = new std::vector<short int>(sexpsz);
SEXP coerced;
PROTECT(coerced = Rf_coerceVector(obj, INTSXP));
int *S = INTEGER_POINTER(coerced);
for (unsigned pos = 0; pos < p->size(); pos++)
{
(*p)[pos] = static_cast<short int>(S[pos]);
}
int res = SWIG_OK;
if (SWIG_IsOK(res)) {
if (val) *val = p;
}
UNPROTECT(1);
return res;
}
};
// 32 bit integer types
template <>
struct traits_asptr < std::vector<unsigned int> > {
static int asptr(SEXP obj, std::vector<unsigned int> **val) {
std::vector<unsigned int> *p;
unsigned int sexpsz = Rf_length(obj);
p = new std::vector<unsigned int>(sexpsz);
SEXP coerced;
PROTECT(coerced = Rf_coerceVector(obj, INTSXP));
int *S = INTEGER_POINTER(coerced);
for (unsigned pos = 0; pos < p->size(); pos++)
{
(*p)[pos] = static_cast<unsigned int>(S[pos]);
}
int res = SWIG_OK;
if (SWIG_IsOK(res)) {
if (val) *val = p;
}
UNPROTECT(1);
return res;
}
};
template <>
struct traits_asptr < std::vector<int> > {
static int asptr(SEXP obj, std::vector<int> **val) {
std::vector<int> *p;
// not sure how to check the size of the SEXP obj is correct
int sexpsz = Rf_length(obj);
p = new std::vector<int>(sexpsz);
SEXP coerced;
PROTECT(coerced = Rf_coerceVector(obj, INTSXP));
int *S = INTEGER_POINTER(coerced);
for (unsigned pos = 0; pos < p->size(); pos++)
{
(*p)[pos] = static_cast<int>(S[pos]);
}
int res = SWIG_OK;
if (SWIG_IsOK(res)) {
if (val) *val = p;
}
UNPROTECT(1);
return res;
}
};
#if defined(SWIGWORDSIZE64)
// 64 bit integer types
template <>
struct traits_asptr < std::vector<unsigned long int> > {
static int asptr(SEXP obj, std::vector<unsigned long int> **val) {
std::vector<unsigned long int> *p;
unsigned int sexpsz = Rf_length(obj);
p = new std::vector<unsigned long int>(sexpsz);
SEXP coerced;
PROTECT(coerced = Rf_coerceVector(obj, INTSXP));
int *S = INTEGER_POINTER(coerced);
for (unsigned pos = 0; pos < p->size(); pos++)
{
(*p)[pos] = static_cast<unsigned long int>(S[pos]);
}
int res = SWIG_OK;
if (SWIG_IsOK(res)) {
if (val) *val = p;
}
UNPROTECT(1);
return res;
}
};
template <>
struct traits_asptr < std::vector<long int> > {
static int asptr(SEXP obj, std::vector<long int> **val) {
std::vector<long int> *p;
// not sure how to check the size of the SEXP obj is correct
int sexpsz = Rf_length(obj);
p = new std::vector<long int>(sexpsz);
SEXP coerced;
PROTECT(coerced = Rf_coerceVector(obj, INTSXP));
int *S = INTEGER_POINTER(coerced);
for (unsigned pos = 0; pos < p->size(); pos++)
{
(*p)[pos] = static_cast<long int>(S[pos]);
}
int res = SWIG_OK;
if (SWIG_IsOK(res)) {
if (val) *val = p;
}
UNPROTECT(1);
return res;
}
};
#else
// 64 bit integer types
template <>
struct traits_asptr < std::vector<unsigned long long int> > {
static int asptr(SEXP obj, std::vector<unsigned long long int> **val) {
std::vector<unsigned long long int> *p;
unsigned int sexpsz = Rf_length(obj);
p = new std::vector<unsigned long long int>(sexpsz);
SEXP coerced;
PROTECT(coerced = Rf_coerceVector(obj, INTSXP));
int *S = INTEGER_POINTER(coerced);
for (unsigned pos = 0; pos < p->size(); pos++)
{
(*p)[pos] = static_cast<unsigned long long int>(S[pos]);
}
int res = SWIG_OK;
if (SWIG_IsOK(res)) {
if (val) *val = p;
}
UNPROTECT(1);
return res;
}
};
template <>
struct traits_asptr < std::vector<long long int> > {
static int asptr(SEXP obj, std::vector<long long int> **val) {
std::vector<long long int> *p;
// not sure how to check the size of the SEXP obj is correct
int sexpsz = Rf_length(obj);
p = new std::vector<long long int>(sexpsz);
SEXP coerced;
PROTECT(coerced = Rf_coerceVector(obj, INTSXP));
int *S = INTEGER_POINTER(coerced);
for (unsigned pos = 0; pos < p->size(); pos++)
{
(*p)[pos] = static_cast<long long int>(S[pos]);
}
int res = SWIG_OK;
if (SWIG_IsOK(res)) {
if (val) *val = p;
}
UNPROTECT(1);
return res;
}
};
#endif
template <>
struct traits_asptr < std::vector<bool> > {
static int asptr(SEXP obj, std::vector<bool> **val) {
std::vector<bool> *p;
// not sure how to check the size of the SEXP obj is correct
int sexpsz = Rf_length(obj);
p = new std::vector<bool>(sexpsz);
SEXP coerced;
PROTECT(coerced = Rf_coerceVector(obj, LGLSXP));
int *S = LOGICAL_POINTER(coerced);
for (unsigned pos = 0; pos < p->size(); pos++)
{
(*p)[pos] = static_cast<bool>(S[pos]);
}
int res = SWIG_OK;
if (SWIG_IsOK(res)) {
if (val) *val = p;
}
UNPROTECT(1);
return res;
}
};
template <>
struct traits_asptr < std::vector<std::basic_string<char> > > {
static int asptr(SEXP obj, std::vector<std::basic_string<char> > **val) {
std::vector<std::basic_string<char> > *p;
// R character vectors are STRSXP containing CHARSXP
// access a CHARSXP using STRING_ELT
int sexpsz = Rf_length(obj);
p = new std::vector<std::basic_string<char> >(sexpsz);
SEXP coerced;
PROTECT(coerced = Rf_coerceVector(obj, STRSXP));
//SEXP *S = CHARACTER_POINTER(coerced);
for (unsigned pos = 0; pos < p->size(); pos++)
{
const char * thecstring = CHAR(STRING_ELT(coerced, pos));
(*p)[pos] = std::basic_string<char>(thecstring);
}
int res = SWIG_OK;
if (SWIG_IsOK(res)) {
if (val) *val = p;
}
UNPROTECT(1);
return res;
}
};
// catchall for R to vector conversion
template <typename T>
struct traits_asptr < std::vector<T> > {
static int asptr(SEXP obj, std::vector<T> **val) {
std::vector<T> *p;
int res = SWIG_R_ConvertPtr(obj, (void**)&p, type_info< std::vector<T> >(), 0);
if (SWIG_IsOK(res)) {
if (val) *val = p;
}
return res;
}
};
// now for vectors of vectors. These will be represented as lists of vectors on the
// catch all that does everything with vectors
template <>
struct traits_from_ptr<std::vector<std::vector<unsigned int> > > {
static SEXP from (std::vector< std::vector<unsigned int> > *val, int owner = 0) {
SEXP result;
// allocate the R list
PROTECT(result = Rf_allocVector(VECSXP, val->size()));
for (unsigned pos = 0; pos < val->size(); pos++)
{
// allocate the R vector
SET_VECTOR_ELT(result, pos, Rf_allocVector(INTSXP, val->at(pos).size()));
// Fill the R vector
for (unsigned vpos = 0; vpos < val->at(pos).size(); ++vpos)
{
INTEGER_POINTER(VECTOR_ELT(result, pos))[vpos] = static_cast<int>(val->at(pos).at(vpos));
}
}
UNPROTECT(1);
return(result);
}
};
template <>
struct traits_from_ptr<std::vector<std::vector<int> > > {
static SEXP from (std::vector< std::vector<int > > *val, int owner = 0) {
SEXP result;
// allocate the R list
PROTECT(result = Rf_allocVector(VECSXP, val->size()));
for (unsigned pos = 0; pos < val->size(); pos++)
{
// allocate the R vector
SET_VECTOR_ELT(result, pos, Rf_allocVector(INTSXP, val->at(pos).size()));
// Fill the R vector
for (unsigned vpos = 0; vpos < val->at(pos).size(); ++vpos)
{
INTEGER_POINTER(VECTOR_ELT(result, pos))[vpos] = static_cast<int>(val->at(pos).at(vpos));
}
}
UNPROTECT(1);
return(result);
}
};
template <>
struct traits_from_ptr<std::vector<std::vector<float> > > {
static SEXP from (std::vector< std::vector<float > > *val, int owner = 0) {
SEXP result;
// allocate the R list
PROTECT(result = Rf_allocVector(VECSXP, val->size()));
for (unsigned pos = 0; pos < val->size(); pos++)
{
// allocate the R vector
SET_VECTOR_ELT(result, pos, Rf_allocVector(REALSXP, val->at(pos).size()));
// Fill the R vector
for (unsigned vpos = 0; vpos < val->at(pos).size(); ++vpos)
{
NUMERIC_POINTER(VECTOR_ELT(result, pos))[vpos] = static_cast<double>(val->at(pos).at(vpos));
}
}
UNPROTECT(1);
return(result);
}
};
template <>
struct traits_from_ptr<std::vector<std::vector<double> > > {
static SEXP from (std::vector< std::vector<double > > *val, int owner = 0) {
SEXP result;
// allocate the R list
PROTECT(result = Rf_allocVector(VECSXP, val->size()));
for (unsigned pos = 0; pos < val->size(); pos++)
{
// allocate the R vector
SET_VECTOR_ELT(result, pos, Rf_allocVector(REALSXP, val->at(pos).size()));
// Fill the R vector
for (unsigned vpos = 0; vpos < val->at(pos).size(); ++vpos)
{
NUMERIC_POINTER(VECTOR_ELT(result, pos))[vpos] = static_cast<double>(val->at(pos).at(vpos));
}
}
UNPROTECT(1);
return(result);
}
};
template <>
struct traits_from_ptr<std::vector<std::vector<bool> > > {
static SEXP from (std::vector< std::vector<bool> > *val, int owner = 0) {
SEXP result;
// allocate the R list
PROTECT(result = Rf_allocVector(VECSXP, val->size()));
for (unsigned pos = 0; pos < val->size(); pos++)
{
// allocate the R vector
SET_VECTOR_ELT(result, pos, Rf_allocVector(LGLSXP, val->at(pos).size()));
// Fill the R vector
for (unsigned vpos = 0; vpos < val->at(pos).size(); ++vpos)
{
LOGICAL_POINTER(VECTOR_ELT(result, pos))[vpos] = (val->at(pos).at(vpos));
}
}
UNPROTECT(1);
return(result);
}
};
template <typename T>
struct traits_from_ptr< std::vector < std::vector< T > > > {
static SEXP from (std::vector < std::vector< T > > *val, int owner = 0) {
return SWIG_R_NewPointerObj(val, type_info< std::vector < std::vector< T > > >(), owner);
}
};
/////////////////////////////////////////////////////////////////
// R side
template <>
struct traits_asptr < std::vector< std::vector<unsigned int> > > {
static int asptr(SEXP obj, std::vector< std::vector<unsigned int> > **val) {
std::vector <std::vector<unsigned int> > *p;
// this is the length of the list
unsigned int sexpsz = Rf_length(obj);
p = new std::vector< std::vector<unsigned int> > (sexpsz);
for (unsigned listpos = 0; listpos < sexpsz; ++listpos)
{
unsigned vecsize = Rf_length(VECTOR_ELT(obj, listpos));
for (unsigned vpos = 0; vpos < vecsize; ++vpos)
{
(*p)[listpos].push_back(static_cast<int>(INTEGER_POINTER(VECTOR_ELT(obj, listpos))[vpos]));
}
}
int res = SWIG_OK;
if (SWIG_IsOK(res)) {
if (val) *val = p;
}
return res;
}
};
template <>
struct traits_asptr < std::vector< std::vector< int> > > {
static int asptr(SEXP obj, std::vector< std::vector< int> > **val) {
std::vector <std::vector< int> > *p;
// this is the length of the list
unsigned int sexpsz = Rf_length(obj);
p = new std::vector< std::vector< int> > (sexpsz);
for (unsigned listpos = 0; listpos < sexpsz; ++listpos)
{
unsigned vecsize = Rf_length(VECTOR_ELT(obj, listpos));
for (unsigned vpos = 0; vpos < vecsize; ++vpos)
{
(*p)[listpos].push_back(static_cast<int>(INTEGER_POINTER(VECTOR_ELT(obj, listpos))[vpos]));
}
}
int res = SWIG_OK;
if (SWIG_IsOK(res)) {
if (val) *val = p;
}
return res;
}
};
template <>
struct traits_asptr < std::vector< std::vector< float> > > {
static int asptr(SEXP obj, std::vector< std::vector< float> > **val) {
std::vector <std::vector< float> > *p;
// this is the length of the list
unsigned int sexpsz = Rf_length(obj);
p = new std::vector< std::vector< float> > (sexpsz);
for (unsigned listpos = 0; listpos < sexpsz; ++listpos)
{
unsigned vecsize = Rf_length(VECTOR_ELT(obj, listpos));
for (unsigned vpos = 0; vpos < vecsize; ++vpos)
{
(*p)[listpos].push_back(static_cast<float>(NUMERIC_POINTER(VECTOR_ELT(obj, listpos))[vpos]));
}
}
int res = SWIG_OK;
if (SWIG_IsOK(res)) {
if (val) *val = p;
}
return res;
}
};
template <>
struct traits_asptr < std::vector< std::vector< double> > > {
static int asptr(SEXP obj, std::vector< std::vector< double> > **val) {
std::vector <std::vector< double> > *p;
// this is the length of the list
unsigned int sexpsz = Rf_length(obj);
p = new std::vector< std::vector< double> > (sexpsz);
for (unsigned listpos = 0; listpos < sexpsz; ++listpos)
{
unsigned vecsize = Rf_length(VECTOR_ELT(obj, listpos));
for (unsigned vpos = 0; vpos < vecsize; ++vpos)
{
(*p)[listpos].push_back(static_cast<double>(NUMERIC_POINTER(VECTOR_ELT(obj, listpos))[vpos]));
}
}
int res = SWIG_OK;
if (SWIG_IsOK(res)) {
if (val) *val = p;
}
return res;
}
};
template <>
struct traits_asptr < std::vector< std::vector< bool > > > {
static int asptr(SEXP obj, std::vector< std::vector< bool> > **val) {
std::vector <std::vector< bool > > *p;
// this is the length of the list
unsigned int sexpsz = Rf_length(obj);
p = new std::vector< std::vector< bool > > (sexpsz);
for (unsigned listpos = 0; listpos < sexpsz; ++listpos)
{
unsigned vecsize = Rf_length(VECTOR_ELT(obj, listpos));
for (unsigned vpos = 0; vpos < vecsize; ++vpos)
{
(*p)[listpos].push_back(static_cast<bool>(LOGICAL_POINTER(VECTOR_ELT(obj, listpos))[vpos]));
}
}
int res = SWIG_OK;
if (SWIG_IsOK(res)) {
if (val) *val = p;
}
return res;
}
};
// catchall
template <typename T>
struct traits_asptr < std::vector< std::vector<T> > > {
static int asptr(SEXP obj, std::vector< std::vector<T> > **val) {
std::vector< std::vector<T> > *p;
Rprintf("vector of vectors - unsupported content\n");
int res = SWIG_R_ConvertPtr(obj, (void**)&p, type_info< std::vector< std::vector<T> > > (), 0);
if (SWIG_IsOK(res)) {
if (val) *val = p;
}
return res;
}
};
}
%}
#define %swig_vector_methods(Type...) %swig_sequence_methods(Type)
#define %swig_vector_methods_val(Type...) %swig_sequence_methods_val(Type);
%define %traits_type_name(Type...)
%fragment(SWIG_Traits_frag(Type), "header",
fragment="StdTraits",fragment="StdVectorTraits") {
namespace swig {
template <> struct traits< Type > {
typedef pointer_category category;
static const char* type_name() {
return #Type;
}
};
}
}
%enddef
%include <std/std_vector.i>
%typemap_traits_ptr(SWIG_TYPECHECK_VECTOR, std::vector<double>)
%traits_type_name(std::vector<double>)
%typemap("rtypecheck") std::vector<double>, std::vector<double> *, std::vector<double> &
%{ is.numeric($arg) %}
%typemap("rtype") std::vector<double> "numeric"
%typemap("scoercein") std::vector<double>, std::vector<double> *, std::vector<double> & "$input = as.numeric($input);";
%typemap_traits_ptr(SWIG_TYPECHECK_VECTOR, std::vector<float>)
%traits_type_name(std::vector<float>)
// reuse these for float
%typemap("rtype") std::vector<float>, std::vector<float> *, std::vector<float> & = std::vector<double>;
%typemap("rtypecheck") std::vector<float>, std::vector<float> *, std::vector<float> & = std::vector<double>;
%typemap("scoercein") std::vector<float>, std::vector<float> *, std::vector<float> & = std::vector<double>;
%typemap_traits_ptr(SWIG_TYPECHECK_VECTOR, std::vector<bool>);
%traits_type_name(std::vector<bool>);
%typemap("rtypecheck") std::vector<bool>, std::vector<bool> *, std::vector<bool> &
%{ is.logical($arg) %}
%typemap("rtype") std::vector<bool> "logical"
%typemap("scoercein") std::vector<bool> , std::vector<bool> & "$input = as.logical($input);";
%typemap_traits_ptr(SWIG_TYPECHECK_VECTOR, std::vector<int>);
%traits_type_name(std::vector<int>);
%typemap("rtypecheck") std::vector<int>, std::vector<int> *, std::vector<int> &
%{ is.integer($arg) || is.numeric($arg) %}
%typemap("rtype") std::vector<int> "integer"
%typemap("scoercein") std::vector<int> , std::vector<int> *, std::vector<int> & "$input = as.integer($input);";
// strings
%typemap("rtype") std::vector< std::basic_string<char> >,
std::vector< std::basic_string<char> > *,
std::vector< std::basic_string<char> > & "character"
%typemap("rtypecheck") std::vector< std::basic_string<char> >,
std::vector< std::basic_string<char> > *,
std::vector< std::basic_string<char> > &
%{ is.character($arg) %}
%typemap("scoercein") std::vector< std::basic_string<char> >,
std::vector< std::basic_string<char> > *,
std::vector< std::basic_string<char> > & "$input = as.character($input);";
%typemap("scoerceout") std::vector< std::basic_string<char> >,
std::vector< std::basic_string<char> > *,
std::vector< std::basic_string<char> > &
%{ %}
%apply std::vector< std::basic_string<char> > { std::vector< std::string> };
// all the related integer vectors
// signed
%typemap_traits_ptr(SWIG_TYPECHECK_VECTOR, std::vector<signed char>);
%traits_type_name(std::vector<signed char>);
%typemap_traits_ptr(SWIG_TYPECHECK_VECTOR, std::vector<signed short>);
%traits_type_name(std::vector<signed short>);
#if defined(SWIGWORDSIZE64)
%typemap_traits_ptr(SWIG_TYPECHECK_VECTOR, std::vector<long int>);
%traits_type_name(std::vector<long int>);
#else
%typemap_traits_ptr(SWIG_TYPECHECK_VECTOR, std::vector<long long int>);
%traits_type_name(std::vector<long long int>);
#endif
// unsigned
%typemap_traits_ptr(SWIG_TYPECHECK_VECTOR, std::vector<unsigned char>);
%traits_type_name(std::vector<unsigned char>);
%typemap_traits_ptr(SWIG_TYPECHECK_VECTOR, std::vector<unsigned short>);
%traits_type_name(std::vector<unsigned short>);
%typemap_traits_ptr(SWIG_TYPECHECK_VECTOR, std::vector<unsigned int>);
%traits_type_name(std::vector<unsigned int>);
#if defined(SWIGWORDSIZE64)
%typemap_traits_ptr(SWIG_TYPECHECK_VECTOR, std::vector<unsigned long int>);
%traits_type_name(std::vector<unsigned long int>);
#else
%typemap_traits_ptr(SWIG_TYPECHECK_VECTOR, std::vector<unsigned long long int>);
%traits_type_name(std::vector<unsigned long long int>);
#endif
// These R side typemaps are common for integer types
// but we can't use %apply as it will copy the C side ones too
// Also note that we don't seem to be able to use types like
// int_least8_t here.
%typemap("rtype") std::vector<signed char>, std::vector<signed char> *, std::vector<signed char> & = std::vector<int>;
%typemap("rtype") std::vector<signed short>, std::vector<signed short> *, std::vector<signed short> & = std::vector<int>;
%typemap("rtype") std::vector<unsigned char>, std::vector<unsigned char> *, std::vector<unsigned char> & = std::vector<int>;
%typemap("rtype") std::vector<unsigned int>, std::vector<unsigned int> *, std::vector<unsigned int> & = std::vector<int>;
#if defined(SWIGWORDSIZE64)
%typemap("rtype") std::vector<long int>, std::vector<long int> *, std::vector<long int> & = std::vector<int>;
%typemap("rtype") std::vector<unsigned long int>, std::vector<unsigned long int> *, std::vector<unsigned long int> & = std::vector<int>;
#else
%typemap("rtype") std::vector<long long int>, std::vector<long long int> *, std::vector<long long int> & = std::vector<int>;
%typemap("rtype") std::vector<unsigned long long int>, std::vector<unsigned long long int> *, std::vector<unsigned long long int> & = std::vector<int>;
#endif
%typemap("scoercein") std::vector<signed char>, std::vector<signed char> *, std::vector<signed char> & = std::vector<int>;
%typemap("scoercein") std::vector<signed short>, std::vector<signed short> *, std::vector<signed short> & = std::vector<int>;
%typemap("scoercein") std::vector<unsigned char>, std::vector<unsigned char> *, std::vector<unsigned char> & = std::vector<int>;
%typemap("scoercein") std::vector<unsigned int>, std::vector<unsigned int> *, std::vector<unsigned int> & = std::vector<int>;
#if defined(SWIGWORDSIZE64)
%typemap("scoercein") std::vector<long int>, std::vector<long int> *, std::vector<long int> & = std::vector<int>;
%typemap("scoercein") std::vector<unsigned long int>, std::vector<unsigned long int> *, std::vector<unsigned long int> & = std::vector<int>;
#else
%typemap("scoercein") std::vector<long long int>, std::vector<long long int> *, std::vector<long long int> & = std::vector<int>;
%typemap("scoercein") std::vector<unsigned long long int>, std::vector<unsigned long long int> *, std::vector<unsigned long long int> & = std::vector<int>;
#endif
%typemap("rtypecheck") std::vector<signed char>, std::vector<signed char> *, std::vector<signed char> & = std::vector<int>;
%typemap("rtypecheck") std::vector<signed short>, std::vector<signed short> *, std::vector<signed short> & = std::vector<int>;
%typemap("rtypecheck") std::vector<unsigned char>, std::vector<unsigned char> *, std::vector<unsigned char> & = std::vector<int>;
%typemap("rtypecheck") std::vector<unsigned int>, std::vector<unsigned int> *, std::vector<unsigned int> & = std::vector<int>;
#if defined(SWIGWORDSIZE64)
%typemap("rtypecheck") std::vector<long int>, std::vector<long int> *, std::vector<long int> & = std::vector<int>;
%typemap("rtypecheck") std::vector<unsigned long int>, std::vector<unsigned long int> *, std::vector<unsigned long int> & = std::vector<int>;
#else
%typemap("rtypecheck") std::vector<long long int>, std::vector<long long int> *, std::vector<long long int> & = std::vector<int>;
%typemap("rtypecheck") std::vector<unsigned long long int>, std::vector<unsigned long long int> *, std::vector<unsigned long long int> & = std::vector<int>;
#endif
///////////////////////////////////////////////////////////////
%typemap_traits_ptr(SWIG_TYPECHECK_VECTOR, std::vector<std::vector<int> >);
%traits_type_name(std::vector< std::vector<int> >);
%typemap("rtypecheck") std::vector<std::vector<int> >, std::vector<std::vector<int> > *, std::vector<std::vector<int> > &
%{ is.list($arg) && all(sapply($arg , is.integer) || sapply($arg, is.numeric)) %}
%typemap("rtype") std::vector<std::vector<int> >, std::vector<std::vector<int> > *, std::vector<std::vector<int> > & "list"
%typemap("scoercein") std::vector< std::vector<int> >, std::vector<std::vector<int> > *, std::vector<std::vector<int> > & "$input = lapply($input, as.integer);";
%typemap_traits_ptr(SWIG_TYPECHECK_VECTOR, std::vector<std::vector<unsigned int> >);
%traits_type_name(std::vector< std::vector<unsigned int> >);
%typemap("rtypecheck") std::vector<std::vector<unsigned int> >, std::vector<std::vector<unsigned int> > *, std::vector<std::vector<unsigned int> > &
%{ is.list($arg) && all(sapply($arg , is.integer) || sapply($arg, is.numeric)) %}
%typemap("rtype") std::vector<std::vector<unsigned int> >, std::vector<std::vector<unsigned int> > *, std::vector<std::vector<unsigned int> > & "list"
%typemap("scoercein") std::vector< std::vector<unsigned int> >, std::vector<std::vector<int> > *, std::vector<std::vector<unsigned int> > & "$input = lapply($input, as.integer);";
%typemap_traits_ptr(SWIG_TYPECHECK_VECTOR, std::vector<std::vector<float> >);
%traits_type_name(std::vector< std::vector<float> >);
%typemap("rtypecheck") std::vector<std::vector<float> >, std::vector<std::vector<float> > *, std::vector<std::vector<float> > &
%{ is.list($arg) && all(sapply($arg , is.integer) || sapply($arg, is.numeric)) %}
%typemap("rtype") std::vector<std::vector<float> >, std::vector<std::vector<float> > *, std::vector<std::vector<float> > "list"
%typemap("scoercein") std::vector< std::vector<float> >, std::vector<std::vector<float> > *, std::vector<std::vector<float> > & "$input = lapply($input, as.numeric);";
%typemap_traits_ptr(SWIG_TYPECHECK_VECTOR, std::vector<std::vector<double> >);
%traits_type_name(std::vector< std::vector<double> >);
%typemap("rtypecheck") std::vector<std::vector<double> >, std::vector<std::vector<double> > *, std::vector<std::vector<double> > &
%{ is.list($arg) && all(sapply($arg , is.integer) || sapply($arg, is.numeric)) %}
%typemap("rtype") std::vector<std::vector<double> >, std::vector<std::vector<double> > *, std::vector<std::vector<double> > & "list"
%typemap("scoercein") std::vector< std::vector<double> >, std::vector<std::vector<double> > *, std::vector<std::vector<double> > &
"$input = lapply($input, as.numeric);";
%typemap_traits_ptr(SWIG_TYPECHECK_VECTOR, std::vector<std::vector<bool> >);
%traits_type_name(std::vector< std::vector<bool> >);
%typemap("rtypecheck") std::vector<std::vector<bool> >, std::vector<std::vector<bool> > *, std::vector<std::vector<bool> > &
%{ is.list($arg) && all(sapply($arg , is.integer) || sapply($arg, is.numeric)) %}
%typemap("rtype") std::vector<std::vector<bool> >, std::vector<std::vector<bool> > *, std::vector<std::vector<bool> > & "list"
%typemap("scoercein") std::vector< std::vector<bool> >, std::vector<std::vector<bool> > *, std::vector<std::vector<bool> > & "$input = lapply($input, as.logical);";
// we don't want these to be given R classes as they
// have already been turned into R vectors.
%typemap(scoerceout) std::vector<double>,
std::vector<double>*,
std::vector<double>&,
std::vector<float> ,
std::vector<float>*,
std::vector<float> ,
std::vector<signed char>,
std::vector<signed char>*,
std::vector<signed char>&,
std::vector<signed short>,
std::vector<signed short>*,
std::vector<signed short>&,
std::vector<int>,
std::vector<int>*,
std::vector<int>&,
std::vector<unsigned char>,
std::vector<unsigned char>*,
std::vector<unsigned char>&,
std::vector<unsigned short>,
std::vector<unsigned short>*,
std::vector<unsigned short>&,
std::vector<unsigned int>,
std::vector<unsigned int>*,
std::vector<unsigned int>&,
std::vector<bool>,
std::vector<bool>*,
std::vector<bool>&,
// vectors of vectors
std::vector< std::vector<unsigned int> >,
std::vector< std::vector<unsigned int> >*,
std::vector< std::vector<unsigned int> >&,
std::vector< std::vector<int> >,
std::vector< std::vector<int> >*,
std::vector< std::vector<int> >&,
std::vector< std::vector<float> >,
std::vector< std::vector<float> >*,
std::vector< std::vector<float> >&,
std::vector< std::vector<double> >,
std::vector< std::vector<double> >*,
std::vector< std::vector<double> >&,
std::vector< std::vector<bool> >,
std::vector< std::vector<bool> >*,
std::vector< std::vector<bool> >&
%{ %}
#if defined(SWIGWORDSIZE64)
%typemap(scoerceout) std::vector<long int>,
std::vector<long int>*,
std::vector<long int>&,
std::vector<unsigned long int>,
std::vector<unsigned long int>*,
std::vector<unsigned long int>&
%{ %}
#else
%typemap(scoerceout) std::vector<long long int>,
std::vector<long long int>*,
std::vector<long long int>&,
std::vector<unsigned long long int>,
std::vector<unsigned long long int>*,
std::vector<unsigned long long int>&
%{ %}
#endif
|