1 |
/* |
2 |
* Hash function |
3 |
* by Tom Epperly |
4 |
* 10/24/89 |
5 |
* Version: $Revision: 1.1 $ |
6 |
* Version control file: $RCSfile: hashpjw.c,v $ |
7 |
* Date last modified: $Date: 1997/07/18 11:38:36 $ |
8 |
* Last modified by: $Author: mthomas $ |
9 |
* |
10 |
* This file is part of the Ascend Language Interpreter. |
11 |
*/ |
12 |
|
13 |
#include <stdio.h> |
14 |
#include "utilities/ascConfig.h" |
15 |
#include "utilities/ascPanic.h" |
16 |
#include "general/hashpjw.h" |
17 |
|
18 |
#ifndef lint |
19 |
static CONST char HashpjwID[] = "$Id: hashpjw.c,v 1.1 1997/07/18 11:38:36 mthomas Exp $"; |
20 |
#endif |
21 |
|
22 |
unsigned long hashpjw(register CONST char *str, |
23 |
register unsigned long int size) |
24 |
{ |
25 |
register CONST char *p; |
26 |
register unsigned long h=0,g; |
27 |
|
28 |
asc_assert((NULL != str) && (size > 0)); |
29 |
|
30 |
for(p = str; *p != '\0'; p++) { |
31 |
h = (h << 4) + (*p); |
32 |
if (0 != (g = h&0xf0000000)) { |
33 |
h = h ^ (g >> 24); |
34 |
h = h ^ g; |
35 |
} |
36 |
} |
37 |
return h % size; |
38 |
} |
39 |
|
40 |
/* |
41 |
* This is a temporary integer hashing function. |
42 |
* It is relatively expensive, as we first do int to str |
43 |
* transformation. This needs to be fixed with a proper |
44 |
* integer hashing function. |
45 |
*/ |
46 |
unsigned long hashpjw_int(int id, |
47 |
register unsigned long int size) |
48 |
{ |
49 |
char tmp[64]; |
50 |
|
51 |
(void)snprintf(tmp, 64, "%d", id); |
52 |
return hashpjw(tmp, size); |
53 |
} |
54 |
|
55 |
|
56 |
|