emacs-devel
[Top][All Lists]
Advanced

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

Re: hash-table-{to, from}-alist


From: Ted Zlatanov
Subject: Re: hash-table-{to, from}-alist
Date: Tue, 25 Nov 2008 17:50:45 -0600
User-agent: Gnus/5.110011 (No Gnus v0.11) Emacs/23.0.60 (gnu/linux)

Here's a patch to print.c to implement printing out a hashtable's
properties and content as discussed.  I *think* I'm doing it right based
on what I see elsewhere in print.c and fns.c.  I rely on print_object to
do the work.  I know the indentation is off, sorry.

This patch does:

(let ((h (make-hash-table :rehash-size 20.0 :rehash-threshold 0.001 :weakness 
'key))) 
  (puthash 1 2 h) 
  (puthash 5 'env h) 
  (print h))

--> #<hash-table 'eql key 2/65 0x83c49a0>

        #s<hash-table size 65 test 'eql weak 'key rehash-size 20.0 
rehash-threshold 0.001 data (1 2 5 env )>

I know it's broken for circular references (e.g. (puthash 6 h h)
above).  How should I handle that?

The new output is offset and printed after the old-style output as a
sanity check.  Let me know if the code is OK and I'll make it the only
output of printing a hashtable, clean it up (based on any comments I get
and the TODO items), and go on to writing the read code.  I won't commit
it of course, just keep it as a patch until the trunk is open again.

Thanks
Ted

Index: print.c
===================================================================
RCS file: /sources/emacs/emacs/src/print.c,v
retrieving revision 1.253
diff -c -r1.253 print.c
*** print.c     31 Jul 2008 05:33:53 -0000      1.253
--- print.c     25 Nov 2008 23:40:54 -0000
***************
*** 2052,2057 ****
--- 2052,2108 ----
          sprintf (buf, " 0x%lx", (unsigned long) h);
          strout (buf, -1, -1, printcharfun, 0);
          PRINTCHAR ('>');
+ 
+ /*
+   implement a readable output:
+   #s(hash-table size 2 test equal data (k1 v1 k2 v2))
+ */
+           /* always print the size */
+           sprintf (buf, "\n\n\t#s<hash-table size %ld", (long) XVECTOR 
(h->next)->size);
+           strout (buf, -1, -1, printcharfun, 0);
+ 
+         if (SYMBOLP (h->test))
+           {
+               strout (" test '", -1, -1, printcharfun, 0);
+               print_object (h->test, printcharfun, 0);
+             }
+ 
+         if (SYMBOLP (h->weak) && ! NILP(h->weak))
+           {
+               strout (" weak '", -1, -1, printcharfun, 0);
+               print_object (h->weak, printcharfun, 0);
+             }
+ 
+         if (FLOATP (h->rehash_size) || INTEGERP (h->rehash_size))
+           {
+               strout (" rehash-size ", -1, -1, printcharfun, 0);
+               print_object (h->rehash_size, printcharfun, 0);
+             }
+ 
+           /* the rehash threshold must be 0 < threshold < 1 so it's never an 
integer */
+         if (FLOATP (h->rehash_threshold))
+           {
+               strout (" rehash-threshold ", -1, -1, printcharfun, 0);
+               print_object (h->rehash_threshold, printcharfun, 0);
+             }
+ 
+           strout (" data ", -1, -1, printcharfun, 0);
+ 
+           /* print the data here as a plist */
+           int i;
+ 
+           PRINTCHAR ('(');
+           for (i = 0; i < HASH_TABLE_SIZE (h); ++i)
+             if (!NILP (HASH_HASH (h, i)))
+               {
+                 print_object (HASH_KEY (h, i), printcharfun, 0);
+                 PRINTCHAR (' ');
+                 print_object (HASH_VALUE (h, i), printcharfun, 0);
+                 PRINTCHAR (' '); /* TODO: should not be printed at end */
+               }
+           PRINTCHAR (')');
+         PRINTCHAR ('>');
+ 
        }
        else if (BUFFERP (obj))
        {





reply via email to

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