Skip to content

Commit

Permalink
Solve _update_number_matches recursively (O(n))
Browse files Browse the repository at this point in the history
  • Loading branch information
monxa committed Oct 4, 2024
1 parent 55dd8dc commit 55d47a3
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 8 deletions.
29 changes: 21 additions & 8 deletions editor/tree_search.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ void TreeSearch::_filter_tree() {
}

void TreeSearch::_filter_tree(TreeItem *p_item, bool p_parent_matching) {
bool visible = number_matches.has(p_item) | p_parent_matching;
bool visible = number_matches.has(p_item) && (number_matches.get(p_item) > 0) || p_parent_matching;

Check failure on line 66 in editor/tree_search.cpp

View workflow job for this annotation

GitHub Actions / 🧪 Unit tests

suggest parentheses around '&&' within '||' [-Werror=parentheses]

p_item->set_visible(visible);

Expand Down Expand Up @@ -237,17 +237,30 @@ void TreeSearch::_update_ordered_tree_items(TreeItem *p_tree_item) {
}

void TreeSearch::_update_number_matches() {
ERR_FAIL_COND(!tree_reference);

number_matches.clear();
number_matches.reserve(ordered_tree_items.size());

for (int i = 0; i < matching_entries.size(); i++) {
TreeItem *item = matching_entries[i];
while (item) {
int previous_match_cnt = number_matches.has(item) ? number_matches.get(item) : 0;
number_matches[item] = previous_match_cnt + 1;
item = item->get_parent();
}
_update_number_matches(tree_reference->get_root());
}

void TreeSearch::_update_number_matches(TreeItem *item) {
for (int i = 0; i < item->get_child_count(); i++) {
TreeItem *child = item->get_child(i);
_update_number_matches(child);
}
int count = _vector_has_bsearch(matching_entries, item) ? 1 : 0;

for (int i = 0; i < item->get_child_count(); i++) {
TreeItem *child = item->get_child(i);
count += number_matches.has(child) ? number_matches.get(child) : 0;
}
if (count == 0) {
return;
}

number_matches[item] = count;
}

String TreeSearch::_get_search_mask() const {
Expand Down
1 change: 1 addition & 0 deletions editor/tree_search.h
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ class TreeSearch : public RefCounted {
void _update_matching_entries(const String &p_search_mask);
void _update_ordered_tree_items(TreeItem *p_tree_item);
void _update_number_matches();
void _update_number_matches(TreeItem *item);

void _find_matching_entries(TreeItem *p_tree_item, const String &p_search_mask, Vector<TreeItem *> &p_accum) const;
String _get_search_mask() const;
Expand Down

0 comments on commit 55d47a3

Please sign in to comment.