Re: How do you check the equivalency of references?
Re: How do you check the equivalency of references?
- Subject: Re: How do you check the equivalency of references?
- From: Arthur J Knapp <email@hidden>
- Date: Fri, 10 Aug 2001 17:35:57 -0400
>
Date: Thu, 9 Aug 2001 22:45:09 -0700
>
From: John W Baxter <email@hidden>
>
Subject: Re: How do you check the equivalency of references?
>
At 17:59 -0400 8/9/01, Arthur J Knapp wrote:
>
> (* A "universal" hash function for strings.
>
> *)
>
> on hash(str, max)
>
> set {a, b, h} to {31415, 27183, 0}
>
> repeat with c in str
>
> set h to (a * h + (ASCII number c)) mod max
>
> end repeat
>
> return h
>
> end hash
>
Hmmm...what does b do in there (aside from separating a and h)? ;-)
Whoops.
Leave it to you people to actually "read" what I post... ;-)
I think that I must have confused two separate functions that I was
porting, inadvertantly mixing up their separate purposes. May Robert
Sedgewick forgive me...
Let's try this:
on hashU(str, max)
set a to 31415
set b to 27183
set h to 0
repeat with c in str
set h to (a * h + (ASCII number c)) mod max
set a to (a * b) mod (max - 1)
end repeat
return h
end hashU
I think that max, by the way, is supposed to be a prime number,
but I don't really recall.
Hashs are often used for creating/looking up the index location
of a given string in an array, (list). If an AppleScripter wanted
to use this handler for that purpose, she would have to increment
the answer by 1, (for AppleScript's 1-based lists).
So, who wants to explain hash-collision...
Arthur J. Knapp
http://www.stellarvisions.com
mailto:email@hidden
Hey, check out:
http://www.eremita.demon.co.uk/scripting/applescript/