Compare commits

..

2 Commits

Author SHA1 Message Date
Alexander Montag 146635126a
Refactor _filter_items again for readability 2024-10-01 18:13:00 +02:00
Alexander Montag 540bb585a8
Refactor tree filtering logic for correctness and efficiency 2024-10-01 17:09:18 +02:00
1 changed files with 40 additions and 33 deletions

View File

@ -19,6 +19,7 @@
#ifdef LIMBOAI_MODULE #ifdef LIMBOAI_MODULE
#include "core/math/math_funcs.h" #include "core/math/math_funcs.h"
#include "core/templates/hash_set.h"
#include "editor/editor_interface.h" #include "editor/editor_interface.h"
#include "editor/themes/editor_scale.h" #include "editor/themes/editor_scale.h"
#include "scene/gui/separator.h" #include "scene/gui/separator.h"
@ -34,6 +35,7 @@
#include <godot_cpp/classes/style_box_flat.hpp> #include <godot_cpp/classes/style_box_flat.hpp>
#include <godot_cpp/classes/viewport.hpp> #include <godot_cpp/classes/viewport.hpp>
#include <godot_cpp/core/math.hpp> #include <godot_cpp/core/math.hpp>
#include <godot_cpp/templates/hash_set.hpp>
#endif // LIMBOAI_GDEXTENSION #endif // LIMBOAI_GDEXTENSION
@ -55,52 +57,56 @@ void TreeSearch::_clean_callable_cache() {
callable_cache = new_callable_cache; callable_cache = new_callable_cache;
} }
// Makes all tree items invisible that don't match the following criteria:
// 1. is a matching tree_item
// 2. is a parent of a matching tree_item
// 3. is any descendant of a matching tree_item
void TreeSearch::_filter_tree(const String &p_search_mask) { void TreeSearch::_filter_tree(const String &p_search_mask) {
if (matching_entries.is_empty()) { if (number_matches.size() == 0){
return; return;
} }
HashSet<TreeItem *> visible_as_parent;
HashSet<TreeItem *> visible_as_descendant;
Vector<bool> item_visibilities; // Mark matching items and all their ancestors (1, 2)
item_visibilities.resize(ordered_tree_items.size()); for (int i = 0; i < matching_entries.size(); i++) {
item_visibilities.fill(false); TreeItem *cur_match = matching_entries[i];
TreeItem *current = cur_match;
// Make all entries visible that have any matching descendents. O(n) while (current != nullptr && !visible_as_parent.has(current)) {
for (int i = 0; i < ordered_tree_items.size(); i++) { visible_as_parent.insert(current);
TreeItem *entry = ordered_tree_items[i]; current = current->get_parent();
if (number_matches.has(entry) && number_matches[entry] > 0) {
item_visibilities.set(i, true);
} }
} }
// Make all descendents of matching entries visible. O(n) * log(|matching_entries|) // Mark matching items and all their descendants (1, 3)
for (int i = 0; i < ordered_tree_items.size(); i++) { for (int i = 0; i < matching_entries.size(); i++) {
TreeItem *entry = ordered_tree_items[i]; TreeItem *cur_match = matching_entries[i];
if (_vector_has_bsearch(matching_entries, entry)) { if (visible_as_descendant.has(cur_match)) {
// the [next_entry] at the same depth or depth above. continue;
TreeItem *next_entry = entry->get_next(); }
// Descendents
Vector<TreeItem *> descendent_list = { cur_match };
// search above current depth if no [next_entry]. for (int j = 0; j < descendent_list.size(); j++) {
while (!next_entry && entry) { TreeItem *descendent = descendent_list[j];
entry = entry->get_parent(); if (visible_as_descendant.has(descendent)) {
} continue; // Skip subtree if already processed
if (entry) {
next_entry = entry->get_next();
} }
visible_as_descendant.insert(descendent);
// mark visible all successors upto next entry at the same depth or above // Collect X-level descendents (bit clunky because godot doesn't return get_children as pointers)
int j = i; for (int k = 0; k < descendent->get_child_count(); k++) {
for (; j < ordered_tree_items.size() && ordered_tree_items[j] != next_entry; j++) { TreeItem *child = descendent->get_child(k);
item_visibilities.set(j, true); descendent_list.push_back(child);
} }
i = j - 1; // every entry is only processed once.
} }
} }
// Apply visibility. O(n) // Set visibility based on whether items are in either visibility set
for (int i = 0; i < ordered_tree_items.size(); i++) { for (int i = 0; i < ordered_tree_items.size(); i++) {
if (!item_visibilities[i]) { TreeItem *item = ordered_tree_items[i];
ordered_tree_items[i]->set_visible(false); bool is_visible = visible_as_parent.has(item) || visible_as_descendant.has(item);
} item->set_visible(is_visible);
} }
} }
@ -170,6 +176,7 @@ void TreeSearch::_draw_highlight_item(TreeItem *p_tree_item, const Rect2 p_rect,
if (!p_tree_item) { if (!p_tree_item) {
return; return;
} }
// call any parent draw methods such as for probability FIRST. // call any parent draw methods such as for probability FIRST.
p_parent_draw_method.call(p_tree_item, p_rect); p_parent_draw_method.call(p_tree_item, p_rect);