Take the 2-minute tour ×
Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

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
share|improve this question

2 Answers 2

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.

share|improve this answer
    
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] –  Rick B. May 28 at 13:38
    
@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. –  maaartinus May 28 at 13:47
    
Ok I will give it try. Initially looking at the code I am not seeing the potential wrap to a negative value. –  Rick B. May 28 at 13:48
    
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 –  Rick B. May 28 at 14:05
up vote 1 down vote accepted

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
share|improve this answer
3  
This isn't an answer, it's a code dump... if you would like the "final post-review version" to be peer reviewed, feel free to ask a follow-up question. Note, it's the review process and feedback provided in answers that's valuable, not the end result. –  Mat's Mug May 28 at 14:37
    
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? –  Rick B. May 28 at 14:44
1  
This site is a Stack Exchange Q&A site, not a forum - valuable answers on this site review the OP's code. If you want to post a "selfie" review, you can - and you are encouraged to do so. But you can't just dump the final code into an answer like this. –  Mat's Mug May 28 at 14:51
    
Alright that is fair. I will add further explanation. –  Rick B. May 28 at 14:54
1  
Code Review is a site that is different to Stack Overflow, and has a different purpose. It is more complicated than can be explained in a comment. Still, code-only answers are not code reviews, they need to at least have an explanation of why the code-dump is an improvement. If you are interested in getting further feedback on the revised code, then post a new question. This will be of interest: What you may and may not do after receiving answers –  rolfl May 28 at 14:55

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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