hashtab.h 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. /* classes: h_files */
  2. #ifndef SCM_HASHTAB_H
  3. #define SCM_HASHTAB_H
  4. /* Copyright (C) 1995,1996,1999,2000,2001, 2003, 2004, 2006, 2008, 2009, 2011 Free Software Foundation, Inc.
  5. *
  6. * This library is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public License
  8. * as published by the Free Software Foundation; either version 3 of
  9. * the License, or (at your option) any later version.
  10. *
  11. * This library is distributed in the hope that it will be useful, but
  12. * WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with this library; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  19. * 02110-1301 USA
  20. */
  21. #include "libguile/__scm.h"
  22. #define SCM_HASHTABLE_P(x) (SCM_HAS_TYP7 (x, scm_tc7_hashtable))
  23. #define SCM_VALIDATE_HASHTABLE(pos, arg) \
  24. SCM_MAKE_VALIDATE_MSG (pos, arg, HASHTABLE_P, "hash-table")
  25. #define SCM_HASHTABLE_VECTOR(h) SCM_CELL_OBJECT_1 (h)
  26. #define SCM_SET_HASHTABLE_VECTOR(x, v) SCM_SET_CELL_OBJECT_1 ((x), (v))
  27. #define SCM_HASHTABLE(x) ((scm_t_hashtable *) SCM_CELL_WORD_2 (x))
  28. #define SCM_HASHTABLE_N_ITEMS(x) (SCM_HASHTABLE (x)->n_items)
  29. #define SCM_SET_HASHTABLE_N_ITEMS(x, n) (SCM_HASHTABLE (x)->n_items = n)
  30. #define SCM_HASHTABLE_INCREMENT(x) (SCM_HASHTABLE_N_ITEMS(x)++)
  31. #define SCM_HASHTABLE_DECREMENT(x) (SCM_HASHTABLE_N_ITEMS(x)--)
  32. #define SCM_HASHTABLE_UPPER(x) (SCM_HASHTABLE (x)->upper)
  33. #define SCM_HASHTABLE_LOWER(x) (SCM_HASHTABLE (x)->lower)
  34. #define SCM_HASHTABLE_N_BUCKETS(h) \
  35. SCM_SIMPLE_VECTOR_LENGTH (SCM_HASHTABLE_VECTOR (h))
  36. #define SCM_HASHTABLE_BUCKET(h, i) \
  37. SCM_SIMPLE_VECTOR_REF (SCM_HASHTABLE_VECTOR (h), i)
  38. #define SCM_SET_HASHTABLE_BUCKET(h, i, x) \
  39. SCM_SIMPLE_VECTOR_SET (SCM_HASHTABLE_VECTOR (h), i, x)
  40. /* Function that computes a hash of OBJ modulo MAX. */
  41. typedef unsigned long (*scm_t_hash_fn) (SCM obj, unsigned long max,
  42. void *closure);
  43. /* Function that returns the value associated with OBJ in ALIST according to
  44. some equality predicate. */
  45. typedef SCM (*scm_t_assoc_fn) (SCM obj, SCM alist, void *closure);
  46. /* Function to fold over the entries of a hash table. */
  47. typedef SCM (*scm_t_hash_fold_fn) (void *closure, SCM key, SCM value,
  48. SCM result);
  49. /* Function to iterate over the handles (key-value pairs) of a hash
  50. table. */
  51. typedef SCM (*scm_t_hash_handle_fn) (void *closure, SCM handle);
  52. typedef struct scm_t_hashtable {
  53. unsigned long n_items; /* number of items in table */
  54. unsigned long lower; /* when to shrink */
  55. unsigned long upper; /* when to grow */
  56. int size_index; /* index into hashtable_size */
  57. int min_size_index; /* minimum size_index */
  58. scm_t_hash_fn hash_fn; /* for rehashing after a GC. */
  59. } scm_t_hashtable;
  60. SCM_API SCM scm_vector_to_hash_table (SCM vector);
  61. SCM_API SCM scm_c_make_hash_table (unsigned long k);
  62. SCM_API SCM scm_make_hash_table (SCM n);
  63. SCM_API SCM scm_hash_table_p (SCM h);
  64. SCM_INTERNAL void scm_i_rehash (SCM table, scm_t_hash_fn hash_fn,
  65. void *closure, const char *func_name);
  66. SCM_API SCM scm_hash_fn_get_handle (SCM table, SCM obj,
  67. scm_t_hash_fn hash_fn,
  68. scm_t_assoc_fn assoc_fn,
  69. void *closure);
  70. SCM_API SCM scm_hash_fn_create_handle_x (SCM table, SCM obj, SCM init,
  71. scm_t_hash_fn hash_fn,
  72. scm_t_assoc_fn assoc_fn,
  73. void *closure);
  74. SCM_API SCM scm_hash_fn_ref (SCM table, SCM obj, SCM dflt,
  75. scm_t_hash_fn hash_fn,
  76. scm_t_assoc_fn assoc_fn,
  77. void *closure);
  78. SCM_API SCM scm_hash_fn_set_x (SCM table, SCM obj, SCM val,
  79. scm_t_hash_fn hash_fn,
  80. scm_t_assoc_fn assoc_fn,
  81. void *closure);
  82. SCM_API SCM scm_hash_fn_remove_x (SCM table, SCM obj,
  83. scm_t_hash_fn hash_fn,
  84. scm_t_assoc_fn assoc_fn,
  85. void *closure);
  86. SCM_API SCM scm_internal_hash_fold (scm_t_hash_fold_fn fn, void *closure,
  87. SCM init, SCM table);
  88. SCM_API void scm_internal_hash_for_each_handle (scm_t_hash_handle_fn fn,
  89. void *closure, SCM table);
  90. SCM_API SCM scm_hash_clear_x (SCM table);
  91. SCM_API SCM scm_hashq_get_handle (SCM table, SCM obj);
  92. SCM_API SCM scm_hashq_create_handle_x (SCM table, SCM obj, SCM init);
  93. SCM_API SCM scm_hashq_ref (SCM table, SCM obj, SCM dflt);
  94. SCM_API SCM scm_hashq_set_x (SCM table, SCM obj, SCM val);
  95. SCM_API SCM scm_hashq_remove_x (SCM table, SCM obj);
  96. SCM_API SCM scm_hashv_get_handle (SCM table, SCM obj);
  97. SCM_API SCM scm_hashv_create_handle_x (SCM table, SCM obj, SCM init);
  98. SCM_API SCM scm_hashv_ref (SCM table, SCM obj, SCM dflt);
  99. SCM_API SCM scm_hashv_set_x (SCM table, SCM obj, SCM val);
  100. SCM_API SCM scm_hashv_remove_x (SCM table, SCM obj);
  101. SCM_API SCM scm_hash_get_handle (SCM table, SCM obj);
  102. SCM_API SCM scm_hash_create_handle_x (SCM table, SCM obj, SCM init);
  103. SCM_API SCM scm_hash_ref (SCM table, SCM obj, SCM dflt);
  104. SCM_API SCM scm_hash_set_x (SCM table, SCM obj, SCM val);
  105. SCM_API SCM scm_hash_remove_x (SCM table, SCM obj);
  106. SCM_API SCM scm_hashx_get_handle (SCM hash, SCM assoc, SCM table, SCM obj);
  107. SCM_API SCM scm_hashx_create_handle_x (SCM hash, SCM assoc, SCM table, SCM obj, SCM init);
  108. SCM_API SCM scm_hashx_ref (SCM hash, SCM assoc, SCM table, SCM obj, SCM dflt);
  109. SCM_API SCM scm_hashx_set_x (SCM hash, SCM assoc, SCM table, SCM obj, SCM val);
  110. SCM_API SCM scm_hashx_remove_x (SCM hash, SCM assoc, SCM table, SCM obj);
  111. SCM_API SCM scm_hash_fold (SCM proc, SCM init, SCM hash);
  112. SCM_API SCM scm_hash_for_each (SCM proc, SCM hash);
  113. SCM_API SCM scm_hash_for_each_handle (SCM proc, SCM hash);
  114. SCM_API SCM scm_hash_map_to_list (SCM proc, SCM hash);
  115. SCM_API SCM scm_hash_count (SCM hash, SCM pred);
  116. SCM_INTERNAL void scm_i_hashtable_print (SCM exp, SCM port, scm_print_state *pstate);
  117. SCM_INTERNAL void scm_init_hashtab (void);
  118. #endif /* SCM_HASHTAB_H */
  119. /*
  120. Local Variables:
  121. c-file-style: "gnu"
  122. End:
  123. */