From 362504e2239cc60e21391fe11633b6633b4ab877 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=98=E9=9F=AC=20=E5=BC=A0?= Date: Thu, 1 Apr 2021 18:02:27 +0800 Subject: [PATCH 1/3] add score bumping with query and do hashcode customized --- Flow.Launcher/Storage/UserSelectedRecord.cs | 36 ++++++++++++++++++--- 1 file changed, 32 insertions(+), 4 deletions(-) diff --git a/Flow.Launcher/Storage/UserSelectedRecord.cs b/Flow.Launcher/Storage/UserSelectedRecord.cs index fd3d87fc4c6..e75ac7f63af 100644 --- a/Flow.Launcher/Storage/UserSelectedRecord.cs +++ b/Flow.Launcher/Storage/UserSelectedRecord.cs @@ -1,5 +1,7 @@ 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; @@ -8,16 +10,42 @@ namespace Flow.Launcher.Storage public class UserSelectedRecord { [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 = 23; + + unchecked + { + // skip the empty space + foreach (var item in query.ActionKeyword.Concat(query.Search)) + { + hashcode = hashcode * 31 + item; + } + + foreach (var item in result.Title) + { + hashcode = hashcode * 31 + item; + } + + foreach (var item in result.SubTitle) + { + hashcode = hashcode * 31 + 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 +59,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; } From d09ff638b3a997085cca2499da2b4f52374cff0b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=98=E9=9F=AC=20=E5=BC=A0?= Date: Fri, 2 Apr 2021 00:40:25 +0800 Subject: [PATCH 2/3] add comment and use for instead of foreach --- Flow.Launcher/Storage/UserSelectedRecord.cs | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/Flow.Launcher/Storage/UserSelectedRecord.cs b/Flow.Launcher/Storage/UserSelectedRecord.cs index e75ac7f63af..7082ee1c5dd 100644 --- a/Flow.Launcher/Storage/UserSelectedRecord.cs +++ b/Flow.Launcher/Storage/UserSelectedRecord.cs @@ -1,4 +1,5 @@ -using System.Collections.Generic; +using System; +using System.Collections.Generic; using System.Linq; using System.Text.Json.Serialization; using Flow.Launcher.Infrastructure; @@ -24,18 +25,30 @@ private static int GenerateCustomHashCode(Query query, Result result) unchecked { // skip the empty space - foreach (var item in query.ActionKeyword.Concat(query.Search)) + // 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 * 31 + item; + } + + for (int i = 0; i < query.Search.Length; i++) + { + char item = query.Search[i]; hashcode = hashcode * 31 + item; } - foreach (var item in result.Title) + for (int i = 0; i < result.Title.Length; i++) { + char item = result.Title[i]; hashcode = hashcode * 31 + item; } - foreach (var item in result.SubTitle) + for (int i = 0; i < result.SubTitle.Length; i++) { + char item = result.SubTitle[i]; hashcode = hashcode * 31 + item; } return hashcode; From 3051afad01d87badd8131852271862c9cd165213 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=98=E9=9F=AC=20=E5=BC=A0?= Date: Tue, 6 Apr 2021 18:35:29 +0800 Subject: [PATCH 3/3] move number become const --- Flow.Launcher/Storage/UserSelectedRecord.cs | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/Flow.Launcher/Storage/UserSelectedRecord.cs b/Flow.Launcher/Storage/UserSelectedRecord.cs index 7082ee1c5dd..668376dac22 100644 --- a/Flow.Launcher/Storage/UserSelectedRecord.cs +++ b/Flow.Launcher/Storage/UserSelectedRecord.cs @@ -10,6 +10,9 @@ 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; } @@ -20,7 +23,7 @@ public UserSelectedRecord() private static int GenerateCustomHashCode(Query query, Result result) { - int hashcode = 23; + int hashcode = HASH_INITIAL; unchecked { @@ -31,25 +34,25 @@ private static int GenerateCustomHashCode(Query query, Result result) for (int i = 0; i < query.ActionKeyword.Length; i++) { char item = query.ActionKeyword[i]; - hashcode = hashcode * 31 + item; + hashcode = hashcode * HASH_MULTIPLIER + item; } for (int i = 0; i < query.Search.Length; i++) { char item = query.Search[i]; - hashcode = hashcode * 31 + item; + hashcode = hashcode * HASH_MULTIPLIER + item; } for (int i = 0; i < result.Title.Length; i++) { char item = result.Title[i]; - hashcode = hashcode * 31 + item; + hashcode = hashcode * HASH_MULTIPLIER + item; } for (int i = 0; i < result.SubTitle.Length; i++) { char item = result.SubTitle[i]; - hashcode = hashcode * 31 + item; + hashcode = hashcode * HASH_MULTIPLIER + item; } return hashcode;