hash.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. /* hash - hashing table processing.
  2. Copyright (C) 1998-1999, 2001, 2003, 2009-2013 Free Software Foundation,
  3. Inc.
  4. Written by Jim Meyering <meyering@ascend.com>, 1998.
  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 3 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, see <http://www.gnu.org/licenses/>. */
  15. /* A generic hash table package. */
  16. /* Make sure USE_OBSTACK is defined to 1 if you want the allocator to use
  17. obstacks instead of malloc, and recompile 'hash.c' with same setting. */
  18. #ifndef HASH_H_
  19. # define HASH_H_
  20. # include <stdio.h>
  21. # include <stdbool.h>
  22. /* The __attribute__ feature is available in gcc versions 2.5 and later.
  23. The warn_unused_result attribute appeared first in gcc-3.4.0. */
  24. # if __GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ >= 4)
  25. # define _GL_ATTRIBUTE_WUR __attribute__ ((__warn_unused_result__))
  26. # else
  27. # define _GL_ATTRIBUTE_WUR /* empty */
  28. # endif
  29. # ifndef _GL_ATTRIBUTE_DEPRECATED
  30. /* The __attribute__((__deprecated__)) feature
  31. is available in gcc versions 3.1 and newer. */
  32. # if __GNUC__ < 3 || (__GNUC__ == 3 && __GNUC_MINOR__ < 1)
  33. # define _GL_ATTRIBUTE_DEPRECATED /* empty */
  34. # else
  35. # define _GL_ATTRIBUTE_DEPRECATED __attribute__ ((__deprecated__))
  36. # endif
  37. # endif
  38. typedef size_t (*Hash_hasher) (const void *, size_t);
  39. typedef bool (*Hash_comparator) (const void *, const void *);
  40. typedef void (*Hash_data_freer) (void *);
  41. typedef bool (*Hash_processor) (void *, void *);
  42. struct hash_tuning
  43. {
  44. /* This structure is mainly used for 'hash_initialize', see the block
  45. documentation of 'hash_reset_tuning' for more complete comments. */
  46. float shrink_threshold; /* ratio of used buckets to trigger a shrink */
  47. float shrink_factor; /* ratio of new smaller size to original size */
  48. float growth_threshold; /* ratio of used buckets to trigger a growth */
  49. float growth_factor; /* ratio of new bigger size to original size */
  50. bool is_n_buckets; /* if CANDIDATE really means table size */
  51. };
  52. typedef struct hash_tuning Hash_tuning;
  53. struct hash_table;
  54. typedef struct hash_table Hash_table;
  55. /* Information and lookup. */
  56. size_t hash_get_n_buckets (const Hash_table *) _GL_ATTRIBUTE_PURE;
  57. size_t hash_get_n_buckets_used (const Hash_table *) _GL_ATTRIBUTE_PURE;
  58. size_t hash_get_n_entries (const Hash_table *) _GL_ATTRIBUTE_PURE;
  59. size_t hash_get_max_bucket_length (const Hash_table *) _GL_ATTRIBUTE_PURE;
  60. bool hash_table_ok (const Hash_table *) _GL_ATTRIBUTE_PURE;
  61. void hash_print_statistics (const Hash_table *, FILE *);
  62. void *hash_lookup (const Hash_table *, const void *);
  63. /* Walking. */
  64. void *hash_get_first (const Hash_table *) _GL_ATTRIBUTE_PURE;
  65. void *hash_get_next (const Hash_table *, const void *);
  66. size_t hash_get_entries (const Hash_table *, void **, size_t);
  67. size_t hash_do_for_each (const Hash_table *, Hash_processor, void *);
  68. /* Allocation and clean-up. */
  69. size_t hash_string (const char *, size_t) _GL_ATTRIBUTE_PURE;
  70. void hash_reset_tuning (Hash_tuning *);
  71. Hash_table *hash_initialize (size_t, const Hash_tuning *,
  72. Hash_hasher, Hash_comparator,
  73. Hash_data_freer) _GL_ATTRIBUTE_WUR;
  74. void hash_clear (Hash_table *);
  75. void hash_free (Hash_table *);
  76. /* Insertion and deletion. */
  77. bool hash_rehash (Hash_table *, size_t) _GL_ATTRIBUTE_WUR;
  78. void *hash_insert (Hash_table *, const void *) _GL_ATTRIBUTE_WUR;
  79. /* Deprecate this interface. It has been renamed to hash_insert_if_absent. */
  80. int hash_insert0 (Hash_table *table, /* FIXME: remove in 2013 */
  81. const void *entry,
  82. const void **matched_ent) _GL_ATTRIBUTE_DEPRECATED;
  83. int hash_insert_if_absent (Hash_table *table, const void *entry,
  84. const void **matched_ent);
  85. void *hash_delete (Hash_table *, const void *);
  86. #endif