00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #include "RcppList.h"
00022
00023 void RcppList::setSize(int n) {
00024 listSize = n;
00025 listArg = PROTECT(Rf_allocVector(VECSXP, n));
00026 numProtected++;
00027 }
00028
00029 void RcppList::append(std::string name, double value) {
00030 if (currListPosn < 0 || currListPosn >= listSize)
00031 throw std::range_error("RcppList::append(double): list posn out of range");
00032 SEXP valsxp = PROTECT(Rf_allocVector(REALSXP,1));
00033 numProtected++;
00034 REAL(valsxp)[0] = value;
00035 SET_VECTOR_ELT(listArg, currListPosn++, valsxp);
00036 names.push_back(name);
00037 }
00038
00039 void RcppList::append(std::string name, int value) {
00040 if (currListPosn < 0 || currListPosn >= listSize)
00041 throw std::range_error("RcppList::append(int): posn out of range");
00042 SEXP valsxp = PROTECT(Rf_allocVector(INTSXP,1));
00043 numProtected++;
00044 INTEGER(valsxp)[0] = value;
00045 SET_VECTOR_ELT(listArg, currListPosn++, valsxp);
00046 names.push_back(name);
00047 }
00048
00049 void RcppList::append(std::string name, std::string value) {
00050 if (currListPosn < 0 || currListPosn >= listSize)
00051 throw std::range_error("RcppList::append(string): posn out of range");
00052 SEXP valsxp = PROTECT(Rf_allocVector(STRSXP,1));
00053 numProtected++;
00054 SET_STRING_ELT(valsxp, 0, Rf_mkChar(value.c_str()));
00055 SET_VECTOR_ELT(listArg, currListPosn++, valsxp);
00056 names.push_back(name);
00057 }
00058
00059 void RcppList::append(std::string name, SEXP sexp) {
00060 if (currListPosn < 0 || currListPosn >= listSize)
00061 throw std::range_error("RcppList::append(sexp): posn out of range");
00062 SET_VECTOR_ELT(listArg, currListPosn++, sexp);
00063 names.push_back(name);
00064 }
00065
00066 void RcppList::clearProtectionStack() {
00067 UNPROTECT(numProtected);
00068 numProtected = 0;
00069 }
00070
00071 SEXP RcppList::getList(void) const {
00072 SEXP li = PROTECT(Rf_allocVector(VECSXP, listSize));
00073 SEXP nm = PROTECT(Rf_allocVector(STRSXP, listSize));
00074 for (int i=0; i<listSize; i++) {
00075 SET_VECTOR_ELT(li, i, VECTOR_ELT(listArg, i));
00076 SET_STRING_ELT(nm, i, Rf_mkChar(names[i].c_str()));
00077 }
00078 Rf_setAttrib(li, R_NamesSymbol, nm);
00079 UNPROTECT(2);
00080 return li;
00081 }
00082
00083