Skip to content

Commit 4a380d3

Browse files
authored
Add fxvector-copy! and flvector-copy! (#922)
These are useful for bulk shifting of values without the overhead of accessing the slots one by one.
1 parent 7de28b9 commit 4a380d3

File tree

7 files changed

+531
-110
lines changed

7 files changed

+531
-110
lines changed

csug/objects.stex

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1153,7 +1153,37 @@ as \var{fxvector}.
11531153
(eq? v (fxvector-copy v))) ;=> #f
11541154
\endschemedisplay
11551155

1156+
%----------------------------------------------------------------------------
1157+
\entryheader
1158+
\formdef{fxvector-copy!}{\categoryprocedure}{(fxvector-copy! \var{src} \var{src-start} \var{dst} \var{dst-start} \var{n})}
1159+
\returns unspecified
1160+
\listlibraries
1161+
\endnoskipentryheader
1162+
1163+
\noindent
1164+
\var{src} and \var{dst} must be fxvectors.
1165+
\var{src-start}, \var{dst-start}, and \var{n} must be exact nonnegative
1166+
integers.
1167+
The sum of \var{src-start} and \var{n} must not exceed the length of \var{src},
1168+
and the sum of \var{dst-start} and \var{n} must not exceed the length of \var{dst}.
11561169

1170+
\scheme{fxvector-copy!} overwrites the \var{n} elements of \var{dst}
1171+
starting at \var{dst-start} with the \var{n} elements of \var{src}
1172+
starting at \var{src-start}.
1173+
This works even if \var{dst} is the same fxvector as \var{src} and the
1174+
source and destination locations overlap.
1175+
That is, the destination is filled with the fixnums that appeared at the
1176+
source before the operation began.
1177+
1178+
\schemedisplay
1179+
(let ([src (fxvector 1 2 3)] [dest (fxvector 0 0 0)])
1180+
(fxvector-copy! src 1 dest 0 2)
1181+
dest) ;=> #vfx(2 3 0)
1182+
1183+
(let ([fxv (fxvector 1 2 3)])
1184+
(fxvector-copy! fxv 1 fxv 0 2)
1185+
fxv) ;=> #vfx(2 3 3)
1186+
\endschemedisplay
11571187

11581188
\section{Flonum-Only Vectors\label{SECTFLVECTORS}}
11591189

@@ -1351,7 +1381,37 @@ as \var{flvector}.
13511381
(eq? v (flvector-copy v))) ;=> #f
13521382
\endschemedisplay
13531383

1384+
%----------------------------------------------------------------------------
1385+
\entryheader
1386+
\formdef{flvector-copy!}{\categoryprocedure}{(flvector-copy! \var{src} \var{src-start} \var{dst} \var{dst-start} \var{n})}
1387+
\returns unspecified
1388+
\listlibraries
1389+
\endnoskipentryheader
1390+
1391+
\noindent
1392+
\var{src} and \var{dst} must be flvectors.
1393+
\var{src-start}, \var{dst-start}, and \var{n} must be exact nonnegative
1394+
integers.
1395+
The sum of \var{src-start} and \var{n} must not exceed the length of \var{src},
1396+
and the sum of \var{dst-start} and \var{n} must not exceed the length of \var{dst}.
13541397

1398+
\scheme{flvector-copy!} overwrites the \var{n} elements of \var{dst}
1399+
starting at \var{dst-start} with the \var{n} elements of \var{src}
1400+
starting at \var{src-start}.
1401+
This works even if \var{dst} is the same flvector as \var{src} and the
1402+
source and destination locations overlap.
1403+
That is, the destination is filled with the flonums that appeared at the
1404+
source before the operation began.
1405+
1406+
\schemedisplay
1407+
(let ([src (flvector 1.0 2.0 3.0)] [dest (fxvector 0.0 0.0 0.0)])
1408+
(flvector-copy! src 1 dest 0 2)
1409+
dest) ;=> #vfl(2.0 3.0 0.0)
1410+
1411+
(let ([flv (flvector 1.0 2.0 3.0)])
1412+
(flvector-copy! flv 1 flv 0 2)
1413+
flv) ;=> #vfl(2.0 3.0 3.0)
1414+
\endschemedisplay
13551415

13561416
\section{Bytevectors\label{SECTBYTEVECTORS}}
13571417

mats/5_6.ms

Lines changed: 192 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -660,6 +660,102 @@
660660
(error? (fxvector-copy '(a b c)))
661661
)
662662
663+
(mat fxvector-copy!
664+
(begin
665+
(define $v1 (fxvector 1 2 3 4))
666+
(define $v2 (fxvector 255 254 253 252 251 250 249 248 247))
667+
(and (fxvector? $v1)
668+
(fxvector? $v2)
669+
(eqv? (fxvector-length $v1) 4)
670+
(eqv? (fxvector-length $v2) 9)))
671+
672+
; wrong number of arguments
673+
(error? (fxvector-copy!))
674+
(error? (fxvector-copy! $v2))
675+
(error? (fxvector-copy! $v2 3))
676+
(error? (fxvector-copy! $v2 3 $v1))
677+
(error? (fxvector-copy! $v2 3 $v1 1))
678+
(error? (if (fxvector-copy! $v2 3 $v1 1 2 3) #f #t))
679+
680+
; not fxvector
681+
(error? (fxvector-copy! 0 0 $v2 0 0))
682+
(error? (if (fxvector-copy! $v1 0 (vector 1 2 3) 0 0) #f #t))
683+
684+
; bad index
685+
(error? (fxvector-copy! $v1 -1 $v2 0 0))
686+
(error? (fxvector-copy! $v1 0 $v2 -1 0))
687+
(error? (fxvector-copy! $v1 'a $v2 0 0))
688+
(error? (fxvector-copy! $v1 0 $v2 0.0 0))
689+
(error? (fxvector-copy! $v1 (+ (most-positive-fixnum) 1) $v2 0 0))
690+
(error? (if (fxvector-copy! $v1 0 $v2 (+ (most-positive-fixnum) 1) 0) #f #t))
691+
692+
; bad count
693+
(error? (fxvector-copy! $v1 0 $v2 0 -1))
694+
(error? (fxvector-copy! $v1 0 $v2 0 (+ (most-positive-fixnum) 1)))
695+
(error? (if (fxvector-copy! $v1 0 $v2 0 'a) #f #t))
696+
697+
; beyond end
698+
(error? (fxvector-copy! $v1 0 $v2 0 5))
699+
(error? (fxvector-copy! $v2 0 $v1 0 5))
700+
(error? (fxvector-copy! $v1 1 $v2 0 4))
701+
(error? (fxvector-copy! $v2 0 $v1 1 4))
702+
(error? (fxvector-copy! $v1 2 $v2 0 3))
703+
(error? (fxvector-copy! $v2 0 $v1 2 3))
704+
(error? (fxvector-copy! $v1 3 $v2 0 2))
705+
(error? (fxvector-copy! $v2 0 $v1 3 2))
706+
(error? (fxvector-copy! $v1 4 $v2 0 1))
707+
(error? (fxvector-copy! $v2 0 $v1 4 1))
708+
(error? (fxvector-copy! $v2 0 $v1 0 500))
709+
(error? (if (fxvector-copy! $v2 500 $v1 0 0) #f #t))
710+
711+
; make sure no damage done
712+
(and (fxvector? $v1)
713+
(fxvector? $v2)
714+
(equal? $v1 #vfx(1 2 3 4))
715+
(equal? $v2 #vfx(255 254 253 252 251 250 249 248 247)))
716+
717+
(begin
718+
(fxvector-copy! $v2 3 $v1 1 2)
719+
(and (equal? $v1 #vfx(1 252 251 4))
720+
(equal? $v2 #vfx(255 254 253 252 251 250 249 248 247))))
721+
(begin
722+
(fxvector-copy! $v2 6 $v1 2 2)
723+
(and (equal? $v1 #vfx(1 252 249 248))
724+
(equal? $v2 #vfx(255 254 253 252 251 250 249 248 247))))
725+
(begin
726+
(fxvector-copy! $v2 0 $v1 4 0)
727+
(and (equal? $v1 #vfx(1 252 249 248))
728+
(equal? $v2 #vfx(255 254 253 252 251 250 249 248 247))))
729+
(begin
730+
(fxvector-copy! $v2 3 $v1 4 0)
731+
(and (equal? $v1 #vfx(1 252 249 248))
732+
(equal? $v2 #vfx(255 254 253 252 251 250 249 248 247))))
733+
(begin
734+
(fxvector-copy! $v2 3 $v2 4 0)
735+
(and (equal? $v1 #vfx(1 252 249 248))
736+
(equal? $v2 #vfx(255 254 253 252 251 250 249 248 247))))
737+
(begin
738+
(fxvector-copy! $v2 2 $v1 1 3)
739+
(and (equal? $v1 #vfx(1 253 252 251))
740+
(equal? $v2 #vfx(255 254 253 252 251 250 249 248 247))))
741+
(begin
742+
(fxvector-copy! $v1 0 $v2 3 4)
743+
(and (equal? $v1 #vfx(1 253 252 251))
744+
(equal? $v2 #vfx(255 254 253 1 253 252 251 248 247))))
745+
(begin
746+
(fxvector-copy! $v2 0 $v2 3 5)
747+
(and (equal? $v1 #vfx(1 253 252 251))
748+
(equal? $v2 #vfx(255 254 253 255 254 253 1 253 247))))
749+
(begin
750+
(fxvector-copy! $v2 4 $v2 2 5)
751+
(and (equal? $v1 #vfx(1 253 252 251))
752+
(equal? $v2 #vfx(255 254 254 253 1 253 247 253 247))))
753+
(begin
754+
(fxvector-copy! $v2 1 $v2 1 7)
755+
(and (equal? $v1 #vfx(1 253 252 251))
756+
(equal? $v2 #vfx(255 254 254 253 1 253 247 253 247))))
757+
)
758+
663759
(mat fxvector-fill!
664760
(let ([v (fxvector-copy '#5vfx(1 2 3 4 5))])
665761
(and (equal? v '#5vfx(1 2 3 4 5))
@@ -815,6 +911,102 @@
815911
(error? (flvector-copy '(a b c)))
816912
)
817913
914+
(mat flvector-copy!
915+
(begin
916+
(define $v1 (flvector 1.0 2.0 3.0 4.0))
917+
(define $v2 (flvector 255.0 254.0 253.0 252.0 251.0 250.0 249.0 248.0 247.0))
918+
(and (flvector? $v1)
919+
(flvector? $v2)
920+
(eqv? (flvector-length $v1) 4)
921+
(eqv? (flvector-length $v2) 9)))
922+
923+
; wrong number of arguments
924+
(error? (flvector-copy!))
925+
(error? (flvector-copy! $v2))
926+
(error? (flvector-copy! $v2 3))
927+
(error? (flvector-copy! $v2 3 $v1))
928+
(error? (flvector-copy! $v2 3 $v1 1))
929+
(error? (if (flvector-copy! $v2 3 $v1 1 2 3) #f #t))
930+
931+
; not flvector
932+
(error? (flvector-copy! 0 0 $v2 0 0))
933+
(error? (if (flvector-copy! $v1 0 (vector 1 2 3) 0 0) #f #t))
934+
935+
; bad index
936+
(error? (flvector-copy! $v1 -1 $v2 0 0))
937+
(error? (flvector-copy! $v1 0 $v2 -1 0))
938+
(error? (flvector-copy! $v1 'a $v2 0 0))
939+
(error? (flvector-copy! $v1 0 $v2 0.0 0))
940+
(error? (flvector-copy! $v1 (+ (most-positive-fixnum) 1) $v2 0 0))
941+
(error? (if (flvector-copy! $v1 0 $v2 (+ (most-positive-fixnum) 1) 0) #f #t))
942+
943+
; bad count
944+
(error? (flvector-copy! $v1 0 $v2 0 -1))
945+
(error? (flvector-copy! $v1 0 $v2 0 (+ (most-positive-fixnum) 1)))
946+
(error? (if (flvector-copy! $v1 0 $v2 0 'a) #f #t))
947+
948+
; beyond end
949+
(error? (flvector-copy! $v1 0 $v2 0 5))
950+
(error? (flvector-copy! $v2 0 $v1 0 5))
951+
(error? (flvector-copy! $v1 1 $v2 0 4))
952+
(error? (flvector-copy! $v2 0 $v1 1 4))
953+
(error? (flvector-copy! $v1 2 $v2 0 3))
954+
(error? (flvector-copy! $v2 0 $v1 2 3))
955+
(error? (flvector-copy! $v1 3 $v2 0 2))
956+
(error? (flvector-copy! $v2 0 $v1 3 2))
957+
(error? (flvector-copy! $v1 4 $v2 0 1))
958+
(error? (flvector-copy! $v2 0 $v1 4 1))
959+
(error? (flvector-copy! $v2 0 $v1 0 500))
960+
(error? (if (flvector-copy! $v2 500 $v1 0 0) #f #t))
961+
962+
; make sure no damage done
963+
(and (flvector? $v1)
964+
(flvector? $v2)
965+
(equal? $v1 #vfl(1.0 2.0 3.0 4.0))
966+
(equal? $v2 #vfl(255.0 254.0 253.0 252.0 251.0 250.0 249.0 248.0 247.0)))
967+
968+
(begin
969+
(flvector-copy! $v2 3 $v1 1 2)
970+
(and (equal? $v1 #vfl(1.0 252.0 251.0 4.0))
971+
(equal? $v2 #vfl(255.0 254.0 253.0 252.0 251.0 250.0 249.0 248.0 247.0))))
972+
(begin
973+
(flvector-copy! $v2 6 $v1 2 2)
974+
(and (equal? $v1 #vfl(1.0 252.0 249.0 248.0))
975+
(equal? $v2 #vfl(255.0 254.0 253.0 252.0 251.0 250.0 249.0 248.0 247.0))))
976+
(begin
977+
(flvector-copy! $v2 0 $v1 4 0)
978+
(and (equal? $v1 #vfl(1.0 252.0 249.0 248.0))
979+
(equal? $v2 #vfl(255.0 254.0 253.0 252.0 251.0 250.0 249.0 248.0 247.0))))
980+
(begin
981+
(flvector-copy! $v2 3 $v1 4 0)
982+
(and (equal? $v1 #vfl(1.0 252.0 249.0 248.0))
983+
(equal? $v2 #vfl(255.0 254.0 253.0 252.0 251.0 250.0 249.0 248.0 247.0))))
984+
(begin
985+
(flvector-copy! $v2 3 $v2 4 0)
986+
(and (equal? $v1 #vfl(1.0 252.0 249.0 248.0))
987+
(equal? $v2 #vfl(255.0 254.0 253.0 252.0 251.0 250.0 249.0 248.0 247.0))))
988+
(begin
989+
(flvector-copy! $v2 2 $v1 1 3)
990+
(and (equal? $v1 #vfl(1.0 253.0 252.0 251.0))
991+
(equal? $v2 #vfl(255.0 254.0 253.0 252.0 251.0 250.0 249.0 248.0 247.0))))
992+
(begin
993+
(flvector-copy! $v1 0 $v2 3 4)
994+
(and (equal? $v1 #vfl(1.0 253.0 252.0 251.0))
995+
(equal? $v2 #vfl(255.0 254.0 253.0 1.0 253.0 252.0 251.0 248.0 247.0))))
996+
(begin
997+
(flvector-copy! $v2 0 $v2 3 5)
998+
(and (equal? $v1 #vfl(1.0 253.0 252.0 251.0))
999+
(equal? $v2 #vfl(255.0 254.0 253.0 255.0 254.0 253.0 1.0 253.0 247.0))))
1000+
(begin
1001+
(flvector-copy! $v2 4 $v2 2 5)
1002+
(and (equal? $v1 #vfl(1.0 253.0 252.0 251.0))
1003+
(equal? $v2 #vfl(255.0 254.0 254.0 253.0 1.0 253.0 247.0 253.0 247.0))))
1004+
(begin
1005+
(flvector-copy! $v2 1 $v2 1 7)
1006+
(and (equal? $v1 #vfl(1.0 253.0 252.0 251.0))
1007+
(equal? $v2 #vfl(255.0 254.0 254.0 253.0 1.0 253.0 247.0 253.0 247.0))))
1008+
)
1009+
8181010
(mat flvector-fill!
8191011
(let ([v (flvector-copy '#5vfl(1.0 2.0 3.0 4.0 5.0))])
8201012
(and (equal? v '#5vfl(1.0 2.0 3.0 4.0 5.0))

0 commit comments

Comments
 (0)