Debian Codegraph @

%3a133/vectors-impl.scm

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