--- act_hash_fct.c 2002/01/16 19:30:36 1.28
+++ act_hash_fct.c 2003/01/06 12:10:57 1.29
@@ -1,9 +1,10 @@
-/*
-** Act - Abstract Container Type Library
-** Copyright (c) 1999-2002 Ralf S. Engelschall <rse@engelschall.com>
+/*
+** OSSP act - Abstract Container Types
+** Copyright (c) 1999-2003 Ralf S. Engelschall <rse@engelschall.com>
+** Copyright (c) 1999-2003 The OSSP Project <http://www.ossp.org/>
**
-** This file is part of Act, a library for dealing with Abstract
-** Container Types which can be found at http://www.ossp.org/pkg/act/.
+** This file is part of OSSP act, an abstract container type library
+** which can be found at http://www.ossp.org/pkg/lib/act/.
**
** Permission to use, copy, modify, and distribute this software for
** any purpose with or without fee is hereby granted, provided that
@@ -33,7 +34,7 @@
** (weaker) requirements of the hash table lookup discipline:
**
** 1. the function must be deterministic and stateless
-** 2. the function must be very fast to compute
+** 2. the function must be very fast to compute
** 3. the function must distribute the keys very good
**
** Every function in this piece of source has the following signature:
@@ -57,12 +58,12 @@
** gazillion keys to each bucket. The number of collisions you get is
** expected to follow the Chi^2 distribution.
**
-** Here's how Chi^2 is computed:
-** 1. Lookup: b = total number of buckets
-** 2. Lookup: k = total number of keys
-** 3. Lookup: b_i = number of buckets which have i keys
+** Here's how Chi^2 is computed:
+** 1. Lookup: b = total number of buckets
+** 2. Lookup: k = total number of keys
+** 3. Lookup: b_i = number of buckets which have i keys
** 4. Compute: p = k/b (the expected number of keys per bucket)
-** 5. Compute: Chi^2 = sum (over all i) (b_i*((i-p)^2)/p)
+** 5. Compute: Chi^2 = sum (over all i) (b_i*((i-p)^2)/p)
**
** The distribution is expected to have a result close to b, i.e.,
** within 3*sqrt(b) of b. Chi^2 measures are usually reported in units of
@@ -126,7 +127,7 @@
* numbers are not useable at all. The remaining 128 odd numbers
* (except for the number 1) work more or less all equally well. They
* all distribute in an acceptable way and this way fill a hash table
- * with an average percent of approx. 86%.
+ * with an average percent of approx. 86%.
*
* If one compares the Chi^2 values of the variants, the number 33 not
* even has the best value. But the number 33 and a few other equally
@@ -144,9 +145,9 @@
* which has the hash unrolled eight times for speed. Both additionally
* are optimized for speed even more by unrolling the loop.
*/
-intern act_uint32_t
+intern act_uint32_t
act_hash_fct_djbx33a(
- register act_uint8_t *key,
+ register act_uint8_t *key,
register act_size_t len)
{
register act_uint32_t hash = 5381;
@@ -190,9 +191,9 @@
* good hash (both fast and with good distribution). It can be found for
* instance in his CDB package (see cdb_hash.c).
*/
-intern act_uint32_t
+intern act_uint32_t
act_hash_fct_djbx33x(
- register act_uint8_t *key,
+ register act_uint8_t *key,
register act_size_t len)
{
register act_uint32_t hash = 5381;
@@ -237,11 +238,11 @@
* So, this variant of DJB's original hash function is intended for
* hashing filesystem path like strings. Below there are two variants:
* the original variant from Frank Denis and additionally a variant
- * which has the hash unrolled eight times for speed.
+ * which has the hash unrolled eight times for speed.
*/
-intern act_uint32_t
+intern act_uint32_t
act_hash_fct_jedi(
- register act_uint8_t *key,
+ register act_uint8_t *key,
register act_size_t len)
{
register act_uint32_t hash = 5381;
@@ -283,9 +284,9 @@
* surprisingly well. It can be found for instance in the Berkeley-DB 3.x
* package (hash/hash_func.c).
*/
-intern act_uint32_t
+intern act_uint32_t
act_hash_fct_vocong(
- register act_uint8_t *key,
+ register act_uint8_t *key,
register act_size_t len)
{
register act_uint32_t hash = 0;
@@ -302,9 +303,9 @@
* in the AT&T's Cdt library. It is very fast and distributes very well.
* For details see Cdt's cdt.h file.
*/
-intern act_uint32_t
+intern act_uint32_t
act_hash_fct_cdt(
- register act_uint8_t *key,
+ register act_uint8_t *key,
register act_size_t len)
{
register act_uint32_t hash = 0;
@@ -332,9 +333,9 @@
* increasing same bytes. So our variant here uses the golden ratio (but
* every arbitrary value != 0 should work) instead.
*/
-intern act_uint32_t
+intern act_uint32_t
act_hash_fct_jotcl(
- register act_uint8_t *key,
+ register act_uint8_t *key,
register act_size_t len)
{
register act_uint32_t hash = 0x9e3779b9;
@@ -358,7 +359,7 @@
* signature). It can be definetely recommended as a good general
* purpose hash function.
*/
-intern act_uint32_t
+intern act_uint32_t
act_hash_fct_bjddj(
register act_uint8_t *k,
register act_size_t length)
@@ -395,7 +396,7 @@
/* handle the last 11 bytes */
c += length;
- switch(len) {
+ switch(len) {
/* all the case statements fall through */
case 11: c+=((ub4)k[10]<<24);
case 10: c+=((ub4)k[ 9]<<16);
@@ -428,7 +429,7 @@
* functions with medium performance but with very good distribution. So
* it can be considered as a rock solid general purpose hash function.
*/
-intern act_uint32_t
+intern act_uint32_t
act_hash_fct_crc32(
register act_uint8_t *key,
register act_size_t len)
@@ -488,7 +489,7 @@
0x2d02ef8dL
};
register act_uint32_t hash;
-
+
hash = 0xffffffff;
while (len-- > 0)
hash = tab[(hash ^ *key++) & 0xff] ^ (hash >> 8);
@@ -508,9 +509,9 @@
* variant above uses the value of the DJBX33A hash (but any arbitrary
* value should work) and this way avoid this, too.
*/
-intern act_uint32_t
+intern act_uint32_t
act_hash_fct_cpoaat(
- register act_uint8_t *ptr,
+ register act_uint8_t *ptr,
register act_size_t len)
{
register act_uint32_t hash = 5381;
@@ -538,12 +539,12 @@
* use inside hash data structures. Because it is slower than most of
* the other functions, although it distributes very well.
*/
-intern act_uint32_t
+intern act_uint32_t
act_hash_fct_teadm(
- register act_uint8_t *key,
+ register act_uint8_t *key,
register act_size_t len)
{
- act_uint32_t k[] = { 0x9464a485, 0x542e1a94, 0x3e846bff, 0xb75bcfc3 };
+ act_uint32_t k[] = { 0x9464a485, 0x542e1a94, 0x3e846bff, 0xb75bcfc3 };
act_uint32_t h0 = k[0], h1 = k[1];
act_uint32_t a, b, c, d;
act_uint32_t pad;
@@ -671,9 +672,9 @@
* below uses the recommended FNV-1 initialization. For more details see
* http://www.isthe.com/chongo/tech/comp/fnv/.
*/
-intern act_uint32_t
+intern act_uint32_t
act_hash_fct_fnv(
- register act_uint8_t *key,
+ register act_uint8_t *key,
register act_size_t len)
{
register act_uint32_t hash = 0x811c9dc5L;
@@ -704,9 +705,9 @@
* HASHC's so we handle all 8 bytes. Essentially, this saves 7 compare &
* branch instructions.
*/
-intern act_uint32_t
+intern act_uint32_t
act_hash_fct_ozsdbm(
- register act_uint8_t *ptr,
+ register act_uint8_t *ptr,
register act_size_t len)
{
register act_uint32_t hash = 0;
@@ -741,9 +742,9 @@
* good distribution, but unfortunately it is one of the slowest hash
* functions.
*/
-intern act_uint32_t
+intern act_uint32_t
act_hash_fct_kazlib(
- register act_uint8_t *key,
+ register act_uint8_t *key,
register act_size_t len)
{
static act_uint32_t tab[] = {
@@ -780,9 +781,9 @@
* 128 one and 128 zero bits (which is for a good statistically expected
* distribution).
*/
-intern act_uint32_t
+intern act_uint32_t
act_hash_fct_buzhash(
- register act_uint8_t *key,
+ register act_uint8_t *key,
register act_size_t len)
{
static act_uint32_t tab[256] = {
@@ -836,7 +837,7 @@
0xf36414bL, 0x1d3a034fL, 0x122d654fL, 0xb336078L, 0x3a8b9600L,
0xb5f1484L, 0x3ccfb7c6L, 0x2ff89cf1L, 0x09919a6L, 0xfa83287eL,
0x694b7cdL, 0x77df5aeaL, 0x944508ccL, 0x581fbb8L, 0x728a05cbL,
- 0x4a31712L, 0xc2f6acfaL, 0x6e560b10L, 0xd8d7ce1L, 0x0d2b2adeL,
+ 0x4a31712L, 0xc2f6acfaL, 0x6e560b10L, 0xd8d7ce1L, 0x0d2b2adeL,
0x0bbaa936L
};
register act_uint32_t hash = 0xe9ae3b8aL /* random init */;
@@ -862,37 +863,37 @@
* much and then produces lots of collisions (but that doesn't matter
* usually).
*/
-intern act_uint32_t
+intern act_uint32_t
act_hash_fct_pearson(
- register act_uint8_t *key,
+ register act_uint8_t *key,
register act_size_t len)
{
static unsigned char ptab[256] = {
- 0xd0, 0x24, 0x61, 0x1f, 0x65, 0xfb, 0xe1, 0x12, 0x64, 0xa7,
- 0xd9, 0x7f, 0x49, 0xf1, 0xfc, 0x89, 0xd8, 0x57, 0x03, 0xda,
- 0x4a, 0x4e, 0xc8, 0xb9, 0x42, 0x7b, 0x44, 0x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 0x1a, 0xa9, 0x46, 0x91, 0x93, 0xbc, 0xbe, 0xe2, 0x4b,
+ 0x18, 0xdc, 0xeb, 0x3c, 0x21, 0x47, 0x70, 0x4d, 0xae, 0xf9,
+ 0xee, 0xa3, 0xec, 0x97, 0x08, 0xab, 0xad, 0xbd, 0x48, 0xb0,
+ 0xa0, 0xb3, 0x68, 0xd7, 0xe4, 0xe3, 0x79, 0x4c, 0x95, 0x8b,
+ 0xb1, 0xf8, 0x2a, 0xa8, 0x9a, 0x30, 0xf3, 0xf5, 0xd3, 0x50,
+ 0xf0, 0x9e, 0x63, 0x9d, 0x72, 0x3f, 0xd2, 0x85, 0x60, 0x3b,
+ 0x0e, 0x6b, 0x19, 0x52, 0xe0, 0xef, 0x13, 0x6c, 0xb5, 0x8e,
+ 0x00, 0x14, 0x8a, 0x1e, 0x06, 0xa2, 0xfa, 0x0a, 0x8f, 0x80,
+ 0x86, 0x07, 0xed, 0x84, 0x92, 0x45, 0x26, 0xf7, 0x75, 0xd4,
+ 0x83, 0x7a, 0xdd, 0x62, 0x7c, 0x9b, 0xe5, 0xa1, 0x2e, 0xdb,
+ 0xea, 0x25, 0x5c, 0x87, 0x74, 0x5b, 0x99, 0x9f, 0xe8, 0x3a,
+ 0x66, 0x02, 0x59, 0x28, 0xb6, 0xcc, 0x53, 0xf4, 0xe9, 0x05,
0xd1, 0x78, 0xbb, 0x77, 0x2d, 0x23
};
register unsigned char h1,h2,h3,h4;
@@ -918,9 +919,9 @@
* as found in a similar form in hash.c inside his ipaudit package.
* It's an average hash function. Neither very good nor very bad.
*/
-intern act_uint32_t
+intern act_uint32_t
act_hash_fct_rifkin(
- register act_uint8_t *key,
+ register act_uint8_t *key,
register act_size_t len)
{
register act_uint32_t hash = 0;
@@ -936,15 +937,15 @@
return hash;
}
-/*
+/*
* ASU (Aho, Sethi, Ullman)
*
* This is the hashing algorithm as proposed by Aho, Seti and Ullmann in
* their algorithm books. It is not very fast, but distributes well.
*/
-intern act_uint32_t
+intern act_uint32_t
act_hash_fct_asu(
- register act_uint8_t *key,
+ register act_uint8_t *key,
register act_size_t len)
{
register act_uint32_t hash;
@@ -968,9 +969,9 @@
* not one of the best hash functions in the set, but might have some
* particular uses.
*/
-intern act_uint32_t
+intern act_uint32_t
act_hash_fct_holub(
- register act_uint8_t *key,
+ register act_uint8_t *key,
register act_size_t len)
{
register act_uint32_t hash;
@@ -998,9 +999,9 @@
* showed a bad distribution for real world keys. So take this hash very
* carefully and test whether it works for your data.
*/
-intern act_uint32_t
+intern act_uint32_t
act_hash_fct_cbu(
- register act_uint8_t *key,
+ register act_uint8_t *key,
register act_size_t len)
{
register act_uint32_t hash = 0;
@@ -1025,9 +1026,9 @@
* So this hash is only useful if one really knows the keys one has to
* hash and also tests whether this hash works for them.
*/
-intern act_uint32_t
+intern act_uint32_t
act_hash_fct_cvs(
- register act_uint8_t *key,
+ register act_uint8_t *key,
register act_size_t len)
{
register act_uint32_t hash = 0;
@@ -1042,10 +1043,10 @@
return hash;
}
-/*
+/*
** ======================================================================
** Hash Function Test and Comparison Suite
-** ======================================================================
+** ======================================================================
*/
#ifdef ACT_TEST
@@ -1245,7 +1246,7 @@
}
/* the driver program */
-int main(int argc, char *argv[])
+int main(int argc, char *argv[])
{
int i;
@@ -1265,16 +1266,16 @@
printf("| Hash Func Time Coll00 Coll55 CollNN Used Min Max Diff Chi2/S Chi2/B |\n");
printf("+ ---------- ------ ------ ------ ------ ----- ---- ---- ---- ------- ------- +\n");
for (i = 0; table[i].name != NULL; i++) {
- printf("| %-10s %6.2f %6d %6d %6d %5.2f %4d %4d %4d %7.2f%c%7.2f%c|\n",
- table[i].name,
- table[i].stat.t,
- table[i].stat.coll00,
- table[i].stat.coll55,
- table[i].stat.collNN,
- table[i].stat.used,
- table[i].stat.min,
- table[i].stat.max,
- table[i].stat.delta,
+ printf("| %-10s %6.2f %6d %6d %6d %5.2f %4d %4d %4d %7.2f%c%7.2f%c|\n",
+ table[i].name,
+ table[i].stat.t,
+ table[i].stat.coll00,
+ table[i].stat.coll55,
+ table[i].stat.collNN,
+ table[i].stat.used,
+ table[i].stat.min,
+ table[i].stat.max,
+ table[i].stat.delta,
table[i].stat.s_chi2,
(table[i].stat.s_chi2 > 3 || table[i].stat.s_chi2 < -3) ? '!' : ' ',
table[i].stat.b_chi2,
|