many-to-many multi-modal routing aggregator
1
2#include "utils.h"
3
4struct OneMaxValue : public RcppParallel::Worker
5{
6 // This fails to compile without the "dummy" variable. Seems to be a bug in
7 // RcppParallel?
8 const RcppParallel::RMatrix <double> m;
9 const int dummy;
10
11 RcppParallel::RVector <double> res;
12
13 // constructor
14 OneMaxValue (
15 const RcppParallel::RMatrix <double> m_in,
16 const int dummy_in,
17 RcppParallel::RVector <double> res_in) :
18 m (m_in), dummy (dummy_in), res (res_in)
19 {
20 }
21
22 // Parallel function operator
23 void operator() (std::size_t begin, std::size_t end)
24 {
25 for (std::size_t i = begin; i < end; i++)
26 {
27 const RcppParallel::RMatrix <double>::Column col_i = m.column (i);
28 std::vector <double> col_i_vec (col_i.size ());
29 std::copy (col_i.begin (), col_i.end (), col_i_vec.begin ());
30
31 std::vector <double>::iterator it =
32 std::max_element (col_i_vec.begin (), col_i_vec.end (),
33 NaNAwareLess <double> ());
34
35 res [i] = *it;
36 }
37 }
38
39};
40
41
42//' rcpp_matrix_max
43//'
44//' Parallel version of `max(mat)`.
45//' @noRd
46// [[Rcpp::export]]
47const double rcpp_matrix_max (Rcpp::NumericMatrix mat)
48{
49 const int dummy = 0;
50
51 const size_t nverts = static_cast <size_t> (mat.ncol ());
52
53 Rcpp::NumericVector res (nverts);
54
55 OneMaxValue one_closest (RcppParallel::RMatrix <double> (mat), dummy,
56 RcppParallel::RVector <double> (res));
57
58 RcppParallel::parallelFor (0, nverts, one_closest);
59
60 std::vector <double> res_vec (nverts);
61 std::copy (res.begin (), res.end (), res_vec.begin ());
62 std::vector <double>::iterator it =
63 std::max_element (res_vec.begin (), res_vec.end (),
64 NaNAwareLess <double> ());
65
66 return *it;
67}