Skip to content

Commit 9da26d0

Browse files
committed
Array compact printer fix
Needs to emit start index if any of array dimensions begins with non-zero index.
1 parent 23c8c2f commit 9da26d0

2 files changed

Lines changed: 71 additions & 5 deletions

File tree

ext/uvector/array.scm

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -124,19 +124,29 @@
124124
(format port ")"))
125125

126126
;; #<rank>a style. FMT can be 'compact or 'dimensions
127+
;; If compact,
128+
;; - If all dimensions start from 0, we omit dimensions altogether.
129+
;; - Otherwise, we show start index for every dimension
130+
;; If dimensions
131+
;; - We show start if it's not 0
132+
;; - We show length for all dimensions
127133
(define (format-array/srfi-163 array port fmt)
128-
(define (dims)
134+
(define (dims full?)
129135
(with-output-to-string
130136
(^[] (dotimes [i (array-rank array)]
131137
(let ([s (array-start array i)]
132138
[e (array-end array i)])
133-
(cond [(= s 0) (begin (display #\:) (display (- e s)))]
134-
[else (begin (display #\@) (display s)
135-
(display #\:) (display (- e s)))]))))))
139+
(unless (and full? (= s 0)) (display #\@) (display s))
140+
(when full? (begin (display #\:) (display (- e s)))))))))
141+
136142
(format port "#~a~a~@[~a~]~s"
137143
(array-rank array)
138144
(array-tag (class-of array))
139-
(and (eq? fmt 'dimensions) (dims))
145+
(cond [(eq? fmt 'dimensions) (dims #t)]
146+
[(not (every (^i (zero? (array-start array i)))
147+
(iota (array-rank array))))
148+
(dims #f)]
149+
[else #f])
140150
(array->nested-list array)))
141151

142152
(define-class <array> (<array-base>)

ext/uvector/test.scm

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1820,6 +1820,62 @@
18201820
(t "#1u8:2:1(1 2 3)" (test-error <read-error> #/rank and dimensions/))
18211821
)
18221822

1823+
(let ()
1824+
(define (t arr ex-compact ex-dimensions ex-reader-ctor)
1825+
(define (tt mode expect)
1826+
(test* #"array writer: ~mode" expect
1827+
(let ((ctrl (make-write-controls :array-format mode)))
1828+
(write-to-string arr (cut write <> ctrl)))))
1829+
(tt 'compact ex-compact)
1830+
(tt 'dimensions ex-dimensions)
1831+
(tt 'reader-ctor ex-reader-ctor))
1832+
1833+
(t (array (shape 0 2 0 3) 1 2 3 4 5 6)
1834+
"#2a((1 2 3) (4 5 6))"
1835+
"#2a:2:3((1 2 3) (4 5 6))"
1836+
"#,(<array> (0 2 0 3) 1 2 3 4 5 6)")
1837+
1838+
(t (array (shape 0 2 -1 2) 1 2 3 4 5 6)
1839+
"#2a@0@-1((1 2 3) (4 5 6))"
1840+
"#2a:2@-1:3((1 2 3) (4 5 6))"
1841+
"#,(<array> (0 2 -1 2) 1 2 3 4 5 6)")
1842+
1843+
(t (array (shape 1 3 0 3) 1 2 3 4 5 6)
1844+
"#2a@1@0((1 2 3) (4 5 6))"
1845+
"#2a@1:2:3((1 2 3) (4 5 6))"
1846+
"#,(<array> (1 3 0 3) 1 2 3 4 5 6)")
1847+
1848+
(t (u8array (shape 0 3) 1 2 3)
1849+
"#1u8(1 2 3)"
1850+
"#1u8:3(1 2 3)"
1851+
"#,(<u8array> (0 3) 1 2 3)")
1852+
1853+
(t (s16array (shape -1 0 0 1) 1)
1854+
"#2s16@-1@0((1))"
1855+
"#2s16@-1:1:1((1))"
1856+
"#,(<s16array> (-1 0 0 1) 1)")
1857+
1858+
(t (c32array (shape 0 2 1 3) 1 +i -i 1+i)
1859+
"#2c32@0@1((1.0 0.0+1.0i) (0.0-1.0i 1.0+1.0i))"
1860+
"#2c32:2@1:2((1.0 0.0+1.0i) (0.0-1.0i 1.0+1.0i))"
1861+
"#,(<c32array> (0 2 1 3) 1.0 0.0+1.0i 0.0-1.0i 1.0+1.0i)")
1862+
1863+
(t (array (shape 0 0 0 1))
1864+
"#2a()"
1865+
"#2a:0:1()"
1866+
"#,(<array> (0 0 0 1))")
1867+
1868+
(t (array (shape 0 1 0 0))
1869+
"#2a(())"
1870+
"#2a:1:0(())"
1871+
"#,(<array> (0 1 0 0))")
1872+
1873+
(t (array (shape -1 -1 1 1))
1874+
"#2a@-1@1()"
1875+
"#2a@-1:0@1:0()"
1876+
"#,(<array> (-1 -1 1 1))")
1877+
)
1878+
18231879

18241880
(test-section "array-rank")
18251881
(test* "array-rank (shape)" 2

0 commit comments

Comments
 (0)