mirror of
https://github.com/postgres/postgres.git
synced 2026-04-22 14:47:29 -04:00
91 lines
2.3 KiB
C
91 lines
2.3 KiB
C
/*-------------------------------------------------------------------------
|
|
*
|
|
* hashfn.c
|
|
* Hash functions for use in dynahash.c hashtables
|
|
*
|
|
*
|
|
* Portions Copyright (c) 1996-2016, PostgreSQL Global Development Group
|
|
* Portions Copyright (c) 1994, Regents of the University of California
|
|
*
|
|
*
|
|
* IDENTIFICATION
|
|
* src/backend/utils/hash/hashfn.c
|
|
*
|
|
* 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().
|
|
*
|
|
*-------------------------------------------------------------------------
|
|
*/
|
|
#include "postgres.h"
|
|
|
|
#include "access/hash.h"
|
|
#include "utils/hsearch.h"
|
|
|
|
|
|
/*
|
|
* string_hash: hash function for keys that are NUL-terminated strings.
|
|
*
|
|
* NOTE: this is the default hash function if none is specified.
|
|
*/
|
|
uint32
|
|
string_hash(const void *key, Size keysize)
|
|
{
|
|
/*
|
|
* 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.
|
|
*/
|
|
Size s_len = strlen((const char *) key);
|
|
|
|
s_len = Min(s_len, keysize - 1);
|
|
return DatumGetUInt32(hash_any((const unsigned char *) key,
|
|
(int) s_len));
|
|
}
|
|
|
|
/*
|
|
* tag_hash: hash function for fixed-size tag values
|
|
*/
|
|
uint32
|
|
tag_hash(const void *key, Size keysize)
|
|
{
|
|
return DatumGetUInt32(hash_any((const unsigned char *) key,
|
|
(int) keysize));
|
|
}
|
|
|
|
/*
|
|
* uint32_hash: hash function for keys that are uint32 or int32
|
|
*
|
|
* (tag_hash works for this case too, but is slower)
|
|
*/
|
|
uint32
|
|
uint32_hash(const void *key, Size keysize)
|
|
{
|
|
Assert(keysize == sizeof(uint32));
|
|
return DatumGetUInt32(hash_uint32(*((const uint32 *) key)));
|
|
}
|
|
|
|
/*
|
|
* 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 *));
|
|
return bms_hash_value(*((const Bitmapset *const *) key));
|
|
}
|
|
|
|
/*
|
|
* bitmap_match: match function to use with bitmap_hash
|
|
*/
|
|
int
|
|
bitmap_match(const void *key1, const void *key2, Size keysize)
|
|
{
|
|
Assert(keysize == sizeof(Bitmapset *));
|
|
return !bms_equal(*((const Bitmapset *const *) key1),
|
|
*((const Bitmapset *const *) key2));
|
|
}
|