I've begun actively programming in D again;
as a test run I've written up a number of useful string hashing functions.
Warning:
This code is provided as is with no warranty implied or otherwise. Users must
determine whether it is fit for any particular purpose, as we, the authors, make
no claim to that end. Use it at your own risk.
hash.d is licensed under the
Academic Free License v3.0.
That being said..
module hash;
//////////////////////
// Hash Library for D
// (C) Derrick Pallas
//
// Authors: Derrick Pallas
// Website: http://derrick.pallas.us/d/hash/
// License: Academic Free License 3.0
// Version: 2007-02-24a
//
/* standard bit-shift and xor */
uint crc( byte[] bytes, uint value = 0 )
{
foreach ( byte x ; bytes )
value = ( value<<5 | value>>>27 ) ^ x;
return value;
}
/* from Daniel Bernstein */
uint djb( byte[] bytes, uint value = 5381 )
{
foreach ( byte x ; bytes )
value = (value<<5) + value + x;
return value;
}
/* from Peter Weinberger */
uint pjw( byte[] bytes, uint value = 0 )
{
foreach ( byte x ; bytes )
{
value = (value<<4) + x;
value = ( value ^ (value & 0xf0000000)>>>24 ) & 0x0fffffff;
}
return value;
}
/* from Robert Sedgwick */
uint rs( byte[] bytes, uint value = 0, uint a = 63689, uint b = 378551 )
{
foreach ( byte x ; bytes )
{
value = value * a + x;
a *= b;
}
return value;
}
/* from Justin Sobel */
uint js( byte[] bytes, uint value = 1315423911 )
{
foreach ( byte x ; bytes )
value ^= (value<<5) + x + (value>>>2);
return value;
}
/* simple bit-shifter */
uint bit( byte[] bytes, uint value = 0, uint shift = 7 )
{
foreach ( byte x ; bytes )
value = value<<shift ^ x;
return value;
}
/* from Fowler, Noll, Vo */
// for 64-bit use value = 14695981039346656037, prime = 1099511628211
uint fnv( byte[] bytes, uint value = 2166136261, uint prime = 16777619 )
{
foreach ( byte x ; bytes )
value = value * prime ^ x;
return value;
}
/* from Donald Knuth */
uint dek( byte[] bytes ) { return dek(bytes,bytes.length); }
uint dek( byte[] bytes, uint value )
{
foreach ( byte x ; bytes )
value = value<<5 ^ x ^ value>>>27;
return value;
}
/* from Brian Kernighan and Dennis Ritchie */
uint bkdr ( byte[] bytes, uint value = 0, uint multiplier = 131 )
{
foreach ( byte x ; bytes )
value = value * multiplier + x;
return value;
}
/* from Ozan Yigit */
uint oy( byte[] bytes, uint value = 0 )
{
foreach ( byte x ; bytes )
value = (value<<16) + (value<<6) - value + x;
return value;
}
/* from Bruno Preiss */
uint brp( byte[] bytes, uint value = 0, uint shift = 6 )
{
uint mask = ~0 << 31-shift;
foreach ( byte x ; bytes )
value = (value & mask) ^ (value<<shift) ^ x;
return value;
}
/* this hash is so simple that you should never use it */
uint lose( byte[] bytes, uint value = 0 )
{
foreach ( byte x ; bytes )
value += x;
return value;
}
/* END
*****/