Skip to content

Commit

Permalink
Several performance improvements (2) (#388)
Browse files Browse the repository at this point in the history
* fix duplicate detection

* add test, update NEWS
  • Loading branch information
Enchufa2 authored Feb 13, 2025
1 parent 0d5b0b7 commit f056aee
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 8 deletions.
2 changes: 1 addition & 1 deletion NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@

* Add support for `brew` path discovery in macOS; #384

* Several performance improvements; #387 addressing #386
* Several performance improvements; #387, #388 addressing #386

# version 0.8-5

Expand Down
2 changes: 1 addition & 1 deletion R/misc.R
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ c.units <- function(..., recursive = FALSE, allow_mixed = units_options("allow_m
if (length(args) == 1)
return(.as.units(NextMethod(), u))

dup <- c(TRUE, duplicated(lapply(args, units))[-1])
dup <- vapply(args, function(x) identical(units(x), u), TRUE)

if (all(dup))
return(.as.units(do.call(c, lapply(args, drop_units)), u))
Expand Down
15 changes: 9 additions & 6 deletions tests/testthat/test_misc.R
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,15 @@ test_that("We can't concatenate units if they have different units", {
test_that("We can concatenate units if their units can be converted", {
x <- 1:4 * as_units("m")
y <- 5:8 * as_units("km")
z <- c(x, y)
z <- c(x, y, x, y)

expect_equal(length(z), length(x) + length(y))
expect_equal(length(z), 2 * (length(x) + length(y)))
expect_equal(as.character(units(z)), "m")
expect_equal(x, z[1:4])
expect_equal(set_units(y, units(as_units("m")), mode = "standard"), z[1:4 + 4])
expect_equal(x, z[1:4 + 8])
y <- set_units(y, units(as_units("m")), mode = "standard")
expect_equal(y, z[1:4 + 4])
expect_equal(y, z[1:4 + 12])
})

test_that("We can use diff on a units object", {
Expand Down Expand Up @@ -161,7 +164,7 @@ test_that("duplicated-related methods work as expected", {
test_that("bind methods work properly", {
a <- set_units(1:10, m)
b <- set_units((1:10) * 0.001, km)

x <- rbind(x=a, y=a)
y <- rbind(x=a, y=b)
expect_equal(as.numeric(x), as.numeric(y))
Expand All @@ -172,7 +175,7 @@ test_that("bind methods work properly", {
expect_equal(as.numeric(x), as.numeric(y) * 1000)
expect_equal(rownames(x), c("a", "a", "a"))
expect_equal(rownames(y), c("b", "b", "b"))

x <- cbind(x=a, y=a)
y <- cbind(x=a, y=b)
expect_equal(as.numeric(x), as.numeric(y))
Expand All @@ -183,7 +186,7 @@ test_that("bind methods work properly", {
expect_equal(as.numeric(x), as.numeric(y) * 1000)
expect_equal(colnames(x), c("a", "a", "a"))
expect_equal(colnames(y), c("b", "b", "b"))

z <- cbind(
rbind(a, b),
rbind(x = a, y = b))
Expand Down

0 comments on commit f056aee

Please sign in to comment.