[Rcpp-commits] r1620 - in pkg/Rcpp: . inst/include/Rcpp/sugar/functions inst/unitTests
noreply at r-forge.r-project.org
noreply at r-forge.r-project.org
Sat Jun 19 12:21:28 CEST 2010
Author: romain
Date: 2010-06-19 12:21:27 +0200 (Sat, 19 Jun 2010)
New Revision: 1620
Added:
pkg/Rcpp/inst/include/Rcpp/sugar/functions/pmax.h
Modified:
pkg/Rcpp/TODO
pkg/Rcpp/inst/include/Rcpp/sugar/functions/functions.h
pkg/Rcpp/inst/unitTests/runit.sugar.pmin.R
Log:
pmax
Modified: pkg/Rcpp/TODO
===================================================================
--- pkg/Rcpp/TODO 2010-06-19 10:14:43 UTC (rev 1619)
+++ pkg/Rcpp/TODO 2010-06-19 10:21:27 UTC (rev 1620)
@@ -45,7 +45,7 @@
Syntactic sugar
- o duplicated, unique, count, sum, rep, head, tail, pmax
+ o duplicated, unique, count, sum, rep, head, tail
o min, max with specialization of the binary operators, so that we can do
things like this lazily:
Modified: pkg/Rcpp/inst/include/Rcpp/sugar/functions/functions.h
===================================================================
--- pkg/Rcpp/inst/include/Rcpp/sugar/functions/functions.h 2010-06-19 10:14:43 UTC (rev 1619)
+++ pkg/Rcpp/inst/include/Rcpp/sugar/functions/functions.h 2010-06-19 10:21:27 UTC (rev 1620)
@@ -30,5 +30,6 @@
#include <Rcpp/sugar/functions/lapply.h>
#include <Rcpp/sugar/functions/ifelse.h>
#include <Rcpp/sugar/functions/pmin.h>
+#include <Rcpp/sugar/functions/pmax.h>
#endif
Added: pkg/Rcpp/inst/include/Rcpp/sugar/functions/pmax.h
===================================================================
--- pkg/Rcpp/inst/include/Rcpp/sugar/functions/pmax.h (rev 0)
+++ pkg/Rcpp/inst/include/Rcpp/sugar/functions/pmax.h 2010-06-19 10:21:27 UTC (rev 1620)
@@ -0,0 +1,207 @@
+// -*- mode: C++; c-indent-level: 4; c-basic-offset: 4; tab-width: 8 -*-
+//
+// pmax.h: Rcpp R/C++ interface class library -- pmax
+//
+// 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__pmax_h
+#define Rcpp__sugar__pmax_h
+
+namespace Rcpp{
+namespace sugar{
+
+template <int RTYPE, bool LHS_NA, bool RHS_NA> class pmax_op {
+public:
+ typedef typename Rcpp::traits::storage_type<RTYPE>::type STORAGE ;
+
+ inline STORAGE operator()( STORAGE left, STORAGE right ) const {
+ if( Rcpp::traits::is_na<RTYPE>(left) ) return left ;
+ if( Rcpp::traits::is_na<RTYPE>(right) ) return right ;
+ return left > right ? left : right ;
+ }
+
+} ;
+template <int RTYPE, bool LHS_NA> class pmax_op<RTYPE,LHS_NA,false> {
+public:
+ typedef typename Rcpp::traits::storage_type<RTYPE>::type STORAGE ;
+
+ inline STORAGE operator()( STORAGE left, STORAGE right ) const {
+ if( Rcpp::traits::is_na<RTYPE>(left) ) return left ;
+ return left > right ? left : right ;
+ }
+} ;
+template <int RTYPE, bool RHS_NA> class pmax_op<RTYPE,false,RHS_NA> {
+public:
+ typedef typename Rcpp::traits::storage_type<RTYPE>::type STORAGE ;
+
+ inline STORAGE operator()( STORAGE left, STORAGE right ) const {
+ if( Rcpp::traits::is_na<RTYPE>(right) ) return right ;
+ return left > right ? left : right ;
+ }
+} ;
+template <int RTYPE> class pmax_op<RTYPE,false,false> {
+public:
+ typedef typename Rcpp::traits::storage_type<RTYPE>::type STORAGE ;
+
+ inline STORAGE operator()( STORAGE left, STORAGE right ) const {
+ return left > right ? left : right ;
+ }
+} ;
+
+template <int RTYPE,bool NA> class pmax_op_Vector_Primitive {
+public:
+ typedef typename Rcpp::traits::storage_type<RTYPE>::type STORAGE ;
+
+ pmax_op_Vector_Primitive( STORAGE right_ ) :
+ right(right_), isna( Rcpp::traits::is_na<RTYPE>(right_) ){}
+
+ inline STORAGE operator()( STORAGE left ) const {
+ if( isna ) return right ;
+ if( Rcpp::traits::is_na<RTYPE>(left) ) return left ;
+ return left > right ? left : right ;
+ }
+
+private:
+ STORAGE right ;
+ bool isna ;
+} ;
+
+template <int RTYPE> class pmax_op_Vector_Primitive<RTYPE,false> {
+public:
+ typedef typename Rcpp::traits::storage_type<RTYPE>::type STORAGE ;
+
+ pmax_op_Vector_Primitive( STORAGE right_ ) :
+ right(right_), isna( Rcpp::traits::is_na<RTYPE>(right_) ){}
+
+ inline STORAGE operator()( STORAGE left ) const {
+ if( isna ) return right ;
+ return left > right ? left : right ;
+ }
+
+private:
+ STORAGE right ;
+ bool isna ;
+} ;
+
+
+
+template <
+ int RTYPE,
+ bool LHS_NA, typename LHS_T,
+ bool RHS_NA, typename RHS_T
+ >
+class Pmax_Vector_Vector : public VectorBase<
+ RTYPE ,
+ ( LHS_NA || RHS_NA ) ,
+ Pmax_Vector_Vector<RTYPE,LHS_NA,LHS_T,RHS_NA,RHS_T>
+> {
+public:
+ typedef typename Rcpp::VectorBase<RTYPE,LHS_NA,LHS_T> LHS_TYPE ;
+ typedef typename Rcpp::VectorBase<RTYPE,RHS_NA,RHS_T> RHS_TYPE ;
+ typedef typename Rcpp::traits::storage_type<RTYPE>::type STORAGE ;
+ typedef pmax_op<RTYPE,LHS_NA,RHS_NA> OPERATOR ;
+
+ Pmax_Vector_Vector( const LHS_TYPE& lhs_, const RHS_TYPE& rhs_ ) :
+ lhs(lhs_), rhs(rhs_), op() {}
+
+ inline STORAGE operator[]( int i ) const {
+ return op( lhs[i], rhs[i] ) ;
+ }
+ inline int size() const { return lhs.size() ; }
+
+private:
+ const LHS_TYPE& lhs ;
+ const RHS_TYPE& rhs ;
+ OPERATOR op ;
+} ;
+
+
+
+template <
+ int RTYPE,
+ bool LHS_NA, typename LHS_T
+ >
+class Pmax_Vector_Primitive : public VectorBase<
+ RTYPE ,
+ true ,
+ Pmax_Vector_Primitive<RTYPE,LHS_NA,LHS_T>
+> {
+public:
+ typedef typename Rcpp::VectorBase<RTYPE,LHS_NA,LHS_T> LHS_TYPE ;
+ typedef typename Rcpp::traits::storage_type<RTYPE>::type STORAGE ;
+ typedef pmax_op_Vector_Primitive<RTYPE,LHS_NA> OPERATOR ;
+
+ Pmax_Vector_Primitive( const LHS_TYPE& lhs_, STORAGE rhs ) :
+ lhs(lhs_), op(rhs) {}
+
+ inline STORAGE operator[]( int i ) const {
+ return op( lhs[i] ) ;
+ }
+ inline int size() const { return lhs.size() ; }
+
+private:
+ const LHS_TYPE& lhs ;
+ OPERATOR op ;
+} ;
+
+
+
+} // sugar
+
+template <
+ int RTYPE,
+ bool LHS_NA, typename LHS_T,
+ bool RHS_NA, typename RHS_T
+>
+inline sugar::Pmax_Vector_Vector<RTYPE,LHS_NA,LHS_T,RHS_NA,RHS_T>
+pmax(
+ const Rcpp::VectorBase<RTYPE,LHS_NA,LHS_T>& lhs,
+ const Rcpp::VectorBase<RTYPE,RHS_NA,RHS_T>& rhs
+ ){
+ return sugar::Pmax_Vector_Vector<RTYPE,LHS_NA,LHS_T,RHS_NA,RHS_T>( lhs, rhs ) ;
+}
+
+template <
+ int RTYPE,
+ bool LHS_NA, typename LHS_T
+>
+inline sugar::Pmax_Vector_Primitive<RTYPE,LHS_NA,LHS_T>
+pmax(
+ const Rcpp::VectorBase<RTYPE,LHS_NA,LHS_T>& lhs,
+ typename Rcpp::traits::storage_type<RTYPE>::type rhs
+ ){
+ return sugar::Pmax_Vector_Primitive<RTYPE,LHS_NA,LHS_T>( lhs, rhs ) ;
+}
+
+
+template <
+ int RTYPE,
+ bool RHS_NA, typename RHS_T
+>
+inline sugar::Pmax_Vector_Primitive<RTYPE,RHS_NA,RHS_T>
+pmax(
+ typename Rcpp::traits::storage_type<RTYPE>::type lhs,
+ const Rcpp::VectorBase<RTYPE,RHS_NA,RHS_T>& rhs
+ ){
+ return sugar::Pmax_Vector_Primitive<RTYPE,RHS_NA,RHS_T>( rhs, lhs ) ;
+}
+
+
+} // Rcpp
+
+#endif
Modified: pkg/Rcpp/inst/unitTests/runit.sugar.pmin.R
===================================================================
--- pkg/Rcpp/inst/unitTests/runit.sugar.pmin.R 2010-06-19 10:14:43 UTC (rev 1619)
+++ pkg/Rcpp/inst/unitTests/runit.sugar.pmin.R 2010-06-19 10:21:27 UTC (rev 1620)
@@ -45,7 +45,41 @@
list(
c(1:5,rep(5,5)),
c(1:5,rep(5,5))
- ) )
+ )
+ )
}
+
+test.sugar.pmax <- function( ){
+
+ fx <- cxxfunction( signature( x = "numeric", y = "numeric" ), '
+
+ NumericVector xx(x) ;
+ NumericVector yy(y) ;
+ NumericVector res = pmax( xx, yy );
+ return res ;
+
+ ', plugin = "Rcpp" )
+
+ checkEquals( fx(1:10, 10:1) , c(10:6,6:10) )
+}
+
+test.sugar.pmax.one <- function( ){
+
+ fx <- cxxfunction( signature( x = "numeric" ), '
+ NumericVector xx(x) ;
+ return List::create(
+ pmax( xx, 5),
+ pmax( 5, xx)
+ ) ;
+ ', plugin = "Rcpp" )
+
+ checkEquals( fx(1:10) ,
+ list(
+ c(rep(5,5), 6:10),
+ c(rep(5,5), 6:10)
+ )
+ )
+}
+
More information about the Rcpp-commits
mailing list