File size: 38,388 Bytes
3dcad1f |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 |
;;; srfi-43.scm -- SRFI 43 Vector library
;; Copyright (C) 2014, 2018 Free Software Foundation, Inc.
;;
;; This library is free software; you can redistribute it and/or
;; modify it under the terms of the GNU Lesser General Public
;; License as published by the Free Software Foundation; either
;; version 3 of the License, or (at your option) any later version.
;;
;; This library is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
;; Lesser General Public License for more details.
;;
;; You should have received a copy of the GNU Lesser General Public
;; License along with this library; if not, write to the Free Software
;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
;;; Author: Mark H Weaver <[email protected]>
(define-module (srfi srfi-43)
#:use-module (srfi srfi-1)
#:use-module (srfi srfi-8)
#:re-export (make-vector vector vector? vector-ref vector-set!
vector-length vector-fill! vector-copy!)
#:replace (vector-copy list->vector vector->list)
#:export (vector-empty? vector= vector-unfold vector-unfold-right
vector-reverse-copy
vector-append vector-concatenate
vector-fold vector-fold-right
vector-map vector-map!
vector-for-each vector-count
vector-index vector-index-right
vector-skip vector-skip-right
vector-binary-search
vector-any vector-every
vector-swap! vector-reverse!
vector-reverse-copy!
reverse-vector->list
reverse-list->vector))
(cond-expand-provide (current-module) '(srfi-43))
(define-syntax error-from
(lambda (stx)
(syntax-case stx (quote)
((_ 'who msg arg ...)
#`(error #,(string-append (symbol->string (syntax->datum #'who))
": "
(syntax->datum #'msg))
arg ...)))))
(define-syntax-rule (assert-nonneg-exact-integer k who)
(unless (and (exact-integer? k)
(not (negative? k)))
(error-from who "expected non-negative exact integer, got" k)))
(define-syntax-rule (assert-procedure f who)
(unless (procedure? f)
(error-from who "expected procedure, got" f)))
(define-syntax-rule (assert-vector v who)
(unless (vector? v)
(error-from who "expected vector, got" v)))
(define-syntax-rule (assert-valid-index i len who)
(unless (and (exact-integer? i)
(<= 0 i len))
(error-from who "invalid index" i)))
(define-syntax-rule (assert-valid-start start len who)
(unless (and (exact-integer? start)
(<= 0 start len))
(error-from who "invalid start index" start)))
(define-syntax-rule (assert-valid-range start end len who)
(unless (and (exact-integer? start)
(exact-integer? end)
(<= 0 start end len))
(error-from who "invalid index range" start end)))
(define-syntax-rule (assert-vectors vs who)
(let loop ((vs vs))
(unless (null? vs)
(assert-vector (car vs) who)
(loop (cdr vs)))))
;; Return the length of the shortest vector in VS.
;; VS must have at least one element.
(define (min-length vs)
(let loop ((vs (cdr vs))
(result (vector-length (car vs))))
(if (null? vs)
result
(loop (cdr vs) (min result (vector-length (car vs)))))))
;; Return a list of the Ith elements of the vectors in VS.
(define (vectors-ref vs i)
(let loop ((vs vs) (xs '()))
(if (null? vs)
(reverse! xs)
(loop (cdr vs) (cons (vector-ref (car vs) i)
xs)))))
(define vector-unfold
(case-lambda
"(vector-unfold f length initial-seed ...) -> vector
The fundamental vector constructor. Create a vector whose length is
LENGTH and iterates across each index k from 0 up to LENGTH - 1,
applying F at each iteration to the current index and current seeds, in
that order, to receive n + 1 values: the element to put in the kth slot
of the new vector, and n new seeds for the next iteration. It is an
error for the number of seeds to vary between iterations."
((f len)
(assert-procedure f 'vector-unfold)
(assert-nonneg-exact-integer len 'vector-unfold)
(let ((v (make-vector len)))
(let loop ((i 0))
(unless (= i len)
(vector-set! v i (f i))
(loop (+ i 1))))
v))
((f len seed)
(assert-procedure f 'vector-unfold)
(assert-nonneg-exact-integer len 'vector-unfold)
(let ((v (make-vector len)))
(let loop ((i 0) (seed seed))
(unless (= i len)
(receive (x seed) (f i seed)
(vector-set! v i x)
(loop (+ i 1) seed))))
v))
((f len seed1 seed2)
(assert-procedure f 'vector-unfold)
(assert-nonneg-exact-integer len 'vector-unfold)
(let ((v (make-vector len)))
(let loop ((i 0) (seed1 seed1) (seed2 seed2))
(unless (= i len)
(receive (x seed1 seed2) (f i seed1 seed2)
(vector-set! v i x)
(loop (+ i 1) seed1 seed2))))
v))
((f len . seeds)
(assert-procedure f 'vector-unfold)
(assert-nonneg-exact-integer len 'vector-unfold)
(let ((v (make-vector len)))
(let loop ((i 0) (seeds seeds))
(unless (= i len)
(receive (x . seeds) (apply f i seeds)
(vector-set! v i x)
(loop (+ i 1) seeds))))
v))))
(define vector-unfold-right
(case-lambda
"(vector-unfold-right f length initial-seed ...) -> vector
The fundamental vector constructor. Create a vector whose length is
LENGTH and iterates across each index k from LENGTH - 1 down to 0,
applying F at each iteration to the current index and current seeds, in
that order, to receive n + 1 values: the element to put in the kth slot
of the new vector, and n new seeds for the next iteration. It is an
error for the number of seeds to vary between iterations."
((f len)
(assert-procedure f 'vector-unfold-right)
(assert-nonneg-exact-integer len 'vector-unfold-right)
(let ((v (make-vector len)))
(let loop ((i (- len 1)))
(unless (negative? i)
(vector-set! v i (f i))
(loop (- i 1))))
v))
((f len seed)
(assert-procedure f 'vector-unfold-right)
(assert-nonneg-exact-integer len 'vector-unfold-right)
(let ((v (make-vector len)))
(let loop ((i (- len 1)) (seed seed))
(unless (negative? i)
(receive (x seed) (f i seed)
(vector-set! v i x)
(loop (- i 1) seed))))
v))
((f len seed1 seed2)
(assert-procedure f 'vector-unfold-right)
(assert-nonneg-exact-integer len 'vector-unfold-right)
(let ((v (make-vector len)))
(let loop ((i (- len 1)) (seed1 seed1) (seed2 seed2))
(unless (negative? i)
(receive (x seed1 seed2) (f i seed1 seed2)
(vector-set! v i x)
(loop (- i 1) seed1 seed2))))
v))
((f len . seeds)
(assert-procedure f 'vector-unfold-right)
(assert-nonneg-exact-integer len 'vector-unfold-right)
(let ((v (make-vector len)))
(let loop ((i (- len 1)) (seeds seeds))
(unless (negative? i)
(receive (x . seeds) (apply f i seeds)
(vector-set! v i x)
(loop (- i 1) seeds))))
v))))
(define guile-vector-copy (@ (guile) vector-copy))
(define vector-copy
(case-lambda*
"(vector-copy vec [start [end [fill]]]) -> vector
Allocate a new vector whose length is END - START and fills it with
elements from vec, taking elements from vec starting at index START
and stopping at index END. START defaults to 0 and END defaults to
the value of (vector-length VEC). If END extends beyond the length of
VEC, the slots in the new vector that obviously cannot be filled by
elements from VEC are filled with FILL, whose default value is
unspecified."
((v) (guile-vector-copy v))
((v start) (guile-vector-copy v start))
((v start end #:optional (fill *unspecified*))
(assert-vector v 'vector-copy)
(let ((len (vector-length v)))
(if (<= end len)
(guile-vector-copy v start end)
(begin
(unless (and (exact-integer? start)
(exact-integer? end)
(<= 0 start end))
(error-from 'vector-copy "invalid index range" start end))
(let ((result (make-vector (- end start) fill)))
(vector-move-left! v start (min end len) result 0)
result)))))))
(define vector-reverse-copy
(let ()
(define (%vector-reverse-copy vec start end)
(let* ((len (- end start))
(result (make-vector len)))
(let loop ((i 0) (j (- end 1)))
(unless (= i len)
(vector-set! result i (vector-ref vec j))
(loop (+ i 1) (- j 1))))
result))
(case-lambda
"(vector-reverse-copy vec [start [end]]) -> vector
Allocate a new vector whose length is END - START and fills it with
elements from vec, taking elements from vec in reverse order starting
at index START and stopping at index END. START defaults to 0 and END
defaults to the value of (vector-length VEC)."
((vec)
(assert-vector vec 'vector-reverse-copy)
(%vector-reverse-copy vec 0 (vector-length vec)))
((vec start)
(assert-vector vec 'vector-reverse-copy)
(let ((len (vector-length vec)))
(assert-valid-start start len 'vector-reverse-copy)
(%vector-reverse-copy vec start len)))
((vec start end)
(assert-vector vec 'vector-reverse-copy)
(let ((len (vector-length vec)))
(assert-valid-range start end len 'vector-reverse-copy)
(%vector-reverse-copy vec start end))))))
(define (%vector-concatenate vs)
(let* ((result-len (let loop ((vs vs) (len 0))
(if (null? vs)
len
(loop (cdr vs) (+ len (vector-length (car vs)))))))
(result (make-vector result-len)))
(let loop ((vs vs) (pos 0))
(unless (null? vs)
(let* ((v (car vs))
(len (vector-length v)))
(vector-move-left! v 0 len result pos)
(loop (cdr vs) (+ pos len)))))
result))
(define vector-append
(case-lambda
"(vector-append vec ...) -> vector
Return a newly allocated vector that contains all elements in order
from the subsequent locations in VEC ..."
(() (vector))
((v)
(assert-vector v 'vector-append)
(guile-vector-copy v))
((v1 v2)
(assert-vector v1 'vector-append)
(assert-vector v2 'vector-append)
(let ((len1 (vector-length v1))
(len2 (vector-length v2)))
(let ((result (make-vector (+ len1 len2))))
(vector-move-left! v1 0 len1 result 0)
(vector-move-left! v2 0 len2 result len1)
result)))
(vs
(assert-vectors vs 'vector-append)
(%vector-concatenate vs))))
(define (vector-concatenate vs)
"(vector-concatenate list-of-vectors) -> vector
Append each vector in LIST-OF-VECTORS. Equivalent to:
(apply vector-append LIST-OF-VECTORS)"
(assert-vectors vs 'vector-concatenate)
(%vector-concatenate vs))
(define (vector-empty? vec)
"(vector-empty? vec) -> boolean
Return true if VEC is empty, i.e. its length is 0, and false if not."
(assert-vector vec 'vector-empty?)
(zero? (vector-length vec)))
(define vector=
(let ()
(define (all-of-length? len vs)
(or (null? vs)
(and (= len (vector-length (car vs)))
(all-of-length? len (cdr vs)))))
(define (=up-to? i elt=? v1 v2)
(or (negative? i)
(let ((x1 (vector-ref v1 i))
(x2 (vector-ref v2 i)))
(and (or (eq? x1 x2) (elt=? x1 x2))
(=up-to? (- i 1) elt=? v1 v2)))))
(case-lambda
"(vector= elt=? vec ...) -> boolean
Return true if the vectors VEC ... have equal lengths and equal
elements according to ELT=?. ELT=? is always applied to two
arguments. Element comparison must be consistent with eq?, in the
following sense: if (eq? a b) returns true, then (elt=? a b) must also
return true. The order in which comparisons are performed is
unspecified."
((elt=?)
(assert-procedure elt=? 'vector=)
#t)
((elt=? v)
(assert-procedure elt=? 'vector=)
(assert-vector v 'vector=)
#t)
((elt=? v1 v2)
(assert-procedure elt=? 'vector=)
(assert-vector v1 'vector=)
(assert-vector v2 'vector=)
(let ((len (vector-length v1)))
(and (= len (vector-length v2))
(=up-to? (- len 1) elt=? v1 v2))))
((elt=? v1 . vs)
(assert-procedure elt=? 'vector=)
(assert-vector v1 'vector=)
(assert-vectors vs 'vector=)
(let ((len (vector-length v1)))
(and (all-of-length? len vs)
(let loop ((vs vs))
(or (null? vs)
(and (=up-to? (- len 1) elt=? v1 (car vs))
(loop (cdr vs)))))))))))
(define vector-fold
(case-lambda
"(vector-fold kons knil vec1 vec2 ...) -> value
The fundamental vector iterator. KONS is iterated over each index in
all of the vectors, stopping at the end of the shortest; KONS is
applied as (KONS i state (vector-ref VEC1 i) (vector-ref VEC2 i) ...)
where STATE is the current state value, and I is the current index.
The current state value begins with KNIL, and becomes whatever KONS
returned at the respective iteration. The iteration is strictly
left-to-right."
((kcons knil v)
(assert-procedure kcons 'vector-fold)
(assert-vector v 'vector-fold)
(let ((len (vector-length v)))
(let loop ((i 0) (state knil))
(if (= i len)
state
(loop (+ i 1) (kcons i state (vector-ref v i)))))))
((kcons knil v1 v2)
(assert-procedure kcons 'vector-fold)
(assert-vector v1 'vector-fold)
(assert-vector v2 'vector-fold)
(let ((len (min (vector-length v1) (vector-length v2))))
(let loop ((i 0) (state knil))
(if (= i len)
state
(loop (+ i 1)
(kcons i state (vector-ref v1 i) (vector-ref v2 i)))))))
((kcons knil . vs)
(assert-procedure kcons 'vector-fold)
(assert-vectors vs 'vector-fold)
(let ((len (min-length vs)))
(let loop ((i 0) (state knil))
(if (= i len)
state
(loop (+ i 1) (apply kcons i state (vectors-ref vs i)))))))))
(define vector-fold-right
(case-lambda
"(vector-fold-right kons knil vec1 vec2 ...) -> value
The fundamental vector iterator. KONS is iterated over each index in
all of the vectors, starting at the end of the shortest; KONS is
applied as (KONS i state (vector-ref VEC1 i) (vector-ref VEC2 i) ...)
where STATE is the current state value, and I is the current index.
The current state value begins with KNIL, and becomes whatever KONS
returned at the respective iteration. The iteration is strictly
right-to-left."
((kcons knil v)
(assert-procedure kcons 'vector-fold-right)
(assert-vector v 'vector-fold-right)
(let ((len (vector-length v)))
(let loop ((i (- len 1)) (state knil))
(if (negative? i)
state
(loop (- i 1) (kcons i state (vector-ref v i)))))))
((kcons knil v1 v2)
(assert-procedure kcons 'vector-fold-right)
(assert-vector v1 'vector-fold-right)
(assert-vector v2 'vector-fold-right)
(let ((len (min (vector-length v1) (vector-length v2))))
(let loop ((i (- len 1)) (state knil))
(if (negative? i)
state
(loop (- i 1)
(kcons i state (vector-ref v1 i) (vector-ref v2 i)))))))
((kcons knil . vs)
(assert-procedure kcons 'vector-fold-right)
(assert-vectors vs 'vector-fold-right)
(let ((len (min-length vs)))
(let loop ((i (- len 1)) (state knil))
(if (negative? i)
state
(loop (- i 1) (apply kcons i state (vectors-ref vs i)))))))))
(define vector-map
(case-lambda
"(vector-map f vec2 vec2 ...) -> vector
Return a new vector of the shortest size of the vector arguments.
Each element at index i of the new vector is mapped from the old
vectors by (F i (vector-ref VEC1 i) (vector-ref VEC2 i) ...). The
dynamic order of application of F is unspecified."
((f v)
(assert-procedure f 'vector-map)
(assert-vector v 'vector-map)
(let* ((len (vector-length v))
(result (make-vector len)))
(let loop ((i 0))
(unless (= i len)
(vector-set! result i (f i (vector-ref v i)))
(loop (+ i 1))))
result))
((f v1 v2)
(assert-procedure f 'vector-map)
(assert-vector v1 'vector-map)
(assert-vector v2 'vector-map)
(let* ((len (min (vector-length v1) (vector-length v2)))
(result (make-vector len)))
(let loop ((i 0))
(unless (= i len)
(vector-set! result i (f i (vector-ref v1 i) (vector-ref v2 i)))
(loop (+ i 1))))
result))
((f . vs)
(assert-procedure f 'vector-map)
(assert-vectors vs 'vector-map)
(let* ((len (min-length vs))
(result (make-vector len)))
(let loop ((i 0))
(unless (= i len)
(vector-set! result i (apply f i (vectors-ref vs i)))
(loop (+ i 1))))
result))))
(define vector-map!
(case-lambda
"(vector-map! f vec2 vec2 ...) -> unspecified
Similar to vector-map, but rather than mapping the new elements into a
new vector, the new mapped elements are destructively inserted into
VEC1. The dynamic order of application of F is unspecified."
((f v)
(assert-procedure f 'vector-map!)
(assert-vector v 'vector-map!)
(let ((len (vector-length v)))
(let loop ((i 0))
(unless (= i len)
(vector-set! v i (f i (vector-ref v i)))
(loop (+ i 1))))))
((f v1 v2)
(assert-procedure f 'vector-map!)
(assert-vector v1 'vector-map!)
(assert-vector v2 'vector-map!)
(let ((len (min (vector-length v1) (vector-length v2))))
(let loop ((i 0))
(unless (= i len)
(vector-set! v1 i (f i (vector-ref v1 i) (vector-ref v2 i)))
(loop (+ i 1))))))
((f . vs)
(assert-procedure f 'vector-map!)
(assert-vectors vs 'vector-map!)
(let ((len (min-length vs))
(v1 (car vs)))
(let loop ((i 0))
(unless (= i len)
(vector-set! v1 i (apply f i (vectors-ref vs i)))
(loop (+ i 1))))))))
(define vector-for-each
(case-lambda
"(vector-for-each f vec1 vec2 ...) -> unspecified
Call (F i VEC1[i] VEC2[i] ...) for each index i less than the length
of the shortest vector passed. The iteration is strictly
left-to-right."
((f v)
(assert-procedure f 'vector-for-each)
(assert-vector v 'vector-for-each)
(let ((len (vector-length v)))
(let loop ((i 0))
(unless (= i len)
(f i (vector-ref v i))
(loop (+ i 1))))))
((f v1 v2)
(assert-procedure f 'vector-for-each)
(assert-vector v1 'vector-for-each)
(assert-vector v2 'vector-for-each)
(let ((len (min (vector-length v1)
(vector-length v2))))
(let loop ((i 0))
(unless (= i len)
(f i (vector-ref v1 i) (vector-ref v2 i))
(loop (+ i 1))))))
((f . vs)
(assert-procedure f 'vector-for-each)
(assert-vectors vs 'vector-for-each)
(let ((len (min-length vs)))
(let loop ((i 0))
(unless (= i len)
(apply f i (vectors-ref vs i))
(loop (+ i 1))))))))
(define vector-count
(case-lambda
"(vector-count pred? vec1 vec2 ...) -> exact non-negative integer
Count the number of indices i for which (PRED? VEC1[i] VEC2[i] ...)
returns true, where i is less than the length of the shortest vector
passed."
((pred? v)
(assert-procedure pred? 'vector-count)
(assert-vector v 'vector-count)
(let ((len (vector-length v)))
(let loop ((i 0) (count 0))
(cond ((= i len) count)
((pred? i (vector-ref v i))
(loop (+ i 1) (+ count 1)))
(else
(loop (+ i 1) count))))))
((pred? v1 v2)
(assert-procedure pred? 'vector-count)
(assert-vector v1 'vector-count)
(assert-vector v2 'vector-count)
(let ((len (min (vector-length v1)
(vector-length v2))))
(let loop ((i 0) (count 0))
(cond ((= i len) count)
((pred? i (vector-ref v1 i) (vector-ref v2 i))
(loop (+ i 1) (+ count 1)))
(else
(loop (+ i 1) count))))))
((pred? . vs)
(assert-procedure pred? 'vector-count)
(assert-vectors vs 'vector-count)
(let ((len (min-length vs)))
(let loop ((i 0) (count 0))
(cond ((= i len) count)
((apply pred? i (vectors-ref vs i))
(loop (+ i 1) (+ count 1)))
(else
(loop (+ i 1) count))))))))
(define vector-index
(case-lambda
"(vector-index pred? vec1 vec2 ...) -> exact non-negative integer or #f
Find and return the index of the first elements in VEC1 VEC2 ... that
satisfy PRED?. If no matching element is found by the end of the
shortest vector, return #f."
((pred? v)
(assert-procedure pred? 'vector-index)
(assert-vector v 'vector-index)
(let ((len (vector-length v)))
(let loop ((i 0))
(and (< i len)
(if (pred? (vector-ref v i))
i
(loop (+ i 1)))))))
((pred? v1 v2)
(assert-procedure pred? 'vector-index)
(assert-vector v1 'vector-index)
(assert-vector v2 'vector-index)
(let ((len (min (vector-length v1)
(vector-length v2))))
(let loop ((i 0))
(and (< i len)
(if (pred? (vector-ref v1 i)
(vector-ref v2 i))
i
(loop (+ i 1)))))))
((pred? . vs)
(assert-procedure pred? 'vector-index)
(assert-vectors vs 'vector-index)
(let ((len (min-length vs)))
(let loop ((i 0))
(and (< i len)
(if (apply pred? (vectors-ref vs i))
i
(loop (+ i 1)))))))))
(define vector-index-right
(case-lambda
"(vector-index-right pred? vec1 vec2 ...) -> exact non-negative integer or #f
Find and return the index of the last elements in VEC1 VEC2 ... that
satisfy PRED?, searching from right-to-left. If no matching element
is found before the end of the shortest vector, return #f."
((pred? v)
(assert-procedure pred? 'vector-index-right)
(assert-vector v 'vector-index-right)
(let ((len (vector-length v)))
(let loop ((i (- len 1)))
(and (>= i 0)
(if (pred? (vector-ref v i))
i
(loop (- i 1)))))))
((pred? v1 v2)
(assert-procedure pred? 'vector-index-right)
(assert-vector v1 'vector-index-right)
(assert-vector v2 'vector-index-right)
(let ((len (min (vector-length v1)
(vector-length v2))))
(let loop ((i (- len 1)))
(and (>= i 0)
(if (pred? (vector-ref v1 i)
(vector-ref v2 i))
i
(loop (- i 1)))))))
((pred? . vs)
(assert-procedure pred? 'vector-index-right)
(assert-vectors vs 'vector-index-right)
(let ((len (min-length vs)))
(let loop ((i (- len 1)))
(and (>= i 0)
(if (apply pred? (vectors-ref vs i))
i
(loop (- i 1)))))))))
(define vector-skip
(case-lambda
"(vector-skip pred? vec1 vec2 ...) -> exact non-negative integer or #f
Find and return the index of the first elements in VEC1 VEC2 ... that
do not satisfy PRED?. If no matching element is found by the end of
the shortest vector, return #f."
((pred? v)
(assert-procedure pred? 'vector-skip)
(assert-vector v 'vector-skip)
(let ((len (vector-length v)))
(let loop ((i 0))
(and (< i len)
(if (pred? (vector-ref v i))
(loop (+ i 1))
i)))))
((pred? v1 v2)
(assert-procedure pred? 'vector-skip)
(assert-vector v1 'vector-skip)
(assert-vector v2 'vector-skip)
(let ((len (min (vector-length v1)
(vector-length v2))))
(let loop ((i 0))
(and (< i len)
(if (pred? (vector-ref v1 i)
(vector-ref v2 i))
(loop (+ i 1))
i)))))
((pred? . vs)
(assert-procedure pred? 'vector-skip)
(assert-vectors vs 'vector-skip)
(let ((len (min-length vs)))
(let loop ((i 0))
(and (< i len)
(if (apply pred? (vectors-ref vs i))
(loop (+ i 1))
i)))))))
(define vector-skip-right
(case-lambda
"(vector-skip-right pred? vec1 vec2 ...) -> exact non-negative integer or #f
Find and return the index of the last elements in VEC1 VEC2 ... that
do not satisfy PRED?, searching from right-to-left. If no matching
element is found before the end of the shortest vector, return #f."
((pred? v)
(assert-procedure pred? 'vector-skip-right)
(assert-vector v 'vector-skip-right)
(let ((len (vector-length v)))
(let loop ((i (- len 1)))
(and (not (negative? i))
(if (pred? (vector-ref v i))
(loop (- i 1))
i)))))
((pred? v1 v2)
(assert-procedure pred? 'vector-skip-right)
(assert-vector v1 'vector-skip-right)
(assert-vector v2 'vector-skip-right)
(let ((len (min (vector-length v1)
(vector-length v2))))
(let loop ((i (- len 1)))
(and (not (negative? i))
(if (pred? (vector-ref v1 i)
(vector-ref v2 i))
(loop (- i 1))
i)))))
((pred? . vs)
(assert-procedure pred? 'vector-skip-right)
(assert-vectors vs 'vector-skip-right)
(let ((len (min-length vs)))
(let loop ((i (- len 1)))
(and (not (negative? i))
(if (apply pred? (vectors-ref vs i))
(loop (- i 1))
i)))))))
(define vector-binary-search
(let ()
(define (%vector-binary-search vec value cmp start end)
(let loop ((lo start) (hi end))
(and (< lo hi)
(let* ((i (quotient (+ lo hi) 2))
(x (vector-ref vec i))
(c (cmp x value)))
(cond ((zero? c) i)
((positive? c) (loop lo i))
((negative? c) (loop (+ i 1) hi)))))))
(case-lambda
"(vector-binary-search vec value cmp [start [end]]) -> exact non-negative integer or #f
Find and return an index of VEC between START and END whose value is
VALUE using a binary search. If no matching element is found, return
#f. The default START is 0 and the default END is the length of VEC.
CMP must be a procedure of two arguments such that (CMP A B) returns
a negative integer if A < B, a positive integer if A > B, or zero if
A = B. The elements of VEC must be sorted in non-decreasing order
according to CMP."
((vec value cmp)
(assert-vector vec 'vector-binary-search)
(assert-procedure cmp 'vector-binary-search)
(%vector-binary-search vec value cmp 0 (vector-length vec)))
((vec value cmp start)
(assert-vector vec 'vector-binary-search)
(let ((len (vector-length vec)))
(assert-valid-start start len 'vector-binary-search)
(%vector-binary-search vec value cmp start len)))
((vec value cmp start end)
(assert-vector vec 'vector-binary-search)
(let ((len (vector-length vec)))
(assert-valid-range start end len 'vector-binary-search)
(%vector-binary-search vec value cmp start end))))))
(define vector-any
(case-lambda
"(vector-any pred? vec1 vec2 ...) -> value or #f
Find the first parallel set of elements from VEC1 VEC2 ... for which
PRED? returns a true value. If such a parallel set of elements
exists, vector-any returns the value that PRED? returned for that set
of elements. The iteration is strictly left-to-right."
((pred? v)
(assert-procedure pred? 'vector-any)
(assert-vector v 'vector-any)
(let ((len (vector-length v)))
(let loop ((i 0))
(and (< i len)
(or (pred? (vector-ref v i))
(loop (+ i 1)))))))
((pred? v1 v2)
(assert-procedure pred? 'vector-any)
(assert-vector v1 'vector-any)
(assert-vector v2 'vector-any)
(let ((len (min (vector-length v1)
(vector-length v2))))
(let loop ((i 0))
(and (< i len)
(or (pred? (vector-ref v1 i)
(vector-ref v2 i))
(loop (+ i 1)))))))
((pred? . vs)
(assert-procedure pred? 'vector-any)
(assert-vectors vs 'vector-any)
(let ((len (min-length vs)))
(let loop ((i 0))
(and (< i len)
(or (apply pred? (vectors-ref vs i))
(loop (+ i 1)))))))))
(define vector-every
(case-lambda
"(vector-every pred? vec1 vec2 ...) -> value or #f
If, for every index i less than the length of the shortest vector
argument, the set of elements VEC1[i] VEC2[i] ... satisfies PRED?,
vector-every returns the value that PRED? returned for the last set of
elements, at the last index of the shortest vector. The iteration is
strictly left-to-right."
((pred? v)
(assert-procedure pred? 'vector-every)
(assert-vector v 'vector-every)
(let ((len (vector-length v)))
(or (zero? len)
(let loop ((i 0))
(let ((val (pred? (vector-ref v i)))
(next-i (+ i 1)))
(if (or (not val) (= next-i len))
val
(loop next-i)))))))
((pred? v1 v2)
(assert-procedure pred? 'vector-every)
(assert-vector v1 'vector-every)
(assert-vector v2 'vector-every)
(let ((len (min (vector-length v1)
(vector-length v2))))
(or (zero? len)
(let loop ((i 0))
(let ((val (pred? (vector-ref v1 i)
(vector-ref v2 i)))
(next-i (+ i 1)))
(if (or (not val) (= next-i len))
val
(loop next-i)))))))
((pred? . vs)
(assert-procedure pred? 'vector-every)
(assert-vectors vs 'vector-every)
(let ((len (min-length vs)))
(or (zero? len)
(let loop ((i 0))
(let ((val (apply pred? (vectors-ref vs i)))
(next-i (+ i 1)))
(if (or (not val) (= next-i len))
val
(loop next-i)))))))))
(define (vector-swap! vec i j)
"(vector-swap! vec i j) -> unspecified
Swap the values of the locations in VEC at I and J."
(assert-vector vec 'vector-swap!)
(let ((len (vector-length vec)))
(assert-valid-index i len 'vector-swap!)
(assert-valid-index j len 'vector-swap!)
(let ((tmp (vector-ref vec i)))
(vector-set! vec i (vector-ref vec j))
(vector-set! vec j tmp))))
(define (%vector-reverse! vec start end)
(let loop ((i start) (j (- end 1)))
(when (< i j)
(let ((tmp (vector-ref vec i)))
(vector-set! vec i (vector-ref vec j))
(vector-set! vec j tmp)
(loop (+ i 1) (- j 1))))))
(define vector-reverse!
(case-lambda
"(vector-reverse! vec [start [end]]) -> unspecified
Destructively reverse the contents of VEC between START and END.
START defaults to 0 and END defaults to the length of VEC."
((vec)
(assert-vector vec 'vector-reverse!)
(%vector-reverse! vec 0 (vector-length vec)))
((vec start)
(assert-vector vec 'vector-reverse!)
(let ((len (vector-length vec)))
(assert-valid-start start len 'vector-reverse!)
(%vector-reverse! vec start len)))
((vec start end)
(assert-vector vec 'vector-reverse!)
(let ((len (vector-length vec)))
(assert-valid-range start end len 'vector-reverse!)
(%vector-reverse! vec start end)))))
(define-syntax-rule (define-vector-copier! copy! docstring inner-proc)
(define copy!
(let ((%copy! inner-proc))
(case-lambda
docstring
((target tstart source)
(assert-vector target 'copy!)
(assert-vector source 'copy!)
(let ((tlen (vector-length target))
(slen (vector-length source)))
(assert-valid-start tstart tlen 'copy!)
(unless (>= tlen (+ tstart slen))
(error-from 'copy! "would write past end of target"))
(%copy! target tstart source 0 slen)))
((target tstart source sstart)
(assert-vector target 'copy!)
(assert-vector source 'copy!)
(let ((tlen (vector-length target))
(slen (vector-length source)))
(assert-valid-start tstart tlen 'copy!)
(assert-valid-start sstart slen 'copy!)
(unless (>= tlen (+ tstart (- slen sstart)))
(error-from 'copy! "would write past end of target"))
(%copy! target tstart source sstart slen)))
((target tstart source sstart send)
(assert-vector target 'copy!)
(assert-vector source 'copy!)
(let ((tlen (vector-length target))
(slen (vector-length source)))
(assert-valid-start tstart tlen 'copy!)
(assert-valid-range sstart send slen 'copy!)
(unless (>= tlen (+ tstart (- send sstart)))
(error-from 'copy! "would write past end of target"))
(%copy! target tstart source sstart send)))))))
(define-vector-copier! vector-reverse-copy!
"(vector-reverse-copy! target tstart source [sstart [send]]) -> unspecified
Like vector-copy!, but copy the elements in the reverse order. It is
an error if TARGET and SOURCE are identical vectors and the TARGET and
SOURCE ranges overlap; however, if TSTART = SSTART,
vector-reverse-copy! behaves as (vector-reverse! TARGET TSTART SEND)
would."
(lambda (target tstart source sstart send)
(if (and (eq? target source) (= tstart sstart))
(%vector-reverse! target sstart send)
(let loop ((i tstart) (j (- send 1)))
(when (>= j sstart)
(vector-set! target i (vector-ref source j))
(loop (+ i 1) (- j 1)))))))
(define vector->list
(let ()
(define (%vector->list vec start end)
(let loop ((i (- end 1))
(result '()))
(if (< i start)
result
(loop (- i 1) (cons (vector-ref vec i) result)))))
(case-lambda
"(vector->list vec [start [end]]) -> proper-list
Return a newly allocated list containing the elements in VEC between
START and END. START defaults to 0 and END defaults to the length of
VEC."
((vec)
(assert-vector vec 'vector->list)
(%vector->list vec 0 (vector-length vec)))
((vec start)
(assert-vector vec 'vector->list)
(let ((len (vector-length vec)))
(assert-valid-start start len 'vector->list)
(%vector->list vec start len)))
((vec start end)
(assert-vector vec 'vector->list)
(let ((len (vector-length vec)))
(assert-valid-range start end len 'vector->list)
(%vector->list vec start end))))))
(define reverse-vector->list
(let ()
(define (%reverse-vector->list vec start end)
(let loop ((i start)
(result '()))
(if (>= i end)
result
(loop (+ i 1) (cons (vector-ref vec i) result)))))
(case-lambda
"(reverse-vector->list vec [start [end]]) -> proper-list
Return a newly allocated list containing the elements in VEC between
START and END in reverse order. START defaults to 0 and END defaults
to the length of VEC."
((vec)
(assert-vector vec 'reverse-vector->list)
(%reverse-vector->list vec 0 (vector-length vec)))
((vec start)
(assert-vector vec 'reverse-vector->list)
(let ((len (vector-length vec)))
(assert-valid-start start len 'reverse-vector->list)
(%reverse-vector->list vec start len)))
((vec start end)
(assert-vector vec 'reverse-vector->list)
(let ((len (vector-length vec)))
(assert-valid-range start end len 'reverse-vector->list)
(%reverse-vector->list vec start end))))))
;; TODO: change to use 'case-lambda' and improve error checking.
(define* (list->vector lst #:optional (start 0) (end (length lst)))
"(list->vector proper-list [start [end]]) -> vector
Return a newly allocated vector of the elements from PROPER-LIST with
indices between START and END. START defaults to 0 and END defaults
to the length of PROPER-LIST."
(let* ((len (- end start))
(result (make-vector len)))
(let loop ((i 0) (lst (drop lst start)))
(if (= i len)
result
(begin (vector-set! result i (car lst))
(loop (+ i 1) (cdr lst)))))))
;; TODO: change to use 'case-lambda' and improve error checking.
(define* (reverse-list->vector lst #:optional (start 0) (end (length lst)))
"(reverse-list->vector proper-list [start [end]]) -> vector
Return a newly allocated vector of the elements from PROPER-LIST with
indices between START and END, in reverse order. START defaults to 0
and END defaults to the length of PROPER-LIST."
(let* ((len (- end start))
(result (make-vector len)))
(let loop ((i (- len 1)) (lst (drop lst start)))
(if (negative? i)
result
(begin (vector-set! result i (car lst))
(loop (- i 1) (cdr lst)))))))
|