1212constexpr auto INF = std::numeric_limits<double >::infinity();
1313
1414template <typename Numeric>
15- constexpr auto EPS ( Numeric x) -> Numeric {
15+ constexpr auto relative_eps ( const Numeric x) -> Numeric {
1616 return 8 * x * std::numeric_limits<Numeric>::epsilon ();
1717}
1818
@@ -41,7 +41,7 @@ constexpr R_xlen_t operator""_rz(unsigned long long n) {
4141// [[Rcpp::export(rng = false)]]
4242Rcpp::IntegerVector wilkinson_bin_to_right_ (const Rcpp::NumericVector& x, const double width) {
4343 const auto n = x.size ();
44- const auto eps = EPS (width);
44+ const auto eps = relative_eps (width);
4545
4646 auto bins = Rcpp::IntegerVector (n);
4747 auto current_bin = 1_rz;
@@ -79,7 +79,7 @@ inline auto place_candidate(
7979 std::vector<std::multiset<double >>& rows,
8080 const std::ptrdiff_t target_row_i
8181) -> bool {
82- const auto eps = EPS (xsize);
82+ const auto eps = relative_eps (xsize);
8383
8484 auto & target_row = rows[target_row_i];
8585 auto insert_loc = target_row.begin ();
@@ -90,7 +90,7 @@ inline auto place_candidate(
9090 // iterate in reverse because we will often have a quick exit by comparison to the
9191 // most recently placed dot
9292 for (auto i = last; i-- > first; ) {
93- auto & row = rows[i];
93+ const auto & row = rows[i];
9494 if (row.size () == 0_uz) continue ;
9595
9696 const auto rows_from_target = static_cast <double >(std::abs (i - target_row_i));
@@ -314,23 +314,23 @@ SEXP grid_swarm_(
314314// ' @noRd
315315// [[Rcpp::export(rng = false)]]
316316SEXP recenter_swarm_clusters_ (
317- Rcpp::NumericVector& x ,
318- Rcpp::NumericVector& y ,
317+ Rcpp::NumericVector& x_vec ,
318+ Rcpp::NumericVector& y_vec ,
319319 const double binwidth
320320) {
321+ auto n = static_cast <std::size_t >(x_vec.size ());
322+ auto x = REAL (x_vec);
323+ auto y = REAL (y_vec);
321324 auto bin_sum = 0.0 ;
322- auto bin_n = 0.0 ;
323- auto bin_start = 0_rz;
324- for (auto bin_end = 0_rz; bin_end < x.size (); ++bin_end) {
325- bin_sum += y[bin_end];
326- bin_n += 1.0 ;
327- if (bin_end == x.size () - 1_rz || x[bin_end + 1_rz] - x[bin_end] >= binwidth) {
328- auto mean = bin_sum / bin_n;
329- for (auto i = bin_start; i <= bin_end; ++i) y[i] -= mean;
330- bin_start = bin_end + 1_rz;
325+ auto bin_start = 0_uz;
326+ for (auto bin_end = 1_uz; bin_end <= n; ++bin_end) {
327+ bin_sum += y[bin_end - 1_uz];
328+ if (bin_end == n || x[bin_end] - x[bin_end - 1_uz] >= binwidth) {
329+ auto mean = bin_sum / static_cast <double >(bin_end - bin_start);
330+ for (auto i = bin_start; i < bin_end; ++i) y[i] -= mean;
331+ bin_start = bin_end;
331332 bin_sum = 0.0 ;
332- bin_n = 0.0 ;
333333 }
334334 }
335- return y ;
335+ return y_vec ;
336336}
0 commit comments