Merge pull request #398 from Flow-Launcher/scoreBumpWithQuery

add score bumping with query and do hashcode customized
This commit is contained in:
Jeremy Wu 2021-04-07 08:35:03 +10:00 committed by GitHub
commit c78e08abaa
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -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<string, int> records { get; private set; }
public Dictionary<int, int> records { get; private set; }
public UserSelectedRecord()
{
records = new Dictionary<string, int>();
records = new Dictionary<int, int>();
}
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 @@ namespace Flow.Launcher.Storage
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;
}