00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 #include <RcppNumList.h>
00024
00025 RcppNumList::RcppNumList(SEXP theList) {
00026 if (!Rf_isNewList(theList))
00027 throw std::range_error("RcppNumList: non-list passed to constructor");
00028 len = Rf_length(theList);
00029 names = Rf_getAttrib(theList, R_NamesSymbol);
00030 namedList = theList;
00031 }
00032
00033 std::string RcppNumList::getName(int i) const {
00034 if (i < 0 || i >= len) {
00035 std::ostringstream oss;
00036 oss << "RcppNumList::getName: index out of bounds: " << i;
00037 throw std::range_error(oss.str());
00038 }
00039 return std::string(CHAR(STRING_ELT(names,i)));
00040 }
00041
00042 double RcppNumList::getValue(int i) const {
00043 if (i < 0 || i >= len) {
00044 std::ostringstream oss;
00045 oss << "RcppNumList::getValue: index out of bounds: " << i;
00046 throw std::range_error(oss.str());
00047 }
00048 SEXP elt = VECTOR_ELT(namedList, i);
00049 if (Rf_isReal(elt))
00050 return REAL(elt)[0];
00051 else if (Rf_isInteger(elt))
00052 return (double)INTEGER(elt)[0];
00053 else
00054 throw std::range_error("RcppNumList: contains non-numeric value");
00055 return 0;
00056 }
00057
00058 int RcppNumList::size() const {
00059 return len;
00060 }