setGetHashTable.c

Go to the documentation of this file.
00001 /*------------------------------------------------------------------------*/
00002 /* file: setGetHashTable.h                                                */
00003 /* Copyright INRIA 2006                                                   */
00004 /* Authors : Jean-Baptiste Silvy                                          */
00005 /* desc : define two hash table to be used in sci_set and sci_get         */
00006 /*        These hash table are based on the Scilab hashTable              */
00007 /*------------------------------------------------------------------------*/
00008 
00009 #include <string.h>
00010 
00011 #include "setGetHashTable.h"
00012 #include "MALLOC.h"
00013 
00014 /*-----------------------------------------------------------------------------------*/
00015 /* see http://www.cse.yorku.ca/~oz/hash.html */
00016 /* like in hashtable_localization by Allan Cornet */
00017 /* I choose djb2 algo. for strings */
00018 static unsigned int setGetHashTableHash( void * key )
00019 {
00020   unsigned long hash = 5381;
00021   int c ;
00022   char * str = key ;
00023 
00024   while ( (c = *str++) )
00025   {
00026     hash = ((hash << 5) + hash) + c ; /* hash * 33 + c */
00027   }
00028 
00029   return hash;
00030 
00031 }
00032 /*-----------------------------------------------------------------------------------*/
00033 static int setGetHashTableEqualKeys( void * k1, void * k2 )
00034 {
00035   if ( strcmp( (char *)k1, (char *)k2 ) == 0 )
00036   {
00037     return 1 ;
00038   }
00039   return 0 ;
00040 }
00041 /*-----------------------------------------------------------------------------------*/
00042 GetPropertyHashTable * createGetHashTable( void )
00043 {
00044   return create_hashtable(16, setGetHashTableHash, setGetHashTableEqualKeys ) ;
00045 }
00046 /*-----------------------------------------------------------------------------------*/
00047 void destroyGetHashTable( GetPropertyHashTable * hashTable )
00048 {
00049   /* we just store pointers */
00050   hashtable_destroy( hashTable, 0 ) ;
00051 }
00052 /*-----------------------------------------------------------------------------------*/
00053 getPropertyFunc searchGetHashtable( GetPropertyHashTable * hashTable, char * key )
00054 {
00055   return (getPropertyFunc) hashtable_search( hashTable, key ) ;
00056 }
00057 /*-----------------------------------------------------------------------------------*/
00058 int insertGetHashtable( GetPropertyHashTable * hashTable, char * key, getPropertyFunc value )
00059 {
00060   /* allocate a new key because the hashtable claims ownership */
00061   /* and will free it when destroyed */
00062   char * copyKey   = NULL ;
00063   int    keyLength = strlen( key ) + 1 ;
00064 
00065   copyKey = MALLOC( keyLength * sizeof(char) ) ;
00066   if ( copyKey == NULL ) { return 0 ; }
00067   strcpy( copyKey, key ) ;
00068 
00069   return hashtable_insert( hashTable, copyKey, value ) ;
00070 }
00071 /*-----------------------------------------------------------------------------------*/
00072 SetPropertyHashTable * createSetHashTable( void )
00073 {
00074   return create_hashtable(16, setGetHashTableHash, setGetHashTableEqualKeys ) ;
00075 }
00076 /*-----------------------------------------------------------------------------------*/
00077 void destroySetHashTable( SetPropertyHashTable * hashTable )
00078 {
00079   /* we just store pointers */
00080   hashtable_destroy( hashTable, 0 ) ;
00081 }
00082 /*-----------------------------------------------------------------------------------*/
00083 setPropertyFunc searchSetHashtable( SetPropertyHashTable * hashTable, char * key )
00084 {
00085   return (setPropertyFunc) hashtable_search( hashTable, key ) ;
00086 }
00087 /*-----------------------------------------------------------------------------------*/
00088 int insertSetHashtable( SetPropertyHashTable * hashTable, char * key, setPropertyFunc value )
00089 {
00090   /* allocate a new key because the hashtable claims ownership */
00091   /* and will free it when destroyed */
00092   char * copyKey   = NULL ;
00093   int    keyLength = strlen( key ) + 1 ;
00094 
00095   copyKey = MALLOC( keyLength * sizeof(char) ) ;
00096   strcpy( copyKey, key ) ;
00097 
00098   return hashtable_insert( hashTable, copyKey, value ) ;
00099 }
00100 /*-----------------------------------------------------------------------------------*/

Generated on Sun Mar 4 15:03:51 2007 for Scilab [trunk] by  doxygen 1.5.1