From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!.POSTED!not-for-mail From: Philipp Stephani Newsgroups: gmane.emacs.devel Subject: [PATCH] Add new function to test whether a key is present in a hash table. Date: Thu, 15 Feb 2018 21:34:06 +0100 Message-ID: <20180215203406.64372-1-phst@google.com> NNTP-Posting-Host: blaine.gmane.org X-Trace: blaine.gmane.org 1518726769 20355 195.159.176.226 (15 Feb 2018 20:32:49 GMT) X-Complaints-To: usenet@blaine.gmane.org NNTP-Posting-Date: Thu, 15 Feb 2018 20:32:49 +0000 (UTC) Cc: Philipp Stephani To: emacs-devel@gnu.org Original-X-From: emacs-devel-bounces+ged-emacs-devel=m.gmane.org@gnu.org Thu Feb 15 21:32:45 2018 Return-path: Envelope-to: ged-emacs-devel@m.gmane.org Original-Received: from lists.gnu.org ([208.118.235.17]) by blaine.gmane.org with esmtp (Exim 4.84_2) (envelope-from ) id 1emQCg-0003qM-5S for ged-emacs-devel@m.gmane.org; Thu, 15 Feb 2018 21:32:26 +0100 Original-Received: from localhost ([::1]:57444 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1emQEh-00026m-OE for ged-emacs-devel@m.gmane.org; Thu, 15 Feb 2018 15:34:31 -0500 Original-Received: from eggs.gnu.org ([2001:4830:134:3::10]:37434) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1emQEZ-00021j-Bc for emacs-devel@gnu.org; Thu, 15 Feb 2018 15:34:24 -0500 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1emQEV-0006aE-EF for emacs-devel@gnu.org; Thu, 15 Feb 2018 15:34:23 -0500 Original-Received: from mail-wr0-x233.google.com ([2a00:1450:400c:c0c::233]:34459) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1emQEV-0006Zn-7G for emacs-devel@gnu.org; Thu, 15 Feb 2018 15:34:19 -0500 Original-Received: by mail-wr0-x233.google.com with SMTP id m5so976679wrg.1 for ; Thu, 15 Feb 2018 12:34:19 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id; bh=e2Wp6WZwI7TO+9OVMLcKE87Qj0jMdhz4LhyqQ79JK2s=; b=AgQx0ql84z8SbbL0LPWSYsPCBQch1UbPwzTaz9KYtCB7cMi8LHRnQOfV0ul1ZSAWW1 9xymeis4l6W220VwENdZlfB01cX9oE43iLeyBWAlo9WuJYjmSfbUTbQeKgRA9zQSO5DE dKdrg2qNkuc8Czq4x4ls35RGsCtY5ftibXI0UBKVIbu3Vgc7WCcnFNPwUk9K46qlUcfB BLitMvTZ0IcorjL/93QWQStXpn8euiE4/wzdFcv+X1+Ehqo1hZbpNn5iRorVH72uX7An EcdLYVee+5PoMK8coLoboHieXSv0Qov6yKWxLeeLXH2b4HB4RXjD6fcC0EvPb9XSr8PU FluQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id; bh=e2Wp6WZwI7TO+9OVMLcKE87Qj0jMdhz4LhyqQ79JK2s=; b=FxffI0PJqCtg6h9hZyaoPS6FSj45QxCmTAIP93v2l/C5BYet+mmEAvA8sAHXoaNc6N +8fWIrKyInC0MjturjPE4JrLS4T2gWaQWsV6Nf5oqho20jQjYycGZhMP0v6cVFFWTiag 7UvvlbQWPsUTyqZYnAse64rDsTz1eEVFR3Umx4PFXPIB2O3DSjjpiLquoIsXOlcLsg31 LP/ENs/zKJYXpKLSi5tii3mPEp2FcSIbVWkidaR3R2kY/nBfa2cY42wZuhUZndj0pKjf H5jNi+rzD6oAyFw+wmPZwj1veW6SR0qtOdDu3CpNcju1tTNgg1rECq2Snv+QhSR2qfKl h0gA== X-Gm-Message-State: APf1xPCNKwOSsNExVGDGQpNBGabHvUB2clQcO5Xxo8qFj6iBmuphyUQ7 S8YTFWG20RA7exNGZFaFcuHyoPQ1 X-Google-Smtp-Source: AH8x226R1jK8Qj66upV/QPjXg5bNOaLG6FmyiiEqYCLDjD8DsbIqlwkCAKP5tDbpshaVy6R0drIw/g== X-Received: by 10.223.153.23 with SMTP id x23mr3549343wrb.134.1518726857644; Thu, 15 Feb 2018 12:34:17 -0800 (PST) Original-Received: from p.cm.cablesurf.de (46.128.198.151.dynamic.cablesurf.de. [46.128.198.151]) by smtp.gmail.com with ESMTPSA id b136sm11687997wme.34.2018.02.15.12.34.15 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Thu, 15 Feb 2018 12:34:16 -0800 (PST) X-Google-Original-From: Philipp Stephani X-Mailer: git-send-email 2.16.1 X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 2a00:1450:400c:c0c::233 X-BeenThere: emacs-devel@gnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: "Emacs development discussions." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: emacs-devel-bounces+ged-emacs-devel=m.gmane.org@gnu.org Original-Sender: "Emacs-devel" Xref: news.gmane.org gmane.emacs.devel:222771 Archived-At: Such a function is useful because in Emacs Lisp, 'gethash' cannot return whether the key is present as in Common Lisp, and using 'gethash' alone to test for presence is nontrivial. * src/fns.c (Fhash_table_contains_p): New function. * test/src/fns-tests.el (hash-table-contains-p): New unit test. * doc/lispref/hash.texi (Hash Access): Document new function. --- doc/lispref/hash.texi | 12 ++++++++++++ etc/NEWS | 3 +++ src/fns.c | 10 ++++++++++ test/src/fns-tests.el | 8 ++++++++ 4 files changed, 33 insertions(+) diff --git a/doc/lispref/hash.texi b/doc/lispref/hash.texi index ddd46a55ed..8848f0462e 100644 --- a/doc/lispref/hash.texi +++ b/doc/lispref/hash.texi @@ -195,6 +195,18 @@ Hash Access association in @var{table}. @end defun +While it might be tempting to use @code{gethash} to check whether a +key is present in a hash table, keep in mind that you often need to +distinguish between @var{key} being absent and @var{key} being mapped +to @var{default}. To easily distinguish between these two cases, +there's another function to explicitly check whether a key is present: + +@defun hash-table-contains-p key table +This function looks up @var{key} in @var{table}; it returns @code{t} +if @var{key} is present, and @code{nil} otherwise. The associated +value is ignored. +@end defun + @defun puthash key value table This function enters an association for @var{key} in @var{table}, with value @var{value}. If @var{key} already has an association in diff --git a/etc/NEWS b/etc/NEWS index 71569c95ad..5c9363c052 100644 --- a/etc/NEWS +++ b/etc/NEWS @@ -282,6 +282,9 @@ depending on the new customizable variable 'read-answer-short'. Specifically, it puts the module name into 'load-history', prints loading messages if requested, and protects against recursive loads. +** New function 'hash-table-contains-p' to check whether a hash table +contains a certain key. + * Changes in Emacs 27.1 on Non-Free Operating Systems diff --git a/src/fns.c b/src/fns.c index 47457e44c8..6075544c69 100644 --- a/src/fns.c +++ b/src/fns.c @@ -4650,6 +4650,15 @@ DEFUN ("clrhash", Fclrhash, Sclrhash, 1, 1, 0, } +DEFUN ("hash-table-contains-p", Fhash_table_contains_p, Shash_table_contains_p, + 2, 2, NULL, + doc: /* Return t if TABLE contains KEY, nil otherwise. */) + (Lisp_Object key, Lisp_Object table) +{ + return hash_lookup (check_hash_table (table), key, NULL) >= 0 ? Qt : Qnil; +} + + DEFUN ("gethash", Fgethash, Sgethash, 2, 3, 0, doc: /* Look up KEY in TABLE and return its associated value. If KEY is not found, return DFLT which defaults to nil. */) @@ -5145,6 +5154,7 @@ syms_of_fns (void) defsubr (&Shash_table_weakness); defsubr (&Shash_table_p); defsubr (&Sclrhash); + defsubr (&Shash_table_contains_p); defsubr (&Sgethash); defsubr (&Sputhash); defsubr (&Sremhash); diff --git a/test/src/fns-tests.el b/test/src/fns-tests.el index f8554636ba..f651ee8985 100644 --- a/test/src/fns-tests.el +++ b/test/src/fns-tests.el @@ -575,4 +575,12 @@ dot2 :type 'wrong-type-argument) '(wrong-type-argument plistp (:foo 1 . :bar))))) +(ert-deftest hash-table-contains-p () + (let ((h (make-hash-table))) + (puthash 1.5 'foo h) + (puthash 2.5 nil h) + (should (equal (hash-table-contains-p 1.5 h) t)) + (should (equal (hash-table-contains-p 2.5 h) t)) + (should (equal (hash-table-contains-p 2 h) nil)))) + (provide 'fns-tests) -- 2.16.1