-
Notifications
You must be signed in to change notification settings - Fork 1
/
scheme_vector.c
256 lines (223 loc) · 7.25 KB
/
scheme_vector.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
/*
libscheme
Copyright (c) 1994 Brent Benson
All rights reserved.
Permission is hereby granted, without written agreement and without
license or royalty fees, to use, copy, modify, and distribute this
software and its documentation for any purpose, provided that the
above copyright notice and the following two paragraphs appear in
all copies of this software.
IN NO EVENT SHALL BRENT BENSON BE LIABLE TO ANY PARTY FOR DIRECT,
INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT
OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF BRENT
BENSON HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
BRENT BENSON SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, BUT
NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER
IS ON AN "AS IS" BASIS, AND BRENT BENSON HAS NO OBLIGATION TO
PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR
MODIFICATIONS.
*/
#include "scheme.h"
/* globals */
Scheme_Value scheme_vector_type;
/* primitive declarations */
static Scheme_Value vector_p (int argc, Scheme_Value argv[]);
static Scheme_Value make_vector (int argc, Scheme_Value argv[]);
static Scheme_Value vector (int argc, Scheme_Value argv[]);
static Scheme_Value vector_length (int argc, Scheme_Value argv[]);
static Scheme_Value vector_ref (int argc, Scheme_Value argv[]);
static Scheme_Value vector_set (int argc, Scheme_Value argv[]);
static Scheme_Value vector_to_list (int argc, Scheme_Value argv[]);
static Scheme_Value list_to_vector (int argc, Scheme_Value argv[]);
static Scheme_Value vector_fill (int argc, Scheme_Value argv[]);
static Scheme_Value vector_append (int argc, Scheme_Value argv[]);
/* exported functions */
void
scheme_init_vector (Scheme_Env *env)
{
scheme_vector_type = scheme_make_type ("<vector>");
scheme_add_global ("<vector>", scheme_vector_type, env);
scheme_add_prim ("vector?", vector_p, env);
scheme_add_prim ("make-vector", make_vector, env);
scheme_add_prim ("vector", vector, env);
scheme_add_prim ("vector-length", vector_length, env);
scheme_add_prim ("vector-ref", vector_ref, env);
scheme_add_prim ("vector-set!", vector_set, env);
scheme_add_prim ("vector->list", vector_to_list, env);
scheme_add_prim ("list->vector", list_to_vector, env);
scheme_add_prim ("vector-fill!", vector_fill, env);
scheme_add_prim ("vector-append", vector_append, env);
}
Scheme_Value
scheme_make_vector (int size, Scheme_Value fill)
{
int i;
Scheme_Value vec, *els;
size_t nbytes = sizeof(Scheme_Object) + sizeof(Scheme_Object*) * size;
vec = (Scheme_Value)scheme_malloc(nbytes);
els = (Scheme_Value *)(&vec[1]);
SCHEME_TYPE(vec) = scheme_vector_type;
SCHEME_VEC_SIZE(vec) = size;
SCHEME_VEC_ELS(vec) = els;
for ( i=0 ; i<size ; ++i )
{
SCHEME_VEC_ELS(vec)[i] = fill;
}
return (vec);
}
Scheme_Value
scheme_list_to_vector (Scheme_Value list)
{
int len, i;
Scheme_Value vec;
len = scheme_list_length (list);
vec = scheme_make_vector (len, 0);
i = 0;
while (! SCHEME_NULLP (list))
{
SCHEME_VEC_ELS(vec)[i] = SCHEME_CAR (list);
i++;
list = SCHEME_CDR (list);
}
return (vec);
}
Scheme_Value
scheme_vector_to_list (Scheme_Value vec)
{
int len, i;
Scheme_Value first, last, pair;
len = SCHEME_VEC_SIZE (vec);
first = last = scheme_null;
for ( i=0 ; i<len ; ++i )
{
pair = scheme_make_pair (SCHEME_VEC_ELS(vec)[i], scheme_null);
if (first == scheme_null)
{
first = last = pair;
}
else
{
SCHEME_CDR (last) = pair;
last = pair;
}
}
return (first);
}
/* primitive functions */
static Scheme_Value
vector_p (int argc, Scheme_Value argv[])
{
SCHEME_ASSERT ((argc == 1), "vector?: wrong number of args");
return (SCHEME_VECTORP(argv[0]) ? scheme_true : scheme_false);
}
static Scheme_Value
make_vector (int argc, Scheme_Value argv[])
{
Scheme_Value vec, fill;
int len;
SCHEME_ASSERT ((argc==1 || argc==2), "make-vector: wrong number of args");
SCHEME_ASSERT (SCHEME_INTP (argv[0]), "make-vector: first arg must be an integer");
len = SCHEME_INT_VAL (argv[0]);
if (argc == 2)
{
fill = argv[1];
}
else
{
fill = scheme_false;
}
vec = scheme_make_vector (len, fill);
return (vec);
}
static Scheme_Value
vector (int argc, Scheme_Value argv[])
{
Scheme_Value vec;
int i;
vec = scheme_make_vector (argc, 0);
for ( i=0 ; i<argc ; ++i )
{
SCHEME_VEC_ELS(vec)[i] = argv[i];
}
return (vec);
}
static Scheme_Value
vector_length (int argc, Scheme_Value argv[])
{
SCHEME_ASSERT ((argc == 1), "vector-length: wrong number of args");
SCHEME_ASSERT (SCHEME_VECTORP (argv[0]), "vector-length: arg must be a vector");
return (scheme_make_integer (SCHEME_VEC_SIZE (argv[0])));
}
static Scheme_Value
vector_ref (int argc, Scheme_Value argv[])
{
int i;
SCHEME_ASSERT ((argc == 2), "vector-ref: wrong number of args");
SCHEME_ASSERT (SCHEME_VECTORP (argv[0]), "vector-ref: first arg must be a vector");
SCHEME_ASSERT (SCHEME_INTP (argv[1]), "vector-ref: second arg must be an integer");
i = SCHEME_INT_VAL (argv[1]);
SCHEME_ASSERT ((i >= 0) && (i < SCHEME_VEC_SIZE (argv[0])),
"vector-ref: index out of range");
return (SCHEME_VEC_ELS(argv[0])[i]);
}
static Scheme_Value
vector_set (int argc, Scheme_Value argv[])
{
int i;
SCHEME_ASSERT ((argc == 3), "vector-set!: wrong number of args");
SCHEME_ASSERT (SCHEME_VECTORP (argv[0]), "vector-set!: first arg must be a vector");
SCHEME_ASSERT (SCHEME_INTP (argv[1]), "vector-set!: second arg must be an integer");
i = SCHEME_INT_VAL (argv[1]);
SCHEME_ASSERT ((i >= 0) && (i < SCHEME_VEC_SIZE (argv[0])),
"vector-ref: index out of range");
SCHEME_VEC_ELS(argv[0])[i] = argv[2];
return (argv[0]);
}
static Scheme_Value
vector_to_list (int argc, Scheme_Value argv[])
{
SCHEME_ASSERT ((argc == 1), "vector->list: wrong number of args");
SCHEME_ASSERT (SCHEME_VECTORP(argv[0]), "vector->list: arg must be a vector");
return (scheme_vector_to_list (argv[0]));
}
static Scheme_Value
list_to_vector (int argc, Scheme_Value argv[])
{
SCHEME_ASSERT ((argc == 1), "list->vector: wrong number of args");
SCHEME_ASSERT (SCHEME_LISTP(argv[0]), "list->vector: arg must be a list");
return (scheme_list_to_vector (argv[0]));
}
static Scheme_Value
vector_fill (int argc, Scheme_Value argv[])
{
int i;
SCHEME_ASSERT ((argc == 2), "vector-fill!: wrong number of args");
SCHEME_ASSERT (SCHEME_VECTORP (argv[0]), "vector-fill!: first arg must be a vector");
for ( i=0 ; i<SCHEME_VEC_SIZE (argv[0]) ; ++i )
{
SCHEME_VEC_ELS(argv[0])[i] = argv[1];
}
return (argv[0]);
}
static Scheme_Value
vector_append (int argc, Scheme_Value argv[])
{
int len1, len2, i;
Scheme_Value new;
SCHEME_ASSERT ((argc == 2), "vector-append: wrong number of args");
SCHEME_ASSERT (SCHEME_VECTORP (argv[0]) && SCHEME_VECTORP (argv[1]),
"vector-append: both args must be vectors");
len1 = SCHEME_VEC_SIZE (argv[0]);
len2 = SCHEME_VEC_SIZE (argv[1]);
new = scheme_make_vector ((len1 + len2), NULL);
for ( i=0 ; i<len1 ; ++i )
{
SCHEME_VEC_ELS(new)[i] = SCHEME_VEC_ELS(argv[0])[i];
}
for ( i=len1 ; i<(len1 + len2) ; ++i )
{
SCHEME_VEC_ELS(new)[i] = SCHEME_VEC_ELS(argv[1])[i-len1];
}
return (new);
}