1996-07-09 08:22:35 +02:00
|
|
|
/*-------------------------------------------------------------------------
|
|
|
|
*
|
1999-02-14 00:22:53 +01:00
|
|
|
* hashfn.c
|
2001-10-01 07:36:17 +02:00
|
|
|
* Hash functions for use in dynahash.c hashtables
|
1997-09-07 07:04:48 +02:00
|
|
|
*
|
1996-07-09 08:22:35 +02:00
|
|
|
*
|
2012-01-02 00:01:58 +01:00
|
|
|
* Portions Copyright (c) 1996-2012, PostgreSQL Global Development Group
|
2000-01-26 06:58:53 +01:00
|
|
|
* Portions Copyright (c) 1994, Regents of the University of California
|
1996-07-09 08:22:35 +02:00
|
|
|
*
|
|
|
|
*
|
|
|
|
* IDENTIFICATION
|
2010-09-20 22:08:53 +02:00
|
|
|
* src/backend/utils/hash/hashfn.c
|
2007-06-01 17:33:19 +02:00
|
|
|
*
|
|
|
|
* NOTES
|
|
|
|
* It is expected that every bit of a hash function's 32-bit result is
|
|
|
|
* as random as every other; failure to ensure this is likely to lead
|
|
|
|
* to poor performance of hash tables. In most cases a hash
|
|
|
|
* function should use hash_any() or its variant hash_uint32().
|
1996-07-09 08:22:35 +02:00
|
|
|
*
|
|
|
|
*-------------------------------------------------------------------------
|
|
|
|
*/
|
1996-11-03 07:54:38 +01:00
|
|
|
#include "postgres.h"
|
|
|
|
|
2002-03-09 18:35:37 +01:00
|
|
|
#include "access/hash.h"
|
1996-07-09 08:22:35 +02:00
|
|
|
|
2002-03-09 18:35:37 +01:00
|
|
|
|
1996-07-09 08:22:35 +02:00
|
|
|
/*
|
2001-10-01 07:36:17 +02:00
|
|
|
* string_hash: hash function for keys that are null-terminated strings.
|
|
|
|
*
|
|
|
|
* NOTE: this is the default hash function if none is specified.
|
1996-07-09 08:22:35 +02:00
|
|
|
*/
|
2002-03-09 18:35:37 +01:00
|
|
|
uint32
|
2003-08-19 03:13:41 +02:00
|
|
|
string_hash(const void *key, Size keysize)
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
2006-09-27 20:40:10 +02:00
|
|
|
/*
|
|
|
|
* If the string exceeds keysize-1 bytes, we want to hash only that many,
|
|
|
|
* because when it is copied into the hash table it will be truncated at
|
|
|
|
* that length.
|
|
|
|
*/
|
2006-10-04 02:30:14 +02:00
|
|
|
Size s_len = strlen((const char *) key);
|
2006-09-27 20:40:10 +02:00
|
|
|
|
2006-10-04 02:30:14 +02:00
|
|
|
s_len = Min(s_len, keysize - 1);
|
2003-08-19 03:13:41 +02:00
|
|
|
return DatumGetUInt32(hash_any((const unsigned char *) key,
|
2006-09-27 20:40:10 +02:00
|
|
|
(int) s_len));
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
|
|
|
|
2001-10-01 07:36:17 +02:00
|
|
|
/*
|
|
|
|
* tag_hash: hash function for fixed-size tag values
|
|
|
|
*/
|
2002-03-09 18:35:37 +01:00
|
|
|
uint32
|
2003-08-19 03:13:41 +02:00
|
|
|
tag_hash(const void *key, Size keysize)
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
2003-08-19 03:13:41 +02:00
|
|
|
return DatumGetUInt32(hash_any((const unsigned char *) key,
|
|
|
|
(int) keysize));
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
2005-04-14 22:32:43 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* oid_hash: hash function for keys that are OIDs
|
|
|
|
*
|
|
|
|
* (tag_hash works for this case too, but is slower)
|
|
|
|
*/
|
|
|
|
uint32
|
|
|
|
oid_hash(const void *key, Size keysize)
|
|
|
|
{
|
|
|
|
Assert(keysize == sizeof(Oid));
|
2007-06-01 17:33:19 +02:00
|
|
|
return DatumGetUInt32(hash_uint32((uint32) *((const Oid *) key)));
|
2005-04-14 22:32:43 +02:00
|
|
|
}
|
2005-06-09 01:02:05 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* bitmap_hash: hash function for keys that are (pointers to) Bitmapsets
|
|
|
|
*
|
|
|
|
* Note: don't forget to specify bitmap_match as the match function!
|
|
|
|
*/
|
|
|
|
uint32
|
|
|
|
bitmap_hash(const void *key, Size keysize)
|
|
|
|
{
|
|
|
|
Assert(keysize == sizeof(Bitmapset *));
|
2005-10-15 04:49:52 +02:00
|
|
|
return bms_hash_value(*((const Bitmapset *const *) key));
|
2005-06-09 01:02:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* bitmap_match: match function to use with bitmap_hash
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
bitmap_match(const void *key1, const void *key2, Size keysize)
|
|
|
|
{
|
|
|
|
Assert(keysize == sizeof(Bitmapset *));
|
2005-10-15 04:49:52 +02:00
|
|
|
return !bms_equal(*((const Bitmapset *const *) key1),
|
|
|
|
*((const Bitmapset *const *) key2));
|
2005-06-09 01:02:05 +02:00
|
|
|
}
|