-
Notifications
You must be signed in to change notification settings - Fork 118
Open
Description
For example, in section 20.3 Q6, the following code creates a version of union() in C++:
#include <Rcpp.h>
#include <unordered_set>
#include <algorithm>
using namespace Rcpp;
// [[Rcpp::plugins(cpp11)]]
// [[Rcpp::export]]
IntegerVector unionC(IntegerVector x, IntegerVector y) {
int nx = x.size();
int ny = y.size();
IntegerVector tmp(nx + ny);
std::sort(x.begin(), x.end()); // unique
std::sort(y.begin(), y.end());
IntegerVector::iterator out_end = std::set_union(
x.begin(), x.end(), y.begin(), y.end(), tmp.begin()
);
int prev_value = 0;
IntegerVector out;
for (IntegerVector::iterator it = tmp.begin();
it != out_end; ++it) {
if ((it != tmp.begin()) && (prev_value == *it)) continue;
out.push_back(*it);
prev_value = *it;
}
return out;
}But it doesn't produce the same output as its R equivalent:
# input vectors include duplicates
x <- c(1, 4, 5, 5, 5, 6, 2)
y <- c(4, 1, 6, 8)
union(x, y)
#> [1] 1 4 5 6 2 8
unionC(x, y)
#> [1] 1 2 4 5 6 8
Metadata
Metadata
Assignees
Labels
No labels