From 9d4dea660f2e0e6947416fafd93f3b880531396a Mon Sep 17 00:00:00 2001 From: Chris Hanson Date: Sat, 22 Feb 1997 07:49:39 +0000 Subject: [PATCH] Add several new operations on vectors. Eliminate unnecessary bindings. --- v7/src/runtime/vector.scm | 173 +++++++++++++++++++++----------------- 1 file changed, 96 insertions(+), 77 deletions(-) diff --git a/v7/src/runtime/vector.scm b/v7/src/runtime/vector.scm index 96a42cb36..46c84b7ff 100644 --- a/v7/src/runtime/vector.scm +++ b/v7/src/runtime/vector.scm @@ -1,8 +1,8 @@ #| -*-Scheme-*- -$Id: vector.scm,v 14.6 1995/07/27 21:33:27 adams Exp $ +$Id: vector.scm,v 14.7 1997/02/22 07:49:39 cph Exp $ -Copyright (c) 1988-1995 Massachusetts Institute of Technology +Copyright (c) 1988-97 Massachusetts Institute of Technology This material was developed by the Scheme project at the Massachusetts Institute of Technology, Department of Electrical Engineering and @@ -42,31 +42,40 @@ MIT in each case. |# list->vector vector subvector->list subvector-move-right! subvector-move-left! subvector-fill!) +(define-integrable (guarantee-vector object procedure) + (if (not (vector? object)) + (error:wrong-type-argument object "vector" procedure))) + +(define-integrable (guarantee-subvector vector start end procedure) + (guarantee-vector vector procedure) + (if (not (index-fixnum? start)) + (error:wrong-type-argument start "valid vector index" procedure)) + (if (not (index-fixnum? end)) + (error:wrong-type-argument end "valid vector index" procedure)) + (if (not (fix:<= start end)) + (error:bad-range-argument start procedure)) + (if (not (fix:<= end (vector-length vector))) + (error:bad-range-argument end procedure))) + (define-integrable (vector? object) (object-type? (ucode-type vector) object)) (define (make-vector size #!optional fill) - (guarantee-index/vector size 'make-vector) - (let ((fill (if (default-object? fill) default-vector-fill-value fill))) - (%make-vector size fill))) - - -(define-integrable default-vector-fill-value #F) - -(define-integrable (%make-vector size fill) - ((ucode-primitive vector-cons) size fill)) + (if (not (index-fixnum? size)) + (error:wrong-type-argument size "valid vector index" 'MAKE-VECTOR)) + ((ucode-primitive vector-cons) size (if (default-object? fill) #f fill))) (define (vector->list vector) - (guarantee-vector vector 'vector->list) + (guarantee-vector vector 'VECTOR->LIST) (subvector->list vector 0 (vector-length vector))) (define (vector-fill! vector value) - (guarantee-vector vector 'vector-fill!) + (guarantee-vector vector 'VECTOR-FILL!) (subvector-fill! vector 0 (vector-length vector) value)) (define (subvector vector start end) - ;; VECTOR, START and END checked by `-' and SUBVECTOR-MOVE-RIGHT! - (let ((result (make-vector (- end start) #F))) + (guarantee-subvector vector start end 'SUBVECTOR) + (let ((result (make-vector (fix:- end start)))) (subvector-move-right! vector start end result 0) result)) @@ -74,28 +83,26 @@ MIT in each case. |# (subvector vector 0 end)) (define (vector-tail vector start) - (guarantee-vector vector 'vector-tail) + (guarantee-vector vector 'VECTOR-TAIL) (subvector vector start (vector-length vector))) (define (vector-copy vector) - (guarantee-vector vector 'vector-copy) + (guarantee-vector vector 'VECTOR-COPY) (let ((length (vector-length vector))) - (let ((new-vector (%make-vector length #F))) + (let ((new-vector (make-vector length))) (subvector-move-right! vector 0 length new-vector 0) new-vector))) - -(define (%vector-append vectors) + +(define (vector-append . vectors) (let ((result - (%make-vector + (make-vector (let loop ((vectors vectors) (length 0)) (if (null? vectors) length (begin - (guarantee-vector (car vectors) 'vector-append) + (guarantee-vector (car vectors) 'VECTOR-APPEND) (loop (cdr vectors) - (fix:+ (vector-length (car vectors)) length))))) - #F))) - + (fix:+ (vector-length (car vectors)) length)))))))) (let loop ((vectors vectors) (index 0)) (if (null? vectors) result @@ -103,31 +110,28 @@ MIT in each case. |# (subvector-move-right! (car vectors) 0 size result index) (loop (cdr vectors) (fix:+ index size))))))) -(define (vector-append . vectors) - (%vector-append vectors)) - (define (vector-grow vector length) - (guarantee-vector vector 'vector-grow) - (let ((new-vector (make-vector length default-vector-fill-value))) + (guarantee-vector vector 'VECTOR-GROW) + (let ((new-vector (make-vector length #f))) (subvector-move-right! vector 0 (vector-length vector) new-vector 0) new-vector)) (define (make-initialized-vector length initialization) ;; LENGTH is checked by MAKE-VECTOR - (let ((vector (make-vector length #F))) + (let ((vector (make-vector length))) (let loop ((index 0)) (if (fix:< index length) (begin (vector-set! vector index (initialization index)) (loop (fix:+ index 1))))) vector)) - + (define (vector-map vector procedure) - (guarantee-vector vector 'vector-map) + (guarantee-vector vector 'VECTOR-MAP) (let ((length (vector-length vector))) - (if (fix:zero? length) + (if (fix:= 0 length) vector - (let ((result (%make-vector length #F))) + (let ((result (make-vector length))) (let loop ((index 0)) (if (fix:< index length) (begin @@ -138,44 +142,56 @@ MIT in each case. |# result)))) (define (for-each-vector-element vector procedure) - (guarantee-vector vector 'for-each-vector-element) + (guarantee-vector vector 'FOR-EACH-VECTOR-ELEMENT) (let ((length (vector-length vector))) (let loop ((index 0)) (if (fix:< index length) (begin (procedure (vector-ref vector index)) (loop (fix:+ index 1))))))) - + (define (subvector-find-next-element vector start end item) - (guarantee-vector vector 'subvector-find-next-element) - (guarantee-index/vector start 'subvector-find-next-element) - (guarantee-vector-bound end vector 'subvector-find-next-element) + (guarantee-subvector vector start end 'SUBVECTOR-FIND-NEXT-ELEMENT) (let loop ((index start)) (and (fix:< index end) (if (eqv? (vector-ref vector index) item) index (loop (fix:+ index 1)))))) +(define (subvector-find-next-element-not vector start end item) + (guarantee-subvector vector start end 'SUBVECTOR-FIND-NEXT-ELEMENT-NOT) + (let loop ((index start)) + (and (fix:< index end) + (if (eqv? (vector-ref vector index) item) + (loop (fix:+ index 1)) + index)))) + (define (subvector-find-previous-element vector start end item) - (guarantee-vector vector 'subvector-find-previous-element) - (guarantee-index/vector start 'subvector-find-previous-element) - (guarantee-vector-bound end vector 'subvector-find-previous-element) + (guarantee-subvector vector start end 'SUBVECTOR-FIND-PREVIOUS-ELEMENT) (let loop ((index (fix:- end 1))) (and (fix:<= start index) (if (eqv? (vector-ref vector index) item) index (loop (fix:- index 1)))))) +(define (subvector-find-previous-element-not vector start end item) + (guarantee-subvector vector start end 'SUBVECTOR-FIND-PREVIOUS-ELEMENT-NOT) + (let loop ((index (fix:- end 1))) + (and (fix:<= start index) + (if (eqv? (vector-ref vector index) item) + (loop (fix:- index 1)) + index)))) + (define-integrable (vector-find-next-element vector item) - (guarantee-vector vector 'vector-find-next-element) + (guarantee-vector vector 'VECTOR-FIND-NEXT-ELEMENT) (subvector-find-next-element vector 0 (vector-length vector) item)) (define-integrable (vector-find-previous-element vector item) - (guarantee-vector vector 'vector-find-previous-element) + (guarantee-vector vector 'VECTOR-FIND-PREVIOUS-ELEMENT) (subvector-find-previous-element vector 0 (vector-length vector) item)) (define (vector-binary-search vector key