[glib: 2/9] ghash: Simplify g_hash_table_set_shift()



commit 171f698ead70bd0450b530069b9f146094148050
Author: Hans Petter Jansson <hpj cl no>
Date:   Tue Jul 10 12:48:26 2018 +0200

    ghash: Simplify g_hash_table_set_shift()
    
    Even if we're using a prime modulo for the initial probe, our table is
    power-of-two-sized, meaning we can set the mask simply by subtracting one
    from the size.

 glib/ghash.c | 14 +++++---------
 1 file changed, 5 insertions(+), 9 deletions(-)
---
diff --git a/glib/ghash.c b/glib/ghash.c
index 888e67e00..11d71627e 100644
--- a/glib/ghash.c
+++ b/glib/ghash.c
@@ -297,19 +297,15 @@ static const gint prime_mod [] =
 static void
 g_hash_table_set_shift (GHashTable *hash_table, gint shift)
 {
-  gint i;
-  guint mask = 0;
-
   hash_table->size = 1 << shift;
   hash_table->mod  = prime_mod [shift];
 
-  for (i = 0; i < shift; i++)
-    {
-      mask <<= 1;
-      mask |= 1;
-    }
+  /* hash_table->size is always a power of two, so we can calculate the mask
+   * by simply subtracting 1 from it. The leading assertion ensures that
+   * we're really dealing with a power of two. */
 
-  hash_table->mask = mask;
+  g_assert ((hash_table->size & (hash_table->size - 1)) == 0);
+  hash_table->mask = hash_table->size - 1;
 }
 
 static gint


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