Skip to content

Commit

Permalink
extend vector-copy, add vector-append and vector-set/copy
Browse files Browse the repository at this point in the history
Make `vector-copy` analaogous `substring` when a start position and
count are supplied. Internally, allocate the vector without
initializing memory, since the content is immediately replaced by
copying, and avoid using write barriers, since the vector copy is
freshly allocated.

Along similar lines, `vector-set/copy` can update one vector element,
and `vector-append` can append vectors, in each case without redundant
initialization and write barriers.
  • Loading branch information
mflatt committed Jan 2, 2024
1 parent 056efe1 commit a0581a1
Show file tree
Hide file tree
Showing 15 changed files with 436 additions and 147 deletions.
4 changes: 2 additions & 2 deletions boot/pb/equates.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* equates.h for Chez Scheme Version 9.9.9-pre-release.22 */
/* equates.h for Chez Scheme Version 9.9.9-pre-release.23 */

/* Do not edit this file. It is automatically generated and */
/* specifically tailored to the version of Chez Scheme named */
Expand Down Expand Up @@ -1010,7 +1010,7 @@ typedef uint64_t U64;
#define rtd_sealed 0x4
#define sbwp (ptr)0x4E
#define scaled_shot_1_shot_flag -0x8
#define scheme_version 0x9090916
#define scheme_version 0x9090917
#define seginfo_generation_disp 0x1
#define seginfo_list_bits_disp 0x8
#define seginfo_space_disp 0x0
Expand Down
Binary file modified boot/pb/petite.boot
Binary file not shown.
Binary file modified boot/pb/scheme.boot
Binary file not shown.
4 changes: 2 additions & 2 deletions boot/pb/scheme.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* scheme.h for Chez Scheme Version 9.9.9-pre-release.22 (pb) */
/* scheme.h for Chez Scheme Version 9.9.9-pre-release.23 (pb) */

/* Do not edit this file. It is automatically generated and */
/* specifically tailored to the version of Chez Scheme named */
Expand Down Expand Up @@ -40,7 +40,7 @@
#endif

/* Chez Scheme Version and machine type */
#define VERSION "9.9.9-pre-release.22"
#define VERSION "9.9.9-pre-release.23"
#define MACHINE_TYPE "pb"

/* Integer typedefs */
Expand Down
59 changes: 55 additions & 4 deletions csug/objects.stex
Original file line number Diff line number Diff line change
Expand Up @@ -760,22 +760,73 @@ Any attempt to modify an immutable vector causes an exception to be raised.
%----------------------------------------------------------------------------
\entryheader
\formdef{vector-copy}{\categoryprocedure}{(vector-copy \var{vector})}
\returns a copy of \var{vector}
\formdef{vector-copy}{\categoryprocedure}{(vector-copy \var{vector} \var{start} \var{n})}
\returns a new vector
\listlibraries
\endentryheader

\noindent
\scheme{vector-copy} creates a new vector of the same length and contents
as \var{vector}.
The elements themselves are not copied.
\var{vector} must be a vector.
\var{start} and \var{n} must be exact nonnegative integers.
The sum of \var{start} and \var{n} must not exceed the length
of \var{vector}. When \var{start}
and \var{n} are not supplied, \scheme{0} and
\scheme{(vector-length \var{vector})} are used.

\noindent
\scheme{vector-copy} creates a new vector
that contains \var{n} consecutive elements of \var{vector}
from position \var{start}. When \var{start} and \var{n} are not
supplied, the result is a copy of \var{vector}. The vector elements
themselves are not copied.

\schemedisplay
(vector-copy '#(a b c)) ;=> #(a b c)
(vector-copy '#(a b c d) 1 2) ;=> #(b c)

(let ([v '#(a b c)])
(eq? v (vector-copy v))) ;=> #f
\endschemedisplay

%----------------------------------------------------------------------------
\entryheader
\formdef{vector-append}{\categoryprocedure}{(vector-append \var{vector} \dots)}
\returns a new vector
\listlibraries
\endentryheader

\noindent
\scheme{vector-append} creates a new vector whose content is the
concatenation of the given \var{vector}s in order.

\schemedisplay
(vector-append '#(a b c)) ;=> #(a b c)
(vector-append '#(a b c) '#(d e) '#(f)) ;=> #(a b c d e f)
(vector-append) ;=> #()
\endschemedisplay

%----------------------------------------------------------------------------
\entryheader
\formdef{vector-set/copy}{\categoryprocedure}{(vector-set/copy \var{vector} \var{n} \var{val})}
\returns a new vector with \var{val} at index \var{n}
\listlibraries
\endentryheader

\noindent
\var{vector} must be a vector.
\var{n} must be exact nonnegative integer that is a valid element index
for the vector.

\noindent
\scheme{vector-set/copy} creates a new vector whose content is the
same as \var{vector}, except that the element at index \var{n}
is changed to \var{val}.

\schemedisplay
(vector-set/copy '#(a b c) 0 'x) ;=> #(x b c)
(vector-set/copy '#(a b c) 2 'x) ;=> #(a b x)
\endschemedisplay

%----------------------------------------------------------------------------
\entryheader
\formdef{vector-set-fixnum!}{\categoryprocedure}{(vector-set-fixnum! \var{vector} \var{n} \var{fixnum})}
Expand Down
41 changes: 41 additions & 0 deletions mats/5_6.ms
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,10 @@
(mat vector-copy
(equal? (vector-copy '#()) '#())
(equal? (vector-copy '#(a b c)) '#(a b c))
(equal? (vector-copy '#(a b c) 0 1) '#(a))
(equal? (vector-copy '#(a b c) 2 1) '#(c))
(equal? (vector-copy '#(a b c d) 1 2) '#(b c))
(eq? (vector-copy '#(a b c d) 1 0) '#())
(let* ((x1 (vector 1 2 3)) (x2 (vector-copy x1)))
(and (equal? x2 x1) (not (eq? x2 x1))))
(andmap
Expand All @@ -122,6 +126,43 @@
(equal? (vector-copy v) v)))
(map random (make-list 500 2500)))
(error? (vector-copy '(a b c)))
(error? (vector-copy '#(a b c) 'x 2))
(error? (vector-copy '#(a b c) 1 'x))
(error? (vector-copy '#(a b c) -1 2))
(error? (vector-copy '#(a b c) 1 3))
(error? (vector-copy '#(a b c) 2 -1))
)

(mat vector-set/copy
(equal? (vector-set/copy '#(a b c) 0 'x) '#(x b c))
(equal? (vector-set/copy '#(a b c) 1 'x) '#(a x c))
(equal? (vector-set/copy '#(a b c) 2 'x) '#(a b x))
(equal? (vector-set/copy '#(a b c d e f g h i) 2 'x) '#(a b x d e f g h i))
(error? (vector-set/copy 1))
(error? (vector-set/copy '#(a b c)))
(error? (vector-set/copy '#(a b c) 1))
(error? (vector-set/copy '#(a b c) 'y 'x))
(error? (vector-set/copy '#(a b c) -1 'x))
(error? (vector-set/copy '#(a b c) 3 'x))
)

(mat vector-append
(eq? (vector-append) '#())
(eq? (vector-append '#()) '#())
(eq? (vector-append '#() '#()) '#())
(eq? (vector-append '#() '#() '#()) '#())
(eq? (vector-append '#() '#() '#() '#()) '#())
(equal? (vector-append '#(a b c)) '#(a b c))
(equal? (vector-append '#(a b c) '#(d e)) '#(a b c d e))
(equal? (vector-append '#(a b c) '#(d e) '#(f) '#(g h i)) '#(a b c d e f g h i))
(equal? (vector-append (vector 'p) '#()) '#(p))
(equal? (vector-append '#() (vector 'p)) '#(p))
(equal? (vector-append (vector 'p) '#(a b c)) '#(p a b c))
(equal? (vector-append '#(a b c) (vector 'p)) '#(a b c p))
(error? (vector-append 1))
(error? (vector-append '#(a b c) 'x))
(error? (vector-append '#(a b c) '#(d) 'x))
(error? (vector-append '#(a b c) '#(d) '#(e)'x))
)

(mat vector-fill!
Expand Down
Loading

0 comments on commit a0581a1

Please sign in to comment.