[Rcpp-commits] r3840 - in pkg/RcppBDT: . R demo inst/include src

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Fri Oct 26 05:00:49 CEST 2012


Author: edd
Date: 2012-10-26 05:00:49 +0200 (Fri, 26 Oct 2012)
New Revision: 3840

Added:
   pkg/RcppBDT/demo/RcppBDTpt.R
Modified:
   pkg/RcppBDT/ChangeLog
   pkg/RcppBDT/R/zzz.R
   pkg/RcppBDT/demo/RcppBDTdu.R
   pkg/RcppBDT/inst/include/RcppBDTdt.h
   pkg/RcppBDT/inst/include/RcppBDTdu.h
   pkg/RcppBDT/src/RcppBDTdt.cpp
   pkg/RcppBDT/src/RcppBDTdu.cpp
Log:
a few more arithmetic methods with int / numeric on the left hand side


Modified: pkg/RcppBDT/ChangeLog
===================================================================
--- pkg/RcppBDT/ChangeLog	2012-10-25 12:45:01 UTC (rev 3839)
+++ pkg/RcppBDT/ChangeLog	2012-10-26 03:00:49 UTC (rev 3840)
@@ -1,3 +1,11 @@
+2012-10-25  Dirk Eddelbuettel  <edd at debian.org>
+
+	* inst/include/RcppBDTdt.h: A few more methods for dates
+	* src/RcppBDTdt.cpp: Idem
+	* inst/include/RcppBDTdu.h: A few more methods for durations
+	* src/RcppBDTdu.cpp: Idem
+	* R/zzz.R: Dispatching to the new methods
+
 2012-10-24  Dirk Eddelbuettel  <edd at debian.org>
 
 	* inst/include/RcppBDTdt.h: Altered several functions to use

Modified: pkg/RcppBDT/R/zzz.R
===================================================================
--- pkg/RcppBDT/R/zzz.R	2012-10-25 12:45:01 UTC (rev 3839)
+++ pkg/RcppBDT/R/zzz.R	2012-10-26 03:00:49 UTC (rev 3840)
@@ -74,9 +74,13 @@
     setMethod("Arith", signature(e1 = "Rcpp_bdtDu", e2 = "integer" ),
               function(e1, e2) arith_bdtDu_int( e1, e2, .Generic ) )
     setMethod("Arith", signature(e1 = "Rcpp_bdtDu", e2 = "numeric" ),
-              function(e1, e2)  arith_bdtDu_int( e1, as.integer(e2), .Generic ) )
+              function(e1, e2) arith_bdtDu_int( e1, as.integer(e2), .Generic ) )
+    setMethod("Arith", signature(e1 = "integer", e2 = "Rcpp_bdtDu"),
+              function(e1, e2) arith_int_bdtDu( e1, e2, .Generic ) )
+    setMethod("Arith", signature(e1 = "numeric", e2 = "Rcpp_bdtDu"),
+              function(e1, e2) arith_int_bdtDu(as.integer(e1), e2, .Generic ) )
     setMethod("Compare", signature(e1 = "Rcpp_bdtDu", e2 = "Rcpp_bdtDu" ),
-              function(e1, e2) {compare_bdtDu_bdtDu( e1, e2, .Generic )} )
+              function(e1, e2) compare_bdtDu_bdtDu( e1, e2, .Generic ) )
 
     setMethod("Arith", signature(e1 = "Rcpp_bdtPt", e2 = "Rcpp_bdtDu" ),
               function(e1, e2) arith_bdtPt_bdtDu(e1, e2, .Generic ))
@@ -89,11 +93,15 @@
               function(e1, e2) compare_bdtDt_bdtDt(e1, e2, .Generic))
     setMethod("Compare", signature(e1 = "Rcpp_bdtDt", e2 = "Date"),
               function(e1, e2) compare_bdtDt_bdtDt(e1, new(bdtDt, e2), .Generic))
-#    setMethod("Compare", signature(e1 = "Date", e2 = "Rcpp_bdtDt"), ,
-#             function(e1, e2) compare_bdtDt_bdtDt(new(bdtDt, e1), e2, .Generic))
+    setMethod("Compare", signature(e1 = "Date", e2 = "Rcpp_bdtDt"),
+             function(e1, e2) compare_bdtDt_bdtDt(new(bdtDt, e1), e2, .Generic))
     setMethod("Arith", signature(e1 = "Rcpp_bdtDt", e2 = "integer"),
               function(e1, e2) arith_bdtDt_int(e1, e2, .Generic) )
     setMethod("Arith", signature(e1 = "Rcpp_bdtDt", e2 = "numeric"),
               function(e1, e2) arith_bdtDt_int(e1, as.integer(e2), .Generic) )
+    setMethod("Arith", signature(e1 = "integer", e2 = "Rcpp_bdtDt"),
+              function(e1, e2) arith_int_bdtDt(e1, e2, .Generic) )
+    setMethod("Arith", signature(e1 = "numeric", e2 = "Rcpp_bdtDt"),
+              function(e1, e2) arith_int_bdtDt(as.integer(e1), e2, .Generic) )
 
 })

Modified: pkg/RcppBDT/demo/RcppBDTdu.R
===================================================================
--- pkg/RcppBDT/demo/RcppBDTdu.R	2012-10-25 12:45:01 UTC (rev 3839)
+++ pkg/RcppBDT/demo/RcppBDTdu.R	2012-10-26 03:00:49 UTC (rev 3840)
@@ -46,4 +46,3 @@
 print(du + pt)
 
 print(pt + hours(3) + minutes(4))
-

Added: pkg/RcppBDT/demo/RcppBDTpt.R
===================================================================
--- pkg/RcppBDT/demo/RcppBDTpt.R	                        (rev 0)
+++ pkg/RcppBDT/demo/RcppBDTpt.R	2012-10-26 03:00:49 UTC (rev 3840)
@@ -0,0 +1,18 @@
+
+## demo for time objects
+
+require(utils, quiet=TRUE, warn=FALSE)
+require(Rcpp, quiet=TRUE, warn=FALSE)
+require(RcppBDT, quiet=TRUE, warn=FALSE)
+
+## use one of several bdtPt ctors
+pt <- new(bdtPt, ISOdatetime(2012,1,1,00,30,0,tz="UTC"))
+
+du <- new(bdtDu, 0, 1, 2, 123456789)
+
+print(pt + du)
+print(du + pt)
+
+print(pt + hours(3) + minutes(4))
+#print(pt + 5)
+print(5 + pt)

Modified: pkg/RcppBDT/inst/include/RcppBDTdt.h
===================================================================
--- pkg/RcppBDT/inst/include/RcppBDTdt.h	2012-10-25 12:45:01 UTC (rev 3839)
+++ pkg/RcppBDT/inst/include/RcppBDTdt.h	2012-10-26 03:00:49 UTC (rev 3840)
@@ -78,6 +78,7 @@
     boost::gregorian::date m_dt; 			// private Boost date instance
 
     friend bdtDt* arith_bdtDt_int(const bdtDt&, const int&, std::string);
+    friend bdtDt* arith_int_bdtDt(const int&, const bdtDt&, std::string);
     friend bool compare_bdtDt_bdtDt(const bdtDt&, const bdtDt&, std::string);
 };
 

Modified: pkg/RcppBDT/inst/include/RcppBDTdu.h
===================================================================
--- pkg/RcppBDT/inst/include/RcppBDTdu.h	2012-10-25 12:45:01 UTC (rev 3839)
+++ pkg/RcppBDT/inst/include/RcppBDTdu.h	2012-10-26 03:00:49 UTC (rev 3840)
@@ -56,12 +56,13 @@
 private:
     boost::posix_time::time_duration m_td;
 
-    friend bdtDu* arith_bdtDu_bdtDu(const bdtDu&, const bdtDu& , std::string);
-    friend bdtDu* arith_bdtDu_int(const bdtDu&,   int, std::string);
-    friend bool compare_bdtDu_bdtDu(const bdtDu&, const bdtDu&, std::string);
-    friend bdtPt* arith_bdtPt_bdtDu(const bdtPt&, const bdtDu&, std::string);
-    friend bdtPt* arith_bdtDu_bdtPt(const bdtDu&, const bdtPt&, std::string);
-    friend bdtDt* arith_bdtDt_int(const bdtDt&,   const int&, std::string);
+    friend bdtDu* arith_bdtDu_bdtDu(  const bdtDu&, const bdtDu&, std::string);
+    friend bdtDu* arith_bdtDu_int(    const bdtDu&, const int,    std::string);
+    friend bdtDu* arith_int_bdtDu(    const int,    const bdtDu&, std::string);
+    friend bool   compare_bdtDu_bdtDu(const bdtDu&, const bdtDu&, std::string);
+    friend bdtPt* arith_bdtPt_bdtDu(  const bdtPt&, const bdtDu&, std::string);
+    friend bdtPt* arith_bdtDu_bdtPt(  const bdtDu&, const bdtPt&, std::string);
+    friend bdtDt* arith_bdtDt_int(    const bdtDt&, const int&,   std::string);
 };
 
 #endif

Modified: pkg/RcppBDT/src/RcppBDTdt.cpp
===================================================================
--- pkg/RcppBDT/src/RcppBDTdt.cpp	2012-10-25 12:45:01 UTC (rev 3839)
+++ pkg/RcppBDT/src/RcppBDTdt.cpp	2012-10-26 03:00:49 UTC (rev 3840)
@@ -79,6 +79,17 @@
     return new bdtDt(0,0,0);
 }
 
+bdtDt* arith_int_bdtDt(const int& e1, const bdtDt& e2, std::string op) {
+    if( ! op.compare("+") ){
+        return new bdtDt(e2.m_dt + boost::gregorian::date_duration(e1));   
+        //} else if( ! op.compare("-") ){
+        //return new bdtDt( e1.m_dt - boost::gregorian::date_duration(e2) );
+    }
+    Rf_error( "operator not implemented" );
+    // not reached
+    return new bdtDt(0,0,0);
+}
+
 bool compare_bdtDt_bdtDt( const bdtDt& e1, const bdtDt& e2, std::string op ){
     if( !op.compare( "==" ) ){
         return e1.m_dt == e2.m_dt ;   
@@ -149,6 +160,7 @@
         ;
 
     Rcpp::function("arith_bdtDt_int",         &arith_bdtDt_int); 
+    Rcpp::function("arith_int_bdtDt",         &arith_int_bdtDt); 
     Rcpp::function("compare_bdtDt_bdtDt",     &compare_bdtDt_bdtDt);
 }
 

Modified: pkg/RcppBDT/src/RcppBDTdu.cpp
===================================================================
--- pkg/RcppBDT/src/RcppBDTdu.cpp	2012-10-25 12:45:01 UTC (rev 3839)
+++ pkg/RcppBDT/src/RcppBDTdu.cpp	2012-10-26 03:00:49 UTC (rev 3840)
@@ -39,16 +39,14 @@
     return new bdtDu( 0,0,0,0 )  ;
 }
 
-bdtDu* arith_bdtDu_int( const bdtDu& e1, int e2, std::string op ){
+bdtDu* arith_bdtDu_int(const bdtDu& e1, const int e2, std::string op) {
     if( ! op.compare("*") ){
         return new bdtDu( e1.m_td * e2 ) ;   
     } else if( ! op.compare("/") ){
         return new bdtDu( e1.m_td / e2 ) ;
     } else if( ! op.compare("+") ){
-        //Rf_warning("Interpreting int as seconds");
         return new bdtDu(e1.m_td + boost::posix_time::time_duration(0,0,e2,0));
     } else if( ! op.compare("-") ){
-        //Rf_warning("Interpreting int as seconds");
         return new bdtDu(e1.m_td - boost::posix_time::time_duration(0, 0, e2, 0));
     }
     Rf_error( "operator not implemented" )  ;
@@ -56,6 +54,21 @@
     return new bdtDu( 0,0,0,0 )  ;
 }
 
+bdtDu* arith_int_bdtDu(const int e1, const bdtDu& e2, std::string op) {
+    if( ! op.compare("*") ){
+        return new bdtDu( e2.m_td * e1 ) ;   
+    // } else if( ! op.compare("/") ){
+    //     return new bdtDu( e1.m_td / e2 ) ;
+    } else if( ! op.compare("+") ){
+        return new bdtDu(e2.m_td + boost::posix_time::time_duration(0,0,e1,0));
+    // } else if( ! op.compare("-") ){
+    //     return new bdtDu(e1.m_td - boost::posix_time::time_duration(0, 0, e2, 0));
+    }
+    Rf_error( "operator not implemented" )  ;
+    // not reached
+    return new bdtDu( 0,0,0,0 )  ;
+}
+
 bool compare_bdtDu_bdtDu( const bdtDu& e1, const bdtDu& e2, std::string op ){
     if( !op.compare( "==" ) ){
         return e1.m_td == e2.m_td ;   
@@ -110,6 +123,7 @@
     
     Rcpp::function( "arith_bdtDu_bdtDu",        &arith_bdtDu_bdtDu ) ; 
     Rcpp::function( "arith_bdtDu_int",          &arith_bdtDu_int ) ; 
+    Rcpp::function( "arith_int_bdtDu",          &arith_int_bdtDu ) ; 
     Rcpp::function( "compare_bdtDu_bdtDu",      &compare_bdtDu_bdtDu );
 
 }



More information about the Rcpp-commits mailing list