a4127f64d8b43635c86b423d5291cc1429e128c1
[fw/altos] / src / scheme / ao_scheme_vector.c
1 /*
2  * Copyright © 2017 Keith Packard <keithp@keithp.com>
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * General Public License for more details.
13  */
14
15 #include "ao_scheme.h"
16
17 #ifdef AO_SCHEME_FEATURE_VECTOR
18
19 static void vector_mark(void *addr)
20 {
21         struct ao_scheme_vector *vector = addr;
22         unsigned int    i;
23
24         for (i = 0; i < vector->length; i++) {
25                 ao_poly v = vector->vals[i];
26
27                 ao_scheme_poly_mark(v, 1);
28         }
29 }
30
31 static int vector_len_size(uint16_t length)
32 {
33         return sizeof (struct ao_scheme_vector) + length * sizeof (ao_poly);
34 }
35
36 static int vector_size(void *addr)
37 {
38         struct ao_scheme_vector *vector = addr;
39
40         return vector_len_size(vector->length);
41 }
42
43 static void vector_move(void *addr)
44 {
45         struct ao_scheme_vector *vector = addr;
46         unsigned int    i;
47
48         for (i = 0; i < vector->length; i++)
49                 (void) ao_scheme_poly_move(&vector->vals[i], 1);
50 }
51
52 const struct ao_scheme_type ao_scheme_vector_type = {
53         .mark = vector_mark,
54         .size = vector_size,
55         .move = vector_move,
56         .name = "vector",
57 };
58
59 struct ao_scheme_vector *
60 ao_scheme_vector_alloc(uint16_t length, ao_poly fill)
61 {
62         struct ao_scheme_vector *vector;
63         unsigned int i;
64
65         vector = ao_scheme_alloc(vector_len_size(length));
66         if (!vector)
67                 return NULL;
68         vector->type = AO_SCHEME_VECTOR;
69         vector->length = length;
70         for (i = 0; i < length; i++)
71                 vector->vals[i] = fill;
72         return vector;
73 }
74
75 void
76 ao_scheme_vector_write(ao_poly v)
77 {
78         struct ao_scheme_vector *vector = ao_scheme_poly_vector(v);
79         unsigned int i;
80
81         printf("#(");
82         for (i = 0; i < vector->length; i++) {
83                 if (i != 0)
84                         printf(" ");
85                 if (vector->vals[i] == v)
86                         printf ("...");
87                 else
88                         ao_scheme_poly_write(vector->vals[i]);
89         }
90         printf(")");
91 }
92
93 void
94 ao_scheme_vector_display(ao_poly v)
95 {
96         struct ao_scheme_vector *vector = ao_scheme_poly_vector(v);
97         unsigned int i;
98
99         for (i = 0; i < vector->length; i++) {
100                 if (vector->vals[i] == v)
101                         printf("...");
102                 else
103                         ao_scheme_poly_display(vector->vals[i]);
104         }
105 }
106
107 static int32_t
108 ao_scheme_vector_offset(struct ao_scheme_vector *vector, ao_poly i)
109 {
110         bool    fail;
111         int32_t offset = ao_scheme_poly_integer(i, &fail);
112
113         if (fail)
114                 ao_scheme_error(AO_SCHEME_INVALID, "vector index %v not integer", i);
115         if (offset < 0 || vector->length <= offset) {
116                 ao_scheme_error(AO_SCHEME_INVALID, "vector index %v out of range (max %d)",
117                                 i, vector->length);
118                 offset = -1;
119         }
120         return offset;
121 }
122
123 ao_poly
124 ao_scheme_vector_get(ao_poly v, ao_poly i)
125 {
126         struct ao_scheme_vector *vector = ao_scheme_poly_vector(v);
127         int32_t                 offset = ao_scheme_vector_offset(vector, i);
128
129         if (offset < 0)
130                 return AO_SCHEME_NIL;
131         return vector->vals[offset];
132 }
133
134 ao_poly
135 ao_scheme_vector_set(ao_poly v, ao_poly i, ao_poly p)
136 {
137         struct ao_scheme_vector *vector = ao_scheme_poly_vector(v);
138         int32_t                 offset = ao_scheme_vector_offset(vector, i);
139
140         if (offset < 0)
141                 return AO_SCHEME_NIL;
142         return vector->vals[offset] = p;
143 }
144
145 struct ao_scheme_vector *
146 ao_scheme_list_to_vector(struct ao_scheme_cons *cons)
147 {
148         uint16_t                length;
149         uint16_t                i;
150         struct ao_scheme_vector *vector;
151
152         length = (uint16_t) ao_scheme_cons_length (cons);
153         if (ao_scheme_exception)
154                 return NULL;
155
156         ao_scheme_cons_stash(0, cons);
157         vector = ao_scheme_vector_alloc(length, AO_SCHEME_NIL);
158         cons = ao_scheme_cons_fetch(0);
159         if (!vector)
160                 return NULL;
161         i = 0;
162         while (cons) {
163                 vector->vals[i++] = cons->car;
164                 cons = ao_scheme_cons_cdr(cons);
165         }
166         return vector;
167 }
168
169 struct ao_scheme_cons *
170 ao_scheme_vector_to_list(struct ao_scheme_vector *vector)
171 {
172         unsigned int            i;
173         uint16_t                length = vector->length;
174         struct ao_scheme_cons   *cons = NULL;
175
176         for (i = length; i-- > 0;) {
177                 ao_scheme_poly_stash(2, ao_scheme_vector_poly(vector));
178                 cons = ao_scheme_cons_cons(vector->vals[i], ao_scheme_cons_poly(cons));
179                 vector = ao_scheme_poly_vector(ao_scheme_poly_fetch(2));
180                 if (!cons)
181                         return NULL;
182         }
183         return cons;
184 }
185
186 #endif /* AO_SCHEME_FEATURE_VECTOR */