perfhlib.h 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. /* Perfect hashing function library. Contains functions to generate perfect
  2. hashing functions
  3. * (C) Mike van Emmerik
  4. */
  5. #define TRUE 1
  6. #define FALSE 0
  7. //#define bool unsigned char
  8. #define byte unsigned char
  9. #define word unsigned short
  10. /* Prototypes */
  11. void hashCleanup(void); /* Frees memory allocated by hashParams() */
  12. void map(void); /* Part 1 of creating the tables */
  13. /* The application must provide these functions: */
  14. void getKey(int i, byte **pKeys);/* Set *keys to point to the i+1th key */
  15. void dispKey(int i); /* Display the key */
  16. class PatternHasher
  17. {
  18. word *T1base, *T2base; /* Pointers to start of T1, T2 */
  19. int NumEntry; /* Number of entries in the hash table (# keys) */
  20. int EntryLen; /* Size (bytes) of each entry (size of keys) */
  21. int SetSize; /* Size of the char set */
  22. char SetMin; /* First char in the set */
  23. int NumVert; /* c times NumEntry */
  24. int *graphNode; /* The array of edges */
  25. int *graphNext; /* Linked list of edges */
  26. int *graphFirst;/* First edge at a vertex */
  27. public:
  28. word *readT1(void); /* Returns a pointer to the T1 table */
  29. word *readT2(void); /* Returns a pointer to the T2 table */
  30. word *readG(void); /* Returns a pointer to the g table */
  31. void init(int _NumEntry, int _EntryLen, int _SetSize, char _SetMin,int _NumVert); /* Set the parameters for the hash table */
  32. void cleanup();
  33. int hash(unsigned char *string); //!< Hash the string to an int 0 .. NUMENTRY-1
  34. };
  35. extern PatternHasher g_pattern_hasher;
  36. /* Macro reads a LH word from the image regardless of host convention */
  37. #ifndef LH
  38. #define LH(p) ((int)((byte *)(p))[0] + ((int)((byte *)(p))[1] << 8))
  39. #endif