From e60e40b21cbf1616cc7fab917e7b7a3c08c4cab1 Mon Sep 17 00:00:00 2001 From: Waldir Leoncio Date: Thu, 22 Dec 2022 10:41:26 +0100 Subject: [PATCH] Translated myintersect() --- R/myintersect.R | 30 +++++++++++++++++++++++++++++- 1 file changed, 29 insertions(+), 1 deletion(-) diff --git a/R/myintersect.R b/R/myintersect.R index b2c19f1..e2e8f26 100644 --- a/R/myintersect.R +++ b/R/myintersect.R @@ -1 +1,29 @@ -myintersect <- function(A, B) {} +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 { + ma <- max(A) + } + + if (is.null(B)) { + mb <- 0 + } else { + mb <- max(B) + } + + if (ma == 0 | mb == 0) { + C <- vector() + } else { + # bits <- sparse(1, max(ma, mb)) + bits <- zeros(1, max(ma, mb)) + bits[A] <- 1 + C <- B[as.logical(bits[B])] + } + return(C) +}