# NOT RUN {
library(Rcpp)
code = "
// [[Rcpp::plugins(cpp11)]]
#include <Rcpp.h>
using namespace std;
using namespace Rcpp;
// drop-in replacement for unordered_map
//#include <unordered_map>
#include <sparsepp/spp.h>
//[[Rcpp::depends(sparsepp)]]
using spp::sparse_hash_map;
// @export
// [[Rcpp::export]]
IntegerVector word_count(CharacterVector v) {
//unordered_map<string, int> smap;
sparse_hash_map<string, int> smap;
for(auto x: v) {
smap[as<string>(x)] ++;
}
IntegerVector res(smap.size());
int i = 0;
for(auto s:smap) {
res[i]=s.second;
i++;
}
return(res);
}"
f = tempfile(, fileext = ".cpp")
writeLines(code, f)
sourceCpp(f)
unlink(f)
word_count(sample(letters, 100, T))
# }
Run the code above in your browser using DataLab