[Rcpp-commits] r1912 - in pkg/Rcpp: . inst inst/include/Rcpp/sugar/functions

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Thu Aug 5 12:06:22 CEST 2010


Author: romain
Date: 2010-08-05 12:06:22 +0200 (Thu, 05 Aug 2010)
New Revision: 1912

Added:
   pkg/Rcpp/inst/include/Rcpp/sugar/functions/sum.h
Modified:
   pkg/Rcpp/TODO
   pkg/Rcpp/inst/ChangeLog
   pkg/Rcpp/inst/include/Rcpp/sugar/functions/functions.h
Log:
initial vertsion of Rcpp::sum


Modified: pkg/Rcpp/TODO
===================================================================
--- pkg/Rcpp/TODO	2010-08-05 09:18:12 UTC (rev 1911)
+++ pkg/Rcpp/TODO	2010-08-05 10:06:22 UTC (rev 1912)
@@ -64,4 +64,8 @@
     o   for character vectors: nchar, grepl, sub, gsub
     
     o   Compound operators: ++,--,+=, -=, ...
+    
+    o	other stats functions : dnorm, etc ...
+    
+    o	sum : deal with missing values
 

Modified: pkg/Rcpp/inst/ChangeLog
===================================================================
--- pkg/Rcpp/inst/ChangeLog	2010-08-05 09:18:12 UTC (rev 1911)
+++ pkg/Rcpp/inst/ChangeLog	2010-08-05 10:06:22 UTC (rev 1912)
@@ -1,4 +1,4 @@
-2010-08-04  Romain Francois <romain at r-enthusiasts.com>
+2010-08-05  Romain Francois <romain at r-enthusiasts.com>
 
 	* inst/include/Rcpp/sugar/functions/seq_along.h: added seq(int,int) to 
 	mimic the R syntax : seq( 0, 5 )
@@ -9,6 +9,9 @@
 	Rcpp::stats::dbinom inspired by Richard Chandler post on Rcpp-devel:
 	http://lists.r-forge.r-project.org/pipermail/rcpp-devel/2010-August/000940.html
 
+	* inst/include/Rcpp/sugar/sum.h: preliminary version of Rcpp::sum (does not 
+	deal with NA properly yet)
+	
 2010-08-04  Romain Francois <romain at r-enthusiasts.com>
 
 	* inst/include/Rcpp/sugar/: rework sugar matrix so that operator()(int,int)

Modified: pkg/Rcpp/inst/include/Rcpp/sugar/functions/functions.h
===================================================================
--- pkg/Rcpp/inst/include/Rcpp/sugar/functions/functions.h	2010-08-05 09:18:12 UTC (rev 1911)
+++ pkg/Rcpp/inst/include/Rcpp/sugar/functions/functions.h	2010-08-05 10:06:22 UTC (rev 1912)
@@ -48,6 +48,8 @@
 #include <Rcpp/sugar/functions/head.h>
 #include <Rcpp/sugar/functions/tail.h>
 
+#include <Rcpp/sugar/functions/sum.h>
+
 #include <Rcpp/sugar/functions/Re.h>
 #include <Rcpp/sugar/functions/Im.h>
 #include <Rcpp/sugar/functions/Conj.h>

Added: pkg/Rcpp/inst/include/Rcpp/sugar/functions/sum.h
===================================================================
--- pkg/Rcpp/inst/include/Rcpp/sugar/functions/sum.h	                        (rev 0)
+++ pkg/Rcpp/inst/include/Rcpp/sugar/functions/sum.h	2010-08-05 10:06:22 UTC (rev 1912)
@@ -0,0 +1,65 @@
+// -*- mode: C++; c-indent-level: 4; c-basic-offset: 4; tab-width: 8 -*-
+//
+// sum.h: Rcpp R/C++ interface class library -- sum
+//
+// 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__sum_h
+#define Rcpp__sugar__sum_h
+
+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:
+	typedef typename Rcpp::VectorBase<RTYPE,NA,T> VEC_TYPE ;
+	typedef typename Rcpp::traits::storage_type<RTYPE>::type STORAGE ;
+	
+	Sum( const VEC_TYPE& object_ ) : object(object_){}
+	
+	inline STORAGE get() const {
+		return std::accumulate( object.begin(), object.end(), STORAGE() , std::plus<STORAGE>() ) ;
+	}         
+private:
+	const VEC_TYPE& object ;
+} ;
+	
+} // sugar
+
+template <bool NA, typename T>
+inline sugar::Sum<INTSXP,NA,T> sum( const VectorBase<INTSXP,NA,T>& t){
+	return sugar::Sum<INTSXP,NA,T>( t ) ;
+}
+
+template <bool NA, typename T>
+inline sugar::Sum<REALSXP,NA,T> sum( const VectorBase<REALSXP,NA,T>& t){
+	return sugar::Sum<REALSXP,NA,T>( t ) ;
+}
+
+
+} // Rcpp
+#endif
+



More information about the Rcpp-commits mailing list