[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Guile-commits] 05/07: Fix bitvectors and non-zero lower bound arrays in
From: |
Daniel Llorens |
Subject: |
[Guile-commits] 05/07: Fix bitvectors and non-zero lower bound arrays in truncated-print |
Date: |
Mon, 27 Feb 2017 20:19:39 -0500 (EST) |
lloda pushed a commit to branch wip-exception-truncate
in repository guile.
commit f87f2c81c942ad56fc06d6753efb7daece7a6d70
Author: Daniel Llorens <address@hidden>
Date: Tue Feb 21 12:23:35 2017 +0100
Fix bitvectors and non-zero lower bound arrays in truncated-print
* module/ice-9/arrays.scm (array-print-prefix): New private function.
* libguile/arrays.c (scm_i_print_array): Reuse (array-print-prefix) from
(ice-9 arrays). Make sure to release the array handle.
* module/ice-9/pretty-print.scm (truncated-print): Support
bitvectors.
Don't try to guess the array prefix but call array-print-prefix from
(ice-9 arrays) instead.
Fix call to print-sequence to support non-zero lower bound arrays.
* test-suite/tests/arrays.test: Test that arrays print properly.
* test-suite/tests/print.test: Test truncated-print with bitvectors,
non-zero lower bound arrays.
---
libguile/arrays.c | 48 +++++++----------------------------
module/ice-9/arrays.scm | 40 ++++++++++++++++++++++++++++-
module/ice-9/pretty-print.scm | 24 ++++++++++++------
test-suite/tests/arrays.test | 55 +++++++++++++++++++++++++++++++++++++++-
test-suite/tests/print.test | 58 +++++++++++++++++++++++++++++++++++++------
5 files changed, 169 insertions(+), 56 deletions(-)
diff --git a/libguile/arrays.c b/libguile/arrays.c
index 8b8bc48..682fbf6 100644
--- a/libguile/arrays.c
+++ b/libguile/arrays.c
@@ -908,50 +908,17 @@ scm_i_print_array_dimension (scm_t_array_handle *h, int
dim, int pos,
return 1;
}
-/* Print an array.
-*/
-
int
scm_i_print_array (SCM array, SCM port, scm_print_state *pstate)
{
scm_t_array_handle h;
- size_t i;
- int print_lbnds = 0, zero_size = 0, print_lens = 0;
+ int d;
+ scm_call_2 (scm_c_private_ref ("ice-9 arrays", "array-print-prefix"),
+ array, port);
+
scm_array_get_handle (array, &h);
- scm_putc ('#', port);
- if (SCM_I_ARRAYP (array))
- scm_intprint (h.ndims, 10, port);
- if (h.element_type != SCM_ARRAY_ELEMENT_TYPE_SCM)
- scm_write (scm_array_handle_element_type (&h), port);
-
- for (i = 0; i < h.ndims; i++)
- {
- if (h.dims[i].lbnd != 0)
- print_lbnds = 1;
- if (h.dims[i].ubnd - h.dims[i].lbnd + 1 == 0)
- zero_size = 1;
- else if (zero_size)
- print_lens = 1;
- }
-
- if (print_lbnds || print_lens)
- for (i = 0; i < h.ndims; i++)
- {
- if (print_lbnds)
- {
- scm_putc ('@', port);
- scm_intprint (h.dims[i].lbnd, 10, port);
- }
- if (print_lens)
- {
- scm_putc (':', port);
- scm_intprint (h.dims[i].ubnd - h.dims[i].lbnd + 1,
- 10, port);
- }
- }
-
if (h.ndims == 0)
{
/* Rank zero arrays, which are really just scalars, are printed
@@ -977,10 +944,13 @@ scm_i_print_array (SCM array, SCM port, scm_print_state
*pstate)
scm_putc ('(', port);
scm_i_print_array_dimension (&h, 0, 0, port, pstate);
scm_putc (')', port);
- return 1;
+ d = 1;
}
else
- return scm_i_print_array_dimension (&h, 0, 0, port, pstate);
+ d = scm_i_print_array_dimension (&h, 0, 0, port, pstate);
+
+ scm_array_handle_release (&h);
+ return d;
}
void
diff --git a/module/ice-9/arrays.scm b/module/ice-9/arrays.scm
index 2c04b2e..f03eb35 100644
--- a/module/ice-9/arrays.scm
+++ b/module/ice-9/arrays.scm
@@ -17,9 +17,13 @@
;;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
USA
(define-module (ice-9 arrays)
+ #:use-module (rnrs io ports)
+ #:use-module (srfi srfi-1)
#:export (array-copy))
-; This is actually defined in boot-9.scm, apparently for b.c.
+;; This is actually defined in boot-9.scm, apparently for backwards
+;; compatibility.
+
;; (define (array-shape a)
;; (map (lambda (ind) (if (number? ind) (list 0 (+ -1 ind)) ind))
;; (array-dimensions a)))
@@ -30,3 +34,37 @@
(array-copy! a b)
b))
+
+;; Printing arrays
+
+;; The dimensions aren't printed out unless they cannot be deduced from
+;; the content, which happens only when certain axes are empty. #:dims?
+;; can be used to force this printing. An array with all the dimensions
+;; printed out is still readable syntax, this can be useful for
+;; truncated-print.
+
+(define* (array-print-prefix a port #:key dims?)
+ (put-char port #\#)
+ (display (array-rank a) port)
+ (let ((t (array-type a)))
+ (unless (eq? #t t)
+ (display t port)))
+ (let ((ss (array-shape a)))
+ (let loop ((s ss) (slos? #f) (szero? #f) (slens? dims?))
+ (define lo caar)
+ (define hi cadar)
+ (if (null? s)
+ (when (or slos? slens?)
+ (pair-for-each (lambda (s)
+ (when slos?
+ (put-char port #\@)
+ (display (lo s) port))
+ (when slens?
+ (put-char port #\:)
+ (display (- (hi s) (lo s) -1) port)))
+ ss))
+ (let ((zero-size? (zero? (- (hi s) (lo s) -1))))
+ (loop (cdr s)
+ (or slos? (not (zero? (lo s))))
+ (or szero? zero-size?)
+ (or slens? (and (not zero-size?) szero?))))))))
diff --git a/module/ice-9/pretty-print.scm b/module/ice-9/pretty-print.scm
index d3d7652..f90e15d 100644
--- a/module/ice-9/pretty-print.scm
+++ b/module/ice-9/pretty-print.scm
@@ -429,17 +429,25 @@ sub-expression, via the @var{breadth-first?} keyword
argument."
(display ")"))
(else
(display "#"))))
+ ((bitvector? x)
+ (cond
+ ((>= width (+ 2 (array-length x)))
+ (format #t "~a" x))
+ ;; the truncated bitvector would print as #1b(...), so we print by
hand.
+ ((>= width (+ 2 ellipsis-width))
+ (format #t "#*")
+ (array-for-each (lambda (xi) (format #t (if xi "1" "0")))
+ (make-shared-array x list (- width 2
ellipsis-width)))
+ (format #t ellipsis))
+ (else
+ (display "#"))))
((and (array? x) (not (string? x)))
(let* ((type (array-type x))
(prefix
(if inner?
""
- (if (zero? (array-rank x))
- (string-append "#0" (if (eq? #t type) "" (symbol->string
type)))
- (let ((s (format #f "~a"
- (apply make-typed-array type *unspecified*
- (make-list (array-rank x) 0)))))
- (substring s 0 (- (string-length s) 2))))))
+ (call-with-output-string
+ (lambda (s) ((@@ (ice-9 arrays) array-print-prefix) x s)))))
(width-prefix (string-length prefix)))
(cond
((>= width (+ 2 width-prefix ellipsis-width))
@@ -447,7 +455,9 @@ sub-expression, via the @var{breadth-first?} keyword
argument."
(if (zero? (array-rank x))
(print (array-ref x) (- width width-prefix 2))
(print-sequence x (- width width-prefix 2) (array-length x)
- array-cell-ref identity
+ (let ((base (caar (array-shape x))))
+ (lambda (x i) (array-cell-ref x (+ base i))))
+ identity
#:inner? (< 1 (array-rank x))))
(display ")"))
(else
diff --git a/test-suite/tests/arrays.test b/test-suite/tests/arrays.test
index 1df77b1..e913e30 100644
--- a/test-suite/tests/arrays.test
+++ b/test-suite/tests/arrays.test
@@ -999,4 +999,57 @@
"#1(b c)"
(format #f "~a" (make-shared-array #(a b c)
(lambda (i) (list (+ i 1)))
- 2))))
+ 2)))
+
+ (pass-if-equal "0-array"
+ "#0(9)"
+ (format #f "~a" (make-array 9)))
+
+ (pass-if-equal "2-array"
+ "#2f64((0.0 1.0) (2.0 3.0))"
+ (format #f "~a" #2f64((0 1) (2 3))))
+
+ (pass-if-equal "empty 3-array"
+ "#3()"
+ (format #f "~a" (make-array 1 0 0 0)))
+
+ (pass-if-equal "empty 3-array with last nonempty dim."
+ "#3:0:0:1()"
+ (format #f "~a" (make-array 1 0 0 1)))
+
+ (pass-if-equal "empty 3-array with middle nonempty dim."
+ "#3:0:1:0()"
+ (format #f "~a" (make-array 1 0 1 0)))
+
+ (pass-if-equal "empty 3-array with first nonempty dim."
+ "#3(())"
+ (format #f "~a" (make-array 1 1 0 0)))
+
+ (pass-if-equal "3-array with non-zero lower bounds"
+ "address@hidden@address@hidden(((1 1 1) (1 1 1)) ((1 1 1) (1 1 1)))"
+ (format #f "~a" (make-array 1 '(1 2) '(0 1) '(1 3))))
+
+ (pass-if-equal "3-array with non-zero-lower bounds and last nonempty dim."
+ "address@hidden:address@hidden:address@hidden:3()"
+ (format #f "~a" (make-array 1 0 0 '(1 3))))
+
+ (pass-if-equal "3-array with non-zero-lower bounds and middle nonempty dim."
+ "address@hidden:address@hidden:address@hidden:0()"
+ (format #f "~a" (make-array 1 0 '(1 3) 0)))
+
+ (pass-if-equal "3-array with non-zero-lower bounds and first nonempty dim."
+ "address@hidden@address@hidden(() () ())"
+ (format #f "~a" (make-array 1 '(1 3) 0 0)))
+
+ (pass-if-equal "3-array with singleton dim case I"
+ "address@hidden@address@hidden(((1 1 1)))"
+ (format #f "~a" (make-array 1 '(1 1) '(1 1) '(-1 1))))
+
+ (pass-if-equal "3-array with singleton dim case II"
+ "address@hidden@address@hidden(((1) (1) (1)))"
+ (format #f "~a" (make-array 1 '(-1 -1) '(1 3) '(1 1))))
+
+ (pass-if-equal "3-array with singleton dim case III"
+ "address@hidden@address@hidden(((1)) ((1)) ((1)))"
+ (format #f "~a" (make-array 1 '(1 3) '(-1 -1) '(1 1)))))
+
diff --git a/test-suite/tests/print.test b/test-suite/tests/print.test
index 82cc776..f2e3145 100644
--- a/test-suite/tests/print.test
+++ b/test-suite/tests/print.test
@@ -147,6 +147,35 @@
(pass-if-equal "#<directory (test-…>"
(tprint (current-module) 20 "UTF-8"))
+ ;; bitvectors
+
+ (let ((testv (bitvector #t #f #f #t #t #f #t #t)))
+ (pass-if-equal "#*10011011"
+ (tprint testv 11 "UTF-8"))
+
+ (pass-if-equal "#*10011011"
+ (tprint testv 11 "ISO-8859-1"))
+
+ (pass-if-equal "#*10011…"
+ (tprint testv 8 "UTF-8"))
+
+ (pass-if-equal "#*100..."
+ (tprint testv 8 "ISO-8859-1"))
+
+ (pass-if-equal "#*10…"
+ (tprint testv 5 "UTF-8"))
+
+ (pass-if-equal "#*..."
+ (tprint testv 5 "ISO-8859-1"))
+
+ (pass-if-equal "#*1…"
+ (tprint testv 4 "UTF-8"))
+
+ (pass-if-equal "#"
+ (tprint testv 4 "ISO-8859-1")))
+
+ ;; rank 0 arrays
+
(pass-if-equal "#0(#)"
(tprint (make-typed-array #t 9.0) 6 "UTF-8"))
@@ -162,18 +191,31 @@
(pass-if-equal "#"
(tprint (make-typed-array 's32 0 20 20) 7 "UTF-8"))
- (pass-if-equal "#2s32(…)"
- (tprint (make-typed-array 's32 0 20 20) 8 "UTF-8"))
+ ;; higher dimensional arrays
+
+ (let ((testa (make-typed-array 's32 0 20 20)))
+ (pass-if-equal "#2s32(…)"
+ (tprint testa 8 "UTF-8"))
+
+ (pass-if-equal "#2s32(# …)"
+ (tprint testa 10 "UTF-8"))
- (pass-if-equal "#2s32(# …)"
- (tprint (make-typed-array 's32 0 20 20) 10 "UTF-8"))
+ (pass-if-equal "#2s32((…) …)"
+ (tprint testa 12 "UTF-8"))
- (pass-if-equal "#2s32((…) …)"
- (tprint (make-typed-array 's32 0 20 20) 12 "UTF-8"))
+ (pass-if-equal "#2s32((0 …) …)"
+ (tprint testa 14 "UTF-8")))
- (pass-if-equal "#2s32((0 …) …)"
- (tprint (make-typed-array 's32 0 20 20) 14 "UTF-8"))
+ ;; check that bounds are printed correctly
+ (pass-if-equal "address@hidden@0((foo foo foo foo …) …)"
+ (tprint (make-array 'foo '(-1 3) 5) 30 "UTF-8"))
+
+ (pass-if-equal "address@hidden:address@hidden:address@hidden:5(() () () # #)"
+ (tprint (make-array 'foo '(-1 3) 0 5) 30 "UTF-8"))
+
+ ;; nested objects including arrays
+
(pass-if-equal "#2((#(9 9) #(9 9)) (#(9 9) #(9 9)))"
(tprint (make-typed-array #t (make-typed-array #t 9 2) 2 2) 40 "UTF-8"))
- [Guile-commits] branch wip-exception-truncate updated (0b0ee66 -> 6118d9d), Daniel Llorens, 2017/02/27
- [Guile-commits] 01/07: Replace uniform-vector-read benchmark with bytevector-io benchmark, Daniel Llorens, 2017/02/27
- [Guile-commits] 02/07: Remove documentation on uniform-vector-read!, uniform-vector-write, Daniel Llorens, 2017/02/27
- [Guile-commits] 04/07: Support non-zero lower bounds in array-slice-for-each, Daniel Llorens, 2017/02/27
- [Guile-commits] 03/07: Fix sort, sort! for arrays with nonzero lower bound, Daniel Llorens, 2017/02/27
- [Guile-commits] 06/07: Remove scm_generalized_vector_get_handle, Daniel Llorens, 2017/02/27
- [Guile-commits] 07/07: (wip) give a handle into format used in exceptions, Daniel Llorens, 2017/02/27
- [Guile-commits] 05/07: Fix bitvectors and non-zero lower bound arrays in truncated-print,
Daniel Llorens <=