@@ -121,17 +121,22 @@ vec_approx_equal0 <- function(vec1, vec2, na_equal, abs_tol, inds1 = NULL, inds2
121121 na_or_nan1 <- is.na(vec1 )
122122 na_or_nan2 <- is.na(vec2 )
123123 # Since above are bare logical vectors, we can use `fifelse`
124- res <- fifelse(
125- ! na_or_nan1 & ! na_or_nan2 ,
126- abs( vec1 - vec2 ) < = abs_tol ,
127- if ( na_equal ) {
124+ if ( na_equal ) {
125+ res <- fifelse(
126+ ! na_or_nan1 & ! na_or_nan2 ,
127+ abs( vec1 - vec2 ) < = abs_tol ,
128128 na_or_nan1 & na_or_nan2 & (is.nan(vec1 ) == is.nan(vec2 ))
129- } else {
130- # Like `==` and `vec_equal`, we consider NaN == {NA, NaN, anything else}
131- # to be NA.
129+ )
130+ } else {
131+ # Like `==` and `vec_equal`, we consider NaN == {NA, NaN, anything else}
132+ # to be NA.
133+ res <- fifelse(
134+ ! na_or_nan1 & ! na_or_nan2 ,
135+ abs(vec1 - vec2 ) < = abs_tol ,
132136 NA
133- }
134- )
137+ )
138+ }
139+
135140 if (! is.null(dim(vec1 ))) {
136141 dim(res ) <- dim(vec1 )
137142 res <- rowSums(res ) == ncol(res )
@@ -278,9 +283,9 @@ tbl_diff2 <- function(earlier_snapshot, later_tbl,
278283 # ukey+val duplicates (cases 2. and 3.).)
279284
280285 # Row indices of first occurrence of each ukey; will be the same as
281- # seq_len(combined_n) except for when that ukey has been re-reported in
282- # `later_tbl`, in which case ( 3. or 4.) it will point back to the row index of
283- # the same ukey in `earlier_snapshot`:
286+ # seq_len(combined_n) for each ukey's first appearance (cases 1., 2., or 5.);
287+ # for re-reported ukeys in `later_tbl` (cases 3. or 4.), it will point back to
288+ # the row index of the same ukey in `earlier_snapshot`:
284289 combined_ukey_firsts <- vec_duplicate_id(combined_ukeys )
285290
286291 # Which rows from combined are cases 3. or 4.?
@@ -368,6 +373,11 @@ tbl_patch <- function(snapshot, update, ukey_names) {
368373 result_tbl <- vec_rbind(update , snapshot )
369374
370375 dup_ids <- vec_duplicate_id(result_tbl [ukey_names ])
376+ # Find the "first" appearance of each ukey; since `update` is ordered before `snapshot`,
377+ # this means favoring the rows from `update` over those in `snapshot`.
378+ # This is like `!duplicated()` but faster, and like `vec_unique_loc()` but guaranteeing
379+ # that we get the first appearance since `vec_duplicate_id()` guarantees that
380+ # it points to the first appearance.
371381 not_overwritten <- dup_ids == vec_seq_along(result_tbl )
372382 result_tbl <- result_tbl [not_overwritten , ]
373383
0 commit comments