[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

additional operations: COPY! and COPY

Dear list,

First of all I would like to express my support for this SRFI; in particular
because it is so concise and useful. In my opinion there is no conflict
at all with SRFI-4, neither in the Scheme system nor in my mind.

Then I would like to support and Taylor's proposal of having a COPY!
operation, for example with the following specification:

(BYTE-VECTOR-COPY! lhs i-lhs rhs i-rhs n)

        copies n bytes from the byte-vector rhs (starting at index i-rhs) into
        the byte-vector lhs (starting at index i-lhs). The exact integers n, i-rhs,
        and i-lhs satisfy

                0 <= i-rhs <= i-rhs + n <= (byte-vector-length rhs),
                0 <= i-lhs <= i-lhs + n <= (byte-vector-length lhs).

        The procedure makes sure the elements are properly copied, even if
        lhs and rhs refer to the same byte-vector object.

(Warning to implementors in C: Use memmove, not memcpy!)

In addition, I am frequently missing VECTOR-COPY, and this will likely also
be the case for byte vectors. So I propose to include also:

(BYTE-VECTOR-COPY byte-vector)

        a newly allocated copy of byte-vector.

Concerning the /fill/ argument in MAKE-BYTE-VECTOR: It is optional in R5RS,
but I would be in favour of having it mandatory here. (In fact, I would be in favour
of having it mandatory in MAKE-VECTOR as well; the internal representations
of vectors are always initialized anyhow. But that is another discussion.)

<minor nitpick>The notation "[0, 255]" could be replaced by "[0..255]" or
better yet by "{0, ..., 255}" for clarity.</minor nitpick>

See you,


--- begin untested code snippet

; proposed operations

(define (byte-vector-copy! lhs i-lhs rhs i-rhs n)
  (let ((vec-lhs (byte-vector-elements lhs))
        (vec-rhs (byte-vector-elements rhs)))
    (let ((n-lhs (vector-length vec-lhs))
          (n-rhs (vector-length vec-rhs)))
      (if (not (and (integer? n) (exact? n) (>= n 0)
                    (integer? i-lhs) (exact? i-lhs)
                    (>= i-lhs 0) (<= (+ i-lhs n) n-lhs)
                    (integer? i-rhs) (exact? i-rhs)
                    (>= i-rhs 0) (<= (+ i-rhs n) n-rhs)))
          (error "bad indices" i-lhs i-rhs n))
      (if (<= i-lhs i-rhs)
          (do ((i 0 (+ i 1))) ((= i n))
            (vector-set! vec-lhs
                         (+ i i-lhs)
                         (vector-ref vec-rhs (+ i i-rhs))))
          (do ((i (- n 1) (- i 1))) ((= i -1))
            (vector-set! vec-lhs
                         (+ i i-lhs)
                         (vector-ref vec-rhs (+ i i-rhs))))))))

(define (byte-vector-copy byte-vector)
  (let ((n (byte-vector-length byte-vector)))
    (let ((new-byte-vector (make-byte-vector n 0)))
      (byte-vector-copy! new-byte-vector 0 byte-vector 0 n)

; test code

(define x1 (byte-vector 1 2 3))
(byte-vector-copy! x1 1 x1 0 2)
(if (not (= (byte-vector-ref x1 2) 2))
    (error "byte-vector-copy! incorrect on overlapping copies"))

--- end untested code snippet

Dr. Sebastian Egner
Senior Scientist Channel Coding & Modulation
Philips Research Laboratories
Prof. Holstlaan 4 (WDC 1-051, 1st floor, room 51)
5656 AA Eindhoven
The Netherlands
tel:       +31 40 27-43166   *** SINCE 10-Feb-2005 ***
fax:      +31 40 27-44004
email: sebastian.egner@xxxxxxxxxxx