Skip to content

Commit

Permalink
change hash algorithm
Browse files Browse the repository at this point in the history
  • Loading branch information
starik222 committed May 3, 2023
1 parent 8324268 commit 7a3f9ee
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 13 deletions.
6 changes: 6 additions & 0 deletions BooruDatasetTagManager/Adler32.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,12 @@ public static long GenerateHash(byte[] buf)
public static long GenerateHash(long adler, byte[] buf)
{
return adler32(adler, buf, 0, buf.Length);
}

public static long GenerateHash(string text)
{
byte[] data = Encoding.UTF8.GetBytes(text);
return adler32(1, data, 0, data.Length);
}
}
}
6 changes: 5 additions & 1 deletion BooruDatasetTagManager/Extensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using System.ComponentModel;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using Translator.Crypto;

namespace BooruDatasetTagManager
{
Expand All @@ -18,7 +19,10 @@ public static void AddRange(this List<TagValue> list, IEnumerable<string> range)
list.Add(new TagValue(item));
}


public static long GetHash(this string text)
{
return Adler32.GenerateHash(text);
}

public static object LoadDataSet(string path)
{
Expand Down
10 changes: 5 additions & 5 deletions BooruDatasetTagManager/TagsDB.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@ public class TagsDB
public List<TagItem> Tags;
public Dictionary<string, long> LoadedFiles;

private Dictionary<int, int> hashes;
private Dictionary<long, int> hashes;

public TagsDB()
{
Tags = new List<TagItem>();
LoadedFiles = new Dictionary<string, long>();
hashes = new Dictionary<int, int>();
hashes = new Dictionary<long, int>();
}

private string[] ReadAllLines(byte[] data, Encoding encoding)
Expand Down Expand Up @@ -148,7 +148,7 @@ private void AddTag(string tag, int count, bool isAlias = false, string parent =
if (Tags.Exists(a => a.Parent == tag))
return;
tag = tag.Trim().ToLower();
int tagHash = tag.GetHashCode();
long tagHash = tag.GetHash();

int existTagIndex = -1;
TagItem tagItem = null;
Expand Down Expand Up @@ -217,7 +217,7 @@ public void SaveTags(string fPath)
public class TagItem
{
public string Tag { get; private set; }
public int TagHash { get; private set; }
public long TagHash { get; private set; }
public int Count;
//public List<string> Aliases;
public bool IsAlias;
Expand All @@ -233,7 +233,7 @@ public TagItem()
public void SetTag(string tag)
{
Tag = tag.Trim().ToLower();
TagHash = Tag.GetHashCode();
TagHash = Tag.GetHash();
}

public string GetTag()
Expand Down
14 changes: 7 additions & 7 deletions BooruDatasetTagManager/TranslationManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,28 +52,28 @@ public void LoadTranslations()

public bool Contains(string orig)
{
return Translations.Exists(a => a.OrigHash == orig.ToLower().GetHashCode());
return Translations.Exists(a => a.OrigHash == orig.ToLower().GetHash());
}

public bool Contains(int hash)
public bool Contains(long hash)
{
return Translations.Exists(a => a.OrigHash == hash);
}

public string GetTranslation(string text)
{
return GetTranslation(text.ToLower().Trim().GetHashCode());
return GetTranslation(text.ToLower().Trim().GetHash());
}

public string GetTranslation(int hash)
public string GetTranslation(long hash)
{
var res = Translations.FirstOrDefault(a => a.OrigHash == hash);
if (res == null)
return null;
return res.Trans;
}

public string GetTranslation(int hash, bool onlyManual)
public string GetTranslation(long hash, bool onlyManual)
{
if (onlyManual)
{
Expand Down Expand Up @@ -123,14 +123,14 @@ public class TransItem
{
public string Orig { get; private set; }
public string Trans {get; set; }
public int OrigHash { get; private set; }
public long OrigHash { get; private set; }
public bool IsManual { get; private set; }

public TransItem(string orig, string trans, bool isManual)
{
Orig = orig;
Trans = trans;
OrigHash = orig.ToLower().GetHashCode();
OrigHash = orig.ToLower().GetHash();
IsManual = isManual;
}

Expand Down

0 comments on commit 7a3f9ee

Please sign in to comment.