Fix from Ken Raeburn <raeburn@raeburn.org>:
[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 \f
42 #include <stdio.h>
43 #include "_scm.h"
44
45 #include "weaks.h"
46 \f
47
48
49 /* {Weak Vectors}
50 */
51
52
53 SCM_PROC(s_make_weak_vector, "make-weak-vector", 1, 1, 0, scm_make_weak_vector);
54
55 SCM
56 scm_make_weak_vector (k, fill)
57 SCM k;
58 SCM fill;
59 {
60 SCM v;
61 v = scm_make_vector (scm_sum (k, SCM_MAKINUM (2)), fill);
62 SCM_DEFER_INTS;
63 SCM_SETLENGTH(v, SCM_INUM (k), scm_tc7_wvect);
64 SCM_VELTS(v)[0] = SCM_EOL;
65 SCM_VELTS(v)[1] = (SCM)0;
66 SCM_SETVELTS(v, SCM_VELTS(v) + 2);
67 SCM_ALLOW_INTS;
68 return v;
69 }
70
71
72 SCM_PROC(s_weak_vector, "weak-vector", 0, 0, 1, scm_weak_vector);
73 SCM_PROC(s_list_to_weak_vector, "list->weak-vector", 1, 0, 0, scm_weak_vector);
74
75 SCM
76 scm_weak_vector (l)
77 SCM l;
78 {
79 SCM res;
80 register SCM *data;
81 long i;
82
83 i = scm_ilength (l);
84 SCM_ASSERT (i >= 0, l, SCM_ARG1, s_weak_vector);
85 res = scm_make_weak_vector (SCM_MAKINUM (i), SCM_UNSPECIFIED);
86 data = SCM_VELTS (res);
87 for (;
88 i && SCM_NIMP (l) && SCM_CONSP (l);
89 --i, l = SCM_CDR (l))
90 *data++ = SCM_CAR (l);
91 return res;
92 }
93
94
95 SCM_PROC(s_weak_vector_p, "weak-vector?", 1, 0, 0, scm_weak_vector_p);
96
97 SCM
98 scm_weak_vector_p (x)
99 SCM x;
100 {
101 return ((SCM_NIMP (x) && SCM_WVECTP (x) && !SCM_IS_WHVEC (x))
102 ? SCM_BOOL_T
103 : SCM_BOOL_F);
104 }
105
106
107
108 \f
109
110
111
112 SCM_PROC(s_make_weak_key_hash_table, "make-weak-key-hash-table", 1, 0, 0, scm_make_weak_key_hash_table);
113
114 SCM
115 scm_make_weak_key_hash_table (k)
116 SCM k;
117 {
118 SCM v;
119 SCM_ASSERT (SCM_INUMP (k), k, SCM_ARG1, s_make_weak_key_hash_table);
120 v = scm_make_weak_vector (k, SCM_EOL);
121 SCM_ALLOW_INTS;
122 SCM_VELTS (v)[-1] = 1;
123 SCM_ALLOW_INTS;
124 return v;
125 }
126
127
128 SCM_PROC (s_make_weak_value_hash_table, "make-weak-value-hash-table", 1, 0, 0, scm_make_weak_value_hash_table);
129
130 SCM
131 scm_make_weak_value_hash_table (k)
132 SCM k;
133 {
134 SCM v;
135 SCM_ASSERT (SCM_INUMP (k), k, SCM_ARG1, s_make_weak_value_hash_table);
136 v = scm_make_weak_vector (k, SCM_EOL);
137 SCM_ALLOW_INTS;
138 SCM_VELTS (v)[-1] = 2;
139 SCM_ALLOW_INTS;
140 return v;
141 }
142
143
144
145 SCM_PROC (s_make_doubly_weak_hash_table, "make-doubly-weak-hash-table", 1, 0, 0, scm_make_doubly_weak_hash_table);
146
147 SCM
148 scm_make_doubly_weak_hash_table (k)
149 SCM k;
150 {
151 SCM v;
152 SCM_ASSERT (SCM_INUMP (k), k, SCM_ARG1, s_make_doubly_weak_hash_table);
153 v = scm_make_weak_vector (k, SCM_EOL);
154 SCM_ALLOW_INTS;
155 SCM_VELTS (v)[-1] = 3;
156 SCM_ALLOW_INTS;
157 return v;
158 }
159
160 SCM_PROC(s_weak_key_hash_table_p, "weak-key-hash-table?", 1, 0, 0, scm_weak_key_hash_table_p);
161
162 SCM
163 scm_weak_key_hash_table_p (x)
164 SCM x;
165 {
166 return ((SCM_NIMP (x) && SCM_WVECTP (x) && SCM_IS_WHVEC(x))
167 ? SCM_BOOL_T
168 : SCM_BOOL_F);
169 }
170
171
172 SCM_PROC (s_weak_value_hash_table_p, "weak-value-hash-table?", 1, 0, 0, scm_weak_value_hash_table_p);
173
174 SCM
175 scm_weak_value_hash_table_p (x)
176 SCM x;
177 {
178 return ((SCM_NIMP (x) && SCM_WVECTP (x) && SCM_IS_WHVEC_V(x))
179 ? SCM_BOOL_T
180 : SCM_BOOL_F);
181 }
182
183
184 SCM_PROC (s_doubly_weak_hash_table_p, "doubly-weak-hash-table?", 1, 0, 0, scm_doubly_weak_hash_table_p);
185
186 SCM
187 scm_doubly_weak_hash_table_p (x)
188 SCM x;
189 {
190 return ((SCM_NIMP (x) && SCM_WVECTP (x) && SCM_IS_WHVEC_B (x))
191 ? SCM_BOOL_T
192 : SCM_BOOL_F);
193 }
194
195
196 \f
197
198
199 void
200 scm_init_weaks ()
201 {
202 #include "weaks.x"
203 }
204