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

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Thu Jun 17 14:29:20 CEST 2010


Author: romain
Date: 2010-06-17 14:29:19 +0200 (Thu, 17 Jun 2010)
New Revision: 1570

Added:
   pkg/Rcpp/inst/unitTests/runit.sugar.all.primitive.R
Modified:
   pkg/Rcpp/inst/include/Rcpp/sugar/Comparator_With_One_Value.h
   pkg/Rcpp/inst/include/Rcpp/sugar/logical_operators__Vector__primitive.h
Log:
more generic Vector to primitive binary operators

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 11:47:23 UTC (rev 1569)
+++ pkg/Rcpp/inst/include/Rcpp/sugar/Comparator_With_One_Value.h	2010-06-17 12:29:19 UTC (rev 1570)
@@ -25,14 +25,14 @@
 namespace Rcpp{
 namespace sugar{
 
-template <int RTYPE, typename Operator>
-class Comparator_With_One_Value : public LogicalResult< Comparator_With_One_Value<RTYPE,Operator> > {
+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:
-	typedef Vector<RTYPE> VEC ;
 	typedef typename traits::storage_type<RTYPE>::type STORAGE ;
 	typedef r_binary_op<RTYPE,Operator> R_OPERATOR ;
 	
-	Comparator_With_One_Value( const VEC& lhs_, STORAGE rhs_ ) : 
+	Comparator_With_One_Value( const VEC_TYPE& lhs_, STORAGE rhs_ ) : 
 		op(), lhs(lhs_), rhs(rhs_){}
 	
 	inline int operator[]( int i ) const {
@@ -43,7 +43,7 @@
 	
 private:
 	R_OPERATOR op ;
-	const VEC& lhs ;
+	const VEC_TYPE& lhs ;
 	STORAGE rhs ;
 } ;
 

Modified: pkg/Rcpp/inst/include/Rcpp/sugar/logical_operators__Vector__primitive.h
===================================================================
--- pkg/Rcpp/inst/include/Rcpp/sugar/logical_operators__Vector__primitive.h	2010-06-17 11:47:23 UTC (rev 1569)
+++ pkg/Rcpp/inst/include/Rcpp/sugar/logical_operators__Vector__primitive.h	2010-06-17 12:29:19 UTC (rev 1570)
@@ -23,104 +23,268 @@
 #ifndef Rcpp__sugar__logical_operators__Vector__primitive_h
 #define Rcpp__sugar__logical_operators__Vector__primitive_h
 
-/* Vector op primitive */
-template <int RTYPE>
-inline Rcpp::sugar::Comparator_With_One_Value< RTYPE , Rcpp::sugar::less<RTYPE> > 
-operator<( const Rcpp::Vector<RTYPE>& lhs , typename Rcpp::traits::storage_type<RTYPE>::type rhs ){
-	return Rcpp::sugar::Comparator_With_One_Value<RTYPE, Rcpp::sugar::less<RTYPE> >( 
-		lhs, rhs
+/* Vector < primitive */
+template <int RTYPE, bool na, typename VEC_TYPE>
+inline Rcpp::sugar::Comparator_With_One_Value<
+	RTYPE , 
+	Rcpp::sugar::less<RTYPE>, 
+	na, 
+	Rcpp::VectorBase<RTYPE,na,VEC_TYPE>
+	> 
+operator<( 
+	const Rcpp::VectorBase<RTYPE,na,VEC_TYPE>& lhs , 
+	typename Rcpp::traits::storage_type<RTYPE>::type rhs
+	){
+	return Rcpp::sugar::Comparator_With_One_Value<
+		RTYPE, 
+		Rcpp::sugar::less<RTYPE>, 
+		na,
+		Rcpp::VectorBase<RTYPE,na,VEC_TYPE> 
+		>( 
+			lhs, rhs
 		) ;
 }
-
-template <int RTYPE>
-inline Rcpp::sugar::Comparator_With_One_Value< RTYPE , Rcpp::sugar::greater<RTYPE> > 
-operator>( const Rcpp::Vector<RTYPE>& lhs , typename Rcpp::traits::storage_type<RTYPE>::type rhs ){
-	return Rcpp::sugar::Comparator_With_One_Value<RTYPE, Rcpp::sugar::greater<RTYPE> >( 
-		lhs, rhs
+template <int RTYPE, bool na, typename VEC_TYPE>
+inline Rcpp::sugar::Comparator_With_One_Value<
+	RTYPE , 
+	Rcpp::sugar::less<RTYPE>, 
+	na, 
+	Rcpp::VectorBase<RTYPE,na,VEC_TYPE>
+	> 
+operator>( 
+	typename Rcpp::traits::storage_type<RTYPE>::type rhs,
+	const Rcpp::VectorBase<RTYPE,na,VEC_TYPE>& lhs
+	){
+	return Rcpp::sugar::Comparator_With_One_Value<
+		RTYPE, 
+		Rcpp::sugar::less<RTYPE>, 
+		na,
+		Rcpp::VectorBase<RTYPE,na,VEC_TYPE> 
+		>( 
+			lhs, rhs
 		) ;
 }
 
-template <int RTYPE>
-inline Rcpp::sugar::Comparator_With_One_Value< RTYPE , Rcpp::sugar::less_or_equal<RTYPE> > 
-operator<=( const Rcpp::Vector<RTYPE>& lhs , typename Rcpp::traits::storage_type<RTYPE>::type rhs ){
-	return Rcpp::sugar::Comparator_With_One_Value<RTYPE, Rcpp::sugar::less_or_equal<RTYPE> >( 
-		lhs, rhs
+
+
+/* Vector > primitive */
+template <int RTYPE, bool na, typename VEC_TYPE>
+inline Rcpp::sugar::Comparator_With_One_Value<
+	RTYPE , 
+	Rcpp::sugar::greater<RTYPE>, 
+	na, 
+	Rcpp::VectorBase<RTYPE,na,VEC_TYPE>
+	> 
+operator>( 
+	const Rcpp::VectorBase<RTYPE,na,VEC_TYPE>& lhs , 
+	typename Rcpp::traits::storage_type<RTYPE>::type rhs
+	){
+	return Rcpp::sugar::Comparator_With_One_Value<
+		RTYPE, 
+		Rcpp::sugar::greater<RTYPE>, 
+		na,
+		Rcpp::VectorBase<RTYPE,na,VEC_TYPE> 
+		>( 
+			lhs, rhs
 		) ;
 }
-
-template <int RTYPE>
-inline Rcpp::sugar::Comparator_With_One_Value< RTYPE , Rcpp::sugar::greater_or_equal<RTYPE> > 
-operator>=( const Rcpp::Vector<RTYPE>& lhs , typename Rcpp::traits::storage_type<RTYPE>::type rhs ){
-	return Rcpp::sugar::Comparator_With_One_Value<RTYPE, Rcpp::sugar::greater_or_equal<RTYPE> >( 
-		lhs, rhs
+template <int RTYPE, bool na, typename VEC_TYPE>
+inline Rcpp::sugar::Comparator_With_One_Value<
+	RTYPE , 
+	Rcpp::sugar::greater<RTYPE>, 
+	na, 
+	Rcpp::VectorBase<RTYPE,na,VEC_TYPE>
+	> 
+operator<( 
+	typename Rcpp::traits::storage_type<RTYPE>::type rhs,
+	const Rcpp::VectorBase<RTYPE,na,VEC_TYPE>& lhs
+	){
+	return Rcpp::sugar::Comparator_With_One_Value<
+		RTYPE, 
+		Rcpp::sugar::greater<RTYPE>, 
+		na,
+		Rcpp::VectorBase<RTYPE,na,VEC_TYPE> 
+		>( 
+			lhs, rhs
 		) ;
 }
 
-template <int RTYPE>
-inline Rcpp::sugar::Comparator_With_One_Value< RTYPE , Rcpp::sugar::equal<RTYPE> > 
-operator==( const Rcpp::Vector<RTYPE>& lhs , typename Rcpp::traits::storage_type<RTYPE>::type rhs ){
-	return Rcpp::sugar::Comparator_With_One_Value<RTYPE, Rcpp::sugar::equal<RTYPE> >( 
-		lhs, rhs
+
+
+/* Vector <= primitive */
+template <int RTYPE, bool na, typename VEC_TYPE>
+inline Rcpp::sugar::Comparator_With_One_Value<
+	RTYPE , 
+	Rcpp::sugar::less_or_equal<RTYPE>, 
+	na, 
+	Rcpp::VectorBase<RTYPE,na,VEC_TYPE>
+	> 
+operator<=( 
+	const Rcpp::VectorBase<RTYPE,na,VEC_TYPE>& lhs , 
+	typename Rcpp::traits::storage_type<RTYPE>::type rhs
+	){
+	return Rcpp::sugar::Comparator_With_One_Value<
+		RTYPE, 
+		Rcpp::sugar::less_or_equal<RTYPE>, 
+		na,
+		Rcpp::VectorBase<RTYPE,na,VEC_TYPE> 
+		>( 
+			lhs, rhs
 		) ;
 }
-
-template <int RTYPE>
-inline Rcpp::sugar::Comparator_With_One_Value< RTYPE , Rcpp::sugar::not_equal<RTYPE> > 
-operator!=( const Rcpp::Vector<RTYPE>& lhs , typename Rcpp::traits::storage_type<RTYPE>::type rhs ){
-	return Rcpp::sugar::Comparator_With_One_Value<RTYPE, Rcpp::sugar::not_equal<RTYPE> >( 
-		lhs, rhs
+template <int RTYPE, bool na, typename VEC_TYPE>
+inline Rcpp::sugar::Comparator_With_One_Value<
+	RTYPE , 
+	Rcpp::sugar::less_or_equal<RTYPE>, 
+	na, 
+	Rcpp::VectorBase<RTYPE,na,VEC_TYPE>
+	> 
+operator>=( 
+	typename Rcpp::traits::storage_type<RTYPE>::type rhs,
+	const Rcpp::VectorBase<RTYPE,na,VEC_TYPE>& lhs 
+	){
+	return Rcpp::sugar::Comparator_With_One_Value<
+		RTYPE, 
+		Rcpp::sugar::less_or_equal<RTYPE>, 
+		na,
+		Rcpp::VectorBase<RTYPE,na,VEC_TYPE> 
+		>( 
+			lhs, rhs
 		) ;
 }
 
 
 
-/* primitive op Vector */
-template <int RTYPE>
-inline Rcpp::sugar::Comparator_With_One_Value< RTYPE , Rcpp::sugar::greater<RTYPE> > 
-operator<( typename Rcpp::traits::storage_type<RTYPE>::type lhs , const Rcpp::Vector<RTYPE>& rhs ){
-	return Rcpp::sugar::Comparator_With_One_Value<RTYPE, Rcpp::sugar::greater<RTYPE> >( 
-		rhs, lhs
+
+/* Vector >= primitive */
+template <int RTYPE, bool na, typename VEC_TYPE>
+inline Rcpp::sugar::Comparator_With_One_Value<
+	RTYPE , 
+	Rcpp::sugar::greater_or_equal<RTYPE>, 
+	na, 
+	Rcpp::VectorBase<RTYPE,na,VEC_TYPE>
+	> 
+operator>=( 
+	const Rcpp::VectorBase<RTYPE,na,VEC_TYPE>& lhs , 
+	typename Rcpp::traits::storage_type<RTYPE>::type rhs
+	){
+	return Rcpp::sugar::Comparator_With_One_Value<
+		RTYPE, 
+		Rcpp::sugar::greater_or_equal<RTYPE>, 
+		na,
+		Rcpp::VectorBase<RTYPE,na,VEC_TYPE> 
+		>( 
+			lhs, rhs
 		) ;
 }
-
-template <int RTYPE>
-inline Rcpp::sugar::Comparator_With_One_Value< RTYPE , Rcpp::sugar::less<RTYPE> > 
-operator>( typename Rcpp::traits::storage_type<RTYPE>::type lhs , const Rcpp::Vector<RTYPE>& rhs ){
-	return Rcpp::sugar::Comparator_With_One_Value<RTYPE, Rcpp::sugar::less<RTYPE> >( 
-		rhs, lhs
+template <int RTYPE, bool na, typename VEC_TYPE>
+inline Rcpp::sugar::Comparator_With_One_Value<
+	RTYPE , 
+	Rcpp::sugar::greater_or_equal<RTYPE>, 
+	na, 
+	Rcpp::VectorBase<RTYPE,na,VEC_TYPE>
+	> 
+operator<=( 
+	typename Rcpp::traits::storage_type<RTYPE>::type rhs,
+	const Rcpp::VectorBase<RTYPE,na,VEC_TYPE>& lhs
+	){
+	return Rcpp::sugar::Comparator_With_One_Value<
+		RTYPE, 
+		Rcpp::sugar::greater_or_equal<RTYPE>, 
+		na,
+		Rcpp::VectorBase<RTYPE,na,VEC_TYPE> 
+		>( 
+			lhs, rhs
 		) ;
 }
 
-template <int RTYPE>
-inline Rcpp::sugar::Comparator_With_One_Value< RTYPE , Rcpp::sugar::greater_or_equal<RTYPE> > 
-operator<=( typename Rcpp::traits::storage_type<RTYPE>::type lhs , const Rcpp::Vector<RTYPE>& rhs ){
-	return Rcpp::sugar::Comparator_With_One_Value<RTYPE, Rcpp::sugar::greater_or_equal<RTYPE> >( 
-		rhs, lhs
+
+
+/* Vector == primitive */
+template <int RTYPE, bool na, typename VEC_TYPE>
+inline Rcpp::sugar::Comparator_With_One_Value<
+	RTYPE , 
+	Rcpp::sugar::equal<RTYPE>, 
+	na, 
+	Rcpp::VectorBase<RTYPE,na,VEC_TYPE>
+	> 
+operator==( 
+	const Rcpp::VectorBase<RTYPE,na,VEC_TYPE>& lhs , 
+	typename Rcpp::traits::storage_type<RTYPE>::type rhs
+	){
+	return Rcpp::sugar::Comparator_With_One_Value<
+		RTYPE, 
+		Rcpp::sugar::equal<RTYPE>, 
+		na,
+		Rcpp::VectorBase<RTYPE,na,VEC_TYPE> 
+		>( 
+			lhs, rhs
 		) ;
 }
-
-template <int RTYPE>
-inline Rcpp::sugar::Comparator_With_One_Value< RTYPE , Rcpp::sugar::less_or_equal<RTYPE> > 
-operator>=( typename Rcpp::traits::storage_type<RTYPE>::type lhs , const Rcpp::Vector<RTYPE>& rhs ){
-	return Rcpp::sugar::Comparator_With_One_Value<RTYPE, Rcpp::sugar::less_or_equal<RTYPE> >( 
-		rhs, lhs
+template <int RTYPE, bool na, typename VEC_TYPE>
+inline Rcpp::sugar::Comparator_With_One_Value<
+	RTYPE , 
+	Rcpp::sugar::equal<RTYPE>, 
+	na, 
+	Rcpp::VectorBase<RTYPE,na,VEC_TYPE>
+	> 
+operator==( 
+	typename Rcpp::traits::storage_type<RTYPE>::type rhs,
+	const Rcpp::VectorBase<RTYPE,na,VEC_TYPE>& lhs
+	){
+	return Rcpp::sugar::Comparator_With_One_Value<
+		RTYPE, 
+		Rcpp::sugar::equal<RTYPE>, 
+		na,
+		Rcpp::VectorBase<RTYPE,na,VEC_TYPE> 
+		>( 
+			lhs, rhs
 		) ;
 }
 
-template <int RTYPE>
-inline Rcpp::sugar::Comparator_With_One_Value< RTYPE , Rcpp::sugar::equal<RTYPE> > 
-operator==( typename Rcpp::traits::storage_type<RTYPE>::type lhs , const Rcpp::Vector<RTYPE>& rhs ){
-	return Rcpp::sugar::Comparator_With_One_Value<RTYPE, Rcpp::sugar::equal<RTYPE> >( 
-		rhs, lhs
+
+
+/* Vector != primitive */
+template <int RTYPE, bool na, typename VEC_TYPE>
+inline Rcpp::sugar::Comparator_With_One_Value<
+	RTYPE , 
+	Rcpp::sugar::not_equal<RTYPE>, 
+	na, 
+	Rcpp::VectorBase<RTYPE,na,VEC_TYPE>
+	> 
+operator!=( 
+	const Rcpp::VectorBase<RTYPE,na,VEC_TYPE>& lhs , 
+	typename Rcpp::traits::storage_type<RTYPE>::type rhs
+	){
+	return Rcpp::sugar::Comparator_With_One_Value<
+		RTYPE, 
+		Rcpp::sugar::not_equal<RTYPE>, 
+		na,
+		Rcpp::VectorBase<RTYPE,na,VEC_TYPE> 
+		>( 
+			lhs, rhs
 		) ;
 }
-
-template <int RTYPE>
-inline Rcpp::sugar::Comparator_With_One_Value< RTYPE , Rcpp::sugar::not_equal<RTYPE> > 
-operator!=( typename Rcpp::traits::storage_type<RTYPE>::type lhs , const Rcpp::Vector<RTYPE>& rhs ){
-	return Rcpp::sugar::Comparator_With_One_Value<RTYPE, Rcpp::sugar::not_equal<RTYPE> >( 
-		rhs, lhs
+template <int RTYPE, bool na, typename VEC_TYPE>
+inline Rcpp::sugar::Comparator_With_One_Value<
+	RTYPE , 
+	Rcpp::sugar::not_equal<RTYPE>, 
+	na, 
+	Rcpp::VectorBase<RTYPE,na,VEC_TYPE>
+	> 
+operator!=( 
+	typename Rcpp::traits::storage_type<RTYPE>::type rhs,
+	const Rcpp::VectorBase<RTYPE,na,VEC_TYPE>& lhs 
+	){
+	return Rcpp::sugar::Comparator_With_One_Value<
+		RTYPE, 
+		Rcpp::sugar::not_equal<RTYPE>, 
+		na,
+		Rcpp::VectorBase<RTYPE,na,VEC_TYPE> 
+		>( 
+			lhs, rhs
 		) ;
 }
 
+
 #endif

Added: pkg/Rcpp/inst/unitTests/runit.sugar.all.primitive.R
===================================================================
--- pkg/Rcpp/inst/unitTests/runit.sugar.all.primitive.R	                        (rev 0)
+++ pkg/Rcpp/inst/unitTests/runit.sugar.all.primitive.R	2010-06-17 12:29:19 UTC (rev 1570)
@@ -0,0 +1,117 @@
+#!/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.all.one.less <- function( ){
+
+	fx <- cxxfunction( signature( x = "numeric" ), '
+		NumericVector xx(x) ;
+		return all( xx < 5.0 ) ;
+	
+	', plugin = "Rcpp" )
+	
+	checkTrue( fx( 1 ) )
+	checkTrue( ! fx( 1:10 ) )
+	checkTrue( is.na( fx( NA ) ) )
+	checkTrue( is.na( fx( c( NA, 1)  ) ) )
+	checkTrue( ! fx( c( 6, NA)  ) )
+	
+}
+
+test.sugar.all.one.greater <- function( ){
+
+	fx <- cxxfunction( signature( x = "numeric" ), '
+	    NumericVector xx(x) ;
+		return all( xx > 5.0 ) ;
+	
+	', plugin = "Rcpp" )
+	
+	checkTrue( ! fx( 1 ) )
+	checkTrue( ! fx( 1:10 ) )
+	checkTrue( fx( 6:10 ) )
+	checkTrue( ! fx( c(NA, 1) ) )
+	checkTrue( is.na( fx( c(NA, 6) ) ) )
+}
+
+
+test.sugar.all.one.less.or.equal <- function( ){
+
+	fx <- cxxfunction( signature( x = "numeric" ), '
+		NumericVector xx(x) ;
+		return all( xx <= 5.0 ) ;
+	
+	', plugin = "Rcpp" )
+	
+	checkTrue( fx( 1 ) )
+	checkTrue( ! fx( 1:10 ) )
+	checkTrue( is.na( fx( NA ) ) )
+	checkTrue( is.na( fx( c( NA, 1)  ) ) )
+	checkTrue( ! fx( c( 6, NA)  ) )
+	checkTrue( fx( 5 ) )
+	
+}
+
+test.sugar.all.one.greater.or.equal <- function( ){
+
+	fx <- cxxfunction( signature( x = "numeric" ), '
+	    NumericVector xx(x) ;
+		return all( xx >= 5.0 ) ;
+	
+	', plugin = "Rcpp" )
+	
+	checkTrue( ! fx( 1 ) )
+	checkTrue( ! fx( 1:10 ) )
+	checkTrue( fx( 6:10 ) )
+	checkTrue( fx( 5 ) )
+	checkTrue( ! fx( c(NA, 1) ) )
+	checkTrue( is.na( fx( c(NA, 6) ) ) )
+}
+ 
+
+test.sugar.all.one.equal <- function( ){
+
+	fx <- cxxfunction( signature( x = "numeric" ), '
+	   	NumericVector xx(x) ;
+		return all( xx == 5.0 ) ;
+	
+	', plugin = "Rcpp" )
+	
+	checkTrue( ! fx( 1 ) )
+	checkTrue( ! fx( 1:2 ) )
+	checkTrue( fx( rep(5,4) ) )
+	checkTrue( is.na( fx( c(5,NA) ) ) )
+	checkTrue(! fx( c(NA, 1) ) )
+	
+}
+
+test.sugar.all.one.not.equal <- function( ){
+
+	fx <- cxxfunction( signature( x = "numeric" ), '
+		NumericVector xx(x) ;
+		return all( xx != 5.0 ) ;
+	
+	', plugin = "Rcpp" )
+	
+	checkTrue( fx( 1 ) )
+	checkTrue( fx( 1:2 ) )
+	checkTrue( ! fx( 5 ) )
+	checkTrue( is.na( fx( c(NA, 1) ) ) )
+	checkTrue( ! fx( c(NA, 5) ) )
+	
+}
+



More information about the Rcpp-commits mailing list