I've implemented a hash table / map as part of the "teachers examples": https://www.b4x.com/android/forum/threads/--examples-for-teachers-algorithms.115894/post-724991
The keys are strings and there is a sub that calculates their hash code.
Which of the following alternative hashes will work properly and which won't:
A.
B.
C.
D.
The keys are strings and there is a sub that calculates their hash code.
Which of the following alternative hashes will work properly and which won't:
A.
B4X:
Private Sub CalcHash (key As String) As Int
Return key.Length
End Sub
B4X:
Private Sub CalcHash (key As String) As Int
Return -5
End Sub
B4X:
Private Sub CalcHash (key As String) As Int
Dim h As Int = Rnd(0, 1000)
For i = 0 To key.Length - 1
h = Bit.ShiftLeft(h, 5) + h + Asc(key.CharAt(i))
Next
Return h
End Sub
B4X:
Private Sub CalcHash (key As String) As Int
key = key.ToLowerCase
Dim h As Int = 5338
For i = 0 To key.Length - 1
h = Bit.ShiftLeft(h, 5) + h + Asc(key.CharAt(i))
Next
Return h
End Sub
Last edited: