Skip to content

Commit

Permalink
Add small optimization on search
Browse files Browse the repository at this point in the history
  • Loading branch information
almedina-ms committed Jul 29, 2020
1 parent 1e121d6 commit fb75160
Showing 1 changed file with 17 additions and 15 deletions.
32 changes: 17 additions & 15 deletions src/TerminalSettings/TerminalSettings/MainPage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ namespace winrt
namespace MUX = Microsoft::UI::Xaml;
}

using namespace winrt::Windows::Foundation;
using namespace winrt::Windows::UI::Xaml;
using namespace winrt::TerminalSettings::implementation;

Expand Down Expand Up @@ -120,8 +121,9 @@ namespace winrt::SettingsControl::implementation
void MainPage::SearchSettings(hstring query, Controls::AutoSuggestBox &autoBox)
{
Windows::Foundation::Collections::IVector<IInspectable> suggestions = single_threaded_vector<IInspectable>();
std::vector<IInspectable> rawSuggestions;

for (std::map<IInspectable, hstring>::iterator it = SearchList.begin(); it != SearchList.end(); ++it)
for (auto& it = SearchList.begin(); it != SearchList.end(); ++it)
{
auto value = it->first;
hstring item = value.as<Windows::Foundation::IPropertyValue>().GetString();
Expand All @@ -136,22 +138,22 @@ namespace winrt::SettingsControl::implementation

if (std::wcsstr(item.c_str(), query.c_str()))
{
bool added = false;
for (int i = 0; i < suggestions.Size(); ++i)
{
if (value.as<Windows::Foundation::IPropertyValue>().GetString() < suggestions.GetAt(i).as<Windows::Foundation::IPropertyValue>().GetString())
{
suggestions.InsertAt(i, value);
added = true;
break;
}
}
if (added == false)
{
suggestions.Append(value);
}
rawSuggestions.emplace_back(value);
}
}

// perform sort comparing strings inside of IPropertyValues
std::sort(rawSuggestions.begin(), rawSuggestions.end(), [](const IInspectable& a, const IInspectable& b) -> bool
{
return a.as<IPropertyValue>().GetString() < b.as<IPropertyValue>().GetString();
});

// Pass all elements from rawSuggestions to suggestions
for (const auto& suggestion : rawSuggestions)
{
suggestions.Append(suggestion);
}

autoBox.ItemsSource(suggestions);
}

Expand Down

0 comments on commit fb75160

Please sign in to comment.