From: Ted Zlatanov <tzz@lifelogs.com>
To: emacs-devel@gnu.org
Subject: Re: hash-table-{to, from}-alist
Date: Mon, 01 Dec 2008 16:01:42 -0600 [thread overview]
Message-ID: <868wqzd06x.fsf@lifelogs.com> (raw)
In-Reply-To: 861vwygpc6.fsf@lifelogs.com
[-- Attachment #1: Type: text/plain, Size: 1987 bytes --]
On Wed, 26 Nov 2008 15:16:41 -0600 Ted Zlatanov <tzz@lifelogs.com> wrote:
TZ> The patch below does all the previously discussed formatting plus
TZ> detection of circular references and Davis Herring's suggestion of
TZ> skipping the first preliminary space. The old-style hashtable printout
TZ> is disabled with an #ifdef, so only the new style is available.
The attached patch does reading and writing of hashtables. I believe
it works correctly and handles the common error cases. Please review.
Here are the issues:
- I don't know how to manage a dynamic array of Lisp_Object objects, as
make-hash-table wants. What I have right now is hacky and breaks for
more than 5 parameters (e.g. reading #s(hash-table size 4 size 5 size
4 size 5 size 4 weakness x) will ignore the weakness parameter.
Ideally I'd put the arguments in a list and pass that off to
make-hash-table, but I couldn't find an example of that in the source
(casting a list of an array of Lisp_Object elements). Additionally I
use xmalloc without freeing the memory; I don't know if that will
cause issues but I saw it used in other places without freeing the
memory.
- it seems to me that
EQ(head, Qhash_table_size_marker) ||
EQ(head, Qhash_table_test_marker) ||
EQ(head, Qhash_table_weakness_marker) ||
EQ(head, Qhash_table_rehash_size_marker) ||
EQ(head, Qhash_table_rehash_threshold_marker))
can be written better, but I don't know enough to do it right. In
addition, I don't know how to convert all those parameters to their :
plist version, e.g. size => :size. I'm sure it's trivial but couldn't
find it in the source. I really wish I could write all this in ELisp :)
- my code is very deliberate, using CAR_SAFE and CDR_SAFE a lot. If
that causes performance issues, please suggest improvements. Ditto
for anything else--remember this is my first trip to the C side of
Emacs and treat everything with suspicion.
Thanks
Ted
[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: hashprint.patch --]
[-- Type: text/x-diff, Size: 6766 bytes --]
? hashprint.patch
Index: lread.c
===================================================================
RCS file: /sources/emacs/emacs/src/lread.c,v
retrieving revision 1.401
diff -u -r1.401 lread.c
--- lread.c 7 Sep 2008 20:41:10 -0000 1.401
+++ lread.c 1 Dec 2008 21:48:58 -0000
@@ -80,6 +80,13 @@
extern int errno;
#endif
+/* hash table read constants */
+Lisp_Object Qhash_table_read_marker, Qhash_table_data_marker;
+Lisp_Object Qhash_table_test_marker, Qhash_table_size_marker;
+Lisp_Object Qhash_table_weakness_marker;
+Lisp_Object Qhash_table_rehash_size_marker;
+Lisp_Object Qhash_table_rehash_threshold_marker;
+
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,6 +2348,89 @@
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;
+ /* will this be freed automatically? */
+ Lisp_Object* params = (Lisp_Object*) xmalloc (10 * sizeof(Lisp_Object));
+ int param_count = 0;
+ if (!EQ (head, Qhash_table_read_marker))
+ error ("Invalid extended read marker at head of #s list"
+ "(only hash-table allowed)");
+
+ while (!NILP(tmp))
+ {
+ tmp = CDR_SAFE(tmp);
+ head = CAR_SAFE(tmp);
+ /* allowed parameters: size test weakness
+ rehash-size rehash-threshold */
+ if (EQ(head, Qhash_table_data_marker))
+ {
+ tmp = CDR_SAFE(tmp);
+ data = CAR_SAFE(tmp);
+ /* debug_print(data); */
+ }
+
+ if (
+ param_count < 9 &&
+ EQ(head, Qhash_table_size_marker) ||
+ EQ(head, Qhash_table_test_marker) ||
+ EQ(head, Qhash_table_weakness_marker) ||
+ EQ(head, Qhash_table_rehash_size_marker) ||
+ EQ(head, Qhash_table_rehash_threshold_marker))
+ {
+ tmp = CDR_SAFE(tmp);
+ val = CAR_SAFE(tmp);
+ /*
+ debug_print(head);
+ debug_print(val);
+ */
+ /* how do I turn head into a symbol with the same contents but beginning with ':'? */
+ params[param_count] = head;
+ params[param_count+1] = val;
+ param_count+=2;
+ }
+ }
+
+ if (NILP(data))
+ error ("No data marker was found in the hash table");
+
+ /* doesn't work because of 'size' vs. ':size' issue, see above
+ Lisp_Object ht = Fmake_hash_table(param_count, params);
+ */
+ Lisp_Object ht = Fmake_hash_table(0, NULL);
+
+ Lisp_Object key = Qnil;
+
+ while (!NILP(data))
+ {
+ key = CAR_SAFE(data);
+ data = CDR_SAFE(data);
+ val = CAR_SAFE(data);
+ data = CDR_SAFE(data);
+ if (NILP(val))
+ error ("Odd number of elements in hashtable data");
+ /*
+ debug_print(key);
+ debug_print(val);
+ */
+ Fputhash(key, val, ht);
+ }
+
+ return ht;
+ }
+ }
if (c == '^')
{
c = READCHAR;
@@ -4432,6 +4522,21 @@
Vloads_in_progress = Qnil;
staticpro (&Vloads_in_progress);
+
+ Qhash_table_read_marker = intern ("hash-table");
+ staticpro (&Qhash_table_read_marker);
+ Qhash_table_data_marker = intern ("data");
+ staticpro (&Qhash_table_data_marker);
+ Qhash_table_test_marker = intern ("test");
+ staticpro (&Qhash_table_size_marker);
+ Qhash_table_test_marker = intern ("size");
+ staticpro (&Qhash_table_size_marker);
+ Qhash_table_weakness_marker = intern ("weakness");
+ staticpro (&Qhash_table_weakness_marker);
+ Qhash_table_rehash_size_marker = intern ("rehash-size");
+ staticpro (&Qhash_table_rehash_size_marker);
+ Qhash_table_rehash_threshold_marker = intern ("rehash-threshold");
+ staticpro (&Qhash_table_rehash_threshold_marker);
}
/* arch-tag: a0d02733-0f96-4844-a659-9fd53c4f414d
Index: print.c
===================================================================
RCS file: /sources/emacs/emacs/src/print.c,v
retrieving revision 1.253
diff -u -r1.253 print.c
--- print.c 31 Jul 2008 05:33:53 -0000 1.253
+++ print.c 1 Dec 2008 21:48:58 -0000
@@ -1341,6 +1341,7 @@
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,6 +1537,7 @@
/* 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,6 +2038,7 @@
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,6 +2055,61 @@
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))
{
next prev parent reply other threads:[~2008-12-01 22:01 UTC|newest]
Thread overview: 96+ messages / expand[flat|nested] mbox.gz Atom feed top
2008-08-28 1:01 print hash table to disk and reread in hash table Xah
2008-08-28 5:21 ` Thierry Volpiatto
[not found] ` <mailman.17732.1219901096.18990.help-gnu-emacs@gnu.org>
2008-08-28 5:27 ` Xah
2008-08-28 6:17 ` Thierry Volpiatto
2008-08-28 14:03 ` Ted Zlatanov
2008-08-28 18:10 ` Eli Zaretskii
[not found] ` <mailman.17796.1219947087.18990.help-gnu-emacs@gnu.org>
2008-08-29 14:06 ` Ted Zlatanov
2008-08-29 14:52 ` Eli Zaretskii
[not found] ` <mailman.17897.1220021571.18990.help-gnu-emacs@gnu.org>
2008-08-29 18:10 ` 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
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 [this message]
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
2008-08-28 8:36 ` print hash table to disk and reread in hash table Niels Giesen
2008-10-10 20:38 ` harven
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
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=868wqzd06x.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 external index
https://git.savannah.gnu.org/cgit/emacs.git
https://git.savannah.gnu.org/cgit/emacs/org-mode.git
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.