Header And Logo

PostgreSQL
| The world's most advanced open source database.

hashfn.c

Go to the documentation of this file.
00001 /*-------------------------------------------------------------------------
00002  *
00003  * hashfn.c
00004  *      Hash functions for use in dynahash.c hashtables
00005  *
00006  *
00007  * Portions Copyright (c) 1996-2013, PostgreSQL Global Development Group
00008  * Portions Copyright (c) 1994, Regents of the University of California
00009  *
00010  *
00011  * IDENTIFICATION
00012  *    src/backend/utils/hash/hashfn.c
00013  *
00014  * NOTES
00015  *    It is expected that every bit of a hash function's 32-bit result is
00016  *    as random as every other; failure to ensure this is likely to lead
00017  *    to poor performance of hash tables.  In most cases a hash
00018  *    function should use hash_any() or its variant hash_uint32().
00019  *
00020  *-------------------------------------------------------------------------
00021  */
00022 #include "postgres.h"
00023 
00024 #include "access/hash.h"
00025 
00026 
00027 /*
00028  * string_hash: hash function for keys that are NUL-terminated strings.
00029  *
00030  * NOTE: this is the default hash function if none is specified.
00031  */
00032 uint32
00033 string_hash(const void *key, Size keysize)
00034 {
00035     /*
00036      * If the string exceeds keysize-1 bytes, we want to hash only that many,
00037      * because when it is copied into the hash table it will be truncated at
00038      * that length.
00039      */
00040     Size        s_len = strlen((const char *) key);
00041 
00042     s_len = Min(s_len, keysize - 1);
00043     return DatumGetUInt32(hash_any((const unsigned char *) key,
00044                                    (int) s_len));
00045 }
00046 
00047 /*
00048  * tag_hash: hash function for fixed-size tag values
00049  */
00050 uint32
00051 tag_hash(const void *key, Size keysize)
00052 {
00053     return DatumGetUInt32(hash_any((const unsigned char *) key,
00054                                    (int) keysize));
00055 }
00056 
00057 /*
00058  * oid_hash: hash function for keys that are OIDs
00059  *
00060  * (tag_hash works for this case too, but is slower)
00061  */
00062 uint32
00063 oid_hash(const void *key, Size keysize)
00064 {
00065     Assert(keysize == sizeof(Oid));
00066     return DatumGetUInt32(hash_uint32((uint32) *((const Oid *) key)));
00067 }
00068 
00069 /*
00070  * bitmap_hash: hash function for keys that are (pointers to) Bitmapsets
00071  *
00072  * Note: don't forget to specify bitmap_match as the match function!
00073  */
00074 uint32
00075 bitmap_hash(const void *key, Size keysize)
00076 {
00077     Assert(keysize == sizeof(Bitmapset *));
00078     return bms_hash_value(*((const Bitmapset *const *) key));
00079 }
00080 
00081 /*
00082  * bitmap_match: match function to use with bitmap_hash
00083  */
00084 int
00085 bitmap_match(const void *key1, const void *key2, Size keysize)
00086 {
00087     Assert(keysize == sizeof(Bitmapset *));
00088     return !bms_equal(*((const Bitmapset *const *) key1),
00089                       *((const Bitmapset *const *) key2));
00090 }