diff --git a/Flow.Launcher/Storage/UserSelectedRecord.cs b/Flow.Launcher/Storage/UserSelectedRecord.cs index fd3d87fc4c6..668376dac22 100644 --- a/Flow.Launcher/Storage/UserSelectedRecord.cs +++ b/Flow.Launcher/Storage/UserSelectedRecord.cs @@ -1,5 +1,8 @@ -using System.Collections.Generic; +using System; +using System.Collections.Generic; +using System.Linq; using System.Text.Json.Serialization; +using Flow.Launcher.Infrastructure; using Flow.Launcher.Infrastructure.Storage; using Flow.Launcher.Plugin; @@ -7,17 +10,58 @@ namespace Flow.Launcher.Storage { public class UserSelectedRecord { + private const int HASH_MULTIPLIER = 31; + private const int HASH_INITIAL = 23; + [JsonInclude] - public Dictionary records { get; private set; } + public Dictionary records { get; private set; } public UserSelectedRecord() { - records = new Dictionary(); + records = new Dictionary(); + } + + private static int GenerateCustomHashCode(Query query, Result result) + { + int hashcode = HASH_INITIAL; + + unchecked + { + // skip the empty space + // https://stackoverflow.com/a/5155015 31 prime and is 2^5 - 1 which allows fast + // optimization without information lost when int overflow + + for (int i = 0; i < query.ActionKeyword.Length; i++) + { + char item = query.ActionKeyword[i]; + hashcode = hashcode * HASH_MULTIPLIER + item; + } + + for (int i = 0; i < query.Search.Length; i++) + { + char item = query.Search[i]; + hashcode = hashcode * HASH_MULTIPLIER + item; + } + + for (int i = 0; i < result.Title.Length; i++) + { + char item = result.Title[i]; + hashcode = hashcode * HASH_MULTIPLIER + item; + } + + for (int i = 0; i < result.SubTitle.Length; i++) + { + char item = result.SubTitle[i]; + hashcode = hashcode * HASH_MULTIPLIER + item; + } + return hashcode; + + } } public void Add(Result result) { - var key = result.ToString(); + var key = GenerateCustomHashCode(result.OriginQuery, result); if (records.ContainsKey(key)) { records[key]++; @@ -31,7 +75,7 @@ public void Add(Result result) public int GetSelectedCount(Result result) { - if (records.TryGetValue(result.ToString(), out int value)) + if (records.TryGetValue(GenerateCustomHashCode(result.OriginQuery, result), out int value)) { return value; }