e42c7ed451/library/data/hash.retro
Commiter: Charles Childers
Author: Charles Childers
Revision: e42c7ed451
File Size: 1.89 KB
(March 07, 2010 03:59 UTC) About 2 years ago
add associate arrays and hashing libraries from Marc
( ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ )
( Proposed hash function for Retro -- based on djb2, Bernstein )
( )
( see: http://www.cse.yorku.ca/~oz/hash.html, for example. )
( ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ )
( Copyright [c] 2010, Marc Simpson )
( License: ISC )
( ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ )
( Most documented hash functions leverage unsigned longs )
( during computation -- here we use *signed* cells as unsigned )
( words are not offered by Retro at the present time. So that )
( we avoid returning negative hash values, hashing is filtered )
( through 'abs'. [Negative values emerge due to shifting into )
( the sign bit.] )
( )
( The 'hash-prime' variable has been selected to provide a )
( reasonable balance between clashing and key size -- this is )
( to ensure that associative arrays built using 'hash' don't )
( need to allocate too much heap. This can be adjusted by )
( revectoring 'hash' in the [unlikely] event of large tables. )
( ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ )
vocab hashing
((
{{
: abs dup 0 <if neg then ;
---reveal---
( Dan Bernstein, comp.lang.c with 'abs' hack )
: djb2 ( $-n )
5381 over getLength fori push
dup 5 << + over pop + @ + nexti nip abs ;
}}
( 193, 389, 769 are all pretty good; if you expect to have )
( a lot of keys, use a larger prime. )
389 variable: hash-prime
( Vector the favoured hash function -- we intend to offer )
( a range of implementations and allow the user to choose )
: hash ( $-n ) djb2 hash-prime @ mod ;
))
' hashing shut |