guile-commits
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Guile-commits] 06/07: Fix bitvectors and non-zero lower bound arrays in


From: Daniel Llorens
Subject: [Guile-commits] 06/07: Fix bitvectors and non-zero lower bound arrays in truncated-print
Date: Mon, 13 Feb 2017 08:46:19 -0500 (EST)

lloda pushed a commit to branch wip-exception-truncate
in repository guile.

commit ab2c0f7b8092f71f912f340d1e7e2a4c42d22a59
Author: Daniel Llorens <address@hidden>
Date:   Mon Feb 13 14:03:30 2017 +0100

    Fix bitvectors and non-zero lower bound arrays in truncated-print
    
    * 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       | 66 ++++++++++++++++++++++++++++++++++++++++++-
 module/ice-9/pretty-print.scm | 26 +++++++++++------
 test-suite/tests/arrays.test  | 55 +++++++++++++++++++++++++++++++++++-
 test-suite/tests/print.test   | 58 +++++++++++++++++++++++++++++++------
 5 files changed, 196 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 5d5276c..4850522 100644
--- a/module/ice-9/arrays.scm
+++ b/module/ice-9/arrays.scm
@@ -17,6 +17,8 @@
 ;;; 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 typed-array-copy))
 
 ; This is actually defined in boot-9.scm, apparently for b.c.
@@ -24,7 +26,7 @@
 ;;   (map (lambda (ind) (if (number? ind) (list 0 (+ -1 ind)) ind))
 ;;        (array-dimensions a)))
 
-; FIXME writes over the array twice if (array-type) is #t
+;; FIXME writes over the array twice if (array-type) is #t
 (define (typed-array-copy t a)
   (let ((b (apply make-typed-array t *unspecified* (array-shape a))))
     (array-copy! a b)
@@ -33,3 +35,65 @@
 (define (array-copy a)
   (typed-array-copy (array-type a) a))
 
+
+;; Printing arrays
+;; FIXME replace scm_i_print_array() when performance allows.
+
+(define (array-print-prefix a port)
+  (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? #f))
+      (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?))))))))
+
+(define (array-print a port)
+  (define (array-rest a s)
+    (apply make-shared-array a (lambda i i)
+           (cons (list (+ 1 (caar s)) (cadar s)) (cdr s))))
+  (array-print-prefix a port)
+  (let ((ss (array-shape a)))
+    (cond
+     ((zero? (array-rank a))
+      (put-char port #\()
+      (display (array-ref a) port)
+      (put-char port #\)))
+     (else
+      (let loop ((a a) (s ss))
+        (put-char port #\()
+        (let ((len (- (cadar s) (caar s))))
+          (cond
+           ((= 1 (array-rank a))
+            (when (not (negative? len))
+              (display (array-ref a (caar s)) port)
+              (when (positive? len)
+                (array-for-each
+                 (lambda (x) (put-char port #\space) (display x port))
+                 (array-rest a s)))))
+           (else
+            (when (not (negative? len))
+              (loop (array-slice a (caar s)) (cdr s))
+              (when (positive? len)
+                (array-slice-for-each 1
+                  (lambda (x) (put-char port #\space) (loop x (cdr s)))
+                  (array-rest a s)))))))
+        (put-char port #\)))))))
+
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"
+      "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"))
 



reply via email to

[Prev in Thread] Current Thread [Next in Thread]