/* * Recursively builds the decision tree based on * the data that it is passed and tha table info. */ node* buildDecisionTree(vvs &table, node* nodePtr, vvs &tableInfo) { if (tableIsEmpty(table)) { return NULL; } if (isHomogeneous(table)) { nodePtr->isLeaf = true; nodePtr->label = table[1][table[1].size()-1]; return nodePtr; } else { string splittingCol = decideSplittingColumn(table); nodePtr->splitOn = splittingCol; int colIndex = returnColumnIndex(splittingCol, tableInfo); int iii; for (iii = 1; iii < tableInfo[colIndex].size(); iii++) { node* newNode = (node*) new node; newNode->label = tableInfo[colIndex][iii]; nodePtr->childrenValues.push_back(tableInfo[colIndex][iii]); newNode->isLeaf = false; newNode->splitOn = splittingCol; vvs auxTable = pruneTable(table, splittingCol, tableInfo[colIndex][iii]); nodePtr->children.push_back(buildDecisionTree(auxTable, newNode, tableInfo)); } } return nodePtr; }
/* Loop over all the key-value pairs and convert them to string and USER_OBJECT_ and put the latter into an R/S LIST and use the vector of keys as the names. */ USER_OBJECT_ fromPerlHV(HV *table, unsigned int depth) { I32 len; char *key; SV *el; I32 n, i; Rboolean sameType; svtype elType; dTHX; USER_OBJECT_ names, ans; sameType = isHomogeneous((SV*)table, &elType); if(sameType && isPerlPrimitiveType(elType, (SV *)table)) { return(fromHomogeneousTable((SV *) table, elType)); } n = hv_iterinit(table); i = 0; PROTECT(names = NEW_CHARACTER(n)); PROTECT(ans = NEW_LIST(n)); while(i < n) { el = hv_iternextsv(table, &key, &len); if(key == NULL) break; SET_VECTOR_ELT(ans, i, fromPerl(el, TRUE)); SET_STRING_ELT(names, i, COPY_TO_USER_STRING(key)); i++; } SET_NAMES(ans, names); UNPROTECT(2); return(ans); }
const internal::NeighborIndex *Set::getNeighborIndex() const { tassert(isHomogeneous()) << "neighbor indices are currently only supported for homogeneous sets"; if (getCardinality() >= 2 && neighbors == nullptr) { // Cast to non-const since adding a neighbor index does not change the this->neighbors = new internal::NeighborIndex(*this); } return this->neighbors; }
USER_OBJECT_ fromPerlAV(AV *arr, SV* filter, unsigned int depth) { I32 n, i; SV **el, *tmp; Rboolean mustFree; USER_OBJECT_ ans; svtype elType; dTHX; Rboolean sameType = SVt_NULL; n = av_len(arr); #if defined(R_PERL_DEBUG) && R_PERL_DEBUG fprintf(stderr, "In fromPerlAV: length %d\n", n); #endif sameType = isHomogeneous((SV*)arr, &elType); if(sameType && isPerlPrimitiveType(elType, (SV *)arr)) { return(fromHomogeneousArray((SV *) arr, elType)); } PROTECT(ans = NEW_LIST(n+1)); for(i = 0; i <= n; i++) { mustFree = FALSE; el = av_fetch(arr, i, 0); if(el && *el != NULL) { if(filter) { tmp = callFilter(filter, *el); mustFree = TRUE; } else tmp = *el; SET_VECTOR_ELT(ans, i, fromPerl(tmp, TRUE)); if(mustFree) SvREFCNT_dec(tmp); } } UNPROTECT(1); return(ans); }
USER_OBJECT_ fromPerl(SV *val, unsigned int depth) { USER_OBJECT_ ans = NULL_USER_OBJECT; USER_OBJECT_ classes; svtype type = SvTYPE(val); svtype elementType = SVt_NULL; svtype refType; SV *refVal = NULL; dTHX; if(type == SVt_PVGV) { if(GvHV(val)) ans = fromPerlHV(GvHV(val), depth - 1); else if(GvAV(val)) ans = fromPerlAV(GvAV(val), NULL, depth - 1); else if(GvCV(val)) ans = fromPerl((SV *) GvCV(val), 0); else if(GvSV(val)) ans = fromPerl(GvSV(val), depth - 1); else if(GvIOp(val)) { /* XXX */ } else { PROBLEM "Don't understand particular type of PVGV at this point" ERROR; } return(ans); } else if (type == SVt_PVMG && !sv_isobject(val)) { /* If it is magic and not an object, then treat it as a scalar and get it back to R as a value, not a reference. Would ideally like to respect the convert option. But we can get ourselves into an infinite loop. Needs more investigation. */ return(GetRScalar(val)); } if(val == NULL || val == &sv_undef || (!SvOK(val) && !SvROK(val) && type != SVt_PVCV) /* || type == SVt_NULL */) { #ifdef R_PERL_DEBUG fprintf(stderr, "Null result: %p (%d) (undef = %p) (type is %s) SvOK=%d, SvROK=%d\n", val, type, &sv_undef, type == SVt_NULL ? "null" : "not null", SvOK(val), SvROK(val)); fflush(stderr); #endif return(NULL_USER_OBJECT); } if(SvROK(val)) { /* && sv_isobject(val)) { */ if(sv_isa(val, "RReferences")) { return(RPerl_getProxyValue(val)); } else { ans = userLevelConversionFromPerl(val, depth); if(ans != NULL) return(ans); #ifdef R_PERL_DEBUG fprintf(stderr, "Didn't get a user-leve conversion. Continuining with regular conversion\n"); #endif } } classes = computeRSPerlClassVector(val, &elementType, depth); if(!depth || (classes && GET_LENGTH(classes))) { /* We protect classes in the subroutines. */ PROTECT(classes); ans = makeForeignPerlReference(val, classes, &exportReferenceTable); UNPROTECT(1); return(ans); } #ifdef R_PERL_DEBUG fprintf(stderr, "[Converting] element type %d %d %d\n", (int) elementType, (int) SvTYPE(val), (int) (SvTYPE(val) == SVt_RV)); #endif /* If it is a reference, then check whether it is an array or hash. */ if(SvROK(val)) { refVal = SvRV(val); refType = SvTYPE(refVal); } else { refVal = val; refType = type; } #ifdef R_PERL_DEBUG fprintf(stderr, "[fromPerl] refType = %d\n", refType); #endif if(refType == SVt_PVAV || refType == SVt_PVHV) { if(isHomogeneous(refVal, &elementType)) { return( (refType == SVt_PVAV) ? fromHomogeneousArray(refVal, elementType) : fromHomogeneousTable(refVal, elementType)); } else { return( (refType == SVt_PVAV) ? fromPerlAV((AV*)refVal, NULL, depth) : fromPerlHV((HV*)refVal, depth)); } } else if(refType == SVt_PVCV) { return(createPerlReference(refVal)); } #ifdef R_PERL_DEBUG fprintf(stderr, "[fromPerl] continuing again as refType (%d) was not an array or table.\n", refType); #endif ans = GetRScalar(val); return(ans); }