# HG changeset patch # User Me # Date 1288741550 25200 # Node ID e072db5aa783d4ebb907ac46802d0e3183323699 # Parent 1ee1005644083e0ccfe56bc64ed0c13ea5a93dbc Fixed bug -- strdup snuck a malloc in -- and added better insert Entry diff -r 1ee100564408 -r e072db5aa783 PrivateHash.c --- a/PrivateHash.c Sun Oct 31 20:25:30 2010 -0700 +++ b/PrivateHash.c Tue Nov 02 16:45:50 2010 -0700 @@ -114,8 +114,9 @@ { hashIdx = hashThisKey( key, table->tableSz ); hashEntry = (HashEntry*) VMS__malloc( sizeof( HashEntry ) ); if( hashEntry == NULL ) return 0; - hashEntry->key = strdup( key ); //TODO: figure out soln for incr Sz + hashEntry->key = VMS__malloc( strlen(key) ); if( hashEntry->key == NULL ) return 0; + strcpy( hashEntry->key, key ); hashEntry->next = (table->entries)[hashIdx]; (table->entries)[hashIdx] = hashEntry; table->numEntries += 1; @@ -143,13 +144,44 @@ } else { (*(table->freeEntryContentFn))( testEntry->content ); + //being lazy -- will create bug in code that relies on having ptr to + // elem given to insert into table! testEntry->content = entry->content; entry->content = NULL; - freeHashEntryUsing( entry, table ); + freeHashEntryButNotContent( entry ); } return 1; } +/*Better version + */ + int +untested_putEntryIntoTable( HashEntry *entry, HashTable * table ) + { HashEntry *testEntry, *prevEntry = NULL; + unsigned int + hashIndex = hashThisKey( entry->key, table->tableSz ); + + testEntry = table->entries[ hashIndex ]; + for( ; testEntry != NULL; testEntry = testEntry->next ) + { if( strcmp( testEntry->key, entry->key ) == 0 ) + { if( prevEntry == NULL ) + { table->entries[hashIndex] = entry; + entry->next = testEntry->next; + } + else + { prevEntry->next = entry; + entry->next = testEntry->next; + } + freeHashEntryUsing( testEntry, table ); //frees content too! + return; + } + } + //wasn't found, so insert + entry->next = table->entries[hashIndex]; + table->entries[hashIndex] = entry; + } + + bool8 deleteEntryFromTable( char *key, HashTable *table )