From 0606adceb3b6efa020c7389bdb31fe541b7d1d5f Mon Sep 17 00:00:00 2001 From: Hans Goudey Date: Mon, 13 Dec 2021 13:57:55 -0600 Subject: UI: String Search: Add an optional weight parameter This builds off of rBf951aa063f7, adding a weight parameter which can be used to change the order of items when they have the same match score. In the future, if string searching gets a C++ API, we could use an optional parameter for the weight, since it is not used yet. This will be used for the node link drag search menu (D8286). Differential Revision: https://developer.blender.org/D13559 --- source/blender/blenlib/BLI_string_search.h | 5 ++++- source/blender/blenlib/intern/string_search.cc | 17 ++++++++++++++--- 2 files changed, 18 insertions(+), 4 deletions(-) (limited to 'source/blender/blenlib') diff --git a/source/blender/blenlib/BLI_string_search.h b/source/blender/blenlib/BLI_string_search.h index b846a4d3c39..f0bf259d213 100644 --- a/source/blender/blenlib/BLI_string_search.h +++ b/source/blender/blenlib/BLI_string_search.h @@ -26,8 +26,11 @@ StringSearch *BLI_string_search_new(void); /** * Add a new possible result to the search. * The caller keeps ownership of all parameters. + * + * \param weight: Can be used to customize the order when multiple items have the same match score. */ -void BLI_string_search_add(StringSearch *search, const char *str, void *user_data); +void BLI_string_search_add(StringSearch *search, const char *str, void *user_data, int weight); + /** * Filter and sort all previously added search items. * Returns an array containing the filtered user data. diff --git a/source/blender/blenlib/intern/string_search.cc b/source/blender/blenlib/intern/string_search.cc index 02b0b814659..c5528dce2f2 100644 --- a/source/blender/blenlib/intern/string_search.cc +++ b/source/blender/blenlib/intern/string_search.cc @@ -389,6 +389,7 @@ struct SearchItem { blender::Span normalized_words; int length; void *user_data; + int weight; }; struct StringSearch { @@ -401,14 +402,19 @@ StringSearch *BLI_string_search_new() return new StringSearch(); } -void BLI_string_search_add(StringSearch *search, const char *str, void *user_data) +void BLI_string_search_add(StringSearch *search, + const char *str, + void *user_data, + const int weight) { using namespace blender; Vector words; StringRef str_ref{str}; string_search::extract_normalized_words(str_ref, search->allocator, words); - search->items.append( - {search->allocator.construct_array_copy(words.as_span()), (int)str_ref.size(), user_data}); + search->items.append({search->allocator.construct_array_copy(words.as_span()), + (int)str_ref.size(), + user_data, + weight}); } int BLI_string_search_query(StringSearch *search, const char *query, void ***r_data) @@ -449,6 +455,11 @@ int BLI_string_search_query(StringSearch *search, const char *query, void ***r_d std::sort(indices.begin(), indices.end(), [&](int a, int b) { return search->items[a].length < search->items[b].length; }); + /* Prefer items with larger weights. Use `stable_sort` so that if the weights are the same, + * the order won't be changed. */ + std::stable_sort(indices.begin(), indices.end(), [&](int a, int b) { + return search->items[a].weight > search->items[b].weight; + }); } sorted_result_indices.extend(indices); } -- cgit v1.2.3