Skip to content

Commit

Permalink
support for arrange() for integer64
Browse files Browse the repository at this point in the history
  • Loading branch information
romainfrancois committed May 20, 2019
1 parent a8b2e2f commit cf607f0
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 0 deletions.
28 changes: 28 additions & 0 deletions inst/include/dplyr/visitors/order/OrderVisitorImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,31 @@ class OrderCharacterVectorVisitorImpl : public OrderVisitor {
OrderVectorVisitorImpl<INTSXP, ascending, Rcpp::IntegerVector> orders;
};

// ---------- int 64

template <bool ascensing>
class OrderInt64VectorVisitor : public OrderVisitor {
public:

OrderInt64VectorVisitor(const Rcpp::NumericVector& vec_) :
vec(vec_),
data(reinterpret_cast<int64_t*>(vec.begin()))
{}

inline bool equal(int i, int j) const {
return comparisons_int64::equal_or_both_na(data[i], data[j]);
}

inline bool before(int i, int j) const {
return ascensing ? comparisons_int64::is_less(data[i], data[j]) : comparisons_int64::is_greater(data[i], data[j]);
}

private:
Rcpp::NumericVector vec;
int64_t* data;
};


// ---------- data frame columns

// ascending = true
Expand Down Expand Up @@ -229,6 +254,9 @@ inline OrderVisitor* order_visitor_asc_vector(SEXP vec) {
case INTSXP:
return new OrderVectorVisitorImpl<INTSXP, ascending, Rcpp::Vector<INTSXP > >(vec);
case REALSXP:
if (Rf_inherits(vec, "integer64")) {
return new OrderInt64VectorVisitor<ascending>(vec);
}
return new OrderVectorVisitorImpl<REALSXP, ascending, Rcpp::Vector<REALSXP> >(vec);
case LGLSXP:
return new OrderVectorVisitorImpl<LGLSXP, ascending, Rcpp::Vector<LGLSXP > >(vec);
Expand Down
23 changes: 23 additions & 0 deletions inst/include/tools/comparisons.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,29 @@ struct comparisons {

};

struct comparisons_int64 {
static inline bool is_less(int64_t lhs, int64_t rhs) {
if (is_na(lhs)) return false;
if (is_na(rhs)) return true;

return lhs < rhs;
}

static inline bool is_greater(int64_t lhs, int64_t rhs) {
return lhs > rhs;
}

static inline bool equal_or_both_na(int64_t lhs, int64_t rhs) {
return lhs == rhs;
}

static inline bool is_na(int64_t x) {
return x == NA_INT64;
}

static int64_t NA_INT64;
};

template <>
struct comparisons<RAWSXP> {
typedef Rbyte STORAGE;
Expand Down
2 changes: 2 additions & 0 deletions src/arrange.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

namespace dplyr {

int64_t comparisons_int64::NA_INT64 = std::numeric_limits<int64_t>::min();

template <typename SlicedTibble>
SEXP arrange_template(const SlicedTibble& gdf, const QuosureList& quosures, SEXP frame) {
const Rcpp::DataFrame& data = gdf.data();
Expand Down
12 changes: 12 additions & 0 deletions tests/testthat/test-arrange.r
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,18 @@ test_that("desc(<not just a symbol>) works (#4099)", {
)
})

test_that("arrange supports bit64::integer64 (#4366)", {
df <- tibble(x = bit64::as.integer64(c(1, 3, 2, 1)))
expect_identical(
arrange(df, x),
tibble(x = bit64::as.integer64(c(1, 1, 2, 3)))
)
expect_identical(
arrange(df, desc(x)),
tibble(x = bit64::as.integer64(c(3, 2, 1, 1)))
)
})

# grouped_df --------------------------------------------------------------

test_that("can choose to include grouping vars", {
Expand Down

0 comments on commit cf607f0

Please sign in to comment.