From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: Ted Zlatanov Newsgroups: gmane.emacs.devel Subject: Re: hash-table-{to, from}-alist Date: Fri, 31 Jul 2009 13:10:47 -0500 Organization: =?utf-8?B?0KLQtdC+0LTQvtGAINCX0LvQsNGC0LDQvdC+0LI=?= @ Cienfuegos Message-ID: <87eirw4taw.fsf@lifelogs.com> References: <863aknitfg.fsf@lifelogs.com> <87vdufk6do.fsf@xemacs.org> <867i6tt4yz.fsf@lifelogs.com> <87bpw4k1z6.fsf@xemacs.org> <86bpw3d829.fsf@lifelogs.com> <87k5ari5jh.fsf@xemacs.org> <86prkiiia2.fsf@lifelogs.com> <86ej0ygr5j.fsf@lifelogs.com> <861vwygpc6.fsf@lifelogs.com> <868wqzd06x.fsf@lifelogs.com> <867i6hrrh9.fsf@lifelogs.com> <86prk8q8yn.fsf@lifelogs.com> <87eiry58qs.fsf@jumptrading.com> <87skge2dm6.fsf@stupidchicken.com> <87ab2m562t.fsf@lifelogs.com> <871vnyot6m.fsf@stupidchicken.com> NNTP-Posting-Host: lo.gmane.org Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="=-=-=" X-Trace: ger.gmane.org 1249063884 5337 80.91.229.12 (31 Jul 2009 18:11:24 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Fri, 31 Jul 2009 18:11:24 +0000 (UTC) To: emacs-devel@gnu.org Original-X-From: emacs-devel-bounces+ged-emacs-devel=m.gmane.org@gnu.org Fri Jul 31 20:11:17 2009 Return-path: Envelope-to: ged-emacs-devel@m.gmane.org Original-Received: from lists.gnu.org ([199.232.76.165]) by lo.gmane.org with esmtp (Exim 4.50) id 1MWwZY-0007yX-Pc for ged-emacs-devel@m.gmane.org; Fri, 31 Jul 2009 20:11:17 +0200 Original-Received: from localhost ([127.0.0.1]:47651 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1MWwZY-0003fN-72 for ged-emacs-devel@m.gmane.org; Fri, 31 Jul 2009 14:11:16 -0400 Original-Received: from mailman by lists.gnu.org with tmda-scanned (Exim 4.43) id 1MWwZT-0003f8-HV for emacs-devel@gnu.org; Fri, 31 Jul 2009 14:11:11 -0400 Original-Received: from exim by lists.gnu.org with spam-scanned (Exim 4.43) id 1MWwZO-0003eU-Ka for emacs-devel@gnu.org; Fri, 31 Jul 2009 14:11:10 -0400 Original-Received: from [199.232.76.173] (port=47012 helo=monty-python.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1MWwZO-0003eR-Hs for emacs-devel@gnu.org; Fri, 31 Jul 2009 14:11:06 -0400 Original-Received: from main.gmane.org ([80.91.229.2]:49138 helo=ciao.gmane.org) by monty-python.gnu.org with esmtps (TLS-1.0:RSA_AES_256_CBC_SHA1:32) (Exim 4.60) (envelope-from ) id 1MWwZN-0004vs-Jb for emacs-devel@gnu.org; Fri, 31 Jul 2009 14:11:06 -0400 Original-Received: from list by ciao.gmane.org with local (Exim 4.43) id 1MWwZG-0005bZ-CR for emacs-devel@gnu.org; Fri, 31 Jul 2009 18:10:58 +0000 Original-Received: from 38.98.147.130 ([38.98.147.130]) by main.gmane.org with esmtp (Gmexim 0.1 (Debian)) id 1AlnuQ-0007hv-00 for ; Fri, 31 Jul 2009 18:10:58 +0000 Original-Received: from tzz by 38.98.147.130 with local (Gmexim 0.1 (Debian)) id 1AlnuQ-0007hv-00 for ; Fri, 31 Jul 2009 18:10:58 +0000 X-Injected-Via-Gmane: http://gmane.org/ Original-Lines: 275 Original-X-Complaints-To: usenet@ger.gmane.org X-Gmane-NNTP-Posting-Host: 38.98.147.130 X-Face: bd.DQ~'29fIs`T_%O%C\g%6jW)yi[zuz6; d4V0`@y-~$#3P_Ng{@m+e4o<4P'#(_GJQ%TT= D}[Ep*b!\e,fBZ'j_+#"Ps?s2!4H2-Y"sx" User-Agent: Gnus/5.110011 (No Gnus v0.11) Emacs/23.1.50 (gnu/linux) Cancel-Lock: sha1:AkzZh40KQFOE5bX+rMcXMmkkkPA= X-detected-operating-system: by monty-python.gnu.org: GNU/Linux 2.6, seldom 2.4 (older, 4) X-BeenThere: emacs-devel@gnu.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: "Emacs development discussions." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Original-Sender: emacs-devel-bounces+ged-emacs-devel=m.gmane.org@gnu.org Errors-To: emacs-devel-bounces+ged-emacs-devel=m.gmane.org@gnu.org Xref: news.gmane.org gmane.emacs.devel:113473 Archived-At: --=-=-= On Thu, 30 Jul 2009 17:04:22 -0400 Stefan Monnier wrote: >>> OK. Can I use two names: "hashtable-read" and "hashtable-print" for the >>> feature? I can't think of a single name that doesn't sound clumsy >>> (suggestions welcome, of course). >> Just hashtable-read, I think. After all, hashtables already have a >> print representation (all Lisp objects do)---the present difficulty is >> that this print representation isn't readable. SM> Or hashtable-print-readable. Attached is the patch for review. I've been using it for a while with no issues, FWIW. Thanks Ted --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=hashprint.patch ? hashprint.patch Index: lread.c =================================================================== RCS file: /sources/emacs/emacs/src/lread.c,v retrieving revision 1.406 diff -c -r1.406 lread.c *** lread.c 29 Apr 2009 03:03:00 -0000 1.406 --- lread.c 31 Jul 2009 18:09:25 -0000 *************** *** 80,85 **** --- 80,93 ---- extern int errno; #endif + /* hash table read constants */ + Lisp_Object Qhash_table, Qdata; + Lisp_Object Qtest, Qsize; + Lisp_Object Qweakness; + Lisp_Object Qrehash_size; + Lisp_Object Qrehash_threshold; + extern Lisp_Object QCtest, QCsize, QCrehash_size, QCrehash_threshold, QCweakness; + Lisp_Object Qread_char, Qget_file_char, Qstandard_input, Qcurrent_load_list; Lisp_Object Qvariable_documentation, Vvalues, Vstandard_input, Vafter_load_alist; Lisp_Object Qascii_character, Qload, Qload_file_name; *************** *** 2341,2346 **** --- 2349,2426 ---- case '#': c = READCHAR; + if (c == 's') + { + c = READCHAR; + if (c == '(') + { + /* Accept extended format for hashtables (extensible to + other types), e.g. + #s(hash-table size 2 test equal data (k1 v1 k2 v2)) */ + Lisp_Object tmp = read_list (0, readcharfun); + Lisp_Object head = CAR_SAFE (tmp); + Lisp_Object data = Qnil; + Lisp_Object val = Qnil; + /* The size is 2 * number of allowed keywords to + make-hash-table. */ + Lisp_Object params[10]; + Lisp_Object ht; + Lisp_Object key = Qnil; + int param_count = 0; + int i; + + if (!EQ (head, Qhash_table)) + error ("Invalid extended read marker at head of #s list " + "(only hash-table allowed)"); + + tmp = CDR_SAFE (tmp); + + /* This is repetitive but fast and simple. */ + params[param_count] = QCsize; + params[param_count+1] = Fplist_get (tmp, Qsize); + if (!NILP (params[param_count+1])) + param_count+=2; + + params[param_count] = QCtest; + params[param_count+1] = Fplist_get (tmp, Qtest); + if (!NILP (params[param_count+1])) + param_count+=2; + + params[param_count] = QCweakness; + params[param_count+1] = Fplist_get (tmp, Qweakness); + if (!NILP (params[param_count+1])) + param_count+=2; + + params[param_count] = QCrehash_size; + params[param_count+1] = Fplist_get (tmp, Qrehash_size); + if (!NILP (params[param_count+1])) + param_count+=2; + + params[param_count] = QCrehash_threshold; + params[param_count+1] = Fplist_get (tmp, Qrehash_threshold); + if (!NILP (params[param_count+1])) + param_count+=2; + + /* This is the hashtable data. */ + data = Fplist_get (tmp, Qdata); + + /* Now use params to make a new hashtable and fill it. */ + ht = Fmake_hash_table (param_count, params); + + while (CONSP (data)) + { + key = XCAR (data); + data = XCDR (data); + if (!CONSP (data)) + error ("Odd number of elements in hashtable data"); + val = XCAR (data); + data = XCDR (data); + Fputhash (key, val, ht); + } + + return ht; + } + } if (c == '^') { c = READCHAR; *************** *** 4436,4441 **** --- 4516,4536 ---- Vloads_in_progress = Qnil; staticpro (&Vloads_in_progress); + + Qhash_table = intern ("hash-table"); + staticpro (&Qhash_table); + Qdata = intern ("data"); + staticpro (&Qdata); + Qtest = intern ("test"); + staticpro (&Qtest); + Qsize = intern ("size"); + staticpro (&Qsize); + Qweakness = intern ("weakness"); + staticpro (&Qweakness); + Qrehash_size = intern ("rehash-size"); + staticpro (&Qrehash_size); + Qrehash_threshold = intern ("rehash-threshold"); + staticpro (&Qrehash_threshold); } /* arch-tag: a0d02733-0f96-4844-a659-9fd53c4f414d Index: print.c =================================================================== RCS file: /sources/emacs/emacs/src/print.c,v retrieving revision 1.256 diff -c -r1.256 print.c *** print.c 3 Apr 2009 06:23:49 -0000 1.256 --- print.c 31 Jul 2009 18:09:25 -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))) *************** *** 2031,2036 **** --- 2033,2039 ---- else if (HASH_TABLE_P (obj)) { struct Lisp_Hash_Table *h = XHASH_TABLE (obj); + #if 0 strout ("#test)) { *************** *** 2047,2052 **** --- 2050,2116 ---- 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 real_size = HASH_TABLE_SIZE (h); + int size = real_size; + + /* Don't print more elements than the specified maximum. */ + if (NATNUMP (Vprint_length) + && XFASTINT (Vprint_length) < size) + size = XFASTINT (Vprint_length); + + PRINTCHAR ('('); + for (i = 0; i < size; i++) + if (!NILP (HASH_HASH (h, i))) + { + if (i) PRINTCHAR (' '); + print_object (HASH_KEY (h, i), printcharfun, 0); + PRINTCHAR (' '); + print_object (HASH_VALUE (h, i), printcharfun, 0); + } + + if (size < real_size) + strout (" ...", 4, 4, printcharfun, 0); + + PRINTCHAR (')'); + PRINTCHAR (')'); + } else if (BUFFERP (obj)) { *************** *** 2354,2359 **** --- 2418,2425 ---- Qfloat_output_format = intern ("float-output-format"); staticpro (&Qfloat_output_format); + Fprovide (intern ("hashtable-print-readable"), Qnil); + DEFVAR_LISP ("print-length", &Vprint_length, doc: /* Maximum length of list to print before abbreviating. A value of nil means no limit. See also `eval-expression-print-length'. */); --=-=-=--