You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: R/setops64.R
+56-25Lines changed: 56 additions & 25 deletions
Original file line number
Diff line number
Diff line change
@@ -10,6 +10,7 @@
10
10
#' @title Set Operations
11
11
#' @description Performs set union, intersection, (asymmetric!) difference, equality and membership on two vectors. As soon as an integer64 vector is involved, the operations are performed using integer64 semantics. Otherwise the \code{base} package functions are called.
12
12
#' @inheritParams base::union
13
+
#' @param ... further arguments passed to or from other methods.
13
14
#' @return
14
15
#' For union, a vector of a common mode or class.
15
16
#'
@@ -28,12 +29,18 @@
28
29
#' setequal(x, y)
29
30
#' is.element(x, y)
30
31
#'
31
-
#' @export
32
+
#' @importFrom generics union
33
+
#' @exportS3Method union default
34
+
#' @aliases union
32
35
#' @rdname sets
33
-
union=function(x, y) {
34
-
if (!(is.integer64(x) || is.integer64(y)))
35
-
return(base::union(x, y))
36
-
36
+
union.default=function(x, y, ...) {
37
+
if (is.integer64(y)) return(union.integer64(x, y))
38
+
base::union(x, y)
39
+
}
40
+
41
+
#' @exportS3Method union integer64
42
+
#' @rdname sets
43
+
union.integer64=function(x, y, ...) {
37
44
target_class= target_class(list(x, y))
38
45
# try using the benefit of integer64 caching, if possible. I.e. call unique() before as().
39
46
x= unique(x)
@@ -56,13 +63,19 @@ union = function(x, y) {
56
63
unique(c(x, y))
57
64
}
58
65
59
-
#' @export
66
+
#' @importFrom generics intersect
67
+
#' @exportS3Method intersect default
68
+
#' @aliases intersect
69
+
#' @rdname sets
70
+
intersect.default=function(x, y, ...) {
71
+
if (is.integer64(y)) return(intersect.integer64(x, y))
72
+
base::intersect(x, y)
73
+
}
74
+
75
+
#' @exportS3Method intersect integer64
60
76
#' @rdname sets
61
-
intersect=function(x, y) {
77
+
intersect.integer64=function(x, y, ...) {
62
78
if (is.null(x) || is.null(y)) return(NULL)
63
-
if (!(is.integer64(x) || is.integer64(y)))
64
-
return(base::intersect(x, y))
65
-
66
79
target_class= target_class(list(x, y))
67
80
x= unique(x)
68
81
class_x= class(x)[1L]
@@ -84,12 +97,18 @@ intersect = function(x, y) {
84
97
x[match(x, y, 0L) >0L]
85
98
}
86
99
87
-
#' @export
100
+
#' @importFrom generics setequal
101
+
#' @exportS3Method setequal default
102
+
#' @aliases setequal
88
103
#' @rdname sets
89
-
setequal=function(x, y) {
90
-
if (!(is.integer64(x) || is.integer64(y)))
91
-
return(base::setequal(x, y))
92
-
104
+
setequal.default=function(x, y, ...) {
105
+
if (is.integer64(y)) return(setequal.integer64(x, y))
106
+
base::setequal(x, y)
107
+
}
108
+
109
+
#' @exportS3Method setequal integer64
110
+
#' @rdname sets
111
+
setequal.integer64=function(x, y, ...) {
93
112
x= unique(x)
94
113
y= unique(y)
95
114
length_x= length(x)
@@ -106,12 +125,18 @@ setequal = function(x, y) {
106
125
!anyNA(match(x, y))
107
126
}
108
127
109
-
#' @export
128
+
#' @importFrom generics setdiff
129
+
#' @exportS3Method setdiff default
130
+
#' @aliases setdiff
110
131
#' @rdname sets
111
-
setdiff=function(x, y) {
112
-
if (!(is.integer64(x) || is.integer64(y)))
113
-
return(base::setdiff(x, y))
114
-
132
+
setdiff.default=function(x, y, ...) {
133
+
if (is.integer64(y)) return(setdiff.integer64(x, y))
134
+
base::setdiff(x, y)
135
+
}
136
+
137
+
#' @exportS3Method setdiff integer64
138
+
#' @rdname sets
139
+
setdiff.integer64=function(x, y, ...) {
115
140
class_x= class(x)[1L]
116
141
if (class_x%in% c("POSIXct", "Date"))
117
142
x= unclass(x)
@@ -135,12 +160,18 @@ setdiff = function(x, y) {
135
160
x[match(x_match, y, 0L) ==0L]
136
161
}
137
162
138
-
#' @export
163
+
#' @importFrom generics is.element
164
+
#' @exportS3Method is.element default
165
+
#' @aliases is.element
139
166
#' @rdname sets
140
-
is.element=function(el, set) {
141
-
if (!(is.integer64(el) || is.integer64(set)))
142
-
return(base::is.element(el, set))
143
-
167
+
is.element.default=function(el, set, ...) {
168
+
if (is.integer64(set)) return(is.element.integer64(el, set))
0 commit comments