PageRenderTime 82ms CodeModel.GetById 40ms app.highlight 3ms RepoModel.GetById 37ms app.codeStats 0ms

/fingerprint/R/ops.R

http://github.com/rajarshi/cdkr
R | 54 lines | 46 code | 8 blank | 0 comment | 8 complexity | 8972e18553803e569bf2eb11d586f7af MD5 | raw file
 1setMethod("&", c("fingerprint", "fingerprint"),
 2          function(e1, e2) {
 3            if (e1@nbit != e2@nbit)
 4              stop("fp1 & fp2 must of the same bit length")
 5            
 6            andbits <- intersect(e1@bits, e2@bits)
 7            new("fingerprint",
 8                bits=andbits,
 9                nbit=e1@nbit,
10                provider="R")
11          })
12
13setMethod("|", c("fingerprint", "fingerprint"),
14          function(e1, e2) {
15            if (e1@nbit != e2@nbit)
16              stop("fp1 & fp2 must of the same bit length")
17            
18            orbits <- union(e1@bits, e2@bits)
19            new("fingerprint",
20                bits=orbits,
21                nbit=e1@nbit,
22                provider="R")
23          })
24
25setMethod("!", c("fingerprint"),
26          function(x) {
27            bs <- 1:(x@nbit)
28            if (length(x@bits) > 0) b <- bs[ -x@bits ]
29            else b <- bs
30            ret <- new("fingerprint",
31                       bits=b,
32                       nbit=x@nbit,
33                       provider="R")
34            return(ret)
35          })
36
37setMethod("xor", c("fingerprint", "fingerprint"),
38          function(x,y) {
39            if (x@nbit != y@nbit)
40              stop("e1 & e2 must of the same bit length")
41
42            tmp1 <- rep(FALSE, x@nbit)
43            tmp2 <- rep(FALSE, y@nbit)
44            tmp1[x@bits] <- TRUE
45            tmp2[y@bits] <- TRUE
46            tmp3 <- xor(tmp1,tmp2)
47            xorbits <- which(tmp3)
48            
49            new("fingerprint",
50                bits=xorbits,
51                nbit=x@nbit,
52                provider="R")
53          })
54