unofficial mirror of guile-devel@gnu.org 
 help / color / mirror / Atom feed
From: daniel.llorens@bluewin.ch
To: guile-devel@gnu.org
Subject: [PATCH 5/6] Fix bitvectors and non-zero lower bound arrays in truncated-print
Date: Tue, 21 Feb 2017 12:59:34 +0100	[thread overview]
Message-ID: <20170221115935.32734-6-daniel.llorens@bluewin.ch> (raw)
In-Reply-To: <20170221115935.32734-1-daniel.llorens@bluewin.ch>

From: Daniel Llorens <daniel.llorens@bluewin.ch>

* module/ice-9/arrays.scm (array-print-prefix, array-print): New private
  functions.
* 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 | 26 +++++++++++++------
 test-suite/tests/arrays.test  | 55 +++++++++++++++++++++++++++++++++++++++-
 test-suite/tests/print.test   | 58 +++++++++++++++++++++++++++++++++++++------
 5 files changed, 170 insertions(+), 57 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))
 
+\f
+;; 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..5be108d 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
+               (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"
+      "#3@1@0@1(((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."
+      "#3@0:0@0:0@1: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."
+      "#3@0:0@1:3@0: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."
+      "#3@1@0@0(() () ())"
+      (format #f "~a" (make-array 1 '(1 3) 0 0)))
+  
+  (pass-if-equal "3-array with singleton dim case I"
+      "#3@1@1@-1(((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"
+      "#3@-1@1@1(((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"
+      "#3@1@-1@1(((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 "#2@-1@0((foo foo foo foo …) …)"
+      (tprint (make-array 'foo '(-1 3) 5) 30 "UTF-8"))
+
+  (pass-if-equal "#3@-1:5@0:0@0: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"))
 
-- 
2.10.1




  parent reply	other threads:[~2017-02-21 11:59 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-02-21 11:59 Better support for non-zero lower bound arrays [v2] daniel.llorens
2017-02-21 11:59 ` [PATCH 1/6] Replace uniform-vector-read benchmark with bytevector-io benchmark daniel.llorens
2017-02-21 11:59 ` [PATCH 2/6] Remove documentation on uniform-vector-read!, uniform-vector-write daniel.llorens
2017-02-21 11:59 ` [PATCH 3/6] Fix sort, sort! for arrays with nonzero lower bound daniel.llorens
2017-02-21 11:59 ` [PATCH 4/6] Support non-zero lower bounds in array-slice-for-each daniel.llorens
2017-02-21 11:59 ` daniel.llorens [this message]
2017-02-21 11:59 ` [PATCH 6/6] Remove scm_generalized_vector_get_handle daniel.llorens

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.gnu.org/software/guile/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20170221115935.32734-6-daniel.llorens@bluewin.ch \
    --to=daniel.llorens@bluewin.ch \
    --cc=guile-devel@gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).