java-topology/defects/panda3d/patch/panda3d-0001-nodepath-collection-dedup.patch

56 lines
1.7 KiB
Diff

# UNDF: UNDF-2026-000000207
# UNDF: (leave blank)
--- a/panda/src/pgraph/nodePathCollection.cxx
+++ b/panda/src/pgraph/nodePathCollection.cxx
@@ -88,25 +88,25 @@ add_paths_from(const NodePathCollection &other) {
*/
void NodePathCollection::
remove_paths_from(const NodePathCollection &other) {
NodePaths new_paths;
int num_paths = get_num_paths();
+ // Build an unordered_set from other to enable O(1) membership checks,
+ // reducing the overall complexity from O(N*M) to O(N+M).
+ pset<NodePath> other_set;
+ int other_num = other.get_num_paths();
+ for (int i = 0; i < other_num; i++) {
+ other_set.insert(other.get_path(i));
+ }
for (int i = 0; i < num_paths; i++) {
NodePath path = get_path(i);
- if (!other.has_path(path)) {
+ if (other_set.find(path) == other_set.end()) {
new_paths.push_back(path);
}
}
_node_paths = new_paths;
}
/**
* Removes any duplicate entries of the same NodePaths on this collection. If
* a NodePath appears multiple times, the first appearance is retained;
* subsequent appearances are removed.
*/
void NodePathCollection::
remove_duplicate_paths() {
NodePaths new_paths;
-
int num_paths = get_num_paths();
+ // Use a set to track seen paths in O(log N) rather than O(N) per element,
+ // reducing the overall complexity from O(N^2) to O(N log N).
+ pset<NodePath> seen;
for (int i = 0; i < num_paths; i++) {
NodePath path = get_path(i);
- bool duplicated = false;
-
- for (int j = 0; j < i && !duplicated; j++) {
- duplicated = (path == get_path(j));
- }
-
- if (!duplicated) {
+ if (seen.insert(path).second) {
new_paths.push_back(path);
}
}
-
_node_paths = new_paths;
}