[Rcpp-commits] r1724 - in pkg/Rcpp: . inst/include inst/include/Rcpp/vector src

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Fri Jun 25 11:07:43 CEST 2010


Author: romain
Date: 2010-06-25 11:07:42 +0200 (Fri, 25 Jun 2010)
New Revision: 1724

Added:
   pkg/Rcpp/inst/include/RcppList__backward.h
   pkg/Rcpp/inst/include/RcppResultSet__backward.h
Modified:
   pkg/Rcpp/DESCRIPTION
   pkg/Rcpp/inst/include/Rcpp.h
   pkg/Rcpp/inst/include/Rcpp/vector/Matrix.h
   pkg/Rcpp/inst/include/RcppList.h
   pkg/Rcpp/inst/include/RcppResultSet.h
   pkg/Rcpp/inst/include/RcppStringVector.h
   pkg/Rcpp/inst/include/RcppVector.h
   pkg/Rcpp/inst/include/RcppVectorView.h
   pkg/Rcpp/src/Date.cpp
   pkg/Rcpp/src/RcppFunction.cpp
   pkg/Rcpp/src/RcppList.cpp
   pkg/Rcpp/src/RcppResultSet.cpp
Log:
clear out some dust of the old api

Modified: pkg/Rcpp/DESCRIPTION
===================================================================
--- pkg/Rcpp/DESCRIPTION	2010-06-25 07:34:52 UTC (rev 1723)
+++ pkg/Rcpp/DESCRIPTION	2010-06-25 09:07:42 UTC (rev 1724)
@@ -1,6 +1,6 @@
 Package: Rcpp
 Title: Rcpp R/C++ interface package
-Version: 0.8.2.12
+Version: 0.8.2.13
 Date: $Date$
 Author: Dirk Eddelbuettel and Romain Francois, with contributions 
  by Simon Urbanek, David Reiss and Douglas Bates; based on code written during 

Modified: pkg/Rcpp/inst/include/Rcpp/vector/Matrix.h
===================================================================
--- pkg/Rcpp/inst/include/Rcpp/vector/Matrix.h	2010-06-25 07:34:52 UTC (rev 1723)
+++ pkg/Rcpp/inst/include/Rcpp/vector/Matrix.h	2010-06-25 09:07:42 UTC (rev 1724)
@@ -46,6 +46,12 @@
 	}
 	
 	Matrix( const int& nrows, const int& ncols) : VECTOR( Dimension( nrows, ncols ) ) {}
+	
+	template <typename Iterator>
+	Matrix( const int& nrows, const int& ncols, Iterator start ) : VECTOR( start, start + (nrows*ncols) ) {
+		VECTOR::attr( "dim" ) = Dimension( nrows, ncols ) ; 
+	}
+	
 	Matrix( const int& n) : VECTOR( Dimension( n, n ) ) {}
 	
 	Matrix( const Matrix& other) throw(not_compatible) : VECTOR() {

Modified: pkg/Rcpp/inst/include/Rcpp.h
===================================================================
--- pkg/Rcpp/inst/include/Rcpp.h	2010-06-25 07:34:52 UTC (rev 1723)
+++ pkg/Rcpp/inst/include/Rcpp.h	2010-06-25 09:07:42 UTC (rev 1724)
@@ -80,4 +80,6 @@
 #include <Rcpp/InternalFunction.h>
 #include <Rcpp/sugar/sugar.h>
 
+#include <RcppResultSet__backward.h>
+
 #endif

Modified: pkg/Rcpp/inst/include/RcppList.h
===================================================================
--- pkg/Rcpp/inst/include/RcppList.h	2010-06-25 07:34:52 UTC (rev 1723)
+++ pkg/Rcpp/inst/include/RcppList.h	2010-06-25 09:07:42 UTC (rev 1724)
@@ -31,15 +31,14 @@
     RcppList(void);
     ~RcppList();
     void setSize(int size);
-    void append(std::string name, double value);
-    void append(std::string name, int value);
-    void append(std::string name, std::string value);
-    //void append(std::string name, RcppDate& date);
-    //void append(std::string name, RcppDatetime& datetime);
-    void append(std::string name, SEXP sexp);
+    
+    // defined later because it needs wrap
+    template <typename T>
+    void append( const std::string& name, const T& value ) throw(std::range_error) ;
+    
     void clearProtectionStack();
     SEXP getList(void) const;
-
+    
 protected:
     friend class RcppResultSet;
 
@@ -49,4 +48,10 @@
     std::vector<std::string> names;
 };
 
+namespace Rcpp{
+	template<> inline SEXP wrap<RcppList>( const RcppList& x ){
+		return x.getList( ) ;	
+	}
+}
+
 #endif

Added: pkg/Rcpp/inst/include/RcppList__backward.h
===================================================================
--- pkg/Rcpp/inst/include/RcppList__backward.h	                        (rev 0)
+++ pkg/Rcpp/inst/include/RcppList__backward.h	2010-06-25 09:07:42 UTC (rev 1724)
@@ -0,0 +1,34 @@
+// -*- mode: C++; c-indent-level: 4; c-basic-offset: 4; tab-width: 8 -*-
+//
+// RcppList__backward.h: Rcpp R/C++ interface class library -- 
+//
+// 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 RcppList__backward_h
+#define RcppList__backward_h
+
+template <typename T>
+void RcppList::append( const std::string& name, const T& value ) throw(std::range_error) {
+	if (currListPosn < 0 || currListPosn >= listSize)
+	throw std::range_error("RcppList::append(): list posn out of range");
+	
+	SET_VECTOR_ELT(listArg, currListPosn++, Rcpp::wrap(value) );
+    names.push_back(name);
+}
+
+#endif

Modified: pkg/Rcpp/inst/include/RcppResultSet.h
===================================================================
--- pkg/Rcpp/inst/include/RcppResultSet.h	2010-06-25 07:34:52 UTC (rev 1723)
+++ pkg/Rcpp/inst/include/RcppResultSet.h	2010-06-25 09:07:42 UTC (rev 1724)
@@ -48,6 +48,9 @@
 
 class RcppResultSet {
 public:
+	typedef std::pair<const std::string,SEXP> PAIR ;
+	typedef std::list<PAIR> LIST ; 
+	
     RcppResultSet();
     void add(std::string, double);
     void add(std::string, int);
@@ -77,7 +80,19 @@
     SEXP getSEXP();
 protected:
     int numProtected;
-    std::list<std::pair<std::string,SEXP> > values;
+    LIST values;
+    
+private:
+	
+	// defined later because it needs wrap
+	template <typename T> 
+	void add__impl( const std::string& name, const T& t ) ;
+	
+	template <typename T> 
+	void add__matrix( const std::string& name, T**, int nx, int ny ) ;
+	
+	template <typename T> 
+	void add__matrix__std( const std::string& name, const std::vector<std::vector<T> >& mat ) throw(std::range_error) ;
 };
 
 #endif

Added: pkg/Rcpp/inst/include/RcppResultSet__backward.h
===================================================================
--- pkg/Rcpp/inst/include/RcppResultSet__backward.h	                        (rev 0)
+++ pkg/Rcpp/inst/include/RcppResultSet__backward.h	2010-06-25 09:07:42 UTC (rev 1724)
@@ -0,0 +1,56 @@
+// -*- mode: C++; c-indent-level: 4; c-basic-offset: 4; tab-width: 8 -*-
+//
+// RcppResultSet.h: Rcpp R/C++ interface class library -- Results back to R
+//
+// 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 RcppResultSet__backward_h
+#define RcppResultSet__backward_h
+
+template <typename T> 
+void RcppResultSet::add__impl( const std::string& name, const T& t ){
+	values.push_back(make_pair(name, Rcpp::wrap(t)));	
+}
+
+template <typename T> 
+void RcppResultSet::add__matrix( const std::string& name, T** input, int nx, int ny ){
+	Rcpp::Matrix< Rcpp::traits::r_sexptype_traits<T>::rtype > mat( nx, ny ) ;
+	for (int i = 0; i < nx; i++)
+	for (int j = 0; j < ny; j++)
+	    mat[i + nx*j] = input[i][j];
+    values.push_back( make_pair(name, mat.asSexp() ));
+}
+
+template <typename T> 
+void RcppResultSet::add__matrix__std( const std::string& name, const std::vector<std::vector<T> >& mat ) throw(std::range_error) {
+    int nx = (int)mat.size();
+    if (nx == 0)
+	throw std::range_error("RcppResultSet::add: zero length vector<vector<> >");
+    
+	int ny = (int)mat[0].size();
+    if (ny == 0)
+	throw std::range_error("RcppResultSet::add: no columns in vector<vector<> >");
+
+	Rcpp::Matrix< Rcpp::traits::r_sexptype_traits<T>::rtype > out( nx, ny ) ;
+	for (int i = 0; i < nx; i++)
+	for (int j = 0; j < ny; j++)
+	    out[i + nx*j] = mat[i][j];
+    values.push_back(make_pair( name, out.asSexp() ));
+}
+
+#endif

Modified: pkg/Rcpp/inst/include/RcppStringVector.h
===================================================================
--- pkg/Rcpp/inst/include/RcppStringVector.h	2010-06-25 07:34:52 UTC (rev 1723)
+++ pkg/Rcpp/inst/include/RcppStringVector.h	2010-06-25 09:07:42 UTC (rev 1724)
@@ -4,6 +4,7 @@
 //
 // Copyright (C) 2005 - 2006 Dominick Samperi
 // Copyright (C) 2008 - 2009 Dirk Eddelbuettel
+// Copyright (C) 2010	     Dirk Eddelbuettel and Romain Francois
 //
 // This file is part of Rcpp.
 //
@@ -27,11 +28,20 @@
 
 class RcppStringVector {
 public:
+	typedef std::string* iterator ;
+	typedef const std::string* const_iterator ;
+	
     RcppStringVector(SEXP vec);
     ~RcppStringVector();
     std::string& operator()(int i) const;
     int size() const;
     std::vector<std::string> stlVector() const;
+    
+    inline const_iterator begin() const { return v ; }
+    inline const_iterator end() const { return v + length ; }
+    inline iterator begin() { return v ; }
+    inline iterator end() { return v + length ; }
+    
 private:
     std::string *v;
     int length;

Modified: pkg/Rcpp/inst/include/RcppVector.h
===================================================================
--- pkg/Rcpp/inst/include/RcppVector.h	2010-06-25 07:34:52 UTC (rev 1723)
+++ pkg/Rcpp/inst/include/RcppVector.h	2010-06-25 09:07:42 UTC (rev 1724)
@@ -4,6 +4,7 @@
 //
 // Copyright (C) 2005 - 2006 Dominick Samperi
 // Copyright (C) 2008 - 2009 Dirk Eddelbuettel
+// Copyright (C) 2010	Dirk Eddelbuettel and Romain Francois
 //
 // This file is part of Rcpp.
 //
@@ -28,12 +29,22 @@
 template <typename T>
 class RcppVector {
 public:
+	typedef T* iterator ;
+	typedef const T* const_iterator ;
+	
     RcppVector(SEXP vec);
     RcppVector(int len);
     int size() const;
     T& operator()(int i) const; 
     T *cVector() const;
     std::vector<T> stlVector() const;
+    
+    inline const_iterator begin() const { return v ; }
+    inline const_iterator end() const { return v + len ; }
+    
+    inline iterator begin(){ return v ; }
+    inline iterator end(){ return v + len ; }
+    
 private:
     int len;
     T *v;

Modified: pkg/Rcpp/inst/include/RcppVectorView.h
===================================================================
--- pkg/Rcpp/inst/include/RcppVectorView.h	2010-06-25 07:34:52 UTC (rev 1723)
+++ pkg/Rcpp/inst/include/RcppVectorView.h	2010-06-25 09:07:42 UTC (rev 1724)
@@ -28,9 +28,15 @@
 template <typename T>
 class RcppVectorView {
 public:
+	typedef T* iterator ;
+	
     RcppVectorView(SEXP vec);
     int size() const;
     T operator()(int i) const;
+    
+    inline iterator begin() { return v ; }
+    inline iterator end(){ return v + len ; }
+    
 private:
     int len;
     T *v;

Modified: pkg/Rcpp/src/Date.cpp
===================================================================
--- pkg/Rcpp/src/Date.cpp	2010-06-25 07:34:52 UTC (rev 1723)
+++ pkg/Rcpp/src/Date.cpp	2010-06-25 09:07:42 UTC (rev 1724)
@@ -146,11 +146,7 @@
     bool  operator!=(const Date &d1, const Date& d2) { return d1.m_d != d2.m_d; }
 
     template <> SEXP wrap(const Date &date) {
-	SEXP value = PROTECT(Rf_allocVector(REALSXP, 1));
-	REAL(value)[0] = date.getDate();
-	Rf_setAttrib(value, R_ClassSymbol, Rf_mkString("Date") ); 
-	UNPROTECT(1);
-	return value;
+    return internal::new_date_object( date.getDate() ) ;
     }
 
 }

Modified: pkg/Rcpp/src/RcppFunction.cpp
===================================================================
--- pkg/Rcpp/src/RcppFunction.cpp	2010-06-25 07:34:52 UTC (rev 1723)
+++ pkg/Rcpp/src/RcppFunction.cpp	2010-06-25 09:07:42 UTC (rev 1724)
@@ -20,7 +20,7 @@
 // You should have received a copy of the GNU General Public License
 // along with Rcpp.  If not, see <http://www.gnu.org/licenses/>.
 
-#include <RcppFunction.h>
+#include <Rcpp.h>
 
 RcppFunction::RcppFunction(SEXP fn_) : fn(fn_) { 
     if (!Rf_isFunction(fn))
@@ -68,10 +68,8 @@
 }
 
 void RcppFunction::setRVector(std::vector<double>& v) {
-    vectorArg = PROTECT(Rf_allocVector(REALSXP,v.size()));
+    vectorArg = PROTECT( Rcpp::wrap( v ) );
     numProtected++;
-    for (int i=0; i < (int)v.size(); i++)
-	REAL(vectorArg)[i] = v[i];
 }
 
 void RcppFunction::setRListSize(int n) {
@@ -83,53 +81,54 @@
 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);
+    SEXP valsxp = PROTECT( Rf_ScalarReal( value ) ) ;
+	numProtected++;
+    // FIXME: valsxp does not need to be protected anymore
+    //        since it is protected by listArg
+	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));
+    SEXP valsxp = PROTECT(Rf_ScalarInteger(value));
     numProtected++;
-    INTEGER(valsxp)[0] = value;
-    SET_VECTOR_ELT(listArg, currListPosn++, valsxp);
+    // FIXME: valsxp does not need to be protected anymore
+    //        since it is protected by listArg
+	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));
+    SEXP valsxp = PROTECT(Rf_mkString(value.c_str()));
     numProtected++;
-    SET_STRING_ELT(valsxp, 0, Rf_mkChar(value.c_str()));
-    SET_VECTOR_ELT(listArg, currListPosn++, valsxp);
+    // FIXME: valsxp does not need to be protected anymore
+    //        since it is protected by listArg
+	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));
+	SEXP valsxp = PROTECT(Rcpp::wrap(date));
     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);
+    // FIXME: valsxp does not need to be protected anymore
+    //        since it is protected by listArg
+	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_ScalarReal(datetime.getFractionalTimestamp()));
+	SEXP valsxp = PROTECT(Rcpp::wrap(datetime));
     numProtected++;
-    Rf_setAttrib(valsxp, R_ClassSymbol, Rcpp::internal::getPosixClasses() );
+    // FIXME: valsxp does not need to be protected anymore
+    //        since it is protected by listArg
     SET_VECTOR_ELT(listArg, currListPosn++, valsxp);
     names.push_back(name);
 }

Modified: pkg/Rcpp/src/RcppList.cpp
===================================================================
--- pkg/Rcpp/src/RcppList.cpp	2010-06-25 07:34:52 UTC (rev 1723)
+++ pkg/Rcpp/src/RcppList.cpp	2010-06-25 09:07:42 UTC (rev 1724)
@@ -28,66 +28,25 @@
 }
 
 RcppList::~RcppList(void) {
-    UNPROTECT(numProtected);
+    clearProtectionStack() ;
 }
 
 void RcppList::setSize(int n) {
+	// FIXME: this should clear the protection stack
     listSize = n;
     listArg = PROTECT(Rf_allocVector(VECSXP, n));
     numProtected++;
 }
 
-void RcppList::append(std::string name, double value) {
-    if (currListPosn < 0 || currListPosn >= listSize)
-	throw std::range_error("RcppList::append(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 RcppList::append(std::string name, int value) {
-    if (currListPosn < 0 || currListPosn >= listSize)
-	throw std::range_error("RcppList::append(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 RcppList::append(std::string name, std::string value) {
-    if (currListPosn < 0 || currListPosn >= listSize)
-	throw std::range_error("RcppList::append(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 RcppList::append(std::string name, SEXP sexp) {
-    if (currListPosn < 0 || currListPosn >= listSize)
-	throw std::range_error("RcppList::append(sexp): posn out of range");
-    SET_VECTOR_ELT(listArg, currListPosn++, sexp);
-    names.push_back(name);
-}
-
 void RcppList::clearProtectionStack() {
     UNPROTECT(numProtected);
     numProtected = 0;
 }
 
 SEXP RcppList::getList(void) const { 
-    SEXP li = PROTECT(Rf_allocVector(VECSXP, listSize));
-    SEXP nm = PROTECT(Rf_allocVector(STRSXP, listSize));
-    for (int i=0; i<listSize; i++) {
-	SET_VECTOR_ELT(li, i, VECTOR_ELT(listArg, i));
-	SET_STRING_ELT(nm, i, Rf_mkChar(names[i].c_str()));
-    }
-    Rf_setAttrib(li, R_NamesSymbol, nm);
-    UNPROTECT(2);
+    SEXP li = PROTECT(Rf_duplicate( listArg )) ;
+    Rf_setAttrib(li, R_NamesSymbol, Rcpp::wrap(names) );
+    UNPROTECT(1) ;
     return li; 
 }
 

Modified: pkg/Rcpp/src/RcppResultSet.cpp
===================================================================
--- pkg/Rcpp/src/RcppResultSet.cpp	2010-06-25 07:34:52 UTC (rev 1723)
+++ pkg/Rcpp/src/RcppResultSet.cpp	2010-06-25 09:07:42 UTC (rev 1724)
@@ -21,7 +21,7 @@
 // You should have received a copy of the GNU General Public License
 // along with Rcpp.  If not, see <http://www.gnu.org/licenses/>.
 
-#include <RcppResultSet.h>
+#include <Rcpp.h>
 
 RcppResultSet::RcppResultSet() : numProtected(0) { }
 
@@ -61,200 +61,115 @@
 }
 
 void RcppResultSet::add(std::string name, RcppDate& date) {
-    values.push_back(make_pair(name, Rcpp::wrap(date)));
+    add__impl( name, date );
 }
 
 void RcppResultSet::add(std::string name, RcppDatetime& datetime) {
-    values.push_back(make_pair(name, Rcpp::wrap(datetime)));
+    add__impl( name, datetime );
 }
 
 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));
+    add__impl( name, x );
 }
 
 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));
+    add__impl( name, i );
 }
 
 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));
+    add__impl( name, strvalue );
 }
 
 void RcppResultSet::add(std::string name, double *vec, int len) {
-    if (vec == 0)
+	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));
+	add__impl( name, Rcpp::wrap( vec, vec + len) );
 }
 
 void RcppResultSet::add(std::string name, RcppDateVector& datevec) {
-    values.push_back(make_pair(name, Rcpp::wrap(datevec)));
+    add__impl( name, datevec ) ;
 }
 
 void RcppResultSet::add(std::string name, RcppDatetimeVector &dtvec) {
-    values.push_back(make_pair(name, Rcpp::wrap(dtvec)));
+    add__impl( name, dtvec ) ;
 }
 
 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()));
-    values.push_back(make_pair(name, value));
+    add__impl( name, stringvec ) ;
 }
 
 void RcppResultSet::add(std::string name, int *vec, int len) {
-    if (vec == 0)
+	if (vec == 0)
 	throw std::range_error("RcppResultSet::add: NULL int vector");
-    SEXP value = PROTECT(Rf_allocVector(INTSXP, len));
-    numProtected++;
-    for (int i = 0; i < len; i++)
-	INTEGER(value)[i] = vec[i];
-    values.push_back(make_pair(name, value));
+	add__impl( name, Rcpp::wrap( vec, vec + len) );
 }
 
 void RcppResultSet::add(std::string name, double **mat, int nx, int ny) {
     if (mat == 0)
 	throw std::range_error("RcppResultSet::add: NULL double matrix");
-    SEXP value = PROTECT(Rf_allocMatrix(REALSXP, nx, ny));
-    numProtected++;
-    for (int i = 0; i < nx; i++)
-	for (int j = 0; j < ny; j++)
-	    REAL(value)[i + nx*j] = mat[i][j];
-    values.push_back(make_pair(name, value));
+	add__matrix( name, mat, nx, ny ) ;
 }
 
 void RcppResultSet::add(std::string name, int **mat, int nx, int ny) {
     if (mat == 0)
 	throw std::range_error("RcppResultSet::add: NULL int matrix");
-    SEXP value = PROTECT(Rf_allocMatrix(INTSXP, nx, ny));
-    numProtected++;
-    for (int i = 0; i < nx; i++)
-	for (int j = 0; j < ny; j++)
-	    INTEGER(value)[i + nx*j] = mat[i][j];
-    values.push_back(make_pair(name, value));
+	add__matrix( name, mat, nx, ny ) ;
 }
 
 void RcppResultSet::add(std::string name, std::vector<std::string>& vec) {
     if (vec.size() == 0)
 	throw std::range_error("RcppResultSet::add; zero length vector<string>");
-    int len = (int)vec.size();
-    SEXP value = PROTECT(Rf_allocVector(STRSXP, len));
-    numProtected++;
-    for (int i = 0; i < len; i++)
-        SET_STRING_ELT(value, i, Rf_mkChar(vec[i].c_str()));
-    values.push_back(make_pair(name, value));
+	add__impl( name, vec ) ;   
 }
 
 void RcppResultSet::add(std::string name, std::vector<int>& vec) {
     if (vec.size() == 0)
 	throw std::range_error("RcppResultSet::add; zero length vector<int>");
-    int len = (int)vec.size();
-    SEXP value = PROTECT(Rf_allocVector(INTSXP, len));
-    numProtected++;
-    for (int i = 0; i < len; i++)
-	INTEGER(value)[i] = vec[i];
-    values.push_back(make_pair(name, value));
+	add__impl( name, vec ) ;   
 }
 
 void RcppResultSet::add(std::string name, std::vector<double>& vec) {
     if (vec.size() == 0)
 	throw std::range_error("RcppResultSet::add; zero length vector<double>");
-    int len = (int)vec.size();
-    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));
+	add__impl( name, vec ) ;   
 }
 
 void RcppResultSet::add(std::string name, std::vector<std::vector<int> >& mat) {
-    if (mat.size() == 0)
-	throw std::range_error("RcppResultSet::add: zero length vector<vector<int> >");
-    else if (mat[0].size() == 0)
-	throw std::range_error("RcppResultSet::add: no columns in vector<vector<int> >");
-    int nx = (int)mat.size();
-    int ny = (int)mat[0].size();
-    SEXP value = PROTECT(Rf_allocMatrix(INTSXP, nx, ny));
-    numProtected++;
-    for (int i = 0; i < nx; i++)
-	for (int j = 0; j < ny; j++)
-	    INTEGER(value)[i + nx*j] = mat[i][j];
-    values.push_back(make_pair(name, value));
+	add__matrix__std( name, mat ) ;
 }
 
 void RcppResultSet::add(std::string name, std::vector<std::vector<double> >& mat) {
-    if (mat.size() == 0)
-	throw std::range_error("RcppResultSet::add: zero length vector<vector<double> >");
-    else if (mat[0].size() == 0)
-	throw std::range_error("RcppResultSet::add: no columns in vector<vector<double> >");
-    int nx = (int)mat.size();
-    int ny = (int)mat[0].size();
-    SEXP value = PROTECT(Rf_allocMatrix(REALSXP, nx, ny));
-    numProtected++;
-    for (int i = 0; i < nx; i++)
-	for (int j = 0; j < ny; j++)
-	    REAL(value)[i + nx*j] = mat[i][j];
-    values.push_back(make_pair(name, value));
+	add__matrix__std( name, mat ) ;
 }
 
 void RcppResultSet::add(std::string name, RcppVector<int>& vec) {
-    int len = vec.size();
-    int *a = vec.cVector();
-    SEXP value = PROTECT(Rf_allocVector(INTSXP, len));
-    numProtected++;
-    for (int i = 0; i < len; i++)
-	INTEGER(value)[i] = a[i];
-    values.push_back(make_pair(name, value));
+	add__impl( name, vec ) ;
 }
 
 void RcppResultSet::add(std::string name, RcppVector<double>& vec) {
-    int len = vec.size();
-    double *a = vec.cVector();
-    SEXP value = PROTECT(Rf_allocVector(REALSXP, len));
-    numProtected++;
-    for (int i = 0; i < len; i++)
-	REAL(value)[i] = a[i];
-    values.push_back(make_pair(name, value));
+	add__impl( name, vec ) ;
 }
 
 void RcppResultSet::add(std::string name, RcppMatrix<int>& mat) {
-    int nx = mat.getDim1();
-    int ny = mat.getDim2();
-    int **a = mat.cMatrix();
-    SEXP value = PROTECT(Rf_allocMatrix(INTSXP, nx, ny));
-    numProtected++;
-    for (int i = 0; i < nx; i++)
-	for (int j = 0; j < ny; j++)
-	    INTEGER(value)[i + nx*j] = a[i][j];
-    values.push_back(make_pair(name, value));
+    add__matrix( name, mat.cMatrix(), mat.getDim1(), mat.getDim2() ) ;
 }
 
 void RcppResultSet::add(std::string name, RcppMatrix<double>& mat) {
-    int nx = mat.getDim1();
-    int ny = mat.getDim2();
-    double **a = mat.cMatrix();
-    SEXP value = PROTECT(Rf_allocMatrix(REALSXP, nx, ny));
-    numProtected++;
-    for (int i = 0; i < nx; i++)
-	for (int j = 0; j < ny; j++)
-	    REAL(value)[i + nx*j] = a[i][j];
-    values.push_back(make_pair(name, value));
+    add__matrix( name, mat.cMatrix(), mat.getDim1(), mat.getDim2() ) ;
 }
 
+
+void RcppResultSet::add(std::string name, RcppList &list) {
+	add__impl( name, list ) ;
+}
+
+void RcppResultSet::add(std::string name, SEXP sexp, bool isProtected) {
+    values.push_back(make_pair(name, sexp));
+    if (isProtected)
+	numProtected++;
+}
+
+// FIXME: this code belongs to RcppFrame::operator SEXP
 void RcppResultSet::add(std::string name, RcppFrame& frame) {
     std::vector<std::string> colNames = frame.getColNames();
     std::vector<std::vector<ColDatum> > table = frame.getTableData();
@@ -333,28 +248,9 @@
     values.push_back(make_pair(name, rl));
 }
 
-void RcppResultSet::add(std::string name, RcppList &list) {
-    // we let RcppList export itself as a SEXP and send it along
-    values.push_back(make_pair(name, list.getList()));
-}
-
-void RcppResultSet::add(std::string name, SEXP sexp, bool isProtected) {
-    values.push_back(make_pair(name, sexp));
-    if (isProtected)
-	numProtected++;
-}
-
 SEXP RcppResultSet::getReturnList() {
-    int nret = (int)values.size();
-    SEXP rl = PROTECT(Rf_allocVector(VECSXP,nret));
-    SEXP nm = PROTECT(Rf_allocVector(STRSXP,nret));
-    std::list<std::pair<std::string,SEXP> >::iterator iter = values.begin();
-    for (int i = 0; iter != values.end(); iter++, i++) {
-	SET_VECTOR_ELT(rl, i, iter->second);
-	SET_STRING_ELT(nm, i, Rf_mkChar(iter->first.c_str()));
-    }
-    Rf_setAttrib(rl, R_NamesSymbol, nm);
-    UNPROTECT(numProtected+2);
+    SEXP rl = PROTECT( Rcpp::wrap( values ) ) ;
+    UNPROTECT(numProtected+1);
     return rl;
 }
 
@@ -362,6 +258,8 @@
     if (values.size() != 1) {
 	throw std::range_error("RcppResultSet::getSEXP only sensible for single return arguments");
     }
+    // FIXME: that looks soooo wrong
+    //        is this ever used ?
     SEXP val = values.begin()->second;
     UNPROTECT(numProtected);
     return val;



More information about the Rcpp-commits mailing list