1996-07-09 08:22:35 +02:00
|
|
|
/*-------------------------------------------------------------------------
|
|
|
|
*
|
1999-02-14 00:22:53 +01:00
|
|
|
* hashfunc.c
|
1997-09-07 07:04:48 +02:00
|
|
|
* Comparison functions for hash access method.
|
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-06-05 09:29:25 +02:00
|
|
|
* $Header: /cvsroot/pgsql/src/backend/access/hash/hashfunc.c,v 1.26 2000/06/05 07:28:35 tgl Exp $
|
1996-07-09 08:22:35 +02:00
|
|
|
*
|
|
|
|
* NOTES
|
1997-09-07 07:04:48 +02:00
|
|
|
* These functions are stored in pg_amproc. For each operator class
|
|
|
|
* defined on hash tables, they compute the hash value of the argument.
|
1996-07-09 08:22:35 +02:00
|
|
|
*
|
|
|
|
*-------------------------------------------------------------------------
|
|
|
|
*/
|
1996-10-20 08:34:30 +02:00
|
|
|
|
1996-07-09 08:22:35 +02:00
|
|
|
#include "postgres.h"
|
|
|
|
|
1996-11-10 04:06:38 +01:00
|
|
|
#include "access/hash.h"
|
|
|
|
|
2000-06-05 09:29:25 +02:00
|
|
|
Datum
|
|
|
|
hashint2(PG_FUNCTION_ARGS)
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
2000-06-05 09:29:25 +02:00
|
|
|
PG_RETURN_UINT32((uint32) ~ PG_GETARG_INT16(0));
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
|
|
|
|
2000-06-05 09:29:25 +02:00
|
|
|
Datum
|
|
|
|
hashint4(PG_FUNCTION_ARGS)
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
2000-06-05 09:29:25 +02:00
|
|
|
PG_RETURN_UINT32(~ PG_GETARG_UINT32(0));
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
|
|
|
|
2000-06-05 09:29:25 +02:00
|
|
|
Datum
|
|
|
|
hashint8(PG_FUNCTION_ARGS)
|
1999-03-14 06:09:05 +01:00
|
|
|
{
|
2000-06-05 09:29:25 +02:00
|
|
|
/* we just use the low 32 bits... */
|
|
|
|
PG_RETURN_UINT32(~((uint32) PG_GETARG_INT64(0)));
|
1999-03-14 06:09:05 +01:00
|
|
|
}
|
|
|
|
|
1996-07-09 08:22:35 +02:00
|
|
|
/* Hash function from Chris Torek. */
|
2000-06-05 09:29:25 +02:00
|
|
|
Datum
|
|
|
|
hashfloat4(PG_FUNCTION_ARGS)
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
2000-06-05 09:29:25 +02:00
|
|
|
float4 key = PG_GETARG_FLOAT4(0);
|
|
|
|
char *kp = (char *) &key;
|
|
|
|
int len = sizeof(key);
|
1997-09-08 04:41:22 +02:00
|
|
|
int loop;
|
|
|
|
uint32 h;
|
1996-07-09 08:22:35 +02:00
|
|
|
|
1997-09-07 07:04:48 +02:00
|
|
|
#define HASH4a h = (h << 5) - h + *kp++;
|
|
|
|
#define HASH4b h = (h << 5) + h + *kp++;
|
1996-07-09 08:22:35 +02:00
|
|
|
#define HASH4 HASH4b
|
|
|
|
|
1997-09-07 07:04:48 +02:00
|
|
|
h = 0;
|
2000-06-05 09:29:25 +02:00
|
|
|
/*
|
|
|
|
* This is a tad silly, given that we expect len = 4, but a smart
|
|
|
|
* compiler should be able to eliminate the redundant code...
|
|
|
|
*/
|
|
|
|
loop = (len + 8 - 1) >> 3;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
2000-06-05 09:29:25 +02:00
|
|
|
switch (len & (8 - 1))
|
|
|
|
{
|
|
|
|
case 0:
|
|
|
|
do
|
|
|
|
{ /* All fall throughs */
|
|
|
|
HASH4;
|
|
|
|
case 7:
|
|
|
|
HASH4;
|
|
|
|
case 6:
|
|
|
|
HASH4;
|
|
|
|
case 5:
|
|
|
|
HASH4;
|
|
|
|
case 4:
|
|
|
|
HASH4;
|
|
|
|
case 3:
|
|
|
|
HASH4;
|
|
|
|
case 2:
|
|
|
|
HASH4;
|
|
|
|
case 1:
|
|
|
|
HASH4;
|
|
|
|
} while (--loop);
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
2000-06-05 09:29:25 +02:00
|
|
|
PG_RETURN_UINT32(h);
|
1997-09-07 07:04:48 +02:00
|
|
|
}
|
1996-07-09 08:22:35 +02:00
|
|
|
|
2000-06-05 09:29:25 +02:00
|
|
|
Datum
|
|
|
|
hashfloat8(PG_FUNCTION_ARGS)
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
2000-06-05 09:29:25 +02:00
|
|
|
float8 key = PG_GETARG_FLOAT8(0);
|
|
|
|
char *kp = (char *) &key;
|
|
|
|
int len = sizeof(key);
|
1997-09-08 04:41:22 +02:00
|
|
|
int loop;
|
|
|
|
uint32 h;
|
1996-07-09 08:22:35 +02:00
|
|
|
|
1997-09-07 07:04:48 +02:00
|
|
|
#define HASH4a h = (h << 5) - h + *kp++;
|
|
|
|
#define HASH4b h = (h << 5) + h + *kp++;
|
1996-07-09 08:22:35 +02:00
|
|
|
#define HASH4 HASH4b
|
|
|
|
|
1997-09-07 07:04:48 +02:00
|
|
|
h = 0;
|
2000-06-05 09:29:25 +02:00
|
|
|
/*
|
|
|
|
* This is a tad silly, given that we expect len = 8, but a smart
|
|
|
|
* compiler should be able to eliminate the redundant code...
|
|
|
|
*/
|
|
|
|
loop = (len + 8 - 1) >> 3;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
2000-06-05 09:29:25 +02:00
|
|
|
switch (len & (8 - 1))
|
|
|
|
{
|
|
|
|
case 0:
|
|
|
|
do
|
|
|
|
{ /* All fall throughs */
|
|
|
|
HASH4;
|
|
|
|
case 7:
|
|
|
|
HASH4;
|
|
|
|
case 6:
|
|
|
|
HASH4;
|
|
|
|
case 5:
|
|
|
|
HASH4;
|
|
|
|
case 4:
|
|
|
|
HASH4;
|
|
|
|
case 3:
|
|
|
|
HASH4;
|
|
|
|
case 2:
|
|
|
|
HASH4;
|
|
|
|
case 1:
|
|
|
|
HASH4;
|
|
|
|
} while (--loop);
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
2000-06-05 09:29:25 +02:00
|
|
|
PG_RETURN_UINT32(h);
|
1997-09-07 07:04:48 +02:00
|
|
|
}
|
1996-07-09 08:22:35 +02:00
|
|
|
|
2000-06-05 09:29:25 +02:00
|
|
|
Datum
|
|
|
|
hashoid(PG_FUNCTION_ARGS)
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
2000-06-05 09:29:25 +02:00
|
|
|
PG_RETURN_UINT32(~(uint32) PG_GETARG_OID(0));
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
|
|
|
|
2000-06-05 09:29:25 +02:00
|
|
|
Datum
|
|
|
|
hashoidvector(PG_FUNCTION_ARGS)
|
1998-08-19 04:04:17 +02:00
|
|
|
{
|
2000-06-05 09:29:25 +02:00
|
|
|
Oid *key = (Oid *) PG_GETARG_POINTER(0);
|
1998-09-01 06:40:42 +02:00
|
|
|
int i;
|
|
|
|
uint32 result = 0;
|
1998-08-19 04:04:17 +02:00
|
|
|
|
2000-04-12 19:17:23 +02:00
|
|
|
for (i = INDEX_MAX_KEYS; --i >= 0;)
|
2000-02-21 04:36:59 +01:00
|
|
|
result = (result << 1) ^ (~(uint32) key[i]);
|
2000-06-05 09:29:25 +02:00
|
|
|
PG_RETURN_UINT32(result);
|
2000-02-21 04:36:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Note: hashint2vector currently can't be used as a user hash table
|
2000-04-12 19:17:23 +02:00
|
|
|
* hash function, because it has no pg_proc entry. We only need it
|
2000-02-21 04:36:59 +01:00
|
|
|
* for catcache indexing.
|
|
|
|
*/
|
2000-06-05 09:29:25 +02:00
|
|
|
Datum
|
|
|
|
hashint2vector(PG_FUNCTION_ARGS)
|
2000-02-21 04:36:59 +01:00
|
|
|
{
|
2000-06-05 09:29:25 +02:00
|
|
|
int16 *key = (int16 *) PG_GETARG_POINTER(0);
|
2000-02-21 04:36:59 +01:00
|
|
|
int i;
|
|
|
|
uint32 result = 0;
|
|
|
|
|
2000-04-12 19:17:23 +02:00
|
|
|
for (i = INDEX_MAX_KEYS; --i >= 0;)
|
2000-02-21 04:36:59 +01:00
|
|
|
result = (result << 1) ^ (~(uint32) key[i]);
|
2000-06-05 09:29:25 +02:00
|
|
|
PG_RETURN_UINT32(result);
|
1998-08-19 04:04:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
1998-04-26 06:12:15 +02:00
|
|
|
#define PRIME1 37
|
|
|
|
#define PRIME2 1048583
|
1996-07-09 08:22:35 +02:00
|
|
|
|
2000-06-05 09:29:25 +02:00
|
|
|
Datum
|
|
|
|
hashchar(PG_FUNCTION_ARGS)
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
1997-09-08 04:41:22 +02:00
|
|
|
uint32 h;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
|
|
|
/* Convert char to integer */
|
2000-06-05 09:29:25 +02:00
|
|
|
h = (PG_GETARG_CHAR(0) - ' ');
|
1997-09-07 07:04:48 +02:00
|
|
|
h %= PRIME2;
|
1996-07-09 08:22:35 +02:00
|
|
|
|
2000-06-05 09:29:25 +02:00
|
|
|
PG_RETURN_UINT32(h);
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
|
|
|
|
2000-06-05 09:29:25 +02:00
|
|
|
Datum
|
|
|
|
hashname(PG_FUNCTION_ARGS)
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
2000-06-05 09:29:25 +02:00
|
|
|
char *key = NameStr(* PG_GETARG_NAME(0));
|
|
|
|
int len = NAMEDATALEN;
|
1997-09-08 04:41:22 +02:00
|
|
|
uint32 h;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
|
|
|
h = 0;
|
|
|
|
/* Convert string to integer */
|
|
|
|
while (len--)
|
|
|
|
h = h * PRIME1 ^ (*key++ - ' ');
|
|
|
|
h %= PRIME2;
|
|
|
|
|
2000-06-05 09:29:25 +02:00
|
|
|
PG_RETURN_UINT32(h);
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* (Comment from the original db3 hashing code: )
|
|
|
|
*
|
|
|
|
* "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.
|
|
|
|
*
|
|
|
|
* "OZ's original sdbm hash"
|
|
|
|
*/
|
2000-06-05 09:29:25 +02:00
|
|
|
Datum
|
|
|
|
hashtext(PG_FUNCTION_ARGS)
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
2000-06-05 09:29:25 +02:00
|
|
|
text *key = PG_GETARG_TEXT_P(0);
|
1997-09-08 04:41:22 +02:00
|
|
|
int keylen;
|
|
|
|
char *keydata;
|
|
|
|
uint32 n;
|
|
|
|
int loop;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
|
|
|
keydata = VARDATA(key);
|
2000-06-05 09:29:25 +02:00
|
|
|
keylen = VARSIZE(key) - VARHDRSZ;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
|
|
|
#define HASHC n = *keydata++ + 65599 * n
|
|
|
|
|
|
|
|
n = 0;
|
|
|
|
if (keylen > 0)
|
|
|
|
{
|
|
|
|
loop = (keylen + 8 - 1) >> 3;
|
|
|
|
|
|
|
|
switch (keylen & (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
|
|
|
}
|
2000-06-05 09:29:25 +02:00
|
|
|
PG_RETURN_UINT32(n);
|
1997-09-07 07:04:48 +02:00
|
|
|
}
|