Skip to content

Instantly share code, notes, and snippets.

@tolotrasmile
Last active August 29, 2015 14:04
Show Gist options
  • Save tolotrasmile/de3be71ae48da3d89890 to your computer and use it in GitHub Desktop.
Save tolotrasmile/de3be71ae48da3d89890 to your computer and use it in GitHub Desktop.
public class Trello
{
public String Letters { get; set; }
public Int64 InitialHash { get; set; }
public Int64 Hash { get; set; }
public Int64 HashDepth { get; set; }
public Trello(String letters, Int64 initialHash, Int64 hashDepth)
{
this.Letters = letters;
this.InitialHash = initialHash;
this.Hash = this.InitialHash;
this.HashDepth = hashDepth;
}
public Trello()
: this(
"acdegilmnoprstuw", // Letters
7, // InitialHash
37 // HashDepth
)
{ }
/// <summary>
/// Get the hashcode of "s" (parameter)
/// </summary>
/// <param name="s"></param>
/// <returns></returns>
public Int64 GetHashCode(String s)
{
foreach (var item in s)
{
this.Hash = (this.Hash * this.HashDepth + this.Letters.IndexOf(item));
}
return this.Hash;
}
/// <summary>
/// Get the initial string of hashCode
/// </summary>
/// <param name="hashCode"></param>
/// <returns></returns>
public String ReverseHashCode(Int64 hashCode)
{
var reversed = "";
while (hashCode > this.InitialHash)
{
var index = hashCode % this.HashDepth;
var charValue = this.Letters[(int)index];
reversed += charValue;
hashCode = (hashCode - index) / this.HashDepth;
}
var x = "";
for (int i = 0; i < reversed.Length; i++)
{
x += reversed[reversed.Length - 1 - i];
}
return x;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment