CS249-Projects/PA10-1115/HashUtility.c
cdez 6911280eb5 Nightly Data Dump
+- HashUtility.c
++ PA10_Main
2023-11-13 23:22:36 -07:00

287 lines
No EOL
9.5 KiB
C

// header files
#include "GeneralUtility.h"
#include "HashUtility.h"
#include "StateDataUtility.h"
// constants
/* Data Structures
typedef struct HashStruct
{
StateDataType *array;
int tableSize;
ProbeType probeStrategy;
} ProbingHashType;
Data Structures */
// function definitions
/*
Name: addItemFromData
Process: adds item to hash table using data input,
sets node to used state
Function input/parameters: hash data (ProbingHashType *),
state name (const char *),
average, lowest, and highest temperatures (double),
Function output/parameters: updated hash table data (ProbingHashType *)
Function output/returned: result of operation (bool)
Device input/---: none
Device output/monitor: probing process displayed (in called function)
Dependencies: setDataNodeFromData, addItemFromStruct
*/
bool addItemFromData( ProbingHashType *hashTable, const char *stateName,
double avgTemp, double lowTemp, double highTemp )
{
// return false (temp)
return false;
}
/*
Name: addItemFromStruct
Process: adds item to hash table using struct input,
uses probing as specified in hashTable data,
either LINEAR_PROBING or QUADRATIC_PROBING,
may probe as many as tableSize times
Function input/parameters: hash data (ProbingHashType *),
new item (StateDataType)
Function output/parameters: updated hash table data (ProbingHashType *)
Function output/returned: result of operation (bool)
Device input/---: none
Device output/monitor: probing process displayed
Dependencies: getHashIndex, privateToPower, printf, stateDataToString,
setDataNodeFromStruct
*/
bool addItemFromStruct( ProbingHashType *hashTable, StateDataType newItem )
{
// return false (temp)
return false;
}
/*
Name: clearHashTable
Process: clear hash table array, deallocates hash struct
Function input/parameters: hash data (ProbingHashType *)
Function output/parameters: updated hash table data (ProbingHashType *)
Function output/returned: none
Device input/---: none
Device output/---: none
Dependencies: free
*/
void clearHashTable( ProbingHashType *hashTable )
{
// free the internal array
free(hashTable->array);
// free the rest of the structure
free(hashTable);
// no return, void.
}
/*
Name: displayHashTable
Process: array data dump of numbered data to screen,
shows data where used, shows "Unused" where not
Function input/parameters: provided hashTable data (const ProbingHashType *)
Function output/parameters: none
Function output/returned: none
Device input/---: none
Device output/monitor: displayed as specified
Dependencies: printf, stateDataToString
*/
void displayHashTable( const ProbingHashType *hashTable )
{
int dispIterator;
char dispState[MAX_STR_LEN];
// print out the 'title card' for the display
printf("\nHASH TABLE ARRAY DUMP:\n");
// iterate through the array
for(dispIterator = 0; dispIterator < hashTable->tableSize; dispIterator++)
{
if(hashTable->array[dispIterator].inUse)
{
// print the state data using stateDataToString
stateDataToString(dispState, hashTable->array[dispIterator]);
printf("%2d) %s\n", dispIterator + 1, dispState);
}
else
{
// print "Unused" in the table
printf("%2d) Unused\n", dispIterator + 1);
}
}
// no return, void.
}
/*
Name: findItem
Process: finds item in hash table, returns
Function input/parameters: provided search data (const StateDataType),
hashTable (const ProbingHashType *)
Function output/parameters: none
Function output/returned: state data set to struct (StateDataType),
or search parameter set to empty node if not found
Device input/---: none
Device output/---: none
Dependencies: findItemIndex, setEmptyDataNode
*/
StateDataType findItem( const ProbingHashType *hashTable,
StateDataType searchItem )
{
// return NULL (temp)
return;
}
/*
Name: findItemIndex
Process: finds item index, using probing strategy provided in hashTable data,
otherwise,
returns ITEM_NOT_FOUND if search fails after tableSize attempts,
displays index probing attempts
Function input/parameters: provided search data (const StateDataType),
hashTable (const ProbingHashType *)
Function output/parameters: none
Function output/returned: index or ITEM_NOT_FOUND (int) as specified
Device input/---: none
Device output/monitor: displays probing action, provided in sample run file
Dependencies: getHashIndex, printf, privateCompareStrings, privateToPower,
setDataNodeFromStruct
*/
int findItemIndex( const ProbingHashType *hashTable, StateDataType searchItem )
{
// return zero (temp)
return 0;
}
/*
Name: getHashIndex
Process: finds hashed index for given data item,
sums integer values of state name characters,
if state name length is less than MINIMUM_HASH_LETTER_COUNT,
repeats going over the state letters as needed to meet this minimum
Function input/parameters: provided search data (const StateDataType),
hashTable (const ProbingHashType)
Function output/parameters: none
Function output/returned: array index generated from state name (int)
Device input/---: none
Device output/---: none
Dependencies: privateGetStringLength
*/
int getHashIndex( const ProbingHashType hashTable, const StateDataType state )
{
// return zero (temp)
return 0;
}
/*
Name: initializeHashTable
Process: creates dynamically allocated hashTable,
creates dynamically allocated array of empty StateDataType items,
initializes tableSize to given capacity,
initializes probe strategy to given strategy
Function input/parameters: provided capacity (int),
provided probe strategy (ProbeType)
Function output/parameters: none
Function output/returned: pointer to created hashTable (ProbingHashType *)
Device input/---: none
Device output/---: none
Dependencies: malloc, setEmptyDataNode,
*/
ProbingHashType *initializeHashTable( int capacity, ProbeType probe )
{
int initIterator;
// use malloc to initialize the table
ProbingHashType *createdHashTable =
(ProbingHashType *) malloc(sizeof(ProbingHashType));
// use malloc to create the array
createdHashTable->array =
(StateDataType *) malloc(sizeof(StateDataType) * capacity);
// iterate through the entire array, set to empty data
for(initIterator = 0; initIterator < capacity; initIterator++)
{
// set the array's items to empty.
setEmptyDataNode(&createdHashTable->array[initIterator]);
}
// define the size of the table to capacity.
createdHashTable->tableSize = capacity;
// define the probing strategy given.
createdHashTable->probeStrategy = probe;
// return the created hash table
return createdHashTable;
}
/*
Name: removeState
Process: finds item in hash table, removes,
sets array location to unused (but does not set any other data),
returns removed state
Function input/parameters: provided search data (const StateDataType),
hashTable (const ProbingHashType)
Function output/parameters: returned state (StateDataType *)
Function output/returned: Boolean result of action (bool)
Device input/---: none
Device output/---: none
Dependencies: findItemIndex, setDataNodeFromStruct
*/
bool removeState( StateDataType *removedState,
const StateDataType toBeRemoved, const ProbingHashType hashTable )
{
// return false (temp)
return false;
}
/*
Name: showHashTableStatus
Process: displays array <D>ata values and <U>unused values
as a string of letters,
displays number of items in hashTable,
displays minimum number of contiguous data items,
displays maximum number of contiguous data items,
displays number of empty/unused array elements
Function input/parameters: hashTable (const ProbingHashType)
Function output/parameters: none
Function output/returned: none
Device input/---: none
Device output/monitor: hash data status displayed as specified
Dependencies: sprintf, printf
*/
void showHashTableStatus( const ProbingHashType hashTable)
{
int itemCount = 0, statusIterator, contigCount, maxContig, minContig;
// display the hash table status, then iterate through either used/unused
printf("\nHash Table Status: ");
for(statusIterator = 0; statusIterator < hashTable.tableSize; statusIterator++)
{
contigCount = 1;
// check to see if inUse
if(hashTable.array[statusIterator].inUse)
{
// print 'D'
printf("D");
itemCount = itemCount + 1;
contigCount = contigCount + 1;
// check to see if the contigCount is bigger than old maxContig
if (contigCount > maxContig)
{
maxContig = contigCount;
}
}
else
{
// check to see if the contigCount is less than old minContig
if(contigCount < minContig)
{
minContig = contigCount;
}
contigCount = 1;
// print 'U'
printf("U");
}
}
printf("\nD => Data, U => Unused\n\n");
printf(" Items in Hash Table: %d\n", itemCount);
printf(" Minimum contiguous bins: %d\n", itemCount);
printf(" Maximum contiguous bins: %d\n", itemCount);
printf(" Number of empty bins: %d\n", itemCount);
// no return, void.
}