Use Hashset and customized equality comparator to filter result

This commit is contained in:
弘韬 张 2021-02-18 06:38:43 +08:00 committed by Jeremy Wu
parent 6543e4f335
commit 32435bcda5

View file

@ -22,9 +22,25 @@ namespace Flow.Launcher.Plugin.Explorer.Search
this.settings = settings;
}
private class PathEqualityComparator : IEqualityComparer<Result>
{
private static PathEqualityComparator instance;
public static PathEqualityComparator Instance => instance ??= new PathEqualityComparator();
public bool Equals(Result x, Result y)
{
return x.SubTitle == y.SubTitle;
}
public int GetHashCode(Result obj)
{
return obj.SubTitle.GetHashCode();
}
}
internal async Task<List<Result>> SearchAsync(Query query, CancellationToken token)
{
var results = new List<Result>();
var results = new HashSet<Result>(PathEqualityComparator.Instance);
var querySearch = query.Search;
@ -50,9 +66,9 @@ namespace Flow.Launcher.Plugin.Explorer.Search
if (!querySearch.IsLocationPathString() && !isEnvironmentVariablePath)
{
results.AddRange(await WindowsIndexFilesAndFoldersSearchAsync(query, querySearch, token).ConfigureAwait(false));
results.UnionWith(await WindowsIndexFilesAndFoldersSearchAsync(query, querySearch, token).ConfigureAwait(false));
return results;
return results.ToList();
}
var locationPath = querySearch;
@ -62,7 +78,7 @@ namespace Flow.Launcher.Plugin.Explorer.Search
// Check that actual location exists, otherwise directory search will throw directory not found exception
if (!FilesFolders.LocationExists(FilesFolders.ReturnPreviousDirectoryIfIncompleteString(locationPath)))
return results;
return results.ToList();
var useIndexSearch = UseWindowsIndexForDirectorySearch(locationPath);
@ -79,9 +95,9 @@ namespace Flow.Launcher.Plugin.Explorer.Search
token.ThrowIfCancellationRequested();
results.AddRange(directoryResult);
results.UnionWith(directoryResult);
return results;
return results.ToList();
}
private async Task<List<Result>> WindowsIndexFileContentSearchAsync(Query query, string querySearchString, CancellationToken token)