eval.c closures are now applicable smobs, not tc3s
[bpt/guile.git] / libguile / hashtab.h
CommitLineData
0f2d19dd
JB
1/* classes: h_files */
2
0527e687
DH
3#ifndef SCM_HASHTAB_H
4#define SCM_HASHTAB_H
5
d587c9e8 6/* Copyright (C) 1995,1996,1999,2000,2001, 2003, 2004, 2006, 2008, 2009 Free Software Foundation, Inc.
0527e687 7 *
73be1d9e 8 * This library is free software; you can redistribute it and/or
53befeb7
NJ
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.
0527e687 12 *
53befeb7
NJ
13 * This library is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
73be1d9e
MV
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
0527e687 17 *
73be1d9e
MV
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
53befeb7
NJ
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21 * 02110-1301 USA
73be1d9e 22 */
0527e687 23
0f2d19dd 24\f
0527e687 25
5e044c02 26#include "libguile/__scm.h"
0f2d19dd 27
c35738c1
MD
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
ecc9f40f 35SCM_API scm_t_bits scm_tc16_hashtable;
cdc5f676 36
f5710d53 37#define SCM_HASHTABLE_P(x) SCM_SMOB_PREDICATE (scm_tc16_hashtable, x)
c35738c1
MD
38#define SCM_VALIDATE_HASHTABLE(pos, arg) \
39 SCM_MAKE_VALIDATE_MSG (pos, arg, HASHTABLE_P, "hash-table")
f5710d53
MV
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))
c35738c1
MD
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) \
3ebc1832
MV
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)
c35738c1 64#define SCM_SET_HASHTABLE_BUCKET(h, i, x) \
3ebc1832 65 SCM_SIMPLE_VECTOR_SET (SCM_HASHTABLE_VECTOR (h), i, x)
c35738c1 66
d587c9e8
LC
67/* Function that computes a hash of OBJ modulo MAX. */
68typedef unsigned long (*scm_t_hash_fn) (SCM obj, unsigned long max,
69 void *closure);
70
71/* Function that returns the value associated with OBJ in ALIST according to
72 some equality predicate. */
73typedef SCM (*scm_t_assoc_fn) (SCM obj, SCM alist, void *closure);
74
a07010bf
LC
75/* Function to fold over the entries of a hash table. */
76typedef SCM (*scm_t_hash_fold_fn) (void *closure, SCM key, SCM value,
77 SCM result);
78
79/* Function to iterate over the handles (key-value pairs) of a hash
80 table. */
81typedef SCM (*scm_t_hash_handle_fn) (void *closure, SCM handle);
82
c35738c1
MD
83typedef struct scm_t_hashtable {
84 int flags; /* properties of table */
85 unsigned long n_items; /* number of items in table */
86 unsigned long lower; /* when to shrink */
87 unsigned long upper; /* when to grow */
88 int size_index; /* index into hashtable_size */
89 int min_size_index; /* minimum size_index */
d587c9e8 90 scm_t_hash_fn hash_fn; /* for rehashing after a GC. */
c35738c1
MD
91} scm_t_hashtable;
92
0f2d19dd 93\f
0f2d19dd 94
f59a096e 95SCM_API SCM scm_vector_to_hash_table (SCM vector);
33b001fd 96SCM_API SCM scm_c_make_hash_table (unsigned long k);
f59a096e 97SCM_API SCM scm_make_hash_table (SCM n);
c35738c1
MD
98SCM_API SCM scm_make_weak_key_hash_table (SCM k);
99SCM_API SCM scm_make_weak_value_hash_table (SCM k);
100SCM_API SCM scm_make_doubly_weak_hash_table (SCM k);
101
102SCM_API SCM scm_hash_table_p (SCM h);
103SCM_API SCM scm_weak_key_hash_table_p (SCM h);
104SCM_API SCM scm_weak_value_hash_table_p (SCM h);
105SCM_API SCM scm_doubly_weak_hash_table_p (SCM h);
106
d587c9e8 107SCM_INTERNAL void scm_i_rehash (SCM table, scm_t_hash_fn hash_fn,
102dbb6f 108 void *closure, const char *func_name);
00ffa0e7 109
d587c9e8
LC
110
111SCM_API SCM scm_hash_fn_get_handle (SCM table, SCM obj,
112 scm_t_hash_fn hash_fn,
113 scm_t_assoc_fn assoc_fn,
114 void *closure);
115SCM_API SCM scm_hash_fn_create_handle_x (SCM table, SCM obj, SCM init,
116 scm_t_hash_fn hash_fn,
117 scm_t_assoc_fn assoc_fn,
118 void *closure);
119SCM_API SCM scm_hash_fn_ref (SCM table, SCM obj, SCM dflt,
120 scm_t_hash_fn hash_fn,
121 scm_t_assoc_fn assoc_fn,
122 void *closure);
123SCM_API SCM scm_hash_fn_set_x (SCM table, SCM obj, SCM val,
124 scm_t_hash_fn hash_fn,
125 scm_t_assoc_fn assoc_fn,
126 void *closure);
127SCM_API SCM scm_hash_fn_remove_x (SCM table, SCM obj,
128 scm_t_hash_fn hash_fn,
129 scm_t_assoc_fn assoc_fn,
130 void *closure);
a07010bf
LC
131SCM_API SCM scm_internal_hash_fold (scm_t_hash_fold_fn fn, void *closure,
132 SCM init, SCM table);
133SCM_API void scm_internal_hash_for_each_handle (scm_t_hash_handle_fn fn,
134 void *closure, SCM table);
c35738c1 135SCM_API SCM scm_hash_clear_x (SCM table);
0f2d19dd 136
33b001fd
MV
137SCM_API SCM scm_hashq_get_handle (SCM table, SCM obj);
138SCM_API SCM scm_hashq_create_handle_x (SCM table, SCM obj, SCM init);
139SCM_API SCM scm_hashq_ref (SCM table, SCM obj, SCM dflt);
140SCM_API SCM scm_hashq_set_x (SCM table, SCM obj, SCM val);
141SCM_API SCM scm_hashq_remove_x (SCM table, SCM obj);
142SCM_API SCM scm_hashv_get_handle (SCM table, SCM obj);
143SCM_API SCM scm_hashv_create_handle_x (SCM table, SCM obj, SCM init);
144SCM_API SCM scm_hashv_ref (SCM table, SCM obj, SCM dflt);
145SCM_API SCM scm_hashv_set_x (SCM table, SCM obj, SCM val);
146SCM_API SCM scm_hashv_remove_x (SCM table, SCM obj);
147SCM_API SCM scm_hash_get_handle (SCM table, SCM obj);
148SCM_API SCM scm_hash_create_handle_x (SCM table, SCM obj, SCM init);
149SCM_API SCM scm_hash_ref (SCM table, SCM obj, SCM dflt);
150SCM_API SCM scm_hash_set_x (SCM table, SCM obj, SCM val);
151SCM_API SCM scm_hash_remove_x (SCM table, SCM obj);
152SCM_API SCM scm_hashx_get_handle (SCM hash, SCM assoc, SCM table, SCM obj);
153SCM_API SCM scm_hashx_create_handle_x (SCM hash, SCM assoc, SCM table, SCM obj, SCM init);
154SCM_API SCM scm_hashx_ref (SCM hash, SCM assoc, SCM table, SCM obj, SCM dflt);
155SCM_API SCM scm_hashx_set_x (SCM hash, SCM assoc, SCM table, SCM obj, SCM val);
a9cf5c71 156SCM_API SCM scm_hashx_remove_x (SCM hash, SCM assoc, SCM table, SCM obj);
33b001fd 157SCM_API SCM scm_hash_fold (SCM proc, SCM init, SCM hash);
c35738c1 158SCM_API SCM scm_hash_for_each (SCM proc, SCM hash);
711a9fd7
MD
159SCM_API SCM scm_hash_for_each_handle (SCM proc, SCM hash);
160SCM_API SCM scm_hash_map_to_list (SCM proc, SCM hash);
102dbb6f
LC
161SCM_INTERNAL void scm_hashtab_prehistory (void);
162SCM_INTERNAL void scm_init_hashtab (void);
0f2d19dd 163
0527e687 164#endif /* SCM_HASHTAB_H */
89e00824
ML
165
166/*
167 Local Variables:
168 c-file-style: "gnu"
169 End:
170*/