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
|
// -*- mode: C++; c-indent-level: 4; c-basic-offset: 4; indent-tabs-mode: nil; -*-
/* :tabSize=4:indentSize=4:noTabs=false:folding=explicit:collapseFolds=1: */
//
// wrap.h: Rcpp R/C++ interface class library -- wrap implementations
//
// Copyright (C) 2010 - 2017 Dirk Eddelbuettel and Romain Francois
//
// This file is part of Rcpp.
//
// Rcpp is free software: you can redistribute it and/or modify it
// under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 2 of the License, or
// (at your option) any later version.
//
// Rcpp is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Rcpp. If not, see <http://www.gnu.org/licenses/>.
#ifndef Rcpp_internal_wrap_h
#define Rcpp_internal_wrap_h
#include <iterator>
// this is a private header, included in RcppCommon.h
// don't include it directly
namespace Rcpp {
namespace RcppEigen {
template <typename T> SEXP eigen_wrap(const T& object);
}
template <typename T> SEXP wrap(const T& object);
template <typename T> class CustomImporter;
namespace internal {
inline SEXP make_charsexp__impl__wstring(const wchar_t* data) {
char* buffer = get_string_buffer();
wcstombs(buffer, data, MAXELTSIZE);
return Rf_mkChar(buffer);
}
inline SEXP make_charsexp__impl__wstring(wchar_t data) {
wchar_t x[2]; x[0] = data; x[1] = '\0';
char* buffer = get_string_buffer();
wcstombs(buffer, x, MAXELTSIZE);
return Rf_mkChar(buffer);
}
inline SEXP make_charsexp__impl__wstring(const std::wstring& st) {
return make_charsexp__impl__wstring(st.data());
}
inline SEXP make_charsexp__impl__cstring(const char* data) {
return Rf_mkChar(data);
}
inline SEXP make_charsexp__impl__cstring(char data) {
char x[2]; x[0] = data; x[1] = '\0';
return Rf_mkChar(x);
}
inline SEXP make_charsexp__impl__cstring(const std::string& st) {
return make_charsexp__impl__cstring(st.c_str());
}
#if __cplusplus >= 201703L
inline SEXP make_charsexp__impl__cstring(std::string_view st) {
return Rf_mkCharLen(st.data(), st.size());
}
#endif
template <typename T>
inline SEXP make_charsexp__impl(const T& s, Rcpp::traits::true_type) {
return make_charsexp__impl__wstring(s);
}
template <typename T>
inline SEXP make_charsexp__impl(const T& s, Rcpp::traits::false_type) {
return make_charsexp__impl__cstring(s);
}
template <typename T>
inline SEXP make_charsexp(const T& s) {
return make_charsexp__impl<T>(s, typename Rcpp::traits::is_wide_string<T>::type());
}
template <>
inline SEXP make_charsexp<Rcpp::String>(const Rcpp::String&);
template <typename InputIterator> SEXP range_wrap(InputIterator first, InputIterator last);
template <typename InputIterator> SEXP rowmajor_wrap(InputIterator first, int nrow, int ncol);
// {{{ range wrap
// {{{ unnamed range wrap
/**
* Range based primitive wrap implementation. used when
* - T is a primitive type, indicated by the r_type_traits
* - T needs a static_cast to be of the type suitable to fit in the R vector
*
* This produces an unnamed vector of the appropriate type using the
* std::transform algorithm
*/
template <typename InputIterator, typename T>
inline SEXP primitive_range_wrap__impl(InputIterator first, InputIterator last,
::Rcpp::traits::true_type) {
size_t size = std::distance(first, last);
const int RTYPE = ::Rcpp::traits::r_sexptype_traits<T>::rtype;
Shield<SEXP> x(Rf_allocVector(RTYPE, size));
std::transform(first, last, r_vector_start<RTYPE>(x), caster< T,
typename ::Rcpp::traits::storage_type<RTYPE>::type >);
return wrap_extra_steps<T>(x);
}
template <typename InputIterator, typename T>
inline SEXP primitive_range_wrap__impl__nocast(InputIterator first, InputIterator last,
std::random_access_iterator_tag) {
size_t size = std::distance(first, last);
const int RTYPE = ::Rcpp::traits::r_sexptype_traits<T>::rtype;
Shield<SEXP> x(Rf_allocVector(RTYPE, size));
typedef typename ::Rcpp::traits::storage_type<RTYPE>::type STORAGE;
R_xlen_t __trip_count = size >> 2;
STORAGE* start = r_vector_start<RTYPE>(x);
R_xlen_t i = 0;
for (; __trip_count > 0; --__trip_count) {
start[i] = first[i]; i++;
start[i] = first[i]; i++;
start[i] = first[i]; i++;
start[i] = first[i]; i++;
}
switch (size - i) {
case 3:
start[i] = first[i]; i++;
// fallthrough
case 2:
start[i] = first[i]; i++;
// fallthrough
case 1:
start[i] = first[i]; i++;
// fallthrough
case 0:
default:
{}
}
return wrap_extra_steps<T>(x);
}
template <typename InputIterator, typename T>
inline SEXP primitive_range_wrap__impl__nocast(InputIterator first, InputIterator last,
std::input_iterator_tag) {
size_t size = std::distance(first, last);
const int RTYPE = ::Rcpp::traits::r_sexptype_traits<T>::rtype;
Shield<SEXP> x(Rf_allocVector(RTYPE, size));
std::copy(first, last, r_vector_start<RTYPE>(x));
return wrap_extra_steps<T>(x);
}
/**
* Range based primitive wrap implementation. used when :
* - T is a primitive type
* - T does not need a cast
*
* This produces an unnamed vector of the appropriate type using
* the std::copy algorithm
*/
template <typename InputIterator, typename T>
inline SEXP primitive_range_wrap__impl(InputIterator first, InputIterator last,
::Rcpp::traits::false_type) {
return primitive_range_wrap__impl__nocast<InputIterator,T>(first, last, typename std::iterator_traits<InputIterator>::iterator_category());
}
/**
* Range based wrap implementation that deals with iterator over
* primitive types (int, double, etc ...)
*
* This produces an unnamed vector of the appropriate type
*/
template <typename InputIterator, typename T>
inline SEXP range_wrap_dispatch___impl(InputIterator first, InputIterator last, ::Rcpp::traits::r_type_primitive_tag) {
return primitive_range_wrap__impl<InputIterator,T>(first, last, typename ::Rcpp::traits::r_sexptype_needscast<T>());
}
/**
* range based wrap implementation that deals with iterators over
* some type U. each U object is itself wrapped
*
* This produces an unnamed generic vector (list)
*/
template <typename InputIterator, typename T>
inline SEXP range_wrap_dispatch___generic(InputIterator first, InputIterator last) {
size_t size = std::distance(first, last);
Shield<SEXP> x(Rf_allocVector(VECSXP, size));
size_t i =0;
while(i < size) {
SET_VECTOR_ELT(x, i, ::Rcpp::wrap(*first));
i++;
++first;
}
return x;
}
template <typename InputIterator, typename T>
inline SEXP range_wrap_dispatch___impl(InputIterator first, InputIterator last, ::Rcpp::traits::r_type_generic_tag) {
return range_wrap_dispatch___generic<InputIterator, T>(first, last);
}
// modules
template <typename InputIterator, typename T>
inline SEXP range_wrap_dispatch___impl(InputIterator first, InputIterator last, ::Rcpp::traits::r_type_module_object_pointer_tag) {
return range_wrap_dispatch___generic<InputIterator, T>(first, last);
}
template <typename InputIterator, typename T>
inline SEXP range_wrap_dispatch___impl(InputIterator first, InputIterator last, ::Rcpp::traits::r_type_module_object_const_pointer_tag) {
return range_wrap_dispatch___generic<InputIterator, T>(first, last);
}
template <typename InputIterator, typename T>
inline SEXP range_wrap_dispatch___impl(InputIterator first, InputIterator last, ::Rcpp::traits::r_type_module_object_tag) {
return range_wrap_dispatch___generic<InputIterator, T>(first, last);
}
template <typename InputIterator, typename T>
inline SEXP range_wrap_dispatch___impl(InputIterator first, InputIterator last, ::Rcpp::traits::r_type_module_object_reference_tag) {
return range_wrap_dispatch___generic<InputIterator, T>(first, last);
}
template <typename InputIterator, typename T>
inline SEXP range_wrap_dispatch___impl(InputIterator first, InputIterator last, ::Rcpp::traits::r_type_module_object_const_reference_tag) {
return range_wrap_dispatch___generic<InputIterator, T>(first, last);
}
/**
* Range based wrap implementation for iterators over std::string
*
* This produces an unnamed character vector
*/
template<typename InputIterator, typename T>
inline SEXP range_wrap_dispatch___impl(InputIterator first, InputIterator last, ::Rcpp::traits::r_type_string_tag) {
size_t size = std::distance(first, last);
Shield<SEXP> x(Rf_allocVector(STRSXP, size));
size_t i = 0;
while(i < size) {
SET_STRING_ELT(x, i, make_charsexp(*first));
i++;
++first;
}
return x;
}
// }}}
// {{{ named range wrap
/**
* range based wrap implementation that deals with iterators over
* pair<const string,T> where T is a primitive type : int, double ...
*
* This version is used when there is no need to cast T
*
* This produces a named R vector of the appropriate type
*/
template <typename InputIterator, typename T> // #nocov start
inline SEXP range_wrap_dispatch___impl__cast(InputIterator first, InputIterator last, ::Rcpp::traits::false_type) {
size_t size = std::distance(first, last);
const int RTYPE = ::Rcpp::traits::r_sexptype_traits<typename T::second_type>::rtype;
Shield<SEXP> x(Rf_allocVector(RTYPE, size));
Shield<SEXP> names(Rf_allocVector(STRSXP, size));
typedef typename ::Rcpp::traits::storage_type<RTYPE>::type CTYPE;
CTYPE* start = r_vector_start<RTYPE>(x);
size_t i =0;
std::string buf;
for (; i<size; i++, ++first) {
start[i] = (*first).second;
buf = (*first).first;
SET_STRING_ELT(names, i, Rf_mkChar(buf.c_str()));
}
::Rf_setAttrib(x, R_NamesSymbol, names);
return wrap_extra_steps<T>(x); // #nocov end
}
/**
* range based wrap implementation that deals with iterators over
* pair<const string,T> where T is a primitive type : int, double ...
*
* This version is used when T needs to be cast to the associated R
* type
*
* This produces a named R vector of the appropriate type
*/
template <typename InputIterator, typename T>
inline SEXP range_wrap_dispatch___impl__cast(InputIterator first, InputIterator last, ::Rcpp::traits::true_type) {
size_t size = std::distance(first, last);
const int RTYPE = ::Rcpp::traits::r_sexptype_traits<typename T::second_type>::rtype;
Shield<SEXP> x(Rf_allocVector(RTYPE, size));
Shield<SEXP> names(Rf_allocVector(STRSXP, size));
typedef typename ::Rcpp::traits::storage_type<RTYPE>::type CTYPE;
CTYPE* start = r_vector_start<RTYPE>(x);
size_t i =0;
std::string buf;
for (; i<size; i++, ++first) {
start[i] = static_cast<CTYPE>(first->second);
buf = first->first;
SET_STRING_ELT(names, i, Rf_mkChar(buf.c_str()));
}
::Rf_setAttrib(x, R_NamesSymbol, names);
return wrap_extra_steps<T>(x);
}
/**
* range based wrap implementation that deals with iterators over
* pair<const string,T> where T is a primitive type : int, double ...
*
* This dispatches further depending on whether the type needs
* a cast to fit into the associated R type
*
* This produces a named R vector of the appropriate type
*/
template <typename InputIterator, typename T> // #nocov start
inline SEXP range_wrap_dispatch___impl(InputIterator first, InputIterator last, ::Rcpp::traits::r_type_pairstring_primitive_tag) {
return range_wrap_dispatch___impl__cast<InputIterator,T>(first, last,
typename ::Rcpp::traits::r_sexptype_needscast<typename T::second_type>());
} // #nocov end
/**
* Range based wrap implementation that deals with iterators over
* pair<const string, U> where U is wrappable. This is the kind of
* iterators that are produced by map<string,U>
*
* This produces a named generic vector (named list). The first
* element of the list contains the result of a call to wrap on the
* object of type U, etc ...
*
* The names are taken from the keys
*/
template <typename InputIterator, typename T>
inline SEXP range_wrap_dispatch___impl(InputIterator first, InputIterator last, ::Rcpp::traits::r_type_pairstring_generic_tag) {
size_t size = std::distance(first, last);
Shield<SEXP> x(Rf_allocVector(VECSXP, size));
Shield<SEXP> names(Rf_allocVector(STRSXP, size));
size_t i =0;
std::string buf;
SEXP element = R_NilValue;
while(i < size) { // #nocov start
element = ::Rcpp::wrap(first->second);
buf = first->first;
SET_VECTOR_ELT(x, i, element);
SET_STRING_ELT(names, i, Rf_mkChar(buf.c_str()));
i++;
++first;
} // #nocov end
::Rf_setAttrib(x, R_NamesSymbol, names);
return x;
}
/**
* Range based wrap for iterators over std::pair<const std::(w)?string, std::(w)?string>
*
* This is mainly used for wrapping map<string,string> and friends
* which happens to produce iterators over pair<const string, string>
*
* This produces a character vector containing copies of the
* string iterated over. The names of the vector is set to the keys
* of the pair
*/
template<typename InputIterator, typename T>
inline SEXP range_wrap_dispatch___impl(InputIterator first, InputIterator last, ::Rcpp::traits::r_type_pairstring_string_tag) {
size_t size = std::distance(first, last);
Shield<SEXP> x(Rf_allocVector(STRSXP, size));
Shield<SEXP> names(Rf_allocVector(STRSXP, size));
for (size_t i = 0; i < size; i++, ++first) {
SET_STRING_ELT(x, i, make_charsexp(first->second));
SET_STRING_ELT(names, i, make_charsexp(first->first));
}
::Rf_setAttrib(x, R_NamesSymbol, names);
return x;
}
/**
* iterating over pair<const int, VALUE>
* where VALUE is some primitive type
*/
template <typename InputIterator, typename KEY, typename VALUE, int RTYPE>
inline SEXP range_wrap_dispatch___impl__pair(InputIterator first, InputIterator last, Rcpp::traits::true_type);
/**
* iterating over pair<const int, VALUE>
* where VALUE is a type that needs wrapping
*/
template <typename InputIterator, typename KEY, typename VALUE, int RTYPE>
inline SEXP range_wrap_dispatch___impl__pair(InputIterator first, InputIterator last, Rcpp::traits::false_type);
/**
* Range wrap dispatch for iterators over std::pair<const int, T>
*/
template<typename InputIterator, typename T>
inline SEXP range_wrap_dispatch___impl(InputIterator first, InputIterator last, ::Rcpp::traits::r_type_pair_tag) {
typedef typename T::second_type VALUE;
typedef typename T::first_type KEY;
return range_wrap_dispatch___impl__pair<InputIterator, KEY, VALUE,
Rcpp::traits::r_sexptype_traits<VALUE>::rtype >(first, last,
typename Rcpp::traits::is_primitive<VALUE>::type());
}
// }}}
/**
* Dispatcher for all range based wrap implementations
*
* This uses the Rcpp::traits::r_type_traits to perform further dispatch
*/
template<typename InputIterator, typename T>
inline SEXP range_wrap_dispatch(InputIterator first, InputIterator last) {
#if RCPP_DEBUG_LEVEL > 0
typedef typename ::Rcpp::traits::r_type_traits<T>::r_category categ;
#endif
RCPP_DEBUG_3("range_wrap_dispatch< InputIterator = \n%s , T = %s, categ = %s>\n", DEMANGLE(InputIterator), DEMANGLE(T), DEMANGLE(categ));
return range_wrap_dispatch___impl<InputIterator,T>(first, last, typename ::Rcpp::traits::r_type_traits<T>::r_category());
}
// we use the iterator trait to make the dispatch
/**
* range based wrap. This uses the std::iterator_traits class
* to perform further dispatch
*/
template <typename InputIterator>
inline SEXP range_wrap(InputIterator first, InputIterator last) {
return range_wrap_dispatch<InputIterator,typename traits::remove_reference<typename std::iterator_traits<InputIterator>::value_type>::type >(first, last);
}
// }}}
// {{{ primitive wrap (wrapping a single primitive value)
/**
* wraps a single primitive value when there is no need for a cast
*/
template <typename T>
inline SEXP primitive_wrap__impl__cast(const T& object, ::Rcpp::traits::false_type) {
const int RTYPE = ::Rcpp::traits::r_sexptype_traits<T>::rtype;
Shield<SEXP> x(Rf_allocVector(RTYPE, 1));
r_vector_start<RTYPE>(x)[0] = object;
return x;
}
/**
* wraps a single primitive value when a cast is needed
*/
template <typename T>
inline SEXP primitive_wrap__impl__cast(const T& object, ::Rcpp::traits::true_type) {
const int RTYPE = ::Rcpp::traits::r_sexptype_traits<T>::rtype;
typedef typename ::Rcpp::traits::storage_type<RTYPE>::type STORAGE_TYPE;
Shield<SEXP> x(Rf_allocVector(RTYPE, 1));
r_vector_start<RTYPE>(x)[0] = caster<T,STORAGE_TYPE>(object);
return x;
}
/**
* primitive wrap for 'easy' primitive types: int, double, Rbyte, Rcomplex
*
* This produces a vector of length 1 of the appropriate type
*/
template <typename T>
inline SEXP primitive_wrap__impl(const T& object, ::Rcpp::traits::r_type_primitive_tag) {
return primitive_wrap__impl__cast(object, typename ::Rcpp::traits::r_sexptype_needscast<T>());
}
/**
* primitive wrap for types that can be converted implicitely to std::string or std::wstring
*
* This produces a character vector of length 1 containing the std::string or wstring
*/
template <typename T>
inline SEXP primitive_wrap__impl(const T& object, ::Rcpp::traits::r_type_string_tag) {
Shield<SEXP> x(::Rf_allocVector(STRSXP, 1));
SET_STRING_ELT(x, 0, make_charsexp(object));
return x;
}
/**
* called when T is a primitive type : int, bool, double, std::string, etc ...
* This uses the Rcpp::traits::r_type_traits on the type T to perform
* further dispatching and wrap the object into an vector of length 1
* of the appropriate SEXP type
*/
template <typename T>
inline SEXP primitive_wrap(const T& object) {
return primitive_wrap__impl(object, typename ::Rcpp::traits::r_type_traits<T>::r_category());
}
// }}}
// {{{ unknown
/**
* Called when the type T is known to be implicitely convertible to
* SEXP. It uses the implicit conversion to SEXP to wrap the object
* into a SEXP
*/
template <typename T>
inline SEXP wrap_dispatch_unknown(const T& object, ::Rcpp::traits::true_type) {
RCPP_DEBUG_1("wrap_dispatch_unknown<%s>(., false )", DEMANGLE(T))
// here we know (or assume) that T is convertible to SEXP
SEXP x = object;
return x;
}
/**
* This is the worst case :
* - not a primitive
* - not implicitely convertible tp SEXP
* - not iterable
*
* so we just give up and attempt to use static_assert to generate
* a compile time message if it is available, otherwise we use
* implicit conversion to SEXP to bomb the compiler, which will give
* quite a cryptic message
*/
template <typename T>
inline SEXP wrap_dispatch_unknown_iterable(const T& object, ::Rcpp::traits::false_type) {
RCPP_DEBUG_1("wrap_dispatch_unknown_iterable<%s>(., false )", DEMANGLE(T))
// here we know that T is not convertible to SEXP
static_assert(!sizeof(T), "cannot convert type to SEXP");
return R_NilValue; // -Wall
}
template <typename T>
inline SEXP wrap_dispatch_unknown_iterable__logical(const T& object, ::Rcpp::traits::true_type) {
RCPP_DEBUG_1("wrap_dispatch_unknown_iterable__logical<%s>(., true )", DEMANGLE(T))
size_t size = object.size();
Shield<SEXP> x(Rf_allocVector(LGLSXP, size));
std::copy(object.begin(), object.end(), LOGICAL(x));
return x;
}
template <typename T>
inline SEXP wrap_range_sugar_expression(const T& object, Rcpp::traits::false_type) {
RCPP_DEBUG_1("wrap_range_sugar_expression<%s>(., false )", DEMANGLE(T))
return range_wrap(object.begin(), object.end());
}
template <typename T>
inline SEXP wrap_range_sugar_expression(const T& object, Rcpp::traits::true_type);
template <typename T>
inline SEXP wrap_dispatch_unknown_iterable__logical(const T& object, ::Rcpp::traits::false_type) {
RCPP_DEBUG_1("wrap_dispatch_unknown_iterable__logical<%s>(., false )", DEMANGLE(T))
return wrap_range_sugar_expression(object, typename Rcpp::traits::is_sugar_expression<T>::type());
}
template <typename T>
inline SEXP wrap_dispatch_unknown_iterable__matrix_interface(const T& object, ::Rcpp::traits::false_type) {
RCPP_DEBUG_1("wrap_dispatch_unknown_iterable__matrix_interface<%s>(., false )", DEMANGLE(T))
return wrap_dispatch_unknown_iterable__logical(object,
typename ::Rcpp::traits::expands_to_logical<T>::type());
}
template <typename T>
inline SEXP wrap_dispatch_matrix_logical(const T& object, ::Rcpp::traits::true_type) {
int nr = object.nrow(), nc = object.ncol();
Shield<SEXP> res(Rf_allocVector(LGLSXP, nr * nc));
int k=0;
int* p = LOGICAL(res);
for (int j=0; j<nc; j++)
for (int i=0; i<nr; i++, k++)
p[k] = object(i,j);
Shield<SEXP> dim(Rf_allocVector(INTSXP, 2));
INTEGER(dim)[0] = nr;
INTEGER(dim)[1] = nc;
Rf_setAttrib(res, R_DimSymbol , dim);
return res;
}
template <typename T, typename STORAGE>
inline SEXP wrap_dispatch_matrix_primitive(const T& object) {
const int RTYPE = ::Rcpp::traits::r_sexptype_traits<STORAGE>::rtype;
int nr = object.nrow(), nc = object.ncol();
Shield<SEXP> res(Rf_allocVector(RTYPE, nr*nc));
int k=0;
STORAGE* p = r_vector_start< RTYPE>(res);
for (int j=0; j<nc; j++)
for (int i=0; i<nr; i++, k++)
p[k] = object(i,j);
Shield<SEXP> dim(Rf_allocVector(INTSXP, 2));
INTEGER(dim)[0] = nr;
INTEGER(dim)[1] = nc;
Rf_setAttrib(res, R_DimSymbol , dim);
return res;
}
template <typename T>
inline SEXP wrap_dispatch_matrix_not_logical(const T& object, ::Rcpp::traits::r_type_primitive_tag) {
return wrap_dispatch_matrix_primitive<T, typename T::stored_type>(object);
}
template <typename T>
inline SEXP wrap_dispatch_matrix_not_logical(const T& object, ::Rcpp::traits::r_type_string_tag) {
int nr = object.nrow(), nc = object.ncol();
Shield<SEXP> res(Rf_allocVector(STRSXP, nr*nc));
int k=0;
for (int j=0; j<nc; j++)
for (int i=0; i<nr; i++, k++)
SET_STRING_ELT(res, k, make_charsexp(object(i,j)));
Shield<SEXP> dim(Rf_allocVector(INTSXP, 2));
INTEGER(dim)[0] = nr;
INTEGER(dim)[1] = nc;
Rf_setAttrib(res, R_DimSymbol , dim);
return res;
}
template <typename T>
inline SEXP wrap_dispatch_matrix_not_logical(const T& object, ::Rcpp::traits::r_type_generic_tag) {
int nr = object.nrow(), nc = object.ncol();
Shield<SEXP> res(Rf_allocVector(VECSXP, nr*nc));
int k=0;
for (int j=0; j<nc; j++)
for (int i=0; i<nr; i++, k++)
SET_VECTOR_ELT(res, k, ::Rcpp::wrap(object(i,j)));
Shield<SEXP> dim(Rf_allocVector(INTSXP, 2));
INTEGER(dim)[0] = nr;
INTEGER(dim)[1] = nc;
Rf_setAttrib(res, R_DimSymbol , dim);
return res;
}
template <typename T>
inline SEXP wrap_dispatch_matrix_logical(const T& object, ::Rcpp::traits::false_type) {
return wrap_dispatch_matrix_not_logical<T>(object, typename ::Rcpp::traits::r_type_traits<typename T::stored_type>::r_category());
}
template <typename T>
inline SEXP wrap_dispatch_unknown_iterable__matrix_interface(const T& object, ::Rcpp::traits::true_type) {
RCPP_DEBUG_1("wrap_dispatch_unknown_iterable__matrix_interface<%s>(., true )", DEMANGLE(T))
return wrap_dispatch_matrix_logical(object, typename ::Rcpp::traits::expands_to_logical<T>::type());
}
/**
* Here we know for sure that type T has a T::iterator typedef
* so we hope for the best and call the range based wrap with begin
* and end
*
* This works fine for all stl containers and classes T that have :
* - T::iterator
* - T::iterator begin()
* - T::iterator end()
*
* If someone knows a better way, please advise
*/
template <typename T>
inline SEXP wrap_dispatch_unknown_iterable(const T& object, ::Rcpp::traits::true_type) {
RCPP_DEBUG_1("wrap_dispatch_unknown_iterable<%s>(., true )", DEMANGLE(T))
return wrap_dispatch_unknown_iterable__matrix_interface(object,
typename ::Rcpp::traits::matrix_interface<T>::type());
}
template <typename T, typename elem_type>
inline SEXP wrap_dispatch_importer__impl__prim(const T& object, ::Rcpp::traits::false_type) {
int size = object.size();
const int RTYPE = ::Rcpp::traits::r_sexptype_traits<elem_type>::rtype;
Shield<SEXP> x(Rf_allocVector(RTYPE, size));
typedef typename ::Rcpp::traits::storage_type<RTYPE>::type CTYPE;
CTYPE* start = r_vector_start<RTYPE>(x);
for (int i=0; i<size; i++) {
start[i] = object.get(i);
}
return x;
}
template <typename T, typename elem_type>
inline SEXP wrap_dispatch_importer__impl__prim(const T& object, ::Rcpp::traits::true_type) {
int size = object.size();
const int RTYPE = ::Rcpp::traits::r_sexptype_traits<elem_type>::rtype;
Shield<SEXP> x(Rf_allocVector(RTYPE, size));
typedef typename ::Rcpp::traits::storage_type<RTYPE>::type CTYPE;
CTYPE* start = r_vector_start<RTYPE>(x);
for (int i=0; i<size; i++) {
start[i] = caster<elem_type,CTYPE>(object.get(i));
}
return x;
}
template <typename T, typename elem_type>
inline SEXP wrap_dispatch_importer__impl(const T& object, ::Rcpp::traits::r_type_primitive_tag) {
return wrap_dispatch_importer__impl__prim<T,elem_type>(object,
typename ::Rcpp::traits::r_sexptype_needscast<elem_type>());
}
template <typename T, typename elem_type>
inline SEXP wrap_dispatch_importer__impl(const T& object, ::Rcpp::traits::r_type_string_tag) {
int size = object.size();
Shield<SEXP> x(Rf_allocVector(STRSXP, size));
for (int i=0; i<size; i++) {
SET_STRING_ELT(x, i, make_charsexp(object.get(i)));
}
return x;
}
template <typename T, typename elem_type>
inline SEXP wrap_dispatch_importer__impl(const T& object, ::Rcpp::traits::r_type_generic_tag) {
int size = object.size();
Shield<SEXP> x(Rf_allocVector(VECSXP, size));
for (int i=0; i<size; i++) {
SET_VECTOR_ELT(x, i, object.wrap(i));
}
return x;
}
template <typename T, typename elem_type>
inline SEXP wrap_dispatch_importer(const T& object) {
return wrap_dispatch_importer__impl<T,elem_type>(object,
typename ::Rcpp::traits::r_type_traits<elem_type>::r_category());
}
/**
* Called when no implicit conversion to SEXP is possible and this is
* not tagged as a primitive type, checks whether the type is
* iterable
*/
template <typename T>
inline SEXP wrap_dispatch_unknown(const T& object, ::Rcpp::traits::false_type) {
RCPP_DEBUG_1("wrap_dispatch_unknown<%s>(., false )", DEMANGLE(T))
return wrap_dispatch_unknown_iterable(object, typename ::Rcpp::traits::has_iterator<T>::type());
}
// }}}
// {{{ wrap dispatch
/**
* wrapping a __single__ primitive type : int, double, std::string, size_t,
* Rbyte, Rcomplex
*/
template <typename T>
inline SEXP wrap_dispatch(const T& object, ::Rcpp::traits::wrap_type_primitive_tag) {
return primitive_wrap(object);
}
template <typename T>
inline SEXP wrap_dispatch(const T& object, ::Rcpp::traits::wrap_type_char_array) {
return Rf_mkString(object);
}
template <typename T>
inline SEXP wrap_dispatch(const T& object, ::Rcpp::traits::wrap_type_module_object_pointer_tag) {
return Rcpp::internal::make_new_object< typename T::object_type >(object.ptr);
}
template <typename T>
inline SEXP wrap_dispatch(const T& object, ::Rcpp::traits::wrap_type_module_object_tag) {
return Rcpp::internal::make_new_object<T>(new T(object));
}
template <typename T>
inline SEXP wrap_dispatch(const T& object, ::Rcpp::traits::wrap_type_enum_tag) {
return wrap((int)object);
}
template <typename T>
inline SEXP wrap_dispatch_eigen(const T& object, ::Rcpp::traits::false_type) {
RCPP_DEBUG_1("wrap_dispatch_eigen<%s>(., false )", DEMANGLE(T))
return wrap_dispatch_unknown(object, typename ::Rcpp::traits::is_convertible<T,SEXP>::type());
}
template <typename T>
inline SEXP wrap_dispatch_eigen(const T& object, ::Rcpp::traits::true_type) {
RCPP_DEBUG_1("wrap_dispatch_eigen<%s>(., true )", DEMANGLE(T))
return ::Rcpp::RcppEigen::eigen_wrap(object);
}
/**
* called when T is wrap_type_unknown_tag and is not an Importer class
* The next step is to try implicit conversion to SEXP
*/
template <typename T>
inline SEXP wrap_dispatch_unknown_importable(const T& object, ::Rcpp::traits::false_type) {
RCPP_DEBUG_1("wrap_dispatch_unknown_importable<%s>(., false )", DEMANGLE(T))
return wrap_dispatch_eigen(object, typename traits::is_eigen_base<T>::type());
}
/**
* called when T is an Importer
*/
template <typename T>
inline SEXP wrap_dispatch_unknown_importable(const T& object, ::Rcpp::traits::true_type) {
RCPP_DEBUG_1("wrap_dispatch_unknown_importable<%s>(., true )", DEMANGLE(T))
return wrap_dispatch_importer<T,typename T::r_import_type>(object);
}
/**
* This is called by wrap when the wrap_type_traits is wrap_type_unknown_tag
*
* This tries to identify if the object conforms to the Importer class
*/
template <typename T>
inline SEXP wrap_dispatch(const T& object, ::Rcpp::traits::wrap_type_unknown_tag) {
RCPP_DEBUG_1("wrap_dispatch<%s>(., wrap_type_unknown_tag)", DEMANGLE(T))
return wrap_dispatch_unknown_importable(object, typename ::Rcpp::traits::is_importer<T>::type());
}
// }}}
// {{{ wrap a container that is structured in row major order
template <typename value_type, typename InputIterator>
inline SEXP rowmajor_wrap__dispatch(InputIterator first, int nrow, int ncol, ::Rcpp::traits::r_type_generic_tag) {
Shield<SEXP> out(::Rf_allocVector(VECSXP, nrow * ncol));
int i=0, j=0;
for (j=0; j<ncol; j++) {
for (i=0; i<nrow; i++, ++first) {
SET_VECTOR_ELT(out, j + ncol*i, ::Rcpp::wrap(*first));
}
}
Shield<SEXP> dims(::Rf_allocVector(INTSXP, 2));
INTEGER(dims)[0] = nrow;
INTEGER(dims)[1] = ncol;
::Rf_setAttrib(out, R_DimSymbol, dims);
return out;
}
template <typename value_type, typename InputIterator>
inline SEXP rowmajor_wrap__dispatch(InputIterator first, int nrow, int ncol, ::Rcpp::traits::r_type_string_tag) {
Shield<SEXP> out(::Rf_allocVector(STRSXP, nrow * ncol));
int i=0, j=0;
for (j=0; j<ncol; j++) {
for (i=0; i<nrow; i++, ++first) {
SET_STRING_ELT(out, j + ncol*i, make_charsexp(*first));
}
}
Shield<SEXP> dims(::Rf_allocVector(INTSXP, 2));
INTEGER(dims)[0] = nrow;
INTEGER(dims)[1] = ncol;
::Rf_setAttrib(out, R_DimSymbol, dims);
return out;
}
template <typename value_type, typename InputIterator>
inline SEXP primitive_rowmajor_wrap__dispatch(InputIterator first, int nrow, int ncol, ::Rcpp::traits::false_type) {
const int RTYPE = ::Rcpp::traits::r_sexptype_traits<value_type>::rtype;
Shield<SEXP> out(::Rf_allocVector(RTYPE, nrow * ncol));
value_type* ptr = r_vector_start<RTYPE>(out);
int i=0, j=0;
for (j=0; j<ncol; j++) {
for (i=0; i<nrow; i++, ++first) {
ptr[ j + ncol*i ] = *first;
}
}
Shield<SEXP> dims(::Rf_allocVector(INTSXP, 2));
INTEGER(dims)[0] = nrow;
INTEGER(dims)[1] = ncol;
::Rf_setAttrib(out, R_DimSymbol, dims);
return out;
}
template <typename value_type, typename InputIterator>
inline SEXP primitive_rowmajor_wrap__dispatch(InputIterator first, int nrow, int ncol, ::Rcpp::traits::true_type) {
const int RTYPE = ::Rcpp::traits::r_sexptype_traits<value_type>::rtype;
typedef typename ::Rcpp::traits::storage_type<RTYPE>::type STORAGE;
Shield<SEXP> out(::Rf_allocVector(RTYPE, nrow * ncol));
STORAGE* ptr = r_vector_start<RTYPE>(out);
int i=0, j=0;
for (j=0; j<ncol; j++) {
for (i=0; i<nrow; i++, ++first) {
ptr[ j + ncol*i ] = caster<value_type,STORAGE>(*first);
}
}
Shield<SEXP> dims(::Rf_allocVector(INTSXP, 2));
INTEGER(dims)[0] = nrow;
INTEGER(dims)[1] = ncol;
::Rf_setAttrib(out, R_DimSymbol, dims);
return out;
}
template <typename value_type, typename InputIterator>
inline SEXP rowmajor_wrap__dispatch(InputIterator first, int nrow, int ncol, ::Rcpp::traits::r_type_primitive_tag) {
return primitive_rowmajor_wrap__dispatch<value_type,InputIterator>(first, nrow, ncol, typename ::Rcpp::traits::r_sexptype_needscast<value_type>());
}
template <typename InputIterator>
inline SEXP rowmajor_wrap(InputIterator first, int nrow, int ncol) {
typedef typename std::iterator_traits<InputIterator>::value_type VALUE_TYPE;
return rowmajor_wrap__dispatch<VALUE_TYPE,InputIterator>(first, nrow, ncol, typename ::Rcpp::traits::r_type_traits<VALUE_TYPE>::r_category());
}
// }}}
} // internal
/**
* wraps an object of type T in a SEXP
*
* This method depends on the Rcpp::traits::wrap_type_traits trait
* class to dispatch to the appropriate internal implementation
* method
*
*/
template <typename T>
inline SEXP wrap(const T& object);
template <> inline SEXP wrap<Rcpp::String>(const Rcpp::String& object);
template <typename T>
inline SEXP module_wrap_dispatch(const T& obj, Rcpp::traits::void_wrap_tag) {
return R_NilValue;
}
// these are defined in wrap_end.h
template <typename T>
inline SEXP module_wrap_dispatch(const T& obj, Rcpp::traits::pointer_wrap_tag);
template <typename T>
inline SEXP module_wrap_dispatch(const T& obj, Rcpp::traits::normal_wrap_tag);
template <typename T>
inline SEXP module_wrap(const T& obj) {
return module_wrap_dispatch<T>(obj, typename Rcpp::traits::module_wrap_traits<T>::category());
}
template <>
inline SEXP module_wrap<SEXP>(const SEXP& obj) {
return obj;
}
inline SEXP wrap(const char* const v) {
if (v != NULL)
return Rf_mkString(v);
else
return R_NilValue; // #nocov
}
/**
* Range based version of wrap
*/
template <typename InputIterator>
inline SEXP wrap(InputIterator first, InputIterator last) {
return internal::range_wrap(first, last);
}
} // Rcpp
#endif
|