2
\$\begingroup\$

The point of this method is to emulate Java hashCode().

In order for this to work the project must have arithmetic overflow allowed under:

project --> compiler options --> advanced.

    Public Shared Function GetHashCodeStr(value As String) As Integer
        Dim finalCode As Integer = 0

        For i As Integer = 0 To value.Length - 1
           Dim powerValue = (Convert.ToInt32(value(i)) * Math.Pow(31, value.Length - 1 - i))

        If powerValue > Integer.MaxValue Then
            Dim timesGreaterThanInt = Math.Floor(powerValue / Integer.MaxValue)
            powerValue = powerValue - timesGreaterThanInt - (timesGreaterThanInt) * Integer.MaxValue
        End If

        finalCode += CInt(powerValue)
    Next

    Return finalCode
End Function
\$\endgroup\$

2 Answers 2

2
\$\begingroup\$

I know little about this language, but dealing with powers of 31 is surely no good idea. Most probably, it'll overflow or lose precession and you get pure garbage.

If there's something like long (64 bit integer), you can rewrite the Java implementation easily. The only relevant part is the loop doing

h = 31*h + val[off++];

Just reduce h after every iteration to the int range, so it doesn't crash your Windows overflow.

Actually, you need no long, a double (floating point with 56 bits of mantissa) would do. Simple float (24 bits mantissa) is unusable here.

\$\endgroup\$
4
  • \$\begingroup\$ First I understand this not ideal, but I must emulate the existing functionality as a requirement (read my comment about turning on arithmetic overflow...).Test It With --> rextester.com/XKJZ76484 If you test it using you will see it uses the algorithm: s[0]*31^(n-1) + s[1]*31^(n-2) + ... + s[n-1] \$\endgroup\$
    – Rick B.
    Commented May 28, 2015 at 13:38
  • \$\begingroup\$ @RickB. I know that you needs exactly the same numbers. But using the Java algorithm is much easier and much faster than using the underlying formula. Test it with a string of a few thousand chars and we'll see. +++ "If you test it using you will see it uses the algorithm" - I pointed you to how it really gets computed in Java, do you think that grepcode lies? +++ And sure, the formula returns the same results. \$\endgroup\$
    – maaartinus
    Commented May 28, 2015 at 13:47
  • \$\begingroup\$ Ok I will give it try. Initially looking at the code I am not seeing the potential wrap to a negative value. \$\endgroup\$
    – Rick B.
    Commented May 28, 2015 at 13:48
  • \$\begingroup\$ Thanks that was very helpful. I was unable through my initial search to find the code snippet that you found. You are correct that does solve the problem. I dismissed your answer to soon ;) Thanks! I will post the code to the solution \$\endgroup\$
    – Rick B.
    Commented May 28, 2015 at 14:05
1
\$\begingroup\$

My initial implementation was flawed in that the exponent used to compute the hash could result (with a sufficiently long string) in a potential overflow of long. Using the official JAVA implementation prevents that from happening and ultimately executes faster. Allowing arithmetic overflow (in your VB.NET project) is still important to not receive an error when overflowing the hash integer value which is expected behavior.

Public Shared Function GetHashCodeStr(ByVal input As String) As Integer

    Dim h As Integer = 0
    Dim hash As Integer = 0
    If h = 0 Then
        Dim off As Integer = 0
        Dim val() As Char = Input.ToCharArray()
        Dim len As Integer = val.Length

        Dim i As Integer
        For i = 0 To len - 1 Step 1
            h = 31 * h + Convert.ToInt32(val(off))
            off = off + 1
        Next
        hash = h
    End If
    Return h
End Function
\$\endgroup\$
2
  • \$\begingroup\$ Interesting... the product of the solution seems to be pretty important in this case. When I come to this site I am looking for existing solutions to problems (if I am not posting a question myself). If I need to go to a link to interpret the "correct" solution that seems like a great learning process, but not what I am looking for if I want the solution quickly. Is this site simply a learning tool to teach others of the code review process? \$\endgroup\$
    – Rick B.
    Commented May 28, 2015 at 14:44
  • \$\begingroup\$ Alright that is fair. I will add further explanation. \$\endgroup\$
    – Rick B.
    Commented May 28, 2015 at 14:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.