ourMELONS/R/myintersect.R

30 lines
560 B
R
Raw Permalink Normal View History

2022-12-22 10:41:26 +01:00
myintersect <- function(A, B) {
# MYINTERSECT Intersection of two sets of positive integers (much faster than built - in intersect)
# C <- myintersect(A, B)
A <- t(A)
B <- t(B)
if (is.null(A)) {
ma <- 0
} else {
2023-02-10 14:52:25 +01:00
ma <- base::max(A)
2022-12-22 10:41:26 +01:00
}
if (is.null(B)) {
mb <- 0
} else {
2023-02-10 14:52:25 +01:00
mb <- base::max(B)
2022-12-22 10:41:26 +01:00
}
2023-02-10 14:52:25 +01:00
if (ma == 0 || mb == 0) {
2022-12-22 10:41:26 +01:00
C <- vector()
} else {
# bits <- sparse(1, max(ma, mb))
2023-02-10 14:52:25 +01:00
bits <- zeros(1, base::max(ma, mb))
bits[as.vector(A)] <- 1
C <- B[as.logical(bits[as.vector(B)])]
2022-12-22 10:41:26 +01:00
}
2023-02-10 14:52:25 +01:00
return(sort(C))
2022-12-22 10:41:26 +01:00
}