1996-07-09 08:22:35 +02:00
|
|
|
/*-------------------------------------------------------------------------
|
|
|
|
*
|
1999-02-14 00:22:53 +01:00
|
|
|
* hashfn.c
|
1997-09-07 07:04:48 +02:00
|
|
|
*
|
1996-07-09 08:22:35 +02:00
|
|
|
*
|
2000-01-26 06:58:53 +01:00
|
|
|
* Portions Copyright (c) 1996-2000, PostgreSQL, Inc
|
|
|
|
* Portions Copyright (c) 1994, Regents of the University of California
|
1996-07-09 08:22:35 +02:00
|
|
|
*
|
|
|
|
*
|
|
|
|
* IDENTIFICATION
|
2000-01-26 06:58:53 +01:00
|
|
|
* $Header: /cvsroot/pgsql/src/backend/utils/hash/hashfn.c,v 1.11 2000/01/26 05:57:24 momjian Exp $
|
1996-07-09 08:22:35 +02:00
|
|
|
*
|
|
|
|
*-------------------------------------------------------------------------
|
|
|
|
*/
|
1996-11-03 07:54:38 +01:00
|
|
|
#include "postgres.h"
|
|
|
|
|
1996-07-09 08:22:35 +02:00
|
|
|
#include "utils/hsearch.h"
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Assume that we've already split the bucket to which this
|
|
|
|
* key hashes, calculate that bucket, and check that in fact
|
|
|
|
* we did already split it.
|
|
|
|
*/
|
|
|
|
long
|
|
|
|
string_hash(char *key, int keysize)
|
|
|
|
{
|
1997-09-08 04:41:22 +02:00
|
|
|
int h;
|
1998-02-11 20:14:04 +01:00
|
|
|
unsigned char *k = (unsigned char *) key;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
|
|
|
h = 0;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Convert string to integer
|
|
|
|
*/
|
|
|
|
while (*k)
|
|
|
|
h = h * PRIME1 ^ (*k++ - ' ');
|
|
|
|
h %= PRIME2;
|
|
|
|
|
1998-09-01 05:29:17 +02:00
|
|
|
return h;
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
long
|
|
|
|
tag_hash(int *key, int keysize)
|
|
|
|
{
|
1998-02-26 05:46:47 +01:00
|
|
|
long h = 0;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Convert tag to integer; Use four byte chunks in a "jump table" to
|
|
|
|
* go a little faster. Currently the maximum keysize is 16 (mar 17
|
|
|
|
* 1992) I have put in cases for up to 24. Bigger than this will
|
|
|
|
* resort to the old behavior of the for loop. (see the default case).
|
|
|
|
*/
|
|
|
|
switch (keysize)
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
1997-09-08 04:41:22 +02:00
|
|
|
case 6 * sizeof(int):
|
1997-09-07 07:04:48 +02:00
|
|
|
h = h * PRIME1 ^ (*key);
|
1997-09-08 04:41:22 +02:00
|
|
|
key++;
|
|
|
|
/* fall through */
|
1997-09-07 07:04:48 +02:00
|
|
|
|
1997-09-08 04:41:22 +02:00
|
|
|
case 5 * sizeof(int):
|
|
|
|
h = h * PRIME1 ^ (*key);
|
|
|
|
key++;
|
|
|
|
/* fall through */
|
|
|
|
|
|
|
|
case 4 * sizeof(int):
|
|
|
|
h = h * PRIME1 ^ (*key);
|
|
|
|
key++;
|
|
|
|
/* fall through */
|
|
|
|
|
|
|
|
case 3 * sizeof(int):
|
|
|
|
h = h * PRIME1 ^ (*key);
|
|
|
|
key++;
|
|
|
|
/* fall through */
|
|
|
|
|
|
|
|
case 2 * sizeof(int):
|
|
|
|
h = h * PRIME1 ^ (*key);
|
|
|
|
key++;
|
|
|
|
/* fall through */
|
1997-09-07 07:04:48 +02:00
|
|
|
|
1997-09-08 04:41:22 +02:00
|
|
|
case sizeof(int):
|
|
|
|
h = h * PRIME1 ^ (*key);
|
|
|
|
key++;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
for (; keysize > (sizeof(int) - 1); keysize -= sizeof(int), key++)
|
|
|
|
h = h * PRIME1 ^ (*key);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* now let's grab the last few bytes of the tag if the tag has
|
|
|
|
* (size % 4) != 0 (which it sometimes will on a sun3).
|
|
|
|
*/
|
|
|
|
if (keysize)
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
1997-09-08 04:41:22 +02:00
|
|
|
char *keytmp = (char *) key;
|
|
|
|
|
|
|
|
switch (keysize)
|
|
|
|
{
|
|
|
|
case 3:
|
|
|
|
h = h * PRIME1 ^ (*keytmp);
|
|
|
|
keytmp++;
|
|
|
|
/* fall through */
|
|
|
|
case 2:
|
|
|
|
h = h * PRIME1 ^ (*keytmp);
|
|
|
|
keytmp++;
|
|
|
|
/* fall through */
|
|
|
|
case 1:
|
|
|
|
h = h * PRIME1 ^ (*keytmp);
|
|
|
|
break;
|
|
|
|
}
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
1997-09-08 04:41:22 +02:00
|
|
|
break;
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
1997-09-07 07:04:48 +02:00
|
|
|
|
|
|
|
h %= PRIME2;
|
1998-09-01 05:29:17 +02:00
|
|
|
return h;
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This is INCREDIBLY ugly, but fast.
|
|
|
|
* We break the string up into 8 byte units. On the first time
|
|
|
|
* through the loop we get the "leftover bytes" (strlen % 8).
|
|
|
|
* On every other iteration, we perform 8 HASHC's so we handle
|
|
|
|
* all 8 bytes. Essentially, this saves us 7 cmp & branch
|
|
|
|
* instructions. If this routine is heavily used enough, it's
|
|
|
|
* worth the ugly coding
|
|
|
|
*/
|
1997-08-19 23:40:56 +02:00
|
|
|
#ifdef NOT_USED
|
1996-07-09 08:22:35 +02:00
|
|
|
long
|
|
|
|
disk_hash(char *key)
|
|
|
|
{
|
1998-02-26 05:46:47 +01:00
|
|
|
int n = 0;
|
|
|
|
char *str = key;
|
|
|
|
int len = strlen(key);
|
|
|
|
int loop;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
|
|
|
#define HASHC n = *str++ + 65599 * n
|
|
|
|
|
|
|
|
if (len > 0)
|
|
|
|
{
|
|
|
|
loop = (len + 8 - 1) >> 3;
|
|
|
|
|
|
|
|
switch (len & (8 - 1))
|
|
|
|
{
|
1997-09-08 04:41:22 +02:00
|
|
|
case 0:
|
|
|
|
do
|
|
|
|
{ /* All fall throughs */
|
|
|
|
HASHC;
|
|
|
|
case 7:
|
|
|
|
HASHC;
|
|
|
|
case 6:
|
|
|
|
HASHC;
|
|
|
|
case 5:
|
|
|
|
HASHC;
|
|
|
|
case 4:
|
|
|
|
HASHC;
|
|
|
|
case 3:
|
|
|
|
HASHC;
|
|
|
|
case 2:
|
|
|
|
HASHC;
|
|
|
|
case 1:
|
|
|
|
HASHC;
|
|
|
|
} while (--loop);
|
1997-09-07 07:04:48 +02:00
|
|
|
}
|
|
|
|
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
1998-09-01 05:29:17 +02:00
|
|
|
return n;
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
|
|
|
|
1997-09-07 07:04:48 +02:00
|
|
|
#endif
|