Re: printing readably an array of type bit!
- From: Barry Margolin <barmar@xxxxxxxxxxxx>
- Date: Thu, 30 Oct 2008 21:46:17 -0400
In article
<5af4dd9e-a6a6-4e39-bd5a-265f9e61d1d4@xxxxxxxxxxxxxxxxxxxxxxxxxxxx>,
"\"(typep 'nil (statisfies 'identity))\"" <christophamort@xxxxxxxx>
wrote:
I like bit arrays because they are so fast in computing bit
operations, but would like to store their content. ... so I ran into
following error:
(let ((bit-array (make-array (list 3 3) :element-type 'bit
:initial-contents
'((0 0 0) (1 1 1) (0 0
0))))
(*print-readably* t)
(*print-array* t))
(print bit-array))
Error: Unable to print #2A((0 0 0) (1 1 1) (0 0 0)) readably and
*print-readably* is true.
[condition type: print-not-readable]
... if I remowe the (*print-readably* t) it prints normal as expected
the array:
=> #2A((0 0 0) (1 1 1) (0 0 0))
... but this is not a (simple-array bit *), it is of type (array t).
Right. There's no read syntax for specifying the element type of an
array, so specialized arrays (except for special cases like strings and
bit-vectors) are not readable.
(let ((bit-array (make-array (list 3 3)
:element-type 'bit
:initial-contents
'((0 0 0) (1 1 1) (0 0 0)))))
(array-element-type (read-from-string (write-to-string bit-
array :array t))))
=> t
Then I was thinking to define my own print-object method:
(defmethod print-object ((array0 array) (stream0 stream))
(format stream0 "#.~S"
`(make-array ,(array-dimensions array0)
:element-type (array-element-type array0)
:initial-contents ,(loop for i from 0 below (array-
dimension array0 0)
collect (loop for j from 0
below (array-dimension array0 0)
collect (bit
array0 i j))))))
Shouldn't the J loop use (array-dimension array0 1)? Your code will
only work for square arrays.
And what do you expect to happen when you try to print arrays that have
more or less than 2 dimensions?
.... when I evaluate the expression my implementation (Allegro 7.1)
fails gracefully like a stone!
You're not allowed to define PRINT-OBJECT methods on standard classes,
because this may conflict with the implementation's own methods. See
item #19 in section 11.1.2.1.2 of the CLHS.
So, what to do?
- Add a specialized method for bit arrays ...
- Write a non generic Function, and every time I print an object I've
to check if it is
an array of type bit ... pffff. NOT NICE!
- find a way to use the #* notation of bit-vectors?
Define your own generic function for printing your application's data.
The method for T can simply call PRINT-OBJECT. The method for ARRAY can
check if it's a 2-dimensional bit array and use the above code (or you
could define your own read-macro to go along with it), otherwise it can
simply call PRINT-OBJECT.
--
Barry Margolin, barmar@xxxxxxxxxxxx
Arlington, MA
*** PLEASE post questions in newsgroups, not directly to me ***
*** PLEASE don't copy me on replies, I'll read them in the group ***
.
- Follow-Ups:
- Re: printing readably an array of type bit!
- From: \"(typep 'nil (statisfies 'identity))\"
- Re: printing readably an array of type bit!
- References:
- printing readably an array of type bit!
- From: \"(typep 'nil (statisfies 'identity))\"
- printing readably an array of type bit!
- Prev by Date: Re: CLOS and the condition system in the CLHS
- Next by Date: Re: CLOS and the condition system in the CLHS
- Previous by thread: printing readably an array of type bit!
- Next by thread: Re: printing readably an array of type bit!
- Index(es):
Relevant Pages
|