From 0ecdd9653fbee296666ab80b9cdb11149505ebe1 Mon Sep 17 00:00:00 2001 From: Jakub Grzesik Date: Sat, 2 Sep 2017 16:17:59 +0200 Subject: [PATCH] Improve 'quick open' search time --- editor/quick_open.cpp | 54 ++++++++++++++++++++++++++++--------------- editor/quick_open.h | 1 + 2 files changed, 37 insertions(+), 18 deletions(-) diff --git a/editor/quick_open.cpp b/editor/quick_open.cpp index c71cc5af3fe..b92ebed1676 100644 --- a/editor/quick_open.cpp +++ b/editor/quick_open.cpp @@ -171,24 +171,7 @@ void EditorQuickOpen::_parse_fs(EditorFileSystemDirectory *efsd, Vector > pair; pair.first = file; pair.second = get_icon((has_icon(efsd->get_file_type(i), ei) ? efsd->get_file_type(i) : ot), ei); - - if (search_text != String() && list.size() > 0) { - - float this_sim = _path_cmp(search_text, file); - float other_sim = _path_cmp(list[0].first, file); - int pos = 1; - - while (pos < list.size() && this_sim <= other_sim) { - other_sim = _path_cmp(list[pos++].first, file); - } - - pos = this_sim >= other_sim ? pos - 1 : pos; - list.insert(pos, pair); - - } else { - - list.push_back(pair); - } + list.push_back(pair); } } @@ -200,6 +183,40 @@ void EditorQuickOpen::_parse_fs(EditorFileSystemDirectory *efsd, Vector > > EditorQuickOpen::_sort_fs(Vector > > &list) { + + String search_text = search_box->get_text(); + Vector > > sorted_list; + + if (search_text == String() || list.size() == 0) + return sorted_list; + + Vector scores; + scores.resize(list.size()); + for (int i = 0; i < list.size(); i++) + scores[i] = _path_cmp(search_text, list[i].first); + + while (list.size() > 0) { + + float best_score = 0.0f; + int best_idx = 0; + + for (int i = 0; i < list.size(); i++) { + float current_score = scores[i]; + if (current_score > best_score) { + best_score = current_score; + best_idx = i; + } + } + + sorted_list.push_back(list[best_idx]); + list.remove(best_idx); + scores.remove(best_idx); + } + + return sorted_list; +} + void EditorQuickOpen::_update_search() { search_options->clear(); @@ -208,6 +225,7 @@ void EditorQuickOpen::_update_search() { Vector > > list; _parse_fs(efsd, list); + list = _sort_fs(list); for (int i = 0; i < list.size(); i++) { TreeItem *ti = search_options->create_item(root); diff --git a/editor/quick_open.h b/editor/quick_open.h index 3f64dd8cf07..5b91965920f 100644 --- a/editor/quick_open.h +++ b/editor/quick_open.h @@ -49,6 +49,7 @@ class EditorQuickOpen : public ConfirmationDialog { void _sbox_input(const Ref &p_ie); void _parse_fs(EditorFileSystemDirectory *efsd, Vector > > &list); + Vector > > _sort_fs(Vector > > &list); float _path_cmp(String search, String path) const; void _confirmed();