Skip to content

Commit 5e7fa40

Browse files
committed
Merge pull request #15 from pachadotdev/complex
Complex
1 parent d97b722 commit 5e7fa40

File tree

4 files changed

+55
-14
lines changed

4 files changed

+55
-14
lines changed

DESCRIPTION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
Package: cpp11
22
Title: A C++11 Interface for R's C Interface
3-
Version: 0.5.2.9000
3+
Version: 0.6.0.9000
44
Authors@R:
55
c(
66
person("Davis", "Vaughan", email = "davis@posit.co", role = c("aut", "cre"), comment = c(ORCID = "0000-0003-4777-038X")),

cpp11test/src/cpp11.cpp

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -701,12 +701,12 @@ static const R_CallMethodDef CallEntries[] = {
701701
{"_cpp11test_gibbs_rcpp2", (DL_FUNC) &_cpp11test_gibbs_rcpp2, 2},
702702
{"_cpp11test_grow_", (DL_FUNC) &_cpp11test_grow_, 1},
703703
{"_cpp11test_grow_cplx_", (DL_FUNC) &_cpp11test_grow_cplx_, 1},
704-
{"_cpp11test_grow_strings_cpp11_", (DL_FUNC) &_cpp11test_grow_strings_cpp11_, 2},
705-
{"_cpp11test_grow_strings_manual_", (DL_FUNC) &_cpp11test_grow_strings_manual_, 2},
706-
{"_cpp11test_grow_strings_rcpp_", (DL_FUNC) &_cpp11test_grow_strings_rcpp_, 2},
707704
{"_cpp11test_mat_mat_copy_dimnames", (DL_FUNC) &_cpp11test_mat_mat_copy_dimnames, 1},
708705
{"_cpp11test_mat_mat_create_dimnames", (DL_FUNC) &_cpp11test_mat_mat_create_dimnames, 0},
709706
{"_cpp11test_mat_sexp_copy_dimnames", (DL_FUNC) &_cpp11test_mat_sexp_copy_dimnames, 1},
707+
{"_cpp11test_grow_strings_cpp11_", (DL_FUNC) &_cpp11test_grow_strings_cpp11_, 2},
708+
{"_cpp11test_grow_strings_manual_", (DL_FUNC) &_cpp11test_grow_strings_manual_, 2},
709+
{"_cpp11test_grow_strings_rcpp_", (DL_FUNC) &_cpp11test_grow_strings_rcpp_, 2},
710710
{"_cpp11test_my_message", (DL_FUNC) &_cpp11test_my_message, 2},
711711
{"_cpp11test_my_message_n1", (DL_FUNC) &_cpp11test_my_message_n1, 1},
712712
{"_cpp11test_my_message_n1fmt", (DL_FUNC) &_cpp11test_my_message_n1fmt, 1},
@@ -719,12 +719,10 @@ static const R_CallMethodDef CallEntries[] = {
719719
{"_cpp11test_my_warning_n1", (DL_FUNC) &_cpp11test_my_warning_n1, 1},
720720
{"_cpp11test_my_warning_n1fmt", (DL_FUNC) &_cpp11test_my_warning_n1fmt, 1},
721721
{"_cpp11test_my_warning_n2fmt", (DL_FUNC) &_cpp11test_my_warning_n2fmt, 2},
722-
{"_cpp11test_notroxcpp1_", (DL_FUNC) &_cpp11test_notroxcpp1_, 1},
723-
{"_cpp11test_notroxcpp6_", (DL_FUNC) &_cpp11test_notroxcpp6_, 1},
724-
{"_cpp11test_nullable_extptr_1", (DL_FUNC) &_cpp11test_nullable_extptr_1, 0},
725-
{"_cpp11test_nullable_extptr_2", (DL_FUNC) &_cpp11test_nullable_extptr_2, 0},
726722
{"_cpp11test_ordered_map_to_list_", (DL_FUNC) &_cpp11test_ordered_map_to_list_, 1},
727723
{"_cpp11test_ordered_map_to_list_2_", (DL_FUNC) &_cpp11test_ordered_map_to_list_2_, 1},
724+
{"_cpp11test_nullable_extptr_1", (DL_FUNC) &_cpp11test_nullable_extptr_1, 0},
725+
{"_cpp11test_nullable_extptr_2", (DL_FUNC) &_cpp11test_nullable_extptr_2, 0},
728726
{"_cpp11test_protect_many_", (DL_FUNC) &_cpp11test_protect_many_, 1},
729727
{"_cpp11test_protect_many_cpp11_", (DL_FUNC) &_cpp11test_protect_many_cpp11_, 1},
730728
{"_cpp11test_protect_many_preserve_", (DL_FUNC) &_cpp11test_protect_many_preserve_, 1},

cpp11test/src/test-matrix.cpp

Lines changed: 39 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
#include "cpp11/function.hpp"
34
#include "cpp11/integers.hpp"
@@ -24,6 +25,7 @@ context("matrix-C++") {
2425
expect_true(x[1].size() == 2);
2526
expect_true(x[1].stride() == 5);
2627
}
28+
2729
test_that("matrix dim attributes are correct for read only matrices") {
2830
auto getExportedValue = cpp11::package("base")["getExportedValue"];
2931

@@ -41,6 +43,7 @@ context("matrix-C++") {
4143
expect_true(x[1].size() == 61);
4244
expect_true(x[1].stride() == 87);
4345
}
46+
4447
test_that("matrix<by_column> attributes are correct") {
4548
cpp11::doubles_matrix<cpp11::by_column> x(getExportedValue("datasets", "volcano"));
4649

@@ -156,4 +159,40 @@ context("matrix-C++") {
156159
cpp11::writable::doubles_matrix<cpp11::by_row> x(5, 2);
157160
expect_error(cpp11::writable::integers_matrix<cpp11::by_column>(x));
158161
}
162+
163+
test_that("complex objects can be created, filled, and copied") {
164+
// vector
165+
166+
cpp11::writable::complexes v(2);
167+
v[0] = std::complex<double>(1, 2);
168+
v[1] = std::complex<double>(3, 4);
169+
170+
cpp11::complexes vc = v;
171+
172+
expect_true(v.size() == vc.size());
173+
174+
for (int i = 0; i < 2; ++i) {
175+
expect_true(v[i] == vc[i]);
176+
}
177+
178+
// matrix
179+
180+
cpp11::writable::complexes_matrix<cpp11::by_row> m(5, 2);
181+
182+
for (int i = 0; i < 5; ++i) {
183+
for (int j = 0; j < 2; ++j) {
184+
m(i, j) = std::complex<double>(i, j);
185+
}
186+
}
187+
188+
cpp11::complexes_matrix<> mc = m;
189+
expect_true(m.nrow() == mc.nrow());
190+
expect_true(m.ncol() == mc.ncol());
191+
192+
for (int i = 0; i < 5; ++i) {
193+
for (int j = 0; j < 2; ++j) {
194+
expect_true(m(i, j) == mc(i, j));
195+
}
196+
}
197+
}
159198
}

inst/include/cpp11/matrix.hpp

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@
44
#include <iterator>
55
#include <string> // for string
66

7-
#include "cpp11/R.hpp" // for SEXP, SEXPREC, R_xlen_t, INT...
8-
#include "cpp11/attribute_proxy.hpp" // for attribute_proxy
9-
#include "cpp11/r_bool.hpp" // for r_bool
10-
#include "cpp11/r_string.hpp" // for r_string
11-
#include "cpp11/r_vector.hpp" // for r_vector
12-
#include "cpp11/sexp.hpp" // for sexp
7+
#include "cpp11/R.hpp" // for SEXP, SEXPREC, R_xlen_t, INT...
8+
#include "cpp11/r_bool.hpp" // for r_bool
9+
#include "cpp11/r_complex.hpp" // for r_complex
10+
#include "cpp11/r_string.hpp" // for r_string
11+
#include "cpp11/r_vector.hpp" // for r_vector
12+
#include "cpp11/sexp.hpp" // for sexp
1313

1414
namespace cpp11 {
1515

@@ -252,6 +252,8 @@ template <typename S = by_column>
252252
using logicals_matrix = matrix<r_vector<r_bool>, r_bool, S>;
253253
template <typename S = by_column>
254254
using strings_matrix = matrix<r_vector<r_string>, r_string, S>;
255+
template <typename S = by_column>
256+
using complexes_matrix = matrix<r_vector<r_complex>, r_complex, S>;
255257

256258
namespace writable {
257259
template <typename S = by_column>
@@ -262,6 +264,8 @@ template <typename S = by_column>
262264
using logicals_matrix = matrix<r_vector<r_bool>, r_vector<r_bool>::proxy, S>;
263265
template <typename S = by_column>
264266
using strings_matrix = matrix<r_vector<r_string>, r_vector<r_string>::proxy, S>;
267+
template <typename S = by_column>
268+
using complexes_matrix = matrix<r_vector<r_complex>, r_vector<r_complex>::proxy, S>;
265269
} // namespace writable
266270

267271
// TODO: Add tests for Matrix class

0 commit comments

Comments
 (0)