* Makefile.am (DEFS): Added. automake adds -I options to DEFS,
[bpt/guile.git] / libguile / weaks.c
1 /* Copyright (C) 1995,1996,1998 Free Software Foundation, Inc.
2 *
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2, or (at your option)
6 * any later version.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this software; see the file COPYING. If not, write to
15 * the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
16 * Boston, MA 02111-1307 USA
17 *
18 * As a special exception, Free Software Foundation gives permission
19 * for additional uses of the text contained in its release of this library.
20 *
21 * The exception is that, if you link this library with other files
22 * to produce an executable, this does not by itself cause the
23 * resulting executable to be covered by the GNU General Public License.
24 * Your use of that executable is in no way restricted on account of
25 * linking this library code into it.
26 *
27 * This exception does not however invalidate any other reasons why
28 * the executable file might be covered by the GNU General Public License.
29 *
30 * This exception applies only to the code released by
31 * Free Software Foundation as part of this library. If you copy
32 * code from other releases distributed under the terms of the GPL into a copy of
33 * this library, as the General Public License permits, the exception does
34 * not apply to the code that you add in this way. To avoid misleading
35 * anyone as to the status of such modified files, you must delete
36 * this exception notice from such code.
37 *
38 * If you write modifications of your own for this library, it is your choice
39 * whether to permit this exception to apply to your modifications.
40 * If you do not wish that, delete this exception notice. */
41
42 /* Software engineering face-lift by Greg J. Badros, 11-Dec-1999,
43 gjb@cs.washington.edu, http://www.cs.washington.edu/homes/gjb */
44
45 \f
46 #include <stdio.h>
47 #include "libguile/_scm.h"
48 #include "libguile/vectors.h"
49
50 #include "libguile/validate.h"
51 #include "libguile/weaks.h"
52 \f
53
54
55 /* {Weak Vectors}
56 */
57
58
59 SCM_DEFINE (scm_make_weak_vector, "make-weak-vector", 1, 1, 0,
60 (SCM k, SCM fill),
61 "Return a weak vector with @var{size} elements. If the optional\n"
62 "argument @var{fill} is given, all entries in the vector will be set to\n"
63 "@var{fill}. The default value for @var{fill} is the empty list.")
64 #define FUNC_NAME s_scm_make_weak_vector
65 {
66 SCM v;
67 v = scm_make_vector (scm_sum (k, SCM_MAKINUM (2)), fill);
68 SCM_DEFER_INTS;
69 SCM_SETLENGTH(v, SCM_INUM (k), scm_tc7_wvect);
70 SCM_SETVELTS(v, SCM_VELTS(v) + 2);
71 SCM_VELTS(v)[-2] = SCM_EOL;
72 SCM_UNPACK (SCM_VELTS (v)[-1]) = 0;
73 SCM_ALLOW_INTS;
74 return v;
75 }
76 #undef FUNC_NAME
77
78
79 SCM_REGISTER_PROC(s_list_to_weak_vector, "list->weak-vector", 1, 0, 0, scm_weak_vector);
80
81 SCM_DEFINE (scm_weak_vector, "weak-vector", 0, 0, 1,
82 (SCM l),
83 "@deffnx primitive list->weak-vector l\n"
84 "Construct a weak vector from a list: @code{weak-vector} uses the list of\n"
85 "its arguments while @code{list->weak-vector} uses its only argument\n"
86 "@var{l} (a list) to construct a weak vector the same way\n"
87 "@code{vector->list} would.")
88 #define FUNC_NAME s_scm_weak_vector
89 {
90 SCM res;
91 register SCM *data;
92 long i;
93
94 i = scm_ilength (l);
95 SCM_ASSERT (i >= 0, l, SCM_ARG1, FUNC_NAME);
96 res = scm_make_weak_vector (SCM_MAKINUM (i), SCM_UNSPECIFIED);
97 data = SCM_VELTS (res);
98 for (;
99 i && SCM_CONSP (l);
100 --i, l = SCM_CDR (l))
101 *data++ = SCM_CAR (l);
102 return res;
103 }
104 #undef FUNC_NAME
105
106
107 SCM_DEFINE (scm_weak_vector_p, "weak-vector?", 1, 0, 0,
108 (SCM x),
109 "Return @var{#t} if @var{obj} is a weak vector. Note that all weak\n"
110 "hashes are also weak vectors.")
111 #define FUNC_NAME s_scm_weak_vector_p
112 {
113 return SCM_BOOL(SCM_WVECTP (x) && !SCM_IS_WHVEC (x));
114 }
115 #undef FUNC_NAME
116
117
118
119 \f
120
121
122
123 SCM_DEFINE (scm_make_weak_key_hash_table, "make-weak-key-hash-table", 1, 0, 0,
124 (SCM k),
125 "@deffnx primitive make-weak-value-hash-table size\n"
126 "@deffnx primitive make-doubly-weak-hash-table size\n"
127 "Return a weak hash table with @var{size} buckets. As with any hash\n"
128 "table, choosing a good size for the table requires some caution.\n\n"
129 "You can modify weak hash tables in exactly the same way you would modify\n"
130 "regular hash tables. (@pxref{Hash Tables})")
131 #define FUNC_NAME s_scm_make_weak_key_hash_table
132 {
133 SCM v;
134 SCM_VALIDATE_INUM (1,k);
135 v = scm_make_weak_vector (k, SCM_EOL);
136 SCM_ALLOW_INTS;
137 SCM_UNPACK (SCM_VELTS (v)[-1]) = 1;
138 SCM_ALLOW_INTS;
139 return v;
140 }
141 #undef FUNC_NAME
142
143
144 SCM_DEFINE (scm_make_weak_value_hash_table, "make-weak-value-hash-table", 1, 0, 0,
145 (SCM k),
146 "")
147 #define FUNC_NAME s_scm_make_weak_value_hash_table
148 {
149 SCM v;
150 SCM_VALIDATE_INUM (1,k);
151 v = scm_make_weak_vector (k, SCM_EOL);
152 SCM_ALLOW_INTS;
153 SCM_UNPACK (SCM_VELTS (v)[-1]) = 2;
154 SCM_ALLOW_INTS;
155 return v;
156 }
157 #undef FUNC_NAME
158
159
160
161 SCM_DEFINE (scm_make_doubly_weak_hash_table, "make-doubly-weak-hash-table", 1, 0, 0,
162 (SCM k),
163 "")
164 #define FUNC_NAME s_scm_make_doubly_weak_hash_table
165 {
166 SCM v;
167 SCM_VALIDATE_INUM (1,k);
168 v = scm_make_weak_vector (k, SCM_EOL);
169 SCM_ALLOW_INTS;
170 SCM_UNPACK (SCM_VELTS (v)[-1]) = 3;
171 SCM_ALLOW_INTS;
172 return v;
173 }
174 #undef FUNC_NAME
175
176 SCM_DEFINE (scm_weak_key_hash_table_p, "weak-key-hash-table?", 1, 0, 0,
177 (SCM x),
178 "@deffnx primitive weak-value-hash-table? obj\n"
179 "@deffnx primitive doubly-weak-hash-table? obj\n"
180 "Return @var{#t} if @var{obj} is the specified weak hash table. Note\n"
181 "that a doubly weak hash table is neither a weak key nor a weak value\n"
182 "hash table.")
183 #define FUNC_NAME s_scm_weak_key_hash_table_p
184 {
185 return SCM_BOOL(SCM_WVECTP (x) && SCM_IS_WHVEC(x));
186 }
187 #undef FUNC_NAME
188
189
190 SCM_DEFINE (scm_weak_value_hash_table_p, "weak-value-hash-table?", 1, 0, 0,
191 (SCM x),
192 "")
193 #define FUNC_NAME s_scm_weak_value_hash_table_p
194 {
195 return SCM_BOOL(SCM_WVECTP (x) && SCM_IS_WHVEC_V(x));
196 }
197 #undef FUNC_NAME
198
199
200 SCM_DEFINE (scm_doubly_weak_hash_table_p, "doubly-weak-hash-table?", 1, 0, 0,
201 (SCM x),
202 "")
203 #define FUNC_NAME s_scm_doubly_weak_hash_table_p
204 {
205 return SCM_BOOL(SCM_WVECTP (x) && SCM_IS_WHVEC_B (x));
206 }
207 #undef FUNC_NAME
208
209
210 \f
211
212
213 void
214 scm_init_weaks ()
215 {
216 #include "libguile/weaks.x"
217 }
218
219
220 /*
221 Local Variables:
222 c-file-style: "gnu"
223 End:
224 */