perfhlib.h 1.8 KB

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