Skip to content

Commit fbe1468

Browse files
authored
Merge branch 'cpp11-to-cpp4r' into nullable_extptr
2 parents 547a7bb + 3db9407 commit fbe1468

File tree

14 files changed

+1427
-13
lines changed

14 files changed

+1427
-13
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
}
@@ -216,6 +220,42 @@ sum_dbl_accumulate2_ <- function(x_sxp) {
216220
.Call(`_cpp11test_sum_dbl_accumulate2_`, x_sxp)
217221
}
218222

223+
sum_cplx_for_ <- function(x) {
224+
.Call(`_cpp11test_sum_cplx_for_`, x)
225+
}
226+
227+
sum_cplx_for_2_ <- function(x) {
228+
.Call(`_cpp11test_sum_cplx_for_2_`, x)
229+
}
230+
231+
sum_cplx_for_3_ <- function(x_sxp) {
232+
.Call(`_cpp11test_sum_cplx_for_3_`, x_sxp)
233+
}
234+
235+
sum_cplx_for_4_ <- function(x_sxp) {
236+
.Call(`_cpp11test_sum_cplx_for_4_`, x_sxp)
237+
}
238+
239+
sum_cplx_for_5_ <- function(x_sxp) {
240+
.Call(`_cpp11test_sum_cplx_for_5_`, x_sxp)
241+
}
242+
243+
sum_cplx_for_6_ <- function(x_sxp) {
244+
.Call(`_cpp11test_sum_cplx_for_6_`, x_sxp)
245+
}
246+
247+
sum_cplx_foreach_ <- function(x) {
248+
.Call(`_cpp11test_sum_cplx_foreach_`, x)
249+
}
250+
251+
sum_cplx_accumulate_ <- function(x) {
252+
.Call(`_cpp11test_sum_cplx_accumulate_`, x)
253+
}
254+
255+
sum_cplx_for2_ <- function(x_sxp) {
256+
.Call(`_cpp11test_sum_cplx_for2_`, x_sxp)
257+
}
258+
219259
sum_int_for_ <- function(x) {
220260
.Call(`_cpp11test_sum_int_for_`, x)
221261
}

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) {
@@ -338,6 +345,55 @@ extern "C" SEXP _cpp11test_rcpp_release_(SEXP n) {
338345
return R_NilValue;
339346
END_CPP11
340347
}
348+
// roxygen1.cpp
349+
double notroxcpp1_(double x);
350+
extern "C" SEXP _cpp11test_notroxcpp1_(SEXP x) {
351+
BEGIN_CPP11
352+
return cpp11::as_sexp(notroxcpp1_(cpp11::as_cpp<cpp11::decay_t<double>>(x)));
353+
END_CPP11
354+
}
355+
// roxygen1.cpp
356+
double roxcpp2_(double x);
357+
extern "C" SEXP _cpp11test_roxcpp2_(SEXP x) {
358+
BEGIN_CPP11
359+
return cpp11::as_sexp(roxcpp2_(cpp11::as_cpp<cpp11::decay_t<double>>(x)));
360+
END_CPP11
361+
}
362+
// roxygen2.cpp
363+
double roxcpp3_(double x);
364+
extern "C" SEXP _cpp11test_roxcpp3_(SEXP x) {
365+
BEGIN_CPP11
366+
return cpp11::as_sexp(roxcpp3_(cpp11::as_cpp<cpp11::decay_t<double>>(x)));
367+
END_CPP11
368+
}
369+
// roxygen2.cpp
370+
double roxcpp4_(double x);
371+
extern "C" SEXP _cpp11test_roxcpp4_(SEXP x) {
372+
BEGIN_CPP11
373+
return cpp11::as_sexp(roxcpp4_(cpp11::as_cpp<cpp11::decay_t<double>>(x)));
374+
END_CPP11
375+
}
376+
// roxygen3.cpp
377+
double roxcpp5_(double x);
378+
extern "C" SEXP _cpp11test_roxcpp5_(SEXP x) {
379+
BEGIN_CPP11
380+
return cpp11::as_sexp(roxcpp5_(cpp11::as_cpp<cpp11::decay_t<double>>(x)));
381+
END_CPP11
382+
}
383+
// roxygen3.cpp
384+
double notroxcpp6_(double x);
385+
extern "C" SEXP _cpp11test_notroxcpp6_(SEXP x) {
386+
BEGIN_CPP11
387+
return cpp11::as_sexp(notroxcpp6_(cpp11::as_cpp<cpp11::decay_t<double>>(x)));
388+
END_CPP11
389+
}
390+
// roxygen3.cpp
391+
double roxcpp7_(double x);
392+
extern "C" SEXP _cpp11test_roxcpp7_(SEXP x) {
393+
BEGIN_CPP11
394+
return cpp11::as_sexp(roxcpp7_(cpp11::as_cpp<cpp11::decay_t<double>>(x)));
395+
END_CPP11
396+
}
341397
// safe.cpp
342398
SEXP cpp11_safe_(SEXP x_sxp);
343399
extern "C" SEXP _cpp11test_cpp11_safe_(SEXP x_sxp) {
@@ -408,6 +464,69 @@ extern "C" SEXP _cpp11test_sum_dbl_accumulate2_(SEXP x_sxp) {
408464
return cpp11::as_sexp(sum_dbl_accumulate2_(cpp11::as_cpp<cpp11::decay_t<SEXP>>(x_sxp)));
409465
END_CPP11
410466
}
467+
// sum.cpp
468+
cpp11::r_complex sum_cplx_for_(cpp11::complexes x);
469+
extern "C" SEXP _cpp11test_sum_cplx_for_(SEXP x) {
470+
BEGIN_CPP11
471+
return cpp11::as_sexp(sum_cplx_for_(cpp11::as_cpp<cpp11::decay_t<cpp11::complexes>>(x)));
472+
END_CPP11
473+
}
474+
// sum.cpp
475+
cpp11::complexes sum_cplx_for_2_(cpp11::complexes x);
476+
extern "C" SEXP _cpp11test_sum_cplx_for_2_(SEXP x) {
477+
BEGIN_CPP11
478+
return cpp11::as_sexp(sum_cplx_for_2_(cpp11::as_cpp<cpp11::decay_t<cpp11::complexes>>(x)));
479+
END_CPP11
480+
}
481+
// sum.cpp
482+
std::complex<double> sum_cplx_for_3_(cpp11::complexes x_sxp);
483+
extern "C" SEXP _cpp11test_sum_cplx_for_3_(SEXP x_sxp) {
484+
BEGIN_CPP11
485+
return cpp11::as_sexp(sum_cplx_for_3_(cpp11::as_cpp<cpp11::decay_t<cpp11::complexes>>(x_sxp)));
486+
END_CPP11
487+
}
488+
// sum.cpp
489+
std::complex<double> sum_cplx_for_4_(SEXP x_sxp);
490+
extern "C" SEXP _cpp11test_sum_cplx_for_4_(SEXP x_sxp) {
491+
BEGIN_CPP11
492+
return cpp11::as_sexp(sum_cplx_for_4_(cpp11::as_cpp<cpp11::decay_t<SEXP>>(x_sxp)));
493+
END_CPP11
494+
}
495+
// sum.cpp
496+
SEXP sum_cplx_for_5_(SEXP x_sxp);
497+
extern "C" SEXP _cpp11test_sum_cplx_for_5_(SEXP x_sxp) {
498+
BEGIN_CPP11
499+
return cpp11::as_sexp(sum_cplx_for_5_(cpp11::as_cpp<cpp11::decay_t<SEXP>>(x_sxp)));
500+
END_CPP11
501+
}
502+
// sum.cpp
503+
cpp11::complexes sum_cplx_for_6_(SEXP x_sxp);
504+
extern "C" SEXP _cpp11test_sum_cplx_for_6_(SEXP x_sxp) {
505+
BEGIN_CPP11
506+
return cpp11::as_sexp(sum_cplx_for_6_(cpp11::as_cpp<cpp11::decay_t<SEXP>>(x_sxp)));
507+
END_CPP11
508+
}
509+
// sum.cpp
510+
std::complex<double> sum_cplx_foreach_(cpp11::complexes x);
511+
extern "C" SEXP _cpp11test_sum_cplx_foreach_(SEXP x) {
512+
BEGIN_CPP11
513+
return cpp11::as_sexp(sum_cplx_foreach_(cpp11::as_cpp<cpp11::decay_t<cpp11::complexes>>(x)));
514+
END_CPP11
515+
}
516+
// sum.cpp
517+
std::complex<double> sum_cplx_accumulate_(cpp11::complexes x);
518+
extern "C" SEXP _cpp11test_sum_cplx_accumulate_(SEXP x) {
519+
BEGIN_CPP11
520+
return cpp11::as_sexp(sum_cplx_accumulate_(cpp11::as_cpp<cpp11::decay_t<cpp11::complexes>>(x)));
521+
END_CPP11
522+
}
523+
// sum.cpp
524+
std::complex<double> sum_cplx_for2_(SEXP x_sxp);
525+
extern "C" SEXP _cpp11test_sum_cplx_for2_(SEXP x_sxp) {
526+
BEGIN_CPP11
527+
return cpp11::as_sexp(sum_cplx_for2_(cpp11::as_cpp<cpp11::decay_t<SEXP>>(x_sxp)));
528+
END_CPP11
529+
}
411530
// sum_int.cpp
412531
double sum_int_for_(cpp11::integers x);
413532
extern "C" SEXP _cpp11test_sum_int_for_(SEXP x) {
@@ -540,6 +659,7 @@ static const R_CallMethodDef CallEntries[] = {
540659
{"_cpp11test_mat_mat_copy_dimnames", (DL_FUNC) &_cpp11test_mat_mat_copy_dimnames, 1},
541660
{"_cpp11test_mat_mat_create_dimnames", (DL_FUNC) &_cpp11test_mat_mat_create_dimnames, 0},
542661
{"_cpp11test_mat_sexp_copy_dimnames", (DL_FUNC) &_cpp11test_mat_sexp_copy_dimnames, 1},
662+
{"_cpp11test_grow_cplx_", (DL_FUNC) &_cpp11test_grow_cplx_, 1},
543663
{"_cpp11test_my_message", (DL_FUNC) &_cpp11test_my_message, 2},
544664
{"_cpp11test_my_message_n1", (DL_FUNC) &_cpp11test_my_message_n1, 1},
545665
{"_cpp11test_my_message_n1fmt", (DL_FUNC) &_cpp11test_my_message_n1fmt, 1},
@@ -555,6 +675,8 @@ static const R_CallMethodDef CallEntries[] = {
555675
{"_cpp11test_nullable_extptr_1", (DL_FUNC) &_cpp11test_nullable_extptr_1, 0},
556676
{"_cpp11test_nullable_extptr_2", (DL_FUNC) &_cpp11test_nullable_extptr_2, 0},
557677
{"_cpp11test_ordered_map_to_list_", (DL_FUNC) &_cpp11test_ordered_map_to_list_, 1},
678+
{"_cpp11test_notroxcpp1_", (DL_FUNC) &_cpp11test_notroxcpp1_, 1},
679+
{"_cpp11test_notroxcpp6_", (DL_FUNC) &_cpp11test_notroxcpp6_, 1},
558680
{"_cpp11test_protect_many_", (DL_FUNC) &_cpp11test_protect_many_, 1},
559681
{"_cpp11test_protect_many_cpp11_", (DL_FUNC) &_cpp11test_protect_many_cpp11_, 1},
560682
{"_cpp11test_protect_many_preserve_", (DL_FUNC) &_cpp11test_protect_many_preserve_, 1},
@@ -573,8 +695,22 @@ static const R_CallMethodDef CallEntries[] = {
573695
{"_cpp11test_rcpp_sum_int_for_", (DL_FUNC) &_cpp11test_rcpp_sum_int_for_, 1},
574696
{"_cpp11test_remove_altrep", (DL_FUNC) &_cpp11test_remove_altrep, 1},
575697
{"_cpp11test_row_sums", (DL_FUNC) &_cpp11test_row_sums, 1},
698+
{"_cpp11test_roxcpp2_", (DL_FUNC) &_cpp11test_roxcpp2_, 1},
699+
{"_cpp11test_roxcpp3_", (DL_FUNC) &_cpp11test_roxcpp3_, 1},
700+
{"_cpp11test_roxcpp4_", (DL_FUNC) &_cpp11test_roxcpp4_, 1},
701+
{"_cpp11test_roxcpp5_", (DL_FUNC) &_cpp11test_roxcpp5_, 1},
702+
{"_cpp11test_roxcpp7_", (DL_FUNC) &_cpp11test_roxcpp7_, 1},
576703
{"_cpp11test_string_proxy_assignment_", (DL_FUNC) &_cpp11test_string_proxy_assignment_, 0},
577704
{"_cpp11test_string_push_back_", (DL_FUNC) &_cpp11test_string_push_back_, 0},
705+
{"_cpp11test_sum_cplx_accumulate_", (DL_FUNC) &_cpp11test_sum_cplx_accumulate_, 1},
706+
{"_cpp11test_sum_cplx_for2_", (DL_FUNC) &_cpp11test_sum_cplx_for2_, 1},
707+
{"_cpp11test_sum_cplx_for_", (DL_FUNC) &_cpp11test_sum_cplx_for_, 1},
708+
{"_cpp11test_sum_cplx_for_2_", (DL_FUNC) &_cpp11test_sum_cplx_for_2_, 1},
709+
{"_cpp11test_sum_cplx_for_3_", (DL_FUNC) &_cpp11test_sum_cplx_for_3_, 1},
710+
{"_cpp11test_sum_cplx_for_4_", (DL_FUNC) &_cpp11test_sum_cplx_for_4_, 1},
711+
{"_cpp11test_sum_cplx_for_5_", (DL_FUNC) &_cpp11test_sum_cplx_for_5_, 1},
712+
{"_cpp11test_sum_cplx_for_6_", (DL_FUNC) &_cpp11test_sum_cplx_for_6_, 1},
713+
{"_cpp11test_sum_cplx_foreach_", (DL_FUNC) &_cpp11test_sum_cplx_foreach_, 1},
578714
{"_cpp11test_sum_dbl_accumulate2_", (DL_FUNC) &_cpp11test_sum_dbl_accumulate2_, 1},
579715
{"_cpp11test_sum_dbl_accumulate_", (DL_FUNC) &_cpp11test_sum_dbl_accumulate_, 1},
580716
{"_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)