[Rcpp-commits] r2360 - in pkg/Rcpp: . inst/include/Rcpp/sugar/functions inst/unitTests
noreply at r-forge.r-project.org
noreply at r-forge.r-project.org
Sun Oct 24 03:51:32 CEST 2010
Author: romain
Date: 2010-10-24 03:51:28 +0200 (Sun, 24 Oct 2010)
New Revision: 2360
Added:
pkg/Rcpp/inst/include/Rcpp/sugar/functions/Lazy.h
pkg/Rcpp/inst/include/Rcpp/sugar/functions/cumsum.h
Modified:
pkg/Rcpp/ChangeLog
pkg/Rcpp/inst/include/Rcpp/sugar/functions/functions.h
pkg/Rcpp/inst/include/Rcpp/sugar/functions/sum.h
pkg/Rcpp/inst/unitTests/runit.sugar.R
Log:
sugar version of cumsum
Modified: pkg/Rcpp/ChangeLog
===================================================================
--- pkg/Rcpp/ChangeLog 2010-10-24 01:35:34 UTC (rev 2359)
+++ pkg/Rcpp/ChangeLog 2010-10-24 01:51:28 UTC (rev 2360)
@@ -11,6 +11,8 @@
* inst/include/Rcpp/sugar/functions/sum.h: the sugar version of sum
now takes care of missing values correctly
+ * inst/include/Rcpp/sugar/functions/cumsum.h: sugar version of cumsum
+
2010-10-21 Romain Francois <romain at r-enthusiasts.com>
* inst/include/Rcpp/vector/MatrixColumn.h: Fixed indexing bug (mismatch
Added: pkg/Rcpp/inst/include/Rcpp/sugar/functions/Lazy.h
===================================================================
--- pkg/Rcpp/inst/include/Rcpp/sugar/functions/Lazy.h (rev 0)
+++ pkg/Rcpp/inst/include/Rcpp/sugar/functions/Lazy.h 2010-10-24 01:51:28 UTC (rev 2360)
@@ -0,0 +1,37 @@
+// -*- mode: C++; c-indent-level: 4; c-basic-offset: 4; tab-width: 8 -*-
+//
+// Lazy.h: Rcpp R/C++ interface class library -- Lazy
+//
+// 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__Lazy_h
+#define Rcpp__sugar__Lazy_h
+
+namespace Rcpp{
+namespace sugar{
+
+template <typename T, typename EXPR>
+class Lazy {
+public:
+ inline operator T() const { return static_cast<const EXPR&>(*this).get() ; }
+} ;
+
+}
+}
+
+#endif
Added: pkg/Rcpp/inst/include/Rcpp/sugar/functions/cumsum.h
===================================================================
--- pkg/Rcpp/inst/include/Rcpp/sugar/functions/cumsum.h (rev 0)
+++ pkg/Rcpp/inst/include/Rcpp/sugar/functions/cumsum.h 2010-10-24 01:51:28 UTC (rev 2360)
@@ -0,0 +1,71 @@
+// -*- mode: C++; c-indent-level: 4; c-basic-offset: 4; tab-width: 8 -*-
+//
+// cumsum.h: Rcpp R/C++ interface class library -- cumsum
+//
+// 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__cumsum_h
+#define Rcpp__sugar__cumsum_h
+
+namespace Rcpp{
+namespace sugar{
+
+template <int RTYPE, bool NA, typename T>
+class Cumsum : public Lazy< Rcpp::Vector<RTYPE> , Cumsum<RTYPE,NA,T> > {
+public:
+ typedef typename Rcpp::VectorBase<RTYPE,NA,T> VEC_TYPE ;
+ typedef typename Rcpp::traits::storage_type<RTYPE>::type STORAGE ;
+ typedef Rcpp::Vector<RTYPE> VECTOR ;
+
+ Cumsum( const VEC_TYPE& object_ ) : object(object_){}
+
+ VECTOR get() const {
+ int n = object.size() ;
+ VECTOR result( n, Rcpp::traits::get_na<RTYPE>() ) ;
+ STORAGE current = object[0] ;
+ if( Rcpp::traits::is_na<RTYPE>(current) )
+ return result ;
+ result[0] = current ;
+ for( int i=1; i<n; i++){
+ current = object[i] ;
+ if( Rcpp::traits::is_na<RTYPE>(current) )
+ return result ;
+ result[i] = result[i-1] + current ;
+ }
+ return result ;
+ }
+private:
+ const VEC_TYPE& object ;
+} ;
+
+} // sugar
+
+template <bool NA, typename T>
+inline sugar::Cumsum<INTSXP,NA,T> cumsum( const VectorBase<INTSXP,NA,T>& t){
+ return sugar::Cumsum<INTSXP,NA,T>( t ) ;
+}
+
+template <bool NA, typename T>
+inline sugar::Cumsum<REALSXP,NA,T> cumsum( const VectorBase<REALSXP,NA,T>& t){
+ return sugar::Cumsum<REALSXP,NA,T>( t ) ;
+}
+
+
+} // Rcpp
+#endif
+
Modified: pkg/Rcpp/inst/include/Rcpp/sugar/functions/functions.h
===================================================================
--- pkg/Rcpp/inst/include/Rcpp/sugar/functions/functions.h 2010-10-24 01:35:34 UTC (rev 2359)
+++ pkg/Rcpp/inst/include/Rcpp/sugar/functions/functions.h 2010-10-24 01:51:28 UTC (rev 2360)
@@ -25,6 +25,8 @@
/* for exp( double ) */
#include <cmath>
+#include <Rcpp/sugar/functions/Lazy.h>
+
#include <Rcpp/sugar/functions/math.h>
#include <Rcpp/sugar/functions/complex.h>
@@ -49,6 +51,6 @@
#include <Rcpp/sugar/functions/tail.h>
#include <Rcpp/sugar/functions/sum.h>
+#include <Rcpp/sugar/functions/cumsum.h>
-
#endif
Modified: pkg/Rcpp/inst/include/Rcpp/sugar/functions/sum.h
===================================================================
--- pkg/Rcpp/inst/include/Rcpp/sugar/functions/sum.h 2010-10-24 01:35:34 UTC (rev 2359)
+++ pkg/Rcpp/inst/include/Rcpp/sugar/functions/sum.h 2010-10-24 01:51:28 UTC (rev 2360)
@@ -25,13 +25,6 @@
namespace Rcpp{
namespace sugar{
-template <typename T, typename EXPR>
-class Lazy {
-public:
- inline operator T() const { return static_cast<const EXPR&>(*this).get() ; }
-} ;
-
-
template <int RTYPE, bool NA, typename T>
class Sum : public Lazy< typename Rcpp::traits::storage_type<RTYPE>::type , Sum<RTYPE,NA,T> > {
public:
Modified: pkg/Rcpp/inst/unitTests/runit.sugar.R
===================================================================
--- pkg/Rcpp/inst/unitTests/runit.sugar.R 2010-10-24 01:35:34 UTC (rev 2359)
+++ pkg/Rcpp/inst/unitTests/runit.sugar.R 2010-10-24 01:51:28 UTC (rev 2360)
@@ -652,6 +652,14 @@
double res = sum( xx ) ;
return wrap( res ) ;
'
+ ),
+ "runit_cumsum" = list(
+ signature( x = "numeric" ),
+ '
+ NumericVector xx(x) ;
+ NumericVector res = cumsum( xx ) ;
+ return res ;
+ '
)
)
@@ -1273,3 +1281,11 @@
checkEquals( fx(x), sum(x) )
}
+test.sugar.cumsum <- function(){
+ fx <- .rcpp.sugar$runit_cumsum
+ x <- rnorm( 10 )
+ checkEquals( fx(x), cumsum(x) )
+ x[4] <- NA
+ checkEquals( fx(x), cumsum(x) )
+}
+
More information about the Rcpp-commits
mailing list