Skip to content

Commit 884455c

Browse files
authored
Add union_with to sorted set. (#4901)
* Add union_with to sorted set.
1 parent 70ca698 commit 884455c

File tree

2 files changed

+19
-4
lines changed

2 files changed

+19
-4
lines changed

Firestore/core/src/firebase/firestore/core/sync_engine.cc

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -402,10 +402,8 @@ DocumentKeySet SyncEngine::GetRemoteKeys(TargetId target_id) const {
402402
}
403403

404404
for (const auto& query : queries_by_target_.at(target_id)) {
405-
for (const auto& key :
406-
query_views_by_query_.at(query)->view().synced_documents()) {
407-
keys = keys.insert(key);
408-
}
405+
keys = keys.union_with(
406+
query_views_by_query_.at(query)->view().synced_documents());
409407
}
410408
return keys;
411409
}

Firestore/core/src/firebase/firestore/immutable/sorted_set.h

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,23 @@ class SortedSet : public SortedContainer {
7474
return SortedSet{map_.insert(key, {})};
7575
}
7676

77+
ABSL_MUST_USE_RESULT SortedSet union_with(const SortedSet& other) const {
78+
const SortedSet* result_ptr = this;
79+
const SortedSet* other_ptr = &other;
80+
81+
// Make sure `result_ptr` always points to the larger one of the two sets.
82+
if (result_ptr->size() < other_ptr->size()) {
83+
result_ptr = other_ptr;
84+
other_ptr = this;
85+
}
86+
87+
auto result = *result_ptr;
88+
for (const auto& k : *other_ptr) {
89+
result = result.insert(k);
90+
}
91+
return result;
92+
}
93+
7794
ABSL_MUST_USE_RESULT SortedSet erase(const K& key) const {
7895
return SortedSet{map_.erase(key)};
7996
}

0 commit comments

Comments
 (0)