Mercurial > cgi-bin > hgwebdir.cgi > VMS > C_Libraries > Hash_impl
changeset 11:8bafd14e9fde
merged VMS__malloc_brch
| author | Merten Sach <msach@mailbox.tu-berlin.de> |
|---|---|
| date | Wed, 28 Sep 2011 13:45:34 +0200 |
| parents | e6fe47763ee6 7c4d2bf121a9 |
| children | 40ec8f9583d8 |
| files | |
| diffstat | 3 files changed, 61 insertions(+), 48 deletions(-) [+] |
line diff
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/.hgignore Wed Sep 28 13:45:34 2011 +0200 1.3 @@ -0,0 +1,3 @@ 1.4 +syntax: glob 1.5 + 1.6 +*.o
2.1 --- a/PrivateHash.c Sat Sep 11 07:57:41 2010 -0700 2.2 +++ b/PrivateHash.c Wed Sep 28 13:45:34 2011 +0200 2.3 @@ -13,16 +13,17 @@ 2.4 #include <stdlib.h> 2.5 2.6 #include "PrivateHash.h" 2.7 +#include "../vmalloc.h" 2.8 2.9 2.10 HashTable * 2.11 makeHashTable( int numHashSlots, FreeEntryContentFnPtr freeFn ) 2.12 { HashTable * retTable; 2.13 - retTable = malloc( sizeof( HashTable ) ); 2.14 + retTable = VMS__malloc( sizeof( HashTable ) ); 2.15 2.16 retTable->freeEntryContentFn = freeFn; 2.17 2.18 - retTable->entries = malloc( numHashSlots * sizeof(HashEntry *) ); 2.19 + retTable->entries = VMS__malloc( numHashSlots * sizeof(HashEntry *) ); 2.20 retTable->numEntries = 0; 2.21 retTable->tableSz = numHashSlots; 2.22 2.23 @@ -40,7 +41,7 @@ 2.24 oldEntries = table->entries; 2.25 2.26 newTableSz = 2 * oldTableSz + 1; 2.27 - newEntries = malloc( newTableSz * sizeof(HashEntry *) ); 2.28 + newEntries = VMS__malloc( newTableSz * sizeof(HashEntry *) ); 2.29 2.30 table->tableSz = newTableSz; 2.31 table->entries = newEntries; 2.32 @@ -70,11 +71,13 @@ 2.33 } 2.34 2.35 unsigned int 2.36 -hashThisKey( char *s, int hashSz ) 2.37 +hashThisKey( char* s, int hashSz ) 2.38 { unsigned int h = 0; 2.39 + unsigned int i; 2.40 + hashkey_t* key = (hashkey_t*)s; 2.41 2.42 - for( ; *s != 0; s++ ) 2.43 - h = *s + h*31; 2.44 + for(i=0 ; i<sizeof(hashkey_t); i++ ) 2.45 + h = key->hashable[i] + h*31; 2.46 return h % hashSz; 2.47 } 2.48 2.49 @@ -87,7 +90,9 @@ 2.50 HashEntry* 2.51 hashEntry = table->entries[ hashIndex ]; 2.52 for( ; hashEntry != NULL; hashEntry = hashEntry->next ) 2.53 - { if( strcmp( hashEntry->key, key ) == 0 ) return hashEntry; 2.54 + { 2.55 + if( memcmp( hashEntry->key, key, sizeof(hashkey_t) ) == 0 ) 2.56 + return hashEntry; 2.57 } 2.58 return NULL; 2.59 } 2.60 @@ -112,10 +117,11 @@ 2.61 hashEntry = getEntryFromTable( key, table ); 2.62 if( hashEntry == NULL ) 2.63 { hashIdx = hashThisKey( key, table->tableSz ); 2.64 - hashEntry = (HashEntry*) malloc( sizeof( HashEntry ) ); 2.65 + hashEntry = (HashEntry*) VMS__malloc( sizeof( HashEntry ) ); 2.66 if( hashEntry == NULL ) return 0; 2.67 - hashEntry->key = strdup( key ); //TODO: figure out soln for incr Sz 2.68 + hashEntry->key = VMS__malloc( sizeof(hashkey_t) ); 2.69 if( hashEntry->key == NULL ) return 0; 2.70 + memcpy( hashEntry->key, key, sizeof(hashkey_t) ); 2.71 hashEntry->next = (table->entries)[hashIdx]; 2.72 (table->entries)[hashIdx] = hashEntry; 2.73 table->numEntries += 1; 2.74 @@ -143,13 +149,44 @@ 2.75 } 2.76 else 2.77 { (*(table->freeEntryContentFn))( testEntry->content ); 2.78 + //being lazy -- will create bug in code that relies on having ptr to 2.79 + // elem given to insert into table! 2.80 testEntry->content = entry->content; 2.81 entry->content = NULL; 2.82 - freeHashEntryUsing( entry, table ); 2.83 + freeHashEntryButNotContent( entry ); 2.84 } 2.85 return 1; 2.86 } 2.87 2.88 +/*Better version 2.89 + */ 2.90 + void 2.91 +untested_putEntryIntoTable( HashEntry *entry, HashTable * table ) 2.92 + { HashEntry *testEntry, *prevEntry = NULL; 2.93 + unsigned int 2.94 + hashIndex = hashThisKey( entry->key, table->tableSz ); 2.95 + 2.96 + testEntry = table->entries[ hashIndex ]; 2.97 + for( ; testEntry != NULL; testEntry = testEntry->next ) 2.98 + { if( memcmp( testEntry->key, entry->key, sizeof(hashkey_t)) == 0 ) 2.99 + { if( prevEntry == NULL ) 2.100 + { table->entries[hashIndex] = entry; 2.101 + entry->next = testEntry->next; 2.102 + } 2.103 + else 2.104 + { prevEntry->next = entry; 2.105 + entry->next = testEntry->next; 2.106 + } 2.107 + freeHashEntryUsing( testEntry, table ); //frees content too! 2.108 + return; 2.109 + } 2.110 + } 2.111 + //wasn't found, so insert 2.112 + entry->next = table->entries[hashIndex]; 2.113 + table->entries[hashIndex] = entry; 2.114 + } 2.115 + 2.116 + 2.117 2.118 bool8 2.119 deleteEntryFromTable( char *key, HashTable *table ) 2.120 @@ -161,7 +198,7 @@ 2.121 addrOfHashEntryPtr = &( table->entries[ hashIndex ] ); 2.122 hashEntry = *addrOfHashEntryPtr; 2.123 while( hashEntry != NULL ) 2.124 - { if( strcmp( hashEntry->key, key ) == 0 ) 2.125 + { if( memcmp( hashEntry->key, key, sizeof(hashkey_t) ) == 0 ) 2.126 { 2.127 *addrOfHashEntryPtr = hashEntry->next; 2.128 //TODO: Free the contents of entry? 2.129 @@ -174,40 +211,7 @@ 2.130 } 2.131 return FALSE; 2.132 } 2.133 - 2.134 2.135 -/* debugging function displays the hashtable in (key.value) pairs 2.136 -*/ 2.137 -/*void hashTableToString( HashTable * table ) 2.138 - { int i; 2.139 - HashEntry *t; 2.140 - for( i = 0; i < table->tableSz; i++ ) 2.141 - { t = entries[i]; 2.142 - if( t == NULL ) 2.143 - strcat_m( retStr, &"()" ); 2.144 - else 2.145 - { strcat_m( retStr, &"(" ); 2.146 - for( ; t != NULL; t = t->next ) 2.147 - { strcat_m( retStr, &" " ); 2.148 - strcat_m( retStr, t->key ); 2.149 - strcat_m( retStr, &"." ); 2.150 - strcat_m( retStr, paramToString( t->param ) ); 2.151 - strcat_m( retStr, &" " ); 2.152 - } 2.153 - strcat_m( retStr, &")" ); 2.154 - } 2.155 - } 2.156 - } 2.157 -*/ 2.158 - 2.159 -/* 2.160 - void 2.161 -setFnToFreeEntryContent( HashTable *table, FreeEntryContentFnPtr fnPtr ) 2.162 - { 2.163 - table->freeEntryContentFn = fnPtr; 2.164 - } 2.165 -*/ 2.166 - 2.167 void 2.168 freeHashTable( HashTable *table ) 2.169 { int i; 2.170 @@ -232,14 +236,14 @@ 2.171 { 2.172 if( entry->content != NULL ) 2.173 (*(table->freeEntryContentFn))( entry->content ); 2.174 - free( entry->key ); //was malloc'd above, so free it 2.175 - free( entry ); 2.176 + VMS__free( entry->key ); //was VMS__malloc'd above, so free it 2.177 + VMS__free( entry ); 2.178 } 2.179 2.180 void 2.181 freeHashEntryButNotContent( HashEntry *entry ) 2.182 { 2.183 - free( entry->key ); //was malloc'd above, so free it 2.184 - free( entry ); 2.185 + VMS__free( entry->key ); //was VMS__malloc'd above, so free it 2.186 + VMS__free( entry ); 2.187 } 2.188
3.1 --- a/PrivateHash.h Sat Sep 11 07:57:41 2010 -0700 3.2 +++ b/PrivateHash.h Wed Sep 28 13:45:34 2011 +0200 3.3 @@ -13,6 +13,12 @@ 3.4 #define TRUE 1 3.5 #define FALSE 0 3.6 3.7 +union hashkey_t{ 3.8 + char hashable[8]; 3.9 + int parts[2]; 3.10 +}; 3.11 + 3.12 +typedef union hashkey_t hashkey_t; 3.13 3.14 #define DEFAULT_HASHSIZE 1 << 10 3.15
