hash.h 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. /* Hash tables for Objective C method dispatch.
  2. Copyright (C) 1993-2015 Free Software Foundation, Inc.
  3. This file is part of GCC.
  4. GCC is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 3, or (at your option)
  7. any later version.
  8. GCC is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. Under Section 7 of GPL version 3, you are granted additional
  13. permissions described in the GCC Runtime Library Exception, version
  14. 3.1, as published by the Free Software Foundation.
  15. You should have received a copy of the GNU General Public License and
  16. a copy of the GCC Runtime Library Exception along with this program;
  17. see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
  18. <http://www.gnu.org/licenses/>. */
  19. /* You need to include this file after including objc.h */
  20. #ifndef __hash_INCLUDE_GNU
  21. #define __hash_INCLUDE_GNU
  22. #include <stddef.h>
  23. #include <string.h>
  24. /*
  25. * This data structure is used to hold items
  26. * stored in a hash table. Each node holds
  27. * a key/value pair.
  28. *
  29. * Items in the cache are really of type void *.
  30. */
  31. typedef struct cache_node
  32. {
  33. struct cache_node *next; /* Pointer to next entry on the list.
  34. NULL indicates end of list. */
  35. const void *key; /* Key used to locate the value. Used
  36. to locate value when more than one
  37. key computes the same hash
  38. value. */
  39. void *value; /* Value stored for the key. */
  40. } *node_ptr;
  41. /*
  42. * This data type is the function that computes a hash code given a key.
  43. * Therefore, the key can be a pointer to anything and the function specific
  44. * to the key type.
  45. *
  46. * Unfortunately there is a mutual data structure reference problem with this
  47. * typedef. Therefore, to remove compiler warnings the functions passed to
  48. * objc_hash_new will have to be casted to this type.
  49. */
  50. typedef unsigned int (*hash_func_type) (void *, const void *);
  51. /*
  52. * This data type is the function that compares two hash keys and returns an
  53. * integer greater than, equal to, or less than 0, according as the first
  54. * parameter is lexicographically greater than, equal to, or less than the
  55. * second.
  56. */
  57. typedef int (*compare_func_type) (const void *, const void *);
  58. /*
  59. * This data structure is the cache.
  60. *
  61. * It must be passed to all of the hashing routines
  62. * (except for new).
  63. */
  64. typedef struct cache
  65. {
  66. /* Variables used to implement the hash itself. */
  67. node_ptr *node_table; /* Pointer to an array of hash nodes. */
  68. /* Variables used to track the size of the hash table so to determine
  69. when to resize it. */
  70. unsigned int size; /* Number of buckets allocated for the hash table
  71. (number of array entries allocated for
  72. "node_table"). Must be a power of two. */
  73. unsigned int used; /* Current number of entries in the hash table. */
  74. unsigned int mask; /* Precomputed mask. */
  75. /* Variables used to implement indexing through the hash table. */
  76. unsigned int last_bucket; /* Tracks which entry in the array where
  77. the last value was returned. */
  78. /* Function used to compute a hash code given a key.
  79. This function is specified when the hash table is created. */
  80. hash_func_type hash_func;
  81. /* Function used to compare two hash keys to see if they are equal. */
  82. compare_func_type compare_func;
  83. } *cache_ptr;
  84. /* Allocate and initialize a hash table. */
  85. cache_ptr objc_hash_new (unsigned int size,
  86. hash_func_type hash_func,
  87. compare_func_type compare_func);
  88. /* Deallocate all of the hash nodes and the cache itself. */
  89. void objc_hash_delete (cache_ptr cache);
  90. /* Add the key/value pair to the hash table. If the
  91. hash table reaches a level of fullness then it will be resized.
  92. assert if the key is already in the hash. */
  93. void objc_hash_add (cache_ptr *cachep, const void *key, void *value);
  94. /* Remove the key/value pair from the hash table.
  95. assert if the key isn't in the table. */
  96. void objc_hash_remove (cache_ptr cache, const void *key);
  97. /* Used to index through the hash table. Start with NULL
  98. to get the first entry.
  99. Successive calls pass the value returned previously.
  100. ** Don't modify the hash during this operation ***
  101. Cache nodes are returned such that key or value can
  102. be extracted. */
  103. node_ptr objc_hash_next (cache_ptr cache, node_ptr node);
  104. /* Used to return a value from a hash table using a given key. */
  105. void *objc_hash_value_for_key (cache_ptr cache, const void *key);
  106. /* Used to determine if the given key exists in the hash table */
  107. BOOL objc_hash_is_key_in_hash (cache_ptr cache, const void *key);
  108. /************************************************
  109. Useful hashing functions.
  110. Declared inline for your pleasure.
  111. ************************************************/
  112. /* Calculate a hash code by performing some
  113. manipulation of the key pointer. (Use the lowest bits
  114. except for those likely to be 0 due to alignment.) */
  115. static inline unsigned int
  116. objc_hash_ptr (cache_ptr cache, const void *key)
  117. {
  118. return ((size_t)key / sizeof (void *)) & cache->mask;
  119. }
  120. /* Calculate a hash code by iterating over a NULL
  121. terminate string. */
  122. static inline unsigned int
  123. objc_hash_string (cache_ptr cache, const void *key)
  124. {
  125. unsigned int ret = 0;
  126. unsigned int ctr = 0;
  127. const char *ckey = (const char *) key;
  128. while (*ckey) {
  129. ret ^= *ckey++ << ctr;
  130. ctr = (ctr + 1) % sizeof (void *);
  131. }
  132. return ret & cache->mask;
  133. }
  134. /* Compare two pointers for equality. */
  135. static inline int
  136. objc_compare_ptrs (const void *k1, const void *k2)
  137. {
  138. return (k1 == k2);
  139. }
  140. /* Compare two strings. */
  141. static inline int
  142. objc_compare_strings (const void *k1, const void *k2)
  143. {
  144. if (k1 == k2)
  145. return 1;
  146. else if (k1 == 0 || k2 == 0)
  147. return 0;
  148. else
  149. return ! strcmp ((const char *) k1, (const char *) k2);
  150. }
  151. #endif /* not __hash_INCLUDE_GNU */