Skip to content

Commit b3de1ae

Browse files
committed
cleanup
1 parent 5e0967a commit b3de1ae

File tree

3 files changed

+24
-24
lines changed

3 files changed

+24
-24
lines changed

R/RcppExports.R

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ grid_swarm_ <- function(xs, xsize, ysize, ygrid, side) {
8888
#' @param y sorted numeric vector of dot heights, same length as x
8989
#' @returns modified `y`
9090
#' @noRd
91-
recenter_swarm_clusters_ <- function(x, y, binwidth) {
92-
.Call(`_ggdist_recenter_swarm_clusters_`, x, y, binwidth)
91+
recenter_swarm_clusters_ <- function(x_vec, y_vec, binwidth) {
92+
.Call(`_ggdist_recenter_swarm_clusters_`, x_vec, y_vec, binwidth)
9393
}
9494

src/RcppExports.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,14 +36,14 @@ BEGIN_RCPP
3636
END_RCPP
3737
}
3838
// recenter_swarm_clusters_
39-
SEXP recenter_swarm_clusters_(Rcpp::NumericVector& x, Rcpp::NumericVector& y, const double binwidth);
40-
RcppExport SEXP _ggdist_recenter_swarm_clusters_(SEXP xSEXP, SEXP ySEXP, SEXP binwidthSEXP) {
39+
SEXP recenter_swarm_clusters_(Rcpp::NumericVector& x_vec, Rcpp::NumericVector& y_vec, const double binwidth);
40+
RcppExport SEXP _ggdist_recenter_swarm_clusters_(SEXP x_vecSEXP, SEXP y_vecSEXP, SEXP binwidthSEXP) {
4141
BEGIN_RCPP
4242
Rcpp::RObject rcpp_result_gen;
43-
Rcpp::traits::input_parameter< Rcpp::NumericVector& >::type x(xSEXP);
44-
Rcpp::traits::input_parameter< Rcpp::NumericVector& >::type y(ySEXP);
43+
Rcpp::traits::input_parameter< Rcpp::NumericVector& >::type x_vec(x_vecSEXP);
44+
Rcpp::traits::input_parameter< Rcpp::NumericVector& >::type y_vec(y_vecSEXP);
4545
Rcpp::traits::input_parameter< const double >::type binwidth(binwidthSEXP);
46-
rcpp_result_gen = Rcpp::wrap(recenter_swarm_clusters_(x, y, binwidth));
46+
rcpp_result_gen = Rcpp::wrap(recenter_swarm_clusters_(x_vec, y_vec, binwidth));
4747
return rcpp_result_gen;
4848
END_RCPP
4949
}

src/bin_dots.cpp

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
constexpr auto INF = std::numeric_limits<double>::infinity();
1313

1414
template<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)]]
4242
Rcpp::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)]]
316316
SEXP 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

Comments
 (0)