Skip to content

Commit 32435bc

Browse files
taoocerosjjw24
authored andcommitted
Use Hashset and customized equality comparator to filter result
1 parent 6543e4f commit 32435bc

File tree

1 file changed

+22
-6
lines changed

1 file changed

+22
-6
lines changed

Plugins/Flow.Launcher.Plugin.Explorer/Search/SearchManager.cs

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,25 @@ public SearchManager(Settings settings, PluginInitContext context)
2222
this.settings = settings;
2323
}
2424

25+
private class PathEqualityComparator : IEqualityComparer<Result>
26+
{
27+
private static PathEqualityComparator instance;
28+
public static PathEqualityComparator Instance => instance ??= new PathEqualityComparator();
29+
public bool Equals(Result x, Result y)
30+
{
31+
return x.SubTitle == y.SubTitle;
32+
}
33+
34+
public int GetHashCode(Result obj)
35+
{
36+
return obj.SubTitle.GetHashCode();
37+
}
38+
39+
}
40+
2541
internal async Task<List<Result>> SearchAsync(Query query, CancellationToken token)
2642
{
27-
var results = new List<Result>();
43+
var results = new HashSet<Result>(PathEqualityComparator.Instance);
2844

2945
var querySearch = query.Search;
3046

@@ -50,9 +66,9 @@ internal async Task<List<Result>> SearchAsync(Query query, CancellationToken tok
5066

5167
if (!querySearch.IsLocationPathString() && !isEnvironmentVariablePath)
5268
{
53-
results.AddRange(await WindowsIndexFilesAndFoldersSearchAsync(query, querySearch, token).ConfigureAwait(false));
69+
results.UnionWith(await WindowsIndexFilesAndFoldersSearchAsync(query, querySearch, token).ConfigureAwait(false));
5470

55-
return results;
71+
return results.ToList();
5672
}
5773

5874
var locationPath = querySearch;
@@ -62,7 +78,7 @@ internal async Task<List<Result>> SearchAsync(Query query, CancellationToken tok
6278

6379
// Check that actual location exists, otherwise directory search will throw directory not found exception
6480
if (!FilesFolders.LocationExists(FilesFolders.ReturnPreviousDirectoryIfIncompleteString(locationPath)))
65-
return results;
81+
return results.ToList();
6682

6783
var useIndexSearch = UseWindowsIndexForDirectorySearch(locationPath);
6884

@@ -79,9 +95,9 @@ internal async Task<List<Result>> SearchAsync(Query query, CancellationToken tok
7995

8096
token.ThrowIfCancellationRequested();
8197

82-
results.AddRange(directoryResult);
98+
results.UnionWith(directoryResult);
8399

84-
return results;
100+
return results.ToList();
85101
}
86102

87103
private async Task<List<Result>> WindowsIndexFileContentSearchAsync(Query query, string querySearchString, CancellationToken token)

0 commit comments

Comments
 (0)