From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: Tassilo Horn Newsgroups: gmane.emacs.help Subject: Re: what's "weakness" in elisp's hash table? Date: Thu, 03 Jan 2008 22:15:58 +0100 Message-ID: <87y7b6mwz5.fsf@member.fsf.org> References: NNTP-Posting-Host: lo.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Trace: ger.gmane.org 1199395035 15718 80.91.229.12 (3 Jan 2008 21:17:15 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Thu, 3 Jan 2008 21:17:15 +0000 (UTC) To: help-gnu-emacs@gnu.org Original-X-From: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Thu Jan 03 22:17:33 2008 Return-path: Envelope-to: geh-help-gnu-emacs@m.gmane.org Original-Received: from lists.gnu.org ([199.232.76.165]) by lo.gmane.org with esmtp (Exim 4.50) id 1JAXRP-0007I5-TM for geh-help-gnu-emacs@m.gmane.org; Thu, 03 Jan 2008 22:17:28 +0100 Original-Received: from localhost ([127.0.0.1] helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1JAXR3-0001K6-Hw for geh-help-gnu-emacs@m.gmane.org; Thu, 03 Jan 2008 16:17:05 -0500 Original-Received: from mailman by lists.gnu.org with tmda-scanned (Exim 4.43) id 1JAXQH-00014R-12 for help-gnu-emacs@gnu.org; Thu, 03 Jan 2008 16:16:17 -0500 Original-Received: from exim by lists.gnu.org with spam-scanned (Exim 4.43) id 1JAXQG-00013i-CP for help-gnu-emacs@gnu.org; Thu, 03 Jan 2008 16:16:16 -0500 Original-Received: from [199.232.76.173] (helo=monty-python.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1JAXQG-00013V-5F for help-gnu-emacs@gnu.org; Thu, 03 Jan 2008 16:16:16 -0500 Original-Received: from main.gmane.org ([80.91.229.2] 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 1JAXQF-0001mc-SP for help-gnu-emacs@gnu.org; Thu, 03 Jan 2008 16:16:16 -0500 Original-Received: from list by ciao.gmane.org with local (Exim 4.43) id 1JAXQ7-0004UU-G4 for help-gnu-emacs@gnu.org; Thu, 03 Jan 2008 21:16:07 +0000 Original-Received: from dslb-084-063-023-053.pools.arcor-ip.net ([84.63.23.53]) by main.gmane.org with esmtp (Gmexim 0.1 (Debian)) id 1AlnuQ-0007hv-00 for ; Thu, 03 Jan 2008 21:16:07 +0000 Original-Received: from tassilo by dslb-084-063-023-053.pools.arcor-ip.net with local (Gmexim 0.1 (Debian)) id 1AlnuQ-0007hv-00 for ; Thu, 03 Jan 2008 21:16:07 +0000 X-Injected-Via-Gmane: http://gmane.org/ Original-Lines: 100 Original-X-Complaints-To: usenet@ger.gmane.org X-Gmane-NNTP-Posting-Host: dslb-084-063-023-053.pools.arcor-ip.net User-Agent: Gnus/5.110007 (No Gnus v0.7) Emacs/23.0.50 (gnu/linux) Cancel-Lock: sha1:zZF1Tmn3y9SzY9U6lj86Oqqplgg= X-detected-kernel: by monty-python.gnu.org: Linux 2.6, seldom 2.4 (older, 4) X-BeenThere: help-gnu-emacs@gnu.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: Users list for the GNU Emacs text editor List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Original-Sender: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Errors-To: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Xref: news.gmane.org gmane.emacs.help:50463 Archived-At: Xah Lee writes: > in emacs lisp, when creating a hash table, there's the “:weakness” > thing. > > See > http://xahlee.org/elisp/Creating-Hash.html > > what does that mean? Suppose if i do > > (setq myhash (make-hash-table :test 'equal :weakness 'key)) > (puthash "mary" "19" myhash) > ... > > Then, the "mary" would disappear if i don't access it?? No, if you don't hold a reference to it. Here an example: ,---- | *** Welcome to IELM *** Type (describe-mode) for help. | ELISP> (setq myhash (make-hash-table :test 'equal :weakness 'key)) | # | ELISP> (puthash "mary" "19" myhash) | "19" | ELISP> (gethash "mary" myhash) | "19" | ELISP> (garbage-collect) | ((896892 . 77250) | (104815 . 0) | (5412 . 7941) | 7026294 1304097 | (916 . 862) | (23843 . 4126) | (224711 . 21115)) | | ELISP> (gethash "mary" myhash) | nil | ELISP> ;; It's gone cause the key "mary" was not referenced. | ELISP> (setq mary "mary") | "mary" | ELISP> (puthash mary "19" myhash) | "19" | ELISP> (gethash mary myhash) | "19" | ELISP> (garbage-collect) | ((902678 . 85905) | (104976 . 1) | (5422 . 7889) | 7040124 1307039 | (916 . 738) | (24339 . 3594) | (225190 . 20636)) | | ELISP> (gethash mary myhash) | "19" | ELISP> ;; Still there cause the variable mary holds a reference to the | ELISP> ;; key "mary". `---- And here are the docs that clearly state that. ,----[ (info "(elisp)Creating Hash") ] | `:weakness WEAK' | The weakness of a hash table specifies whether the presence | of a key or value in the hash table preserves it from garbage | collection. | | The value, WEAK, must be one of `nil', `key', `value', | `key-or-value', `key-and-value', or `t' which is an alias for | `key-and-value'. If WEAK is `key' then the hash table does | not prevent its keys from being collected as garbage (if they | are not referenced anywhere else); if a particular key does | get collected, the corresponding association is removed from | the hash table. | | If WEAK is `value', then the hash table does not prevent | values from being collected as garbage (if they are not | referenced anywhere else); if a particular value does get | collected, the corresponding association is removed from the | hash table. | | If WEAK is `key-and-value' or `t', both the key and the value | must be live in order to preserve the association. Thus, the | hash table does not protect either keys or values from garbage | collection; if either one is collected as garbage, that | removes the association. | | If WEAK is `key-or-value', either the key or the value can | preserve the association. Thus, associations are removed | from the hash table when both their key and value would be | collected as garbage (if not for references from weak hash | tables). | | The default for WEAK is `nil', so that all keys and values | referenced in the hash table are preserved from garbage | collection. `---- Bye, Tassilo