Commit 609546c4b8d5bb27e579a6bfb8cababad068c0f0

Werner Lemberg 2015-12-20T07:17:29

[base, bdf] Use a union as a hash key. We want to support both an integer and a string key later on. * include/freetype/internal/fthash.h (FT_Hashkey): New union. (FT_HashnodeRec): Updated. (ft_hash_insert, ft_hash_lookup): Renamed to ... (ft_hash_str_insert, ft_hash_str_lookup): ... this. * src/base/fthash.c (hash_bucket): Updated. (ft_hash_insert, ft_hash_lookup): Renamed to ... (hash_insert, hash_lookup): ... this. (ft_hash_str_insert, ft_hash_str_lookup): New wrapper functions. * src/bdf/bdflib.c: Updated.

diff --git a/ChangeLog b/ChangeLog
index 24bab49..54ebb86 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,21 @@
+2015-12-20  Werner Lemberg  <wl@gnu.org>
+
+	[base, bdf] Use a union as a hash key.
+
+	We want to support both an integer and a string key later on.
+
+	* include/freetype/internal/fthash.h (FT_Hashkey): New union.
+	(FT_HashnodeRec): Updated.
+	(ft_hash_insert, ft_hash_lookup): Renamed to ...
+	(ft_hash_str_insert, ft_hash_str_lookup): ... this.
+
+	* src/base/fthash.c (hash_bucket): Updated.
+	(ft_hash_insert, ft_hash_lookup): Renamed to ...
+	(hash_insert, hash_lookup): ... this.
+	(ft_hash_str_insert, ft_hash_str_lookup): New wrapper functions.
+
+	* src/bdf/bdflib.c: Updated.
+
 2015-12-19  Werner Lemberg  <wl@gnu.org>
 
 	[bdf] Use new hash functions.
diff --git a/include/freetype/internal/fthash.h b/include/freetype/internal/fthash.h
index 46496d6..3449b75 100644
--- a/include/freetype/internal/fthash.h
+++ b/include/freetype/internal/fthash.h
@@ -50,10 +50,18 @@
 FT_BEGIN_HEADER
 
 
+  typedef union  FT_Hashkey_
+  {
+    FT_Int       num;
+    const char*  str;
+
+  } FT_Hashkey;
+
+
   typedef struct  FT_HashnodeRec_
   {
-    const char*  key;
-    size_t       data;
+    FT_Hashkey  key;
+    size_t      data;
 
   } FT_HashnodeRec;
 
@@ -82,14 +90,14 @@ FT_BEGIN_HEADER
                 FT_Memory  memory );
 
   FT_Error
-  ft_hash_insert( char*      key,
-                  size_t     data,
-                  FT_Hash    hash,
-                  FT_Memory  memory );
+  ft_hash_str_insert( const char*  key,
+                      size_t       data,
+                      FT_Hash      hash,
+                      FT_Memory    memory );
 
   FT_Hashnode
-  ft_hash_lookup( const char*  key,
-                  FT_Hash      hash );
+  ft_hash_str_lookup( const char*  key,
+                      FT_Hash      hash );
 
 FT_END_HEADER
 
diff --git a/src/base/fthash.c b/src/base/fthash.c
index c26077c..091646a 100644
--- a/src/base/fthash.c
+++ b/src/base/fthash.c
@@ -47,10 +47,10 @@
 
 
   static FT_Hashnode*
-  hash_bucket( const char*  key,
-               FT_Hash      hash )
+  hash_bucket( FT_Hashkey  key,
+               FT_Hash     hash )
   {
-    const char*   kp  = key;
+    const char*   kp  = key.str;
     FT_ULong      res = 0;
     FT_Hashnode*  bp  = hash->table;
     FT_Hashnode*  ndp;
@@ -63,10 +63,10 @@
     ndp = bp + ( res % hash->size );
     while ( *ndp )
     {
-      kp = (*ndp)->key;
+      kp = (*ndp)->key.str;
 
-      if ( kp[0] == key[0]           &&
-           ft_strcmp( kp, key ) == 0 )
+      if ( kp[0] == key.str[0]           &&
+           ft_strcmp( kp, key.str ) == 0 )
         break;
 
       ndp--;
@@ -149,11 +149,11 @@
   }
 
 
-  FT_Error
-  ft_hash_insert( char*      key,
-                  size_t     data,
-                  FT_Hash    hash,
-                  FT_Memory  memory )
+  static FT_Error
+  hash_insert( FT_Hashkey  key,
+               size_t      data,
+               FT_Hash     hash,
+               FT_Memory   memory )
   {
     FT_Hashnode   nn;
     FT_Hashnode*  bp    = hash_bucket( key, hash );
@@ -187,9 +187,24 @@
   }
 
 
-  FT_Hashnode
-  ft_hash_lookup( const char*  key,
-                  FT_Hash      hash )
+  FT_Error
+  ft_hash_str_insert( const char*  key,
+                      size_t       data,
+                      FT_Hash      hash,
+                      FT_Memory    memory )
+  {
+    FT_Hashkey  hk;
+
+
+    hk.str = key;
+
+    return hash_insert( hk, data, hash, memory );
+  }
+
+
+  static FT_Hashnode
+  hash_lookup( FT_Hashkey  key,
+               FT_Hash     hash )
   {
     FT_Hashnode*  np = hash_bucket( key, hash );
 
@@ -198,4 +213,17 @@
   }
 
 
+  FT_Hashnode
+  ft_hash_str_lookup( const char*  key,
+                      FT_Hash      hash )
+  {
+    FT_Hashkey  hk;
+
+
+    hk.str = key;
+
+    return hash_lookup( hk, hash );
+  }
+
+
 /* END */
diff --git a/src/bdf/bdflib.c b/src/bdf/bdflib.c
index d4c22e2..34410a2 100644
--- a/src/bdf/bdflib.c
+++ b/src/bdf/bdflib.c
@@ -812,7 +812,7 @@
     /* First check whether the property has        */
     /* already been added or not.  If it has, then */
     /* simply ignore it.                           */
-    if ( ft_hash_lookup( name, &(font->proptbl) ) )
+    if ( ft_hash_str_lookup( name, &(font->proptbl) ) )
       goto Exit;
 
     if ( FT_RENEW_ARRAY( font->user_props,
@@ -837,7 +837,7 @@
 
     n = _num_bdf_properties + font->nuser_props;
 
-    error = ft_hash_insert( p->name, n, &(font->proptbl), memory );
+    error = ft_hash_str_insert( p->name, n, &(font->proptbl), memory );
     if ( error )
       goto Exit;
 
@@ -859,7 +859,7 @@
     if ( name == 0 || *name == 0 )
       return 0;
 
-    if ( ( hn = ft_hash_lookup( name, &(font->proptbl) ) ) == 0 )
+    if ( ( hn = ft_hash_str_lookup( name, &(font->proptbl) ) ) == 0 )
       return 0;
 
     propid = hn->data;
@@ -1084,7 +1084,7 @@
 
 
     /* First, check whether the property already exists in the font. */
-    if ( ( hn = ft_hash_lookup( name, (FT_Hash)font->internal ) ) != 0 )
+    if ( ( hn = ft_hash_str_lookup( name, (FT_Hash)font->internal ) ) != 0 )
     {
       /* The property already exists in the font, so simply replace */
       /* the value of the property with the current value.          */
@@ -1120,13 +1120,13 @@
 
     /* See whether this property type exists yet or not. */
     /* If not, create it.                                */
-    hn = ft_hash_lookup( name, &(font->proptbl) );
+    hn = ft_hash_str_lookup( name, &(font->proptbl) );
     if ( hn == 0 )
     {
       error = bdf_create_property( name, BDF_ATOM, font );
       if ( error )
         goto Exit;
-      hn = ft_hash_lookup( name, &(font->proptbl) );
+      hn = ft_hash_str_lookup( name, &(font->proptbl) );
     }
 
     /* Allocate another property if this is overflow. */
@@ -1187,10 +1187,10 @@
     if ( _bdf_strncmp( name, "COMMENT", 7 ) != 0 )
     {
       /* Add the property to the font property table. */
-      error = ft_hash_insert( fp->name,
-                              font->props_used,
-                              (FT_Hash)font->internal,
-                              memory );
+      error = ft_hash_str_insert( fp->name,
+                                  font->props_used,
+                                  (FT_Hash)font->internal,
+                                  memory );
       if ( error )
         goto Exit;
     }
@@ -1947,8 +1947,8 @@
         for ( i = 0, prop = (bdf_property_t*)_bdf_properties;
               i < _num_bdf_properties; i++, prop++ )
         {
-          error = ft_hash_insert( prop->name, i,
-                                  &(font->proptbl), memory );
+          error = ft_hash_str_insert( prop->name, i,
+                                      &(font->proptbl), memory );
           if ( error )
             goto Exit;
         }
@@ -2414,7 +2414,7 @@
     if ( font == 0 || font->props_size == 0 || name == 0 || *name == 0 )
       return 0;
 
-    hn = ft_hash_lookup( name, (FT_Hash)font->internal );
+    hn = ft_hash_str_lookup( name, (FT_Hash)font->internal );
 
     return hn ? ( font->props + hn->data ) : 0;
   }