gif_hash.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. /*****************************************************************************
  2. gif_hash.c -- module to support the following operations:
  3. 1. InitHashTable - initialize hash table.
  4. 2. ClearHashTable - clear the hash table to an empty state.
  5. 2. InsertHashTable - insert one item into data structure.
  6. 3. ExistsHashTable - test if item exists in data structure.
  7. This module is used to hash the GIF codes during encoding.
  8. *****************************************************************************/
  9. //#include <unistd.h>
  10. #include <stdint.h>
  11. #include <stdlib.h>
  12. #include <fcntl.h>
  13. #include <stdio.h>
  14. #include <string.h>
  15. #include "gif_lib.h"
  16. #include "gif_hash.h"
  17. #include "gif_lib_private.h"
  18. /* #define DEBUG_HIT_RATE Debug number of misses per hash Insert/Exists. */
  19. #ifdef DEBUG_HIT_RATE
  20. static long NumberOfTests = 0,
  21. NumberOfMisses = 0;
  22. #endif /* DEBUG_HIT_RATE */
  23. static int KeyItem(uint32_t Item);
  24. /******************************************************************************
  25. Initialize HashTable - allocate the memory needed and clear it. *
  26. ******************************************************************************/
  27. GifHashTableType *_InitHashTable(void)
  28. {
  29. GifHashTableType *HashTable;
  30. if ((HashTable = (GifHashTableType *) malloc(sizeof(GifHashTableType)))
  31. == NULL)
  32. return NULL;
  33. _ClearHashTable(HashTable);
  34. return HashTable;
  35. }
  36. /******************************************************************************
  37. Routine to clear the HashTable to an empty state. *
  38. This part is a little machine depended. Use the commented part otherwise. *
  39. ******************************************************************************/
  40. void _ClearHashTable(GifHashTableType *HashTable)
  41. {
  42. memset(HashTable -> HTable, 0xFF, HT_SIZE * sizeof(uint32_t));
  43. }
  44. /******************************************************************************
  45. Routine to insert a new Item into the HashTable. The data is assumed to be *
  46. new one. *
  47. ******************************************************************************/
  48. void _InsertHashTable(GifHashTableType *HashTable, uint32_t Key, int Code)
  49. {
  50. int HKey = KeyItem(Key);
  51. uint32_t *HTable = HashTable -> HTable;
  52. #ifdef DEBUG_HIT_RATE
  53. NumberOfTests++;
  54. NumberOfMisses++;
  55. #endif /* DEBUG_HIT_RATE */
  56. while (HT_GET_KEY(HTable[HKey]) != 0xFFFFFL) {
  57. #ifdef DEBUG_HIT_RATE
  58. NumberOfMisses++;
  59. #endif /* DEBUG_HIT_RATE */
  60. HKey = (HKey + 1) & HT_KEY_MASK;
  61. }
  62. HTable[HKey] = HT_PUT_KEY(Key) | HT_PUT_CODE(Code);
  63. }
  64. /******************************************************************************
  65. Routine to test if given Key exists in HashTable and if so returns its code *
  66. Returns the Code if key was found, -1 if not. *
  67. ******************************************************************************/
  68. int _ExistsHashTable(GifHashTableType *HashTable, uint32_t Key)
  69. {
  70. int HKey = KeyItem(Key);
  71. uint32_t *HTable = HashTable -> HTable, HTKey;
  72. #ifdef DEBUG_HIT_RATE
  73. NumberOfTests++;
  74. NumberOfMisses++;
  75. #endif /* DEBUG_HIT_RATE */
  76. while ((HTKey = HT_GET_KEY(HTable[HKey])) != 0xFFFFFL) {
  77. #ifdef DEBUG_HIT_RATE
  78. NumberOfMisses++;
  79. #endif /* DEBUG_HIT_RATE */
  80. if (Key == HTKey) return HT_GET_CODE(HTable[HKey]);
  81. HKey = (HKey + 1) & HT_KEY_MASK;
  82. }
  83. return -1;
  84. }
  85. /******************************************************************************
  86. Routine to generate an HKey for the hashtable out of the given unique key. *
  87. The given Key is assumed to be 20 bits as follows: lower 8 bits are the *
  88. new postfix character, while the upper 12 bits are the prefix code. *
  89. Because the average hit ratio is only 2 (2 hash references per entry), *
  90. evaluating more complex keys (such as twin prime keys) does not worth it! *
  91. ******************************************************************************/
  92. static int KeyItem(uint32_t Item)
  93. {
  94. return ((Item >> 12) ^ Item) & HT_KEY_MASK;
  95. }
  96. #ifdef DEBUG_HIT_RATE
  97. /******************************************************************************
  98. Debugging routine to print the hit ratio - number of times the hash table *
  99. was tested per operation. This routine was used to test the KeyItem routine *
  100. ******************************************************************************/
  101. void HashTablePrintHitRatio(void)
  102. {
  103. printf("Hash Table Hit Ratio is %ld/%ld = %ld%%.\n",
  104. NumberOfMisses, NumberOfTests,
  105. NumberOfMisses * 100 / NumberOfTests);
  106. }
  107. #endif /* DEBUG_HIT_RATE */
  108. /* end */