hashtab.h 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. /* An expandable hash tables datatype.
  2. Copyright (C) 1999-2019 Free Software Foundation, Inc.
  3. Contributed by Vladimir Makarov (vmakarov@cygnus.com).
  4. This program 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 2 of the License, or
  7. (at your option) any later version.
  8. This program 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. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA. */
  15. /* This package implements basic hash table functionality. It is possible
  16. to search for an entry, create an entry and destroy an entry.
  17. Elements in the table are generic pointers.
  18. The size of the table is not fixed; if the occupancy of the table
  19. grows too high the hash table will be expanded.
  20. The abstract data implementation is based on generalized Algorithm D
  21. from Knuth's book "The art of computer programming". Hash table is
  22. expanded by creation of new hash table and transferring elements from
  23. the old table to the new table. */
  24. #ifndef __HASHTAB_H__
  25. #define __HASHTAB_H__
  26. #ifdef __cplusplus
  27. extern "C" {
  28. #endif /* __cplusplus */
  29. #include "ansidecl.h"
  30. /* The type for a hash code. */
  31. typedef unsigned int hashval_t;
  32. /* Callback function pointer types. */
  33. /* Calculate hash of a table entry. */
  34. typedef hashval_t (*htab_hash) (const void *);
  35. /* Compare a table entry with a possible entry. The entry already in
  36. the table always comes first, so the second element can be of a
  37. different type (but in this case htab_find and htab_find_slot
  38. cannot be used; instead the variants that accept a hash value
  39. must be used). */
  40. typedef int (*htab_eq) (const void *, const void *);
  41. /* Cleanup function called whenever a live element is removed from
  42. the hash table. */
  43. typedef void (*htab_del) (void *);
  44. /* Function called by htab_traverse for each live element. The first
  45. arg is the slot of the element (which can be passed to htab_clear_slot
  46. if desired), the second arg is the auxiliary pointer handed to
  47. htab_traverse. Return 1 to continue scan, 0 to stop. */
  48. typedef int (*htab_trav) (void **, void *);
  49. /* Memory-allocation function, with the same functionality as calloc().
  50. Iff it returns NULL, the hash table implementation will pass an error
  51. code back to the user, so if your code doesn't handle errors,
  52. best if you use xcalloc instead. */
  53. typedef void *(*htab_alloc) (size_t, size_t);
  54. /* We also need a free() routine. */
  55. typedef void (*htab_free) (void *);
  56. /* Memory allocation and deallocation; variants which take an extra
  57. argument. */
  58. typedef void *(*htab_alloc_with_arg) (void *, size_t, size_t);
  59. typedef void (*htab_free_with_arg) (void *, void *);
  60. /* This macro defines reserved value for empty table entry. */
  61. #define HTAB_EMPTY_ENTRY ((PTR) 0)
  62. /* This macro defines reserved value for table entry which contained
  63. a deleted element. */
  64. #define HTAB_DELETED_ENTRY ((PTR) 1)
  65. /* Hash tables are of the following type. The structure
  66. (implementation) of this type is not needed for using the hash
  67. tables. All work with hash table should be executed only through
  68. functions mentioned below. The size of this structure is subject to
  69. change. */
  70. struct htab {
  71. /* Pointer to hash function. */
  72. htab_hash hash_f;
  73. /* Pointer to comparison function. */
  74. htab_eq eq_f;
  75. /* Pointer to cleanup function. */
  76. htab_del del_f;
  77. /* Table itself. */
  78. void **entries;
  79. /* Current size (in entries) of the hash table. */
  80. size_t size;
  81. /* Current number of elements including also deleted elements. */
  82. size_t n_elements;
  83. /* Current number of deleted elements in the table. */
  84. size_t n_deleted;
  85. /* The following member is used for debugging. Its value is number
  86. of all calls of `htab_find_slot' for the hash table. */
  87. unsigned int searches;
  88. /* The following member is used for debugging. Its value is number
  89. of collisions fixed for time of work with the hash table. */
  90. unsigned int collisions;
  91. /* Pointers to allocate/free functions. */
  92. htab_alloc alloc_f;
  93. htab_free free_f;
  94. /* Alternate allocate/free functions, which take an extra argument. */
  95. void *alloc_arg;
  96. htab_alloc_with_arg alloc_with_arg_f;
  97. htab_free_with_arg free_with_arg_f;
  98. /* Current size (in entries) of the hash table, as an index into the
  99. table of primes. */
  100. unsigned int size_prime_index;
  101. };
  102. typedef struct htab *htab_t;
  103. /* An enum saying whether we insert into the hash table or not. */
  104. enum insert_option {NO_INSERT, INSERT};
  105. /* The prototypes of the package functions. */
  106. extern htab_t htab_create_alloc (size_t, htab_hash,
  107. htab_eq, htab_del,
  108. htab_alloc, htab_free);
  109. extern htab_t htab_create_alloc_ex (size_t, htab_hash,
  110. htab_eq, htab_del,
  111. void *, htab_alloc_with_arg,
  112. htab_free_with_arg);
  113. extern htab_t htab_create_typed_alloc (size_t, htab_hash, htab_eq, htab_del,
  114. htab_alloc, htab_alloc, htab_free);
  115. /* Backward-compatibility functions. */
  116. extern htab_t htab_create (size_t, htab_hash, htab_eq, htab_del);
  117. extern htab_t htab_try_create (size_t, htab_hash, htab_eq, htab_del);
  118. extern void htab_set_functions_ex (htab_t, htab_hash,
  119. htab_eq, htab_del,
  120. void *, htab_alloc_with_arg,
  121. htab_free_with_arg);
  122. extern void htab_delete (htab_t);
  123. extern void htab_empty (htab_t);
  124. extern void * htab_find (htab_t, const void *);
  125. extern void ** htab_find_slot (htab_t, const void *, enum insert_option);
  126. extern void * htab_find_with_hash (htab_t, const void *, hashval_t);
  127. extern void ** htab_find_slot_with_hash (htab_t, const void *,
  128. hashval_t, enum insert_option);
  129. extern void htab_clear_slot (htab_t, void **);
  130. extern void htab_remove_elt (htab_t, void *);
  131. extern void htab_remove_elt_with_hash (htab_t, void *, hashval_t);
  132. extern void htab_traverse (htab_t, htab_trav, void *);
  133. extern void htab_traverse_noresize (htab_t, htab_trav, void *);
  134. extern size_t htab_size (htab_t);
  135. extern size_t htab_elements (htab_t);
  136. extern double htab_collisions (htab_t);
  137. /* A hash function for pointers. */
  138. extern htab_hash htab_hash_pointer;
  139. /* An equality function for pointers. */
  140. extern htab_eq htab_eq_pointer;
  141. /* A hash function for null-terminated strings. */
  142. extern hashval_t htab_hash_string (const void *);
  143. /* An iterative hash function for arbitrary data. */
  144. extern hashval_t iterative_hash (const void *, size_t, hashval_t);
  145. /* Shorthand for hashing something with an intrinsic size. */
  146. #define iterative_hash_object(OB,INIT) iterative_hash (&OB, sizeof (OB), INIT)
  147. #ifdef __cplusplus
  148. }
  149. #endif /* __cplusplus */
  150. #endif /* __HASHTAB_H */