You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+1-1Lines changed: 1 addition & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -19,7 +19,7 @@ LinkingTo: cpp11
19
19
```
20
20
21
21
Then decorate C++ functions you want to expose to R with `[[cpp11::register]]`.
22
-
*Note that this is a [C++11 attribute](https://en.cppreference.com/w/cpp/language/attributes.html), not a comment like is used in Rcpp.*
22
+
*Note that this is a [C++11 attribute](https://en.cppreference.com/cpp/language/attributes.html), not a comment like is used in Rcpp.*
23
23
24
24
cpp11 is a header only library with no hard dependencies and does not use a shared library, so it is straightforward and reliable to use in packages without fear of compile-time and run-time mismatches.
Copy file name to clipboardExpand all lines: vignettes/cpp11.Rmd
+27-17Lines changed: 27 additions & 17 deletions
Original file line number
Diff line number
Diff line change
@@ -45,7 +45,7 @@ Typical bottlenecks that C++ can address include:
45
45
The aim of this vignette is to discuss only those aspects of C++ and cpp11 that are absolutely necessary to help you eliminate bottlenecks in your code.
46
46
We won't spend much time on advanced features like object-oriented programming or templates because the focus is on writing small, self-contained functions, not big programs.
47
47
A working knowledge of C++ is helpful, but not essential.
48
-
Many good tutorials and references are freely available, including <https://www.learncpp.com/> and <https://en.cppreference.com/w/cpp.html>.
48
+
Many good tutorials and references are freely available, including <https://www.learncpp.com/> and <https://en.cppreference.com/cpp>.
49
49
For more advanced topics, the *Effective C++* series by Scott Meyers is a popular choice.
50
50
51
51
### Outline
@@ -87,10 +87,12 @@ To get it:
87
87
`cpp_function()` allows you to write C++ functions in R:
88
88
89
89
```{r add}
90
-
cpp_function('int add(int x, int y, int z) {
90
+
cpp_function(
91
+
'int add(int x, int y, int z) {
91
92
int sum = x + y + z;
92
93
return sum;
93
-
}')
94
+
}'
95
+
)
94
96
# add works like a regular R function
95
97
add
96
98
add(1, 2, 3)
@@ -127,9 +129,11 @@ int one() {
127
129
We can compile and use this from R with `cpp_function()`
128
130
129
131
```{r one-cpp}
130
-
cpp_function('int one() {
132
+
cpp_function(
133
+
'int one() {
131
134
return 1;
132
-
}')
135
+
}'
136
+
)
133
137
```
134
138
135
139
This small function illustrates a number of important differences between R and C++:
@@ -161,15 +165,17 @@ sign_r <- function(x) {
161
165
-1
162
166
}
163
167
}
164
-
cpp_function('int sign_cpp(int x) {
168
+
cpp_function(
169
+
'int sign_cpp(int x) {
165
170
if (x > 0) {
166
171
return 1;
167
172
} else if (x == 0) {
168
173
return 0;
169
174
} else {
170
175
return -1;
171
176
}
172
-
}')
177
+
}'
178
+
)
173
179
```
174
180
175
181
In the C++ version:
@@ -201,14 +207,16 @@ In C++, loops have very little overhead, so it's fine to use them.
201
207
In Section [stl](#stl), you'll see alternatives to `for` loops that more clearly express your intent; they're not faster, but they can make your code easier to understand.
202
208
203
209
```{r sum-cpp}
204
-
cpp_function('double sum_cpp(doubles x) {
210
+
cpp_function(
211
+
'double sum_cpp(doubles x) {
205
212
int n = x.size();
206
213
double total = 0;
207
214
for(int i = 0; i < n; ++i) {
208
215
total += x[i];
209
216
}
210
217
return total;
211
-
}')
218
+
}'
219
+
)
212
220
```
213
221
214
222
The C++ version is similar, but:
@@ -250,22 +258,24 @@ Next we'll create a function that computes the Euclidean distance between a valu
250
258
251
259
```{r pdist-r}
252
260
pdist_r <- function(x, ys) {
253
-
sqrt((x - ys) ^ 2)
261
+
sqrt((x - ys)^2)
254
262
}
255
263
```
256
264
257
265
In R, it's not obvious that we want `x` to be a scalar from the function definition, and we'd need to make that clear in the documentation.
258
266
That's not a problem in the C++ version because we have to be explicit about types:
All R objects have attributes, which can be queried and modified with `.attr()`.
511
521
cpp11 also provides `.names()` as an alias for the `names` attribute.
512
522
The following code snippet illustrates these methods.
513
-
Note the use of `{}`[initializer list](https://en.cppreference.com/w/cpp/utility/initializer_list.html) syntax.
523
+
Note the use of `{}`[initializer list](https://en.cppreference.com/cpp/utility/initializer_list.html) syntax.
514
524
This allows you to create an R vector from C++ scalar values:
515
525
516
526
```{r attribs, engine = "cpp11"}
@@ -736,7 +746,7 @@ local({
736
746
### Algorithms
737
747
738
748
The `<algorithm>` header provides a large number of algorithms that work with iterators.
739
-
A good reference is available at <https://en.cppreference.com/w/cpp/algorithm.html>.
749
+
A good reference is available at <https://en.cppreference.com/cpp/algorithm.html>.
740
750
For example, we could write a basic cpp11 version of `findInterval()` that takes two arguments, a vector of values and a vector of breaks, and locates the bin that each x falls into.
741
751
This shows off a few more advanced iterator features.
742
752
Read the code below and see if you can figure out how it works.
@@ -790,7 +800,7 @@ The most important of these data structures are the `vector`, the `unordered_set
790
800
We'll focus on these three in this section, but using the others is similar: they just have different performance trade-offs.
791
801
For example, the `deque` (pronounced "deck") has a very similar interface to vectors but a different underlying implementation that has different performance trade-offs.
792
802
You may want to try it for your problem.
793
-
A good reference for STL data structures is <https://en.cppreference.com/w/cpp/container.html> --- I recommend you keep it open while working with the STL.
803
+
A good reference for STL data structures is <https://en.cppreference.com/cpp/container.html> --- I recommend you keep it open while working with the STL.
794
804
795
805
cpp11 knows how to convert from many STL data structures to their R equivalents, so you can return them from your functions without explicitly converting to R data structures.
796
806
@@ -843,7 +853,7 @@ list rle_cpp(doubles x) {
843
853
(An alternative implementation would be to replace `i` with the iterator `lengths.rbegin()` which always points to the last element of the vector.
844
854
You might want to try implementing that.)
845
855
846
-
Other methods of a vector are described at <https://en.cppreference.com/w/cpp/container/vector.html>.
856
+
Other methods of a vector are described at <https://en.cppreference.com/cpp/container/vector.html>.
847
857
848
858
### Sets
849
859
@@ -854,7 +864,7 @@ Unordered sets can somtimes be much faster (because they use a hash table intern
854
864
Often even if you need an ordered set, you could consider using an unordered set and then sorting the output.
855
865
Benchmarking with your expected dataset is the best way to determine which is fastest for your data.
856
866
Like vectors, sets are templated, so you need to request the appropriate type of set for your purpose: `unordered_set<int>`, `unordered_set<bool>`, etc.
857
-
More details are available at <https://en.cppreference.com/w/cpp/container/set.html> and <https://en.cppreference.com/w/cpp/container/unordered_set.html>.
867
+
More details are available at <https://en.cppreference.com/cpp/container/set.html> and <https://en.cppreference.com/cpp/container/unordered_set.html>.
858
868
859
869
The following function uses an unordered set to implement an equivalent to `duplicated()` for integer vectors.
Copy file name to clipboardExpand all lines: vignettes/internals.Rmd
+1-1Lines changed: 1 addition & 1 deletion
Original file line number
Diff line number
Diff line change
@@ -108,7 +108,7 @@ The various methods for both functions are defined in [cpp11/as.hpp](https://git
108
108
109
109
This is definitely the most complex part of the cpp11 code, with extensive use of [template metaprogramming](https://en.wikipedia.org/wiki/Template_metaprogramming).
110
110
In particular the [substitution failure is not an error (SFINAE)](https://en.wikipedia.org/wiki/Substitution_failure_is_not_an_error) technique is used to control overloading of the functions.
111
-
If we could use C++20 a lot of this code would be made simpler with [Concepts](https://en.cppreference.com/w/cpp/language/constraints.html), but alas.
111
+
If we could use C++20 a lot of this code would be made simpler with [Concepts](https://en.cppreference.com/cpp/language/constraints.html), but alas.
112
112
113
113
The most common C++ types are included in the test suite and should work without issues, as more exotic types are used in real projects additional issues may arise.
Copy file name to clipboardExpand all lines: vignettes/motivations.Rmd
+7-7Lines changed: 7 additions & 7 deletions
Original file line number
Diff line number
Diff line change
@@ -172,7 +172,7 @@ Exceptions provide a clean, safe way for objects to obtain and cleanup resources
172
172
### C safety
173
173
174
174
The C language does not have support for exceptions, so error handling is done a variety of ways.
175
-
These include error codes like [errno](https://en.cppreference.com/w/c/error/errno), conditional statements, and in the R codebase the [longjmp](https://cplusplus.com/reference/csetjmp/longjmp/) function.
175
+
These include error codes like [errno](https://en.cppreference.com/c/error/errno), conditional statements, and in the R codebase the [longjmp](https://cplusplus.com/reference/csetjmp/longjmp/) function.
176
176
177
177
`longjmp`, which stands for 'long jump' is a function that allows you to transfer the control flow of a program to another location elsewhere in the program.
178
178
R uses long jumps extensively in its error handling routines.
0 commit comments