comparison PrivateHash.c @ 5:e072db5aa783

Fixed bug -- strdup snuck a malloc in -- and added better insert Entry
author Me
date Tue, 02 Nov 2010 16:45:50 -0700
parents 1ee100564408
children ec5c00d4023e bac20745c52e
comparison
equal deleted inserted replaced
3:40d29777ed73 4:5de93c6d80b3
112 hashEntry = getEntryFromTable( key, table ); 112 hashEntry = getEntryFromTable( key, table );
113 if( hashEntry == NULL ) 113 if( hashEntry == NULL )
114 { hashIdx = hashThisKey( key, table->tableSz ); 114 { hashIdx = hashThisKey( key, table->tableSz );
115 hashEntry = (HashEntry*) VMS__malloc( sizeof( HashEntry ) ); 115 hashEntry = (HashEntry*) VMS__malloc( sizeof( HashEntry ) );
116 if( hashEntry == NULL ) return 0; 116 if( hashEntry == NULL ) return 0;
117 hashEntry->key = strdup( key ); //TODO: figure out soln for incr Sz 117 hashEntry->key = VMS__malloc( strlen(key) );
118 if( hashEntry->key == NULL ) return 0; 118 if( hashEntry->key == NULL ) return 0;
119 strcpy( hashEntry->key, key );
119 hashEntry->next = (table->entries)[hashIdx]; 120 hashEntry->next = (table->entries)[hashIdx];
120 (table->entries)[hashIdx] = hashEntry; 121 (table->entries)[hashIdx] = hashEntry;
121 table->numEntries += 1; 122 table->numEntries += 1;
122 if( table->tableSz < table->numEntries ) doubleTableSize( table ); 123 if( table->tableSz < table->numEntries ) doubleTableSize( table );
123 } 124 }
141 table->numEntries += 1; 142 table->numEntries += 1;
142 if( table->tableSz < table->numEntries ) doubleTableSize( table ); 143 if( table->tableSz < table->numEntries ) doubleTableSize( table );
143 } 144 }
144 else 145 else
145 { (*(table->freeEntryContentFn))( testEntry->content ); 146 { (*(table->freeEntryContentFn))( testEntry->content );
147 //being lazy -- will create bug in code that relies on having ptr to
148 // elem given to insert into table!
146 testEntry->content = entry->content; 149 testEntry->content = entry->content;
147 entry->content = NULL; 150 entry->content = NULL;
148 freeHashEntryUsing( entry, table ); 151 freeHashEntryButNotContent( entry );
149 } 152 }
150 return 1; 153 return 1;
151 } 154 }
155
156 /*Better version
157 */
158 int
159 untested_putEntryIntoTable( HashEntry *entry, HashTable * table )
160 { HashEntry *testEntry, *prevEntry = NULL;
161 unsigned int
162 hashIndex = hashThisKey( entry->key, table->tableSz );
163
164 testEntry = table->entries[ hashIndex ];
165 for( ; testEntry != NULL; testEntry = testEntry->next )
166 { if( strcmp( testEntry->key, entry->key ) == 0 )
167 { if( prevEntry == NULL )
168 { table->entries[hashIndex] = entry;
169 entry->next = testEntry->next;
170 }
171 else
172 { prevEntry->next = entry;
173 entry->next = testEntry->next;
174 }
175 freeHashEntryUsing( testEntry, table ); //frees content too!
176 return;
177 }
178 }
179 //wasn't found, so insert
180 entry->next = table->entries[hashIndex];
181 table->entries[hashIndex] = entry;
182 }
183
152 184
153 185
154 bool8 186 bool8
155 deleteEntryFromTable( char *key, HashTable *table ) 187 deleteEntryFromTable( char *key, HashTable *table )
156 { HashEntry *hashEntry; 188 { HashEntry *hashEntry;