[Rcpp-commits] r172 - pkg/src

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Mon Nov 16 02:06:04 CET 2009


Author: edd
Date: 2009-11-16 02:06:01 +0100 (Mon, 16 Nov 2009)
New Revision: 172

Added:
   pkg/src/RcppDateVector.cpp
   pkg/src/RcppDateVector.h
   pkg/src/RcppDatetimeVector.cpp
   pkg/src/RcppDatetimeVector.h
   pkg/src/RcppFunction.cpp
   pkg/src/RcppFunction.h
   pkg/src/RcppMatrix.cpp
   pkg/src/RcppMatrix.h
   pkg/src/RcppMatrixView.cpp
   pkg/src/RcppMatrixView.h
   pkg/src/RcppNumList.cpp
   pkg/src/RcppNumList.h
   pkg/src/RcppResultSet.cpp
   pkg/src/RcppResultSet.h
   pkg/src/RcppStringVector.cpp
   pkg/src/RcppStringVector.h
   pkg/src/RcppStringVectorView.cpp
   pkg/src/RcppStringVectorView.h
   pkg/src/RcppVector.cpp
   pkg/src/RcppVector.h
   pkg/src/RcppVectorView.cpp
   pkg/src/RcppVectorView.h
Log:
added new files split off from Rcpp.h and Rcpp.cpp

Added: pkg/src/RcppDateVector.cpp
===================================================================
--- pkg/src/RcppDateVector.cpp	                        (rev 0)
+++ pkg/src/RcppDateVector.cpp	2009-11-16 01:06:01 UTC (rev 172)
@@ -0,0 +1,52 @@
+// -*- mode: C++; c-indent-level: 4; c-basic-offset: 4; tab-width: 8 -*-
+//
+// RcppDateVector.cpp: Rcpp R/C++ interface class library -- Date vector 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 <RcppDateVector.h>
+
+RcppDateVector::RcppDateVector(SEXP vec) {
+    int i;
+    if (!Rf_isNumeric(vec) || Rf_isMatrix(vec) || Rf_isLogical(vec))
+	throw std::range_error("RcppDateVector: invalid numeric vector in constructor");
+    int len = Rf_length(vec);
+    if (len == 0)
+	throw std::range_error("RcppDateVector: null vector in constructor");
+    v = new RcppDate[len];
+    for (i = 0; i < len; i++)
+	v[i] = RcppDate((int)REAL(vec)[i]);
+    length = len;
+}
+
+RcppDateVector::~RcppDateVector() {
+    delete [] v;
+}
+
+inline RcppDate& RcppDateVector::operator()(int i) {
+    if (i < 0 || i >= length) {
+	std::ostringstream oss;
+	oss << "RcppDateVector: subscript out of range: " << i;
+	throw std::range_error(oss.str());
+    }
+    return v[i];
+}
+
+int RcppDateVector::size() const { 
+    return length; 
+}

Added: pkg/src/RcppDateVector.h
===================================================================
--- pkg/src/RcppDateVector.h	                        (rev 0)
+++ pkg/src/RcppDateVector.h	2009-11-16 01:06:01 UTC (rev 172)
@@ -0,0 +1,39 @@
+// -*- mode: C++; c-indent-level: 4; c-basic-offset: 4; tab-width: 8 -*-
+//
+// RcppDateVector.h: Rcpp R/C++ interface class library -- Date vector 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 RcppDateVector_h
+#define RcppDateVector_h
+
+#include <RcppCommon.h>
+#include <RcppDate.h>
+
+class RcppDateVector {
+public:
+    RcppDateVector(SEXP vec);
+    ~RcppDateVector();
+    RcppDate& operator()(int i);
+    int size() const;
+private:
+    RcppDate *v;
+    int length;
+};
+
+#endif

Added: pkg/src/RcppDatetimeVector.cpp
===================================================================
--- pkg/src/RcppDatetimeVector.cpp	                        (rev 0)
+++ pkg/src/RcppDatetimeVector.cpp	2009-11-16 01:06:01 UTC (rev 172)
@@ -0,0 +1,52 @@
+// -*- mode: C++; c-indent-level: 4; c-basic-offset: 4; tab-width: 8 -*-
+//
+// RcppDatetimeVector.cpp: Rcpp R/C++ interface class library -- Datetime vector 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 <RcppDatetimeVector.h>
+
+RcppDatetimeVector::RcppDatetimeVector(SEXP vec) {
+    int i;
+    if (!Rf_isNumeric(vec) || Rf_isMatrix(vec) || Rf_isLogical(vec))
+	throw std::range_error("RcppDatetimeVector: invalid numeric vector in constructor");
+    int len = Rf_length(vec);
+    if (len == 0)
+	throw std::range_error("RcppDatetimeVector: null vector in constructor");
+    v = new RcppDatetime[len];
+    for (i = 0; i < len; i++)
+	v[i] = RcppDatetime(REAL(vec)[i]);
+    length = len;
+}
+
+RcppDatetimeVector::~RcppDatetimeVector() {
+    delete [] v;
+}
+
+inline RcppDatetime & RcppDatetimeVector::operator()(int i) const {
+    if (i < 0 || i >= length) {
+	std::ostringstream oss;
+	oss << "RcppDatetimeVector: subscript out of range: " << i;
+	throw std::range_error(oss.str());
+    }
+    return v[i];
+}
+
+int RcppDatetimeVector::size() const { 
+    return length; 
+}
+

Added: pkg/src/RcppDatetimeVector.h
===================================================================
--- pkg/src/RcppDatetimeVector.h	                        (rev 0)
+++ pkg/src/RcppDatetimeVector.h	2009-11-16 01:06:01 UTC (rev 172)
@@ -0,0 +1,38 @@
+// -*- mode: C++; c-indent-level: 4; c-basic-offset: 4; tab-width: 8 -*-
+//
+// RcppDatetimeeVector.h: Rcpp R/C++ interface class library -- Datetime vector 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 RcppDatetimeVector_h
+#define RcppDatetimeVector_h
+
+#include <RcppCommon.h>
+#include <RcppDatetime.h>
+
+class RcppDatetimeVector {
+public:
+    RcppDatetimeVector(SEXP vec);
+    ~RcppDatetimeVector();
+    RcppDatetime &operator()(int i) const;
+    int size() const;
+private:
+    RcppDatetime *v;
+    int length;
+};
+
+#endif

Added: pkg/src/RcppFunction.cpp
===================================================================
--- pkg/src/RcppFunction.cpp	                        (rev 0)
+++ pkg/src/RcppFunction.cpp	2009-11-16 01:06:01 UTC (rev 172)
@@ -0,0 +1,144 @@
+// -*- mode: C++; c-indent-level: 4; c-basic-offset: 4; tab-width: 8 -*-
+//
+// RcppFunction.cpp: Rcpp R/C++ interface class library -- function 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 <RcppFunction.h>
+
+RcppFunction::RcppFunction(SEXP fn) : fn(fn) { 
+    if (!Rf_isFunction(fn))
+	throw std::range_error("RcppFunction: non-function where function expected");
+    numProtected = 0;
+    currListPosn = 0;
+    listSize = 0;
+    vectorArg = listArg = R_NilValue;
+};
+
+RcppFunction::~RcppFunction() {
+    UNPROTECT(numProtected);
+}
+
+SEXP RcppFunction::listCall() {
+    if (names.size() != (unsigned)listSize)
+	throw std::range_error("RcppFunction::listCall: no. of names != no. of items");
+    if (currListPosn != listSize)
+	throw std::range_error("RcppFunction::listCall: list has incorrect size");
+    SEXP nm = PROTECT(Rf_allocVector(STRSXP,listSize));
+    numProtected++;
+    for (int i=0; i < listSize; i++)
+	SET_STRING_ELT(nm, i, Rf_mkChar(names[i].c_str()));
+    Rf_setAttrib(listArg, R_NamesSymbol, nm);
+    SEXP R_fcall;
+    PROTECT(R_fcall = Rf_lang2(fn, R_NilValue));
+    numProtected++;
+    SETCADR(R_fcall, listArg);
+    SEXP result = Rf_eval(R_fcall, R_NilValue);
+    names.clear();
+    listSize = currListPosn = 0; // Ready for next call.
+    return result;
+}
+
+SEXP RcppFunction::vectorCall() {
+    if (vectorArg == R_NilValue)
+	throw std::range_error("RcppFunction::vectorCall: vector has not been set");
+    SEXP R_fcall;
+    PROTECT(R_fcall = Rf_lang2(fn, R_NilValue));
+    numProtected++;
+    SETCADR(R_fcall, vectorArg);
+    SEXP result = Rf_eval(R_fcall, R_NilValue);
+    vectorArg = R_NilValue; // Ready for next call.
+    return result;
+}
+
+void RcppFunction::setRVector(std::vector<double>& v) {
+    vectorArg = PROTECT(Rf_allocVector(REALSXP,v.size()));
+    numProtected++;
+    for (int i=0; i < (int)v.size(); i++)
+	REAL(vectorArg)[i] = v[i];
+}
+
+void RcppFunction::setRListSize(int n) {
+    listSize = n;
+    listArg = PROTECT(Rf_allocVector(VECSXP, n));
+    numProtected++;
+}
+
+void RcppFunction::appendToRList(std::string name, double value) {
+    if (currListPosn < 0 || currListPosn >= listSize)
+	throw std::range_error("RcppFunction::appendToRList(double): list posn out of range");
+    SEXP valsxp = PROTECT(Rf_allocVector(REALSXP,1));
+    numProtected++;
+    REAL(valsxp)[0] = value;
+    SET_VECTOR_ELT(listArg, currListPosn++, valsxp);
+    names.push_back(name);
+}
+
+void RcppFunction::appendToRList(std::string name, int value) {
+    if (currListPosn < 0 || currListPosn >= listSize)
+	throw std::range_error("RcppFunction::appendToRlist(int): posn out of range");
+    SEXP valsxp = PROTECT(Rf_allocVector(INTSXP,1));
+    numProtected++;
+    INTEGER(valsxp)[0] = value;
+    SET_VECTOR_ELT(listArg, currListPosn++, valsxp);
+    names.push_back(name);
+}
+
+void RcppFunction::appendToRList(std::string name, std::string value) {
+    if (currListPosn < 0 || currListPosn >= listSize)
+	throw std::range_error("RcppFunction::appendToRlist(string): posn out of range");
+    SEXP valsxp = PROTECT(Rf_allocVector(STRSXP,1));
+    numProtected++;
+    SET_STRING_ELT(valsxp, 0, Rf_mkChar(value.c_str()));
+    SET_VECTOR_ELT(listArg, currListPosn++, valsxp);
+    names.push_back(name);
+}
+
+void RcppFunction::appendToRList(std::string name, RcppDate& date) {
+    if (currListPosn < 0 || currListPosn >= listSize)
+	throw std::range_error("RcppFunction::appendToRlist(RcppDate): list posn out of range");
+    SEXP valsxp = PROTECT(Rf_allocVector(REALSXP,1));
+    numProtected++;
+    REAL(valsxp)[0] = date.getJDN() - RcppDate::Jan1970Offset;
+    SEXP dateclass = PROTECT(Rf_allocVector(STRSXP, 1));
+    numProtected++;
+    SET_STRING_ELT(dateclass, 0, Rf_mkChar("Date"));
+    Rf_setAttrib(valsxp, R_ClassSymbol, dateclass);
+    SET_VECTOR_ELT(listArg, currListPosn++, valsxp);
+    names.push_back(name);
+}
+
+void RcppFunction::appendToRList(std::string name, RcppDatetime& datetime) {
+    if (currListPosn < 0 || currListPosn >= listSize)
+	throw std::range_error("RcppFunction::appendToRlist(RcppDatetime): list posn out of range");
+    SEXP valsxp = PROTECT(Rf_allocVector(REALSXP,1));
+    numProtected++;
+    REAL(valsxp)[0] = datetime.getFractionalTimestamp();
+    SEXP datetimeclass = PROTECT(Rf_allocVector(STRSXP, 2));
+    numProtected++;
+    SET_STRING_ELT(datetimeclass, 0, Rf_mkChar("POSIXt"));
+    SET_STRING_ELT(datetimeclass, 1, Rf_mkChar("POSIXct"));
+    Rf_setAttrib(valsxp, R_ClassSymbol, datetimeclass);
+    SET_VECTOR_ELT(listArg, currListPosn++, valsxp);
+    names.push_back(name);
+}
+
+void RcppFunction::clearProtectionStack() {
+    UNPROTECT(numProtected);
+    numProtected = 0;
+}

Added: pkg/src/RcppFunction.h
===================================================================
--- pkg/src/RcppFunction.h	                        (rev 0)
+++ pkg/src/RcppFunction.h	2009-11-16 01:06:01 UTC (rev 172)
@@ -0,0 +1,50 @@
+// -*- mode: C++; c-indent-level: 4; c-basic-offset: 4; tab-width: 8 -*-
+//
+// RcppFunction.h: Rcpp R/C++ interface class library -- function 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 RcppFunction_h
+#define RcppFunction_h
+
+#include <RcppCommon.h>
+#include <RcppDate.h>
+#include <RcppDatetime.h>
+
+class RcppFunction {
+public:
+    RcppFunction(SEXP fn);
+    ~RcppFunction();
+    SEXP listCall();
+    SEXP vectorCall();
+    void setRVector(std::vector<double>& v);
+    void setRListSize(int size);
+    void appendToRList(std::string name, double value);
+    void appendToRList(std::string name, int value);
+    void appendToRList(std::string name, std::string value);
+    void appendToRList(std::string name, RcppDate& date);
+    void appendToRList(std::string name, RcppDatetime& datetime);
+    void clearProtectionStack();
+
+private:
+    SEXP fn, listArg, vectorArg;
+    int listSize, currListPosn, numProtected;
+    std::vector<std::string> names;
+};
+
+#endif

Added: pkg/src/RcppMatrix.cpp
===================================================================
--- pkg/src/RcppMatrix.cpp	                        (rev 0)
+++ pkg/src/RcppMatrix.cpp	2009-11-16 01:06:01 UTC (rev 172)
@@ -0,0 +1,124 @@
+// -*- mode: C++; c-indent-level: 4; c-basic-offset: 4; tab-width: 8 -*-
+//
+// RcppMatrix.h: Rcpp R/C++ interface class library -- templated matrix 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 <RcppMatrix.h>
+
+template <typename T>
+RcppMatrix<T>::RcppMatrix(SEXP mat) {
+
+    if (!Rf_isNumeric(mat) || !Rf_isMatrix(mat))
+	throw std::range_error("RcppMatrix: invalid numeric matrix in constructor");
+
+    // Get matrix dimensions
+    SEXP dimAttr = Rf_getAttrib(mat, R_DimSymbol);
+    dim1 = INTEGER(dimAttr)[0];
+    dim2 = INTEGER(dimAttr)[1];
+
+    // We guard against  the possibility that R might pass an integer matrix.
+    // Can be prevented using R code: temp <- as.double(a), dim(temp) <- dim(a)
+    int i,j;
+    int isInt = Rf_isInteger(mat);
+    T *m = (T *)R_alloc(dim1*dim2, sizeof(T));
+    a = (T **)R_alloc(dim1, sizeof(T *));
+    for (i = 0; i < dim1; i++)
+	a[i] = m + i*dim2;
+    if (isInt) {
+	for (i=0; i < dim1; i++)
+	    for (j=0; j < dim2; j++)
+		a[i][j] = (T)(INTEGER(mat)[i+dim1*j]);
+    }	
+    else {
+	for (i=0; i < dim1; i++)
+	    for (j=0; j < dim2; j++)
+		a[i][j] = (T)(REAL(mat)[i+dim1*j]);
+    }	
+}
+
+template <typename T>
+RcppMatrix<T>::RcppMatrix(int _dim1, int _dim2) {
+    dim1 = _dim1;
+    dim2 = _dim2;
+    int i,j;
+    T *m = (T *)R_alloc(dim1*dim2, sizeof(T));
+    a = (T **)R_alloc(dim1, sizeof(T *));
+    for (i = 0; i < dim1; i++)
+	a[i] = m + i*dim2;
+    for (i=0; i < dim1; i++)
+	for (j=0; j < dim2; j++)
+	    a[i][j] = 0;
+}
+
+template <typename T> int RcppMatrix<T>::getDim1() const { 
+    return dim1; 
+}
+
+template <typename T> int RcppMatrix<T>::getDim2() const { 
+    return dim2; 
+}
+
+template <typename T> int RcppMatrix<T>::rows() const { 
+    return dim1; 
+}
+
+template <typename T> int RcppMatrix<T>::cols() const { 
+    return dim2; 
+}
+
+template <typename T>
+T& RcppMatrix<T>::operator()(int i, int j) const {
+    if (i < 0 || i >= dim1 || j < 0 || j >= dim2) {
+	std::ostringstream oss;
+	oss << "RcppMatrix: subscripts out of range: " << i << ", " << j;
+	throw std::range_error(oss.str());
+    }
+    return a[i][j];
+}
+
+template <typename T>
+T **RcppMatrix<T>::cMatrix() {
+    int i,j;
+    T *m = (T *)R_alloc(dim1*dim2, sizeof(T));
+    T **tmp = (T **)R_alloc(dim1, sizeof(T *));
+    for (i = 0; i < dim1; i++)
+	tmp[i] = m + i*dim2;
+    for (i=0; i < dim1; i++)
+	for (j=0; j < dim2; j++)
+	    tmp[i][j] = a[i][j];
+    return tmp;
+}
+
+template <typename T>
+std::vector<std::vector<T> > RcppMatrix<T>::stlMatrix() {
+    int i,j;
+    std::vector<std::vector<T> > temp;
+    for (i = 0; i < dim1; i++) {
+	temp.push_back(std::vector<T>(dim2));
+    }
+    for (i = 0; i < dim1; i++)
+	for (j = 0; j < dim2; j++)
+	    temp[i][j] = a[i][j];
+    return temp;
+}
+
+// Explicit instantiation (required for external linkage)
+template class RcppMatrix<int>;
+template class RcppMatrix<double>;
+

Added: pkg/src/RcppMatrix.h
===================================================================
--- pkg/src/RcppMatrix.h	                        (rev 0)
+++ pkg/src/RcppMatrix.h	2009-11-16 01:06:01 UTC (rev 172)
@@ -0,0 +1,44 @@
+// -*- mode: C++; c-indent-level: 4; c-basic-offset: 4; tab-width: 8 -*-
+//
+// RcppMatrix.h: Rcpp R/C++ interface class library -- templated matrix 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 RcppMatrix_h
+#define RcppMatrix_h
+
+#include <RcppCommon.h>
+
+template <typename T>
+class RcppMatrix {
+public:
+    RcppMatrix(SEXP mat);
+    RcppMatrix(int nx, int ny);
+    int getDim1() const; 
+    int getDim2() const;
+    int rows() const;
+    int cols() const;
+    T& operator()(int i, int j) const;
+    T **cMatrix();
+    std::vector<std::vector<T> > stlMatrix();
+private:
+    int dim1, dim2;
+    T **a;
+};
+
+#endif

Added: pkg/src/RcppMatrixView.cpp
===================================================================
--- pkg/src/RcppMatrixView.cpp	                        (rev 0)
+++ pkg/src/RcppMatrixView.cpp	2009-11-16 01:06:01 UTC (rev 172)
@@ -0,0 +1,72 @@
+// -*- mode: C++; c-indent-level: 4; c-basic-offset: 4; tab-width: 8 -*-
+//
+// RcppMatrixView.cpp: Rcpp R/C++ interface class library -- templated matrix view
+//
+// 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 <RcppMatrixView.h>
+
+template <typename T>
+RcppMatrixView<T>::RcppMatrixView(SEXP mat) {
+    if (!Rf_isNumeric(mat) || !Rf_isMatrix(mat))
+	throw std::range_error("RcppMatrixView: invalid numeric matrix in constructor");
+    // Get matrix dimensions
+    SEXP dimAttr = Rf_getAttrib(mat, R_DimSymbol);
+    d1 = INTEGER(dimAttr)[0];
+    d2 = INTEGER(dimAttr)[1];
+    if (Rf_isInteger(mat)) {
+	a = (T *)(INTEGER(mat));
+    } else if (Rf_isReal(mat)) {
+	a = (T *)(REAL(mat));
+    }
+}
+
+template <typename T>
+inline int RcppMatrixView<T>::dim1() const { 
+    return d1; 
+}
+
+template <typename T>
+inline int RcppMatrixView<T>::dim2() const { 
+    return d2; 
+}
+
+template <typename T>
+inline int RcppMatrixView<T>::rows() const { 
+    return d1; 
+}
+
+template <typename T>
+inline int RcppMatrixView<T>::cols() const { 
+    return d2; 
+}
+
+template <typename T>
+inline T RcppMatrixView<T>::operator()(int i, int j) const {
+    if (i < 0 || i >= d1 || j < 0 || j >= d2) {
+	std::ostringstream oss;
+	oss << "RcppMatrixView: subscripts out of range: " << i << ", " << j;
+	throw std::range_error(oss.str());
+    }
+    return a[i + d1 * j];
+}
+
+template class RcppMatrixView<int>;
+template class RcppMatrixView<double>;
+
+

Added: pkg/src/RcppMatrixView.h
===================================================================
--- pkg/src/RcppMatrixView.h	                        (rev 0)
+++ pkg/src/RcppMatrixView.h	2009-11-16 01:06:01 UTC (rev 172)
@@ -0,0 +1,41 @@
+// -*- mode: C++; c-indent-level: 4; c-basic-offset: 4; tab-width: 8 -*-
+//
+// RcppMatrixView.h: Rcpp R/C++ interface class library -- templated matrix view
+//
+// 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 RcppMatrixView_h
+#define RcppMatrixView_h
+
+#include <RcppCommon.h>
+
+template <typename T>
+class RcppMatrixView {
+public:
+    RcppMatrixView(SEXP mat);
+    int dim1() const;
+    int dim2() const;
+    int rows() const;
+    int cols() const;
+    T operator()(int i, int j) const;
+private:
+    int d1, d2;
+    T *a;
+};
+
+#endif

Added: pkg/src/RcppNumList.cpp
===================================================================
--- pkg/src/RcppNumList.cpp	                        (rev 0)
+++ pkg/src/RcppNumList.cpp	2009-11-16 01:06:01 UTC (rev 172)
@@ -0,0 +1,59 @@
+// -*- mode: C++; c-indent-level: 4; c-basic-offset: 4; tab-width: 8 -*-
+//
+// RcppNumList.cpp: Rcpp R/C++ interface class library -- numlist 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 <RcppNumList.h>
+
+RcppNumList::RcppNumList(SEXP theList) {
+    if (!Rf_isNewList(theList))
+	throw std::range_error("RcppNumList: non-list passed to constructor");
+    len = Rf_length(theList);
+    names = Rf_getAttrib(theList, R_NamesSymbol);
+    namedList = theList;
+}
+
+std::string RcppNumList::getName(int i) const {
+    if (i < 0 || i >= len) {
+	std::ostringstream oss;
+	oss << "RcppNumList::getName: index out of bounds: " << i;
+	throw std::range_error(oss.str());
+    }
+    return std::string(CHAR(STRING_ELT(names,i)));
+}
+
+double RcppNumList::getValue(int i) const {
+    if (i < 0 || i >= len) {
+	std::ostringstream oss;
+	oss << "RcppNumList::getValue: index out of bounds: " << i;
+	throw std::range_error(oss.str());
+    }
+    SEXP elt = VECTOR_ELT(namedList, i);
+    if (Rf_isReal(elt))
+	return REAL(elt)[0];
+    else if (Rf_isInteger(elt))
+	return (double)INTEGER(elt)[0];
+    else
+	throw std::range_error("RcppNumList: contains non-numeric value");
+    return 0; // never get here
+}
+
+int RcppNumList::size() const { 
+    return len; 
+}

Added: pkg/src/RcppNumList.h
===================================================================
--- pkg/src/RcppNumList.h	                        (rev 0)
+++ pkg/src/RcppNumList.h	2009-11-16 01:06:01 UTC (rev 172)
@@ -0,0 +1,39 @@
+// -*- mode: C++; c-indent-level: 4; c-basic-offset: 4; tab-width: 8 -*-
+//
+// RcppNumList.h: Rcpp R/C++ interface class library -- numlist type
+//
+// 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 RcppNumList_h
+#define RcppNumList_h
+
+#include <RcppCommon.h>
+
+class RcppNumList {
+public:
+    RcppNumList(SEXP theList); 
+    std::string getName(int i) const;
+    double getValue(int i) const;
+    int size() const;
+private:
+    int len;
+    SEXP namedList;
+    SEXP names;
+};
+
+#endif

Added: pkg/src/RcppResultSet.cpp
===================================================================
--- pkg/src/RcppResultSet.cpp	                        (rev 0)
+++ pkg/src/RcppResultSet.cpp	2009-11-16 01:06:01 UTC (rev 172)
@@ -0,0 +1,361 @@
+// -*- mode: C++; c-indent-level: 4; c-basic-offset: 4; tab-width: 8 -*-
+//
+// RcppResultSet.cpp: Rcpp R/C++ interface class library -- Results back to R
+//
+// 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 <RcppResultSet.h>
+
+RcppResultSet::RcppResultSet() : numProtected(0) { }
+
+void RcppResultSet::add(std::string name, RcppDate& date) {
+    SEXP value = PROTECT(Rf_allocVector(REALSXP, 1));
+    numProtected++;
+    REAL(value)[0] = date.getJDN() - RcppDate::Jan1970Offset;
+    SEXP dateclass = PROTECT(Rf_allocVector(STRSXP,1));
+    numProtected++;
+    SET_STRING_ELT(dateclass, 0, Rf_mkChar("Date"));
+    Rf_setAttrib(value, R_ClassSymbol, dateclass); 
+    values.push_back(make_pair(name, value));
+}
+
+void RcppResultSet::add(std::string name, RcppDatetime& datetime) {
+    SEXP value = PROTECT(Rf_allocVector(REALSXP, 1));
+    numProtected++;
+    REAL(value)[0] = datetime.getFractionalTimestamp();
+    SEXP datetimeclass = PROTECT(Rf_allocVector(STRSXP,2));
+    numProtected++;
+    SET_STRING_ELT(datetimeclass, 0, Rf_mkChar("POSIXt"));
+    SET_STRING_ELT(datetimeclass, 1, Rf_mkChar("POSIXct"));
+    Rf_setAttrib(value, R_ClassSymbol, datetimeclass); 
+    values.push_back(make_pair(name, value));
+}
+
+void RcppResultSet::add(std::string name, double x) {
+    SEXP value = PROTECT(Rf_allocVector(REALSXP, 1));
+    numProtected++;
+    REAL(value)[0] = x;
+    values.push_back(make_pair(name, value));
+}
+
+void RcppResultSet::add(std::string name, int i) {
+    SEXP value = PROTECT(Rf_allocVector(INTSXP, 1));
+    numProtected++;
+    INTEGER(value)[0] = i;
+    values.push_back(make_pair(name, value));
+}
+
+void RcppResultSet::add(std::string name, std::string strvalue) {
+    SEXP value = PROTECT(Rf_allocVector(STRSXP, 1));
+    numProtected++;
+    SET_STRING_ELT(value, 0, Rf_mkChar(strvalue.c_str()));
+    values.push_back(make_pair(name, value));
+}
+
+void RcppResultSet::add(std::string name, double *vec, int len) {
+    if (vec == 0)
+	throw std::range_error("RcppResultSet::add: NULL double vector");
+    SEXP value = PROTECT(Rf_allocVector(REALSXP, len));
+    numProtected++;
+    for (int i = 0; i < len; i++)
+	REAL(value)[i] = vec[i];
+    values.push_back(make_pair(name, value));
+}
+
+void RcppResultSet::add(std::string name, RcppDateVector& datevec) {
+    SEXP value = PROTECT(Rf_allocVector(REALSXP, datevec.size()));
+    numProtected++;
+    for (int i = 0; i < datevec.size(); i++) {
+	REAL(value)[i] = datevec(i).getJDN() - RcppDate::Jan1970Offset;
+    }
+    SEXP dateclass = PROTECT(Rf_allocVector(STRSXP,1));
+    numProtected++;
+    SET_STRING_ELT(dateclass, 0, Rf_mkChar("Date"));
+    Rf_setAttrib(value, R_ClassSymbol, dateclass); 
+    values.push_back(make_pair(name, value));
+}
+
+void RcppResultSet::add(std::string name, RcppDatetimeVector &dtvec) {
+    SEXP value = PROTECT(Rf_allocVector(REALSXP, dtvec.size()));
+    numProtected++;
+    for (int i = 0; i < dtvec.size(); i++) {
+	REAL(value)[i] = dtvec(i).getFractionalTimestamp();
+    }
+    SEXP datetimeclass = PROTECT(Rf_allocVector(STRSXP,2));
+    numProtected++;
+    SET_STRING_ELT(datetimeclass, 0, Rf_mkChar("POSIXt"));
+    SET_STRING_ELT(datetimeclass, 1, Rf_mkChar("POSIXct"));
+    Rf_setAttrib(value, R_ClassSymbol, datetimeclass); 
+    values.push_back(make_pair(name, value));
+}
+
+void RcppResultSet::add(std::string name, RcppStringVector& stringvec) {
+    int len = (int)stringvec.size();
+    SEXP value = PROTECT(Rf_allocVector(STRSXP, len));
+    numProtected++;
+    for (int i = 0; i < len; i++)
+        SET_STRING_ELT(value, i, Rf_mkChar(stringvec(i).c_str()));
[TRUNCATED]

To get the complete diff run:
    svnlook diff /svnroot/rcpp -r 172


More information about the Rcpp-commits mailing list