From 249848421749c5ea47fdfdaadc4bbb1914204151 Mon Sep 17 00:00:00 2001
From: Marko Lindqvist <cazfi74@gmail.com>
Date: Mon, 8 Jul 2024 22:44:09 +0300
Subject: [PATCH 24/24] genhash.c: Replace NULLs with nullptrs

See RM #756

Signed-off-by: Marko Lindqvist <cazfi74@gmail.com>
---
 utility/genhash.c | 119 ++++++++++++++++++++++++----------------------
 1 file changed, 63 insertions(+), 56 deletions(-)

diff --git a/utility/genhash.c b/utility/genhash.c
index f9a63ef064..d64d47edd3 100644
--- a/utility/genhash.c
+++ b/utility/genhash.c
@@ -131,7 +131,7 @@ bool genhash_str_comp_func(const char *vkey1, const char *vkey2)
 ****************************************************************************/
 char *genhash_str_copy_func(const char *vkey)
 {
-  return fc_strdup(NULL != vkey ? vkey : "");
+  return fc_strdup(vkey != nullptr ? vkey : "");
 }
 
 /************************************************************************//**
@@ -140,7 +140,7 @@ char *genhash_str_copy_func(const char *vkey)
 void genhash_str_free_func(char *vkey)
 {
 #ifdef FREECIV_DEBUG
-  fc_assert_ret(NULL != vkey);
+  fc_assert_ret(vkey != nullptr);
 #endif
   free(vkey);
 }
@@ -255,7 +255,7 @@ struct genhash *genhash_new_nentries(genhash_val_fn_t key_val_func,
                                      size_t nentries)
 {
   return genhash_new_nbuckets(key_val_func, key_comp_func,
-                              NULL, NULL, NULL, NULL,
+                              nullptr, nullptr, nullptr, nullptr,
                               genhash_calc_num_buckets(nentries));
 }
 
@@ -284,7 +284,7 @@ struct genhash *genhash_new(genhash_val_fn_t key_val_func,
                             genhash_comp_fn_t key_comp_func)
 {
   return genhash_new_nbuckets(key_val_func, key_comp_func,
-                              NULL, NULL, NULL, NULL, MIN_BUCKETS);
+                              nullptr, nullptr, nullptr, nullptr, MIN_BUCKETS);
 }
 
 /************************************************************************//**
@@ -292,7 +292,8 @@ struct genhash *genhash_new(genhash_val_fn_t key_val_func,
 ****************************************************************************/
 void genhash_destroy(struct genhash *pgenhash)
 {
-  fc_assert_ret(NULL != pgenhash);
+  fc_assert_ret(pgenhash != nullptr);
+
   pgenhash->no_shrink = TRUE;
   genhash_clear(pgenhash);
   free(pgenhash->buckets);
@@ -315,7 +316,7 @@ static void genhash_resize_table(struct genhash *pgenhash,
   bucket = pgenhash->buckets;
   end = bucket + pgenhash->num_buckets;
   for (; bucket < end; bucket++) {
-    for (iter = *bucket; NULL != iter; iter = next) {
+    for (iter = *bucket; iter != nullptr; iter = next) {
       slot = new_buckets + (iter->hash_val % new_nbuckets);
       next = iter->next;
       iter->next = *slot;
@@ -381,7 +382,7 @@ static bool genhash_maybe_resize(struct genhash *pgenhash, bool expandingp)
 static inline genhash_val_t genhash_val_calc(const struct genhash *pgenhash,
                                              const void *key)
 {
-  if (NULL != pgenhash->key_val_func) {
+  if (pgenhash->key_val_func != nullptr) {
     return pgenhash->key_val_func(key);
   } else {
     return ((intptr_t) key);
@@ -401,15 +402,15 @@ genhash_slot_lookup(const struct genhash *pgenhash,
   genhash_comp_fn_t key_comp_func = pgenhash->key_comp_func;
 
   slot = pgenhash->buckets + (hash_val % pgenhash->num_buckets);
-  if (NULL != key_comp_func) {
-    for (; NULL != *slot; slot = &(*slot)->next) {
+  if (key_comp_func != nullptr) {
+    for (; *slot != nullptr; slot = &(*slot)->next) {
       if (hash_val == (*slot)->hash_val
           && key_comp_func((*slot)->key, key)) {
         return slot;
       }
     }
   } else {
-    for (; NULL != *slot; slot = &(*slot)->next) {
+    for (; *slot != nullptr; slot = &(*slot)->next) {
       if (key == (*slot)->key) {
         return slot;
       }
@@ -423,11 +424,11 @@ genhash_slot_lookup(const struct genhash *pgenhash,
 ****************************************************************************/
 static inline void genhash_default_get(void **pkey, void **data)
 {
-  if (NULL != pkey) {
-    *pkey = NULL;
+  if (pkey != nullptr) {
+    *pkey = nullptr;
   }
-  if (NULL != data) {
-    *data = NULL;
+  if (data != nullptr) {
+    *data = nullptr;
   }
 }
 
@@ -439,10 +440,10 @@ static inline void genhash_slot_get(struct genhash_entry *const *slot,
 {
   const struct genhash_entry *entry = *slot;
 
-  if (NULL != pkey) {
+  if (pkey != nullptr) {
     *pkey = entry->key;
   }
-  if (NULL != data) {
+  if (data != nullptr) {
     *data = entry->data;
   }
 }
@@ -457,9 +458,9 @@ static inline void genhash_slot_create(struct genhash *pgenhash,
 {
   struct genhash_entry *entry = fc_malloc(sizeof(*entry));
 
-  entry->key = (NULL != pgenhash->key_copy_func
+  entry->key = (pgenhash->key_copy_func != nullptr
                 ? pgenhash->key_copy_func(key) : (void *) key);
-  entry->data = (NULL != pgenhash->data_copy_func
+  entry->data = (pgenhash->data_copy_func != nullptr
                  ? pgenhash->data_copy_func(data) : (void *) data);
   entry->hash_val = hash_val;
   entry->next = *slot;
@@ -474,12 +475,13 @@ static inline void genhash_slot_free(struct genhash *pgenhash,
 {
   struct genhash_entry *entry = *slot;
 
-  if (NULL != pgenhash->key_free_func) {
+  if (pgenhash->key_free_func != nullptr) {
     pgenhash->key_free_func(entry->key);
   }
-  if (NULL != pgenhash->data_free_func) {
+  if (pgenhash->data_free_func != nullptr) {
     pgenhash->data_free_func(entry->data);
   }
+
   *slot = entry->next;
   free(entry);
 }
@@ -493,15 +495,16 @@ static inline void genhash_slot_set(struct genhash *pgenhash,
 {
   struct genhash_entry *entry = *slot;
 
-  if (NULL != pgenhash->key_free_func) {
+  if (pgenhash->key_free_func != nullptr) {
     pgenhash->key_free_func(entry->key);
   }
-  if (NULL != pgenhash->data_free_func) {
+  if (pgenhash->data_free_func != nullptr) {
     pgenhash->data_free_func(entry->data);
   }
-  entry->key = (NULL != pgenhash->key_copy_func
+
+  entry->key = (pgenhash->key_copy_func != nullptr
                 ? pgenhash->key_copy_func(key) : (void *) key);
-  entry->data = (NULL != pgenhash->data_copy_func
+  entry->data = (pgenhash->data_copy_func != nullptr
                  ? pgenhash->data_copy_func(data) : (void *) data);
 }
 
@@ -513,9 +516,11 @@ bool genhash_set_no_shrink(struct genhash *pgenhash, bool no_shrink)
 {
   bool old;
 
-  fc_assert_ret_val(NULL != pgenhash, FALSE);
+  fc_assert_ret_val(pgenhash != nullptr, FALSE);
+
   old = pgenhash->no_shrink;
   pgenhash->no_shrink = no_shrink;
+
   return old;
 }
 
@@ -524,7 +529,8 @@ bool genhash_set_no_shrink(struct genhash *pgenhash, bool no_shrink)
 ****************************************************************************/
 size_t genhash_size(const struct genhash *pgenhash)
 {
-  fc_assert_ret_val(NULL != pgenhash, 0);
+  fc_assert_ret_val(pgenhash != nullptr, 0);
+
   return pgenhash->num_entries;
 }
 
@@ -533,7 +539,8 @@ size_t genhash_size(const struct genhash *pgenhash)
 ****************************************************************************/
 size_t genhash_capacity(const struct genhash *pgenhash)
 {
-  fc_assert_ret_val(NULL != pgenhash, 0);
+  fc_assert_ret_val(pgenhash != nullptr, 0);
+
   return pgenhash->num_buckets;
 }
 
@@ -547,7 +554,7 @@ struct genhash *genhash_copy(const struct genhash *pgenhash)
   const struct genhash_entry *src_iter;
   struct genhash_entry **dest_slot, **dest_bucket;
 
-  fc_assert_ret_val(NULL != pgenhash, NULL);
+  fc_assert_ret_val(pgenhash != nullptr, nullptr);
 
   new_genhash = fc_malloc(sizeof(*new_genhash));
 
@@ -565,7 +572,7 @@ struct genhash *genhash_copy(const struct genhash *pgenhash)
 
   for (; src_bucket < end; src_bucket++, dest_bucket++) {
     dest_slot = dest_bucket;
-    for (src_iter = *src_bucket; NULL != src_iter;
+    for (src_iter = *src_bucket; src_iter != nullptr;
          src_iter = src_iter->next) {
       genhash_slot_create(new_genhash, dest_slot, src_iter->key,
                           src_iter->data, src_iter->hash_val);
@@ -583,12 +590,12 @@ void genhash_clear(struct genhash *pgenhash)
 {
   struct genhash_entry **bucket, **end;
 
-  fc_assert_ret(NULL != pgenhash);
+  fc_assert_ret(pgenhash != nullptr);
 
   bucket = pgenhash->buckets;
   end = bucket + pgenhash->num_buckets;
   for (; bucket < end; bucket++) {
-    while (NULL != *bucket) {
+    while (*bucket != nullptr) {
       genhash_slot_free(pgenhash, bucket);
     }
   }
@@ -607,11 +614,11 @@ bool genhash_insert(struct genhash *pgenhash, const void *key,
   struct genhash_entry **slot;
   genhash_val_t hash_val;
 
-  fc_assert_ret_val(NULL != pgenhash, FALSE);
+  fc_assert_ret_val(pgenhash != nullptr, FALSE);
 
   hash_val = genhash_val_calc(pgenhash, key);
   slot = genhash_slot_lookup(pgenhash, key, hash_val);
-  if (NULL != *slot) {
+  if (*slot != nullptr) {
     return FALSE;
   } else {
     if (genhash_maybe_expand(pgenhash)) {
@@ -632,7 +639,7 @@ bool genhash_insert(struct genhash *pgenhash, const void *key,
 bool genhash_replace(struct genhash *pgenhash, const void *key,
                      const void *data)
 {
-  return genhash_replace_full(pgenhash, key, data, NULL, NULL);
+  return genhash_replace_full(pgenhash, key, data, nullptr, nullptr);
 }
 
 /************************************************************************//**
@@ -641,7 +648,7 @@ bool genhash_replace(struct genhash *pgenhash, const void *key,
   insertion.
 
   Returns in 'old_pkey' and 'old_pdata' the old content of the bucket if
-  they are not NULL. NB: It can returns freed pointers if free functions
+  they are not nullptr. NB: It can returns freed pointers if free functions
   were supplied to the genhash table.
 ****************************************************************************/
 bool genhash_replace_full(struct genhash *pgenhash, const void *key,
@@ -651,12 +658,12 @@ bool genhash_replace_full(struct genhash *pgenhash, const void *key,
   struct genhash_entry **slot;
   genhash_val_t hash_val;
 
-  fc_assert_action(NULL != pgenhash,
+  fc_assert_action(pgenhash != nullptr,
                    genhash_default_get(old_pkey, old_pdata); return FALSE);
 
   hash_val = genhash_val_calc(pgenhash, key);
   slot = genhash_slot_lookup(pgenhash, key, hash_val);
-  if (NULL != *slot) {
+  if (*slot != nullptr) {
     /* Replace. */
     genhash_slot_get(slot, old_pkey, old_pdata);
     genhash_slot_set(pgenhash, slot, key, data);
@@ -675,7 +682,7 @@ bool genhash_replace_full(struct genhash *pgenhash, const void *key,
 }
 
 /************************************************************************//**
-  Lookup data. Return TRUE on success, then pdata - if not NULL will be set
+  Lookup data. Return TRUE on success, then pdata - if not nullptr will be set
   to the data value.
 ****************************************************************************/
 bool genhash_lookup(const struct genhash *pgenhash, const void *key,
@@ -683,15 +690,15 @@ bool genhash_lookup(const struct genhash *pgenhash, const void *key,
 {
   struct genhash_entry **slot;
 
-  fc_assert_action(NULL != pgenhash,
-                   genhash_default_get(NULL, pdata); return FALSE);
+  fc_assert_action(pgenhash != nullptr,
+                   genhash_default_get(nullptr, pdata); return FALSE);
 
   slot = genhash_slot_lookup(pgenhash, key, genhash_val_calc(pgenhash, key));
-  if (NULL != *slot) {
-    genhash_slot_get(slot, NULL, pdata);
+  if (*slot != nullptr) {
+    genhash_slot_get(slot, nullptr, pdata);
     return TRUE;
   } else {
-    genhash_default_get(NULL, pdata);
+    genhash_default_get(nullptr, pdata);
     return FALSE;
   }
 }
@@ -701,14 +708,14 @@ bool genhash_lookup(const struct genhash *pgenhash, const void *key,
 ****************************************************************************/
 bool genhash_remove(struct genhash *pgenhash, const void *key)
 {
-  return genhash_remove_full(pgenhash, key, NULL, NULL);
+  return genhash_remove_full(pgenhash, key, nullptr, nullptr);
 }
 
 /************************************************************************//**
   Delete an entry from the genhash table. Returns TRUE on success.
 
   Returns in 'deleted_pkey' and 'deleted_pdata' the old contents of the
-  deleted entry if not NULL. NB: It can returns freed pointers if free
+  deleted entry if not nullptr. NB: It can returns freed pointers if free
   functions were supplied to the genhash table.
 ****************************************************************************/
 bool genhash_remove_full(struct genhash *pgenhash, const void *key,
@@ -716,12 +723,12 @@ bool genhash_remove_full(struct genhash *pgenhash, const void *key,
 {
   struct genhash_entry **slot;
 
-  fc_assert_action(NULL != pgenhash,
+  fc_assert_action(pgenhash != nullptr,
                    genhash_default_get(deleted_pkey, deleted_pdata);
                    return FALSE);
 
   slot = genhash_slot_lookup(pgenhash, key, genhash_val_calc(pgenhash, key));
-  if (NULL != *slot) {
+  if (*slot != nullptr) {
     genhash_slot_get(slot, deleted_pkey, deleted_pdata);
     genhash_slot_free(pgenhash, slot);
     genhash_maybe_shrink(pgenhash);
@@ -741,7 +748,7 @@ bool genhash_remove_full(struct genhash *pgenhash, const void *key,
 bool genhashes_are_equal(const struct genhash *pgenhash1,
                          const struct genhash *pgenhash2)
 {
-  return genhashes_are_equal_full(pgenhash1, pgenhash2, NULL);
+  return genhashes_are_equal_full(pgenhash1, pgenhash2, nullptr);
 }
 
 /************************************************************************//**
@@ -757,7 +764,7 @@ bool genhashes_are_equal_full(const struct genhash *pgenhash1,
   /* Check pointers. */
   if (pgenhash1 == pgenhash2) {
     return TRUE;
-  } else if (NULL == pgenhash1 || NULL == pgenhash2) {
+  } else if (pgenhash1 == nullptr || pgenhash2 == nullptr) {
     return FALSE;
   }
 
@@ -774,11 +781,11 @@ bool genhashes_are_equal_full(const struct genhash *pgenhash1,
   bucket1 = pgenhash1->buckets;
   max1 = bucket1 + pgenhash1->num_buckets;
   for (; bucket1 < max1; bucket1++) {
-    for (iter1 = *bucket1; NULL != iter1; iter1 = iter1->next) {
+    for (iter1 = *bucket1; iter1 != nullptr; iter1 = iter1->next) {
       slot2 = genhash_slot_lookup(pgenhash2, iter1->key, iter1->hash_val);
-      if (NULL == *slot2
+      if (*slot2 == nullptr
           || (iter1->data != (*slot2)->data
-              && (NULL == data_comp_func
+              && (data_comp_func == nullptr
                   || !data_comp_func(iter1->data, (*slot2)->data)))) {
         return FALSE;
       }
@@ -823,12 +830,12 @@ static void genhash_iter_next(struct iterator *genhash_iter)
   struct genhash_iter *iter = GENHASH_ITER(genhash_iter);
 
   iter->iterator = iter->iterator->next;
-  if (NULL != iter->iterator) {
+  if (iter->iterator != nullptr) {
     return;
   }
 
   for (iter->bucket++; iter->bucket < iter->end; iter->bucket++) {
-    if (NULL != *iter->bucket) {
+    if (*iter->bucket != nullptr) {
       iter->iterator = *iter->bucket;
       return;
     }
@@ -862,7 +869,7 @@ genhash_iter_init_common(struct genhash_iter *iter,
                          const struct genhash *pgenhash,
                          void * (*get) (const struct iterator *))
 {
-  if (NULL == pgenhash) {
+  if (pgenhash == nullptr) {
     return invalid_iter_init(ITERATOR(iter));
   }
 
@@ -874,7 +881,7 @@ genhash_iter_init_common(struct genhash_iter *iter,
 
   /* Seek to the first used bucket. */
   for (; iter->bucket < iter->end; iter->bucket++) {
-    if (NULL != *iter->bucket) {
+    if (*iter->bucket != nullptr) {
       iter->iterator = *iter->bucket;
       break;
     }
-- 
2.43.0

