hashtab.h 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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 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
  8. * License as published by the Free Software Foundation; either
  9. * version 2.1 of the License, or (at your option) any later version.
  10. *
  11. * This library is distributed in the hope that it will be useful,
  12. * but 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 02110-1301 USA
  19. */
  20. #include "libguile/__scm.h"
  21. #include "weaks.h"
  22. #define SCM_HASHTABLEF_WEAK_CAR SCM_WVECTF_WEAK_KEY
  23. #define SCM_HASHTABLEF_WEAK_CDR SCM_WVECTF_WEAK_VALUE
  24. SCM_API scm_t_bits scm_tc16_hashtable;
  25. #define SCM_HASHTABLE_P(x) SCM_SMOB_PREDICATE (scm_tc16_hashtable, x)
  26. #define SCM_VALIDATE_HASHTABLE(pos, arg) \
  27. SCM_MAKE_VALIDATE_MSG (pos, arg, HASHTABLE_P, "hash-table")
  28. #define SCM_HASHTABLE_VECTOR(h) SCM_SMOB_OBJECT (h)
  29. #define SCM_SET_HASHTABLE_VECTOR(x, v) SCM_SET_SMOB_OBJECT ((x), (v))
  30. #define SCM_HASHTABLE(x) ((scm_t_hashtable *) SCM_SMOB_DATA_2 (x))
  31. #define SCM_HASHTABLE_NEXT(x) SCM_SMOB_OBJECT_3 (x)
  32. #define SCM_HASHTABLE_NEXTLOC(x) SCM_SMOB_OBJECT_3_LOC (x)
  33. #define SCM_SET_HASHTABLE_NEXT(x, n) SCM_SET_SMOB_OBJECT_3 ((x), (n))
  34. #define SCM_HASHTABLE_FLAGS(x) (SCM_HASHTABLE (x)->flags)
  35. #define SCM_HASHTABLE_WEAK_KEY_P(x) \
  36. (SCM_HASHTABLE_FLAGS (x) & SCM_HASHTABLEF_WEAK_CAR)
  37. #define SCM_HASHTABLE_WEAK_VALUE_P(x) \
  38. (SCM_HASHTABLE_FLAGS (x) & SCM_HASHTABLEF_WEAK_CDR)
  39. #define SCM_HASHTABLE_DOUBLY_WEAK_P(x) \
  40. ((SCM_HASHTABLE_FLAGS (x) \
  41. & (SCM_HASHTABLEF_WEAK_CAR | SCM_HASHTABLEF_WEAK_CDR)) \
  42. == (SCM_HASHTABLEF_WEAK_CAR | SCM_HASHTABLEF_WEAK_CDR))
  43. #define SCM_HASHTABLE_WEAK_P(x) SCM_HASHTABLE_FLAGS (x)
  44. #define SCM_HASHTABLE_N_ITEMS(x) (SCM_HASHTABLE (x)->n_items)
  45. #define SCM_SET_HASHTABLE_N_ITEMS(x, n) (SCM_HASHTABLE (x)->n_items = n)
  46. #define SCM_HASHTABLE_INCREMENT(x) (SCM_HASHTABLE_N_ITEMS(x)++)
  47. #define SCM_HASHTABLE_DECREMENT(x) (SCM_HASHTABLE_N_ITEMS(x)--)
  48. #define SCM_HASHTABLE_UPPER(x) (SCM_HASHTABLE (x)->upper)
  49. #define SCM_HASHTABLE_LOWER(x) (SCM_HASHTABLE (x)->lower)
  50. #define SCM_HASHTABLE_N_BUCKETS(h) \
  51. SCM_SIMPLE_VECTOR_LENGTH (SCM_HASHTABLE_VECTOR (h))
  52. #define SCM_HASHTABLE_BUCKET(h, i) \
  53. SCM_SIMPLE_VECTOR_REF (SCM_HASHTABLE_VECTOR (h), i)
  54. #define SCM_SET_HASHTABLE_BUCKET(h, i, x) \
  55. SCM_SIMPLE_VECTOR_SET (SCM_HASHTABLE_VECTOR (h), i, x)
  56. typedef struct scm_t_hashtable {
  57. int flags; /* properties of table */
  58. unsigned long n_items; /* number of items in table */
  59. unsigned long lower; /* when to shrink */
  60. unsigned long upper; /* when to grow */
  61. int size_index; /* index into hashtable_size */
  62. int min_size_index; /* minimum size_index */
  63. unsigned long (*hash_fn) (); /* for rehashing after a GC. */
  64. } scm_t_hashtable;
  65. #if 0
  66. typedef unsigned int scm_t_hash_fn (SCM obj, unsigned int d, void *closure);
  67. typedef SCM scm_t_assoc_fn (SCM key, SCM alist, void *closure);
  68. typedef SCM scm_t_delete_fn (SCM elt, SCM list);
  69. #endif
  70. SCM_API SCM scm_vector_to_hash_table (SCM vector);
  71. SCM_API SCM scm_c_make_hash_table (unsigned long k);
  72. SCM_API SCM scm_make_hash_table (SCM n);
  73. SCM_API SCM scm_make_weak_key_hash_table (SCM k);
  74. SCM_API SCM scm_make_weak_value_hash_table (SCM k);
  75. SCM_API SCM scm_make_doubly_weak_hash_table (SCM k);
  76. SCM_API SCM scm_hash_table_p (SCM h);
  77. SCM_API SCM scm_weak_key_hash_table_p (SCM h);
  78. SCM_API SCM scm_weak_value_hash_table_p (SCM h);
  79. SCM_API SCM scm_doubly_weak_hash_table_p (SCM h);
  80. SCM_API void scm_i_rehash (SCM table, unsigned long (*hash_fn)(), void *closure, const char*func_name);
  81. SCM_API void scm_i_scan_weak_hashtables (void);
  82. SCM_API SCM scm_hash_fn_get_handle (SCM table, SCM obj, unsigned long (*hash_fn) (), SCM (*assoc_fn) (), void * closure);
  83. SCM_API SCM scm_hash_fn_create_handle_x (SCM table, SCM obj, SCM init, unsigned long (*hash_fn) (), SCM (*assoc_fn) (), void * closure);
  84. SCM_API SCM scm_hash_fn_ref (SCM table, SCM obj, SCM dflt, unsigned long (*hash_fn) (), SCM (*assoc_fn) (), void * closure);
  85. SCM_API SCM scm_hash_fn_set_x (SCM table, SCM obj, SCM val, unsigned long (*hash_fn) (), SCM (*assoc_fn) (), void * closure);
  86. SCM_API SCM scm_hash_fn_remove_x (SCM table, SCM obj, unsigned long (*hash_fn) (), SCM (*assoc_fn) (), void * closure);
  87. SCM_API SCM scm_internal_hash_fold (SCM (*fn) (), void *closure, SCM init, SCM table);
  88. SCM_API void scm_internal_hash_for_each_handle (SCM (*fn) (), void *closure, SCM table);
  89. SCM_API SCM scm_hash_clear_x (SCM table);
  90. SCM_API SCM scm_hashq_get_handle (SCM table, SCM obj);
  91. SCM_API SCM scm_hashq_create_handle_x (SCM table, SCM obj, SCM init);
  92. SCM_API SCM scm_hashq_ref (SCM table, SCM obj, SCM dflt);
  93. SCM_API SCM scm_hashq_set_x (SCM table, SCM obj, SCM val);
  94. SCM_API SCM scm_hashq_remove_x (SCM table, SCM obj);
  95. SCM_API SCM scm_hashv_get_handle (SCM table, SCM obj);
  96. SCM_API SCM scm_hashv_create_handle_x (SCM table, SCM obj, SCM init);
  97. SCM_API SCM scm_hashv_ref (SCM table, SCM obj, SCM dflt);
  98. SCM_API SCM scm_hashv_set_x (SCM table, SCM obj, SCM val);
  99. SCM_API SCM scm_hashv_remove_x (SCM table, SCM obj);
  100. SCM_API SCM scm_hash_get_handle (SCM table, SCM obj);
  101. SCM_API SCM scm_hash_create_handle_x (SCM table, SCM obj, SCM init);
  102. SCM_API SCM scm_hash_ref (SCM table, SCM obj, SCM dflt);
  103. SCM_API SCM scm_hash_set_x (SCM table, SCM obj, SCM val);
  104. SCM_API SCM scm_hash_remove_x (SCM table, SCM obj);
  105. SCM_API SCM scm_hashx_get_handle (SCM hash, SCM assoc, SCM table, SCM obj);
  106. SCM_API SCM scm_hashx_create_handle_x (SCM hash, SCM assoc, SCM table, SCM obj, SCM init);
  107. SCM_API SCM scm_hashx_ref (SCM hash, SCM assoc, SCM table, SCM obj, SCM dflt);
  108. SCM_API SCM scm_hashx_set_x (SCM hash, SCM assoc, SCM table, SCM obj, SCM val);
  109. SCM_API SCM scm_hashx_remove_x (SCM hash, SCM assoc, SCM table, SCM obj);
  110. SCM_API SCM scm_hash_fold (SCM proc, SCM init, SCM hash);
  111. SCM_API SCM scm_hash_for_each (SCM proc, SCM hash);
  112. SCM_API SCM scm_hash_for_each_handle (SCM proc, SCM hash);
  113. SCM_API SCM scm_hash_map_to_list (SCM proc, SCM hash);
  114. SCM_API void scm_hashtab_prehistory (void);
  115. SCM_API void scm_init_hashtab (void);
  116. #endif /* SCM_HASHTAB_H */
  117. /*
  118. Local Variables:
  119. c-file-style: "gnu"
  120. End:
  121. */