Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions nCompiler/R/compile_aaa_operatorLists.R
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,20 @@ getOperatorDef <- function(op, field = NULL, subfield = NULL) {
getOperatorField(opDef, field, subfield)
}

assignOperatorDef(
'ETaccess',
list(
matchDef = function(obj, copy=FALSE) {},
compileArgs = "copy",
simpleTransformations = list(
handler = 'EvalCompileArgs'),
labelAbstractTypes = list(
handler = 'ETaccess'),
cppOutput = list(
handler = 'ETaccess')
)
)

assignOperatorDef(
'nCppVec',
list(
Expand Down
12 changes: 12 additions & 0 deletions nCompiler/R/compile_generateCpp.R
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,18 @@ inGenCppEnv(
}
)

inGenCppEnv(
ETaccess <- function(code, symTab) {
copy <- isTRUE(code$aux$compileArgs$copy)
copy_piece <- if(copy) '<true>' else ''
paste0('ETaccessPtr', copy_piece, '(',
paste0(unlist(lapply(code$args,
compile_generateCpp,
symTab,
asArg = TRUE)), collapse=","), ")")
}
)

inGenCppEnv(
Switch <- function(code, symTab) {
IDs <- code$aux$compileArgs$IDs
Expand Down
9 changes: 9 additions & 0 deletions nCompiler/R/compile_labelAbstractTypes.R
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,15 @@ inLabelAbstractTypesEnv(
}
)

inLabelAbstractTypesEnv(
ETaccess <- function(code, symTab, auxEnv, handlingInfo) {
inserts <- recurse_labelAbstractTypes(code, symTab, auxEnv,
handlingInfo)
code$type <- symbolETaccBase$new(name = '') # should never be looked at because ETaccess has no return type
if(length(inserts) == 0) NULL else inserts
}
)

nCompiler:::inLabelAbstractTypesEnv(
DoubleBracket <- function(code, symTab, auxEnv, handlingInfo) {
# specializations from generic will have already been handled
Expand Down
9 changes: 9 additions & 0 deletions nCompiler/R/compile_simpleTransformations.R
Original file line number Diff line number Diff line change
Expand Up @@ -156,4 +156,13 @@ inSimpleTransformationsEnv(
code$aux$compileArgs$IDs <- IDs
if(code$caller$name != "{") stop("nSwitch can not be used within an expression. It does not return anything.")
}
)

inSimpleTransformationsEnv(
EvalCompileArgs <- function(code, symTab, auxEnv, info) {
for(argname in names(code$aux$compileArgs)) {
evaled_arg <- eval(code$aux$compileArgs[[argname]], envir = auxEnv$where)
code$aux$compileArgs[[argname]] <- evaled_arg
}
}
)
7 changes: 4 additions & 3 deletions nCompiler/R/cppDefs_variables.R
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ cppVarClass <- R6::R6Class(
if(length(printName) > 0)
printName <- paste0(printName, collapse = ', ')
cleanWhite(paste(self$baseType,
self$ptrs,
ptrs,
if(isTRUE(self$ref))
'&'
else
Expand Down Expand Up @@ -195,10 +195,11 @@ cppNcppVec <- function(name = character(),
templateArgs = list(elementVar))
}

cppETaccBase <- function(name = character()) {
cppETaccBase <- function(name = character(), ...) {
cppVarFullClass$new(name = name,
baseType = "std::unique_ptr",
templateArgs = list("ETaccessorBase"))
templateArgs = list("ETaccessorBase"),
...)
}

cppEigenTensorRef <- function(name = character(),
Expand Down
7 changes: 3 additions & 4 deletions nCompiler/R/symbolTable.R
Original file line number Diff line number Diff line change
Expand Up @@ -499,10 +499,9 @@ symbolETaccBase <- R6::R6Class(
inherit = symbolBase,
portable = TRUE,
public = list(
initialize = function(name, isArg = FALSE) {
self$name <- name
initialize = function(...) {
super$initialize(...)
self$type <- "ETaccessorBase"
self$isArg <- isArg
},
print = function() {
writeLines(paste0(self$name, ': symbolETaccBase (ETaccessorBase) '))
Expand All @@ -511,7 +510,7 @@ symbolETaccBase <- R6::R6Class(
paste0("ETaccessorBase")
},
genCppVar = function() {
cppETaccBase(name = self$name)
cppETaccBase(name = self$name, ref = self$isArg)
}
)
)
Expand Down
5 changes: 4 additions & 1 deletion nCompiler/R/typeDeclarations.R
Original file line number Diff line number Diff line change
Expand Up @@ -366,7 +366,7 @@ typeDeclarationList <- list(
## symbolRcppType$new(RcppType = "Rcpp::Named", ...)
## },
RcppDataFrame = function(...) {
symbolRcppType$new(RcppType = "Rcpp::DataFrame")
symbolRcppType$new(RcppType = "Rcpp::DataFrame", ...)
},
RcppLogicalMatrix = function(...) {
symbolRcppType$new(RcppType = "Rcpp::LogicalMatrix", ...)
Expand Down Expand Up @@ -414,6 +414,9 @@ typeDeclarationList <- list(
elementSym <- type2symbol({{ttype}}, where = parent.frame())
symbolNcppVec$new(elementSym = elementSym)
},
ETaccessor = function(...) {
symbolETaccBase$new(...)
},
## determine type from an evaluated object
typeDeclarationFromObject = function(x) {
if(inherits(x, 'symbolBasic'))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#define NCOMPILER_ETACCESSOR_POST_RCPP_H_

#include <unsupported/Eigen/CXX11/Tensor>
#include <memory>
#include <type_traits>
#include <nCompiler/ET_ext/StridedTensorMap.h>
#include <nCompiler/ET_ext/post_Rcpp/tensorUtils.h>
Expand Down Expand Up @@ -394,6 +395,18 @@ ETaccess(const T &x) {
return ETaccessor<T, true>(x);
}

template<bool copy=false, typename T>
std::enable_if_t<!copy, std::unique_ptr<ETaccessorBase>>
ETaccessPtr(T &x) {
return std::make_unique<ETaccessor<T, false>>(x);
}

template<bool copy, typename T>
std::enable_if_t<copy, std::unique_ptr<ETaccessorBase>>
ETaccessPtr(const T &x) {
return std::make_unique<ETaccessor<T, true>>(x);
}

// end ETaccess

#endif // NCOMPILER_ETACCESSOR_POST_RCPP_H_
59 changes: 59 additions & 0 deletions nCompiler/tests/testthat/specificOp_tests/test-ETaccess-DSL.R
Original file line number Diff line number Diff line change
@@ -1,6 +1,65 @@
library(nCompiler)
library(testthat)

test_that("ETaccessor type works", {
nc <- nClass(
Cpublic = list(
s = 'numericScalar',
v = 'numericVector',
m = 'numericMatrix',
get_s = nFunction(
function() {
ans <- ETaccess(s)
return(ans)
returnType('ETaccessor')
}
),
get_inner = nFunction(
function(vn = 'string') {
ans <- self[[vn]]
return(ans)
returnType('ETaccessor')
}
),
use = nFunction(
function(acc = 'ETaccessor') {
return(as(acc, "numericMatrix"))
returnType("numericMatrix")
}
),
get = nFunction(
function(i = 'integerScalar', vn = 'string') {
nSwitch(i, 1:4,
eta <- get_s(),
eta <- get_inner(vn),
eta <- self[[vn]],
{
eta <- self[[vn]]
res <- use(eta)
}
)
if(i < 4)
res <- as(eta, "numericMatrix")
return(res)
returnType("numericMatrix")
}
)
),
compileInfo=list(interfaceMembers = c("s","v","m", "get"))
)

cnc <- nCompile(nc)
obj <- cnc$new()
obj$s <- 1.2
obj$v <- c(2.3, 3.4)
obj$m <- matrix(5:10, nrow = 3)
expect_equal(obj$get(1, "not_used"), matrix(1.2))
expect_equal(obj$get(2, "v"), matrix(obj$v))
expect_equal(obj$get(3, "m"), obj$m)
expect_equal(obj$get(4, "v"), matrix(obj$v))
rm(obj); gc()
})

test_that("obj[['x']] works like obj$x", {
nc <- nClass(
Cpublic = list(
Expand Down
Loading