willy tarreau | 1235015 | 2005-12-18 01:03:27 +0100 | [diff] [blame] | 1 | /* |
| 2 | This File is copied from |
| 3 | |
| 4 | http://www.oreilly.com/catalog/masteralgoc/index.html |
| 5 | Mastering Algorithms with C |
| 6 | By Kyle Loudon |
| 7 | ISBN: 1-56592-453-3 |
| 8 | Publishd by O'Reilly |
| 9 | |
| 10 | We have added our own struct to these function. |
| 11 | */ |
| 12 | |
| 13 | /***************************************************************************** |
| 14 | * * |
| 15 | * ------------------------------- hashpjw.c ------------------------------ * |
| 16 | * * |
| 17 | *****************************************************************************/ |
| 18 | |
Willy Tarreau | e3ba5f0 | 2006-06-29 18:54:54 +0200 | [diff] [blame] | 19 | #include <common/config.h> |
Willy Tarreau | 2dd0d47 | 2006-06-29 17:53:05 +0200 | [diff] [blame] | 20 | #include <common/hashpjw.h> |
| 21 | #include <common/appsession.h> |
willy tarreau | 1235015 | 2005-12-18 01:03:27 +0100 | [diff] [blame] | 22 | |
| 23 | /***************************************************************************** |
| 24 | * * |
| 25 | * -------------------------------- hashpjw ------------------------------- * |
| 26 | * * |
| 27 | *****************************************************************************/ |
| 28 | |
| 29 | int hashpjw(const void *key) { |
| 30 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 31 | const char *ptr; |
| 32 | unsigned int val; |
| 33 | appsess *appsession_temp; |
willy tarreau | 1235015 | 2005-12-18 01:03:27 +0100 | [diff] [blame] | 34 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 35 | /********************************************************************* |
| 36 | * * |
| 37 | * Hash the key by performing a number of bit operations on it. * |
| 38 | * * |
| 39 | *********************************************************************/ |
willy tarreau | 1235015 | 2005-12-18 01:03:27 +0100 | [diff] [blame] | 40 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 41 | val = 0; |
| 42 | appsession_temp = (appsess *)key; |
| 43 | ptr = appsession_temp->sessid; |
willy tarreau | 1235015 | 2005-12-18 01:03:27 +0100 | [diff] [blame] | 44 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 45 | while (*ptr != '\0') { |
willy tarreau | 1235015 | 2005-12-18 01:03:27 +0100 | [diff] [blame] | 46 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 47 | int tmp; |
willy tarreau | 1235015 | 2005-12-18 01:03:27 +0100 | [diff] [blame] | 48 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 49 | val = (val << 4) + (*ptr); |
willy tarreau | 1235015 | 2005-12-18 01:03:27 +0100 | [diff] [blame] | 50 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 51 | if((tmp = (val & 0xf0000000))) { |
| 52 | val = val ^ (tmp >> 24); |
| 53 | val = val ^ tmp; |
| 54 | } |
| 55 | ptr++; |
| 56 | }/* end while */ |
willy tarreau | 1235015 | 2005-12-18 01:03:27 +0100 | [diff] [blame] | 57 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 58 | /********************************************************************* |
| 59 | * * |
| 60 | * In practice, replace PRIME_TBLSIZ with the actual table size. * |
| 61 | * * |
| 62 | *********************************************************************/ |
| 63 | return val % PRIME_TBLSIZ; |
willy tarreau | 1235015 | 2005-12-18 01:03:27 +0100 | [diff] [blame] | 64 | }/* end hashpjw */ |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 65 | |
| 66 | /* |
| 67 | * Local variables: |
| 68 | * c-indent-level: 8 |
| 69 | * c-basic-offset: 8 |
| 70 | * End: |
| 71 | */ |