[Rcpp-commits] r1597 - in pkg/Rcpp: . inst/include/Rcpp/sugar inst/unitTests
noreply at r-forge.r-project.org
noreply at r-forge.r-project.org
Fri Jun 18 13:05:42 CEST 2010
Author: romain
Date: 2010-06-18 13:05:42 +0200 (Fri, 18 Jun 2010)
New Revision: 1597
Added:
pkg/Rcpp/inst/include/Rcpp/sugar/divides.h
Modified:
pkg/Rcpp/TODO
pkg/Rcpp/inst/include/Rcpp/sugar/sugar.h
pkg/Rcpp/inst/unitTests/runit.sugar.times.R
Log:
operator/
Modified: pkg/Rcpp/TODO
===================================================================
--- pkg/Rcpp/TODO 2010-06-18 10:58:10 UTC (rev 1596)
+++ pkg/Rcpp/TODO 2010-06-18 11:05:42 UTC (rev 1597)
@@ -62,9 +62,7 @@
- size (nrow*ncol)
o matrix functions : row, col, lower_tri, upper_tri
-
- o other operators: /, ...
-
+
o lazy version of sapply as in this thread:
http://article.gmane.org/gmane.comp.lang.r.rcpp/455
Added: pkg/Rcpp/inst/include/Rcpp/sugar/divides.h
===================================================================
--- pkg/Rcpp/inst/include/Rcpp/sugar/divides.h (rev 0)
+++ pkg/Rcpp/inst/include/Rcpp/sugar/divides.h 2010-06-18 11:05:42 UTC (rev 1597)
@@ -0,0 +1,167 @@
+// -*- mode: C++; c-indent-level: 4; c-basic-offset: 4; tab-width: 8 -*-
+//
+// .h: Rcpp R/C++ interface class library -- operator/
+//
+// 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__divides_h
+#define Rcpp__sugar__divides_h
+
+namespace Rcpp{
+namespace sugar{
+
+ // TODO: what happens in the limits, see what R does
+ template <int RTYPE,bool LHS_NA, bool RHS_NA>
+ class divides{
+ public:
+ typedef typename traits::storage_type<RTYPE>::type STORAGE ;
+ inline STORAGE apply( STORAGE lhs, STORAGE rhs) const {
+ return traits::is_na<RTYPE>(lhs) ? lhs : ( traits::is_na<RTYPE>(rhs) ? rhs : (lhs / rhs) ) ;
+ }
+ } ;
+ template <int RTYPE,bool RHS_NA>
+ class divides<RTYPE,false,RHS_NA>{
+ public:
+ typedef typename traits::storage_type<RTYPE>::type STORAGE ;
+ inline STORAGE apply( STORAGE lhs, STORAGE rhs) const {
+ return traits::is_na<RTYPE>(rhs) ? rhs : (lhs / rhs);
+ }
+ } ;
+ template <int RTYPE,bool LHS_NA>
+ class divides<RTYPE,LHS_NA,false>{
+ public:
+ typedef typename traits::storage_type<RTYPE>::type STORAGE ;
+ inline STORAGE apply( STORAGE lhs, STORAGE rhs) const {
+ return traits::is_na<RTYPE>(lhs) ? lhs : (lhs / rhs);
+ }
+ } ;
+ template <int RTYPE>
+ class divides<RTYPE,false,false>{
+ public:
+ typedef typename traits::storage_type<RTYPE>::type STORAGE ;
+ inline STORAGE apply( STORAGE lhs, STORAGE rhs) const {
+ return lhs / rhs;
+ }
+ } ;
+
+
+ template <int RTYPE, bool _NA_, typename VEC_TYPE>
+ class Divides_Vector_Primitive : public Rcpp::VectorBase<RTYPE,true, Divides_Vector_Primitive<RTYPE,_NA_,VEC_TYPE> > {
+ public:
+ typedef typename traits::storage_type<RTYPE>::type STORAGE ;
+ typedef divides<RTYPE,_NA_,true> OPERATOR ;
+
+ Divides_Vector_Primitive( const VEC_TYPE& lhs_, STORAGE rhs_ ) :
+ lhs(lhs_), rhs(rhs_), op() {}
+
+ inline STORAGE operator[]( int i ) const {
+ return op.apply( lhs[i], rhs ) ;
+ }
+
+ inline int size() const { return lhs.size() ; }
+
+
+ private:
+ const VEC_TYPE& lhs ;
+ STORAGE rhs ;
+ OPERATOR op ;
+ } ;
+
+ template <int RTYPE, bool _NA_, typename VEC_TYPE>
+ class Divides_Primitive_Vector : public Rcpp::VectorBase<RTYPE,true, Divides_Primitive_Vector<RTYPE,_NA_,VEC_TYPE> > {
+ public:
+ typedef typename traits::storage_type<RTYPE>::type STORAGE ;
+ typedef divides<RTYPE,_NA_,true> OPERATOR ;
+
+ Divides_Primitive_Vector( STORAGE lhs_, const VEC_TYPE& rhs_ ) :
+ lhs(lhs_), rhs(rhs_), op() {}
+
+ inline STORAGE operator[]( int i ) const {
+ return op.apply( lhs, rhs[i] ) ;
+ }
+
+ inline int size() const { return rhs.size() ; }
+
+
+ private:
+ STORAGE lhs ;
+ const VEC_TYPE& rhs ;
+ OPERATOR op ;
+ } ;
+
+
+ template <int RTYPE, bool LHS_NA, typename LHS_VEC_TYPE, bool RHS_NA, typename RHS_VEC_TYPE >
+ class Divides_Vector_Vector : public Rcpp::VectorBase<RTYPE,true, Divides_Vector_Vector<RTYPE,LHS_NA,LHS_VEC_TYPE,RHS_NA,RHS_VEC_TYPE> > {
+ public:
+ typedef typename traits::storage_type<RTYPE>::type STORAGE ;
+ typedef divides<RTYPE,LHS_NA,RHS_NA> OPERATOR ;
+
+ Divides_Vector_Vector( const LHS_VEC_TYPE& lhs_, const RHS_VEC_TYPE& rhs_ ) :
+ lhs(lhs_), rhs(rhs_), op() {}
+
+ inline STORAGE operator[]( int i ) const {
+ return op.apply( lhs[i], rhs[i] ) ;
+ }
+
+ inline int size() const { return lhs.size() ; }
+
+ private:
+ const LHS_VEC_TYPE& lhs ;
+ const RHS_VEC_TYPE& rhs ;
+ OPERATOR op ;
+ } ;
+}
+}
+
+template <int RTYPE,bool _NA_, typename T>
+inline Rcpp::sugar::Divides_Vector_Primitive< RTYPE , _NA_ , Rcpp::VectorBase<RTYPE,_NA_,T> >
+operator/(
+ const Rcpp::VectorBase<RTYPE,_NA_,T>& lhs,
+ typename Rcpp::traits::storage_type<RTYPE>::type rhs
+) {
+ return Rcpp::sugar::Divides_Vector_Primitive<RTYPE,_NA_, Rcpp::VectorBase<RTYPE,_NA_,T> >( lhs, rhs ) ;
+}
+
+
+template <int RTYPE,bool _NA_, typename T>
+inline Rcpp::sugar::Divides_Primitive_Vector< RTYPE , _NA_ , Rcpp::VectorBase<RTYPE,_NA_,T> >
+operator/(
+ typename Rcpp::traits::storage_type<RTYPE>::type lhs,
+ const Rcpp::VectorBase<RTYPE,_NA_,T>& rhs
+) {
+ return Rcpp::sugar::Divides_Primitive_Vector<RTYPE,_NA_, Rcpp::VectorBase<RTYPE,_NA_,T> >( lhs, rhs ) ;
+}
+
+template <int RTYPE,bool LHS_NA, typename LHS_T, bool RHS_NA, typename RHS_T>
+inline Rcpp::sugar::Divides_Vector_Vector<
+ RTYPE ,
+ LHS_NA, Rcpp::VectorBase<RTYPE,LHS_NA,LHS_T>,
+ RHS_NA, Rcpp::VectorBase<RTYPE,RHS_NA,RHS_T>
+ >
+operator/(
+ const Rcpp::VectorBase<RTYPE,LHS_NA,LHS_T>& lhs,
+ const Rcpp::VectorBase<RTYPE,RHS_NA,RHS_T>& rhs
+) {
+ return Rcpp::sugar::Divides_Vector_Vector<
+ RTYPE,
+ LHS_NA, Rcpp::VectorBase<RTYPE,LHS_NA,LHS_T>,
+ RHS_NA, Rcpp::VectorBase<RTYPE,RHS_NA,RHS_T>
+ >( lhs, rhs ) ;
+}
+
+#endif
Modified: pkg/Rcpp/inst/include/Rcpp/sugar/sugar.h
===================================================================
--- pkg/Rcpp/inst/include/Rcpp/sugar/sugar.h 2010-06-18 10:58:10 UTC (rev 1596)
+++ pkg/Rcpp/inst/include/Rcpp/sugar/sugar.h 2010-06-18 11:05:42 UTC (rev 1597)
@@ -22,17 +22,19 @@
#ifndef RCPP_SUGAR_H
#define RCPP_SUGAR_H
-// implementations
+// functions
#include <Rcpp/sugar/any.h>
#include <Rcpp/sugar/all.h>
#include <Rcpp/sugar/is_na.h>
#include <Rcpp/sugar/seq_along.h>
+// operators
#include <Rcpp/sugar/Comparator.h>
#include <Rcpp/sugar/Comparator_With_One_Value.h>
#include <Rcpp/sugar/logical_operators.h>
#include <Rcpp/sugar/plus.h>
#include <Rcpp/sugar/minus.h>
#include <Rcpp/sugar/times.h>
+#include <Rcpp/sugar/divides.h>
#endif
Modified: pkg/Rcpp/inst/unitTests/runit.sugar.times.R
===================================================================
--- pkg/Rcpp/inst/unitTests/runit.sugar.times.R 2010-06-18 10:58:10 UTC (rev 1596)
+++ pkg/Rcpp/inst/unitTests/runit.sugar.times.R 2010-06-18 11:05:42 UTC (rev 1597)
@@ -39,3 +39,23 @@
)
}
+test.sugar.divides <- function( ){
+
+ fx <- cxxfunction( signature( x = "numeric" ), '
+ NumericVector xx(x) ;
+ return List::create(
+ xx / 10,
+ 10 / xx,
+ xx / xx
+ ) ;
+ ', plugin = "Rcpp" )
+
+ checkEquals( fx(1:10) ,
+ list(
+ 1:10/10,
+ 10/1:10,
+ rep(1,10)
+ )
+ )
+}
+
More information about the Rcpp-commits
mailing list