|
12 | 12 | #' give faster computation if `vec1` and `vec2` are data frames. |
13 | 13 | #' |
14 | 14 | #' @return logical vector; no nonmissing entries if `na_equal = TRUE`. Behavior |
15 | | -#' may differ from `vec_equal` with non-`NA` `NaN`s involved. |
| 15 | +#' may differ from `vec_equal` with non-`NA` `NaN`s involved, or for bare |
| 16 | +#' lists that contain named vectors. |
16 | 17 | approx_equal <- function(vec1, vec2, abs_tol, na_equal, .ptype = NULL, inds1 = NULL, inds2 = NULL) { |
17 | 18 | # Recycle inds if provided; vecs if not: |
18 | 19 | common_size <- vec_size_common( |
@@ -63,10 +64,25 @@ approx_equal0 <- function(vec1, vec2, abs_tol, na_equal, inds1 = NULL, inds2 = N |
63 | 64 | approx_equal0(vec1[[col_i]], vec2[[col_i]], abs_tol, na_equal, inds1, inds2) |
64 | 65 | })) |
65 | 66 | } |
| 67 | + } else if (is_bare_list(vec1)) { |
| 68 | + vapply(seq_along(vec1), function(i) { |
| 69 | + entry1 <- vec1[[i]] |
| 70 | + entry2 <- vec2[[i]] |
| 71 | + vec_size(entry1) == vec_size(entry2) && |
| 72 | + # This is inconsistent with vec_equal on named vectors; to be |
| 73 | + # consistently inconsistent, we avoid dispatching to vec_equal for bare |
| 74 | + # lists even with abs_tol = 0: |
| 75 | + identical(vec_ptype(entry1), vec_ptype(entry2)) && |
| 76 | + all(approx_equal0(entry1, entry2, abs_tol, na_equal)) |
| 77 | + }, logical(1L)) |
66 | 78 | } else { |
67 | 79 | # XXX No special handling for any other types/situations. Makes sense for |
68 | | - # unclassed atomic things; bare lists and certain vctrs classes might want |
69 | | - # recursion / specialization, though. |
| 80 | + # unclassed atomic things; custom classes (e.g., distributions) might want |
| 81 | + # recursion / specialization, though. approx_equal0 should probably be an S3 |
| 82 | + # method. Also, abs_tol == 0 --> vec_equal logic should maybe be either be |
| 83 | + # hoisted to approx_equal or we should manually recurse on data frames even |
| 84 | + # with abs_tol = 0 when that's faster (might depend on presence of inds*), |
| 85 | + # after some inconsistencies are ironed out. |
70 | 86 | if (!is.null(inds1)) { |
71 | 87 | vec1 <- vec_slice(vec1, inds1) |
72 | 88 | vec2 <- vec_slice(vec2, inds2) |
|
0 commit comments