[Rcpp-commits] r1575 - in pkg/Rcpp: . inst/include/Rcpp/sugar inst/unitTests

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Thu Jun 17 19:32:41 CEST 2010


Author: romain
Date: 2010-06-17 19:32:41 +0200 (Thu, 17 Jun 2010)
New Revision: 1575

Added:
   pkg/Rcpp/inst/include/Rcpp/sugar/is_na.h
   pkg/Rcpp/inst/unitTests/runit.sugar.isna.R
Modified:
   pkg/Rcpp/TODO
   pkg/Rcpp/inst/include/Rcpp/sugar/Comparator.h
   pkg/Rcpp/inst/include/Rcpp/sugar/Comparator_With_One_Value.h
   pkg/Rcpp/inst/include/Rcpp/sugar/LogicalResult.h
   pkg/Rcpp/inst/include/Rcpp/sugar/sugar.h
Log:
added is_na to mimic the R function is.na lazily

Modified: pkg/Rcpp/TODO
===================================================================
--- pkg/Rcpp/TODO	2010-06-17 16:48:12 UTC (rev 1574)
+++ pkg/Rcpp/TODO	2010-06-17 17:32:41 UTC (rev 1575)
@@ -41,7 +41,7 @@
 		
 Syntactic sugar
 
-    o   duplicated, unique, is_na, count, sum, seq_along, seq_len, rep
+    o   duplicated, unique, count, sum, seq_along, seq_len, rep
     
     o	for matrices: row, col, lower_tri, upper_tri
 	

Modified: pkg/Rcpp/inst/include/Rcpp/sugar/Comparator.h
===================================================================
--- pkg/Rcpp/inst/include/Rcpp/sugar/Comparator.h	2010-06-17 16:48:12 UTC (rev 1574)
+++ pkg/Rcpp/inst/include/Rcpp/sugar/Comparator.h	2010-06-17 17:32:41 UTC (rev 1575)
@@ -26,7 +26,7 @@
 namespace sugar{
 
 template <int RTYPE, typename Operator, typename LHS_TYPE, typename RHS_TYPE>
-class Comparator : public LogicalResult< Comparator<RTYPE,Operator,LHS_TYPE,RHS_TYPE> > {
+class Comparator : public LogicalResult< Comparator<RTYPE,Operator,LHS_TYPE,RHS_TYPE>, true > {
 public:
 	typedef typename traits::storage_type<RTYPE>::type STORAGE ;
 	typedef r_binary_op<RTYPE,Operator> R_OPERATOR ;

Modified: pkg/Rcpp/inst/include/Rcpp/sugar/Comparator_With_One_Value.h
===================================================================
--- pkg/Rcpp/inst/include/Rcpp/sugar/Comparator_With_One_Value.h	2010-06-17 16:48:12 UTC (rev 1574)
+++ pkg/Rcpp/inst/include/Rcpp/sugar/Comparator_With_One_Value.h	2010-06-17 17:32:41 UTC (rev 1575)
@@ -27,7 +27,7 @@
 
 template <int RTYPE, typename Operator, bool na, typename VEC_TYPE>
 class Comparator_With_One_Value : 
-	public LogicalResult< Comparator_With_One_Value<RTYPE,Operator,na,VEC_TYPE> > {
+	public LogicalResult< Comparator_With_One_Value<RTYPE,Operator,na,VEC_TYPE>, true > {
 public:
 	typedef typename traits::storage_type<RTYPE>::type STORAGE ;
 	typedef r_binary_op<RTYPE,Operator> R_OPERATOR ;

Modified: pkg/Rcpp/inst/include/Rcpp/sugar/LogicalResult.h
===================================================================
--- pkg/Rcpp/inst/include/Rcpp/sugar/LogicalResult.h	2010-06-17 16:48:12 UTC (rev 1574)
+++ pkg/Rcpp/inst/include/Rcpp/sugar/LogicalResult.h	2010-06-17 17:32:41 UTC (rev 1575)
@@ -25,8 +25,8 @@
 namespace Rcpp{
 namespace sugar{  
 
-template <typename T>
-class LogicalResult : public Rcpp::VectorBase<LGLSXP,true,LogicalResult<T> > {
+template <typename T, bool na>
+class LogicalResult : public Rcpp::VectorBase<LGLSXP,na,LogicalResult<T,na> > {
 public:
 	
 	LogicalResult() {} ;

Added: pkg/Rcpp/inst/include/Rcpp/sugar/is_na.h
===================================================================
--- pkg/Rcpp/inst/include/Rcpp/sugar/is_na.h	                        (rev 0)
+++ pkg/Rcpp/inst/include/Rcpp/sugar/is_na.h	2010-06-17 17:32:41 UTC (rev 1575)
@@ -0,0 +1,79 @@
+// -*- mode: C++; c-indent-level: 4; c-basic-offset: 4; tab-width: 8 -*-
+//
+// any.h: Rcpp R/C++ interface class library -- any
+//
+// Copyright (C) 2010 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__sugar__is_na_h
+#define Rcpp__sugar__is_na_h
+
+namespace Rcpp{
+namespace sugar{
+
+template <int RTYPE, bool _NA_, typename VEC_TYPE>
+class IsNa : public LogicalResult< IsNa<RTYPE,_NA_,VEC_TYPE>, false > {
+public:
+	typedef typename traits::storage_type<RTYPE>::type STORAGE ;
+	typedef Rcpp::VectorBase<RTYPE,_NA_,VEC_TYPE> BASE ;
+	
+	IsNa( const BASE& obj_) : obj(obj_){}
+	
+	inline int operator[]( int i ) const {
+		return ::Rcpp::traits::is_na<RTYPE>( obj[i] ) ;
+	}
+	
+	inline int size() const { return obj.size() ; }
+	         
+private:
+	const BASE& obj ;
+	
+} ;
+      
+// specialization for the case where we already know
+// the result (FALSE) because it is embedded in the type
+// (the second template parameter of VectorBase)
+template <int RTYPE, typename VEC_TYPE>
+class IsNa<RTYPE,false,VEC_TYPE> : public LogicalResult< IsNa<RTYPE,false,VEC_TYPE>, false > {
+public:
+	typedef typename traits::storage_type<RTYPE>::type STORAGE ;
+	typedef Rcpp::VectorBase<RTYPE,false,VEC_TYPE> BASE ;
+	
+	IsNa( const BASE& obj_) : obj(obj_){}
+	
+	inline int operator[]( int i ) const {
+		return FALSE ;
+	}
+	
+	inline int size() const { return obj.size() ; }
+	
+private:
+	const BASE& obj ;
+	
+} ;
+
+	
+} // sugar
+
+template <int RTYPE, bool _NA_, typename T>
+inline sugar::IsNa<RTYPE,_NA_,T> is_na( const Rcpp::VectorBase<RTYPE,_NA_,T>& t){
+	return sugar::IsNa<RTYPE,_NA_,T>( t ) ;
+}
+
+} // Rcpp
+#endif
+

Modified: pkg/Rcpp/inst/include/Rcpp/sugar/sugar.h
===================================================================
--- pkg/Rcpp/inst/include/Rcpp/sugar/sugar.h	2010-06-17 16:48:12 UTC (rev 1574)
+++ pkg/Rcpp/inst/include/Rcpp/sugar/sugar.h	2010-06-17 17:32:41 UTC (rev 1575)
@@ -25,6 +25,7 @@
 // implementations
 #include <Rcpp/sugar/any.h>
 #include <Rcpp/sugar/all.h>
+#include <Rcpp/sugar/is_na.h>
 
 #include <Rcpp/sugar/Comparator.h>
 #include <Rcpp/sugar/Comparator_With_One_Value.h>

Added: pkg/Rcpp/inst/unitTests/runit.sugar.isna.R
===================================================================
--- pkg/Rcpp/inst/unitTests/runit.sugar.isna.R	                        (rev 0)
+++ pkg/Rcpp/inst/unitTests/runit.sugar.isna.R	2010-06-17 17:32:41 UTC (rev 1575)
@@ -0,0 +1,39 @@
+#!/usr/bin/r -t
+#
+# Copyright (C) 2010	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/>.
+
+test.sugar.isna <- function( ){
+
+	fx <- cxxfunction( signature( x = "numeric" ), '
+		NumericVector xx(x) ;
+		return wrap( is_na( xx ) ) ;
+	', plugin = "Rcpp" )
+	
+	checkEquals( fx( 1:10) , rep(FALSE,10) )
+}
+
+test.sugar.isna.isna <- function( ){
+
+	fx <- cxxfunction( signature( x = "numeric" ), '
+		NumericVector xx(x) ;
+		return wrap( is_na( is_na( xx ) ) ) ;
+	', plugin = "Rcpp" )
+	
+	checkEquals( fx( c(1:5,NA,7:10) ) , rep(FALSE,10) )
+}
+



More information about the Rcpp-commits mailing list