unofficial mirror of emacs-devel@gnu.org 
 help / color / mirror / code / Atom feed
From: Ted Zlatanov <tzz@lifelogs.com>
To: emacs-devel@gnu.org
Subject: Re: hash-table-{to, from}-alist
Date: Tue, 25 Nov 2008 17:50:45 -0600	[thread overview]
Message-ID: <86d4gjnz56.fsf@lifelogs.com> (raw)
In-Reply-To: 86bpw3d829.fsf@lifelogs.com

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	other threads:[~2008-11-25 23:50 UTC|newest]

Thread overview: 86+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <34f9604c-a23b-4ad9-9c84-f45884a6df23@x16g2000prn.googlegroups.com>
     [not found] ` <mailman.17732.1219901096.18990.help-gnu-emacs@gnu.org>
     [not found]   ` <bbbc0efc-affc-42dd-bfc0-ebe217ad7073@z6g2000pre.googlegroups.com>
     [not found]     ` <86od3dfd86.fsf@lifelogs.com>
     [not found]       ` <mailman.17796.1219947087.18990.help-gnu-emacs@gnu.org>
     [not found]         ` <868wuflxv9.fsf@lifelogs.com>
     [not found]           ` <mailman.17897.1220021571.18990.help-gnu-emacs@gnu.org>
2008-08-29 18:10             ` print hash table to disk and reread in hash table Ted Zlatanov
2008-08-30  5:18               ` tomas
2008-08-30  9:17                 ` Ted Zlatanov
2008-08-30 12:03                   ` tomas
2008-11-17 17:15                   ` Ted Zlatanov
2008-11-19 21:37                     ` hash-table-{to, from}-alist (was: print hash table to disk and reread in hash table) Ted Zlatanov
2008-11-19 21:57                       ` hash-table-{to, from}-alist Glenn Morris
2008-11-20 19:07                         ` Ted Zlatanov
2008-11-21 22:02                       ` Stefan Monnier
2008-11-21 22:22                         ` Ted Zlatanov
2008-11-22  3:18                           ` Stefan Monnier
2008-11-22  5:45                             ` tomas
2008-11-22 12:27                               ` Stephen J. Turnbull
2008-11-22 15:21                                 ` tomas
2008-11-22 17:38                                   ` Stephen J. Turnbull
2008-11-24 15:44                                     ` Richard M Stallman
2008-11-24 16:58                                       ` Stefan Monnier
2008-11-24 17:21                                       ` Ted Zlatanov
2008-11-25  1:50                                         ` Stephen J. Turnbull
2008-11-25 17:33                                           ` Ted Zlatanov
2008-11-25 23:50                                             ` Ted Zlatanov [this message]
2008-11-26  1:46                                               ` Stefan Monnier
2008-11-26  2:16                                                 ` David De La Harpe Golden
2008-11-26  3:48                                                   ` Stefan Monnier
2008-11-26  5:46                                                     ` David De La Harpe Golden
2008-11-26  2:29                                             ` Stephen J. Turnbull
2008-11-26  3:34                                               ` David De La Harpe Golden
2008-11-26 16:06                                               ` Ted Zlatanov
2008-11-26 18:10                                                 ` Stefan Monnier
2008-11-26 20:37                                                   ` Ted Zlatanov
2008-11-26 21:16                                                     ` Ted Zlatanov
2008-12-01 22:01                                                       ` Ted Zlatanov
2008-12-02  0:13                                                         ` Andreas Schwab
2008-12-02 14:27                                                           ` Ted Zlatanov
2008-12-02 21:59                                                           ` Stefan Monnier
2008-12-02 22:20                                                             ` Andreas Schwab
2008-12-02 20:56                                                         ` Ted Zlatanov
2008-12-02 21:27                                                           ` Andreas Schwab
2008-12-02 21:58                                                         ` Stefan Monnier
2008-12-03 19:25                                                           ` Ted Zlatanov
2008-12-04  2:05                                                             ` Stefan Monnier
2008-12-04  6:23                                                               ` Stephen J. Turnbull
2008-12-04  6:34                                                                 ` Miles Bader
2008-12-04  9:08                                                                   ` Andreas Schwab
2008-12-04 13:18                                                                   ` Stefan Monnier
2008-12-04 15:02                                                               ` Ted Zlatanov
2008-12-04 19:34                                                                 ` Stefan Monnier
2008-12-04 19:57                                                                   ` Ted Zlatanov
2009-07-30 18:24                                                                 ` Ted Zlatanov
2009-07-30 19:08                                                                   ` Chong Yidong
2009-07-30 19:22                                                                     ` Ted Zlatanov
2009-07-30 19:40                                                                       ` Chong Yidong
2009-07-30 21:04                                                                         ` Stefan Monnier
2009-07-31  1:30                                                                           ` Stefan Monnier
2009-07-31 17:49                                                                             ` Ted Zlatanov
2009-07-31 19:03                                                                               ` Stefan Monnier
2009-07-31 20:25                                                                                 ` Ted Zlatanov
2009-08-01  2:22                                                                                   ` Stephen J. Turnbull
2009-08-01 11:35                                                                                   ` Chong Yidong
2009-08-03 14:53                                                                                     ` Ted Zlatanov
2009-08-05  9:20                                                                                       ` Ted Zlatanov
2009-08-05 13:48                                                                                         ` Chong Yidong
2009-08-05 16:48                                                                                           ` Ted Zlatanov
2009-08-06 16:30                                                                                             ` Stefan Monnier
2009-08-06 16:47                                                                                               ` Ted Zlatanov
2009-08-08 18:36                                                                                                 ` Chong Yidong
2009-08-06 10:13                                                                                         ` Dan Nicolaescu
2009-08-06 16:58                                                                                         ` Florian Beck
2009-08-03 21:23                                                                                   ` Stefan Monnier
2009-07-31 18:10                                                                           ` Ted Zlatanov
2008-11-27  0:24                                                     ` Stephen J. Turnbull
2008-12-01 21:53                                                       ` Ted Zlatanov
2008-12-02  9:05                                                         ` Stephen J. Turnbull
2008-12-02 14:21                                                           ` Ted Zlatanov
2008-12-02 15:54                                                             ` Stephen J. Turnbull
2008-12-02 16:10                                                               ` Ted Zlatanov
2008-12-02 16:54                                                                 ` Stephen J. Turnbull
2008-12-02 22:19                                                         ` Stefan Monnier
2008-12-02 23:27                                                           ` Ted Zlatanov
2008-11-26 19:44                                                 ` Davis Herring
2008-11-26 20:41                                                   ` Ted Zlatanov
2008-11-25  2:57                                         ` Stefan Monnier
2008-11-25  7:02                                           ` Stephen J. Turnbull
2008-11-22  6:07                             ` Miles Bader
2008-11-22 15:27                               ` tomas
2008-11-22 18:57                             ` Ted Zlatanov

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/emacs/

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

  git send-email \
    --in-reply-to=86d4gjnz56.fsf@lifelogs.com \
    --to=tzz@lifelogs.com \
    --cc=emacs-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.
Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs.git

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).