[Rcpp-commits] r168 - pkg/src

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Mon Nov 9 22:29:49 CET 2009


Author: edd
Date: 2009-11-09 22:29:48 +0100 (Mon, 09 Nov 2009)
New Revision: 168

Added:
   pkg/src/RcppParams.cpp
   pkg/src/RcppParams.h
Modified:
   pkg/src/Rcpp.cpp
   pkg/src/Rcpp.h
Log:
split off RcppParams as well

Modified: pkg/src/Rcpp.cpp
===================================================================
--- pkg/src/Rcpp.cpp	2009-11-09 17:46:44 UTC (rev 167)
+++ pkg/src/Rcpp.cpp	2009-11-09 21:29:48 UTC (rev 168)
@@ -21,41 +21,6 @@
 
 #include <Rcpp.h>
 
-RcppParams::RcppParams(SEXP params) {
-    if (!Rf_isNewList(params))
-	throw std::range_error("RcppParams: non-list passed to constructor");
-    int len = Rf_length(params);
-    SEXP names = Rf_getAttrib(params, R_NamesSymbol);
-    if (names == R_NilValue)
-	throw std::range_error("RcppParams: list must have named elements");
-    for (int i = 0; i < len; i++) {
-	std::string nm = std::string(CHAR(STRING_ELT(names,i)));
-	if (nm.size() == 0)
-	    throw std::range_error("RcppParams: all list elements must be named");
-	pmap[nm] = i;
-    }
-    _params = params;
-}
-
-void RcppParams::checkNames(char *inputNames[], int len) {
-    for (int i = 0; i < len; i++) {
-	std::map<std::string,int>::iterator iter = pmap.find(inputNames[i]);
-	if (iter == pmap.end()) {
-	    std::string mesg = "RcppParams::checkNames: missing required parameter ";
-	    throw std::range_error(mesg+inputNames[i]);
-	}
-    }
-}
-
-bool RcppParams::exists(std::string name) {
-    bool rc = true;
-    std::map<std::string,int>::iterator iter = pmap.find(name);
-    if (iter == pmap.end()) {
-	rc = false;
-    }
-    return rc;
-}
-
 RcppFrame::RcppFrame(SEXP df) {
     if (!Rf_isNewList(df))
 	throw std::range_error("RcppFrame::RcppFrame: invalid data frame.");
@@ -129,131 +94,6 @@
     }
 }
 
-double RcppParams::getDoubleValue(std::string name) {
-    std::map<std::string,int>::iterator iter = pmap.find(name);
-    if (iter == pmap.end()) {
-	std::string mesg = "RcppParams::getDoubleValue: no such name: ";
-	throw std::range_error(mesg+name);
-    }
-    int posn = iter->second;
-    SEXP elt = VECTOR_ELT(_params,posn);
-    if (!Rf_isNumeric(elt) || Rf_length(elt) != 1) {
-	std::string mesg = "RcppParams::getDoubleValue: must be scalar ";
-	throw std::range_error(mesg+name);
-    }
-    if (Rf_isInteger(elt))
-	return (double)INTEGER(elt)[0];
-    else if (Rf_isReal(elt))
-	return REAL(elt)[0];
-    else {
-	std::string mesg = "RcppParams::getDoubleValue: invalid value for ";
-	throw std::range_error(mesg+name);
-    }
-    return 0; // never get here
-}
-
-int RcppParams::getIntValue(std::string name) {
-    std::map<std::string,int>::iterator iter = pmap.find(name);
-    if (iter == pmap.end()) {
-	std::string mesg = "RcppParams::getIntValue: no such name: ";
-	throw std::range_error(mesg+name);
-    }
-    int posn = iter->second;
-    SEXP elt = VECTOR_ELT(_params,posn);
-    if (!Rf_isNumeric(elt) || Rf_length(elt) != 1) {
-	std::string mesg = "RcppParams::getIntValue: must be scalar: ";
-	throw std::range_error(mesg+name);
-    }
-    if (Rf_isInteger(elt))
-	return INTEGER(elt)[0];
-    else if (Rf_isReal(elt))
-	return (int)REAL(elt)[0];
-    else {
-	std::string mesg = "RcppParams::getIntValue: invalid value for: ";
-	throw std::range_error(mesg+name);
-    }
-    return 0; // never get here
-}
-
-bool RcppParams::getBoolValue(std::string name) {
-    std::map<std::string,int>::iterator iter = pmap.find(name);
-    if (iter == pmap.end()) {
-	std::string mesg = "RcppParams::getBoolValue: no such name: ";
-	throw std::range_error(mesg+name);
-    }
-    int posn = iter->second;
-    SEXP elt = VECTOR_ELT(_params,posn);
-    if (Rf_isLogical(elt))
-	return INTEGER(elt)[0];
-    else {
-	std::string mesg = "RcppParams::getBoolValue: invalid value for: ";
-	throw std::range_error(mesg+name);
-    }
-    return false; // never get here
-}
-
-std::string RcppParams::getStringValue(std::string name) {
-    std::map<std::string,int>::iterator iter = pmap.find(name);
-    if (iter == pmap.end()) {
-	std::string mesg = "RcppParams::getStringValue: no such name: ";
-	throw std::range_error(mesg+name);
-    }
-    int posn = iter->second;
-    SEXP elt = VECTOR_ELT(_params,posn);
-    if (Rf_isString(elt))
-		return std::string(CHAR(STRING_ELT(elt,0)));
-    else {
-	std::string mesg = "RcppParams::getStringValue: invalid value for: ";
-	throw std::range_error(mesg+name);
-    }
-    return ""; // never get here
-}
-
-RcppDate RcppParams::getDateValue(std::string name) {
-    std::map<std::string,int>::iterator iter = pmap.find(name);
-    if (iter == pmap.end()) {
-	std::string mesg = "RcppParams::getDateValue: no such name: ";
-	throw std::range_error(mesg+name);
-    }
-    int posn = iter->second;
-    SEXP elt = VECTOR_ELT(_params,posn);
-    if (!Rf_isNumeric(elt) || Rf_length(elt) != 1) {
-	std::string mesg = "RcppParams::getDateValue: invalide date: ";
-	throw std::range_error(mesg+name);
-    }
-
-    int d;
-    if (Rf_isReal(elt)) // R stores julian value in a double.
-	d = (int)REAL(elt)[0];
-    else {
-	std::string mesg = "RcppParams::getDateValue: invalid value for: ";
-	throw std::range_error(mesg+name);
-    }
-    return RcppDate(d);
-}
-
-RcppDatetime RcppParams::getDatetimeValue(std::string name) {
-    std::map<std::string,int>::iterator iter = pmap.find(name);
-    if (iter == pmap.end()) {
-        std::string mesg = "RcppParams::getDatetimeValue: no such name: ";
-	throw std::range_error(mesg+name);
-    }
-    int posn = iter->second;
-    SEXP elt = VECTOR_ELT(_params, posn);
-    if (!Rf_isNumeric(elt) || Rf_length(elt) != 1) {
-	std::string mesg = "RcppParams::getDateValue: invalide date: ";
-	throw std::range_error(mesg+name);
-    }
-    double d;
-    if (Rf_isReal(elt)) 	// R stores POSIXt as a double
-	d = REAL(elt)[0];
-    else {
-	std::string mesg = "RcppParams::getDatetimeValue: invalid value for: ";
-	throw std::range_error(mesg+name);
-    }
-    return RcppDatetime(d);
-}
-
 RcppDateVector::RcppDateVector(SEXP vec) {
     int i;
     if (!Rf_isNumeric(vec) || Rf_isMatrix(vec) || Rf_isLogical(vec))

Modified: pkg/src/Rcpp.h
===================================================================
--- pkg/src/Rcpp.h	2009-11-09 17:46:44 UTC (rev 167)
+++ pkg/src/Rcpp.h	2009-11-09 21:29:48 UTC (rev 168)
@@ -26,23 +26,8 @@
 #include <RcppDate.h>
 #include <RcppDatetime.h>
 #include <RcppList.h>
+#include <RcppParams.h>
 
-class RcppParams {
-public:
-    RcppParams(SEXP params);
-    void   	checkNames(char *inputNames[], int len);
-    bool	exists(std::string name);
-    double 	getDoubleValue(std::string name);
-    int    	getIntValue(std::string name);
-    std::string getStringValue(std::string name);
-    bool   	getBoolValue(std::string name);
-    RcppDate 	getDateValue(std::string name);
-    RcppDatetime getDatetimeValue(std::string name);
-private:
-    std::map<std::string, int> pmap;
-    SEXP _params;
-};
-
 // Supported data frame column types.
 enum ColType { COLTYPE_DOUBLE, COLTYPE_INT, COLTYPE_STRING,
 	       COLTYPE_FACTOR, COLTYPE_LOGICAL, 

Added: pkg/src/RcppParams.cpp
===================================================================
--- pkg/src/RcppParams.cpp	                        (rev 0)
+++ pkg/src/RcppParams.cpp	2009-11-09 21:29:48 UTC (rev 168)
@@ -0,0 +1,182 @@
+// -*- mode: C++; c-indent-level: 4; c-basic-offset: 4; tab-width: 8 -*-
+//
+// RcppParams.h: Rcpp R/C++ interface class library -- Parameters from 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 <RcppParams.h>
+
+RcppParams::RcppParams(SEXP params) {
+    if (!Rf_isNewList(params))
+	throw std::range_error("RcppParams: non-list passed to constructor");
+    int len = Rf_length(params);
+    SEXP names = Rf_getAttrib(params, R_NamesSymbol);
+    if (names == R_NilValue)
+	throw std::range_error("RcppParams: list must have named elements");
+    for (int i = 0; i < len; i++) {
+	std::string nm = std::string(CHAR(STRING_ELT(names,i)));
+	if (nm.size() == 0)
+	    throw std::range_error("RcppParams: all list elements must be named");
+	pmap[nm] = i;
+    }
+    _params = params;
+}
+
+void RcppParams::checkNames(char *inputNames[], int len) {
+    for (int i = 0; i < len; i++) {
+	std::map<std::string,int>::iterator iter = pmap.find(inputNames[i]);
+	if (iter == pmap.end()) {
+	    std::string mesg = "RcppParams::checkNames: missing required parameter ";
+	    throw std::range_error(mesg+inputNames[i]);
+	}
+    }
+}
+
+bool RcppParams::exists(std::string name) {
+    bool rc = true;
+    std::map<std::string,int>::iterator iter = pmap.find(name);
+    if (iter == pmap.end()) {
+	rc = false;
+    }
+    return rc;
+}
+
+double RcppParams::getDoubleValue(std::string name) {
+    std::map<std::string,int>::iterator iter = pmap.find(name);
+    if (iter == pmap.end()) {
+	std::string mesg = "RcppParams::getDoubleValue: no such name: ";
+	throw std::range_error(mesg+name);
+    }
+    int posn = iter->second;
+    SEXP elt = VECTOR_ELT(_params,posn);
+    if (!Rf_isNumeric(elt) || Rf_length(elt) != 1) {
+	std::string mesg = "RcppParams::getDoubleValue: must be scalar ";
+	throw std::range_error(mesg+name);
+    }
+    if (Rf_isInteger(elt))
+	return (double)INTEGER(elt)[0];
+    else if (Rf_isReal(elt))
+	return REAL(elt)[0];
+    else {
+	std::string mesg = "RcppParams::getDoubleValue: invalid value for ";
+	throw std::range_error(mesg+name);
+    }
+    return 0; // never get here
+}
+
+int RcppParams::getIntValue(std::string name) {
+    std::map<std::string,int>::iterator iter = pmap.find(name);
+    if (iter == pmap.end()) {
+	std::string mesg = "RcppParams::getIntValue: no such name: ";
+	throw std::range_error(mesg+name);
+    }
+    int posn = iter->second;
+    SEXP elt = VECTOR_ELT(_params,posn);
+    if (!Rf_isNumeric(elt) || Rf_length(elt) != 1) {
+	std::string mesg = "RcppParams::getIntValue: must be scalar: ";
+	throw std::range_error(mesg+name);
+    }
+    if (Rf_isInteger(elt))
+	return INTEGER(elt)[0];
+    else if (Rf_isReal(elt))
+	return (int)REAL(elt)[0];
+    else {
+	std::string mesg = "RcppParams::getIntValue: invalid value for: ";
+	throw std::range_error(mesg+name);
+    }
+    return 0; // never get here
+}
+
+std::string RcppParams::getStringValue(std::string name) {
+    std::map<std::string,int>::iterator iter = pmap.find(name);
+    if (iter == pmap.end()) {
+	std::string mesg = "RcppParams::getStringValue: no such name: ";
+	throw std::range_error(mesg+name);
+    }
+    int posn = iter->second;
+    SEXP elt = VECTOR_ELT(_params,posn);
+    if (Rf_isString(elt))
+		return std::string(CHAR(STRING_ELT(elt,0)));
+    else {
+	std::string mesg = "RcppParams::getStringValue: invalid value for: ";
+	throw std::range_error(mesg+name);
+    }
+    return ""; // never get here
+}
+
+bool RcppParams::getBoolValue(std::string name) {
+    std::map<std::string,int>::iterator iter = pmap.find(name);
+    if (iter == pmap.end()) {
+	std::string mesg = "RcppParams::getBoolValue: no such name: ";
+	throw std::range_error(mesg+name);
+    }
+    int posn = iter->second;
+    SEXP elt = VECTOR_ELT(_params,posn);
+    if (Rf_isLogical(elt))
+	return INTEGER(elt)[0];
+    else {
+	std::string mesg = "RcppParams::getBoolValue: invalid value for: ";
+	throw std::range_error(mesg+name);
+    }
+    return false; // never get here
+}
+
+RcppDate RcppParams::getDateValue(std::string name) {
+    std::map<std::string,int>::iterator iter = pmap.find(name);
+    if (iter == pmap.end()) {
+	std::string mesg = "RcppParams::getDateValue: no such name: ";
+	throw std::range_error(mesg+name);
+    }
+    int posn = iter->second;
+    SEXP elt = VECTOR_ELT(_params,posn);
+    if (!Rf_isNumeric(elt) || Rf_length(elt) != 1) {
+	std::string mesg = "RcppParams::getDateValue: invalide date: ";
+	throw std::range_error(mesg+name);
+    }
+
+    int d;
+    if (Rf_isReal(elt)) // R stores julian value in a double.
+	d = (int)REAL(elt)[0];
+    else {
+	std::string mesg = "RcppParams::getDateValue: invalid value for: ";
+	throw std::range_error(mesg+name);
+    }
+    return RcppDate(d);
+}
+
+RcppDatetime RcppParams::getDatetimeValue(std::string name) {
+    std::map<std::string,int>::iterator iter = pmap.find(name);
+    if (iter == pmap.end()) {
+        std::string mesg = "RcppParams::getDatetimeValue: no such name: ";
+	throw std::range_error(mesg+name);
+    }
+    int posn = iter->second;
+    SEXP elt = VECTOR_ELT(_params, posn);
+    if (!Rf_isNumeric(elt) || Rf_length(elt) != 1) {
+	std::string mesg = "RcppParams::getDateValue: invalide date: ";
+	throw std::range_error(mesg+name);
+    }
+    double d;
+    if (Rf_isReal(elt)) 	// R stores POSIXt as a double
+	d = REAL(elt)[0];
+    else {
+	std::string mesg = "RcppParams::getDatetimeValue: invalid value for: ";
+	throw std::range_error(mesg+name);
+    }
+    return RcppDatetime(d);
+}


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

Added: pkg/src/RcppParams.h
===================================================================
--- pkg/src/RcppParams.h	                        (rev 0)
+++ pkg/src/RcppParams.h	2009-11-09 21:29:48 UTC (rev 168)
@@ -0,0 +1,46 @@
+// -*- mode: C++; c-indent-level: 4; c-basic-offset: 4; tab-width: 8 -*-
+//
+// RcppParams.h: Rcpp R/C++ interface class library -- Parameters from 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 
+
+#ifndef RcppParams_h
+#define RcppParams_h
+
+#include <RcppCommon.h>
+#include <RcppDate.h>
+#include <RcppDatetime.h>
+
+class RcppParams {
+public:
+    RcppParams(SEXP params);
+    void   		 checkNames(char *inputNames[], int len);
+    bool		 exists(std::string name);
+    double 		 getDoubleValue(std::string name);
+    int    		 getIntValue(std::string name);
+    std::string  getStringValue(std::string name);
+    bool   		 getBoolValue(std::string name);
+    RcppDate 	 getDateValue(std::string name);
+    RcppDatetime getDatetimeValue(std::string name);
+
+private:
+    std::map<std::string, int> pmap;
+    SEXP _params;
+};
+
+#endif


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



More information about the Rcpp-commits mailing list