From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on dcvr.yhbt.net X-Spam-Level: X-Spam-ASN: AS22989 209.51.188.0/24 X-Spam-Status: No, score=-4.0 required=3.0 tests=AWL,BAYES_00, HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI,SPF_PASS shortcircuit=no autolearn=ham autolearn_force=no version=3.4.2 Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (No client certificate requested) by dcvr.yhbt.net (Postfix) with ESMTPS id 10BB91F453 for ; Mon, 11 Feb 2019 00:29:22 +0000 (UTC) Received: from localhost ([127.0.0.1]:41711 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1gszTL-0007aw-Nf for normalperson@yhbt.net; Sun, 10 Feb 2019 19:29:19 -0500 Received: from eggs.gnu.org ([209.51.188.92]:60023) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1gszTH-0007aq-Ax for bug-gnulib@gnu.org; Sun, 10 Feb 2019 19:29:16 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1gszTF-0003bQ-UI for bug-gnulib@gnu.org; Sun, 10 Feb 2019 19:29:15 -0500 Received: from b-painless.mh.aa.net.uk ([81.187.30.52]:39703) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1gszTF-0003aj-NI for bug-gnulib@gnu.org; Sun, 10 Feb 2019 19:29:13 -0500 Received: from 3.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.4.1.b.e.2.f.f.b.0.b.8.0.1.0.0.2.ip6.arpa ([2001:8b0:bff2:eb14::3] helo=riva.pelham.vpn.ucam.org) by b-painless.mh.aa.net.uk with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.89) (envelope-from ) id 1gszTC-0006d0-3m for bug-gnulib@gnu.org; Mon, 11 Feb 2019 00:29:12 +0000 Received: from ns1.pelham.vpn.ucam.org ([172.20.153.2] helo=riva.ucam.org) by riva.pelham.vpn.ucam.org with esmtp (Exim 4.89) (envelope-from ) id 1gszT4-0000i3-Bs for bug-gnulib@gnu.org; Mon, 11 Feb 2019 00:29:02 +0000 Date: Mon, 11 Feb 2019 00:29:02 +0000 From: Colin Watson To: bug-gnulib@gnu.org Subject: [PATCH] *-map, *-omap: Allow passing NULL to search Message-ID: <20190211002902.zeap3f6hrr6vb5fe@riva.ucam.org> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline User-Agent: NeoMutt/20170113 (1.7.2) X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 81.187.30.52 X-BeenThere: bug-gnulib@gnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: Gnulib discussion list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: bug-gnulib-bounces+normalperson=yhbt.net@gnu.org Sender: "bug-gnulib" It's sometimes convenient to have a map whose values may be NULL, but in that case it's a little awkward to determine whether a key exists in the map: gl_map_get returns NULL for both "key not found" and "value is NULL", so one needs a local variable just for the purpose of passing its address to gl_map_search. Instead, allow the return pointers to be NULL, so that one can use gl_map_search (map, NULL, NULL) to check existence. * lib/gl_anytree_omap.h (gl_tree_search): Only set *valuep if valuep is non-NULL. * lib/gl_array_map.c (gl_array_search): Likewise. * lib/gl_array_omap.c (gl_array_search): Likewise. * lib/gl_hash_map.c (gl_hash_search): Likewise. * lib/gl_linkedhash_map.c (gl_linkedhash_search): Likewise. * lib/gl_map.h (gl_map_search): Describe new behaviour. * lib/gl_omap.h (gl_omap_search): Likewise. * lib/gl_anytree_omap.h (gl_tree_search_atleast): Only set *keyp or *valuep if keyp or valuep respectively is non-NULL. * lib/gl_array_omap.c (gl_array_search_atleast): Likewise. * lib/gl_omap.h (gl_omap_search_atleast): Likewise. --- lib/gl_anytree_omap.h | 9 ++++++--- lib/gl_array_map.c | 3 ++- lib/gl_array_omap.c | 9 ++++++--- lib/gl_hash_map.c | 3 ++- lib/gl_linkedhash_map.c | 3 ++- lib/gl_map.h | 4 ++-- lib/gl_omap.h | 9 +++++---- 7 files changed, 25 insertions(+), 15 deletions(-) diff --git a/lib/gl_anytree_omap.h b/lib/gl_anytree_omap.h index d2bd88eb6..a8c6f129e 100644 --- a/lib/gl_anytree_omap.h +++ b/lib/gl_anytree_omap.h @@ -75,7 +75,8 @@ gl_tree_search (gl_omap_t map, const void *key, const void **valuep) else /* cmp == 0 */ { /* We have a key equal to KEY. */ - *valuep = node->value; + if (valuep) + *valuep = node->value; return true; } } @@ -110,8 +111,10 @@ gl_tree_search_atleast (gl_omap_t map, node = node->left; } } - *keyp = found->key; - *valuep = found->value; + if (keyp) + *keyp = found->key; + if (valuep) + *valuep = found->value; return true; } } diff --git a/lib/gl_array_map.c b/lib/gl_array_map.c index 33dc719da..2c5770054 100644 --- a/lib/gl_array_map.c +++ b/lib/gl_array_map.c @@ -112,7 +112,8 @@ gl_array_search (gl_map_t map, const void *key, const void **valuep) size_t index = gl_array_indexof (map, key); if (index != (size_t)(-1)) { - *valuep = map->pairs[index].value; + if (valuep) + *valuep = map->pairs[index].value; return true; } else diff --git a/lib/gl_array_omap.c b/lib/gl_array_omap.c index 3d3aff613..11b660e8d 100644 --- a/lib/gl_array_omap.c +++ b/lib/gl_array_omap.c @@ -115,7 +115,8 @@ gl_array_search (gl_omap_t map, const void *key, const void **valuep) size_t index = gl_array_indexof (map, key); if (index != (size_t)(-1)) { - *valuep = map->pairs[index].value; + if (valuep) + *valuep = map->pairs[index].value; return true; } else @@ -163,8 +164,10 @@ gl_array_search_atleast (gl_omap_t map, else high = mid2; } - *keyp = map->pairs[low].key; - *valuep = map->pairs[low].value; + if (keyp) + *keyp = map->pairs[low].key; + if (valuep) + *valuep = map->pairs[low].value; return true; } } diff --git a/lib/gl_hash_map.c b/lib/gl_hash_map.c index 534b472fa..2f0b5bb8b 100644 --- a/lib/gl_hash_map.c +++ b/lib/gl_hash_map.c @@ -119,7 +119,8 @@ gl_hash_search (gl_map_t map, const void *key, const void **valuep) ? equals (key, node->key) : key == node->key)) { - *valuep = node->value; + if (valuep) + *valuep = node->value; return true; } return false; diff --git a/lib/gl_linkedhash_map.c b/lib/gl_linkedhash_map.c index 9e16971a0..000e33f6d 100644 --- a/lib/gl_linkedhash_map.c +++ b/lib/gl_linkedhash_map.c @@ -144,7 +144,8 @@ gl_linkedhash_search (gl_map_t map, const void *key, const void **valuep) ? equals (key, node->key) : key == node->key)) { - *valuep = node->value; + if (valuep) + *valuep = node->value; return true; } return false; diff --git a/lib/gl_map.h b/lib/gl_map.h index 02a3ac376..790e3fa2b 100644 --- a/lib/gl_map.h +++ b/lib/gl_map.h @@ -133,8 +133,8 @@ extern size_t gl_map_size (gl_map_t map); extern const void * gl_map_get (gl_map_t map, const void *key); /* Search whether a pair with the given key is already in the map. - Return true and set *VALUEP to the value if found. - Return false if not present in the map. */ + If found, return true, and set *VALUEP to the value if VALUEP is non-NULL. + Otherwise, return false. */ extern bool gl_map_search (gl_map_t map, const void *key, const void **valuep); /* Add a pair to a map. diff --git a/lib/gl_omap.h b/lib/gl_omap.h index d11474972..53571ec52 100644 --- a/lib/gl_omap.h +++ b/lib/gl_omap.h @@ -132,16 +132,17 @@ extern size_t gl_omap_size (gl_omap_t map); extern const void * gl_omap_get (gl_omap_t map, const void *key); /* Search whether a pair with the given key is already in the ordered map. - Return true and set *VALUEP to the value if found. - Return false if not present in the map. */ + If found, return true, and set *VALUEP to the value if VALUEP is non-NULL. + Otherwise, return false. */ extern bool gl_omap_search (gl_omap_t map, const void *key, const void **valuep); /* Search the pair with the least key in the ordered map that compares greater or equal to the given THRESHOLD. The representation of the THRESHOLD is defined by the THRESHOLD_FN. - Return true and store the found pair in *KEYP and *VALUEP if found. - Otherwise return false. */ + If found, return true, set *KEYP to the found key if KEYP is non-NULL, + and set *VALUEP to the found value if VALUEP is non-NULL. + Otherwise, return false. */ extern bool gl_omap_search_atleast (gl_omap_t map, gl_mapkey_threshold_fn threshold_fn, const void *threshold, -- 2.17.1