*** empty log message ***
[bpt/guile.git] / libguile / weaks.c
CommitLineData
7dc6e754 1/* Copyright (C) 1995,1996,1998 Free Software Foundation, Inc.
0f2d19dd
JB
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
82892bed
JB
15 * the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
16 * Boston, MA 02111-1307 USA
0f2d19dd
JB
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.
82892bed 40 * If you do not wish that, delete this exception notice. */
1bbd0b84
GB
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
0f2d19dd
JB
45\f
46#include <stdio.h>
47#include "_scm.h"
48
b6791b2e 49#include "validate.h"
20e6290e 50#include "weaks.h"
0f2d19dd
JB
51\f
52
53
54/* {Weak Vectors}
55 */
56
57
3b3b36dd 58SCM_DEFINE (scm_make_weak_vector, "make-weak-vector", 1, 1, 0,
1bbd0b84 59 (SCM k, SCM fill),
b380b885
MD
60 "Return a weak vector with @var{size} elements. If the optional\n"
61 "argument @var{fill} is given, all entries in the vector will be set to\n"
62 "@var{fill}. The default value for @var{fill} is the empty list.")
1bbd0b84 63#define FUNC_NAME s_scm_make_weak_vector
0f2d19dd
JB
64{
65 SCM v;
250da369 66 v = scm_make_vector (scm_sum (k, SCM_MAKINUM (2)), fill);
0f2d19dd
JB
67 SCM_DEFER_INTS;
68 SCM_SETLENGTH(v, SCM_INUM (k), scm_tc7_wvect);
250da369
JB
69 SCM_VELTS(v)[0] = SCM_EOL;
70 SCM_VELTS(v)[1] = (SCM)0;
71 SCM_SETVELTS(v, SCM_VELTS(v) + 2);
0f2d19dd
JB
72 SCM_ALLOW_INTS;
73 return v;
74}
1bbd0b84 75#undef FUNC_NAME
0f2d19dd
JB
76
77
1bbd0b84 78SCM_REGISTER_PROC(s_list_to_weak_vector, "list->weak-vector", 1, 0, 0, scm_weak_vector);
1cc91f1b 79
3b3b36dd 80SCM_DEFINE (scm_weak_vector, "weak-vector", 0, 0, 1,
1bbd0b84 81 (SCM l),
b380b885
MD
82 "@deffnx primitive list->weak-vector l\n"
83 "Construct a weak vector from a list: @code{weak-vector} uses the list of\n"
84 "its arguments while @code{list->weak-vector} uses its only argument\n"
85 "@var{l} (a list) to construct a weak vector the same way\n"
86 "@code{vector->list} would.")
1bbd0b84 87#define FUNC_NAME s_scm_weak_vector
0f2d19dd
JB
88{
89 SCM res;
90 register SCM *data;
91 long i;
92
93 i = scm_ilength (l);
1bbd0b84 94 SCM_ASSERT (i >= 0, l, SCM_ARG1, FUNC_NAME);
0f2d19dd
JB
95 res = scm_make_weak_vector (SCM_MAKINUM (i), SCM_UNSPECIFIED);
96 data = SCM_VELTS (res);
97 for (;
0c95b57d 98 i && SCM_CONSP (l);
0f2d19dd
JB
99 --i, l = SCM_CDR (l))
100 *data++ = SCM_CAR (l);
101 return res;
102}
1bbd0b84 103#undef FUNC_NAME
0f2d19dd
JB
104
105
3b3b36dd 106SCM_DEFINE (scm_weak_vector_p, "weak-vector?", 1, 0, 0,
1bbd0b84 107 (SCM x),
b380b885
MD
108 "Return @var{#t} if @var{obj} is a weak vector. Note that all weak\n"
109 "hashes are also weak vectors.")
1bbd0b84 110#define FUNC_NAME s_scm_weak_vector_p
0f2d19dd 111{
0c95b57d 112 return SCM_BOOL(SCM_WVECTP (x) && !SCM_IS_WHVEC (x));
0f2d19dd 113}
1bbd0b84 114#undef FUNC_NAME
0f2d19dd
JB
115
116
117
118\f
119
120
121
3b3b36dd 122SCM_DEFINE (scm_make_weak_key_hash_table, "make-weak-key-hash-table", 1, 0, 0,
1bbd0b84 123 (SCM k),
b380b885
MD
124 "@deffnx primitive make-weak-value-hash-table size\n"
125 "@deffnx primitive make-doubly-weak-hash-table size\n"
126 "Return a weak hash table with @var{size} buckets. As with any hash\n"
127 "table, choosing a good size for the table requires some caution.\n\n"
128 "You can modify weak hash tables in exactly the same way you would modify\n"
129 "regular hash tables. (@pxref{Hash Tables})")
1bbd0b84 130#define FUNC_NAME s_scm_make_weak_key_hash_table
0f2d19dd
JB
131{
132 SCM v;
3b3b36dd 133 SCM_VALIDATE_INUM (1,k);
0f2d19dd
JB
134 v = scm_make_weak_vector (k, SCM_EOL);
135 SCM_ALLOW_INTS;
413cb56f 136 SCM_BITS (SCM_VELTS (v)[-1]) = 1;
0f2d19dd
JB
137 SCM_ALLOW_INTS;
138 return v;
139}
1bbd0b84 140#undef FUNC_NAME
0f2d19dd
JB
141
142
a1ec6916 143SCM_DEFINE (scm_make_weak_value_hash_table, "make-weak-value-hash-table", 1, 0, 0,
1bbd0b84 144 (SCM k),
b380b885 145 "")
1bbd0b84 146#define FUNC_NAME s_scm_make_weak_value_hash_table
0f2d19dd
JB
147{
148 SCM v;
3b3b36dd 149 SCM_VALIDATE_INUM (1,k);
0f2d19dd
JB
150 v = scm_make_weak_vector (k, SCM_EOL);
151 SCM_ALLOW_INTS;
413cb56f 152 SCM_BITS (SCM_VELTS (v)[-1]) = 2;
0f2d19dd
JB
153 SCM_ALLOW_INTS;
154 return v;
155}
1bbd0b84 156#undef FUNC_NAME
0f2d19dd
JB
157
158
159
a1ec6916 160SCM_DEFINE (scm_make_doubly_weak_hash_table, "make-doubly-weak-hash-table", 1, 0, 0,
1bbd0b84 161 (SCM k),
b380b885 162 "")
1bbd0b84 163#define FUNC_NAME s_scm_make_doubly_weak_hash_table
0f2d19dd
JB
164{
165 SCM v;
3b3b36dd 166 SCM_VALIDATE_INUM (1,k);
0f2d19dd
JB
167 v = scm_make_weak_vector (k, SCM_EOL);
168 SCM_ALLOW_INTS;
413cb56f 169 SCM_BITS (SCM_VELTS (v)[-1]) = 3;
0f2d19dd
JB
170 SCM_ALLOW_INTS;
171 return v;
172}
1bbd0b84 173#undef FUNC_NAME
0f2d19dd 174
3b3b36dd 175SCM_DEFINE (scm_weak_key_hash_table_p, "weak-key-hash-table?", 1, 0, 0,
1bbd0b84 176 (SCM x),
b380b885
MD
177 "@deffnx primitive weak-value-hash-table? obj\n"
178 "@deffnx primitive doubly-weak-hash-table? obj\n"
179 "Return @var{#t} if @var{obj} is the specified weak hash table. Note\n"
180 "that a doubly weak hash table is neither a weak key nor a weak value\n"
181 "hash table.")
1bbd0b84 182#define FUNC_NAME s_scm_weak_key_hash_table_p
0f2d19dd 183{
0c95b57d 184 return SCM_BOOL(SCM_WVECTP (x) && SCM_IS_WHVEC(x));
0f2d19dd 185}
1bbd0b84 186#undef FUNC_NAME
0f2d19dd
JB
187
188
a1ec6916 189SCM_DEFINE (scm_weak_value_hash_table_p, "weak-value-hash-table?", 1, 0, 0,
1bbd0b84 190 (SCM x),
b380b885 191 "")
1bbd0b84 192#define FUNC_NAME s_scm_weak_value_hash_table_p
0f2d19dd 193{
0c95b57d 194 return SCM_BOOL(SCM_WVECTP (x) && SCM_IS_WHVEC_V(x));
0f2d19dd 195}
1bbd0b84 196#undef FUNC_NAME
0f2d19dd
JB
197
198
a1ec6916 199SCM_DEFINE (scm_doubly_weak_hash_table_p, "doubly-weak-hash-table?", 1, 0, 0,
1bbd0b84 200 (SCM x),
b380b885 201 "")
1bbd0b84 202#define FUNC_NAME s_scm_doubly_weak_hash_table_p
0f2d19dd 203{
0c95b57d 204 return SCM_BOOL(SCM_WVECTP (x) && SCM_IS_WHVEC_B (x));
0f2d19dd 205}
1bbd0b84 206#undef FUNC_NAME
0f2d19dd
JB
207
208
209\f
210
1cc91f1b 211
0f2d19dd
JB
212void
213scm_init_weaks ()
0f2d19dd
JB
214{
215#include "weaks.x"
216}
217