-
Notifications
You must be signed in to change notification settings - Fork 152
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
112a03b
commit a37b891
Showing
5 changed files
with
38 additions
and
55 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,27 +1,24 @@ | ||
|
||
recurse <- function(object, callback, ...) { | ||
renv_recurse_impl(list(), object, callback, ...) | ||
|
||
queue <- vector("list", 8192L) | ||
queue[[1L]] <- object | ||
index <- 0L | ||
slot <- 1L | ||
|
||
while (index != slot) { | ||
|
||
index <- index + 1L | ||
result <- callback(queue[[index]], ...) | ||
|
||
if (is.recursive(object <- if (is.call(result)) result else queue[[index]])) { | ||
for (i in seq_along(object)) { | ||
slot <- slot + 1L | ||
queue[[slot]] <- object[[i]] | ||
} | ||
} | ||
|
||
} | ||
|
||
} | ||
|
||
renv_recurse_impl <- function(stack, object, callback, ...) { | ||
|
||
# ignore missing values | ||
if (missing(object) || identical(object, quote(expr = ))) | ||
return(FALSE) | ||
|
||
# push node on to stack | ||
stack[[length(stack) + 1]] <- object | ||
|
||
# invoke callback | ||
result <- callback(object, stack, ...) | ||
if (is.call(result)) | ||
object <- result | ||
else if (identical(result, FALSE)) | ||
return(FALSE) | ||
|
||
# recurse | ||
if (is.recursive(object)) | ||
for (i in seq_along(object)) | ||
renv_recurse_impl(stack, object[[i]], callback, ...) | ||
|
||
} |
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,5 @@ | ||
|
||
test_that("recurse() can handle missing objects", { | ||
data <- substitute(list(a = A), list(A = quote(expr = ))) | ||
expect_no_error(recurse(data, function(node) print(node))) | ||
}) |