emacs-devel
[Top][All Lists]
Advanced

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

Re: print hash table to disk and reread in hash table


From: Ted Zlatanov
Subject: Re: print hash table to disk and reread in hash table
Date: Mon, 17 Nov 2008 11:15:46 -0600
User-agent: Gnus/5.110011 (No Gnus v0.11) Emacs/23.0.60 (gnu/linux)

On Sat, 30 Aug 2008 04:17:54 -0500 Ted Zlatanov <address@hidden> wrote: 

TZ> I was trying to serialize a hashtable to a file, and there's no good way
TZ> other than converting it to a list or making a file of (puthash)
TZ> statements.  bindat.el seems OK but doesn't seem to have a hashtable
TZ> spec in the format.

Steve Yegge published Ejacs over the weekend, which included some (IMHO
very reasonable) complaints about Emacs Lisp's lack of hashtable
serialization.

I propose the following functions (originals were contributed to Gnus by
Andreas Fuchs <address@hidden>):

(defun hash-table-to-alist (hash)
  "Build an alist from the values in HASH."
  (let ((list nil))
    (maphash
     (lambda (key value)
       (setq list (cons (cons key value) list)))
     hash)
    list))

;; this would take the usual make-hash-table arguments (:test, :size,
;; :rehash-size, :rehash-threshold, :weakness) so those don't have to
;; get serialized with the alist.  This makes the implementation much
;; simpler, complicating life slightly for the API consumers

;; untested

(defun hash-table-from-alist (alist &rest options)
  "Build a hashtable from the values in ALIST."
  (let ((ht (apply 'make-hash-table options)))
    (mapc
     (lambda (kv-pair)
       (puthash (car kv-pair) (cdr kv-pair) ht))
     alist)
     ht))

Implemented in C these would probably be a bit faster.  They are pretty
trivial, but in the Emacs core they would provide a universal way to
serialize hashtables.  IMO this is better than trying to serialize the
hashtable directly, since there are so many ways to read and write lists
in Emacs Lisp, while hashtable support for the same will require a lot
of effort, new APIs, and little real gain.

Ted





reply via email to

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