-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathvector.c
186 lines (163 loc) · 4.58 KB
/
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
/* LeeScheme/vector.c - Copyright (C) Lee Richard Boynton, 1993-2000. */
#include "scheme.h"
object make_vector(long length, object fill) {
long i = length * sizeof(object);
long size = sizeof(struct vector_heap_structure) + i;
object *ve, v = make_heap_object(VECTOR_TYPE,size);
VECTOR_LENGTH(v) = length;
VECTOR_TAG(v) = false_object;
ve = VECTOR_ELEMENTS(v);
while (length--)
*ve++ = fill;
return v;
}
static void primop_vector_p(long argc) {
if (VECTOR_P(*sp) && FALSE_P(VECTOR_TAG(*sp))) return;
*sp = false_object;
}
static void primop_make_vector(long argc) {
object o = *sp++;
long size = the_long(1,o);
if (size < 0 || size > MAX_VECTOR_SIZE)
error(sp[0],"too big of a size for a vector");
if (argc == 2)
*sp = make_vector(size, *sp);
else
*--sp = make_vector(size, null_object);
}
static void primop_vector(long argc) {
object *ve, v = make_vector(argc,null_object);
long i;
ve = VECTOR_ELEMENTS(v);
for (i=0; i<argc; i++)
*ve++ = *sp++;
*--sp = v;
}
static void primop_vector_length(long argc) {
object o = *sp;
TYPE_CHECK(VECTOR_P(o),1,"vector",o);
*sp = MAKE_FIXNUM(VECTOR_LENGTH(o));
}
static void primop_set_vector_tag(long argc) {
object o = *sp++;
TYPE_CHECK(VECTOR_P(o),1,"vector",o);
VECTOR_TAG(o) = *sp;
}
static void primop_vector_ref(long argc) {
object o = sp[1];
long i = the_long(2,o);
o = *sp++;
TYPE_CHECK(VECTOR_P(o),1,"vector",o);
if (i < 0 || i >= VECTOR_LENGTH(o))
error(MAKE_FIXNUM(i),"index out of range");
*sp = VECTOR_ELEMENTS(o)[i];
}
static void primop_vector_set(long argc) {
object o = sp[1];
long i = the_long(2,o);
o = *sp;
sp += 2;
TYPE_CHECK(VECTOR_P(o),1,"vector",o);
if (i < 0 || i >= VECTOR_LENGTH(o))
error(MAKE_FIXNUM(i),"index out of range");
VECTOR_ELEMENTS(o)[i] = *sp;
}
object list_to_vector(object l) {
long i = length(l);
object v, *ve;
if (i < 0)
error(l,"argument not a proper list");
v = make_vector(i,null_object);
ve = VECTOR_ELEMENTS(v);
while (i--) {
*ve++ = CAR(l);
l = CDR(l);
}
return v;
}
static void primop_list_to_vector(long argc) {
object l = *sp;
TYPE_CHECK(LIST_P(l),1,"list",l);
*sp = list_to_vector(l);
}
object vector_to_list(object v) {
object l = null_object, *ve;
long i = VECTOR_LENGTH(v);
PUSH_GC_PROTECT(l);
ve = VECTOR_ELEMENTS(v);
while (i--)
l = cons(ve[i],l);
POP_GC_PROTECT(1);
return l;
}
static void primop_vector_to_list(long argc) {
object v = *sp;
TYPE_CHECK(VECTOR_P(v),1,"vector",v);
*sp = vector_to_list(v);
}
object make_signal(long length, double fill) {
long i = length * sizeof(double);
long size = sizeof(struct signal_heap_structure) + i;
object v = make_heap_object(SIGNAL_TYPE,size);
double *ve;
SIGNAL_LENGTH(v) = length;
ve = SIGNAL_ELEMENTS(v);
while (length--)
*ve++ = fill;
return v;
}
static void primop_signal_p(long argc) {
if (SIGNAL_P(*sp)) return;
*sp = false_object;
}
static void primop_make_signal(long argc) {
object o = *sp++;
long size = the_long(1,o);
if (size < 0 || size > MAX_VECTOR_SIZE)
error(o,"too big of a size for a signal");
if (argc == 2) {
double fill = the_double(2, *sp);
*sp = make_signal(size, fill);
} else
*--sp = make_signal(size, 0.0);
}
static void primop_signal_length(long argc) {
object o = *sp;
TYPE_CHECK(SIGNAL_P(o),1,"signal",o);
*sp = MAKE_FIXNUM(SIGNAL_LENGTH(o));
}
static void primop_signal_ref(long argc) {
object o = sp[1];
long i = the_long(2,o);
o = *sp++;
TYPE_CHECK(SIGNAL_P(o),1,"signal",o);
if (i < 0 || i >= SIGNAL_LENGTH(o))
error(MAKE_FIXNUM(i),"index out of range");
*sp = make_flonum(SIGNAL_ELEMENTS(o)[i]);
}
static void primop_signal_set(long argc) {
object o = sp[1];
long i = the_long(2,o);
o = *sp;
sp += 2;
TYPE_CHECK(SIGNAL_P(o),1,"signal",o);
if (i < 0 || i >= SIGNAL_LENGTH(o))
error(MAKE_FIXNUM(i),"index out of range");
SIGNAL_ELEMENTS(o)[i] = the_double(3, *sp);
}
void init_vector(void) {
define_primop("vector?",primop_vector_p,1,1);
define_primop("make-vector",primop_make_vector,1,2);
define_primop("vector",primop_vector,0,MAX_ARGC);
define_primop("vector-length",primop_vector_length,1,1);
define_primop("system:set-vector-type!",primop_set_vector_tag,2,2);
define_primop("vector-ref",primop_vector_ref,2,2);
define_primop("vector-set!",primop_vector_set,3,3);
define_primop("list->vector",primop_list_to_vector,1,1);
define_primop("vector->list",primop_vector_to_list,1,1);
define_primop("signal?",primop_signal_p,1,1);
define_primop("make-signal",primop_make_signal,1,2);
define_primop("signal-length",primop_signal_length,1,1);
define_primop("signal-ref",primop_signal_ref,2,2);
define_primop("signal-set!",primop_signal_set,3,3);
}