Skip to content

Commit b7ce3ec

Browse files
authored
Merge branch 'cpp11-to-cpp4r' into map-to-list-clean
2 parents 96ac5e2 + 933f214 commit b7ce3ec

File tree

14 files changed

+1472
-25
lines changed

14 files changed

+1472
-25
lines changed

Makefile

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,13 @@ all:
33
@Rscript -e 'devtools::load_all("cpp11test")'
44
@echo "make: Leaving directory 'cpp11test/src'"
55

6+
install:
7+
@Rscript -e 'devtools::document()'
8+
@Rscript -e 'devtools::install()'
9+
610
test: all
7-
@echo "make: Entering directory 'cpp11test/tests/testthat'"
11+
@echo "make: Entering directory 'cpp11test'"
12+
@Rscript -e 'devtools::document("cpp11test")'
813
@Rscript -e 'devtools::test("cpp11test")'
914
@echo "make: Leaving directory 'cpp11test/tests/testthat'"
1015

cpp11test/R/cpp11.R

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,10 @@ grow_ <- function(n) {
8484
.Call(`_cpp11test_grow_`, n)
8585
}
8686

87+
grow_cplx_ <- function(n) {
88+
.Call(`_cpp11test_grow_cplx_`, n)
89+
}
90+
8791
cpp11_insert_ <- function(num_sxp) {
8892
.Call(`_cpp11test_cpp11_insert_`, num_sxp)
8993
}
@@ -204,6 +208,42 @@ sum_dbl_accumulate2_ <- function(x_sxp) {
204208
.Call(`_cpp11test_sum_dbl_accumulate2_`, x_sxp)
205209
}
206210

211+
sum_cplx_for_ <- function(x) {
212+
.Call(`_cpp11test_sum_cplx_for_`, x)
213+
}
214+
215+
sum_cplx_for_2_ <- function(x) {
216+
.Call(`_cpp11test_sum_cplx_for_2_`, x)
217+
}
218+
219+
sum_cplx_for_3_ <- function(x_sxp) {
220+
.Call(`_cpp11test_sum_cplx_for_3_`, x_sxp)
221+
}
222+
223+
sum_cplx_for_4_ <- function(x_sxp) {
224+
.Call(`_cpp11test_sum_cplx_for_4_`, x_sxp)
225+
}
226+
227+
sum_cplx_for_5_ <- function(x_sxp) {
228+
.Call(`_cpp11test_sum_cplx_for_5_`, x_sxp)
229+
}
230+
231+
sum_cplx_for_6_ <- function(x_sxp) {
232+
.Call(`_cpp11test_sum_cplx_for_6_`, x_sxp)
233+
}
234+
235+
sum_cplx_foreach_ <- function(x) {
236+
.Call(`_cpp11test_sum_cplx_foreach_`, x)
237+
}
238+
239+
sum_cplx_accumulate_ <- function(x) {
240+
.Call(`_cpp11test_sum_cplx_accumulate_`, x)
241+
}
242+
243+
sum_cplx_for2_ <- function(x_sxp) {
244+
.Call(`_cpp11test_sum_cplx_for2_`, x_sxp)
245+
}
246+
207247
sum_int_for_ <- function(x) {
208248
.Call(`_cpp11test_sum_int_for_`, x)
209249
}

cpp11test/src/cpp11.cpp

Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,13 @@ extern "C" SEXP _cpp11test_grow_(SEXP n) {
166166
return cpp11::as_sexp(grow_(cpp11::as_cpp<cpp11::decay_t<R_xlen_t>>(n)));
167167
END_CPP11
168168
}
169+
// grow.cpp
170+
cpp11::writable::complexes grow_cplx_(R_xlen_t n);
171+
extern "C" SEXP _cpp11test_grow_cplx_(SEXP n) {
172+
BEGIN_CPP11
173+
return cpp11::as_sexp(grow_cplx_(cpp11::as_cpp<cpp11::decay_t<R_xlen_t>>(n)));
174+
END_CPP11
175+
}
169176
// insert.cpp
170177
SEXP cpp11_insert_(SEXP num_sxp);
171178
extern "C" SEXP _cpp11test_cpp11_insert_(SEXP num_sxp) {
@@ -317,6 +324,55 @@ extern "C" SEXP _cpp11test_rcpp_release_(SEXP n) {
317324
return R_NilValue;
318325
END_CPP11
319326
}
327+
// roxygen1.cpp
328+
double notroxcpp1_(double x);
329+
extern "C" SEXP _cpp11test_notroxcpp1_(SEXP x) {
330+
BEGIN_CPP11
331+
return cpp11::as_sexp(notroxcpp1_(cpp11::as_cpp<cpp11::decay_t<double>>(x)));
332+
END_CPP11
333+
}
334+
// roxygen1.cpp
335+
double roxcpp2_(double x);
336+
extern "C" SEXP _cpp11test_roxcpp2_(SEXP x) {
337+
BEGIN_CPP11
338+
return cpp11::as_sexp(roxcpp2_(cpp11::as_cpp<cpp11::decay_t<double>>(x)));
339+
END_CPP11
340+
}
341+
// roxygen2.cpp
342+
double roxcpp3_(double x);
343+
extern "C" SEXP _cpp11test_roxcpp3_(SEXP x) {
344+
BEGIN_CPP11
345+
return cpp11::as_sexp(roxcpp3_(cpp11::as_cpp<cpp11::decay_t<double>>(x)));
346+
END_CPP11
347+
}
348+
// roxygen2.cpp
349+
double roxcpp4_(double x);
350+
extern "C" SEXP _cpp11test_roxcpp4_(SEXP x) {
351+
BEGIN_CPP11
352+
return cpp11::as_sexp(roxcpp4_(cpp11::as_cpp<cpp11::decay_t<double>>(x)));
353+
END_CPP11
354+
}
355+
// roxygen3.cpp
356+
double roxcpp5_(double x);
357+
extern "C" SEXP _cpp11test_roxcpp5_(SEXP x) {
358+
BEGIN_CPP11
359+
return cpp11::as_sexp(roxcpp5_(cpp11::as_cpp<cpp11::decay_t<double>>(x)));
360+
END_CPP11
361+
}
362+
// roxygen3.cpp
363+
double notroxcpp6_(double x);
364+
extern "C" SEXP _cpp11test_notroxcpp6_(SEXP x) {
365+
BEGIN_CPP11
366+
return cpp11::as_sexp(notroxcpp6_(cpp11::as_cpp<cpp11::decay_t<double>>(x)));
367+
END_CPP11
368+
}
369+
// roxygen3.cpp
370+
double roxcpp7_(double x);
371+
extern "C" SEXP _cpp11test_roxcpp7_(SEXP x) {
372+
BEGIN_CPP11
373+
return cpp11::as_sexp(roxcpp7_(cpp11::as_cpp<cpp11::decay_t<double>>(x)));
374+
END_CPP11
375+
}
320376
// safe.cpp
321377
SEXP cpp11_safe_(SEXP x_sxp);
322378
extern "C" SEXP _cpp11test_cpp11_safe_(SEXP x_sxp) {
@@ -387,6 +443,69 @@ extern "C" SEXP _cpp11test_sum_dbl_accumulate2_(SEXP x_sxp) {
387443
return cpp11::as_sexp(sum_dbl_accumulate2_(cpp11::as_cpp<cpp11::decay_t<SEXP>>(x_sxp)));
388444
END_CPP11
389445
}
446+
// sum.cpp
447+
cpp11::r_complex sum_cplx_for_(cpp11::complexes x);
448+
extern "C" SEXP _cpp11test_sum_cplx_for_(SEXP x) {
449+
BEGIN_CPP11
450+
return cpp11::as_sexp(sum_cplx_for_(cpp11::as_cpp<cpp11::decay_t<cpp11::complexes>>(x)));
451+
END_CPP11
452+
}
453+
// sum.cpp
454+
cpp11::complexes sum_cplx_for_2_(cpp11::complexes x);
455+
extern "C" SEXP _cpp11test_sum_cplx_for_2_(SEXP x) {
456+
BEGIN_CPP11
457+
return cpp11::as_sexp(sum_cplx_for_2_(cpp11::as_cpp<cpp11::decay_t<cpp11::complexes>>(x)));
458+
END_CPP11
459+
}
460+
// sum.cpp
461+
std::complex<double> sum_cplx_for_3_(cpp11::complexes x_sxp);
462+
extern "C" SEXP _cpp11test_sum_cplx_for_3_(SEXP x_sxp) {
463+
BEGIN_CPP11
464+
return cpp11::as_sexp(sum_cplx_for_3_(cpp11::as_cpp<cpp11::decay_t<cpp11::complexes>>(x_sxp)));
465+
END_CPP11
466+
}
467+
// sum.cpp
468+
std::complex<double> sum_cplx_for_4_(SEXP x_sxp);
469+
extern "C" SEXP _cpp11test_sum_cplx_for_4_(SEXP x_sxp) {
470+
BEGIN_CPP11
471+
return cpp11::as_sexp(sum_cplx_for_4_(cpp11::as_cpp<cpp11::decay_t<SEXP>>(x_sxp)));
472+
END_CPP11
473+
}
474+
// sum.cpp
475+
SEXP sum_cplx_for_5_(SEXP x_sxp);
476+
extern "C" SEXP _cpp11test_sum_cplx_for_5_(SEXP x_sxp) {
477+
BEGIN_CPP11
478+
return cpp11::as_sexp(sum_cplx_for_5_(cpp11::as_cpp<cpp11::decay_t<SEXP>>(x_sxp)));
479+
END_CPP11
480+
}
481+
// sum.cpp
482+
cpp11::complexes sum_cplx_for_6_(SEXP x_sxp);
483+
extern "C" SEXP _cpp11test_sum_cplx_for_6_(SEXP x_sxp) {
484+
BEGIN_CPP11
485+
return cpp11::as_sexp(sum_cplx_for_6_(cpp11::as_cpp<cpp11::decay_t<SEXP>>(x_sxp)));
486+
END_CPP11
487+
}
488+
// sum.cpp
489+
std::complex<double> sum_cplx_foreach_(cpp11::complexes x);
490+
extern "C" SEXP _cpp11test_sum_cplx_foreach_(SEXP x) {
491+
BEGIN_CPP11
492+
return cpp11::as_sexp(sum_cplx_foreach_(cpp11::as_cpp<cpp11::decay_t<cpp11::complexes>>(x)));
493+
END_CPP11
494+
}
495+
// sum.cpp
496+
std::complex<double> sum_cplx_accumulate_(cpp11::complexes x);
497+
extern "C" SEXP _cpp11test_sum_cplx_accumulate_(SEXP x) {
498+
BEGIN_CPP11
499+
return cpp11::as_sexp(sum_cplx_accumulate_(cpp11::as_cpp<cpp11::decay_t<cpp11::complexes>>(x)));
500+
END_CPP11
501+
}
502+
// sum.cpp
503+
std::complex<double> sum_cplx_for2_(SEXP x_sxp);
504+
extern "C" SEXP _cpp11test_sum_cplx_for2_(SEXP x_sxp) {
505+
BEGIN_CPP11
506+
return cpp11::as_sexp(sum_cplx_for2_(cpp11::as_cpp<cpp11::decay_t<SEXP>>(x_sxp)));
507+
END_CPP11
508+
}
390509
// sum_int.cpp
391510
double sum_int_for_(cpp11::integers x);
392511
extern "C" SEXP _cpp11test_sum_int_for_(SEXP x) {
@@ -502,6 +621,7 @@ static const R_CallMethodDef CallEntries[] = {
502621
{"_cpp11test_gibbs_rcpp", (DL_FUNC) &_cpp11test_gibbs_rcpp, 2},
503622
{"_cpp11test_gibbs_rcpp2", (DL_FUNC) &_cpp11test_gibbs_rcpp2, 2},
504623
{"_cpp11test_grow_", (DL_FUNC) &_cpp11test_grow_, 1},
624+
{"_cpp11test_grow_cplx_", (DL_FUNC) &_cpp11test_grow_cplx_, 1},
505625
{"_cpp11test_my_message", (DL_FUNC) &_cpp11test_my_message, 2},
506626
{"_cpp11test_my_message_n1", (DL_FUNC) &_cpp11test_my_message_n1, 1},
507627
{"_cpp11test_my_message_n1fmt", (DL_FUNC) &_cpp11test_my_message_n1fmt, 1},
@@ -515,6 +635,8 @@ static const R_CallMethodDef CallEntries[] = {
515635
{"_cpp11test_my_warning_n1fmt", (DL_FUNC) &_cpp11test_my_warning_n1fmt, 1},
516636
{"_cpp11test_my_warning_n2fmt", (DL_FUNC) &_cpp11test_my_warning_n2fmt, 2},
517637
{"_cpp11test_ordered_map_to_list_", (DL_FUNC) &_cpp11test_ordered_map_to_list_, 1},
638+
{"_cpp11test_notroxcpp1_", (DL_FUNC) &_cpp11test_notroxcpp1_, 1},
639+
{"_cpp11test_notroxcpp6_", (DL_FUNC) &_cpp11test_notroxcpp6_, 1},
518640
{"_cpp11test_protect_many_", (DL_FUNC) &_cpp11test_protect_many_, 1},
519641
{"_cpp11test_protect_many_cpp11_", (DL_FUNC) &_cpp11test_protect_many_cpp11_, 1},
520642
{"_cpp11test_protect_many_preserve_", (DL_FUNC) &_cpp11test_protect_many_preserve_, 1},
@@ -533,8 +655,22 @@ static const R_CallMethodDef CallEntries[] = {
533655
{"_cpp11test_rcpp_sum_int_for_", (DL_FUNC) &_cpp11test_rcpp_sum_int_for_, 1},
534656
{"_cpp11test_remove_altrep", (DL_FUNC) &_cpp11test_remove_altrep, 1},
535657
{"_cpp11test_row_sums", (DL_FUNC) &_cpp11test_row_sums, 1},
658+
{"_cpp11test_roxcpp2_", (DL_FUNC) &_cpp11test_roxcpp2_, 1},
659+
{"_cpp11test_roxcpp3_", (DL_FUNC) &_cpp11test_roxcpp3_, 1},
660+
{"_cpp11test_roxcpp4_", (DL_FUNC) &_cpp11test_roxcpp4_, 1},
661+
{"_cpp11test_roxcpp5_", (DL_FUNC) &_cpp11test_roxcpp5_, 1},
662+
{"_cpp11test_roxcpp7_", (DL_FUNC) &_cpp11test_roxcpp7_, 1},
536663
{"_cpp11test_string_proxy_assignment_", (DL_FUNC) &_cpp11test_string_proxy_assignment_, 0},
537664
{"_cpp11test_string_push_back_", (DL_FUNC) &_cpp11test_string_push_back_, 0},
665+
{"_cpp11test_sum_cplx_accumulate_", (DL_FUNC) &_cpp11test_sum_cplx_accumulate_, 1},
666+
{"_cpp11test_sum_cplx_for2_", (DL_FUNC) &_cpp11test_sum_cplx_for2_, 1},
667+
{"_cpp11test_sum_cplx_for_", (DL_FUNC) &_cpp11test_sum_cplx_for_, 1},
668+
{"_cpp11test_sum_cplx_for_2_", (DL_FUNC) &_cpp11test_sum_cplx_for_2_, 1},
669+
{"_cpp11test_sum_cplx_for_3_", (DL_FUNC) &_cpp11test_sum_cplx_for_3_, 1},
670+
{"_cpp11test_sum_cplx_for_4_", (DL_FUNC) &_cpp11test_sum_cplx_for_4_, 1},
671+
{"_cpp11test_sum_cplx_for_5_", (DL_FUNC) &_cpp11test_sum_cplx_for_5_, 1},
672+
{"_cpp11test_sum_cplx_for_6_", (DL_FUNC) &_cpp11test_sum_cplx_for_6_, 1},
673+
{"_cpp11test_sum_cplx_foreach_", (DL_FUNC) &_cpp11test_sum_cplx_foreach_, 1},
538674
{"_cpp11test_sum_dbl_accumulate2_", (DL_FUNC) &_cpp11test_sum_dbl_accumulate2_, 1},
539675
{"_cpp11test_sum_dbl_accumulate_", (DL_FUNC) &_cpp11test_sum_dbl_accumulate_, 1},
540676
{"_cpp11test_sum_dbl_for2_", (DL_FUNC) &_cpp11test_sum_dbl_for2_, 1},

cpp11test/src/grow.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
#include "cpp11/complexes.hpp"
12
#include "cpp11/doubles.hpp"
23

34
[[cpp11::register]] cpp11::writable::doubles grow_(R_xlen_t n) {
@@ -9,3 +10,14 @@
910

1011
return x;
1112
}
13+
14+
[[cpp11::register]] cpp11::writable::complexes grow_cplx_(R_xlen_t n) {
15+
cpp11::writable::complexes x;
16+
R_xlen_t i = 0;
17+
while (i < n) {
18+
x.push_back(std::complex<double>(i, i));
19+
i++;
20+
}
21+
22+
return x;
23+
}

cpp11test/src/sum.cpp

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#include <numeric>
2+
#include "cpp11/complexes.hpp"
23
#include "cpp11/doubles.hpp"
34

45
[[cpp11::register]] double sum_dbl_for_(cpp11::doubles x) {
@@ -58,3 +59,111 @@
5859
const cpp11::doubles x(x_sxp, false);
5960
return std::accumulate(x.cbegin(), x.cend(), 0.);
6061
}
62+
63+
// Pacha: Functions for complex data type
64+
65+
[[cpp11::register]] cpp11::r_complex sum_cplx_for_(cpp11::complexes x) {
66+
std::complex<double> sum = {0.0, 0.0};
67+
R_xlen_t n = x.size();
68+
for (R_xlen_t i = 0; i < n; ++i) {
69+
// sum.real(sum.real() + x[i].real());
70+
// sum.imag(sum.imag() + x[i].imag());
71+
sum += x[i];
72+
}
73+
74+
return cpp11::r_complex(sum.real(), sum.imag());
75+
}
76+
77+
[[cpp11::register]] cpp11::complexes sum_cplx_for_2_(cpp11::complexes x) {
78+
std::complex<double> sum = {0.0, 0.0};
79+
R_xlen_t n = x.size();
80+
for (R_xlen_t i = 0; i < n; ++i) {
81+
// sum.real(sum.real() + x[i].real());
82+
// sum.imag(sum.imag() + x[i].imag());
83+
sum += x[i];
84+
}
85+
86+
cpp11::writable::complexes result(1);
87+
// result[0] = cpp11::r_complex(sum.real(), sum.imag());
88+
result[0] = sum;
89+
90+
return result;
91+
}
92+
93+
[[cpp11::register]] std::complex<double> sum_cplx_for_3_(cpp11::complexes x_sxp) {
94+
std::complex<double> sum = {0.0, 0.0};
95+
const cpp11::complexes x(x_sxp, false);
96+
R_xlen_t n = x.size();
97+
for (R_xlen_t i = 0; i < n; ++i) {
98+
// sum.real(sum.real() + x[i].real());
99+
// sum.imag(sum.imag() + x[i].imag());
100+
sum += x[i];
101+
}
102+
103+
return sum;
104+
}
105+
106+
[[cpp11::register]] std::complex<double> sum_cplx_for_4_(SEXP x_sxp) {
107+
std::complex<double> sum = {0.0, 0.0};
108+
const cpp11::complexes x(x_sxp, false);
109+
R_xlen_t n = x.size();
110+
for (R_xlen_t i = 0; i < n; ++i) {
111+
// sum.real(sum.real() + x[i].real());
112+
// sum.imag(sum.imag() + x[i].imag());
113+
sum += x[i];
114+
}
115+
116+
return sum;
117+
}
118+
119+
[[cpp11::register]] SEXP sum_cplx_for_5_(SEXP x_sxp) {
120+
std::complex<double> sum = {0.0, 0.0};
121+
const cpp11::complexes x(x_sxp, false);
122+
R_xlen_t n = x.size();
123+
for (R_xlen_t i = 0; i < n; ++i) {
124+
// sum.real(sum.real() + x[i].real());
125+
// sum.imag(sum.imag() + x[i].imag());
126+
sum += x[i];
127+
}
128+
129+
return cpp11::as_sexp(sum);
130+
}
131+
132+
[[cpp11::register]] cpp11::complexes sum_cplx_for_6_(SEXP x_sxp) {
133+
std::complex<double> sum = {0.0, 0.0};
134+
const cpp11::complexes x(x_sxp, false);
135+
R_xlen_t n = x.size();
136+
for (R_xlen_t i = 0; i < n; ++i) {
137+
// sum.real(sum.real() + x[i].real());
138+
// sum.imag(sum.imag() + x[i].imag());
139+
sum += x[i];
140+
}
141+
142+
return cpp11::as_sexp(sum);
143+
}
144+
145+
[[cpp11::register]] std::complex<double> sum_cplx_foreach_(cpp11::complexes x) {
146+
std::complex<double> sum = {0.0, 0.0};
147+
for (const auto&& val : x) {
148+
// sum.real(sum.real() + val.real());
149+
// sum.imag(sum.imag() + val.imag());
150+
sum += val;
151+
}
152+
153+
return sum;
154+
}
155+
156+
[[cpp11::register]] std::complex<double> sum_cplx_accumulate_(cpp11::complexes x) {
157+
return std::accumulate(x.cbegin(), x.cend(), std::complex<double>(0.0, 0.0));
158+
}
159+
160+
[[cpp11::register]] std::complex<double> sum_cplx_for2_(SEXP x_sxp) {
161+
std::complex<double> sum = {0.0, 0.0};
162+
const cpp11::complexes x(x_sxp);
163+
R_xlen_t n = x.size();
164+
for (R_xlen_t i = 0; i < n; ++i) {
165+
sum += x[i];
166+
}
167+
168+
return sum;
169+
}

0 commit comments

Comments
 (0)