[Rcpp-commits] r1655 - in pkg/Rcpp: inst inst/include inst/include/Rcpp inst/unitTests src

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Wed Jun 23 05:41:07 CEST 2010


Author: edd
Date: 2010-06-23 05:41:07 +0200 (Wed, 23 Jun 2010)
New Revision: 1655

Added:
   pkg/Rcpp/inst/include/Rcpp/Date.h
   pkg/Rcpp/inst/unitTests/runit.Date.R
   pkg/Rcpp/src/Date.cpp
Modified:
   pkg/Rcpp/inst/ChangeLog
   pkg/Rcpp/inst/include/Rcpp.h
Log:
beginnings of a new Date class


Modified: pkg/Rcpp/inst/ChangeLog
===================================================================
--- pkg/Rcpp/inst/ChangeLog	2010-06-22 21:28:47 UTC (rev 1654)
+++ pkg/Rcpp/inst/ChangeLog	2010-06-23 03:41:07 UTC (rev 1655)
@@ -1,3 +1,14 @@
+2010-06-22  Dirk Eddelbuettel  <edd at debian.org>
+
+	* inst/include/Rcpp/Date.h: New Date class (not yet complete)
+	* src/Date.cpp: Implementation for new Date class (not yet complete)
+	* inst/unitTests/runit.Date.R: first tests for Rcpp::Date
+
+2010-06-21  Dirk Eddelbuettel  <edd at debian.org>
+
+	* inst/examples/SugarBenchmark/: New example trying to benchmark
+	new syntactic 'sugar' classes
+
 2010-06-19  Dirk Eddelbuettel  <edd at debian.org>
 
 	* inst/include/RcppDoxygenExamples.h: Correct three wrong paths for

Added: pkg/Rcpp/inst/include/Rcpp/Date.h
===================================================================
--- pkg/Rcpp/inst/include/Rcpp/Date.h	                        (rev 0)
+++ pkg/Rcpp/inst/include/Rcpp/Date.h	2010-06-23 03:41:07 UTC (rev 1655)
@@ -0,0 +1,65 @@
+// -*- mode: C++; c-indent-level: 4; c-basic-offset: 4; tab-width: 8 -*-
+//
+// DataFrame.h: Rcpp R/C++ interface class library -- data frames
+//
+// Copyright (C) 2010	Dirk Eddelbuettel and Romain Francois
+//
+// This file is part of Rcpp.
+//
+// Rcpp 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.
+//
+// Rcpp 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 Rcpp.  If not, see <http://www.gnu.org/licenses/>.
+
+#ifndef Rcpp__Date_h
+#define Rcpp__Date_h
+
+#include <RcppCommon.h>
+
+namespace Rcpp {
+
+    class Date {
+    public:	
+	Date();
+	Date(const int &dt);	// from integer, just like R (with negative dates before Jan 1, 1970)
+	Date(const std::string &s, const std::string &fmt="%Y-%m-%d");
+	Date(const unsigned int &m, const unsigned int &d, const unsigned int &y); 
+	~Date();
+		
+	int getDate(void) const; 
+
+#if 0		
+	// rest to follow
+	// assignment
+	// copy
+	// ...
+	
+	// Minimal set of date operations.
+	friend Date   operator+(const Date &date, int offset);
+	friend int    operator-(const Date& date1, const Date& date2);
+	friend bool   operator<(const Date &date1, const Date& date2);
+	friend bool   operator>(const Date &date1, const Date& date2);
+	friend bool   operator==(const Date &date1, const Date& date2);
+	friend bool   operator>=(const Date &date1, const Date& date2);
+	friend bool   operator<=(const Date &date1, const Date& date2);
+#endif
+
+    private:
+        int d;					// day number, relative to epoch of Jan 1, 1970
+    };
+
+
+    // template specialisation for wrap() on the date 
+    template <> SEXP wrap<Rcpp::Date>(const Rcpp::Date &date);
+
+}
+
+#endif

Modified: pkg/Rcpp/inst/include/Rcpp.h
===================================================================
--- pkg/Rcpp/inst/include/Rcpp.h	2010-06-22 21:28:47 UTC (rev 1654)
+++ pkg/Rcpp/inst/include/Rcpp.h	2010-06-23 03:41:07 UTC (rev 1655)
@@ -68,6 +68,7 @@
 #include <Rcpp/StringTransformer.h>
 #include <Rcpp/Formula.h>
 #include <Rcpp/DataFrame.h>
+#include <Rcpp/Date.h>
 
 #ifdef RCPP_ENABLE_MODULES
 #include <Rcpp/Module.h>

Added: pkg/Rcpp/inst/unitTests/runit.Date.R
===================================================================
--- pkg/Rcpp/inst/unitTests/runit.Date.R	                        (rev 0)
+++ pkg/Rcpp/inst/unitTests/runit.Date.R	2010-06-23 03:41:07 UTC (rev 1655)
@@ -0,0 +1,44 @@
+#!/usr/bin/r -t
+#
+# Copyright (C) 2010	Dirk Eddelbuettel and Romain Francois
+#
+# This file is part of Rcpp.
+#
+# Rcpp 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.
+#
+# Rcpp 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 Rcpp.  If not, see <http://www.gnu.org/licenses/>.
+
+test.Date.ctor.mdy <- function() {
+    src <- 'Rcpp::Date dt = Rcpp::Date(12,31,2005);
+	    return Rcpp::wrap(dt);';
+    fun <- cppfunction(signature(), src)
+    checkEquals(fun(), as.Date("2005-12-31"), msg = "Date.ctor.mdy")
+}
+
+test.Date.ctor.ymd <- function() {
+    src <- 'Rcpp::Date dt = Rcpp::Date(2005,12,31);
+	    return Rcpp::wrap(dt);';
+    fun <- cppfunction(signature(), src)
+    checkEquals(fun(), as.Date("2005-12-31"), msg = "Date.ctor.ymd")
+}
+
+test.Date.ctor.int <- function() {
+    src <- 'Rcpp::Date dt = Rcpp::Date(Rcpp::as<int>(d));
+	    return Rcpp::wrap(dt);';
+    fun <- cppfunction(signature(d="numeric"), src)
+    d <- as.Date("2005-12-31")
+    checkEquals(fun(as.numeric(d)), d, msg = "Date.ctor.int")
+    checkEquals(fun(-1), as.Date("1970-01-01")-1, msg = "Date.ctor.int")
+    checkException(fun("foo"), msg = "Date.ctor -> exception" )
+}
+
+

Added: pkg/Rcpp/src/Date.cpp
===================================================================
--- pkg/Rcpp/src/Date.cpp	                        (rev 0)
+++ pkg/Rcpp/src/Date.cpp	2010-06-23 03:41:07 UTC (rev 1655)
@@ -0,0 +1,78 @@
+// -*- mode: C++; c-indent-level: 4; c-basic-offset: 4; tab-width: 8 -*-
+//
+// Date.h: Rcpp R/C++ interface class library -- Date type
+//
+// Copyright (C) 2010	Dirk Eddelbuettel and Romain Francois
+//
+// This file is part of Rcpp.
+//
+// Rcpp 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.
+//
+// Rcpp 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 Rcpp.  If not, see <http://www.gnu.org/licenses/>.
+
+#include <Rcpp/Date.h>
+
+namespace Rcpp {
+
+    Date::Date() {
+	d = 0; 
+    };
+
+    Date::Date(const int &dt) {
+	d = dt;
+    }
+
+    Date::Date(const std::string &s, const std::string &fmt) {
+	// we cheat and call strptime() from R
+	Rcpp::Function strptime("strptime");
+	d = Rcpp::as<int>(strptime(s, fmt));
+    }
+
+    Date::Date(const unsigned int &mon, const unsigned int &day, const unsigned int &year) {
+
+	struct tm tm;;
+	tm.tm_sec = tm.tm_min = tm.tm_hour = tm.tm_isdst = 0;
+
+	// allow for special case (yyyy, mm, dd) which we prefer over (mm, dd, year)
+	if (mon >= 1900 && day <= 12 && year <= 31) {
+	    tm.tm_year = mon - 1900;
+	    tm.tm_mon  = day - 1;       // range 0 to 11
+	    tm.tm_mday = year;
+	} else {
+	    tm.tm_mday  = day;
+	    tm.tm_mon   = mon - 1;	// range 0 to 11
+	    tm.tm_year  = year - 1900;
+	}
+	double tmp = mktime(&tm);
+	d = tmp/(24*60*60);
+    }
+
+    Date::~Date() {
+	// nothing to do
+    }
+
+    int Date::getDate(void) const { 
+	return d; 
+    };
+
+
+    template <> SEXP wrap(const Date &date) {
+	SEXP value = PROTECT(Rf_allocVector(REALSXP, 1));
+	REAL(value)[0] = date.getDate();
+	SEXP dateclass = PROTECT(Rf_allocVector(STRSXP,1));
+	SET_STRING_ELT(dateclass, 0, Rf_mkChar("Date"));
+	Rf_setAttrib(value, R_ClassSymbol, dateclass); 
+	UNPROTECT(2);
+	return value;
+    }
+
+}



More information about the Rcpp-commits mailing list