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

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Sat Nov 3 19:41:35 CET 2012


Author: edd
Date: 2012-11-03 19:41:35 +0100 (Sat, 03 Nov 2012)
New Revision: 3891

Modified:
   pkg/RcppBDT/ChangeLog
   pkg/RcppBDT/R/zzz.R
   pkg/RcppBDT/demo/RcppBDTdd.R
   pkg/RcppBDT/demo/RcppBDTpt.R
   pkg/RcppBDT/inst/include/RcppBDTpt.h
   pkg/RcppBDT/src/RcppBDTpt.cpp
Log:
added addition of seconds to pt objects


Modified: pkg/RcppBDT/ChangeLog
===================================================================
--- pkg/RcppBDT/ChangeLog	2012-11-03 07:58:25 UTC (rev 3890)
+++ pkg/RcppBDT/ChangeLog	2012-11-03 18:41:35 UTC (rev 3891)
@@ -1,3 +1,8 @@
+2012-11-03  Dirk Eddelbuettel  <edd at debian.org>
+
+	* src/RcppBDTpt.cpp: Added support for adding seconds (converted to
+	duration) to pt objects
+
 2012-10-31  Dirk Eddelbuettel  <edd at debian.org>
 
 	* R/bdt.R: switch from fromDate() to setDate()
@@ -4,7 +9,7 @@
 
 	* inst/include/RcppBDTdt.h: addMonth(), addYears() added which can't
 	be done via date_duration as they need a date for context
-	* src/RcppBDTdt.cpp: idem 
+	* src/RcppBDTdt.cpp: idem
 
 	* src/RcppBDTdd.cpp: added weeks()
 

Modified: pkg/RcppBDT/R/zzz.R
===================================================================
--- pkg/RcppBDT/R/zzz.R	2012-11-03 07:58:25 UTC (rev 3890)
+++ pkg/RcppBDT/R/zzz.R	2012-11-03 18:41:35 UTC (rev 3891)
@@ -79,6 +79,10 @@
               function(e1, e2) arith_bdtDu_bdtPt(e1, e2, .Generic ))
     setMethod("Compare", signature(e1 = "Rcpp_bdtPt", e2 = "Rcpp_bdtPt"),
               function(e1, e2) compare_bdtPt_bdtPt(e1, e2, .Generic))
+    setMethod("Arith", signature(e1 = "Rcpp_bdtPt", e2 = "numeric" ),
+              function(e1, e2) arith_bdtPt_double(e1, e2, .Generic ))
+    setMethod("Arith", signature(e1 = "numeric", e2 = "Rcpp_bdtPt"),
+              function(e1, e2) arith_double_bdtPt(e1, e2, .Generic ))
 
     setMethod("Compare", signature(e1 = "Rcpp_bdtDt", e2 = "Rcpp_bdtDt"),
               function(e1, e2) compare_bdtDt_bdtDt(e1, e2, .Generic))

Modified: pkg/RcppBDT/demo/RcppBDTdd.R
===================================================================
--- pkg/RcppBDT/demo/RcppBDTdd.R	2012-11-03 07:58:25 UTC (rev 3890)
+++ pkg/RcppBDT/demo/RcppBDTdd.R	2012-11-03 18:41:35 UTC (rev 3891)
@@ -5,21 +5,25 @@
 require(Rcpp, quiet=TRUE, warn=FALSE)
 require(RcppBDT, quiet=TRUE, warn=FALSE)
 
+cat("\n(1) Create a bdtDd object and print it -- as a difftime()\n")
 dd <- new(bdtDd, 5)
 print(dd)  ## converts to difftime
 
+cat("\n(2) Simple arithmetic, adding btdDd object and adding/subtracing integers\n")
 print(dd + dd)                          # date_duration + date_duration
 print(dd + 3)
 print(dd - 2)
 #print(2L + dd)				# should work
 ##print(2 - dd)				# not permitted
 
+cat("\n(3) Creating a bdtDt date object and adding the bdtDd object\n")
 dt <- new(bdtDt, 2012, 10, 10)
 print(dt)
 #print(dd + dt)
 print(dt + dd)
 #print(dt - dd)
 
+cat("\n(4) Adding to the bdtDt date object\n")
 print(dt + dd + days(3) + weeks(2))
 print(dt)
 dt$addMonths(2)

Modified: pkg/RcppBDT/demo/RcppBDTpt.R
===================================================================
--- pkg/RcppBDT/demo/RcppBDTpt.R	2012-11-03 07:58:25 UTC (rev 3890)
+++ pkg/RcppBDT/demo/RcppBDTpt.R	2012-11-03 18:41:35 UTC (rev 3891)
@@ -5,6 +5,8 @@
 require(Rcpp, quiet=TRUE, warn=FALSE)
 require(RcppBDT, quiet=TRUE, warn=FALSE)
 
+op <- options("digits.secs"=6)          # to make sure R display microseconds
+
 ## use one of several bdtPt ctors
 pt <- new(bdtPt, ISOdatetime(2012,1,1,00,30,0,tz="UTC"))
 
@@ -14,5 +16,13 @@
 print(du + pt)
 
 print(pt + hours(3) + minutes(4))
-#print(pt + 5)
-print(5 + pt)
+print(pt + 5.5)
+print(pt + 5.005)
+print(pt + 5.005005)
+print(pt + 5.005005005)
+print(5.5 + pt)
+
+print(pt + c(5.5, 5.5005))
+
+options(op)
+

Modified: pkg/RcppBDT/inst/include/RcppBDTpt.h
===================================================================
--- pkg/RcppBDT/inst/include/RcppBDTpt.h	2012-11-03 07:58:25 UTC (rev 3890)
+++ pkg/RcppBDT/inst/include/RcppBDTpt.h	2012-11-03 18:41:35 UTC (rev 3891)
@@ -66,9 +66,11 @@
 private:
     boost::posix_time::ptime m_pt; 		// private ptime instace
 
-    friend bdtPt* arith_bdtPt_bdtDu(const bdtPt&, const bdtDu&, std::string);
-    friend bdtPt* arith_bdtDu_bdtPt(const bdtDu&, const bdtPt&, std::string);
-    friend bool compare_bdtPt_bdtPt(const bdtPt&, const bdtPt&, 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 bool   compare_bdtPt_bdtPt(const bdtPt&, const bdtPt&,  std::string);
+    friend bdtPt* arith_bdtPt_double(const bdtPt&,  const double&, std::string);
+    friend bdtPt* arith_double_bdtPt(const double&, const bdtPt&,  std::string);
 };
 
 #endif

Modified: pkg/RcppBDT/src/RcppBDTpt.cpp
===================================================================
--- pkg/RcppBDT/src/RcppBDTpt.cpp	2012-11-03 07:58:25 UTC (rev 3890)
+++ pkg/RcppBDT/src/RcppBDTpt.cpp	2012-11-03 18:41:35 UTC (rev 3891)
@@ -77,7 +77,36 @@
     return R_NilValue ;
 }
 
+bdtPt* arith_bdtPt_double(const bdtPt& e1, const double& d, std::string op){
+    int secs = static_cast<int>(d);
+    int fracs = (d - secs) *  boost::posix_time::time_duration::ticks_per_second();
+    //REprintf("%f -> %d %d\n", d, secs, fracs);
+    boost::posix_time::time_duration td(0, 0, secs, fracs);
+    if( ! op.compare("+") ){
+        return new bdtPt(e1.m_pt + td);   
+    } else if( ! op.compare("-") ) {
+        return new bdtPt(e1.m_pt - td);
+    }
+    Rf_error("operator not implemented");
+    // not reached
+    return new bdtPt();
+}
 
+bdtPt* arith_double_bdtPt(const double& d, const bdtPt& e1, std::string op){
+    int secs = static_cast<int>(d);
+    int fracs = (d - secs) *  boost::posix_time::time_duration::ticks_per_second();
+    boost::posix_time::time_duration td(0, 0, secs, fracs);
+    if( ! op.compare("+") ){
+        return new bdtPt(e1.m_pt + td);   
+    } else if( ! op.compare("-") ) {
+        return new bdtPt(e1.m_pt - td);
+    }
+    Rf_error("operator not implemented");
+    // not reached
+    return new bdtPt();
+}
+
+
 RCPP_MODULE(bdtPtMod) {
     Rcpp::class_<bdtPt>("bdtPt")   
 	
@@ -108,5 +137,7 @@
     Rcpp::function("arith_bdtPt_bdtDu", 	&arith_bdtPt_bdtDu); 
     Rcpp::function("arith_bdtDu_bdtPt", 	&arith_bdtDu_bdtPt); 
     Rcpp::function("compare_bdtPt_bdtPt",	&compare_bdtPt_bdtPt);
+    Rcpp::function("arith_bdtPt_double", 	&arith_bdtPt_double); 
+    Rcpp::function("arith_double_bdtPt", 	&arith_double_bdtPt); 
 }
 



More information about the Rcpp-commits mailing list