# HG changeset patch # User Me # Date 1280348094 25200 # Node ID 5900d90f5d714090fe9a04f6eb41754812dde9c9 # Parent ee3ad252427e2ef12ca7db7fa0676187868c47f1 Works -- fixed some minor bugs -- VMSHW_matrix_mult working diff -r ee3ad252427e -r 5900d90f5d71 PrivateHash.c --- a/PrivateHash.c Sat May 22 19:49:28 2010 -0700 +++ b/PrivateHash.c Wed Jul 28 13:14:54 2010 -0700 @@ -2,7 +2,6 @@ * Copyright 2009 OpenSourceCodeStewardshipFoundation.org * Licensed under GNU General Public License version 2 * - * NOTE: this version of SRSW correct as of April 25, 2010 * * Author: seanhalle@yahoo.com */ @@ -23,8 +22,9 @@ retTable->freeEntryContentFn = freeFn; - retTable->entries = malloc( numHashSlots * sizeof(HashEntry *) ); - retTable->tableSz = numHashSlots; + retTable->entries = malloc( numHashSlots * sizeof(HashEntry *) ); + retTable->numEntries = 0; + retTable->tableSz = numHashSlots; nullOutTablesArray( retTable ); @@ -34,7 +34,7 @@ void doubleTableSize( HashTable *table ) { int i, oldTableSz, newTableSz; - HashEntry *entry, **oldEntries, **newEntries; + HashEntry *entry, *nextEntry, **oldEntries, **newEntries; oldTableSz = table->tableSz; oldEntries = table->entries; @@ -51,15 +51,16 @@ { if( oldEntries[i] != NULL ) { entry = oldEntries[i]; while( entry != NULL ) - { - addEntryToTable( entry, table ); //does not allocate anything - entry = entry->next; + { nextEntry = entry->next; + entry->next = NULL; + putEntryIntoTable( entry, table ); //does not allocate anything + entry = nextEntry; } } } } - void +void nullOutTablesArray( HashTable *table ) { int i, tableSz; tableSz = table->tableSz; @@ -68,7 +69,7 @@ entries[ i ] = NULL; } - unsigned int +unsigned int hashThisKey( char *s, int hashSz ) { unsigned int h = 0; @@ -79,10 +80,10 @@ /*Need this to be separated out, for use in both getParam and putParam */ - HashEntry * +HashEntry * getEntryFromTable( char *key, HashTable * table ) { unsigned int - hashIndex = hashKey( key, table->tableSz ); + hashIndex = hashThisKey( key, table->tableSz ); HashEntry* hashEntry = table->entries[ hashIndex ]; for( ; hashEntry != NULL; hashEntry = hashEntry->next ) @@ -91,17 +92,20 @@ return NULL; } - void * +void * getValueFromTable( char *key, HashTable * table ) { HashEntry *entry; - entry = lookupKeyInHash( key, table ); + entry = getEntryFromTable( key, table ); if( entry == NULL ) return NULL; return entry->content; } + +/*If key already has a value, clobber the old one and replace it + */ int -addToTable( char* key, void *content, HashTable *table ) +addValueIntoTable( char* key, void *content, HashTable *table ) { unsigned int hashIdx; HashEntry* hashEntry; @@ -110,7 +114,7 @@ { hashIdx = hashThisKey( key, table->tableSz ); hashEntry = (HashEntry*) malloc( sizeof( HashEntry ) ); if( hashEntry == NULL ) return 0; - hashEntry->key = strdup_m( key ); //TODO: figure out soln for incr Sz + hashEntry->key = strdup( key ); //TODO: figure out soln for incr Sz if( hashEntry->key == NULL ) return 0; hashEntry->next = (table->entries)[hashIdx]; (table->entries)[hashIdx] = hashEntry; @@ -125,7 +129,7 @@ } int -addEntryToTable( HashEntry *entry, HashTable *table ) +putEntryIntoTable( HashEntry *entry, HashTable *table ) { unsigned int hashIdx; HashEntry* testEntry; @@ -146,22 +150,32 @@ return 1; } + bool8 deleteEntryFromTable( char *key, HashTable *table ) - { - table->numEntries -= 1; + { HashEntry *hashEntry; + HashEntry **addrOfHashEntryPtr; + unsigned int hashIndex; + hashIndex = hashThisKey( key, table->tableSz ); + addrOfHashEntryPtr = &( table->entries[ hashIndex ] ); + hashEntry = *addrOfHashEntryPtr; + while( hashEntry != NULL ) + { if( strcmp( hashEntry->key, key ) == 0 ) + { + *addrOfHashEntryPtr = hashEntry->next; + //TODO: Free the contents of entry? + freeHashEntryButNotContent( hashEntry ); + table->numEntries -= 1; + return TRUE; + } + addrOfHashEntryPtr = &( hashEntry->next ); + hashEntry = *addrOfHashEntryPtr; + } + return FALSE; } - - char* -strdup_m( char *o ) - { int len = strlen(o)+1; - char *ns = (char*) malloc( len * sizeof(char) ); - strcpy( ns, o ); - return ns; - } - + /* debugging function displays the hashtable in (key.value) pairs */ /*void hashTableToString( HashTable * table ) @@ -194,7 +208,7 @@ } */ - void +void freeHashTable( HashTable *table ) { int i; HashEntry *hashEntry, *temp, **entries; @@ -213,7 +227,7 @@ } } - void +void freeHashEntryUsing( HashEntry *entry, HashTable *table ) { if( entry->content != NULL ) @@ -222,3 +236,10 @@ free( entry ); } +void +freeHashEntryButNotContent( HashEntry *entry ) + { + free( entry->key ); //was malloc'd above, so free it + free( entry ); + } + diff -r ee3ad252427e -r 5900d90f5d71 PrivateHash.h --- a/PrivateHash.h Sat May 22 19:49:28 2010 -0700 +++ b/PrivateHash.h Wed Jul 28 13:14:54 2010 -0700 @@ -8,12 +8,13 @@ #ifndef _PRIVATE_HASH_H #define _PRIVATE_HASH_H +#include "../VMS_primitive_data_types.h" #define TRUE 1 #define FALSE 0 -#define HASHSIZE 101 +#define DEFAULT_HASHSIZE 1 << 10 typedef struct _HashEntry HashEntry; @@ -34,27 +35,30 @@ } HashTable; -//=========================================================================== -// Internal functions -void freeHashEntryUsing( HashEntry *entry, HashTable *table ); -char* strdup_m(char *o); -unsigned int hashThisKey( char *s, int hashSz ); -void nullOutTablesArray( HashTable *table ); -void *getEntryFromTable( char *key, HashTable *table ); -void doubleTableSize( HashTable *table ); -void addEntryToTable( HashEntry *entry, HashTable *table ); //=========================================================================== // Public functions HashTable *makeHashTable( int numHashSlots, FreeEntryContentFnPtr freeFn ); +int putEntryIntoTable( HashEntry *entry, HashTable *table); +int addValueIntoTable( char* key, void *value, HashTable *table); +HashEntry *getEntryFromTable( char *key, HashTable *table ); void *getValueFromTable( char *key, HashTable *table ); + bool8 deleteEntryFromTable( char *key, HashTable *table ); +bool8 deleteThisEntryFromTable( HashEntry *entry, HashTable *table ); bool8 deleteEntrysValueInTable( char *key, HashTable *table ); +bool8 deleteEntryFromTableAndFreeValue( char *key, HashTable *table ); +void freeHashTable( HashTable *table ); +//char *paramBagToString( ParamBag * bag ) -int addToTable( char* key, void *content, HashTable *table ); -void freeTable( HashTable *table ); -//char *paramBagToString( ParamBag * bag ) +//=========================================================================== +// Internal functions +void freeHashEntryUsing( HashEntry *entry, HashTable *table ); +unsigned int hashThisKey( char *s, int hashSz ); +void nullOutTablesArray( HashTable *table ); +void doubleTableSize( HashTable *table ); +void freeHashEntryButNotContent( HashEntry *entry ); #endif /* _PRIVATE_HASH_H */