Skip to content

Commit adb861e

Browse files
committed
Remove w/ redirect
1 parent 0c6964a commit adb861e

5 files changed

Lines changed: 37 additions & 27 deletions

File tree

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ LinkingTo: cpp11
1919
```
2020

2121
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.*
2323

2424
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.
2525

vignettes/FAQ.Rmd

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ is_named(c(x = "foo"))
153153

154154
#### 7. How do I return a `cpp11::writable::logicals` object with only a `FALSE` value?
155155

156-
You need to use [list initialization](https://en.cppreference.com/w/cpp/language/list_initialization.html) with `{}` to create the object.
156+
You need to use [list initialization](https://en.cppreference.com/cpp/language/list_initialization.html) with `{}` to create the object.
157157

158158
```{cpp11}
159159
#include <cpp11.hpp>

vignettes/cpp11.Rmd

Lines changed: 27 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ Typical bottlenecks that C++ can address include:
4545
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.
4646
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.
4747
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>.
4949
For more advanced topics, the *Effective C++* series by Scott Meyers is a popular choice.
5050

5151
### Outline
@@ -87,10 +87,12 @@ To get it:
8787
`cpp_function()` allows you to write C++ functions in R:
8888

8989
```{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) {
9192
int sum = x + y + z;
9293
return sum;
93-
}')
94+
}'
95+
)
9496
# add works like a regular R function
9597
add
9698
add(1, 2, 3)
@@ -127,9 +129,11 @@ int one() {
127129
We can compile and use this from R with `cpp_function()`
128130

129131
```{r one-cpp}
130-
cpp_function('int one() {
132+
cpp_function(
133+
'int one() {
131134
return 1;
132-
}')
135+
}'
136+
)
133137
```
134138

135139
This small function illustrates a number of important differences between R and C++:
@@ -161,15 +165,17 @@ sign_r <- function(x) {
161165
-1
162166
}
163167
}
164-
cpp_function('int sign_cpp(int x) {
168+
cpp_function(
169+
'int sign_cpp(int x) {
165170
if (x > 0) {
166171
return 1;
167172
} else if (x == 0) {
168173
return 0;
169174
} else {
170175
return -1;
171176
}
172-
}')
177+
}'
178+
)
173179
```
174180

175181
In the C++ version:
@@ -201,14 +207,16 @@ In C++, loops have very little overhead, so it's fine to use them.
201207
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.
202208

203209
```{r sum-cpp}
204-
cpp_function('double sum_cpp(doubles x) {
210+
cpp_function(
211+
'double sum_cpp(doubles x) {
205212
int n = x.size();
206213
double total = 0;
207214
for(int i = 0; i < n; ++i) {
208215
total += x[i];
209216
}
210217
return total;
211-
}')
218+
}'
219+
)
212220
```
213221

214222
The C++ version is similar, but:
@@ -250,22 +258,24 @@ Next we'll create a function that computes the Euclidean distance between a valu
250258

251259
```{r pdist-r}
252260
pdist_r <- function(x, ys) {
253-
sqrt((x - ys) ^ 2)
261+
sqrt((x - ys)^2)
254262
}
255263
```
256264

257265
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.
258266
That's not a problem in the C++ version because we have to be explicit about types:
259267

260268
```{r pdist-cpp}
261-
cpp_function('doubles pdist_cpp(double x, doubles ys) {
269+
cpp_function(
270+
'doubles pdist_cpp(double x, doubles ys) {
262271
int n = ys.size();
263272
writable::doubles out(n);
264273
for(int i = 0; i < n; ++i) {
265274
out[i] = sqrt(pow(ys[i] - x, 2.0));
266275
}
267276
return out;
268-
}')
277+
}'
278+
)
269279
```
270280

271281
This function introduces a few new concepts:
@@ -510,7 +520,7 @@ f("x"_nm = "y", "value"_nm = 1);
510520
All R objects have attributes, which can be queried and modified with `.attr()`.
511521
cpp11 also provides `.names()` as an alias for the `names` attribute.
512522
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.
514524
This allows you to create an R vector from C++ scalar values:
515525

516526
```{r attribs, engine = "cpp11"}
@@ -736,7 +746,7 @@ local({
736746
### Algorithms
737747

738748
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>.
740750
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.
741751
This shows off a few more advanced iterator features.
742752
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
790800
We'll focus on these three in this section, but using the others is similar: they just have different performance trade-offs.
791801
For example, the `deque` (pronounced "deck") has a very similar interface to vectors but a different underlying implementation that has different performance trade-offs.
792802
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.
794804

795805
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.
796806

@@ -843,7 +853,7 @@ list rle_cpp(doubles x) {
843853
(An alternative implementation would be to replace `i` with the iterator `lengths.rbegin()` which always points to the last element of the vector.
844854
You might want to try implementing that.)
845855

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>.
847857

848858
### Sets
849859

@@ -854,7 +864,7 @@ Unordered sets can somtimes be much faster (because they use a hash table intern
854864
Often even if you need an ordered set, you could consider using an unordered set and then sorting the output.
855865
Benchmarking with your expected dataset is the best way to determine which is fastest for your data.
856866
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>.
858868

859869
The following function uses an unordered set to implement an equivalent to `duplicated()` for integer vectors.
860870
Note the use of `seen.insert(x[i]).second`.

vignettes/internals.Rmd

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ The various methods for both functions are defined in [cpp11/as.hpp](https://git
108108

109109
This is definitely the most complex part of the cpp11 code, with extensive use of [template metaprogramming](https://en.wikipedia.org/wiki/Template_metaprogramming).
110110
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.
112112

113113
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.
114114

vignettes/motivations.Rmd

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ Exceptions provide a clean, safe way for objects to obtain and cleanup resources
172172
### C safety
173173

174174
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.
176176

177177
`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.
178178
R uses long jumps extensively in its error handling routines.
@@ -339,12 +339,12 @@ Concretely cpp11 always uses `Rf_translateCharUTF8()` when obtaining `const char
339339
C++11 provides a host of new features to the C++ language.
340340
cpp11 uses a number of these including
341341

342-
- [move semantics](https://en.cppreference.com/w/cpp/language/move_constructor.html)
343-
- [type traits](https://en.cppreference.com/w/cpp/header/type_traits.html)
344-
- [initializer_list](https://en.cppreference.com/w/cpp/utility/initializer_list.html)
345-
- [variadic templates / parameter packs](https://en.cppreference.com/w/cpp/language/parameter_pack.html)
346-
- [user defined literals](https://en.cppreference.com/w/cpp/language/user_literal.html)
347-
- [user defined attributes](https://en.cppreference.com/w/cpp/language/attributes.html)
342+
- [move semantics](https://en.cppreference.com/cpp/language/move_constructor.html)
343+
- [type traits](https://en.cppreference.com/cpp/header/type_traits.html)
344+
- [initializer_list](https://en.cppreference.com/cpp/utility/initializer_list.html)
345+
- [variadic templates / parameter packs](https://en.cppreference.com/cpp/language/parameter_pack.html)
346+
- [user defined literals](https://en.cppreference.com/cpp/language/user_literal.html)
347+
- [user defined attributes](https://en.cppreference.com/cpp/language/attributes.html)
348348

349349
## Simpler implementation {#simpler-implementation}
350350

0 commit comments

Comments
 (0)