Author: pepeto
Date: Mon Jun  2 09:24:46 2014
New Revision: 25010

URL: http://svn.gna.org/viewcvs/freeciv?rev=25010&view=rev
Log:
Do not assume the hash values of the caller will match the number of buckets.
Move the modulo inside genhash module.

See gna patch #4729

Modified:
    trunk/common/generate_packets.py
    trunk/common/government.c
    trunk/utility/genhash.c
    trunk/utility/genhash.h
    trunk/utility/spechash.h

Modified: trunk/common/generate_packets.py
URL: 
http://svn.gna.org/viewcvs/freeciv/trunk/common/generate_packets.py?rev=25010&r1=25009&r2=25010&view=diff
==============================================================================
--- trunk/common/generate_packets.py    (original)
+++ trunk/common/generate_packets.py    Mon Jun  2 09:24:46 2014
@@ -716,7 +716,7 @@
         if len(self.key_fields)==0:
             return "#define hash_%(name)s hash_const\n\n"%self.__dict__
         else:
-            intro='''static genhash_val_t hash_%(name)s(const void *vkey, 
size_t num_buckets)
+            intro='''static genhash_val_t hash_%(name)s(const void *vkey)
 {
 '''%self.__dict__
 
@@ -731,7 +731,7 @@
                 a="(%s << 8) ^ %s"%(keys[0], keys[1])
             else:
                 assert 0
-            body=body+('  return ((%s) %% num_buckets);\n'%a)
+            body=body+('  return %s;\n'%a)
             extro="}\n\n"
             return intro+body+extro
 
@@ -1640,7 +1640,7 @@
 
 #include "packets.h"
 
-static genhash_val_t hash_const(const void *vkey, size_t num_buckets)
+static genhash_val_t hash_const(const void *vkey)
 {
   return 0;
 }

Modified: trunk/common/government.c
URL: 
http://svn.gna.org/viewcvs/freeciv/trunk/common/government.c?rev=25010&r1=25009&r2=25010&view=diff
==============================================================================
--- trunk/common/government.c   (original)
+++ trunk/common/government.c   Mon Jun  2 09:24:46 2014
@@ -192,13 +192,9 @@
 /****************************************************************************
   Hash function.
 ****************************************************************************/
-static genhash_val_t nation_hash_val(const struct nation_type *pnation,
-                                     size_t num_buckets)
-{
-  genhash_val_t base = (NULL != pnation ? nation_number(pnation)
-                        : nation_count());
-
-  return base % num_buckets;
+static genhash_val_t nation_hash_val(const struct nation_type *pnation)
+{
+  return NULL != pnation ? nation_number(pnation) : nation_count();
 }
 
 /****************************************************************************

Modified: trunk/utility/genhash.c
URL: 
http://svn.gna.org/viewcvs/freeciv/trunk/utility/genhash.c?rev=25010&r1=25009&r2=25010&view=diff
==============================================================================
--- trunk/utility/genhash.c     (original)
+++ trunk/utility/genhash.c     Mon Jun  2 09:24:46 2014
@@ -105,7 +105,7 @@
   A supplied genhash function appropriate to nul-terminated strings.
   Prefers table sizes that are prime numbers.
 ****************************************************************************/
-genhash_val_t genhash_str_val_func(const void *vkey, size_t num_buckets)
+genhash_val_t genhash_str_val_func(const void *vkey)
 {
   const char *key = (const char *) vkey;
   unsigned long result = 0;
@@ -115,7 +115,7 @@
     result += *key;
   }
   result &= 0xFFFFFFFF; /* To make results independent of sizeof(long) */
-  return (result % num_buckets);
+  return result;
 }
 
 /****************************************************************************
@@ -150,10 +150,10 @@
   themselves; this way a void* (or, with casting, a long) can be used
   as a key, and also without having allocated space for it.
 ***************************************************************************/
-genhash_val_t genhash_ptr_val_func(const void *vkey, size_t num_buckets)
+genhash_val_t genhash_ptr_val_func(const void *vkey)
 {
   intptr_t result = ((intptr_t) vkey);
-  return (result % num_buckets);
+  return result;
 }
 
 /****************************************************************************
@@ -361,7 +361,7 @@
   end = bucket + pgenhash->num_buckets;
   for (; bucket < end; bucket++) {
     for (iter = *bucket; NULL != iter; iter = next) {
-      slot = new_buckets + key_val_func(iter->key, new_nbuckets);
+      slot = new_buckets + (key_val_func(iter->key) % new_nbuckets);
       next = iter->next;
       iter->next = *slot;
       *slot = iter;
@@ -429,7 +429,7 @@
   struct genhash_entry **slot;
 
   for (slot = (pgenhash->buckets
-               + pgenhash->key_val_func(key, pgenhash->num_buckets));
+               + (pgenhash->key_val_func(key) % pgenhash->num_buckets));
        NULL != *slot; slot = &(*slot)->next) {
     if (pgenhash->key_comp_func((*slot)->key, key)) {
       return slot;

Modified: trunk/utility/genhash.h
URL: 
http://svn.gna.org/viewcvs/freeciv/trunk/utility/genhash.h?rev=25010&r1=25009&r2=25010&view=diff
==============================================================================
--- trunk/utility/genhash.h     (original)
+++ trunk/utility/genhash.h     Mon Jun  2 09:24:46 2014
@@ -32,7 +32,7 @@
 typedef unsigned int genhash_val_t;
 
 /* Function typedefs: */
-typedef genhash_val_t (*genhash_val_fn_t) (const void *, size_t);
+typedef genhash_val_t (*genhash_val_fn_t) (const void *);
 typedef bool (*genhash_comp_fn_t) (const void *, const void *);
 typedef void * (*genhash_copy_fn_t) (const void *);
 typedef void (*genhash_free_fn_t) (void *);
@@ -40,7 +40,7 @@
 
 /* Supplied functions (matching above typedefs) appropriate for
  * keys being normal nul-terminated strings: */
-genhash_val_t genhash_str_val_func(const void *vkey, size_t num_buckets);
+genhash_val_t genhash_str_val_func(const void *vkey);
 bool genhash_str_comp_func(const void *vkey1, const void *vkey2);
 /* and malloc'ed strings: */
 void *genhash_str_copy_func(const void *vkey);
@@ -48,7 +48,7 @@
 
 /* Appropriate for void pointers, integers or casted longs, used as keys
  * directly instead of by reference. */
-genhash_val_t genhash_ptr_val_func(const void *vkey, size_t num_buckets);
+genhash_val_t genhash_ptr_val_func(const void *vkey);
 bool genhash_ptr_comp_func(const void *vkey1, const void *vkey2);
 
 

Modified: trunk/utility/spechash.h
URL: 
http://svn.gna.org/viewcvs/freeciv/trunk/utility/spechash.h?rev=25010&r1=25009&r2=25010&view=diff
==============================================================================
--- trunk/utility/spechash.h    (original)
+++ trunk/utility/spechash.h    Mon Jun  2 09:24:46 2014
@@ -42,7 +42,7 @@
  *    struct foo_hash_iter;
  *
  * function typedefs:
- *    typedef genhash_val_t (*foo_hash_key_val_fn_t) (const key_t, size_t);
+ *    typedef genhash_val_t (*foo_hash_key_val_fn_t) (const key_t);
  *    typedef bool (*foo_hash_key_comp_fn_t) (const key_t, const key_t);
  *    typedef key_t (*foo_hash_key_copy_fn_t) (const key_t);
  *    typedef void (*foo_hash_key_free_fn_t) (key_t);
@@ -189,7 +189,7 @@
 
 /* Function related typedefs. */
 typedef genhash_val_t
-(*SPECHASH_FOO(_hash_key_val_fn_t)) (const SPECHASH_KEY_TYPE, size_t);
+(*SPECHASH_FOO(_hash_key_val_fn_t)) (const SPECHASH_KEY_TYPE);
 typedef bool (*SPECHASH_FOO(_hash_key_comp_fn_t)) (const SPECHASH_KEY_TYPE,
                                                    const SPECHASH_KEY_TYPE);
 typedef SPECHASH_KEY_TYPE


_______________________________________________
Freeciv-commits mailing list
Freeciv-commits@gna.org
https://mail.gna.org/listinfo/freeciv-commits

Reply via email to