Mercurial > cgi-bin > hgwebdir.cgi > VMS > C_Libraries > Hash_impl
comparison PrivateHash.c @ 9:7c4d2bf121a9
Bugfix: Strings were used to handle keys. So 0 byte in key will terminate all functions.
| author | Merten Sach <msach@mailbox.tu-berlin.de> |
|---|---|
| date | Wed, 28 Sep 2011 13:38:15 +0200 |
| parents | 8b225987970d |
| children | 5b89d57e5d10 093cad17d992 |
comparison
equal
deleted
inserted
replaced
| 6:8f248a7fab2e | 8:19a6bc503b35 |
|---|---|
| 69 for( i = 0; i < tableSz; i++ ) | 69 for( i = 0; i < tableSz; i++ ) |
| 70 entries[ i ] = NULL; | 70 entries[ i ] = NULL; |
| 71 } | 71 } |
| 72 | 72 |
| 73 unsigned int | 73 unsigned int |
| 74 hashThisKey( char *s, int hashSz ) | 74 hashThisKey( char* s, int hashSz ) |
| 75 { unsigned int h = 0; | 75 { unsigned int h = 0; |
| 76 | 76 unsigned int i; |
| 77 for( ; *s != 0; s++ ) | 77 hashkey_t* key = (hashkey_t*)s; |
| 78 h = *s + h*31; | 78 |
| 79 for(i=0 ; i<sizeof(hashkey_t); i++ ) | |
| 80 h = key->hashable[i] + h*31; | |
| 79 return h % hashSz; | 81 return h % hashSz; |
| 80 } | 82 } |
| 81 | 83 |
| 82 /*Need this to be separated out, for use in both getParam and putParam | 84 /*Need this to be separated out, for use in both getParam and putParam |
| 83 */ | 85 */ |
| 86 { unsigned int | 88 { unsigned int |
| 87 hashIndex = hashThisKey( key, table->tableSz ); | 89 hashIndex = hashThisKey( key, table->tableSz ); |
| 88 HashEntry* | 90 HashEntry* |
| 89 hashEntry = table->entries[ hashIndex ]; | 91 hashEntry = table->entries[ hashIndex ]; |
| 90 for( ; hashEntry != NULL; hashEntry = hashEntry->next ) | 92 for( ; hashEntry != NULL; hashEntry = hashEntry->next ) |
| 91 { if( strcmp( hashEntry->key, key ) == 0 ) return hashEntry; | 93 { |
| 94 if( memcmp( hashEntry->key, key, sizeof(hashkey_t) ) == 0 ) | |
| 95 return hashEntry; | |
| 92 } | 96 } |
| 93 return NULL; | 97 return NULL; |
| 94 } | 98 } |
| 95 | 99 |
| 96 void * | 100 void * |
| 113 hashEntry = getEntryFromTable( key, table ); | 117 hashEntry = getEntryFromTable( key, table ); |
| 114 if( hashEntry == NULL ) | 118 if( hashEntry == NULL ) |
| 115 { hashIdx = hashThisKey( key, table->tableSz ); | 119 { hashIdx = hashThisKey( key, table->tableSz ); |
| 116 hashEntry = (HashEntry*) VMS__malloc( sizeof( HashEntry ) ); | 120 hashEntry = (HashEntry*) VMS__malloc( sizeof( HashEntry ) ); |
| 117 if( hashEntry == NULL ) return 0; | 121 if( hashEntry == NULL ) return 0; |
| 118 hashEntry->key = VMS__malloc( strlen(key) ); | 122 hashEntry->key = VMS__malloc( sizeof(hashkey_t) ); |
| 119 if( hashEntry->key == NULL ) return 0; | 123 if( hashEntry->key == NULL ) return 0; |
| 120 strcpy( hashEntry->key, key ); | 124 memcpy( hashEntry->key, key, sizeof(hashkey_t) ); |
| 121 hashEntry->next = (table->entries)[hashIdx]; | 125 hashEntry->next = (table->entries)[hashIdx]; |
| 122 (table->entries)[hashIdx] = hashEntry; | 126 (table->entries)[hashIdx] = hashEntry; |
| 123 table->numEntries += 1; | 127 table->numEntries += 1; |
| 124 if( table->tableSz < table->numEntries ) doubleTableSize( table ); | 128 if( table->tableSz < table->numEntries ) doubleTableSize( table ); |
| 125 } | 129 } |
| 162 unsigned int | 166 unsigned int |
| 163 hashIndex = hashThisKey( entry->key, table->tableSz ); | 167 hashIndex = hashThisKey( entry->key, table->tableSz ); |
| 164 | 168 |
| 165 testEntry = table->entries[ hashIndex ]; | 169 testEntry = table->entries[ hashIndex ]; |
| 166 for( ; testEntry != NULL; testEntry = testEntry->next ) | 170 for( ; testEntry != NULL; testEntry = testEntry->next ) |
| 167 { if( strcmp( testEntry->key, entry->key ) == 0 ) | 171 { if( memcmp( testEntry->key, entry->key, sizeof(hashkey_t)) == 0 ) |
| 168 { if( prevEntry == NULL ) | 172 { if( prevEntry == NULL ) |
| 169 { table->entries[hashIndex] = entry; | 173 { table->entries[hashIndex] = entry; |
| 170 entry->next = testEntry->next; | 174 entry->next = testEntry->next; |
| 171 } | 175 } |
| 172 else | 176 else |
| 192 | 196 |
| 193 hashIndex = hashThisKey( key, table->tableSz ); | 197 hashIndex = hashThisKey( key, table->tableSz ); |
| 194 addrOfHashEntryPtr = &( table->entries[ hashIndex ] ); | 198 addrOfHashEntryPtr = &( table->entries[ hashIndex ] ); |
| 195 hashEntry = *addrOfHashEntryPtr; | 199 hashEntry = *addrOfHashEntryPtr; |
| 196 while( hashEntry != NULL ) | 200 while( hashEntry != NULL ) |
| 197 { if( strcmp( hashEntry->key, key ) == 0 ) | 201 { if( memcmp( hashEntry->key, key, sizeof(hashkey_t) ) == 0 ) |
| 198 { | 202 { |
| 199 *addrOfHashEntryPtr = hashEntry->next; | 203 *addrOfHashEntryPtr = hashEntry->next; |
| 200 //TODO: Free the contents of entry? | 204 //TODO: Free the contents of entry? |
| 201 freeHashEntryButNotContent( hashEntry ); | 205 freeHashEntryButNotContent( hashEntry ); |
| 202 table->numEntries -= 1; | 206 table->numEntries -= 1; |
| 205 addrOfHashEntryPtr = &( hashEntry->next ); | 209 addrOfHashEntryPtr = &( hashEntry->next ); |
| 206 hashEntry = *addrOfHashEntryPtr; | 210 hashEntry = *addrOfHashEntryPtr; |
| 207 } | 211 } |
| 208 return FALSE; | 212 return FALSE; |
| 209 } | 213 } |
| 210 | |
| 211 | 214 |
| 212 /* debugging function displays the hashtable in (key.value) pairs | |
| 213 */ | |
| 214 /*void hashTableToString( HashTable * table ) | |
| 215 { int i; | |
| 216 HashEntry *t; | |
| 217 for( i = 0; i < table->tableSz; i++ ) | |
| 218 { t = entries[i]; | |
| 219 if( t == NULL ) | |
| 220 strcat_m( retStr, &"()" ); | |
| 221 else | |
| 222 { strcat_m( retStr, &"(" ); | |
| 223 for( ; t != NULL; t = t->next ) | |
| 224 { strcat_m( retStr, &" " ); | |
| 225 strcat_m( retStr, t->key ); | |
| 226 strcat_m( retStr, &"." ); | |
| 227 strcat_m( retStr, paramToString( t->param ) ); | |
| 228 strcat_m( retStr, &" " ); | |
| 229 } | |
| 230 strcat_m( retStr, &")" ); | |
| 231 } | |
| 232 } | |
| 233 } | |
| 234 */ | |
| 235 | |
| 236 /* | |
| 237 void | |
| 238 setFnToFreeEntryContent( HashTable *table, FreeEntryContentFnPtr fnPtr ) | |
| 239 { | |
| 240 table->freeEntryContentFn = fnPtr; | |
| 241 } | |
| 242 */ | |
| 243 | |
| 244 void | 215 void |
| 245 freeHashTable( HashTable *table ) | 216 freeHashTable( HashTable *table ) |
| 246 { int i; | 217 { int i; |
| 247 HashEntry *hashEntry, *temp, **entries; | 218 HashEntry *hashEntry, *temp, **entries; |
| 248 | 219 |
