-
Notifications
You must be signed in to change notification settings - Fork 66
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
Implement vec_map()
#1227
Closed
Closed
Implement vec_map()
#1227
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
07dfe18
Add `VECTOR_PTR_RO()` accessor
lionel- 8fd5b51
Draft `vec_map()` implementation
lionel- e380523
Use `vec_map()` in the compat-purrr utils
lionel- 8cd177b
Add missing functions in the purrr compat file
lionel- d6e8057
Force-eval `vec_map()` calls
lionel- 2cd5b9a
Fix `map_if()` compat
lionel- 3c3bbd3
Import purrr tests for `vec_map()`
lionel- d95195f
Zap attributes of input before mapping to list
lionel- a7d9de9
Test `vec_map()` with S3 atomic vectors
lionel- ef1eaff
Don't allow prototype to be inferred
lionel- 5f5ff4f
Extract list and atomic mapping in different functions
lionel- 5605fbd
Check that mapped function returned a size 1 vector
lionel- 609098b
Document genericity of `vec_map()`
lionel- de112a8
Only assign names when they exist
lionel- 7065a82
Qualify vctrs in compat file
lionel- 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 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 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 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,6 @@ | ||
|
||
vec_map <- function(.x, .fn, ..., .ptype = list()) { | ||
.elt <- NULL # Defined in the mapping loop | ||
.fn <- as_function(.fn) | ||
.External(vctrs_map, .x, environment(), .ptype) | ||
} |
This file contains 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 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 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,124 @@ | ||
#include "vctrs.h" | ||
#include "slice.h" | ||
#include "utils.h" | ||
|
||
// Defined at load time | ||
SEXP vec_map_call = NULL; | ||
|
||
static SEXP atomic_map(SEXP x, SEXP env, SEXP ptype); | ||
static SEXP list_map(SEXP x, SEXP env, SEXP ptype); | ||
|
||
|
||
SEXP vctrs_map(SEXP args) { | ||
args = CDR(args); | ||
SEXP x = CAR(args); args = CDR(args); | ||
SEXP env = CAR(args); args = CDR(args); | ||
SEXP ptype = CAR(args); | ||
|
||
if (ptype == R_NilValue) { | ||
r_abort("`.ptype` can't be NULL."); | ||
} | ||
|
||
SEXP orig = x; | ||
bool list_input = vec_is_list(orig); | ||
bool list_output = vec_is_list(ptype); | ||
|
||
// Instead of using `[[` or equivalent to access the elements of | ||
// atomic inputs, we chop them into a list | ||
if (!list_input) { | ||
x = vec_chop2(x); | ||
} | ||
PROTECT(x); | ||
|
||
SEXP out; | ||
if (list_output) { | ||
out = list_map(x, env, ptype); | ||
} else { | ||
out = atomic_map(x, env, ptype); | ||
} | ||
PROTECT(out); | ||
|
||
SEXP names = PROTECT(vec_names(orig)); | ||
if (names != R_NilValue) { | ||
vec_set_names(out, names); | ||
} | ||
|
||
UNPROTECT(3); | ||
return out; | ||
} | ||
|
||
static | ||
SEXP list_map(SEXP x, SEXP env, SEXP ptype) { | ||
// When mapping to a list, we update the input list with the results | ||
// inplace. We first zap the attributes of this list because it's | ||
// cast to the target prototype later on. | ||
SEXP out = PROTECT(r_clone_referenced(x)); | ||
SET_ATTRIB(out, R_NilValue); | ||
SET_OBJECT(out, 0); | ||
|
||
r_ssize n = r_length(x); | ||
const SEXP* p_x = VECTOR_PTR_RO(x); | ||
|
||
for (r_ssize i = 0; i < n; ++i) { | ||
r_env_poke(env, syms_dot_elt, p_x[i]); | ||
SET_VECTOR_ELT(out, i, r_eval_force(vec_map_call, env)); | ||
} | ||
|
||
// Genericity is accomplished by casting the complete list of | ||
// outputs to the target prototype. Should use a ptype identity | ||
// check before casting. Probably `vec_cast()` should make that | ||
// check. | ||
if (OBJECT(ptype)) { | ||
out = vec_cast(out, ptype, NULL, NULL); | ||
} | ||
|
||
UNPROTECT(1); | ||
return out; | ||
} | ||
|
||
static | ||
SEXP atomic_map(SEXP x, SEXP env, SEXP ptype) { | ||
r_ssize n = r_length(x); | ||
|
||
// Genericity is handled in a typical fashion when mapping to an | ||
// atomic vector. We initialise the target prototype and coerce each | ||
// element to that target before assigning. | ||
SEXP out = PROTECT(vec_init(ptype, n)); | ||
|
||
SEXP out_proxy = vec_proxy(out); | ||
PROTECT_INDEX out_proxy_pi; | ||
PROTECT_WITH_INDEX(out_proxy, &out_proxy_pi); | ||
|
||
SEXP loc = PROTECT(compact_seq(0, 0, true)); | ||
int* p_loc = INTEGER(loc); | ||
|
||
const SEXP* p_x = VECTOR_PTR_RO(x); | ||
|
||
for (r_ssize i = 0; i < n; ++i) { | ||
r_env_poke(env, syms_dot_elt, p_x[i]); | ||
|
||
SEXP elt_out = PROTECT(r_eval_force(vec_map_call, env)); | ||
if (vec_size(elt_out) != 1) { | ||
r_abort("Mapped function must return a size 1 vector."); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Would be useful to includde index in this error message. |
||
} | ||
|
||
elt_out = PROTECT(vec_cast(elt_out, ptype, NULL, NULL)); | ||
|
||
init_compact_seq(p_loc, i, 1, true); | ||
out_proxy = vec_proxy_assign(out_proxy, loc, elt_out); | ||
|
||
UNPROTECT(2); | ||
REPROTECT(out_proxy, out_proxy_pi); | ||
} | ||
|
||
out = vec_restore(out_proxy, ptype, R_NilValue, VCTRS_OWNED_true); | ||
|
||
UNPROTECT(3); | ||
return out; | ||
} | ||
|
||
|
||
void vctrs_init_map(SEXP ns) { | ||
vec_map_call = r_parse(".fn(.elt, ...)"); | ||
R_PreserveObject(vec_map_call); | ||
} |
This file contains 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 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 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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why does this change?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To avoid an infinite recursion.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
But why did it work before?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Because
map()
didn't use vctrs operations and genericity.The purrr compat file has been updated to use the vec_map() to get some internal testing and to make it easy to import unit tests from purrr.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
oops sorry, the infinite recursion was another problem. It doesn't make sense for ptype-full to be recursed into anyway.
The problem is that
map()
now assigns names, and partial factors and data frames don't support that:This is a flaw in the partial types but as usual I'm just working around these types when they cause problems for now.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We no longer assign
NULL
names to be a little more efficient. However thisunclass()
change is still needed because partial types inherit fromvctrs_sclr
which have an unsupportednames<-
method but still allownames()
, sovec_map()
sees the internal field names. Making the latter unsupported causes a bunch of other issues. It wouldn't solve the problem at hand anyway because now we'd have a vector type for whichnames()
is an error, which is a big genericity flaw. I think we shouldn't worry about these types too much for now.