Debian Codegraph @

%3a43/vector-lib.scm

raw symbols SCIP
1 ;;;;;; SRFI 43: Vector library -*- Scheme -*-
2 ;;;
3 ;;; $Id: vector-lib.scm,v 1.7 2009/03/29 09:46:03 sperber Exp $
4 ;;;
5 ;;; Taylor Campbell wrote this code; he places it in the public domain.
6 ;;; Will Clinger [wdc] made some corrections, also in the public domain.
7
8 ;;; --------------------
9 ;;; Exported procedure index
10 ;;;
11 ;;; * Constructors
12 ;;; make-vector vector
13 ;;; vector-unfold vector-unfold-right
14 ;;; vector-copy vector-reverse-copy
15 ;;; vector-append vector-concatenate
16 ;;;
17 ;;; * Predicates
18 ;;; vector?
19 ;;; vector-empty?
20 ;;; vector=
21 ;;;
22 ;;; * Selectors
23 ;;; vector-ref
24 ;;; vector-length
25 ;;;
26 ;;; * Iteration
27 ;;; vector-fold vector-fold-right
28 ;;; vector-map vector-map!
29 ;;; vector-for-each
30 ;;; vector-count
31 ;;;
32 ;;; * Searching
33 ;;; vector-index vector-skip
34 ;;; vector-index-right vector-skip-right
35 ;;; vector-binary-search
36 ;;; vector-any vector-every
37 ;;;
38 ;;; * Mutators
39 ;;; vector-set!
40 ;;; vector-swap!
41 ;;; vector-fill!
42 ;;; vector-reverse!
43 ;;; vector-copy! vector-reverse-copy!
44 ;;; vector-reverse!
45 ;;;
46 ;;; * Conversion
47 ;;; vector->list reverse-vector->list
48 ;;; list->vector reverse-list->vector
49
50
51
52 ;;; --------------------
53 ;;; Commentary on efficiency of the code
54
55 ;;; This code is somewhat tuned for efficiency. There are several
56 ;;; internal routines that can be optimized greatly to greatly improve
57 ;;; the performance of much of the library. These internal procedures
58 ;;; are already carefully tuned for performance, and lambda-lifted by
59 ;;; hand. Some other routines are lambda-lifted by hand, but only the
60 ;;; loops are lambda-lifted, and only if some routine has two possible
61 ;;; loops -- a fast path and an n-ary case --, whereas _all_ of the
62 ;;; internal routines' loops are lambda-lifted so as to never cons a
63 ;;; closure in their body (VECTOR-PARSE-START+END doesn't have a loop),
64 ;;; even in Scheme systems that perform no loop optimization (which is
65 ;;; most of them, unfortunately).
66 ;;;
67 ;;; Fast paths are provided for common cases in most of the loops in
68 ;;; this library.
69 ;;;
70 ;;; All calls to primitive vector operations are protected by a prior
71 ;;; type check; they can be safely converted to use unsafe equivalents
72 ;;; of the operations, if available. Ideally, the compiler should be
73 ;;; able to determine this, but the state of Scheme compilers today is
74 ;;; not a happy one.
75 ;;;
76 ;;; Efficiency of the actual algorithms is a rather mundane point to
77 ;;; mention; vector operations are rarely beyond being straightforward.
78
79
80
81 ;;; --------------------
82 ;;; Utilities
83
84 ;;; SRFI 8, too trivial to put in the dependencies list.
85 (define-syntax receive
86 (syntax-rules ()
87 ((receive ?formals ?producer ?body1 ?body2 ...)
88 (call-with-values (lambda () ?producer)
89 (lambda ?formals ?body1 ?body2 ...)))))
90
91 ;;; Not the best LET*-OPTIONALS, but not the worst, either. Use Olin's
92 ;;; if it's available to you.
93 (define-syntax let*-optionals
94 (syntax-rules ()
95 ((let*-optionals (?x ...) ((?var ?default) ...) ?body1 ?body2 ...)
96 (let ((args (?x ...)))
97 (let*-optionals args ((?var ?default) ...) ?body1 ?body2 ...)))
98 ((let*-optionals ?args ((?var ?default) ...) ?body1 ?body2 ...)
99 (let*-optionals:aux ?args ?args ((?var ?default) ...)
100 ?body1 ?body2 ...))))
101
102 (define-syntax let*-optionals:aux
103 (syntax-rules ()
104 ((aux ?orig-args-var ?args-var () ?body1 ?body2 ...)
105 (if (null? ?args-var)
106 (let () ?body1 ?body2 ...)
107 (error "too many arguments" (length ?orig-args-var)
108 ?orig-args-var)))
109 ((aux ?orig-args-var ?args-var
110 ((?var ?default) ?more ...)
111 ?body1 ?body2 ...)
112 (if (null? ?args-var)
113 (let* ((?var ?default) ?more ...) ?body1 ?body2 ...)
114 (let ((?var (car ?args-var))
115 (new-args (cdr ?args-var)))
116 (let*-optionals:aux ?orig-args-var new-args
117 (?more ...)
118 ?body1 ?body2 ...))))))
119
120 (define (nonneg-int? x)
121 (and (integer? x)
122 (not (negative? x))))
123
124 (define (between? x y z)
125 (and (< x y)
126 (<= y z)))
127
128 (define (unspecified-value) (if #f #f))
129
130 ;++ This should be implemented more efficiently. It shouldn't cons a
131 ;++ closure, and the cons cells used in the loops when using this could
132 ;++ be reused.
133 (define (vectors-ref vectors i)
134 (map (lambda (v) (vector-ref v i)) vectors))
135
136
137
138 ;;; --------------------
139 ;;; Error checking
140
141 ;;; Error signalling (not checking) is done in a way that tries to be
142 ;;; as helpful to the person who gets the debugging prompt as possible.
143 ;;; That said, error _checking_ tries to be as unredundant as possible.
144
145 ;;; I don't use any sort of general condition mechanism; I use simply
146 ;;; SRFI 23's ERROR, even in cases where it might be better to use such
147 ;;; a general condition mechanism. Fix that when porting this to a
148 ;;; Scheme implementation that has its own condition system.
149
150 ;;; In argument checks, upon receiving an invalid argument, the checker
151 ;;; procedure recursively calls itself, but in one of the arguments to
152 ;;; itself is a call to ERROR; this mechanism is used in the hopes that
153 ;;; the user may be thrown into a debugger prompt, proceed with another
154 ;;; value, and let it be checked again.
155
156 ;;; Type checking is pretty basic, but easily factored out and replaced
157 ;;; with whatever your implementation's preferred type checking method
158 ;;; is. I doubt there will be many other methods of index checking,
159 ;;; though the index checkers might be better implemented natively.
160
161 ;;; (CHECK-TYPE <type-predicate?> <value> <callee>) -> value
162 ;;; Ensure that VALUE satisfies TYPE-PREDICATE?; if not, signal an
163 ;;; error stating that VALUE did not satisfy TYPE-PREDICATE?, showing
164 ;;; that this happened while calling CALLEE. Return VALUE if no
165 ;;; error was signalled.
166 (define (check-type pred? value callee)
167 (if (pred? value)
168 value
169 ;; Recur: when (or if) the user gets a debugger prompt, he can
170 ;; proceed where the call to ERROR was with the correct value.
171 (check-type pred?
172 (error "erroneous value"
173 (list pred? value)
174 `(while calling ,callee))
175 callee)))
176
177 ;;; (CHECK-INDEX <vector> <index> <callee>) -> index
178 ;;; Ensure that INDEX is a valid index into VECTOR; if not, signal an
179 ;;; error stating that it is not and that this happened in a call to
180 ;;; CALLEE. Return INDEX when it is valid. (Note that this does NOT
181 ;;; check that VECTOR is indeed a vector.)
182 (define (check-index vec index callee)
183 (let ((index (check-type integer? index callee)))
184 (cond ((< index 0)
185 (check-index vec
186 (error "vector index too low"
187 index
188 `(into vector ,vec)
189 `(while calling ,callee))
190 callee))
191 ((>= index (vector-length vec))
192 (check-index vec
193 (error "vector index too high"
194 index
195 `(into vector ,vec)
196 `(while calling ,callee))
197 callee))
198 (else index))))
199
200 ;;; (CHECK-INDICES <vector>
201 ;;; <start> <start-name>
202 ;;; <end> <end-name>
203 ;;; <caller>) -> [start end]
204 ;;; Ensure that START and END are valid bounds of a range within
205 ;;; VECTOR; if not, signal an error stating that they are not, with
206 ;;; the message being informative about what the argument names were
207 ;;; called -- by using START-NAME & END-NAME --, and that it occurred
208 ;;; while calling CALLEE. Also ensure that VEC is in fact a vector.
209 ;;; Returns no useful value.
210 (define (check-indices vec start start-name end end-name callee)
211 (let ((lose (lambda things
212 (apply error "vector range out of bounds"
213 (append things
214 `(vector was ,vec)
215 `(,start-name was ,start)
216 `(,end-name was ,end)
217 `(while calling ,callee)))))
218 (start (check-type integer? start callee))
219 (end (check-type integer? end callee)))
220 (cond ((> start end)
221 ;; I'm not sure how well this will work. The intent is that
222 ;; the programmer tells the debugger to proceed with both a
223 ;; new START & a new END by returning multiple values
224 ;; somewhere.
225 (receive (new-start new-end)
226 (lose `(,end-name < ,start-name))
227 (check-indices vec
228 new-start start-name
229 new-end end-name
230 callee)))
231 ((< start 0)
232 (check-indices vec
233 (lose `(,start-name < 0))
234 start-name
235 end end-name
236 callee))
237 ((>= start (vector-length vec))
238 (check-indices vec
239 (lose `(,start-name > len)
240 `(len was ,(vector-length vec)))
241 start-name
242 end end-name
243 callee))
244 ((> end (vector-length vec))
245 (check-indices vec
246 start start-name
247 (lose `(,end-name > len)
248 `(len was ,(vector-length vec)))
249 end-name
250 callee))
251 (else
252 (values start end)))))
253
254
255
256 ;;; --------------------
257 ;;; Internal routines
258
259 ;;; These should all be integrated, native, or otherwise optimized --
260 ;;; they're used a _lot_ --. All of the loops and LETs inside loops
261 ;;; are lambda-lifted by hand, just so as not to cons closures in the
262 ;;; loops. (If your compiler can do better than that if they're not
263 ;;; lambda-lifted, then lambda-drop (?) them.)
264
265 ;;; (VECTOR-PARSE-START+END <vector> <arguments>
266 ;;; <start-name> <end-name>
267 ;;; <callee>)
268 ;;; -> [start end]
269 ;;; Return two values, composing a valid range within VECTOR, as
270 ;;; extracted from ARGUMENTS or defaulted from VECTOR -- 0 for START
271 ;;; and the length of VECTOR for END --; START-NAME and END-NAME are
272 ;;; purely for error checking.
273 (define (vector-parse-start+end vec args start-name end-name callee)
274 (let ((len (vector-length vec)))
275 (cond ((null? args)
276 (values 0 len))
277 ((null? (cdr args))
278 (check-indices vec
279 (car args) start-name
280 len end-name
281 callee))
282 ((null? (cddr args))
283 (check-indices vec
284 (car args) start-name
285 (cadr args) end-name
286 callee))
287 (else
288 (error "too many arguments"
289 `(extra args were ,(cddr args))
290 `(while calling ,callee))))))
291
292 (define-syntax let-vector-start+end
293 (syntax-rules ()
294 ((let-vector-start+end ?callee ?vec ?args (?start ?end)
295 ?body1 ?body2 ...)
296 (let ((?vec (check-type vector? ?vec ?callee)))
297 (receive (?start ?end)
298 (vector-parse-start+end ?vec ?args '?start '?end
299 ?callee)
300 ?body1 ?body2 ...)))))
301
302 ;;; (%SMALLEST-LENGTH <vector-list> <default-length> <callee>)
303 ;;; -> exact, nonnegative integer
304 ;;; Compute the smallest length of VECTOR-LIST. DEFAULT-LENGTH is
305 ;;; the length that is returned if VECTOR-LIST is empty. Common use
306 ;;; of this is in n-ary vector routines:
307 ;;; (define (f vec . vectors)
308 ;;; (let ((vec (check-type vector? vec f)))
309 ;;; ...(%smallest-length vectors (vector-length vec) f)...))
310 ;;; %SMALLEST-LENGTH takes care of the type checking -- which is what
311 ;;; the CALLEE argument is for --; thus, the design is tuned for
312 ;;; avoiding redundant type checks.
313 (define %smallest-length
314 (letrec ((loop (lambda (vector-list length callee)
315 (if (null? vector-list)
316 length
317 (loop (cdr vector-list)
318 (min (vector-length
319 (check-type vector?
320 (car vector-list)
321 callee))
322 length)
323 callee)))))
324 loop))
325
326 ;;; (%VECTOR-COPY! <target> <tstart> <source> <sstart> <send>)
327 ;;; Copy elements at locations SSTART to SEND from SOURCE to TARGET,
328 ;;; starting at TSTART in TARGET.
329 ;;;
330 ;;; Optimize this! Probably with some combination of:
331 ;;; - Force it to be integrated.
332 ;;; - Let it use unsafe vector element dereferencing routines: bounds
333 ;;; checking already happens outside of it. (Or use a compiler
334 ;;; that figures this out, but Olin Shivers' PhD thesis seems to
335 ;;; have been largely ignored in actual implementations...)
336 ;;; - Implement it natively as a VM primitive: the VM can undoubtedly
337 ;;; perform much faster than it can make Scheme perform, even with
338 ;;; bounds checking.
339 ;;; - Implement it in assembly: you _want_ the fine control that
340 ;;; assembly can give you for this.
341 ;;; I already lambda-lift it by hand, but you should be able to make it
342 ;;; even better than that.
343 (define %vector-copy!
344 (letrec ((loop/l->r (lambda (target source send i j)
345 (cond ((< i send)
346 (vector-set! target j
347 (vector-ref source i))
348 (loop/l->r target source send
349 (+ i 1) (+ j 1))))))
350 (loop/r->l (lambda (target source sstart i j)
351 (cond ((>= i sstart)
352 (vector-set! target j
353 (vector-ref source i))
354 (loop/r->l target source sstart
355 (- i 1) (- j 1)))))))
356 (lambda (target tstart source sstart send)
357 (if (> sstart tstart) ; Make sure we don't copy over
358 ; ourselves.
359 (loop/l->r target source send sstart tstart)
360 (loop/r->l target source sstart (- send 1)
361 (+ -1 tstart send (- sstart)))))))
362
363 ;;; (%VECTOR-REVERSE-COPY! <target> <tstart> <source> <sstart> <send>)
364 ;;; Copy elements from SSTART to SEND from SOURCE to TARGET, in the
365 ;;; reverse order.
366 (define %vector-reverse-copy!
367 (letrec ((loop (lambda (target source sstart i j)
368 (cond ((>= i sstart)
369 (vector-set! target j (vector-ref source i))
370 (loop target source sstart
371 (- i 1)
372 (+ j 1)))))))
373 (lambda (target tstart source sstart send)
374 (loop target source sstart
375 (- send 1)
376 tstart))))
377
378 ;;; (%VECTOR-REVERSE! <vector>)
379 (define %vector-reverse!
380 (letrec ((loop (lambda (vec i j)
381 (cond ((<= i j)
382 (let ((v (vector-ref vec i)))
383 (vector-set! vec i (vector-ref vec j))
384 (vector-set! vec j v)
385 (loop vec (+ i 1) (- j 1))))))))
386 (lambda (vec start end)
387 (loop vec start (- end 1)))))
388
389 ;;; (%VECTOR-FOLD1 <kons> <knil> <vector>) -> knil'
390 ;;; (KONS <index> <knil> <elt>) -> knil'
391 (define %vector-fold1
392 (letrec ((loop (lambda (kons knil len vec i)
393 (if (= i len)
394 knil
395 (loop kons
396 (kons i knil (vector-ref vec i))
397 len vec (+ i 1))))))
398 (lambda (kons knil len vec)
399 (loop kons knil len vec 0))))
400
401 ;;; (%VECTOR-FOLD2+ <kons> <knil> <vector> ...) -> knil'
402 ;;; (KONS <index> <knil> <elt> ...) -> knil'
403 (define %vector-fold2+
404 (letrec ((loop (lambda (kons knil len vectors i)
405 (if (= i len)
406 knil
407 (loop kons
408 (apply kons i knil
409 (vectors-ref vectors i))
410 len vectors (+ i 1))))))
411 (lambda (kons knil len vectors)
412 (loop kons knil len vectors 0))))
413
414 ;;; (%VECTOR-MAP! <f> <target> <length> <vector>) -> target
415 ;;; (F <index> <elt>) -> elt'
416 (define %vector-map1!
417 (letrec ((loop (lambda (f target vec i)
418 (if (zero? i)
419 target
420 (let ((j (- i 1)))
421 (vector-set! target j
422 (f j (vector-ref vec j)))
423 (loop f target vec j))))))
424 (lambda (f target vec len)
425 (loop f target vec len))))
426
427 ;;; (%VECTOR-MAP2+! <f> <target> <vectors> <len>) -> target
428 ;;; (F <index> <elt> ...) -> elt'
429 (define %vector-map2+!
430 (letrec ((loop (lambda (f target vectors i)
431 (if (zero? i)
432 target
433 (let ((j (- i 1)))
434 (vector-set! target j
435 (apply f j (vectors-ref vectors j)))
436 (loop f target vectors j))))))
437 (lambda (f target vectors len)
438 (loop f target vectors len))))
439
440
441
442 ;;;;;;;;;;;;;;;;;;;;;;;; ***** vector-lib ***** ;;;;;;;;;;;;;;;;;;;;;;;
443
444 ;;; --------------------
445 ;;; Constructors
446
447 ;;; (MAKE-VECTOR <size> [<fill>]) -> vector
448 ;;; [R5RS] Create a vector of length LENGTH. If FILL is present,
449 ;;; initialize each slot in the vector with it; if not, the vector's
450 ;;; initial contents are unspecified.
451 (define make-vector make-vector)
452
453 ;;; (VECTOR <elt> ...) -> vector
454 ;;; [R5RS] Create a vector containing ELEMENT ..., in order.
455 (define vector vector)
456
457 ;;; This ought to be able to be implemented much more efficiently -- if
458 ;;; we have the number of arguments available to us, we can create the
459 ;;; vector without using LENGTH to determine the number of elements it
460 ;;; should have.
461 ;(define (vector . elements) (list->vector elements))
462
463 ;;; (VECTOR-UNFOLD <f> <length> <initial-seed> ...) -> vector
464 ;;; (F <index> <seed> ...) -> [elt seed' ...]
465 ;;; The fundamental vector constructor. Creates a vector whose
466 ;;; length is LENGTH and iterates across each index K between 0 and
467 ;;; LENGTH, applying F at each iteration to the current index and the
468 ;;; current seeds to receive N+1 values: first, the element to put in
469 ;;; the Kth slot and then N new seeds for the next iteration.
470 (define vector-unfold
471 (letrec ((tabulate! ; Special zero-seed case.
472 (lambda (f vec i len)
473 (cond ((< i len)
474 (vector-set! vec i (f i))
475 (tabulate! f vec (+ i 1) len)))))
476 (unfold1! ; Fast path for one seed.
477 (lambda (f vec i len seed)
478 (if (< i len)
479 (receive (elt new-seed)
480 (f i seed)
481 (vector-set! vec i elt)
482 (unfold1! f vec (+ i 1) len new-seed)))))
483 (unfold2+! ; Slower variant for N seeds.
484 (lambda (f vec i len seeds)
485 (if (< i len)
486 (receive (elt . new-seeds)
487 (apply f i seeds)
488 (vector-set! vec i elt)
489 (unfold2+! f vec (+ i 1) len new-seeds))))))
490 (lambda (f len . initial-seeds)
491 (let ((f (check-type procedure? f vector-unfold))
492 (len (check-type nonneg-int? len vector-unfold)))
493 (let ((vec (make-vector len)))
494 (cond ((null? initial-seeds)
495 (tabulate! f vec 0 len))
496 ((null? (cdr initial-seeds))
497 (unfold1! f vec 0 len (car initial-seeds)))
498 (else
499 (unfold2+! f vec 0 len initial-seeds)))
500 vec)))))
501
502 ;;; (VECTOR-UNFOLD-RIGHT <f> <length> <initial-seed> ...) -> vector
503 ;;; (F <seed> ...) -> [seed' ...]
504 ;;; Like VECTOR-UNFOLD, but it generates elements from LENGTH to 0
505 ;;; (still exclusive with LENGTH and inclusive with 0), not 0 to
506 ;;; LENGTH as with VECTOR-UNFOLD.
507 (define vector-unfold-right
508 (letrec ((tabulate!
509 (lambda (f vec i)
510 (cond ((>= i 0)
511 (vector-set! vec i (f i))
512 (tabulate! f vec (- i 1))))))
513 (unfold1!
514 (lambda (f vec i seed)
515 (if (>= i 0)
516 (receive (elt new-seed)
517 (f i seed)
518 (vector-set! vec i elt)
519 (unfold1! f vec (- i 1) new-seed)))))
520 (unfold2+!
521 (lambda (f vec i seeds)
522 (if (>= i 0)
523 (receive (elt . new-seeds)
524 (apply f i seeds)
525 (vector-set! vec i elt)
526 (unfold2+! f vec (- i 1) new-seeds))))))
527 (lambda (f len . initial-seeds)
528 (let ((f (check-type procedure? f vector-unfold-right))
529 (len (check-type nonneg-int? len vector-unfold-right)))
530 (let ((vec (make-vector len))
531 (i (- len 1)))
532 (cond ((null? initial-seeds)
533 (tabulate! f vec i))
534 ((null? (cdr initial-seeds))
535 (unfold1! f vec i (car initial-seeds)))
536 (else
537 (unfold2+! f vec i initial-seeds)))
538 vec)))))
539
540 ;;; (VECTOR-COPY <vector> [<start> <end> <fill>]) -> vector
541 ;;; Create a newly allocated vector containing the elements from the
542 ;;; range [START,END) in VECTOR. START defaults to 0; END defaults
543 ;;; to the length of VECTOR. END may be greater than the length of
544 ;;; VECTOR, in which case the vector is enlarged; if FILL is passed,
545 ;;; the new locations from which there is no respective element in
546 ;;; VECTOR are filled with FILL.
547 (define (vector-copy vec . args)
548 (let ((vec (check-type vector? vec vector-copy)))
549 ;; We can't use LET-VECTOR-START+END, because we have one more
550 ;; argument, and we want finer control, too.
551 ;;
552 ;; Olin's implementation of LET*-OPTIONALS would prove useful here:
553 ;; the built-in argument-checks-as-you-go-along produces almost
554 ;; _exactly_ the same code as VECTOR-COPY:PARSE-ARGS.
555 (receive (start end fill)
556 (vector-copy:parse-args vec args)
557 (let ((new-vector (make-vector (- end start) fill)))
558 (%vector-copy! new-vector 0
559 vec start
560 (if (> end (vector-length vec))
561 (vector-length vec)
562 end))
563 new-vector))))
564
565 ;;; Auxiliary for VECTOR-COPY.
566 ;;; [wdc] Corrected to allow 0 <= start <= (vector-length vec).
567 (define (vector-copy:parse-args vec args)
568 (define (parse-args start end n fill)
569 (let ((start (check-type nonneg-int? start vector-copy))
570 (end (check-type nonneg-int? end vector-copy)))
571 (cond ((and (<= 0 start end)
572 (<= start n))
573 (values start end fill))
574 (else
575 (error "illegal arguments"
576 `(while calling ,vector-copy)
577 `(start was ,start)
578 `(end was ,end)
579 `(vector was ,vec))))))
580 (let ((n (vector-length vec)))
581 (cond ((null? args)
582 (parse-args 0 n n (unspecified-value)))
583 ((null? (cdr args))
584 (parse-args (car args) n n (unspecified-value)))
585 ((null? (cddr args))
586 (parse-args (car args) (cadr args) n (unspecified-value)))
587 ((null? (cdddr args))
588 (parse-args (car args) (cadr args) n (caddr args)))
589 (else
590 (error "too many arguments"
591 vector-copy
592 (cdddr args))))))
593
594 ;;; (VECTOR-REVERSE-COPY <vector> [<start> <end>]) -> vector
595 ;;; Create a newly allocated vector whose elements are the reversed
596 ;;; sequence of elements between START and END in VECTOR. START's
597 ;;; default is 0; END's default is the length of VECTOR.
598 (define (vector-reverse-copy vec . maybe-start+end)
599 (let-vector-start+end vector-reverse-copy vec maybe-start+end
600 (start end)
601 (let ((new (make-vector (- end start))))
602 (%vector-reverse-copy! new 0 vec start end)
603 new)))
604
605 ;;; (VECTOR-APPEND <vector> ...) -> vector
606 ;;; Append VECTOR ... into a newly allocated vector and return that
607 ;;; new vector.
608 (define (vector-append . vectors)
609 (vector-concatenate:aux vectors vector-append))
610
611 ;;; (VECTOR-CONCATENATE <vector-list>) -> vector
612 ;;; Concatenate the vectors in VECTOR-LIST. This is equivalent to
613 ;;; (apply vector-append VECTOR-LIST)
614 ;;; but VECTOR-APPEND tends to be implemented in terms of
615 ;;; VECTOR-CONCATENATE, and some Schemes bork when the list to apply
616 ;;; a function to is too long.
617 ;;;
618 ;;; Actually, they're both implemented in terms of an internal routine.
619 (define (vector-concatenate vector-list)
620 (vector-concatenate:aux vector-list vector-concatenate))
621
622 ;;; Auxiliary for VECTOR-APPEND and VECTOR-CONCATENATE
623 (define vector-concatenate:aux
624 (letrec ((compute-length
625 (lambda (vectors len callee)
626 (if (null? vectors)
627 len
628 (let ((vec (check-type vector? (car vectors)
629 callee)))
630 (compute-length (cdr vectors)
631 (+ (vector-length vec) len)
632 callee)))))
633 (concatenate!
634 (lambda (vectors target to)
635 (if (null? vectors)
636 target
637 (let* ((vec1 (car vectors))
638 (len (vector-length vec1)))
639 (%vector-copy! target to vec1 0 len)
640 (concatenate! (cdr vectors) target
641 (+ to len)))))))
642 (lambda (vectors callee)
643 (cond ((null? vectors) ;+++
644 (make-vector 0))
645 ((null? (cdr vectors)) ;+++
646 ;; Blech, we still have to allocate a new one.
647 (let* ((vec (check-type vector? (car vectors) callee))
648 (len (vector-length vec))
649 (new (make-vector len)))
650 (%vector-copy! new 0 vec 0 len)
651 new))
652 (else
653 (let ((new-vector
654 (make-vector (compute-length vectors 0 callee))))
655 (concatenate! vectors new-vector 0)
656 new-vector))))))
657
658
659
660 ;;; --------------------
661 ;;; Predicates
662
663 ;;; (VECTOR? <value>) -> boolean
664 ;;; [R5RS] Return #T if VALUE is a vector and #F if not.
665 (define vector? vector?)
666
667 ;;; (VECTOR-EMPTY? <vector>) -> boolean
668 ;;; Return #T if VECTOR has zero elements in it, i.e. VECTOR's length
669 ;;; is 0, and #F if not.
670 (define (vector-empty? vec)
671 (let ((vec (check-type vector? vec vector-empty?)))
672 (zero? (vector-length vec))))
673
674 ;;; (VECTOR= <elt=?> <vector> ...) -> boolean
675 ;;; (ELT=? <value> <value>) -> boolean
676 ;;; Determine vector equality generalized across element comparators.
677 ;;; Vectors A and B are equal iff their lengths are the same and for
678 ;;; each respective elements E_a and E_b (element=? E_a E_b) returns
679 ;;; a true value. ELT=? is always applied to two arguments. Element
680 ;;; comparison must be consistent wtih EQ?; that is, if (eq? E_a E_b)
681 ;;; results in a true value, then (ELEMENT=? E_a E_b) must result in a
682 ;;; true value. This may be exploited to avoid multiple unnecessary
683 ;;; element comparisons. (This implementation does, but does not deal
684 ;;; with the situation that ELEMENT=? is EQ? to avoid more unnecessary
685 ;;; comparisons, but I believe this optimization is probably fairly
686 ;;; insignificant.)
687 ;;;
688 ;;; If the number of vector arguments is zero or one, then #T is
689 ;;; automatically returned. If there are N vector arguments,
690 ;;; VECTOR_1 VECTOR_2 ... VECTOR_N, then VECTOR_1 & VECTOR_2 are
691 ;;; compared; if they are equal, the vectors VECTOR_2 ... VECTOR_N
692 ;;; are compared. The precise order in which ELT=? is applied is not
693 ;;; specified.
694 (define (vector= elt=? . vectors)
695 (let ((elt=? (check-type procedure? elt=? vector=)))
696 (cond ((null? vectors)
697 #t)
698 ((null? (cdr vectors))
699 (check-type vector? (car vectors) vector=)
700 #t)
701 (else
702 (let loop ((vecs vectors))
703 (let ((vec1 (check-type vector? (car vecs) vector=))
704 (vec2+ (cdr vecs)))
705 (or (null? vec2+)
706 (and (binary-vector= elt=? vec1 (car vec2+))
707 (loop vec2+)))))))))
708 (define (binary-vector= elt=? vector-a vector-b)
709 (or (eq? vector-a vector-b) ;+++
710 (let ((length-a (vector-length vector-a))
711 (length-b (vector-length vector-b)))
712 (letrec ((loop (lambda (i)
713 (or (= i length-a)
714 (and (< i length-b)
715 (test (vector-ref vector-a i)
716 (vector-ref vector-b i)
717 i)))))
718 (test (lambda (elt-a elt-b i)
719 (and (or (eq? elt-a elt-b) ;+++
720 (elt=? elt-a elt-b))
721 (loop (+ i 1))))))
722 (and (= length-a length-b)
723 (loop 0))))))
724
725
726
727 ;;; --------------------
728 ;;; Selectors
729
730 ;;; (VECTOR-REF <vector> <index>) -> value
731 ;;; [R5RS] Return the value that the location in VECTOR at INDEX is
732 ;;; mapped to in the store.
733 (define vector-ref vector-ref)
734
735 ;;; (VECTOR-LENGTH <vector>) -> exact, nonnegative integer
736 ;;; [R5RS] Return the length of VECTOR.
737 (define vector-length vector-length)
738
739
740
741 ;;; --------------------
742 ;;; Iteration
743
744 ;;; (VECTOR-FOLD <kons> <initial-knil> <vector> ...) -> knil
745 ;;; (KONS <knil> <elt> ...) -> knil' ; N vectors -> N+1 args
746 ;;; The fundamental vector iterator. KONS is iterated over each
747 ;;; index in all of the vectors in parallel, stopping at the end of
748 ;;; the shortest; KONS is applied to an argument list of (list I
749 ;;; STATE (vector-ref VEC I) ...), where STATE is the current state
750 ;;; value -- the state value begins with KNIL and becomes whatever
751 ;;; KONS returned at the respective iteration --, and I is the
752 ;;; current index in the iteration. The iteration is strictly left-
753 ;;; to-right.
754 ;;; (vector-fold KONS KNIL (vector E_1 E_2 ... E_N))
755 ;;; <=>
756 ;;; (KONS (... (KONS (KONS KNIL E_1) E_2) ... E_N-1) E_N)
757 (define (vector-fold kons knil vec . vectors)
758 (let ((kons (check-type procedure? kons vector-fold))
759 (vec (check-type vector? vec vector-fold)))
760 (if (null? vectors)
761 (%vector-fold1 kons knil (vector-length vec) vec)
762 (%vector-fold2+ kons knil
763 (%smallest-length vectors
764 (vector-length vec)
765 vector-fold)
766 (cons vec vectors)))))
767
768 ;;; (VECTOR-FOLD-RIGHT <kons> <initial-knil> <vector> ...) -> knil
769 ;;; (KONS <knil> <elt> ...) -> knil' ; N vectors => N+1 args
770 ;;; The fundamental vector recursor. Iterates in parallel across
771 ;;; VECTOR ... right to left, applying KONS to the elements and the
772 ;;; current state value; the state value becomes what KONS returns
773 ;;; at each next iteration. KNIL is the initial state value.
774 ;;; (vector-fold-right KONS KNIL (vector E_1 E_2 ... E_N))
775 ;;; <=>
776 ;;; (KONS (... (KONS (KONS KNIL E_N) E_N-1) ... E_2) E_1)
777 ;;;
778 ;;; Not implemented in terms of a more primitive operations that might
779 ;;; called %VECTOR-FOLD-RIGHT due to the fact that it wouldn't be very
780 ;;; useful elsewhere.
781 (define vector-fold-right
782 (letrec ((loop1 (lambda (kons knil vec i)
783 (if (negative? i)
784 knil
785 (loop1 kons (kons i knil (vector-ref vec i))
786 vec
787 (- i 1)))))
788 (loop2+ (lambda (kons knil vectors i)
789 (if (negative? i)
790 knil
791 (loop2+ kons
792 (apply kons i knil
793 (vectors-ref vectors i))
794 vectors
795 (- i 1))))))
796 (lambda (kons knil vec . vectors)
797 (let ((kons (check-type procedure? kons vector-fold-right))
798 (vec (check-type vector? vec vector-fold-right)))
799 (if (null? vectors)
800 (loop1 kons knil vec (- (vector-length vec) 1))
801 (loop2+ kons knil (cons vec vectors)
802 (- (%smallest-length vectors
803 (vector-length vec)
804 vector-fold-right)
805 1)))))))
806
807 ;;; (VECTOR-MAP <f> <vector> ...) -> vector
808 ;;; (F <elt> ...) -> value ; N vectors -> N args
809 ;;; Constructs a new vector of the shortest length of the vector
810 ;;; arguments. Each element at index I of the new vector is mapped
811 ;;; from the old vectors by (F I (vector-ref VECTOR I) ...). The
812 ;;; dynamic order of application of F is unspecified.
813 (define (vector-map f vec . vectors)
814 (let ((f (check-type procedure? f vector-map))
815 (vec (check-type vector? vec vector-map)))
816 (if (null? vectors)
817 (let ((len (vector-length vec)))
818 (%vector-map1! f (make-vector len) vec len))
819 (let ((len (%smallest-length vectors
820 (vector-length vec)
821 vector-map)))
822 (%vector-map2+! f (make-vector len) (cons vec vectors)
823 len)))))
824
825 ;;; (VECTOR-MAP! <f> <vector> ...) -> unspecified
826 ;;; (F <elt> ...) -> element' ; N vectors -> N args
827 ;;; Similar to VECTOR-MAP, but rather than mapping the new elements
828 ;;; into a new vector, the new mapped elements are destructively
829 ;;; inserted into the first vector. Again, the dynamic order of
830 ;;; application of F is unspecified, so it is dangerous for F to
831 ;;; manipulate the first VECTOR.
832 (define (vector-map! f vec . vectors)
833 (let ((f (check-type procedure? f vector-map!))
834 (vec (check-type vector? vec vector-map!)))
835 (if (null? vectors)
836 (%vector-map1! f vec vec (vector-length vec))
837 (%vector-map2+! f vec (cons vec vectors)
838 (%smallest-length vectors
839 (vector-length vec)
840 vector-map!)))
841 (unspecified-value)))
842
843 ;;; (VECTOR-FOR-EACH <f> <vector> ...) -> unspecified
844 ;;; (F <elt> ...) ; N vectors -> N args
845 ;;; Simple vector iterator: applies F to each index in the range [0,
846 ;;; LENGTH), where LENGTH is the length of the smallest vector
847 ;;; argument passed, and the respective element at that index. In
848 ;;; contrast with VECTOR-MAP, F is reliably applied to each
849 ;;; subsequent elements, starting at index 0 from left to right, in
850 ;;; the vectors.
851 (define vector-for-each
852 (letrec ((for-each1
853 (lambda (f vec i len)
854 (cond ((< i len)
855 (f i (vector-ref vec i))
856 (for-each1 f vec (+ i 1) len)))))
857 (for-each2+
858 (lambda (f vecs i len)
859 (cond ((< i len)
860 (apply f i (vectors-ref vecs i))
861 (for-each2+ f vecs (+ i 1) len))))))
862 (lambda (f vec . vectors)
863 (let ((f (check-type procedure? f vector-for-each))
864 (vec (check-type vector? vec vector-for-each)))
865 (if (null? vectors)
866 (for-each1 f vec 0 (vector-length vec))
867 (for-each2+ f (cons vec vectors) 0
868 (%smallest-length vectors
869 (vector-length vec)
870 vector-for-each)))))))
871
872 ;;; (VECTOR-COUNT <predicate?> <vector> ...)
873 ;;; -> exact, nonnegative integer
874 ;;; (PREDICATE? <index> <value> ...) ; N vectors -> N+1 args
875 ;;; PREDICATE? is applied element-wise to the elements of VECTOR ...,
876 ;;; and a count is tallied of the number of elements for which a
877 ;;; true value is produced by PREDICATE?. This count is returned.
878 (define (vector-count pred? vec . vectors)
879 (let ((pred? (check-type procedure? pred? vector-count))
880 (vec (check-type vector? vec vector-count)))
881 (if (null? vectors)
882 (%vector-fold1 (lambda (index count elt)
883 (if (pred? index elt)
884 (+ count 1)
885 count))
886 0
887 (vector-length vec)
888 vec)
889 (%vector-fold2+ (lambda (index count . elts)
890 (if (apply pred? index elts)
891 (+ count 1)
892 count))
893 0
894 (%smallest-length vectors
895 (vector-length vec)
896 vector-count)
897 (cons vec vectors)))))
898
899
900
901 ;;; --------------------
902 ;;; Searching
903
904 ;;; (VECTOR-INDEX <predicate?> <vector> ...)
905 ;;; -> exact, nonnegative integer or #F
906 ;;; (PREDICATE? <elt> ...) -> boolean ; N vectors -> N args
907 ;;; Search left-to-right across VECTOR ... in parallel, returning the
908 ;;; index of the first set of values VALUE ... such that (PREDICATE?
909 ;;; VALUE ...) returns a true value; if no such set of elements is
910 ;;; reached, return #F.
911 (define (vector-index pred? vec . vectors)
912 (vector-index/skip pred? vec vectors vector-index))
913
914 ;;; (VECTOR-SKIP <predicate?> <vector> ...)
915 ;;; -> exact, nonnegative integer or #F
916 ;;; (PREDICATE? <elt> ...) -> boolean ; N vectors -> N args
917 ;;; (vector-index (lambda elts (not (apply PREDICATE? elts)))
918 ;;; VECTOR ...)
919 ;;; Like VECTOR-INDEX, but find the index of the first set of values
920 ;;; that do _not_ satisfy PREDICATE?.
921 (define (vector-skip pred? vec . vectors)
922 (vector-index/skip (lambda elts (not (apply pred? elts)))
923 vec vectors
924 vector-skip))
925
926 ;;; Auxiliary for VECTOR-INDEX & VECTOR-SKIP
927 (define vector-index/skip
928 (letrec ((loop1 (lambda (pred? vec len i)
929 (cond ((= i len) #f)
930 ((pred? (vector-ref vec i)) i)
931 (else (loop1 pred? vec len (+ i 1))))))
932 (loop2+ (lambda (pred? vectors len i)
933 (cond ((= i len) #f)
934 ((apply pred? (vectors-ref vectors i)) i)
935 (else (loop2+ pred? vectors len
936 (+ i 1)))))))
937 (lambda (pred? vec vectors callee)
938 (let ((pred? (check-type procedure? pred? callee))
939 (vec (check-type vector? vec callee)))
940 (if (null? vectors)
941 (loop1 pred? vec (vector-length vec) 0)
942 (loop2+ pred? (cons vec vectors)
943 (%smallest-length vectors
944 (vector-length vec)
945 callee)
946 0))))))
947
948 ;;; (VECTOR-INDEX-RIGHT <predicate?> <vector> ...)
949 ;;; -> exact, nonnegative integer or #F
950 ;;; (PREDICATE? <elt> ...) -> boolean ; N vectors -> N args
951 ;;; Right-to-left variant of VECTOR-INDEX.
952 (define (vector-index-right pred? vec . vectors)
953 (vector-index/skip-right pred? vec vectors vector-index-right))
954
955 ;;; (VECTOR-SKIP-RIGHT <predicate?> <vector> ...)
956 ;;; -> exact, nonnegative integer or #F
957 ;;; (PREDICATE? <elt> ...) -> boolean ; N vectors -> N args
958 ;;; Right-to-left variant of VECTOR-SKIP.
959 (define (vector-skip-right pred? vec . vectors)
960 (vector-index/skip-right (lambda elts (not (apply pred? elts)))
961 vec vectors
962 vector-index-right))
963
964 (define vector-index/skip-right
965 (letrec ((loop1 (lambda (pred? vec i)
966 (cond ((negative? i) #f)
967 ((pred? (vector-ref vec i)) i)
968 (else (loop1 pred? vec (- i 1))))))
969 (loop2+ (lambda (pred? vectors i)
970 (cond ((negative? i) #f)
971 ((apply pred? (vectors-ref vectors i)) i)
972 (else (loop2+ pred? vectors (- i 1)))))))
973 (lambda (pred? vec vectors callee)
974 (let ((pred? (check-type procedure? pred? callee))
975 (vec (check-type vector? vec callee)))
976 (if (null? vectors)
977 (loop1 pred? vec (- (vector-length vec) 1))
978 (loop2+ pred? (cons vec vectors)
979 (- (%smallest-length vectors
980 (vector-length vec)
981 callee)
982 1)))))))
983
984 ;;; (VECTOR-BINARY-SEARCH <vector> <value> <cmp> [<start> <end>])
985 ;;; -> exact, nonnegative integer or #F
986 ;;; (CMP <value1> <value2>) -> integer
987 ;;; positive -> VALUE1 > VALUE2
988 ;;; zero -> VALUE1 = VALUE2
989 ;;; negative -> VALUE1 < VALUE2
990 ;;; Perform a binary search through VECTOR for VALUE, comparing each
991 ;;; element to VALUE with CMP.
992 (define (vector-binary-search vec value cmp . maybe-start+end)
993 (let ((cmp (check-type procedure? cmp vector-binary-search)))
994 (let-vector-start+end vector-binary-search vec maybe-start+end
995 (start end)
996 (let loop ((start start) (end end) (j #f))
997 (let ((i (quotient (+ start end) 2)))
998 (if (or (= start end) (and j (= i j)))
999 #f
1000 (let ((comparison
1001 (check-type integer?
1002 (cmp (vector-ref vec i) value)
1003 `(,cmp for ,vector-binary-search))))
1004 (cond ((zero? comparison) i)
1005 ((positive? comparison) (loop start i i))
1006 (else (loop i end i))))))))))
1007
1008 ;;; (VECTOR-ANY <pred?> <vector> ...) -> value
1009 ;;; Apply PRED? to each parallel element in each VECTOR ...; if PRED?
1010 ;;; should ever return a true value, immediately stop and return that
1011 ;;; value; otherwise, when the shortest vector runs out, return #F.
1012 ;;; The iteration and order of application of PRED? across elements
1013 ;;; is of the vectors is strictly left-to-right.
1014 (define vector-any
1015 (letrec ((loop1 (lambda (pred? vec i len len-1)
1016 (and (not (= i len))
1017 (if (= i len-1)
1018 (pred? (vector-ref vec i))
1019 (or (pred? (vector-ref vec i))
1020 (loop1 pred? vec (+ i 1)
1021 len len-1))))))
1022 (loop2+ (lambda (pred? vectors i len len-1)
1023 (and (not (= i len))
1024 (if (= i len-1)
1025 (apply pred? (vectors-ref vectors i))
1026 (or (apply pred? (vectors-ref vectors i))
1027 (loop2+ pred? vectors (+ i 1)
1028 len len-1)))))))
1029 (lambda (pred? vec . vectors)
1030 (let ((pred? (check-type procedure? pred? vector-any))
1031 (vec (check-type vector? vec vector-any)))
1032 (if (null? vectors)
1033 (let ((len (vector-length vec)))
1034 (loop1 pred? vec 0 len (- len 1)))
1035 (let ((len (%smallest-length vectors
1036 (vector-length vec)
1037 vector-any)))
1038 (loop2+ pred? (cons vec vectors) 0 len (- len 1))))))))
1039
1040 ;;; (VECTOR-EVERY <pred?> <vector> ...) -> value
1041 ;;; Apply PRED? to each parallel value in each VECTOR ...; if PRED?
1042 ;;; should ever return #F, immediately stop and return #F; otherwise,
1043 ;;; if PRED? should return a true value for each element, stopping at
1044 ;;; the end of the shortest vector, return the last value that PRED?
1045 ;;; returned. In the case that there is an empty vector, return #T.
1046 ;;; The iteration and order of application of PRED? across elements
1047 ;;; is of the vectors is strictly left-to-right.
1048 (define vector-every
1049 (letrec ((loop1 (lambda (pred? vec i len len-1)
1050 (or (= i len)
1051 (if (= i len-1)
1052 (pred? (vector-ref vec i))
1053 (and (pred? (vector-ref vec i))
1054 (loop1 pred? vec (+ i 1)
1055 len len-1))))))
1056 (loop2+ (lambda (pred? vectors i len len-1)
1057 (or (= i len)
1058 (if (= i len-1)
1059 (apply pred? (vectors-ref vectors i))
1060 (and (apply pred? (vectors-ref vectors i))
1061 (loop2+ pred? vectors (+ i 1)
1062 len len-1)))))))
1063 (lambda (pred? vec . vectors)
1064 (let ((pred? (check-type procedure? pred? vector-every))
1065 (vec (check-type vector? vec vector-every)))
1066 (if (null? vectors)
1067 (let ((len (vector-length vec)))
1068 (loop1 pred? vec 0 len (- len 1)))
1069 (let ((len (%smallest-length vectors
1070 (vector-length vec)
1071 vector-every)))
1072 (loop2+ pred? (cons vec vectors) 0 len (- len 1))))))))
1073
1074
1075
1076 ;;; --------------------
1077 ;;; Mutators
1078
1079 ;;; (VECTOR-SET! <vector> <index> <value>) -> unspecified
1080 ;;; [R5RS] Assign the location at INDEX in VECTOR to VALUE.
1081 (define vector-set! vector-set!)
1082
1083 ;;; (VECTOR-SWAP! <vector> <index1> <index2>) -> unspecified
1084 ;;; Swap the values in the locations at INDEX1 and INDEX2.
1085 (define (vector-swap! vec i j)
1086 (let ((vec (check-type vector? vec vector-swap!)))
1087 (let ((i (check-index vec i vector-swap!))
1088 (j (check-index vec j vector-swap!)))
1089 (let ((x (vector-ref vec i)))
1090 (vector-set! vec i (vector-ref vec j))
1091 (vector-set! vec j x)))))
1092
1093 ;;; (VECTOR-FILL! <vector> <value> [<start> <end>]) -> unspecified
1094 ;;; [R5RS+] Fill the locations in VECTOR between START, whose default
1095 ;;; is 0, and END, whose default is the length of VECTOR, with VALUE.
1096 ;;;
1097 ;;; This one can probably be made really fast natively.
1098 (define vector-fill!
1099 (let ((%vector-fill! vector-fill!)) ; Take the native one, under
1100 ; the assumption that it's
1101 ; faster, so we can use it if
1102 ; there are no optional
1103 ; arguments.
1104 (lambda (vec value . maybe-start+end)
1105 (if (null? maybe-start+end)
1106 (%vector-fill! vec value) ;+++
1107 (let-vector-start+end vector-fill! vec maybe-start+end
1108 (start end)
1109 (do ((i start (+ i 1)))
1110 ((= i end))
1111 (vector-set! vec i value)))))))
1112
1113 ;;; (VECTOR-COPY! <target> <tstart> <source> [<sstart> <send>])
1114 ;;; -> unspecified
1115 ;;; Copy the values in the locations in [SSTART,SEND) from SOURCE to
1116 ;;; to TARGET, starting at TSTART in TARGET.
1117 ;;; [wdc] Corrected to allow 0 <= sstart <= send <= (vector-length source).
1118 (define (vector-copy! target tstart source . maybe-sstart+send)
1119 (define (doit! sstart send source-length)
1120 (let ((tstart (check-type nonneg-int? tstart vector-copy!))
1121 (sstart (check-type nonneg-int? sstart vector-copy!))
1122 (send (check-type nonneg-int? send vector-copy!)))
1123 (cond ((and (<= 0 sstart send source-length)
1124 (<= (+ tstart (- send sstart)) (vector-length target)))
1125 (%vector-copy! target tstart source sstart send))
1126 (else
1127 (error "illegal arguments"
1128 `(while calling ,vector-copy!)
1129 `(target was ,target)
1130 `(target-length was ,(vector-length target))
1131 `(tstart was ,tstart)
1132 `(source was ,source)
1133 `(source-length was ,source-length)
1134 `(sstart was ,sstart)
1135 `(send was ,send))))))
1136 (let ((n (vector-length source)))
1137 (cond ((null? maybe-sstart+send)
1138 (doit! 0 n n))
1139 ((null? (cdr maybe-sstart+send))
1140 (doit! (car maybe-sstart+send) n n))
1141 ((null? (cddr maybe-sstart+send))
1142 (doit! (car maybe-sstart+send) (cadr maybe-sstart+send) n))
1143 (else
1144 (error "too many arguments"
1145 vector-copy!
1146 (cddr maybe-sstart+send))))))
1147
1148 ;;; (VECTOR-REVERSE-COPY! <target> <tstart> <source> [<sstart> <send>])
1149 ;;; [wdc] Corrected to allow 0 <= sstart <= send <= (vector-length source).
1150 (define (vector-reverse-copy! target tstart source . maybe-sstart+send)
1151 (define (doit! sstart send source-length)
1152 (let ((tstart (check-type nonneg-int? tstart vector-reverse-copy!))
1153 (sstart (check-type nonneg-int? sstart vector-reverse-copy!))
1154 (send (check-type nonneg-int? send vector-reverse-copy!)))
1155 (cond ((and (eq? target source)
1156 (or (between? sstart tstart send)
1157 (between? tstart sstart
1158 (+ tstart (- send sstart)))))
1159 (error "vector range for self-copying overlaps"
1160 vector-reverse-copy!
1161 `(vector was ,target)
1162 `(tstart was ,tstart)
1163 `(sstart was ,sstart)
1164 `(send was ,send)))
1165 ((and (<= 0 sstart send source-length)
1166 (<= (+ tstart (- send sstart)) (vector-length target)))
1167 (%vector-reverse-copy! target tstart source sstart send))
1168 (else
1169 (error "illegal arguments"
1170 `(while calling ,vector-reverse-copy!)
1171 `(target was ,target)
1172 `(target-length was ,(vector-length target))
1173 `(tstart was ,tstart)
1174 `(source was ,source)
1175 `(source-length was ,source-length)
1176 `(sstart was ,sstart)
1177 `(send was ,send))))))
1178 (let ((n (vector-length source)))
1179 (cond ((null? maybe-sstart+send)
1180 (doit! 0 n n))
1181 ((null? (cdr maybe-sstart+send))
1182 (doit! (car maybe-sstart+send) n n))
1183 ((null? (cddr maybe-sstart+send))
1184 (doit! (car maybe-sstart+send) (cadr maybe-sstart+send) n))
1185 (else
1186 (error "too many arguments"
1187 vector-reverse-copy!
1188 (cddr maybe-sstart+send))))))
1189
1190 ;;; (VECTOR-REVERSE! <vector> [<start> <end>]) -> unspecified
1191 ;;; Destructively reverse the contents of the sequence of locations
1192 ;;; in VECTOR between START, whose default is 0, and END, whose
1193 ;;; default is the length of VECTOR.
1194 (define (vector-reverse! vec . start+end)
1195 (let-vector-start+end vector-reverse! vec start+end
1196 (start end)
1197 (%vector-reverse! vec start end)))
1198
1199
1200
1201 ;;; --------------------
1202 ;;; Conversion
1203
1204 ;;; (VECTOR->LIST <vector> [<start> <end>]) -> list
1205 ;;; [R5RS+] Produce a list containing the elements in the locations
1206 ;;; between START, whose default is 0, and END, whose default is the
1207 ;;; length of VECTOR, from VECTOR.
1208 (define vector->list
1209 (let ((%vector->list vector->list))
1210 (lambda (vec . maybe-start+end)
1211 (if (null? maybe-start+end) ; Oughta use CASE-LAMBDA.
1212 (%vector->list vec) ;+++
1213 (let-vector-start+end vector->list vec maybe-start+end
1214 (start end)
1215 ;(unfold (lambda (i) ; No SRFI 1.
1216 ; (< i start))
1217 ; (lambda (i) (vector-ref vec i))
1218 ; (lambda (i) (- i 1))
1219 ; (- end 1))
1220 (do ((i (- end 1) (- i 1))
1221 (result '() (cons (vector-ref vec i) result)))
1222 ((< i start) result)))))))
1223
1224 ;;; (REVERSE-VECTOR->LIST <vector> [<start> <end>]) -> list
1225 ;;; Produce a list containing the elements in the locations between
1226 ;;; START, whose default is 0, and END, whose default is the length
1227 ;;; of VECTOR, from VECTOR, in reverse order.
1228 (define (reverse-vector->list vec . maybe-start+end)
1229 (let-vector-start+end reverse-vector->list vec maybe-start+end
1230 (start end)
1231 ;(unfold (lambda (i) (= i end)) ; No SRFI 1.
1232 ; (lambda (i) (vector-ref vec i))
1233 ; (lambda (i) (+ i 1))
1234 ; start)
1235 (do ((i start (+ i 1))
1236 (result '() (cons (vector-ref vec i) result)))
1237 ((= i end) result))))
1238
1239 ;;; (LIST->VECTOR <list> [<start> <end>]) -> vector
1240 ;;; [R5RS+] Produce a vector containing the elements in LIST, which
1241 ;;; must be a proper list, between START, whose default is 0, & END,
1242 ;;; whose default is the length of LIST. It is suggested that if the
1243 ;;; length of LIST is known in advance, the START and END arguments
1244 ;;; be passed, so that LIST->VECTOR need not call LENGTH to determine
1245 ;;; the the length.
1246 ;;;
1247 ;;; This implementation diverges on circular lists, unless LENGTH fails
1248 ;;; and causes - to fail as well. Given a LENGTH* that computes the
1249 ;;; length of a list's cycle, this wouldn't diverge, and would work
1250 ;;; great for circular lists.
1251 (define list->vector
1252 (let ((%list->vector list->vector))
1253 (lambda (lst . maybe-start+end)
1254 ;; Checking the type of a proper list is expensive, so we do it
1255 ;; amortizedly, or let %LIST->VECTOR or LIST-TAIL do it.
1256 (if (null? maybe-start+end) ; Oughta use CASE-LAMBDA.
1257 (%list->vector lst) ;+++
1258 ;; We can't use LET-VECTOR-START+END, because we're using the
1259 ;; bounds of a _list_, not a vector.
1260 (let*-optionals maybe-start+end
1261 ((start 0)
1262 (end (length lst))) ; Ugh -- LENGTH
1263 (let ((start (check-type nonneg-int? start list->vector))
1264 (end (check-type nonneg-int? end list->vector)))
1265 ((lambda (f)
1266 (vector-unfold f (- end start) (list-tail lst start)))
1267 (lambda (index l)
1268 (cond ((null? l)
1269 (error "list was too short"
1270 `(list was ,lst)
1271 `(attempted end was ,end)
1272 `(while calling ,list->vector)))
1273 ((pair? l)
1274 (values (car l) (cdr l)))
1275 (else
1276 ;; Make this look as much like what CHECK-TYPE
1277 ;; would report as possible.
1278 (error "erroneous value"
1279 ;; We want SRFI 1's PROPER-LIST?, but it
1280 ;; would be a waste to link all of SRFI
1281 ;; 1 to this module for only the single
1282 ;; function PROPER-LIST?.
1283 (list list? lst)
1284 `(while calling
1285 ,list->vector))))))))))))
1286
1287 ;;; (REVERSE-LIST->VECTOR <list> [<start> <end>]) -> vector
1288 ;;; Produce a vector containing the elements in LIST, which must be a
1289 ;;; proper list, between START, whose default is 0, and END, whose
1290 ;;; default is the length of LIST, in reverse order. It is suggested
1291 ;;; that if the length of LIST is known in advance, the START and END
1292 ;;; arguments be passed, so that REVERSE-LIST->VECTOR need not call
1293 ;;; LENGTH to determine the the length.
1294 ;;;
1295 ;;; This also diverges on circular lists unless, again, LENGTH returns
1296 ;;; something that makes - bork.
1297 (define (reverse-list->vector lst . maybe-start+end)
1298 (let*-optionals maybe-start+end
1299 ((start 0)
1300 (end (length lst))) ; Ugh -- LENGTH
1301 (let ((start (check-type nonneg-int? start reverse-list->vector))
1302 (end (check-type nonneg-int? end reverse-list->vector)))
1303 ((lambda (f)
1304 (vector-unfold-right f (- end start) (list-tail lst start)))
1305 (lambda (index l)
1306 (cond ((null? l)
1307 (error "list too short"
1308 `(list was ,lst)
1309 `(attempted end was ,end)
1310 `(while calling ,reverse-list->vector)))
1311 ((pair? l)
1312 (values (car l) (cdr l)))
1313 (else
1314 (error "erroneous value"
1315 (list list? lst)
1316 `(while calling ,reverse-list->vector)))))))))