[Rcpp-commits] r3805 - in pkg/RcppBDT: . R src

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Mon Oct 22 11:41:42 CEST 2012


Author: romain
Date: 2012-10-22 11:41:42 +0200 (Mon, 22 Oct 2012)
New Revision: 3805

Modified:
   pkg/RcppBDT/ChangeLog
   pkg/RcppBDT/DESCRIPTION
   pkg/RcppBDT/R/zzz.R
   pkg/RcppBDT/src/RcppBDTdu.cpp
Log:
hours, minutes, ... and operators to work with bdtDu objects

Modified: pkg/RcppBDT/ChangeLog
===================================================================
--- pkg/RcppBDT/ChangeLog	2012-10-22 08:45:54 UTC (rev 3804)
+++ pkg/RcppBDT/ChangeLog	2012-10-22 09:41:42 UTC (rev 3805)
@@ -1,3 +1,9 @@
+2012-10-22  Romain Francois  <romain at r-enthusiasts.com>
+
+	* src/RcppBDTdu.cpp: implementing functions hours, minutes, ... and operators
+	+,-(du, du) *,/(du,int)
+	* R/zzz.R: associated S4 methods
+
 2012-10-21  Dirk Eddelbuettel  <edd at debian.org>
 
 	* src/RcppBDTdu.cpp: Added method to add posix time, and return the

Modified: pkg/RcppBDT/DESCRIPTION
===================================================================
--- pkg/RcppBDT/DESCRIPTION	2012-10-22 08:45:54 UTC (rev 3804)
+++ pkg/RcppBDT/DESCRIPTION	2012-10-22 09:41:42 UTC (rev 3805)
@@ -14,7 +14,7 @@
  provide supplementary date to/from strings conversion functions. 
 License: GPL (>= 2)
 LazyLoad: yes
-Depends: Rcpp (>= 0.9.13), methods
+Depends: Rcpp (>= 0.9.15.2), methods
 Imports: methods, Rcpp
 LinkingTo: Rcpp, BoostHeaders
 SystemRequirements: Boost (or the BoostHeaders package)

Modified: pkg/RcppBDT/R/zzz.R
===================================================================
--- pkg/RcppBDT/R/zzz.R	2012-10-22 08:45:54 UTC (rev 3804)
+++ pkg/RcppBDT/R/zzz.R	2012-10-22 09:41:42 UTC (rev 3805)
@@ -67,4 +67,17 @@
     setMethod("format", "Rcpp_bdtTz", .format_tz)
     setMethod("format", "Rcpp_bdtPt", .format_pt)
     setMethod("format", "Rcpp_bdtDu", .format_du)
+    
+    setMethod("Arith", signature(e1 = "Rcpp_bdtDu", e2 = "Rcpp_bdtDu" ), function(e1, e2){
+        arith_bdtDu_bdtDu( e1, e2, .Generic )
+    } )
+    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 )
+    } )
+    setMethod("Compare", signature(e1 = "Rcpp_bdtDu", e2 = "Rcpp_bdtDu" ), function(e1, e2){
+        compare_bdtDu_bdtDu( e1, e2, .Generic )
+    } )
 })

Modified: pkg/RcppBDT/src/RcppBDTdu.cpp
===================================================================
--- pkg/RcppBDT/src/RcppBDTdu.cpp	2012-10-22 08:45:54 UTC (rev 3804)
+++ pkg/RcppBDT/src/RcppBDTdu.cpp	2012-10-22 09:41:42 UTC (rev 3805)
@@ -26,7 +26,9 @@
 public:
 
     bdtDu(int hours, int minutes, int seconds, int fractionalseconds) : m_td(hours, minutes, seconds, fractionalseconds) { }
-
+    bdtDu(boost::posix_time::time_duration td) : m_td(td){} 
+    bdtDu(const bdtDu& other) : m_td(other.m_td){}
+    
     long getHours()             { return(m_td.hours()); }
     long getMinutes()           { return(m_td.minutes()); }
     long getSeconds()           { return(m_td.seconds()); }
@@ -44,20 +46,81 @@
     void addMilliSeconds(int s) { m_td += boost::posix_time::milliseconds(s); }
     void addMicroSeconds(int s) { m_td += boost::posix_time::microseconds(s); }
     void addNanoSeconds(int s)  { m_td += boost::posix_time::nanoseconds(s); }
-
     Rcpp::Datetime getAddedPosixtime(SEXP ptsexp)  { 
         boost::posix_time::ptime pt(Rcpp::as<boost::posix_time::ptime>(ptsexp)); 
         pt += m_td;
         return Rcpp::wrap(pt); 
     }
             
-private:
+// [RF] easier for me to have this public
+// private:
     boost::posix_time::time_duration m_td;
             
 };
 
+using namespace Rcpp ;
+
+object<bdtDu> hours(int h){
+    return new bdtDu( boost::posix_time::hours(h) ) ;   
+}
+object<bdtDu> minutes(int m){
+    return new bdtDu( boost::posix_time::minutes(m)) ;   
+}
+object<bdtDu> seconds(int s){
+    return new bdtDu( boost::posix_time::seconds(s)) ;   
+}
+object<bdtDu> milliseconds(int ms){
+    return new bdtDu( boost::posix_time::milliseconds(ms)) ;   
+}
+object<bdtDu> microseconds(int ms){
+    return new bdtDu( boost::posix_time::microseconds(ms)) ;   
+}
+object<bdtDu> nanoseconds(int ms){
+    return new bdtDu( boost::posix_time::nanoseconds(ms)) ;   
+}
+  
+object<bdtDu> arith_bdtDu_bdtDu( object<bdtDu> e1, object<bdtDu> e2, std::string op ){
+    if( ! op.compare("+") ){
+        return new bdtDu( e1->m_td + e2->m_td ) ;   
+    } else if( ! op.compare("-") ){
+        return new bdtDu( e1->m_td - e2->m_td ) ;
+    }
+    Rf_error( "operator not implemented" )  ;
+    // not reached
+    return new bdtDu( 0,0,0,0 )  ;
+}
+
+object<bdtDu> arith_bdtDu_int( object<bdtDu> e1, 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 ) ;
+    }
+    Rf_error( "operator not implemented" )  ;
+    // not reached
+    return new bdtDu( 0,0,0,0 )  ;
+}
+
+bool compare_bdtDu_bdtDu( object<bdtDu> e1, object<bdtDu> e2, std::string op ){
+    if( !op.compare( "==" ) ){
+        return e1->m_td == e2->m_td ;   
+    } else if( !op.compare( "!=" ) ){
+        return e1->m_td != e2->m_td ;
+    } else if( !op.compare( ">" ) ){
+        return e1->m_td > e2->m_td ;
+    } else if( !op.compare( "<" ) ){
+        return e1->m_td < e2->m_td ;
+    } else if( !op.compare( ">=" ) ){
+        return e1->m_td >= e2->m_td ;
+    } else if( !op.compare( "<=" ) ){
+        return e1->m_td <= e2->m_td ;
+    }
+    Rf_error( "unknown operator" ) ;
+    return R_NilValue ;
+}
+
 RCPP_MODULE(bdtDuMod) {
-    Rcpp::class_<bdtDu>("bdtDu")   
+    class_<bdtDu>("bdtDu")   
 	
         .constructor<int,int,int,int>("constructor with hours, minutes, seconds and fractional_seconds")  
 
@@ -79,8 +142,18 @@
         .method("addMilliSeconds",        &bdtDu::addMilliSeconds,        "add given milliseconds to duration object")
         .method("addMicroSeconds",        &bdtDu::addMicroSeconds,        "add given microseconds to duration object")
         .method("addNanoSeconds",         &bdtDu::addNanoSeconds,         "add given nanoseconds to duration object")
-
         .method("getAddedPosixtime",      &bdtDu::getAddedPosixtime,	  "adds duration to given posix time and returns posix time")
-
     ;
+    
+    function( "hours", &hours ) ; 
+    function( "minutes", &minutes ) ; 
+    function( "seconds", &seconds ) ; 
+    function( "milliseconds", &milliseconds ) ; 
+    function( "microseconds", &microseconds ) ;
+    function( "nanoseconds", &nanoseconds ) ; 
+    
+    function( "arith_bdtDu_bdtDu", &arith_bdtDu_bdtDu ) ; 
+    function( "arith_bdtDu_int", &arith_bdtDu_int ) ; 
+    function( "compare_bdtDu_bdtDu", &compare_bdtDu_bdtDu ) ;
 }
+



More information about the Rcpp-commits mailing list