Skip to content

Commit 19b5d68

Browse files
committed
conversion: nt2::table -> R matrix
1 parent 76f9c44 commit 19b5d68

5 files changed

Lines changed: 85 additions & 0 deletions

File tree

R/precompiled-headers.R

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ precompileRcppNT2 <- function() {
2525
CXX,
2626
"-x c++-header", header,
2727
paste("-I", include, sep = ""),
28+
paste("-I", R.home("include"), sep = ""),
2829
"-std=c++11",
2930
cxxFlags
3031
)

inst/include/RcppNT2.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
#include <nt2/trigonometric/trigonometric.hpp>
4141

4242
#include <RcppNT2/core/core.h>
43+
#include <RcppNT2/convert/convert.h>
4344
#include <RcppNT2/algorithm/algorithm.h>
4445
#include <RcppNT2/functor/functor.h>
4546
#include <RcppNT2/variadic/variadic.h>

inst/include/RcppNT2/convert/as.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#ifndef RCPP_NT2_CONVERT_AS_H
2+
#define RCPP_NT2_CONVERT_AS_H
3+
4+
5+
6+
#endif /* RCPP_NT2_CONVERT_AS_H */
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#ifndef RCPP_NT2_CONVERT_CONVERT_H
2+
#define RCPP_NT2_CONVERT_CONVERT_H
3+
4+
#include <RcppNT2/convert/as.h>
5+
#include <RcppNT2/convert/wrap.h>
6+
7+
#endif /* RCPP_NT2_CONVERT_CONVERT_H */
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
#ifndef RCPP_NT2_CONVERT_WRAP_H
2+
#define RCPP_NT2_CONVERT_WRAP_H
3+
4+
#include <R.h>
5+
#include <Rinternals.h>
6+
7+
namespace RcppNT2 {
8+
namespace convert {
9+
10+
#define RCPP_NT2_WRAP_MATRIX_IMPL(__CTYPE__, __SEXPTYPE__, __ACCESSOR__) \
11+
SEXP wrap_matrix(const nt2::table<__CTYPE__>& data) \
12+
{ \
13+
std::size_t n = nt2::numel(data); \
14+
std::size_t nrow = nt2::size(data, 1); \
15+
std::size_t ncol = nt2::size(data, 2); \
16+
\
17+
SEXP result = Rf_allocMatrix(__SEXPTYPE__, nrow, ncol); \
18+
::memcpy( \
19+
(char*) __ACCESSOR__(result), \
20+
(char*) data.begin(), \
21+
sizeof(__CTYPE__) * n \
22+
); \
23+
\
24+
return result; \
25+
}
26+
27+
RCPP_NT2_WRAP_MATRIX_IMPL(double, REALSXP, REAL);
28+
RCPP_NT2_WRAP_MATRIX_IMPL(int, INTSXP, INTEGER);
29+
30+
#define RCPP_NT2_WRAP_VECTOR_IMPL(__CTYPE__, __SEXPTYPE__, __ACCESSOR__) \
31+
SEXP wrap_vector(const nt2::table<__CTYPE__>& data) \
32+
{ \
33+
std::size_t n = nt2::numel(data); \
34+
SEXP result = Rf_allocVector(__SEXPTYPE__, n); \
35+
::memcpy( \
36+
(char*) __ACCESSOR__(result), \
37+
(char*) data.begin(), \
38+
sizeof(__CTYPE__) * n \
39+
); \
40+
return result; \
41+
}
42+
43+
RCPP_NT2_WRAP_VECTOR_IMPL(double, REALSXP, REAL);
44+
RCPP_NT2_WRAP_VECTOR_IMPL(int, INTSXP, INTEGER);
45+
46+
#undef RCPP_NT2_WRAP_VECTOR_IMPL
47+
#undef RCPP_NT2_WRAP_MATRIX_IMPL
48+
49+
} // namespace convert
50+
} // namespace RcppNT2
51+
52+
namespace Rcpp {
53+
54+
SEXP wrap(const nt2::table<double>& data)
55+
{
56+
std::size_t ndims = nt2::ndims(data);
57+
if (ndims == 1)
58+
return RcppNT2::convert::wrap_vector(data);
59+
else if (ndims == 2)
60+
return RcppNT2::convert::wrap_matrix(data);
61+
62+
// TODO: wrap arbitrary arrays
63+
Rf_warning("cannot wrap nt2::table<T>s of dimension %i\n", (int) ndims);
64+
65+
return R_NilValue;
66+
}
67+
68+
} // namespace Rcpp
69+
70+
#endif /* RCPP_NT2_CONVERT_WRAP_H */

0 commit comments

Comments
 (0)