[Rcpp-devel] [Rcpp-commits] r167 - pkg/src

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Mon Nov 9 18:46:44 CET 2009


Author: edd
Date: 2009-11-09 18:46:44 +0100 (Mon, 09 Nov 2009)
New Revision: 167

Added:
   pkg/src/RcppCommon.h
   pkg/src/RcppDate.cpp
   pkg/src/RcppDate.h
   pkg/src/RcppDatetime.cpp
   pkg/src/RcppDatetime.h
Modified:
   pkg/src/Makevars
   pkg/src/Rcpp.cpp
   pkg/src/Rcpp.h
   pkg/src/RcppExample.cpp
   pkg/src/RcppList.cpp
   pkg/src/RcppList.h
Log:
split RcppDate and RcppDatetime off from Rcpp
create new header RcppCommon.h
updated Makevars


Modified: pkg/src/Makevars
===================================================================
--- pkg/src/Makevars	2009-11-09 15:38:28 UTC (rev 166)
+++ pkg/src/Makevars	2009-11-09 17:46:44 UTC (rev 167)
@@ -11,9 +11,19 @@
 
 all: 		$(SHLIB) userLibrary 
 
+## all sources and objects
+SOURCES =	$(wildcard *.cpp)
+OBJECTS =	$(SOURCES:.cpp=.o)
+
+## the 'user' library contains all source apart from RcppExample.o and is what
+## users need for their projects -- RcppExample is used by the package library
+USEROBJ	=	$(filter-out RcppExample.o,$(OBJECTS))
+
+## we place it inside the inst/ directory so that it gets installed by the package
+USERDIR =	../inst/lib
+
 USERLIB	=	libRcpp$(DYLIB_EXT)
 USERLIBST =	libRcpp.a
-USERDIR =	../inst/lib
 
 PKG_CPPFLAGS += -I.
 
@@ -24,11 +34,11 @@
 		cp $(USERLIBST) $(USERDIR)$(R_ARCH)
 		rm $(USERLIB) $(USERLIBST)
 
-$(USERLIB): 	Rcpp.o RcppList.o
+$(USERLIB): 	$(USEROBJ)
 		$(SHLIB_CXXLD) -o $(USERLIB) $^ $(SHLIB_CXXLDFLAGS) $(ALL_LIBS)
 		@if test -e "/usr/bin/install_name_tool"; then /usr/bin/install_name_tool -id $(R_PACKAGE_DIR)/lib$(R_ARCH)/$(USERLIB) $(USERLIB); fi
 
-$(USERLIBST): 	Rcpp.o RcppList.o
+$(USERLIBST): 	$(USEROBJ)
 		$(AR) qc $(USERLIBST) $^
 		@if test -n "$(RANLIB)"; then $(RANLIB) $(USERLIBST); fi
 

Modified: pkg/src/Rcpp.cpp
===================================================================
--- pkg/src/Rcpp.cpp	2009-11-09 15:38:28 UTC (rev 166)
+++ pkg/src/Rcpp.cpp	2009-11-09 17:46:44 UTC (rev 167)
@@ -19,7 +19,7 @@
 // along with this library; if not, write to the Free Software Foundation, 
 // Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 
 
-#include "Rcpp.h"
+#include <Rcpp.h>
 
 RcppParams::RcppParams(SEXP params) {
     if (!Rf_isNewList(params))
@@ -800,94 +800,6 @@
     return rl;
 }
 
-// Print an RcppDate.
-std::ostream& operator<<(std::ostream& os, const RcppDate& date) {
-    os << date.getYear() << "-" << date.getMonth() << "-" << date.getDay();
-    return os;
-}
-
-// A few basic date operations.
-RcppDate operator+(const RcppDate& date, int offset) {
-    RcppDate temp(date.month, date.day, date.year);
-    temp.jdn += offset;
-    temp.jdn2mdy();
-    return temp;
-}
-
-int operator-(const RcppDate& date2, const RcppDate& date1) {
-    return date2.jdn - date1.jdn;
-}
-
-bool  operator<(const RcppDate &date1, const RcppDate& date2) {
-    return date1.jdn < date2.jdn;
-}
-
-bool  operator>(const RcppDate &date1, const RcppDate& date2) {
-    return date1.jdn > date2.jdn;
-}
-
-bool  operator>=(const RcppDate &date1, const RcppDate& date2) {
-    return date1.jdn >= date2.jdn;
-}
-
-bool  operator<=(const RcppDate &date1, const RcppDate& date2) {
-    return date1.jdn <= date2.jdn;
-}
-
-bool  operator==(const RcppDate &date1, const RcppDate& date2) {
-    return date1.jdn == date2.jdn;
-}
-
-// Offset used to convert from R date representation to Julian day number.
-const int RcppDate::Jan1970Offset = 2440588;
-
-// Offset used to convert between R / Unix date of Jan 1, 1970 and the QL base date
-const int RcppDate::QLtoJan1970Offset = 25569; 
-
-// The Julian day number (jdn) is the number of days since Monday,
-// Jan 1, 4713BC (year = -4712). Here 1BC is year 0, 2BC is year -1, etc.
-// On the other hand, R measures days since Jan 1, 1970, and these dates are
-// converted to jdn's by adding Jan1970Offset.
-//
-// mdy2jdn and jdn2mdy are inverse functions for dates back to 
-// year = -4799 (4800BC).
-//
-// See the Wikipedia entry on Julian day number for more information 
-// on these algorithms.
-//
-
-// Transform month/day/year to Julian day number.
-void RcppDate::mdy2jdn() {
-    int m = month, d = day, y = year;
-    int a = (14 - m)/12;
-    y += 4800 - a;
-    m += 12*a - 3;
-    jdn = (d + (153*m + 2)/5 + 365*y
-	   + y/4 - y/100 + y/400 - 32045);
-}
-
-// Transform from Julian day number to month/day/year.
-void RcppDate::jdn2mdy() {
-    int jul = jdn + 32044;
-    int g = jul/146097;
-    int dg = jul % 146097;
-    int c = (dg/36524 + 1)*3/4;
-    int dc = dg - c*36524;
-    int b = dc/1461;
-    int db = dc % 1461;
-    int a = (db/365 + 1)*3/4;
-    int da = db - a*365;
-    int y = g*400 + c*100 + b*4 + a;
-    int m = (da*5 + 308)/153 - 2;
-    int d = da - (m + 4)*153 /5 + 122;
-    y = y - 4800 + (m + 2)/12;
-    m = (m + 2) % 12 + 1;
-    d = d + 1;
-    month = m;
-    day   = d;
-    year  = y;
-}
-
 SEXP RcppFunction::listCall() {
     if (names.size() != (unsigned)listSize)
 	throw std::range_error("RcppFunction::listCall: no. of names != no. of items");

Modified: pkg/src/Rcpp.h
===================================================================
--- pkg/src/Rcpp.h	2009-11-09 15:38:28 UTC (rev 166)
+++ pkg/src/Rcpp.h	2009-11-09 17:46:44 UTC (rev 167)
@@ -22,132 +22,11 @@
 #ifndef Rcpp_hpp
 #define Rcpp_hpp
 
-#include <iostream>
-#include <sstream>
-#include <string>
-#include <list>
-#include <map>
-#include <stdexcept>
-#include <vector>
-
-// include R headers, but set R_NO_REMAP and access everything via Rf_ prefixes
-#define R_NO_REMAP
-
-#include <R.h>
-#include <Rinternals.h>
-
+#include <RcppCommon.h>
+#include <RcppDate.h>
+#include <RcppDatetime.h>
 #include <RcppList.h>
 
-// #ifdef BUILDING_DLL
-// #define RcppExport extern "C" __declspec(dllexport)
-// #else
-#define RcppExport extern "C"
-// #endif
-
-char *copyMessageToR(const char* const mesg);
-
-class RcppDate {
-private:
-    void mdy2jdn(); // month/day/year to Julian day number.
-    void jdn2mdy(); // Julian day number to month/day/year.
-    int month, day, year;
-    int jdn; // Julian day number
-
-public:
-    static const int Jan1970Offset;	// offset between Jan 1, 1970 and Julian Day Number
-    static const int QLtoJan1970Offset;	// offset between Jan 1, 1970 and QuantLib BaseDate
-    RcppDate() { month=1, day=1, year=1970; mdy2jdn(); }
-    RcppDate(int Rjdn) { jdn = Rjdn+Jan1970Offset; jdn2mdy(); }
-    RcppDate(int month_, int day_, int year_) : month(month_), 
-						day(day_),
-						year(year_) { 
-        if (month < 1 || month > 12 || day < 1 || day > 31)
-	    throw std::range_error("RcppDate: invalid date");
-        mdy2jdn();
-    }
-    int getMonth() const { return month; }
-    int getDay()  const  { return day; }
-    int getYear() const  { return year; }
-    int getJDN()  const  { return jdn; }
-
-    // Minimal set of date operations.
-
-    // These operators tend to conflict with QuantLib's
-    friend RcppDate operator+(const RcppDate &date, int offset);
-    friend int      operator-(const RcppDate& date1, const RcppDate& date2);
-    friend bool     operator<(const RcppDate &date1, const RcppDate& date2);
-    friend bool     operator>(const RcppDate &date1, const RcppDate& date2);
-    friend bool     operator==(const RcppDate &date1, const RcppDate& date2);
-    friend bool     operator>=(const RcppDate &date1, const RcppDate& date2);
-    friend bool     operator<=(const RcppDate &date1, const RcppDate& date2);
-
-    friend std::ostream& operator<<(std::ostream& os, const RcppDate& date);
-// #ifdef USING_QUANTLIB
-//     // Conversions from/to a QuantLib Date.
-//     RcppDate(Date dateQL);
-//     operator Date() const;
-// #endif
-};
-
-class RcppDatetime {
-private:
-    double m_d;
-    bool m_parsed;
-    int m_us;					// microseconds giving us fractional seconds
-    struct tm m_tm;
-    void parseTime() {
-	time_t tt = static_cast<time_t>(floor(m_d));		// time_t is the nb of seconds, ignore the fractional microseconds
-	m_us = static_cast<int>(round( (m_d - tt) * 1.0e6));	// fractional (micro)secs is diff. between (fractional) m_d and m_tt
-	m_tm = *localtime(&tt);			// parse time type into time structure 
-	m_parsed = true;			// and note that we parsed the time type
-	//printf("Time is %s %20u %8u\n", ctime(&m_tt), (unsigned int) m_tt, m_us);
-    };
-
-protected:
-    //friend class RcppResultSet;
-    friend class ColDatum;
-    //friend class RcppFunction;
-
-public:
-    RcppDatetime(void) : m_d(0), m_parsed(false), m_us(0) { };
-    RcppDatetime(const double d) : m_d(d), m_parsed(false), m_us(0) { };
-
-    double getFractionalTimestamp(void) const { return m_d; }
-
-    int getYear(void)     { if (!m_parsed) parseTime(); return m_tm.tm_year + 1900; }
-    int getMonth(void)    { if (!m_parsed) parseTime(); return m_tm.tm_mon + 1; }
-    int getDay(void)      { if (!m_parsed) parseTime(); return m_tm.tm_mday; } 
-    int getWeekday(void)  { if (!m_parsed) parseTime(); return m_tm.tm_wday; } 
-    int getHour(void)     { if (!m_parsed) parseTime(); return m_tm.tm_hour; } 
-    int getMinute(void)   { if (!m_parsed) parseTime(); return m_tm.tm_min; } 
-    int getSecond(void)   { if (!m_parsed) parseTime(); return m_tm.tm_sec; } 
-    int getMicroSec(void) { if (!m_parsed) parseTime(); return m_us; } 
-
-    friend RcppDatetime operator+(const RcppDatetime &date,   double offset) {
-	RcppDatetime tmp(date.m_d);
-	tmp.m_d += offset;
-	tmp.m_parsed = false;
-	return tmp;
-    }
-    friend double       operator-(const RcppDatetime& dt1,  const RcppDatetime& dt2) { return dt2.m_d -  dt1.m_d; }
-    friend bool         operator<(const RcppDatetime &dt1,  const RcppDatetime& dt2) { return dt1.m_d <  dt2.m_d; }
-    friend bool         operator<=(const RcppDatetime &dt1, const RcppDatetime& dt2) { return dt1.m_d <= dt2.m_d; }
-    friend bool         operator>(const RcppDatetime &dt1,  const RcppDatetime& dt2) { return dt1.m_d >  dt2.m_d; }
-    friend bool         operator>=(const RcppDatetime &dt1, const RcppDatetime& dt2) { return dt1.m_d >= dt2.m_d; }
-    friend bool         operator==(const RcppDatetime &dt1, const RcppDatetime& dt2) { return dt1.m_d == dt2.m_d; }  // remember float math...
-
-    friend std::ostream& operator<<(std::ostream& os, const RcppDatetime &datetime) {
-	RcppDatetime dt(datetime);
-	dt.parseTime();
-	char buf[32], usec[16];
-	strftime(buf, 31, "%Y-%m-%d %H:%M:%S", &dt.m_tm);
-	snprintf(usec, 15, ".%.06d", dt.m_us);
-	os << buf << usec;
-	return os;
-    }
-
-};
-
 class RcppParams {
 public:
     RcppParams(SEXP params);

Added: pkg/src/RcppCommon.h
===================================================================
--- pkg/src/RcppCommon.h	                        (rev 0)
+++ pkg/src/RcppCommon.h	2009-11-09 17:46:44 UTC (rev 167)
@@ -0,0 +1,47 @@
+// -*- mode: C++; c-indent-level: 4; c-basic-offset: 4; tab-width: 8 -*-
+//
+// RcppCommon.h: Rcpp R/C++ interface class library -- common include and defines statements
+//
+// Copyright (C) 2005 - 2006 Dominick Samperi
+// Copyright (C) 2008 - 2009 Dirk Eddelbuettel
+//
+// This library is free software; you can redistribute it and/or modify it 
+// under the terms of the GNU Lesser General Public License as published by 
+// the Free Software Foundation; either version 2.1 of the License, or (at 
+// your option) any later version.
+//
+// This library 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 Lesser General Public 
+// License for more details.
+//
+// You should have received a copy of the GNU Lesser General Public License 
+// along with this library; if not, write to the Free Software Foundation, 
+// Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 
+
+#ifndef RcppCommon_h
+#define RcppCommon_h
+
+#include <iostream>
+#include <sstream>
+#include <string>
+#include <list>
+#include <map>
+#include <stdexcept>
+#include <vector>
+
+// include R headers, but set R_NO_REMAP and access everything via Rf_ prefixes
+#define R_NO_REMAP
+
+#include <R.h>
+#include <Rinternals.h>
+
+// #ifdef BUILDING_DLL
+// #define RcppExport extern "C" __declspec(dllexport)
+// #else
+#define RcppExport extern "C"
+// #endif
+
+char *copyMessageToR(const char* const mesg);
+
+#endif


Property changes on: pkg/src/RcppCommon.h
___________________________________________________________________
Name: svn:eol-style
   + native

Added: pkg/src/RcppDate.cpp
===================================================================
--- pkg/src/RcppDate.cpp	                        (rev 0)
+++ pkg/src/RcppDate.cpp	2009-11-09 17:46:44 UTC (rev 167)
@@ -0,0 +1,126 @@
+// -*- mode: C++; c-indent-level: 4; c-basic-offset: 4; tab-width: 8 -*-
+//
+// RcppDate.h: Rcpp R/C++ interface class library -- Date type support
+//
+// Copyright (C) 2005 - 2006 Dominick Samperi
+// Copyright (C) 2008 - 2009 Dirk Eddelbuettel
+//
+// This library is free software; you can redistribute it and/or modify it 
+// under the terms of the GNU Lesser General Public License as published by 
+// the Free Software Foundation; either version 2.1 of the License, or (at 
+// your option) any later version.
+//
+// This library 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 Lesser General Public 
+// License for more details.
+//
+// You should have received a copy of the GNU Lesser General Public License 
+// along with this library; if not, write to the Free Software Foundation, 
+// Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 
+
+#include <RcppDate.h>
+
+const int RcppDate::Jan1970Offset = 2440588;    // Offset from R date representation to Julian day number.
+const int RcppDate::QLtoJan1970Offset = 25569;  // Offset between R / Unix epoch date and the QL base date
+
+RcppDate::RcppDate() : month(1), 
+				   day(1), 
+				   year(1970) { 
+	mdy2jdn(); 
+}
+
+RcppDate::RcppDate(int Rjdn) { 
+	jdn = Rjdn+Jan1970Offset; 
+	jdn2mdy(); 
+}
+
+RcppDate::RcppDate(int month_, int day_, int year_) : month(month_), 
+													  day(day_), 
+													  year(year_) { 
+	if (month < 1 || month > 12 || day < 1 || day > 31)
+		throw std::range_error("RcppDate: invalid date");
+	mdy2jdn();
+}
+
+// Print an RcppDate.
+std::ostream& operator<<(std::ostream& os, const RcppDate& date) {
+    os << date.getYear() << "-" << date.getMonth() << "-" << date.getDay();
+    return os;
+}
+
+// A few basic date operations.
+RcppDate operator+(const RcppDate& date, int offset) {
+    RcppDate temp(date.month, date.day, date.year);
+    temp.jdn += offset;
+    temp.jdn2mdy();
+    return temp;
+}
+
+int operator-(const RcppDate& date2, const RcppDate& date1) {
+    return date2.jdn - date1.jdn;
+}
+
+bool  operator<(const RcppDate &date1, const RcppDate& date2) {
+    return date1.jdn < date2.jdn;
+}
+
+bool  operator>(const RcppDate &date1, const RcppDate& date2) {
+    return date1.jdn > date2.jdn;
+}
+
+bool  operator>=(const RcppDate &date1, const RcppDate& date2) {
+    return date1.jdn >= date2.jdn;
+}
+
+bool  operator<=(const RcppDate &date1, const RcppDate& date2) {
+    return date1.jdn <= date2.jdn;
+}
+
+bool  operator==(const RcppDate &date1, const RcppDate& date2) {
+    return date1.jdn == date2.jdn;
+}
+
+// The Julian day number (jdn) is the number of days since Monday,
+// Jan 1, 4713BC (year = -4712). Here 1BC is year 0, 2BC is year -1, etc.
+// On the other hand, R measures days since Jan 1, 1970, and these dates are
+// converted to jdn's by adding Jan1970Offset.
+//
+// mdy2jdn and jdn2mdy are inverse functions for dates back to 
+// year = -4799 (4800BC).
+//
+// See the Wikipedia entry on Julian day number for more information 
+// on these algorithms.
+//
+
+// Transform month/day/year to Julian day number.
+void RcppDate::mdy2jdn() {
+    int m = month, d = day, y = year;
+    int a = (14 - m)/12;
+    y += 4800 - a;
+    m += 12*a - 3;
+    jdn = (d + (153*m + 2)/5 + 365*y
+	   + y/4 - y/100 + y/400 - 32045);
+}
+
+// Transform from Julian day number to month/day/year.
+void RcppDate::jdn2mdy() {
+    int jul = jdn + 32044;
+    int g = jul/146097;
+    int dg = jul % 146097;
+    int c = (dg/36524 + 1)*3/4;
+    int dc = dg - c*36524;
+    int b = dc/1461;
+    int db = dc % 1461;
+    int a = (db/365 + 1)*3/4;
+    int da = db - a*365;
+    int y = g*400 + c*100 + b*4 + a;
+    int m = (da*5 + 308)/153 - 2;
+    int d = da - (m + 4)*153 /5 + 122;
+    y = y - 4800 + (m + 2)/12;
+    m = (m + 2) % 12 + 1;
+    d = d + 1;
+    month = m;
+    day   = d;
+    year  = y;
+}


Property changes on: pkg/src/RcppDate.cpp
___________________________________________________________________
Name: svn:eol-style
   + native

Added: pkg/src/RcppDate.h
===================================================================
--- pkg/src/RcppDate.h	                        (rev 0)
+++ pkg/src/RcppDate.h	2009-11-09 17:46:44 UTC (rev 167)
@@ -0,0 +1,63 @@
+// -*- mode: C++; c-indent-level: 4; c-basic-offset: 4; tab-width: 8 -*-
+//
+// RcppDate.h: Rcpp R/C++ interface class library -- Date type support
+//
+// Copyright (C) 2005 - 2006 Dominick Samperi
+// Copyright (C) 2008 - 2009 Dirk Eddelbuettel
+//
+// This library is free software; you can redistribute it and/or modify it 
+// under the terms of the GNU Lesser General Public License as published by 
+// the Free Software Foundation; either version 2.1 of the License, or (at 
+// your option) any later version.
+//
+// This library 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 Lesser General Public 
+// License for more details.
+//
+// You should have received a copy of the GNU Lesser General Public License 
+// along with this library; if not, write to the Free Software Foundation, 
+// Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 
+
+#ifndef RcppDate_h
+#define RcppDate_h
+
+#include <RcppCommon.h>
+
+class RcppDate {
+private:
+    void mdy2jdn(); 						// month/day/year to Julian day number.
+    void jdn2mdy(); 						// Julian day number to month/day/year.
+    int month, day, year;
+    int jdn; 								// Julian day number
+
+public:
+    static const int Jan1970Offset;			// offset between Jan 1, 1970 and Julian Day Number
+    static const int QLtoJan1970Offset;		// offset between Jan 1, 1970 and QuantLib BaseDate
+    RcppDate();
+    RcppDate(int Rjdn);
+    RcppDate(int month_, int day_, int year_);
+    int getMonth() const { return month; }
+    int getDay()  const  { return day; }
+    int getYear() const  { return year; }
+    int getJDN()  const  { return jdn; }
+
+    // Minimal set of date operations.
+    // These operators tend to conflict with QuantLib's
+    friend RcppDate operator+(const RcppDate &date, int offset);
+    friend int      operator-(const RcppDate& date1, const RcppDate& date2);
+    friend bool     operator<(const RcppDate &date1, const RcppDate& date2);
+    friend bool     operator>(const RcppDate &date1, const RcppDate& date2);
+    friend bool     operator==(const RcppDate &date1, const RcppDate& date2);
+    friend bool     operator>=(const RcppDate &date1, const RcppDate& date2);
+    friend bool     operator<=(const RcppDate &date1, const RcppDate& date2);
+
+    friend std::ostream& operator<<(std::ostream& os, const RcppDate& date);
+// #ifdef USING_QUANTLIB
+//     // Conversions from/to a QuantLib Date.
+//     RcppDate(Date dateQL);
+//     operator Date() const;
+// #endif
+};
+
+#endif


Property changes on: pkg/src/RcppDate.h
___________________________________________________________________
Name: svn:eol-style
   + native

Added: pkg/src/RcppDatetime.cpp
===================================================================
--- pkg/src/RcppDatetime.cpp	                        (rev 0)
+++ pkg/src/RcppDatetime.cpp	2009-11-09 17:46:44 UTC (rev 167)
@@ -0,0 +1,59 @@
+// -*- mode: C++; c-indent-level: 4; c-basic-offset: 4; tab-width: 8 -*-
+//
+// RcppDatetime.h: Rcpp R/C++ interface class library -- Datetime type support
+//
+// Copyright (C) 2008 - 2009 Dirk Eddelbuettel
+//
+// This library is free software; you can redistribute it and/or modify it 
+// under the terms of the GNU Lesser General Public License as published by 
+// the Free Software Foundation; either version 2.1 of the License, or (at 
+// your option) any later version.
+//
+// This library 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 Lesser General Public 
+// License for more details.
+//
+// You should have received a copy of the GNU Lesser General Public License 
+// along with this library; if not, write to the Free Software Foundation, 
+// Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 
+
+#include <RcppDatetime.h>
+
+RcppDatetime::RcppDatetime(void) : m_d(0), 
+								   m_parsed(false), 
+								   m_us(0) { 
+};
+
+RcppDatetime::RcppDatetime(const double d) : m_d(d), 
+											 m_parsed(false), 
+											 m_us(0) { 
+};
+
+void RcppDatetime::parseTime() {
+	// tt is the nb of seconds, ignores fractional microsec
+	time_t tt = static_cast<time_t>(floor(m_d));	
+	m_tm = *localtime(&tt);			// parse time type into time structure 
+
+	// m_us is fractional (micro)secs is diff. between (fractional) m_d and m_tm
+	m_us = static_cast<int>(round( (m_d - tt) * 1.0e6));	
+
+	m_parsed = true;				// and note that we parsed the time type
+};
+
+std::ostream& operator<<(std::ostream& os, const RcppDatetime &datetime) {
+	RcppDatetime dt(datetime);
+	dt.parseTime();
+	char buf[32], usec[16];
+	strftime(buf, 31, "%Y-%m-%d %H:%M:%S", &dt.m_tm);
+	snprintf(usec, 15, ".%.06d", dt.m_us);
+	os << buf << usec;
+	return os;
+};
+
+RcppDatetime operator+(const RcppDatetime &date,   double offset) {
+	RcppDatetime tmp(date.m_d);
+	tmp.m_d += offset;
+	tmp.m_parsed = false;
+	return tmp;
+};


Property changes on: pkg/src/RcppDatetime.cpp
___________________________________________________________________
Name: svn:eol-style
   + native

Added: pkg/src/RcppDatetime.h
===================================================================
--- pkg/src/RcppDatetime.h	                        (rev 0)
+++ pkg/src/RcppDatetime.h	2009-11-09 17:46:44 UTC (rev 167)
@@ -0,0 +1,61 @@
+// -*- mode: C++; c-indent-level: 4; c-basic-offset: 4; tab-width: 8 -*-
+//
+// RcppDatetime.h: Rcpp R/C++ interface class library -- Datetime type support
+//
+// Copyright (C) 2008 - 2009 Dirk Eddelbuettel
+//
+// This library is free software; you can redistribute it and/or modify it 
+// under the terms of the GNU Lesser General Public License as published by 
+// the Free Software Foundation; either version 2.1 of the License, or (at 
+// your option) any later version.
+//
+// This library 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 Lesser General Public 
+// License for more details.
+//
+// You should have received a copy of the GNU Lesser General Public License 
+// along with this library; if not, write to the Free Software Foundation, 
+// Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 
+
+#ifndef RcppDatetime_h
+#define RcppDatetime_h
+
+#include <RcppCommon.h>
+
+class RcppDatetime {
+private:
+    double m_d;
+    bool m_parsed;
+    int m_us;							// microseconds giving us fractional seconds
+    struct tm m_tm;
+    void parseTime();
+
+protected:
+    friend class ColDatum;
+
+public:
+    RcppDatetime(void);
+    RcppDatetime(const double d);
+
+    double getFractionalTimestamp(void) const { return m_d; }
+    int getYear(void)     { if (!m_parsed) parseTime(); return m_tm.tm_year + 1900; }
+    int getMonth(void)    { if (!m_parsed) parseTime(); return m_tm.tm_mon + 1; }
+    int getDay(void)      { if (!m_parsed) parseTime(); return m_tm.tm_mday; } 
+    int getWeekday(void)  { if (!m_parsed) parseTime(); return m_tm.tm_wday; } 
+    int getHour(void)     { if (!m_parsed) parseTime(); return m_tm.tm_hour; } 
+    int getMinute(void)   { if (!m_parsed) parseTime(); return m_tm.tm_min; } 
+    int getSecond(void)   { if (!m_parsed) parseTime(); return m_tm.tm_sec; } 
+    int getMicroSec(void) { if (!m_parsed) parseTime(); return m_us; } 
+
+    friend double        operator-(const RcppDatetime& dt1,  const RcppDatetime& dt2) { return dt2.m_d -  dt1.m_d; }
+    friend bool          operator<(const RcppDatetime &dt1,  const RcppDatetime& dt2) { return dt1.m_d <  dt2.m_d; }
+    friend bool          operator<=(const RcppDatetime &dt1, const RcppDatetime& dt2) { return dt1.m_d <= dt2.m_d; }
+    friend bool          operator>(const RcppDatetime &dt1,  const RcppDatetime& dt2) { return dt1.m_d >  dt2.m_d; }
+    friend bool          operator>=(const RcppDatetime &dt1, const RcppDatetime& dt2) { return dt1.m_d >= dt2.m_d; }
+    friend bool          operator==(const RcppDatetime &dt1, const RcppDatetime& dt2) { return dt1.m_d == dt2.m_d; }  // remember float math...
+    friend std::ostream& operator<<(std::ostream& os, const RcppDatetime &datetime); 
+    friend RcppDatetime  operator+(const RcppDatetime &date,   double offset);
+};
+
+#endif


Property changes on: pkg/src/RcppDatetime.h
___________________________________________________________________
Name: svn:eol-style
   + native

Modified: pkg/src/RcppExample.cpp
===================================================================
--- pkg/src/RcppExample.cpp	2009-11-09 15:38:28 UTC (rev 166)
+++ pkg/src/RcppExample.cpp	2009-11-09 17:46:44 UTC (rev 167)
@@ -19,7 +19,7 @@
 // along with this library; if not, write to the Free Software Foundation, 
 // Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 
 
-#include "Rcpp.h"
+#include <Rcpp.h>
 
 /*
  * The following class definitions employ advanced features of the Rcpp

Modified: pkg/src/RcppList.cpp
===================================================================
--- pkg/src/RcppList.cpp	2009-11-09 15:38:28 UTC (rev 166)
+++ pkg/src/RcppList.cpp	2009-11-09 17:46:44 UTC (rev 167)
@@ -18,8 +18,18 @@
 // along with this library; if not, write to the Free Software Foundation, 
 // Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 
 
-#include "RcppList.h"
+#include <RcppList.h>
 
+RcppList::RcppList(void) : listArg(R_NilValue), 
+			   listSize(0), 
+			   currListPosn(0), 
+			   numProtected(0) { 
+}; 
+
+RcppList::~RcppList(void) {
+    UNPROTECT(numProtected);
+}
+
 void RcppList::setSize(int n) {
     listSize = n;
     listArg = PROTECT(Rf_allocVector(VECSXP, n));

Modified: pkg/src/RcppList.h
===================================================================
--- pkg/src/RcppList.h	2009-11-09 15:38:28 UTC (rev 166)
+++ pkg/src/RcppList.h	2009-11-09 17:46:44 UTC (rev 167)
@@ -1,6 +1,6 @@
 // -*- mode: C++; c-indent-level: 4; c-basic-offset: 4; tab-width: 8 -*-
 //
-// RcppList.h: Rcpp.h: R/C++ interface class library -- 'list' type support
+// RcppList.h: Rcpp R/C++ interface class library -- 'list' type support
 //
 // Copyright (C) 2009 Dirk Eddelbuettel
 //
@@ -21,29 +21,14 @@
 #ifndef RcppList_h
 #define RcppList_h
 
-#include <iostream>
-#include <sstream>
-#include <string>
-#include <list>
-#include <map>
-#include <stdexcept>
-#include <vector>
+#include <RcppCommon.h>
 
-// include R headers, but set R_NO_REMAP and access everything via Rf_ prefixes
-#define R_NO_REMAP
-
-#include <R.h>
-#include <Rinternals.h>
-
 // This class was first used in the RProtoBuf project (currently on r-forge only)
 // but is more generally useful and hence moved over here
 class RcppList {
 public:
-    RcppList(void): 
-	listArg(R_NilValue), listSize(0), currListPosn(0), numProtected(0) { }; 
-    ~RcppList() {
-	UNPROTECT(numProtected);
-    }
+    RcppList(void);
+    ~RcppList();
     void setSize(int size);
     void append(std::string name, double value);
     void append(std::string name, int value);

_______________________________________________
Rcpp-commits mailing list
Rcpp-commits at lists.r-forge.r-project.org
https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-commits


More information about the Rcpp-devel mailing list