Merge branch 'master' into boehm-demers-weiser-gc
[bpt/guile.git] / libguile / hashtab.h
1 /* classes: h_files */
2
3 #ifndef SCM_HASHTAB_H
4 #define SCM_HASHTAB_H
5
6 /* Copyright (C) 1995,1996,1999,2000,2001, 2003, 2004, 2006, 2008 Free Software Foundation, Inc.
7 *
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public License
10 * as published by the Free Software Foundation; either version 3 of
11 * the License, or (at your option) any later version.
12 *
13 * This library is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21 * 02110-1301 USA
22 */
23
24 \f
25
26 #include "libguile/__scm.h"
27
28 #include "weaks.h"
29
30 \f
31
32 #define SCM_HASHTABLEF_WEAK_CAR SCM_WVECTF_WEAK_KEY
33 #define SCM_HASHTABLEF_WEAK_CDR SCM_WVECTF_WEAK_VALUE
34
35 SCM_API scm_t_bits scm_tc16_hashtable;
36
37 #define SCM_HASHTABLE_P(x) SCM_SMOB_PREDICATE (scm_tc16_hashtable, x)
38 #define SCM_VALIDATE_HASHTABLE(pos, arg) \
39 SCM_MAKE_VALIDATE_MSG (pos, arg, HASHTABLE_P, "hash-table")
40 #define SCM_HASHTABLE_VECTOR(h) SCM_SMOB_OBJECT (h)
41 #define SCM_SET_HASHTABLE_VECTOR(x, v) SCM_SET_SMOB_OBJECT ((x), (v))
42 #define SCM_HASHTABLE(x) ((scm_t_hashtable *) SCM_SMOB_DATA_2 (x))
43 #define SCM_HASHTABLE_FLAGS(x) (SCM_HASHTABLE (x)->flags)
44 #define SCM_HASHTABLE_WEAK_KEY_P(x) \
45 (SCM_HASHTABLE_FLAGS (x) & SCM_HASHTABLEF_WEAK_CAR)
46 #define SCM_HASHTABLE_WEAK_VALUE_P(x) \
47 (SCM_HASHTABLE_FLAGS (x) & SCM_HASHTABLEF_WEAK_CDR)
48 #define SCM_HASHTABLE_DOUBLY_WEAK_P(x) \
49 ((SCM_HASHTABLE_FLAGS (x) \
50 & (SCM_HASHTABLEF_WEAK_CAR | SCM_HASHTABLEF_WEAK_CDR)) \
51 == (SCM_HASHTABLEF_WEAK_CAR | SCM_HASHTABLEF_WEAK_CDR))
52 #define SCM_HASHTABLE_WEAK_P(x) SCM_HASHTABLE_FLAGS (x)
53 #define SCM_HASHTABLE_N_ITEMS(x) (SCM_HASHTABLE (x)->n_items)
54 #define SCM_SET_HASHTABLE_N_ITEMS(x, n) (SCM_HASHTABLE (x)->n_items = n)
55 #define SCM_HASHTABLE_INCREMENT(x) (SCM_HASHTABLE_N_ITEMS(x)++)
56 #define SCM_HASHTABLE_DECREMENT(x) (SCM_HASHTABLE_N_ITEMS(x)--)
57 #define SCM_HASHTABLE_UPPER(x) (SCM_HASHTABLE (x)->upper)
58 #define SCM_HASHTABLE_LOWER(x) (SCM_HASHTABLE (x)->lower)
59
60 #define SCM_HASHTABLE_N_BUCKETS(h) \
61 SCM_SIMPLE_VECTOR_LENGTH (SCM_HASHTABLE_VECTOR (h))
62 #define SCM_HASHTABLE_BUCKET(h, i) \
63 SCM_SIMPLE_VECTOR_REF (SCM_HASHTABLE_VECTOR (h), i)
64 #define SCM_SET_HASHTABLE_BUCKET(h, i, x) \
65 SCM_SIMPLE_VECTOR_SET (SCM_HASHTABLE_VECTOR (h), i, x)
66
67 typedef struct scm_t_hashtable {
68 int flags; /* properties of table */
69 unsigned long n_items; /* number of items in table */
70 unsigned long lower; /* when to shrink */
71 unsigned long upper; /* when to grow */
72 int size_index; /* index into hashtable_size */
73 int min_size_index; /* minimum size_index */
74 unsigned long (*hash_fn) (); /* for rehashing after a GC. */
75 } scm_t_hashtable;
76
77 \f
78
79 #if 0
80 typedef unsigned int scm_t_hash_fn (SCM obj, unsigned int d, void *closure);
81 typedef SCM scm_t_assoc_fn (SCM key, SCM alist, void *closure);
82 typedef SCM scm_t_delete_fn (SCM elt, SCM list);
83 #endif
84
85 SCM_API SCM scm_vector_to_hash_table (SCM vector);
86 SCM_API SCM scm_c_make_hash_table (unsigned long k);
87 SCM_API SCM scm_make_hash_table (SCM n);
88 SCM_API SCM scm_make_weak_key_hash_table (SCM k);
89 SCM_API SCM scm_make_weak_value_hash_table (SCM k);
90 SCM_API SCM scm_make_doubly_weak_hash_table (SCM k);
91
92 SCM_API SCM scm_hash_table_p (SCM h);
93 SCM_API SCM scm_weak_key_hash_table_p (SCM h);
94 SCM_API SCM scm_weak_value_hash_table_p (SCM h);
95 SCM_API SCM scm_doubly_weak_hash_table_p (SCM h);
96
97 SCM_INTERNAL void scm_i_rehash (SCM table, unsigned long (*hash_fn)(),
98 void *closure, const char *func_name);
99
100 SCM_API SCM scm_hash_fn_get_handle (SCM table, SCM obj, unsigned long (*hash_fn) (), SCM (*assoc_fn) (), void * closure);
101 SCM_API SCM scm_hash_fn_create_handle_x (SCM table, SCM obj, SCM init, unsigned long (*hash_fn) (), SCM (*assoc_fn) (), void * closure);
102 SCM_API SCM scm_hash_fn_ref (SCM table, SCM obj, SCM dflt, unsigned long (*hash_fn) (), SCM (*assoc_fn) (), void * closure);
103 SCM_API SCM scm_hash_fn_set_x (SCM table, SCM obj, SCM val, unsigned long (*hash_fn) (), SCM (*assoc_fn) (), void * closure);
104 SCM_API SCM scm_hash_fn_remove_x (SCM table, SCM obj, unsigned long (*hash_fn) (), SCM (*assoc_fn) (), void * closure);
105 SCM_API SCM scm_internal_hash_fold (SCM (*fn) (), void *closure, SCM init, SCM table);
106 SCM_API void scm_internal_hash_for_each_handle (SCM (*fn) (), void *closure, SCM table);
107 SCM_API SCM scm_hash_clear_x (SCM table);
108
109 SCM_API SCM scm_hashq_get_handle (SCM table, SCM obj);
110 SCM_API SCM scm_hashq_create_handle_x (SCM table, SCM obj, SCM init);
111 SCM_API SCM scm_hashq_ref (SCM table, SCM obj, SCM dflt);
112 SCM_API SCM scm_hashq_set_x (SCM table, SCM obj, SCM val);
113 SCM_API SCM scm_hashq_remove_x (SCM table, SCM obj);
114 SCM_API SCM scm_hashv_get_handle (SCM table, SCM obj);
115 SCM_API SCM scm_hashv_create_handle_x (SCM table, SCM obj, SCM init);
116 SCM_API SCM scm_hashv_ref (SCM table, SCM obj, SCM dflt);
117 SCM_API SCM scm_hashv_set_x (SCM table, SCM obj, SCM val);
118 SCM_API SCM scm_hashv_remove_x (SCM table, SCM obj);
119 SCM_API SCM scm_hash_get_handle (SCM table, SCM obj);
120 SCM_API SCM scm_hash_create_handle_x (SCM table, SCM obj, SCM init);
121 SCM_API SCM scm_hash_ref (SCM table, SCM obj, SCM dflt);
122 SCM_API SCM scm_hash_set_x (SCM table, SCM obj, SCM val);
123 SCM_API SCM scm_hash_remove_x (SCM table, SCM obj);
124 SCM_API SCM scm_hashx_get_handle (SCM hash, SCM assoc, SCM table, SCM obj);
125 SCM_API SCM scm_hashx_create_handle_x (SCM hash, SCM assoc, SCM table, SCM obj, SCM init);
126 SCM_API SCM scm_hashx_ref (SCM hash, SCM assoc, SCM table, SCM obj, SCM dflt);
127 SCM_API SCM scm_hashx_set_x (SCM hash, SCM assoc, SCM table, SCM obj, SCM val);
128 SCM_API SCM scm_hashx_remove_x (SCM hash, SCM assoc, SCM table, SCM obj);
129 SCM_API SCM scm_hash_fold (SCM proc, SCM init, SCM hash);
130 SCM_API SCM scm_hash_for_each (SCM proc, SCM hash);
131 SCM_API SCM scm_hash_for_each_handle (SCM proc, SCM hash);
132 SCM_API SCM scm_hash_map_to_list (SCM proc, SCM hash);
133 SCM_INTERNAL void scm_hashtab_prehistory (void);
134 SCM_INTERNAL void scm_init_hashtab (void);
135
136 #endif /* SCM_HASHTAB_H */
137
138 /*
139 Local Variables:
140 c-file-style: "gnu"
141 End:
142 */