Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 49 additions & 5 deletions Flow.Launcher/Storage/UserSelectedRecord.cs
Original file line number Diff line number Diff line change
@@ -1,23 +1,67 @@
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;

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]++;
Expand All @@ -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;
}
Expand Down