Debian Codegraph @

scheme/ikarus.vectors.ss

raw
1 ;;; Ikarus Scheme -- A compiler for R6RS Scheme.
2 ;;; Copyright (C) 2006,2007,2008 Abdulaziz Ghuloum
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 version 3 as
6 ;;; published by the Free Software Foundation.
7 ;;;
8 ;;; This program is distributed in the hope that it will be useful, but
9 ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
10 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 ;;; General Public License for more details.
12 ;;;
13 ;;; You should have received a copy of the GNU General Public License
14 ;;; along with this program. If not, see <http://www.gnu.org/licenses/>.
15
16
17 (library (ikarus vectors)
18 (export make-vector vector vector-length vector-ref vector-set!
19 vector->list list->vector vector-map vector-for-each
20 vector-fill!)
21 (import
22 (except (ikarus) make-vector vector
23 vector-length vector-ref vector-set!
24 vector->list list->vector vector-map vector-for-each
25 vector-fill!)
26 (ikarus system $fx)
27 (ikarus system $pairs)
28 (ikarus system $vectors))
29
30
31 (define vector-length
32 (lambda (x)
33 (unless (vector? x)
34 (die 'vector-length "not a vector" x))
35 ($vector-length x)))
36
37 (module (make-vector)
38 (define fill!
39 (lambda (v i n fill)
40 (cond
41 [($fx= i n) v]
42 [else
43 ($vector-set! v i fill)
44 (fill! v ($fx+ i 1) n fill)])))
45 (define make-vector
46 (case-lambda
47 [(n) (make-vector n (void))]
48 [(n fill)
49 (unless (and (fixnum? n) ($fx>= n 0))
50 (die 'make-vector "not a valid length" n))
51 (fill! ($make-vector n) 0 n fill)])))
52
53
54 (define vector
55 ;;; FIXME: add case-lambda
56 (letrec ([length
57 (lambda (ls n)
58 (cond
59 [(null? ls) n]
60 [else (length ($cdr ls) ($fx+ n 1))]))]
61 [loop
62 (lambda (v ls i n)
63 (cond
64 [($fx= i n) v]
65 [else
66 ($vector-set! v i ($car ls))
67 (loop v ($cdr ls) ($fx+ i 1) n)]))])
68 (lambda ls
69 (let ([n (length ls 0)])
70 (let ([v (make-vector n)])
71 (loop v ls 0 n))))))
72
73
74 (define vector-ref
75 (lambda (v i)
76 (unless (vector? v)
77 (die 'vector-ref "not a vector" v))
78 (unless (fixnum? i)
79 (die 'vector-ref "not a valid index" i))
80 (unless (and ($fx< i ($vector-length v))
81 ($fx<= 0 i))
82 (die 'vector-ref "index is out of range" i v))
83 ($vector-ref v i)))
84
85 (define vector-set!
86 (lambda (v i c)
87 (unless (vector? v)
88 (die 'vector-set! "not a vector" v))
89 (unless (fixnum? i)
90 (die 'vector-set! "not a valid index" i))
91 (unless (and ($fx< i ($vector-length v))
92 ($fx<= 0 i))
93 (die 'vector-set! "index is out of range" i v))
94 ($vector-set! v i c)))
95
96 (define vector->list
97 (lambda (v)
98 (define f
99 (lambda (v i ls)
100 (cond
101 [($fx< i 0) ls]
102 [else
103 (f v ($fxsub1 i) (cons ($vector-ref v i) ls))])))
104 (if (vector? v)
105 (let ([n ($vector-length v)])
106 (if ($fxzero? n)
107 '()
108 (f v ($fxsub1 n) '())))
109 (die 'vector->list "not a vector" v))))
110
111 (define list->vector
112 (letrec ([race
113 (lambda (h t ls n)
114 (if (pair? h)
115 (let ([h ($cdr h)])
116 (if (pair? h)
117 (if (not (eq? h t))
118 (race ($cdr h) ($cdr t) ls ($fx+ n 2))
119 (die 'list->vector "circular list" ls))
120 (if (null? h)
121 ($fx+ n 1)
122 (die 'list->vector "not a proper list" ls))))
123 (if (null? h)
124 n
125 (die 'list->vector "not a proper list" ls))))]
126 [fill
127 (lambda (v i ls)
128 (cond
129 [(null? ls) v]
130 [else
131 (let ([c ($car ls)])
132 ($vector-set! v i c)
133 (fill v ($fxadd1 i) (cdr ls)))]))])
134 (lambda (ls)
135 (let ([n (race ls ls ls 0)])
136 (let ([v (make-vector n)])
137 (fill v 0 ls))))))
138
139 (module (vector-map)
140 (define who 'vector-map)
141 (define (ls->vec ls n)
142 (let f ([v (make-vector n)]
143 [n n]
144 [ls ls])
145 (cond
146 [(null? ls) v]
147 [else
148 (let ([n ($fxsub1 n)])
149 ($vector-set! v n ($car ls))
150 (f v n ($cdr ls)))])))
151 (define vector-map
152 (case-lambda
153 [(p v)
154 (unless (procedure? p)
155 (die who "not a procedure" p))
156 (unless (vector? v)
157 (die who "not a vector" v))
158 (let f ([p p] [v v] [i 0] [n (vector-length v)] [ac '()])
159 (cond
160 [($fx= i n) (ls->vec ac n)]
161 [else
162 (f p v ($fxadd1 i) n (cons (p (vector-ref v i)) ac))]))]
163 [(p v0 v1)
164 (unless (procedure? p)
165 (die who "not a procedure" p))
166 (unless (vector? v0)
167 (die who "not a vector" v0))
168 (unless (vector? v1)
169 (die who "not a vector" v1))
170 (let ([n (vector-length v0)])
171 (unless ($fx= n ($vector-length v1))
172 (die who "length mismatch" v0 v1))
173 (let f ([p p] [v0 v0] [v1 v1] [i 0] [n n] [ac '()])
174 (cond
175 [($fx= i n) (ls->vec ac n)]
176 [else
177 (f p v0 v1 ($fxadd1 i) n
178 (cons (p ($vector-ref v0 i) ($vector-ref v1 i)) ac))])))]
179 [(p v0 v1 . v*)
180 (unless (procedure? p)
181 (die who "not a procedure" p))
182 (unless (vector? v0)
183 (die who "not a vector" v0))
184 (unless (vector? v1)
185 (die who "not a vector" v1))
186 (let ([n (vector-length v0)])
187 (unless ($fx= n ($vector-length v1))
188 (die who "length mismatch" v0 v1))
189 (let f ([v* v*] [n n])
190 (unless (null? v*)
191 (let ([a ($car v*)])
192 (unless (vector? a)
193 (die who "not a vector" a))
194 (unless ($fx= ($vector-length a) n)
195 (die who "length mismatch")))
196 (f ($cdr v*) n)))
197 (let f ([p p] [v0 v0] [v1 v1] [v* v*] [i 0] [n n] [ac '()])
198 (cond
199 [($fx= i n) (ls->vec ac n)]
200 [else
201 (f p v0 v1 v* ($fxadd1 i) n
202 (cons
203 (apply p ($vector-ref v0 i) ($vector-ref v1 i)
204 (let f ([i i] [v* v*])
205 (if (null? v*)
206 '()
207 (cons ($vector-ref ($car v*) i)
208 (f i ($cdr v*))))))
209 ac))])))])))
210
211
212 (module (vector-for-each)
213 (define who 'vector-for-each)
214 (define vector-for-each
215 (case-lambda
216 [(p v)
217 (unless (procedure? p)
218 (die who "not a procedure" p))
219 (unless (vector? v)
220 (die who "not a vector" v))
221 (let f ([p p] [v v] [i 0] [n (vector-length v)])
222 (cond
223 [($fx= i n) (void)]
224 [else
225 (p (vector-ref v i))
226 (f p v ($fxadd1 i) n)]))]
227 [(p v0 v1)
228 (unless (procedure? p)
229 (die who "not a procedure" p))
230 (unless (vector? v0)
231 (die who "not a vector" v0))
232 (unless (vector? v1)
233 (die who "not a vector" v1))
234 (let ([n (vector-length v0)])
235 (unless ($fx= n ($vector-length v1))
236 (die who "length mismatch" v0 v1))
237 (let f ([p p] [v0 v0] [v1 v1] [i 0] [n n])
238 (cond
239 [($fx= i n) (void)]
240 [else
241 (p ($vector-ref v0 i) ($vector-ref v1 i))
242 (f p v0 v1 ($fxadd1 i) n)])))]
243 [(p v0 v1 . v*)
244 (unless (procedure? p)
245 (die who "not a procedure" p))
246 (unless (vector? v0)
247 (die who "not a vector" v0))
248 (unless (vector? v1)
249 (die who "not a vector" v1))
250 (let ([n (vector-length v0)])
251 (unless ($fx= n ($vector-length v1))
252 (die who "length mismatch" v0 v1))
253 (let f ([v* v*] [n n])
254 (unless (null? v*)
255 (let ([a ($car v*)])
256 (unless (vector? a)
257 (die who "not a vector" a))
258 (unless ($fx= ($vector-length a) n)
259 (die who "length mismatch")))
260 (f ($cdr v*) n)))
261 (let f ([p p] [v0 v0] [v1 v1] [v* v*] [i 0] [n n])
262 (cond
263 [($fx= i n) (void)]
264 [else
265 (apply p ($vector-ref v0 i) ($vector-ref v1 i)
266 (let f ([i i] [v* v*])
267 (if (null? v*)
268 '()
269 (cons ($vector-ref ($car v*) i)
270 (f i ($cdr v*))))))
271 (f p v0 v1 v* ($fxadd1 i) n)])))])))
272
273 (define (vector-fill! v fill)
274 (unless (vector? v)
275 (die 'vector-fill! "not a vector" v))
276 (let f ([v v] [i 0] [n ($vector-length v)] [fill fill])
277 (unless ($fx= i n)
278 ($vector-set! v i fill)
279 (f v ($fxadd1 i) n fill))))
280
281 )
282
283
284 (library (ikarus system vectors)
285 (export $vector-ref $vector-length)
286 (import (ikarus))
287 (define $vector-ref vector-ref)
288 (define $vector-length vector-length))
289