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: Wed, 26 Nov 2008 15:16:41 -0600
User-agent: Gnus/5.110011 (No Gnus v0.11) Emacs/23.0.60 (gnu/linux)

On Wed, 26 Nov 2008 14:37:28 -0600 Ted Zlatanov <address@hidden> wrote: 

TZ> On Wed, 26 Nov 2008 13:10:38 -0500 Stefan Monnier <address@hidden> wrote: 
SM> The print code does 2 passes over the data: a first one to collect the
SM> set of objects it's going to print, and a second to do the actual print.
SM> This 2-pass approach is done to handle cycles and sharing (so that when
SM> an object appears several times in the output, we know it right when we
SM> print the first occurrence).  IIUC your patch only patches the second
SM> pass, but not the first.

TZ> I'll research this, thanks for explaining.

The patch below does all the previously discussed formatting plus
detection of circular references and Davis Herring's suggestion of
skipping the first preliminary space.  The old-style hashtable printout
is disabled with an #ifdef, so only the new style is available.

I tested it quite a bit, especially with compound data members, and it
seems to work all right.  If this patch is OK (Stefan, please make the
call since you've been looking at it) I'll go on to the reading code.

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     26 Nov 2008 21:15:18 -0000
***************
*** 1341,1346 ****
--- 1341,1347 ----
   loop:
    if (STRINGP (obj) || CONSP (obj) || VECTORP (obj)
        || COMPILEDP (obj) || CHAR_TABLE_P (obj) || SUB_CHAR_TABLE_P (obj)
+       || HASH_TABLE_P (obj)
        || (! NILP (Vprint_gensym)
          && SYMBOLP (obj)
          && !SYMBOL_INTERNED_P (obj)))
***************
*** 1536,1541 ****
--- 1537,1543 ----
    /* Detect circularities and truncate them.  */
    if (STRINGP (obj) || CONSP (obj) || VECTORP (obj)
        || COMPILEDP (obj) || CHAR_TABLE_P (obj) || SUB_CHAR_TABLE_P (obj)
+       || HASH_TABLE_P (obj)
        || (! NILP (Vprint_gensym)
          && SYMBOLP (obj)
          && !SYMBOL_INTERNED_P (obj)))
***************
*** 2036,2041 ****
--- 2038,2044 ----
        else if (HASH_TABLE_P (obj))
        {
          struct Lisp_Hash_Table *h = XHASH_TABLE (obj);
+ #if 0
          strout ("#<hash-table", -1, -1, printcharfun, 0);
          if (SYMBOLP (h->test))
            {
***************
*** 2052,2057 ****
--- 2055,2115 ----
          sprintf (buf, " 0x%lx", (unsigned long) h);
          strout (buf, -1, -1, printcharfun, 0);
          PRINTCHAR ('>');
+ #endif
+         /*
+           implement a readable output, e.g.:
+           #s(hash-table size 2 test equal data (k1 v1 k2 v2))
+         */
+         /* always print the size */
+         sprintf (buf, "#s(hash-table size %ld", (long) XVECTOR 
(h->next)->size);
+         strout (buf, -1, -1, printcharfun, 0);
+ 
+         if (!NILP(h->test))
+           {
+             strout (" test ", -1, -1, printcharfun, 0);
+             print_object (h->test, printcharfun, 0);
+           }
+ 
+         if (!NILP(h->weak))
+           {
+             strout (" weakness ", -1, -1, printcharfun, 0);
+             print_object (h->weak, printcharfun, 0);
+           }
+ 
+         if (!NILP(h->rehash_size))
+           {
+             strout (" rehash-size ", -1, -1, printcharfun, 0);
+             print_object (h->rehash_size, printcharfun, 0);
+           }
+ 
+         if (!NILP(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;
+         int printed=0;
+ 
+         PRINTCHAR ('(');
+         for (i = 0; i < HASH_TABLE_SIZE (h); ++i)
+           if (!NILP (HASH_HASH (h, i)))
+             {
+               if (printed)
+                 {
+                   PRINTCHAR (' ');
+                 }
+               print_object (HASH_KEY (h, i), printcharfun, 0);
+               PRINTCHAR (' ');
+               print_object (HASH_VALUE (h, i), printcharfun, 0);
+               printed = 1;
+             }
+         PRINTCHAR (')');
+         PRINTCHAR (')');
+ 
        }
        else if (BUFFERP (obj))
        {





reply via email to

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