source: trunk/symtab/pbgs_hash_functions.h @ 1653

Last change on this file since 1653 was 1653, checked in by vla24, 8 years ago

SymbolTable?: implemented auto-resize and custom hashing for different symbol length for division and identity length grouping

File size: 1.9 KB
Line 
1#ifndef HASH_FUNCTIONS_H
2#define HASH_FUNCTIONS_H
3
4//#define DEBUG_HASH_FUNCTION
5#ifdef DEBUG_HASH_FUNCTION
6#include <stdio.h>
7#endif
8#include "library_conversion.h"
9
10// T is hashvalue type
11// hashBitSize is the number of bits in hashvalue1 and hashvalue2
12// bucketBitSize is the number of bits of the returned value
13// PRE-CONDITION: hashBitSize < bucketBitSize
14template <class T>
15static unsigned int inline shortHashFunction(const T* hashvalue1, const T* hashvalue2, unsigned int hashBitSize, unsigned int bucketBitSize);
16
17// PRE-CONDITION: hashBitSize >= bucketBitSize
18template <class T>
19static unsigned int inline longHashFunction(const T* hashvalue1, const T* hashvalue2, unsigned int hashBitSize, unsigned int bucketBitSize);
20
21
22template <class T>
23inline unsigned int shortHashFunction(const T* hashvalue1, const T* hashvalue2, unsigned int hashBitSize, unsigned int bucketBitSize)
24{
25#ifdef DEBUG_HASH_FUNCTION
26    unsigned int shift = bucketBitSize - hashBitSize;
27    printf ("hashvalue1: %x | hashvalue2: %x | hashBitSize: %i | bucketBitSize: %i\n", *hashvalue1, *hashvalue2, hashBitSize, bucketBitSize);
28    printf ("bucketBitSize - hashBitSize: %i | hashvalue2 << %i = %x\n", shift, shift, (*hashvalue2 << shift));
29#endif
30    return *hashvalue1 ^ (*hashvalue2 << (bucketBitSize - hashBitSize));
31}
32
33template <class T>
34inline unsigned int longHashFunction(const T* hashvalue1, const T* hashvalue2, unsigned int hashBitSize, unsigned int bucketBitSize)
35{
36#ifdef DEBUG_HASH_FUNCTION
37    printf ("hashvalue1: %x | hashvalue2: %x | hashBitSize: %i | bucketBitSize: %i\n", *hashvalue1, *hashvalue2, hashBitSize, bucketBitSize);
38    printf ("hashBitSize - bucketBitSize: %i | hashvalue2 >> %i = %x\n", hashBitSize - bucketBitSize, hashBitSize - bucketBitSize, (*hashvalue2 >> hashBitSize - bucketBitSize));
39#endif
40    return *hashvalue1 ^ (*hashvalue2 >> (hashBitSize - bucketBitSize));
41}
42
43#endif // HASH_FUNCTIONS_H
Note: See TracBrowser for help on using the repository browser.