unofficial mirror of guile-user@gnu.org 
 help / color / mirror / Atom feed
* hidden feature of vector-copy or bug?
@ 2023-08-06  9:13 Damien Mattei
  2023-08-07 13:54 ` Damien Mattei
  0 siblings, 1 reply; 2+ messages in thread
From: Damien Mattei @ 2023-08-06  9:13 UTC (permalink / raw)
  To: guile-user

hello,

i'm maintaining and developing some code i wrote which is using
something is a hidden feature or bug in vector-copy?

here is the code but there no need to understand it, the problem is
explained after

;; scheme@(guile-user)> (define gva (growable-vector 1 2 3 4 5))
;; scheme@(guile-user)> (define gva2 (growable-vector-resize gva 8))
;; growable-vector-resize : new-vector :#(1 2 3 4 5 #<unspecified>
#<unspecified> #<unspecified>)
;; scheme@(guile-user)> (describe gva2)
;; #<<growable-vector> 10394d640> is an instance of class <growable-vector>
;; Slots are:
;;      v = #(1 2 3 4 5 #<unspecified> #<unspecified> #<unspecified>)
(define-method (growable-vector-resize (gv <growable-vector>)
(new-size <integer>)) ;; fill unspecified
  (define actual-size (vector-length gv))
  (define old-vector (growable-vector-v gv))
  (define new-vector (vector-copy old-vector 0 new-size))
  ;;(display "growable-vector-resize : new-vector :") (display
new-vector) (newline)
  (growable-vector-set-v! gv new-vector))

it seems that vector-copy when given a end-index greater than the
length of vector to copy does not return an error but create a new
vector of the good size indeed:

scheme@(guile-user)> (define v (make-vector 7 1)
)
scheme@(guile-user)> v
$2 = #(1 1 1 1 1 1 1)
scheme@(guile-user)> (vector-copy v)
$3 = #(1 1 1 1 1 1 1)
scheme@(guile-user)> (vector-copy v 0 10)
$4 = #(1 1 1 1 1 1 1 #<unspecified> #<unspecified> #<unspecified>)

here 10 is greater than (length v) but it works and return:
#(1 1 1 1 1 1 1 #<unspecified> #<unspecified> #<unspecified>)

but the doc does not specify that:
"Scheme Procedure: vector-copy vec [start [end]]C Function:
scm_vector_copy (vec)

Returns a freshly allocated vector containing the elements of vec in
the range [start ... end). start defaults to 0 and end defaults to the
length of vec."

by default end is length of vec, so if i specify an end greater than
length of vec should it not be normal to return an error?

Damien



^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: hidden feature of vector-copy or bug?
  2023-08-06  9:13 hidden feature of vector-copy or bug? Damien Mattei
@ 2023-08-07 13:54 ` Damien Mattei
  0 siblings, 0 replies; 2+ messages in thread
From: Damien Mattei @ 2023-08-07 13:54 UTC (permalink / raw)
  To: guile-user

just understood there is 2 versions of vector-copy ;one is srfi 43 and doc says:

Scheme Procedure: vector-copy vec [start [end [fill]]]

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.


On Sun, Aug 6, 2023 at 11:13 AM Damien Mattei <damien.mattei@gmail.com> wrote:
>
> hello,
>
> i'm maintaining and developing some code i wrote which is using
> something is a hidden feature or bug in vector-copy?
>
> here is the code but there no need to understand it, the problem is
> explained after
>
> ;; scheme@(guile-user)> (define gva (growable-vector 1 2 3 4 5))
> ;; scheme@(guile-user)> (define gva2 (growable-vector-resize gva 8))
> ;; growable-vector-resize : new-vector :#(1 2 3 4 5 #<unspecified>
> #<unspecified> #<unspecified>)
> ;; scheme@(guile-user)> (describe gva2)
> ;; #<<growable-vector> 10394d640> is an instance of class <growable-vector>
> ;; Slots are:
> ;;      v = #(1 2 3 4 5 #<unspecified> #<unspecified> #<unspecified>)
> (define-method (growable-vector-resize (gv <growable-vector>)
> (new-size <integer>)) ;; fill unspecified
>   (define actual-size (vector-length gv))
>   (define old-vector (growable-vector-v gv))
>   (define new-vector (vector-copy old-vector 0 new-size))
>   ;;(display "growable-vector-resize : new-vector :") (display
> new-vector) (newline)
>   (growable-vector-set-v! gv new-vector))
>
> it seems that vector-copy when given a end-index greater than the
> length of vector to copy does not return an error but create a new
> vector of the good size indeed:
>
> scheme@(guile-user)> (define v (make-vector 7 1)
> )
> scheme@(guile-user)> v
> $2 = #(1 1 1 1 1 1 1)
> scheme@(guile-user)> (vector-copy v)
> $3 = #(1 1 1 1 1 1 1)
> scheme@(guile-user)> (vector-copy v 0 10)
> $4 = #(1 1 1 1 1 1 1 #<unspecified> #<unspecified> #<unspecified>)
>
> here 10 is greater than (length v) but it works and return:
> #(1 1 1 1 1 1 1 #<unspecified> #<unspecified> #<unspecified>)
>
> but the doc does not specify that:
> "Scheme Procedure: vector-copy vec [start [end]]C Function:
> scm_vector_copy (vec)
>
> Returns a freshly allocated vector containing the elements of vec in
> the range [start ... end). start defaults to 0 and end defaults to the
> length of vec."
>
> by default end is length of vec, so if i specify an end greater than
> length of vec should it not be normal to return an error?
>
> Damien



^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2023-08-07 13:54 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-08-06  9:13 hidden feature of vector-copy or bug? Damien Mattei
2023-08-07 13:54 ` Damien Mattei

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).