realbasic-nug
[Top] [All Lists]

Re: Doh! Variant.Hash is case insenstive

To: REALbasic NUG <realbasic-nug at lists dot realsoftware dot com>
Subject: Re: Doh! Variant.Hash is case insenstive
From: Phil M <phil at mobleybros dot com>
Date: Wed, 30 Nov 2005 18:38:49 -0800
Delivered-to: realbasic-nug at lists dot realsoftware dot com
References: <BFB3A603 dot 5C82%Roph at kc dot rr dot com>
On Nov 30, 2005, at 4:54 PM, Roph wrote:

1) I realized there's a bug in this since MidB returns a character, not an
integer.  The line showing:

I don't know how good this is speed wise or with collisions, but I would do something like the function below (but I am no hash expert). It seems rather fast in my tests except when you get into long strings... I am not sure where the break-point is, but running MD5 on a long string first (and then hashing the hash) seems to work almost as fast as short strings.

I think I am using REALbasic 2005 only features with the MemoryBlock, or at least 5.5.4 is required. Yes, you pass in any string to this function and the string gets automatically converted to the MemoryBlock.

Protected Function StringHash(s As MemoryBlock) As Integer

  Dim a, b, k, kMax As Integer

  kMax = Ceil(s.Size / 2) * 2     // Round up to nearest 2-byte block
  a = 42487                       // start with a decent prime number
  b = 11953                       // start with a decent prime number

s.LittleEndian = False // make hash cross-platform compatible
  s.Size = kMax                   // make MemoryBlock 2-byte friendly

  #pragma DisableBackgroundTasks
  While k < kMax
    a = (a + s.UShort(k)) Mod 65521
    b = (b + a) Mod 65521
    k = k + 2
  Wend

  Return Bitwise.BitOr(Bitwise.ShiftLeft(b, 16), a)

End Function

_______________________________________________
Unsubscribe or switch delivery mode:
<http://www.realsoftware.com/support/listmanager/>

Search the archives of this list here:
<http://support.realsoftware.com/listarchives/lists.html>

<Prev in Thread] Current Thread [Next in Thread>