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

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Tue Oct 23 02:56:15 CEST 2012


Author: edd
Date: 2012-10-23 02:56:15 +0200 (Tue, 23 Oct 2012)
New Revision: 3809

Modified:
   pkg/RcppBDT/ChangeLog
   pkg/RcppBDT/R/zzz.R
   pkg/RcppBDT/inst/include/RcppBDT.h
   pkg/RcppBDT/src/RcppBDTdu.cpp
   pkg/RcppBDT/src/RcppBDTpt.cpp
Log:
now durations can be added to posix time too (but need to split RcppBDT.h into several files...)


Modified: pkg/RcppBDT/ChangeLog
===================================================================
--- pkg/RcppBDT/ChangeLog	2012-10-23 00:09:00 UTC (rev 3808)
+++ pkg/RcppBDT/ChangeLog	2012-10-23 00:56:15 UTC (rev 3809)
@@ -1,3 +1,8 @@
+2012-10-22  Dirk Eddelbuettel  <edd at debian.org>
+
+	* src/RcppBDTpt.cpp: Comparison and arithmethic support for posix time
+	* R/zzz.R: associated S4 methods
+
 2012-10-22  Romain Francois  <romain at r-enthusiasts.com>
 
 	* src/RcppBDTdu.cpp: implementing functions hours, minutes, ... and operators

Modified: pkg/RcppBDT/R/zzz.R
===================================================================
--- pkg/RcppBDT/R/zzz.R	2012-10-23 00:09:00 UTC (rev 3808)
+++ pkg/RcppBDT/R/zzz.R	2012-10-23 00:56:15 UTC (rev 3809)
@@ -62,22 +62,24 @@
     setMethod("show", "Rcpp_bdtTz", .show_tz)
     setMethod("show", "Rcpp_bdtPt", .show_pt)
     setMethod("show", "Rcpp_bdtDu", .show_du)
+
     setGeneric("format", function(x,...) standardGeneric("format"))
     setMethod("format", "Rcpp_bdtDt", .format_dt)
     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 )
-    } )
+
+    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 )} )
+
+    setMethod("Arith", signature(e1 = "Rcpp_bdtPt", e2 = "Rcpp_bdtDu" ),
+              function(e1, e2){ arith_bdtPt_bdtDu( e1, e2, .Generic )} )
+    setMethod("Compare", signature(e1 = "Rcpp_bdtPt", e2 = "Rcpp_bdtPt"),
+              function(e1, e2) compare_bdtPt_bdtPt(e1, e2, .Generic))
 })

Modified: pkg/RcppBDT/inst/include/RcppBDT.h
===================================================================
--- pkg/RcppBDT/inst/include/RcppBDT.h	2012-10-23 00:09:00 UTC (rev 3808)
+++ pkg/RcppBDT/inst/include/RcppBDT.h	2012-10-23 00:56:15 UTC (rev 3809)
@@ -69,4 +69,95 @@
 
 #include <Rcpp.h>
 
+
+class bdtDu;
+
+class bdtPt {
+
+public:
+    bdtPt() 				  { setFromLocalTimeInMicroSeconds(); }  	// default empty constructor 
+    bdtPt(SEXP dt) 			  { setFromDatetime(dt); }     			// constructor from R's / Rcpp's Datetime
+    bdtPt(double dt) 			  { setFromDouble(dt); }     			// constructor from double using Rcpp's Datetime
+    bdtPt(int year, int month, int day, 			// constructor from date and duration
+          int hours, int minutes, int seconds, 
+          int fractionalseconds) : m_pt(boost::gregorian::date(year, month, day), 
+                                        boost::posix_time::time_duration(hours, minutes, seconds, fractionalseconds)) { }
+    bdtPt(boost::posix_time::ptime pt) : m_pt(pt) {} 
+    bdtPt(const bdtPt& other) : m_pt(other.m_pt ) {}
+    
+    void setFromLocalTimeInSeconds()      { m_pt = boost::posix_time::second_clock::local_time(); }
+    void setFromUTCInSeconds()            { m_pt = boost::posix_time::second_clock::universal_time(); }
+    void setFromLocalTimeInMicroSeconds() { m_pt = boost::posix_time::microsec_clock::local_time(); }
+    void setFromUTCInMicroSeconds()       { m_pt = boost::posix_time::microsec_clock::universal_time(); }
+                                    
+    void setFromTimeT(const time_t t)     { m_pt = boost::posix_time::from_time_t(t); }
+
+    void setFromDatetime(const SEXP dt)   { m_pt = Rcpp::as<boost::posix_time::ptime>(dt); }
+    void setFromDouble(const double d)   { 
+        Rcpp::Datetime dt(d);
+        m_pt = boost::posix_time::ptime(boost::gregorian::date(dt.getYear(), dt.getMonth(), dt.getDay()), 
+                                        boost::posix_time::time_duration(dt.getHours(), dt.getMinutes(), dt.getSeconds(), dt.getMicroSeconds()/1000.0));
+    }
+
+    Rcpp::Datetime getDatetime() 	  { return Rcpp::wrap(m_pt); }
+
+    Rcpp::Date getDate() { 
+        boost::gregorian::date::ymd_type ymd = m_pt.date().year_month_day();     // convert to date and then to y/m/d struct
+        return Rcpp::Date( ymd.year, ymd.month, ymd.day );
+    }
+
+    void addHours(int h)        	  { m_pt += boost::posix_time::time_duration(h, 0, 0, 0); }
+    void addMinutes(int m)        	  { m_pt += boost::posix_time::time_duration(0, m, 0, 0); }
+    void addSeconds(int s)        	  { m_pt += boost::posix_time::time_duration(0, 0, s, 0); }
+    void addFractionalSeconds(int fs)     { m_pt += boost::posix_time::time_duration(0, 0, 0, fs); }
+
+private:
+    boost::posix_time::ptime m_pt; 		// private ptime instace
+
+    friend Rcpp::object<bdtPt> arith_bdtPt_bdtDu(Rcpp::object<bdtPt>, Rcpp::object<bdtDu>, std::string);
+    friend bool compare_bdtPt_bdtPt(Rcpp::object<bdtPt>, Rcpp::object<bdtPt>, std::string);
+};
+
+class bdtDu {
+
+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()); }
+    long getTotalSeconds()      { return(m_td.total_seconds()); }
+    long getTotalMilliSeconds() { return(m_td.total_milliseconds()); }
+    long getTotalMicroSeconds() { return(m_td.total_microseconds()); }
+    long getTotalNanoSeconds()  { return(m_td.total_nanoseconds()); }
+    long getFractionalSeconds() { return(m_td.fractional_seconds()); }
+    unsigned short getNumFractionalDigits() { return(m_td.num_fractional_digits()); }
+    long getTicksPerSecond()    { return(m_td.ticks_per_second()); }
+
+    void addHours(int h)        { m_td += boost::posix_time::hours(h); }
+    void addMinutes(int m)      { m_td += boost::posix_time::minutes(m); }
+    void addSeconds(int s)      { m_td += boost::posix_time::seconds(s); }
+    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:
+    boost::posix_time::time_duration m_td;
+
+    friend Rcpp::object<bdtDu> arith_bdtDu_bdtDu(Rcpp::object<bdtDu>, Rcpp::object<bdtDu>, std::string);
+    friend Rcpp::object<bdtDu> arith_bdtDu_int(Rcpp::object<bdtDu>, int, std::string);
+    friend bool compare_bdtDu_bdtDu(Rcpp::object<bdtDu>, Rcpp::object<bdtDu>, std::string);
+
+    friend Rcpp::object<bdtPt> arith_bdtPt_bdtDu(Rcpp::object<bdtPt>, Rcpp::object<bdtDu>, std::string);
+};
+
+
 #endif

Modified: pkg/RcppBDT/src/RcppBDTdu.cpp
===================================================================
--- pkg/RcppBDT/src/RcppBDTdu.cpp	2012-10-23 00:09:00 UTC (rev 3808)
+++ pkg/RcppBDT/src/RcppBDTdu.cpp	2012-10-23 00:56:15 UTC (rev 3809)
@@ -21,45 +21,6 @@
 
 #include <RcppBDT.h>
 
-class bdtDu {
-
-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()); }
-    long getTotalSeconds()      { return(m_td.total_seconds()); }
-    long getTotalMilliSeconds() { return(m_td.total_milliseconds()); }
-    long getTotalMicroSeconds() { return(m_td.total_microseconds()); }
-    long getTotalNanoSeconds()  { return(m_td.total_nanoseconds()); }
-    long getFractionalSeconds() { return(m_td.fractional_seconds()); }
-    unsigned short getNumFractionalDigits() { return(m_td.num_fractional_digits()); }
-    long getTicksPerSecond()    { return(m_td.ticks_per_second()); }
-
-    void addHours(int h)        { m_td += boost::posix_time::hours(h); }
-    void addMinutes(int m)      { m_td += boost::posix_time::minutes(m); }
-    void addSeconds(int s)      { m_td += boost::posix_time::seconds(s); }
-    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:
-    boost::posix_time::time_duration m_td;
-
-    friend Rcpp::object<bdtDu> arith_bdtDu_bdtDu(Rcpp::object<bdtDu>, Rcpp::object<bdtDu>, std::string);
-    friend Rcpp::object<bdtDu> arith_bdtDu_int(Rcpp::object<bdtDu>, int, std::string);
-    friend bool compare_bdtDu_bdtDu(Rcpp::object<bdtDu>, Rcpp::object<bdtDu>, std::string);
-};
-
 //using namespace Rcpp ;
 
 Rcpp::object<bdtDu> hours(int h)		{ return new bdtDu( boost::posix_time::hours(h) ); }

Modified: pkg/RcppBDT/src/RcppBDTpt.cpp
===================================================================
--- pkg/RcppBDT/src/RcppBDTpt.cpp	2012-10-23 00:09:00 UTC (rev 3808)
+++ pkg/RcppBDT/src/RcppBDTpt.cpp	2012-10-23 00:56:15 UTC (rev 3809)
@@ -38,48 +38,36 @@
     }
 }
 
-class bdtPt {
-
-public:
-    bdtPt() 				  { setFromLocalTimeInMicroSeconds(); }  	// default empty constructor 
-    bdtPt(SEXP dt) 			  { setFromDatetime(dt); }     			// constructor from R's / Rcpp's Datetime
-    bdtPt(double dt) 			  { setFromDouble(dt); }     			// constructor from double using Rcpp's Datetime
-    bdtPt(int year, int month, int day, 			// constructor from date and duration
-          int hours, int minutes, int seconds, 
-          int fractionalseconds) : m_pt(boost::gregorian::date(year, month, day), 
-                                        boost::posix_time::time_duration(hours, minutes, seconds, fractionalseconds)) { }
-    
-    void setFromLocalTimeInSeconds()      { m_pt = boost::posix_time::second_clock::local_time(); }
-    void setFromUTCInSeconds()            { m_pt = boost::posix_time::second_clock::universal_time(); }
-    void setFromLocalTimeInMicroSeconds() { m_pt = boost::posix_time::microsec_clock::local_time(); }
-    void setFromUTCInMicroSeconds()       { m_pt = boost::posix_time::microsec_clock::universal_time(); }
-                                    
-    void setFromTimeT(const time_t t)     { m_pt = boost::posix_time::from_time_t(t); }
-
-    void setFromDatetime(const SEXP dt)   { m_pt = Rcpp::as<boost::posix_time::ptime>(dt); }
-    void setFromDouble(const double d)   { 
-        Rcpp::Datetime dt(d);
-        m_pt = boost::posix_time::ptime(boost::gregorian::date(dt.getYear(), dt.getMonth(), dt.getDay()), 
-                                        boost::posix_time::time_duration(dt.getHours(), dt.getMinutes(), dt.getSeconds(), dt.getMicroSeconds()/1000.0));
+Rcpp::object<bdtPt> arith_bdtPt_bdtDu(Rcpp::object<bdtPt> e1, Rcpp::object<bdtDu> e2, std::string op){
+    if( ! op.compare("+") ){
+        return new bdtPt( e1->m_pt + e2->m_td ) ;   
+    } else if( ! op.compare("-") ){
+        return new bdtPt( e1->m_pt - e2->m_td ) ;
     }
+    Rf_error( "operator not implemented" )  ;
+    // not reached
+    return new bdtPt();
+}
 
-    Rcpp::Datetime getDatetime() 	  { return Rcpp::wrap(m_pt); }
-
-    Rcpp::Date getDate() { 
-        boost::gregorian::date::ymd_type ymd = m_pt.date().year_month_day();     // convert to date and then to y/m/d struct
-        return Rcpp::Date( ymd.year, ymd.month, ymd.day );
+bool compare_bdtPt_bdtPt(Rcpp::object<bdtPt> e1, Rcpp::object<bdtPt> e2, std::string op) {
+    if( !op.compare( "==" ) ){
+        return e1->m_pt == e2->m_pt ;   
+    } else if( !op.compare( "!=" ) ){
+        return e1->m_pt != e2->m_pt ;
+    } else if( !op.compare( ">" ) ){
+        return e1->m_pt > e2->m_pt ;
+    } else if( !op.compare( "<" ) ){
+        return e1->m_pt < e2->m_pt ;
+    } else if( !op.compare( ">=" ) ){
+        return e1->m_pt >= e2->m_pt ;
+    } else if( !op.compare( "<=" ) ){
+        return e1->m_pt <= e2->m_pt ;
     }
+    Rf_error("unknown operator in bdtPt comparison");
+    return R_NilValue ;
+}
 
-    void addHours(int h)        	  { m_pt += boost::posix_time::time_duration(h, 0, 0, 0); }
-    void addMinutes(int m)        	  { m_pt += boost::posix_time::time_duration(0, m, 0, 0); }
-    void addSeconds(int s)        	  { m_pt += boost::posix_time::time_duration(0, 0, s, 0); }
-    void addFractionalSeconds(int fs)     { m_pt += boost::posix_time::time_duration(0, 0, 0, fs); }
 
-private:
-    boost::posix_time::ptime m_pt; 		// private ptime instace
-
-};
-
 RCPP_MODULE(bdtPtMod) {
     Rcpp::class_<bdtPt>("bdtPt")   
 	
@@ -106,4 +94,7 @@
         .method("addFractionalSeconds",           &bdtPt::addFractionalSeconds,           "add given fractional seconds to posix time object")
 
     ;
+
+    function("arith_bdtPt_bdtDu", 	&arith_bdtPt_bdtDu); 
+    function("compare_bdtPt_bdtPt",	&compare_bdtPt_bdtPt);
 }



More information about the Rcpp-commits mailing list