Mercurial > cgi-bin > hgwebdir.cgi > VMS > C_Libraries > Hash_impl
changeset 1:5900d90f5d71
Works -- fixed some minor bugs -- VMSHW_matrix_mult working
| author | Me |
|---|---|
| date | Wed, 28 Jul 2010 13:14:54 -0700 |
| parents | ee3ad252427e |
| children | 1218b245530c |
| files | PrivateHash.c PrivateHash.h |
| diffstat | 2 files changed, 67 insertions(+), 42 deletions(-) [+] |
line diff
1.1 --- a/PrivateHash.c Sat May 22 19:49:28 2010 -0700 1.2 +++ b/PrivateHash.c Wed Jul 28 13:14:54 2010 -0700 1.3 @@ -2,7 +2,6 @@ 1.4 * Copyright 2009 OpenSourceCodeStewardshipFoundation.org 1.5 * Licensed under GNU General Public License version 2 1.6 * 1.7 - * NOTE: this version of SRSW correct as of April 25, 2010 1.8 * 1.9 * Author: seanhalle@yahoo.com 1.10 */ 1.11 @@ -23,8 +22,9 @@ 1.12 1.13 retTable->freeEntryContentFn = freeFn; 1.14 1.15 - retTable->entries = malloc( numHashSlots * sizeof(HashEntry *) ); 1.16 - retTable->tableSz = numHashSlots; 1.17 + retTable->entries = malloc( numHashSlots * sizeof(HashEntry *) ); 1.18 + retTable->numEntries = 0; 1.19 + retTable->tableSz = numHashSlots; 1.20 1.21 nullOutTablesArray( retTable ); 1.22 1.23 @@ -34,7 +34,7 @@ 1.24 void 1.25 doubleTableSize( HashTable *table ) 1.26 { int i, oldTableSz, newTableSz; 1.27 - HashEntry *entry, **oldEntries, **newEntries; 1.28 + HashEntry *entry, *nextEntry, **oldEntries, **newEntries; 1.29 1.30 oldTableSz = table->tableSz; 1.31 oldEntries = table->entries; 1.32 @@ -51,15 +51,16 @@ 1.33 { if( oldEntries[i] != NULL ) 1.34 { entry = oldEntries[i]; 1.35 while( entry != NULL ) 1.36 - { 1.37 - addEntryToTable( entry, table ); //does not allocate anything 1.38 - entry = entry->next; 1.39 + { nextEntry = entry->next; 1.40 + entry->next = NULL; 1.41 + putEntryIntoTable( entry, table ); //does not allocate anything 1.42 + entry = nextEntry; 1.43 } 1.44 } 1.45 } 1.46 } 1.47 1.48 - void 1.49 +void 1.50 nullOutTablesArray( HashTable *table ) 1.51 { int i, tableSz; 1.52 tableSz = table->tableSz; 1.53 @@ -68,7 +69,7 @@ 1.54 entries[ i ] = NULL; 1.55 } 1.56 1.57 - unsigned int 1.58 +unsigned int 1.59 hashThisKey( char *s, int hashSz ) 1.60 { unsigned int h = 0; 1.61 1.62 @@ -79,10 +80,10 @@ 1.63 1.64 /*Need this to be separated out, for use in both getParam and putParam 1.65 */ 1.66 - HashEntry * 1.67 +HashEntry * 1.68 getEntryFromTable( char *key, HashTable * table ) 1.69 { unsigned int 1.70 - hashIndex = hashKey( key, table->tableSz ); 1.71 + hashIndex = hashThisKey( key, table->tableSz ); 1.72 HashEntry* 1.73 hashEntry = table->entries[ hashIndex ]; 1.74 for( ; hashEntry != NULL; hashEntry = hashEntry->next ) 1.75 @@ -91,17 +92,20 @@ 1.76 return NULL; 1.77 } 1.78 1.79 - void * 1.80 +void * 1.81 getValueFromTable( char *key, HashTable * table ) 1.82 { HashEntry *entry; 1.83 - entry = lookupKeyInHash( key, table ); 1.84 + entry = getEntryFromTable( key, table ); 1.85 if( entry == NULL ) return NULL; 1.86 1.87 return entry->content; 1.88 } 1.89 1.90 + 1.91 +/*If key already has a value, clobber the old one and replace it 1.92 + */ 1.93 int 1.94 -addToTable( char* key, void *content, HashTable *table ) 1.95 +addValueIntoTable( char* key, void *content, HashTable *table ) 1.96 { unsigned int hashIdx; 1.97 HashEntry* hashEntry; 1.98 1.99 @@ -110,7 +114,7 @@ 1.100 { hashIdx = hashThisKey( key, table->tableSz ); 1.101 hashEntry = (HashEntry*) malloc( sizeof( HashEntry ) ); 1.102 if( hashEntry == NULL ) return 0; 1.103 - hashEntry->key = strdup_m( key ); //TODO: figure out soln for incr Sz 1.104 + hashEntry->key = strdup( key ); //TODO: figure out soln for incr Sz 1.105 if( hashEntry->key == NULL ) return 0; 1.106 hashEntry->next = (table->entries)[hashIdx]; 1.107 (table->entries)[hashIdx] = hashEntry; 1.108 @@ -125,7 +129,7 @@ 1.109 } 1.110 1.111 int 1.112 -addEntryToTable( HashEntry *entry, HashTable *table ) 1.113 +putEntryIntoTable( HashEntry *entry, HashTable *table ) 1.114 { unsigned int hashIdx; 1.115 HashEntry* testEntry; 1.116 1.117 @@ -146,22 +150,32 @@ 1.118 return 1; 1.119 } 1.120 1.121 + 1.122 bool8 1.123 deleteEntryFromTable( char *key, HashTable *table ) 1.124 - { 1.125 - table->numEntries -= 1; 1.126 + { HashEntry *hashEntry; 1.127 + HashEntry **addrOfHashEntryPtr; 1.128 + unsigned int hashIndex; 1.129 1.130 + hashIndex = hashThisKey( key, table->tableSz ); 1.131 + addrOfHashEntryPtr = &( table->entries[ hashIndex ] ); 1.132 + hashEntry = *addrOfHashEntryPtr; 1.133 + while( hashEntry != NULL ) 1.134 + { if( strcmp( hashEntry->key, key ) == 0 ) 1.135 + { 1.136 + *addrOfHashEntryPtr = hashEntry->next; 1.137 + //TODO: Free the contents of entry? 1.138 + freeHashEntryButNotContent( hashEntry ); 1.139 + table->numEntries -= 1; 1.140 + return TRUE; 1.141 + } 1.142 + addrOfHashEntryPtr = &( hashEntry->next ); 1.143 + hashEntry = *addrOfHashEntryPtr; 1.144 + } 1.145 + return FALSE; 1.146 } 1.147 1.148 - 1.149 - char* 1.150 -strdup_m( char *o ) 1.151 - { int len = strlen(o)+1; 1.152 - char *ns = (char*) malloc( len * sizeof(char) ); 1.153 - strcpy( ns, o ); 1.154 - return ns; 1.155 - } 1.156 - 1.157 + 1.158 /* debugging function displays the hashtable in (key.value) pairs 1.159 */ 1.160 /*void hashTableToString( HashTable * table ) 1.161 @@ -194,7 +208,7 @@ 1.162 } 1.163 */ 1.164 1.165 - void 1.166 +void 1.167 freeHashTable( HashTable *table ) 1.168 { int i; 1.169 HashEntry *hashEntry, *temp, **entries; 1.170 @@ -213,7 +227,7 @@ 1.171 } 1.172 } 1.173 1.174 - void 1.175 +void 1.176 freeHashEntryUsing( HashEntry *entry, HashTable *table ) 1.177 { 1.178 if( entry->content != NULL ) 1.179 @@ -222,3 +236,10 @@ 1.180 free( entry ); 1.181 } 1.182 1.183 +void 1.184 +freeHashEntryButNotContent( HashEntry *entry ) 1.185 + { 1.186 + free( entry->key ); //was malloc'd above, so free it 1.187 + free( entry ); 1.188 + } 1.189 +
2.1 --- a/PrivateHash.h Sat May 22 19:49:28 2010 -0700 2.2 +++ b/PrivateHash.h Wed Jul 28 13:14:54 2010 -0700 2.3 @@ -8,12 +8,13 @@ 2.4 #ifndef _PRIVATE_HASH_H 2.5 #define _PRIVATE_HASH_H 2.6 2.7 +#include "../VMS_primitive_data_types.h" 2.8 2.9 #define TRUE 1 2.10 #define FALSE 0 2.11 2.12 2.13 -#define HASHSIZE 101 2.14 +#define DEFAULT_HASHSIZE 1 << 10 2.15 2.16 typedef struct _HashEntry HashEntry; 2.17 2.18 @@ -34,27 +35,30 @@ 2.19 } 2.20 HashTable; 2.21 2.22 -//=========================================================================== 2.23 -// Internal functions 2.24 -void freeHashEntryUsing( HashEntry *entry, HashTable *table ); 2.25 -char* strdup_m(char *o); 2.26 -unsigned int hashThisKey( char *s, int hashSz ); 2.27 -void nullOutTablesArray( HashTable *table ); 2.28 -void *getEntryFromTable( char *key, HashTable *table ); 2.29 -void doubleTableSize( HashTable *table ); 2.30 -void addEntryToTable( HashEntry *entry, HashTable *table ); 2.31 2.32 //=========================================================================== 2.33 // Public functions 2.34 HashTable *makeHashTable( int numHashSlots, FreeEntryContentFnPtr freeFn ); 2.35 2.36 +int putEntryIntoTable( HashEntry *entry, HashTable *table); 2.37 +int addValueIntoTable( char* key, void *value, HashTable *table); 2.38 +HashEntry *getEntryFromTable( char *key, HashTable *table ); 2.39 void *getValueFromTable( char *key, HashTable *table ); 2.40 + 2.41 bool8 deleteEntryFromTable( char *key, HashTable *table ); 2.42 +bool8 deleteThisEntryFromTable( HashEntry *entry, HashTable *table ); 2.43 bool8 deleteEntrysValueInTable( char *key, HashTable *table ); 2.44 +bool8 deleteEntryFromTableAndFreeValue( char *key, HashTable *table ); 2.45 +void freeHashTable( HashTable *table ); 2.46 +//char *paramBagToString( ParamBag * bag ) 2.47 2.48 -int addToTable( char* key, void *content, HashTable *table ); 2.49 -void freeTable( HashTable *table ); 2.50 -//char *paramBagToString( ParamBag * bag ) 2.51 +//=========================================================================== 2.52 +// Internal functions 2.53 +void freeHashEntryUsing( HashEntry *entry, HashTable *table ); 2.54 +unsigned int hashThisKey( char *s, int hashSz ); 2.55 +void nullOutTablesArray( HashTable *table ); 2.56 +void doubleTableSize( HashTable *table ); 2.57 +void freeHashEntryButNotContent( HashEntry *entry ); 2.58 2.59 #endif /* _PRIVATE_HASH_H */ 2.60
