hashtab.h 7.1 KB

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