Translated mysetdiff() (#3)
This commit is contained in:
parent
84e0eb61d8
commit
86a5dda6d6
2 changed files with 21 additions and 0 deletions
18
R/mysetdiff.R
Normal file
18
R/mysetdiff.R
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
mysetdiff <- function(A, B) {
|
||||
# MYSETDIFF Set difference of two sets of positive integers (much faster than
|
||||
# built - in setdiff)
|
||||
# C <- mysetdiff(A, B)
|
||||
# C <- A \ B = { things in A that are not in B }
|
||||
# MATLAB Original by Kevin Murphy, modified by Leon Peshkin
|
||||
if (is.null(A)) {
|
||||
return(vector())
|
||||
} else if (is.null(B)) {
|
||||
return(A)
|
||||
} else { # both non-empty
|
||||
bits <- zeros(1, base::max(base::max(A), base::max(B)))
|
||||
bits[A] <- 1
|
||||
bits[B] <- 0
|
||||
C <- A[as.logical(bits[A])]
|
||||
}
|
||||
return(C)
|
||||
}
|
||||
|
|
@ -5,10 +5,13 @@ test_that("functions work with basic input", {
|
|||
w <- array(c(1, 3, 2, 4, 5, 7, 6, 8), c(2, 2, 2))
|
||||
e <- computeCounts(q, 4, 5, w)
|
||||
r <- matrix(c(5, 3, 3, 6, 4, 4, 7, 9, 1), 3)
|
||||
t <- 1:5
|
||||
y <- c(4, 3, 1, 4, 8)
|
||||
expect_equal(e$cliqcounts, t(c(1, 1, 1, 0, 0)))
|
||||
expect_equal(e$sepcounts, t(c(0, 0, 0, 1, 0)))
|
||||
expect_equal(computeDiffInCliqCounts(r, 4), matrix(c(0, 1, 1)))
|
||||
expect_equal(computeDiffInCliqCounts(r, 3), matrix(c(0, 1, 1)))
|
||||
expect_equal(computeDiffInCliqCounts(r, 5), matrix(c(1, 0, 0)))
|
||||
expect_equal(computeDiffInCliqCounts(r, 0), matrix(c(0, 0, 0)))
|
||||
expect_equal(mysetdiff(t, y), c(2, 5))
|
||||
})
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue