[Rcpp-commits] r449 - in pkg: inst src

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Mon Jan 25 11:42:10 CET 2010


Author: romain
Date: 2010-01-25 11:42:10 +0100 (Mon, 25 Jan 2010)
New Revision: 449

Modified:
   pkg/inst/ChangeLog
   pkg/src/as.cpp
Log:
more coercion (FR#758)

Modified: pkg/inst/ChangeLog
===================================================================
--- pkg/inst/ChangeLog	2010-01-25 10:11:48 UTC (rev 448)
+++ pkg/inst/ChangeLog	2010-01-25 10:42:10 UTC (rev 449)
@@ -1,5 +1,8 @@
 2010-01-25  Romain Francois <francoisromain at free.fr>
 
+	* src/as.cpp: also use coercion for as<bool>, as<int>, as<double>
+	as<Rbyte> (FR #768)
+
 	* src/Rcpp/as.h: smarter template as<T>, now the template 
 	attempts to create an object by passing the SEXP as the first 
 	parameter, so any class that has a constructor taking a SEXP

Modified: pkg/src/as.cpp
===================================================================
--- pkg/src/as.cpp	2010-01-25 10:11:48 UTC (rev 448)
+++ pkg/src/as.cpp	2010-01-25 10:42:10 UTC (rev 449)
@@ -29,13 +29,13 @@
     }
     switch( TYPEOF(m_sexp) ){
     	case LGLSXP:
-    		return LOGICAL(m_sexp)[0] ? 1.0 : 0.0 ; 
+    		return Rboolean_to_double( LOGICAL(m_sexp)[0] ) ; 
     	case REALSXP:
     		return REAL(m_sexp)[0] ; 
     	case INTSXP:
-    		return (double)INTEGER(m_sexp)[0]; 
+    		return int_to_double( INTEGER(m_sexp)[0] ); 
     	case RAWSXP:
-    		return (double)RAW(m_sexp)[0];
+    		return static_cast<double>( RAW(m_sexp)[0] );
     	default:
     		throw std::range_error("as<double> invalid type");
     }
@@ -48,13 +48,13 @@
     }
     switch( TYPEOF(m_sexp)){
     	case LGLSXP:
-    		return LOGICAL(m_sexp)[0] ? 1 : 0 ; 
+    		return Rboolean_to_int( LOGICAL(m_sexp)[0] ) ; 
     	case REALSXP:
-    		return (int)REAL(m_sexp)[0] ; // some of this might be lost
+    		return double_to_int( REAL(m_sexp)[0] ); // some of this might be lost
     	case INTSXP:
     		return INTEGER(m_sexp)[0]; 
     	case RAWSXP:
-    		return (int)RAW(m_sexp)[0];
+    		return static_cast<int>( RAW(m_sexp)[0] );
     	default:
     		throw std::range_error("as<int>");
     }
@@ -67,11 +67,11 @@
     }
     switch( TYPEOF(m_sexp) ){
     	case LGLSXP:
-    		return LOGICAL(m_sexp)[0] ? (Rbyte)1 : (Rbyte)0 ; 
+    		return Rboolean_to_Rbyte( LOGICAL(m_sexp)[0] ) ; 
     	case REALSXP:
-    		return (Rbyte)REAL(m_sexp)[0] ;
+    		return double_to_Rbyte( REAL(m_sexp)[0] );
     	case INTSXP:
-    		return (Rbyte)INTEGER(m_sexp)[0] ;
+    		return int_to_Rbyte( INTEGER(m_sexp)[0] );
     	case RAWSXP:
     		return RAW(m_sexp)[0] ;
     	default:
@@ -88,11 +88,11 @@
     	case LGLSXP:
     		return LOGICAL(m_sexp)[0] ? true : false ; 
     	case REALSXP:
-    		return (bool)REAL(m_sexp)[0] ;
+    		return double_to_bool( REAL(m_sexp)[0] ) ;
     	case INTSXP:
-    		return (bool)INTEGER(m_sexp)[0] ;
+    		return int_to_bool( INTEGER(m_sexp)[0] ) ;
     	case RAWSXP:
-    		return (bool)RAW(m_sexp)[0] ;
+    		return Rbyte_to_bool( RAW(m_sexp)[0] );
     	default:
     		throw std::range_error("as<bool> expects raw, double or int");
     }
@@ -115,19 +115,15 @@
     switch( TYPEOF(m_sexp) ){
     case LGLSXP:
     	transform( LOGICAL(m_sexp), LOGICAL(m_sexp)+n, v.begin(), Rboolean_to_bool ) ;
-    	// v.assign( LOGICAL(m_sexp), LOGICAL(m_sexp)+n ) ;
     	break ;
     case INTSXP:
     	transform( INTEGER(m_sexp), INTEGER(m_sexp)+n, v.begin(), int_to_bool ) ;
-    	// v.assign( INTEGER(m_sexp), INTEGER(m_sexp)+n ) ;
     	break;
     case REALSXP:
     	transform( REAL(m_sexp), REAL(m_sexp)+n, v.begin(), double_to_bool ) ;
-    	// v.assign( REAL(m_sexp), REAL(m_sexp)+n ) ;
     	break;
     case RAWSXP:
     	transform( RAW(m_sexp), RAW(m_sexp)+n, v.begin(), Rbyte_to_bool ) ;
-    	// v.assign( RAW(m_sexp), RAW(m_sexp)+n ) ;
     	break;
     default:
     		throw std::range_error( "as< vector<bool> >: invalid R type" ) ; 
@@ -145,15 +141,12 @@
     	break;
     case LGLSXP:
     	transform( LOGICAL(m_sexp), LOGICAL(m_sexp)+n, v.begin(), Rboolean_to_int ) ;
-    	// v.assign( LOGICAL(m_sexp), LOGICAL(m_sexp)+n) ;
     	break;
     case REALSXP:
     	transform( REAL(m_sexp), REAL(m_sexp)+n, v.begin(), double_to_int ) ;
-    	// v.assign( REAL(m_sexp), REAL(m_sexp) + n) ;
     	break;
     case RAWSXP:
     	transform( RAW(m_sexp), RAW(m_sexp)+n, v.begin(), Rbyte_to_int ) ;
-    	// v.assign( RAW(m_sexp), RAW(m_sexp)+n) ;
     	break;
     default:
     		throw std::range_error( "as< vector<int> >: invalid R type" ) ; 
@@ -167,18 +160,15 @@
     switch( TYPEOF(m_sexp) ){
     case LGLSXP:
     	transform( LOGICAL(m_sexp), LOGICAL(m_sexp)+n, v.begin(), Rboolean_to_Rbyte ) ;
-    	// v.assign( LOGICAL(m_sexp), LOGICAL(m_sexp)+n) ;
     	break ;
     case RAWSXP:
     	v.assign( RAW(m_sexp), RAW(m_sexp)+n ) ;
     	break ;
     case REALSXP:
     	transform( REAL(m_sexp), REAL(m_sexp)+n, v.begin(), double_to_Rbyte ) ;
-    	// v.assign( REAL(m_sexp), REAL(m_sexp) + n ) ;
     	break;
     case INTSXP:
     	transform( INTEGER(m_sexp), INTEGER(m_sexp)+n, v.begin(), int_to_Rbyte ) ;
-    	// v.assign( INTEGER(m_sexp), INTEGER(m_sexp)+n) ;
     	break;
     default:
     	throw std::range_error("as< vector<Rbyte> > expects raw, double or int");
@@ -192,18 +182,15 @@
     switch( TYPEOF(m_sexp) ){
     case LGLSXP:
     	transform( LOGICAL(m_sexp), LOGICAL(m_sexp)+n, v.begin(), Rboolean_to_double ) ;
-    	// v.assign( LOGICAL(m_sexp), LOGICAL(m_sexp)+n ); 
     	break ;
     case RAWSXP:
     	transform( RAW(m_sexp), RAW(m_sexp)+n, v.begin(), Rbyte_to_double ) ;
-    	// v.assign( RAW(m_sexp), RAW(m_sexp)+n ) ;
     	break ;
     case REALSXP:
     	v.assign( REAL(m_sexp), REAL(m_sexp)+n) ;
     	break;
     case INTSXP:
     	transform( INTEGER(m_sexp), INTEGER(m_sexp)+n, v.begin(), int_to_double) ;
-    	// v.assign( INTEGER(m_sexp), INTEGER(m_sexp)+n ) ;
     	break;
     default:
     	    throw std::range_error("as< vector<double> >:  expects raw, double or int");



More information about the Rcpp-commits mailing list