-
Notifications
You must be signed in to change notification settings - Fork 12
add set operations #238
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
hcirellu
wants to merge
19
commits into
main
Choose a base branch
from
union
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
add set operations #238
Changes from 16 commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
f0dd02a
add set operations
hcirellu 55d080b
vapply
hcirellu 7238eb5
replace str2lang for ancient test
hcirellu f6b58de
skip POSIXct test with ancient R
hcirellu fbbfbb3
fix test for ancient
hcirellu 6a64391
missed a test
hcirellu 65d5825
skip test for Date in ancient
hcirellu 3e9aa47
fix ancient test
hcirellu 59acf37
fix ancient test
hcirellu 844fe8f
fix ancient test
hcirellu 48cd720
fix ancient test
hcirellu 8d29d62
fix ancient test
hcirellu 56fb939
Merge branch 'main' into union
hcirellu 00d718a
move to new file setops64
hcirellu c679250
Merge branch 'union' of https://github.com/r-lib/bit64 into union
hcirellu 5bf1dfd
update copyright
MichaelChirico 1a869cc
remove return value `sampleValue` from target_class_and_sample_value
hcirellu 821c45b
Merge branch 'main' into union
hcirellu d3010ce
Merge branch 'main' into union
hcirellu File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,120 @@ | ||
| # /* | ||
| # R-Code | ||
| # S3 atomic 64bit integers for R | ||
| # (c) 2026 Michael Chirico | ||
| # Licence: GPL2 | ||
| # Provided 'as is', use at your own risk | ||
| # Created: 2026-01-14 | ||
| #*/ | ||
|
|
||
| #' @title Set Operations | ||
| #' @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. | ||
| #' @param x,y,el,set vectors (of the same mode) containing a sequence of items (conceptually) with no duplicated values. | ||
| #' @return | ||
| #' For union, a vector of a common mode or class. | ||
| #' | ||
| #' For intersect, a vector of a common mode or class, or NULL if x or y is NULL. | ||
| #' | ||
| #' For setdiff, a vector of the same mode or class as x. | ||
| #' | ||
| #' A logical scalar for setequal and a logical of the same length as x for is.element. | ||
| #' @seealso [base::union] | ||
| #' @examples | ||
| #' x <- as.integer64(1:5) | ||
| #' y <- c(1L, 3L, 5L, 7L) | ||
| #' union(x, y) | ||
| #' intersect(x, y) | ||
| #' setdiff(x, y) | ||
| #' setequal(x, y) | ||
| #' is.element(x, y) | ||
| #' | ||
| #' @export | ||
| #' @rdname sets | ||
| union = function(x, y) { | ||
| if (!(is.integer64(x) || is.integer64(y))) | ||
| return(base::union(x, y)) | ||
|
|
||
| target_class = target_class_and_sample_value(list(x, y))$class | ||
| # try using the benefit of integer64 caching, if possible. I.e. call unique() before as(). | ||
| x = unique(x) | ||
| if (class(x)[1L] != target_class) | ||
| x = as(x, target_class) | ||
| y = unique(y) | ||
| if (class(y)[1L] != target_class) | ||
| y = as(y, target_class) | ||
|
|
||
| unique(c(x, y)) | ||
| } | ||
|
|
||
| #' @export | ||
| #' @rdname sets | ||
| intersect = function(x, y) { | ||
| if (!(is.integer64(x) || is.integer64(y))) | ||
| return(base::intersect(x, y)) | ||
|
|
||
| target_class = target_class_and_sample_value(list(x, y))$class | ||
| x = unique(x) | ||
| if (class(x)[1L] != target_class) | ||
| x = as(x, target_class) | ||
| y = unique(y) | ||
| if (class(y)[1L] != target_class) | ||
| y = as(y, target_class) | ||
|
|
||
| x[match(x, y, 0L) > 0L] | ||
| } | ||
|
|
||
| #' @export | ||
| #' @rdname sets | ||
| setequal = function(x, y) { | ||
| if (!(is.integer64(x) || is.integer64(y))) | ||
| return(base::setequal(x, y)) | ||
|
|
||
| target_class = target_class_and_sample_value(list(x, y))$class | ||
| x = unique(x) | ||
| if (class(x)[1L] != target_class) | ||
| x = as(x, target_class) | ||
| y = unique(y) | ||
| if (class(y)[1L] != target_class) | ||
| y = as(y, target_class) | ||
|
|
||
| length(x) == length(y) && !anyNA(match(x, y)) | ||
| } | ||
|
|
||
| #' @export | ||
| #' @rdname sets | ||
| setdiff = function(x, y) { | ||
| if (!(is.integer64(x) || is.integer64(y))) | ||
| return(base::setdiff(x, y)) | ||
|
|
||
| if (class(x)[1L] %in% c("POSIXct", "Date")) | ||
| x = unclass(x) | ||
| if (class(x)[1L] %in% c("factor", "ordered")) | ||
| x = as.character(x) | ||
| target_class = target_class_and_sample_value(list(x, y))$class | ||
| x = unique(x) | ||
| y = unique(y) | ||
| if (class(x)[1L] != target_class) | ||
| x_match = as(x, target_class) | ||
| else | ||
| x_match = x | ||
| if (class(y)[1L] != target_class) | ||
| y = as(y, target_class) | ||
|
|
||
| x[match(x_match, y, 0L) == 0L] | ||
| } | ||
|
|
||
| #' @export | ||
| #' @rdname sets | ||
| is.element = function(el, set) { | ||
| if (!(is.integer64(el) || is.integer64(set))) | ||
| return(base::is.element(el, set)) | ||
|
|
||
| target_class = target_class_and_sample_value(list(el, set))$class | ||
| if (class(el)[1L] != target_class) | ||
| el = as(el, target_class) | ||
| set = unique(set) | ||
| if (class(set)[1L] != target_class) | ||
| set = as(set, target_class) | ||
|
|
||
| match(el, set, 0L) > 0L | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.