|
Rcpp Version 0.9.10
|
00001 // -*- mode: C++; c-indent-level: 4; c-basic-offset: 4; tab-width: 8 -*- 00002 // 00003 // coerce.cpp: Rcpp R/C++ interface class library -- coercion 00004 // 00005 // Copyright (C) 2010 - 2011 Dirk Eddelbuettel and Romain Francois 00006 // 00007 // This file is part of Rcpp. 00008 // 00009 // Rcpp is free software: you can redistribute it and/or modify it 00010 // under the terms of the GNU General Public License as published by 00011 // the Free Software Foundation, either version 2 of the License, or 00012 // (at your option) any later version. 00013 // 00014 // Rcpp is distributed in the hope that it will be useful, but 00015 // WITHOUT ANY WARRANTY; without even the implied warranty of 00016 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00017 // GNU General Public License for more details. 00018 // 00019 // You should have received a copy of the GNU General Public License 00020 // along with Rcpp. If not, see <http://www.gnu.org/licenses/>. 00021 00022 #include <RcppCommon.h> 00023 00024 namespace Rcpp{ 00025 namespace internal{ 00026 00027 template <> int r_coerce<INTSXP,INTSXP>(int from) { return from ; } 00028 template <> int r_coerce<LGLSXP,LGLSXP>(int from) { return from ; } 00029 template <> double r_coerce<REALSXP,REALSXP>(double from) { return from ; } 00030 template <> Rcomplex r_coerce<CPLXSXP,CPLXSXP>(Rcomplex from) { return from ; } 00031 template <> Rbyte r_coerce<RAWSXP,RAWSXP>(Rbyte from) { return from ; } 00032 00033 // -> INTSXP 00034 template <> int r_coerce<LGLSXP,INTSXP>(int from){ 00035 return (from==NA_LOGICAL) ? NA_INTEGER : from ; 00036 } 00037 template <> int r_coerce<REALSXP,INTSXP>(double from){ 00038 if (ISNAN(from)) return NA_INTEGER; 00039 else if (from > INT_MAX || from <= INT_MIN ) { 00040 return NA_INTEGER; 00041 } 00042 return static_cast<int>(from); 00043 00044 } 00045 template <> int r_coerce<CPLXSXP,INTSXP>(Rcomplex from){ 00046 return r_coerce<REALSXP,INTSXP>(from.r) ; 00047 } 00048 template <> int r_coerce<RAWSXP,INTSXP>(Rbyte from){ 00049 return static_cast<int>(from); 00050 } 00051 00052 // -> REALSXP 00053 template <> double r_coerce<LGLSXP,REALSXP>(int from){ 00054 return from == NA_LOGICAL ? NA_REAL : static_cast<double>(from) ; 00055 } 00056 template <> double r_coerce<INTSXP,REALSXP>(int from){ 00057 return from == NA_INTEGER ? NA_REAL : static_cast<double>(from) ; 00058 } 00059 template <> double r_coerce<CPLXSXP,REALSXP>(Rcomplex from){ 00060 return from.r ; 00061 } 00062 template <> double r_coerce<RAWSXP,REALSXP>(Rbyte from){ 00063 return static_cast<double>(from) ; 00064 } 00065 00066 // -> LGLSXP 00067 template <> int r_coerce<REALSXP,LGLSXP>(double from){ 00068 return ( from == NA_REAL ) ? NA_LOGICAL : (from!=0.0); 00069 } 00070 template <> int r_coerce<INTSXP,LGLSXP>(int from){ 00071 return ( from == NA_INTEGER ) ? NA_LOGICAL : (from!=0); 00072 } 00073 template <> int r_coerce<CPLXSXP,LGLSXP>(Rcomplex from){ 00074 if( from.r == NA_REAL ) return NA_LOGICAL ; 00075 if( from.r == 0.0 || from.i == 0.0 ) return FALSE ; 00076 return TRUE ; 00077 } 00078 template <> int r_coerce<RAWSXP,LGLSXP>(Rbyte from){ 00079 if( from != static_cast<Rbyte>(0) ) return TRUE ; 00080 return FALSE ; 00081 } 00082 00083 // -> RAWSXP 00084 template <> Rbyte r_coerce<REALSXP,RAWSXP>(double from){ 00085 if( from == NA_REAL) return static_cast<Rbyte>(0) ; 00086 return r_coerce<INTSXP,RAWSXP>(static_cast<int>(from)) ; 00087 } 00088 template <> Rbyte r_coerce<INTSXP,RAWSXP>(int from){ 00089 return (from < 0 || from > 255) ? static_cast<Rbyte>(0) : static_cast<Rbyte>(from) ; 00090 } 00091 template <> Rbyte r_coerce<CPLXSXP,RAWSXP>(Rcomplex from){ 00092 return r_coerce<REALSXP,RAWSXP>(from.r) ; 00093 } 00094 template <> Rbyte r_coerce<LGLSXP,RAWSXP>(int from){ 00095 return static_cast<Rbyte>(from == TRUE) ; 00096 } 00097 00098 // -> CPLXSXP 00099 template <> Rcomplex r_coerce<REALSXP,CPLXSXP>(double from){ 00100 Rcomplex c ; 00101 if( from == NA_REAL ){ 00102 c.r = NA_REAL; 00103 c.i = NA_REAL; 00104 } else{ 00105 c.r = from ; 00106 c.i = 0.0 ; 00107 } 00108 return c ; 00109 } 00110 template <> Rcomplex r_coerce<INTSXP,CPLXSXP>(int from){ 00111 Rcomplex c ; 00112 if( from == NA_INTEGER ){ 00113 c.r = NA_REAL; 00114 c.i = NA_REAL; 00115 } else{ 00116 c.r = static_cast<double>(from) ; 00117 c.i = 0.0 ; 00118 } 00119 return c ; 00120 } 00121 template <> Rcomplex r_coerce<RAWSXP,CPLXSXP>(Rbyte from){ 00122 Rcomplex c ; 00123 c.r = static_cast<double>(from); 00124 c.i = 0.0 ; 00125 return c ; 00126 } 00127 template <> Rcomplex r_coerce<LGLSXP,CPLXSXP>(int from){ 00128 Rcomplex c ; 00129 if( from == TRUE ){ 00130 c.r = 1.0 ; c.i = 0.0 ; 00131 } else if( from == FALSE ){ 00132 c.r = c.i = 0.0 ; 00133 } else { /* NA */ 00134 c.r = c.i = NA_REAL; 00135 } 00136 return c ; 00137 } 00138 00139 00140 } // internal 00141 } // Rcpp 00142