bug-gnulib
[Top][All Lists]
Advanced

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

[PATCH] *-map, *-omap: Allow passing NULL to search


From: Colin Watson
Subject: [PATCH] *-map, *-omap: Allow passing NULL to search
Date: Mon, 11 Feb 2019 00:29:02 +0000
User-agent: NeoMutt/20170113 (1.7.2)

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




reply via email to

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