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

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Sat Oct 27 02:42:00 CEST 2012


Author: edd
Date: 2012-10-27 02:42:00 +0200 (Sat, 27 Oct 2012)
New Revision: 3844

Added:
   pkg/RcppBDT/inst/include/RcppBDTdd.h
   pkg/RcppBDT/src/RcppBDTdd.cpp
Modified:
   pkg/RcppBDT/ChangeLog
   pkg/RcppBDT/R/zzz.R
   pkg/RcppBDT/inst/NEWS.Rd
   pkg/RcppBDT/inst/include/RcppBDT.h
   pkg/RcppBDT/inst/include/RcppBDTdt.h
   pkg/RcppBDT/src/RcppBDTdt.cpp
Log:
new clas bdtDd for date_duration


Modified: pkg/RcppBDT/ChangeLog
===================================================================
--- pkg/RcppBDT/ChangeLog	2012-10-26 19:42:05 UTC (rev 3843)
+++ pkg/RcppBDT/ChangeLog	2012-10-27 00:42:00 UTC (rev 3844)
@@ -1,3 +1,9 @@
+2012-10-26  Dirk Eddelbuettel  <edd at dexter>
+
+	* inst/include/RcppBDTdd.h: New class date_duration
+	* src/RcppBDTdd.cpp: Implementation
+	* R/zzz.R: Dispatching new methods for new class
+
 2012-10-25  Dirk Eddelbuettel  <edd at debian.org>
 
 	* inst/include/RcppBDTdt.h: A few more methods for dates

Modified: pkg/RcppBDT/R/zzz.R
===================================================================
--- pkg/RcppBDT/R/zzz.R	2012-10-26 19:42:05 UTC (rev 3843)
+++ pkg/RcppBDT/R/zzz.R	2012-10-27 00:42:00 UTC (rev 3844)
@@ -30,10 +30,11 @@
 ## }
 
 
+loadModule("bdtDdMod", TRUE)
 loadModule("bdtDtMod", TRUE)
-loadModule("bdtTzMod", TRUE)
 loadModule("bdtDuMod", TRUE)
 loadModule("bdtPtMod", TRUE)
+loadModule("bdtTzMod", TRUE)
 
 ## create a variable 'bdt' from the bdtDt Module (formerly: bdtMod)
 ## this variable is used as a package-global instance in some R access function
@@ -43,6 +44,9 @@
     x
 }) )
 
+.format_dd <- function(x, ...) format(as.difftime( x$getDays(), units="days"))
+.show_dd   <- function(object) print(as.difftime( object$getDays(), units="days"))
+
 .format_dt <- function(x, ...) format(x$getDate(), ...)
 .show_dt   <- function(object) print(object$getDate())
 
@@ -62,12 +66,14 @@
     setMethod("show", "Rcpp_bdtTz", .show_tz)
     setMethod("show", "Rcpp_bdtPt", .show_pt)
     setMethod("show", "Rcpp_bdtDu", .show_du)
+    setMethod("show", "Rcpp_bdtDd", .show_dd)
 
     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("format", "Rcpp_bdtDd", .format_dd)
 
     setMethod("Arith", signature(e1 = "Rcpp_bdtDu", e2 = "Rcpp_bdtDu" ),
               function(e1, e2) arith_bdtDu_bdtDu( e1, e2, .Generic ) )
@@ -104,4 +110,26 @@
     setMethod("Arith", signature(e1 = "numeric", e2 = "Rcpp_bdtDt"),
               function(e1, e2) arith_int_bdtDt(as.integer(e1), e2, .Generic) )
 
-})
+    setMethod("Arith", signature(e1 = "Rcpp_bdtDd", e2 = "Rcpp_bdtDd" ),
+              function(e1, e2) arith_bdtDd_bdtDd( e1, e2, .Generic ) )
+    setMethod("Arith", signature(e1 = "Rcpp_bdtDd", e2 = "integer" ),
+              function(e1, e2) arith_bdtDd_int( e1, e2, .Generic ) )
+    setMethod("Arith", signature(e1 = "Rcpp_bdtDd", e2 = "numeric" ),
+              function(e1, e2) arith_bdtDd_int( e1, as.integer(e2), .Generic ) )
+    setMethod("Arith", signature(e1 = "integer", e2 = "Rcpp_bdtDd"),
+              function(e1, e2) arith_int_bdtDd( e1, e2, .Generic ) )
+    setMethod("Arith", signature(e1 = "numeric", e2 = "Rcpp_bdtDd"),
+              function(e1, e2) arith_int_bdtDd(as.integer(e1), e2, .Generic ) )
+    setMethod("Compare", signature(e1 = "Rcpp_bdtDd", e2 = "Rcpp_bdtDd" ),
+              function(e1, e2) compare_bdtDd_bdtDd( e1, e2, .Generic ) )
+    setMethod("Arith", signature(e1 = "Rcpp_bdtDd", e2 = "Rcpp_bdtDt" ),
+              function(e1, e2) arith_bdtDd_bdtDt( e1, e2, .Generic ) )
+    setMethod("Arith", signature(e1 = "Rcpp_bdtDt", e2 = "Rcpp_bdtDd" ),
+              function(e1, e2) arith_bdtDt_bdtDd( e1, e2, .Generic ) )
+  })
+
+
+
+
+
+

Modified: pkg/RcppBDT/inst/NEWS.Rd
===================================================================
--- pkg/RcppBDT/inst/NEWS.Rd	2012-10-26 19:42:05 UTC (rev 3843)
+++ pkg/RcppBDT/inst/NEWS.Rd	2012-10-27 00:42:00 UTC (rev 3844)
@@ -7,6 +7,8 @@
     \item New module 'bdtDt' replacing the old 'bdtDate' module in a
     more transparent stule using a local class which is wrapped, just like
     the three other new classes do
+    \item New module 'bdtTd' providing date durations which can be added
+    to dates.
     \item New module 'bdtTz' providing time zone information such as offset to
     UTC, amount of DST, abbreviated and full timezone names.
     \item New module 'bdtDu' using 'posix_time::duration' for time durations types

Modified: pkg/RcppBDT/inst/include/RcppBDT.h
===================================================================
--- pkg/RcppBDT/inst/include/RcppBDT.h	2012-10-26 19:42:05 UTC (rev 3843)
+++ pkg/RcppBDT/inst/include/RcppBDT.h	2012-10-27 00:42:00 UTC (rev 3844)
@@ -25,9 +25,11 @@
 #include <RcppCommon.h>
 
 // forward declarations and helping module classes 
+class bdtDd;
 class bdtDt;
 class bdtDu;
 class bdtPt;
+RCPP_EXPOSED_CLASS(bdtDd)
 RCPP_EXPOSED_CLASS(bdtDu)
 RCPP_EXPOSED_CLASS(bdtDt)
 RCPP_EXPOSED_CLASS(bdtPt)
@@ -78,6 +80,7 @@
 
 #include <Rcpp.h>
 
+#include <RcppBDTdd.h>
 #include <RcppBDTdt.h>
 #include <RcppBDTpt.h>
 #include <RcppBDTdu.h>

Added: pkg/RcppBDT/inst/include/RcppBDTdd.h
===================================================================
--- pkg/RcppBDT/inst/include/RcppBDTdd.h	                        (rev 0)
+++ pkg/RcppBDT/inst/include/RcppBDTdd.h	2012-10-27 00:42:00 UTC (rev 3844)
@@ -0,0 +1,47 @@
+// -*- mode: C++; c-indent-level: 4; c-basic-offset: 4; indent-tabs-mode: nil; -*-
+//
+// RcppBDTdd.h: Rcpp and Boost Date_Time date duration ("days") class 
+//
+// Copyright (C) 2012  Dirk Eddelbuettel and Romain Francois
+//
+// This file is part of RcppBDT.
+//
+// RcppBDT 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.
+//                           
+// RcppBDT 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 RcppBDT.  If not, see <http://www.gnu.org/licenses/>.
+
+#ifndef RCPPBDT__RCPPBDTDD_H
+#define RCPPBDT__RCPPBDTDD_H
+
+class bdtDd {
+
+public:
+    bdtDd() : m_dd(0)			 {} 				// default empty constructor, zero length 
+    //bdtDd(SEXP dd) 			 { m_dd = Rcpp::as<int>(dd); } 	// constructor from SEXP using int
+    bdtDd(long dd) : m_dd(dd)		 {} 	// constructor from day count
+    bdtDd(boost::gregorian::date_duration dd) : m_dd(dd) {} 
+    bdtDd(const bdtDd& other) : m_dd(other.m_dd) {}
+
+    long getDays()             { return m_dd.days(); }
+
+private:
+    boost::gregorian::date_duration m_dd; 				// private Boost date duration instance
+
+    friend bdtDd* arith_bdtDd_bdtDd(  const bdtDd&, const bdtDd&, std::string);
+    friend bdtDd* arith_bdtDd_int(    const bdtDd&, const int&,   std::string);
+    friend bdtDd* arith_int_bdtDd(    const int&,   const bdtDd&, std::string);
+    friend bool   compare_bdtDd_bdtDd(const bdtDd&, const bdtDd&, std::string);
+    friend bdtDt* arith_bdtDd_bdtDt(  const bdtDd&, const bdtDt&, std::string);
+    friend bdtDt* arith_bdtDt_bdtDd(  const bdtDt&, const bdtDd&, std::string);
+};
+
+#endif

Modified: pkg/RcppBDT/inst/include/RcppBDTdt.h
===================================================================
--- pkg/RcppBDT/inst/include/RcppBDTdt.h	2012-10-26 19:42:05 UTC (rev 3843)
+++ pkg/RcppBDT/inst/include/RcppBDTdt.h	2012-10-27 00:42:00 UTC (rev 3844)
@@ -80,6 +80,8 @@
     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);
+    friend bdtDt* arith_bdtDd_bdtDt(const bdtDd&, const bdtDt&, std::string);
+    friend bdtDt* arith_bdtDt_bdtDd(const bdtDt&, const bdtDd&, std::string);
 };
 
 #endif

Added: pkg/RcppBDT/src/RcppBDTdd.cpp
===================================================================
--- pkg/RcppBDT/src/RcppBDTdd.cpp	                        (rev 0)
+++ pkg/RcppBDT/src/RcppBDTdd.cpp	2012-10-27 00:42:00 UTC (rev 3844)
@@ -0,0 +1,89 @@
+// -*- mode: C++; c-indent-level: 4; c-basic-offset: 4; indent-tabs-mode: nil; -*-
+//
+// RcppBDTdd.cpp: Rcpp and Boost Date_Time date duration ("days") class 
+//
+// Copyright (C) 2012  Dirk Eddelbuettel and Romain Francois
+//
+// This file is part of RcppBDT.
+//
+// RcppBDT 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.
+//                           
+// RcppBDT 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 RcppBDT.  If not, see <http://www.gnu.org/licenses/>.
+
+#include <RcppBDT.h>
+
+bdtDd* days(int d)		{ return new bdtDd(boost::gregorian::date_duration(d)); }
+
+bdtDd* arith_bdtDd_bdtDd( const bdtDd& e1, const bdtDd& e2, std::string op) {
+    if( ! op.compare("+") ){
+        return new bdtDd(e1.m_dd + e2.m_dd );   
+    } else if( ! op.compare("-") ){
+        return new bdtDd(e1.m_dd - e2.m_dd);
+    }
+    Rf_error("operator not implemented");
+    // not reached
+    return new bdtDd(0);
+}
+
+bdtDd* arith_bdtDd_int(const bdtDd& e1, const int& e2, std::string op) {
+    if (! op.compare("+")) {
+        return new bdtDd(e1.m_dd + boost::gregorian::date_duration(e2));
+    } else if (! op.compare("-")) {
+        return new bdtDd(e1.m_dd - boost::gregorian::date_duration(e2));
+    }
+    Rf_error("operator not implemented");
+    return new bdtDd(0);    // not reached
+}
+
+bdtDd* arith_int_bdtDd(const int& e1, const bdtDd& e2, std::string op) {
+    if (! op.compare("+")) {
+        return new bdtDd(e2.m_dd + boost::gregorian::date_duration(e1));
+    }
+    Rf_error("operator not implemented");
+    return new bdtDd(0);    // not reached
+}
+
+bool compare_bdtDd_bdtDd( const bdtDd& e1, const bdtDd& e2, std::string op ){
+    if( !op.compare( "==" ) ){
+        return e1.m_dd == e2.m_dd;   
+    } else if( !op.compare( "!=" ) ){
+        return e1.m_dd != e2.m_dd;
+    } else if( !op.compare( ">" ) ){
+        return e1.m_dd > e2.m_dd;
+    } else if( !op.compare( "<" ) ){
+        return e1.m_dd < e2.m_dd;
+    } else if( !op.compare( ">=" ) ){
+        return e1.m_dd >= e2.m_dd;
+    } else if( !op.compare( "<=" ) ){
+        return e1.m_dd <= e2.m_dd;
+    }
+    Rf_error("unknown operator");
+    return R_NilValue ;
+}
+
+RCPP_MODULE(bdtDdMod) {
+    Rcpp::class_<bdtDd>("bdtDd")   
+	
+        .constructor<int>("constructor with days")  
+        // ?  .constructor<SEXP>("constructor with days")  
+
+        .method("getDays",               &bdtDd::getDays,               "get hours of the time duration")
+
+    ;
+    
+    Rcpp::function( "days",              &days ) ; 
+    
+    Rcpp::function( "arith_bdtDd_bdtDd",        &arith_bdtDd_bdtDd); 
+    Rcpp::function( "arith_bdtDd_int",          &arith_bdtDd_int); 
+    Rcpp::function( "arith_int_bdtDd",          &arith_int_bdtDd); 
+    Rcpp::function( "compare_bdtDd_bdtDd",      &compare_bdtDd_bdtDd);
+}

Modified: pkg/RcppBDT/src/RcppBDTdt.cpp
===================================================================
--- pkg/RcppBDT/src/RcppBDTdt.cpp	2012-10-26 19:42:05 UTC (rev 3843)
+++ pkg/RcppBDT/src/RcppBDTdt.cpp	2012-10-27 00:42:00 UTC (rev 3844)
@@ -108,6 +108,26 @@
     return R_NilValue ;
 }
 
+bdtDt* arith_bdtDd_bdtDt(const bdtDd& e1, const bdtDt& e2, std::string op) {
+    if ( ! op.compare("+") ) {
+        return new bdtDt(e2.m_dt + e1.m_dd);
+    }
+    Rf_error( "operator not implemented" )  ;
+    // not reached
+    return new bdtDt(0);
+}
+
+bdtDt* arith_bdtDt_bdtDd(const bdtDt& e1, const bdtDd& e2, std::string op) {
+    if ( ! op.compare("+") ) {
+        return new bdtDt(e1.m_dt + e2.m_dd);
+    } else if( ! op.compare("-") ) {
+        return new bdtDt(e1.m_dt - e2.m_dd);
+    }
+    Rf_error( "operator not implemented" )  ;
+    // not reached
+    return new bdtDt(0);
+}
+
 RCPP_MODULE(bdtDtMod) {
     Rcpp::class_<bdtDt>("bdtDt")   
 	
@@ -162,6 +182,8 @@
     Rcpp::function("arith_bdtDt_int",         &arith_bdtDt_int); 
     Rcpp::function("arith_int_bdtDt",         &arith_int_bdtDt); 
     Rcpp::function("compare_bdtDt_bdtDt",     &compare_bdtDt_bdtDt);
+    Rcpp::function("arith_bdtDd_bdtDt",       &arith_bdtDd_bdtDt); 
+    Rcpp::function("arith_bdtDt_bdtDd",       &arith_bdtDt_bdtDd); 
 }
 
 



More information about the Rcpp-commits mailing list