186 lines
6.8 KiB
C
186 lines
6.8 KiB
C
#ifndef HASH_UTILITIES_H
|
|
#define HASH_UTILITIES_H
|
|
|
|
// header files
|
|
#include "StandardConstants.h"
|
|
#include "GeneralUtility.h"
|
|
#include "StateDataUtility.h"
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
|
|
// constants
|
|
typedef enum { NO_PROBING, LINEAR_PROBING = 101,
|
|
QUADRATIC_PROBING = 202 } ProbeType;
|
|
extern const int MINIMUM_HASH_LETTER_COUNT;
|
|
extern const int ITEM_NOT_FOUND;
|
|
extern const bool USED_NODE;
|
|
extern const bool UNUSED_NODE;
|
|
|
|
typedef struct HashStruct
|
|
{
|
|
StateDataType *array;
|
|
|
|
int tableSize;
|
|
|
|
ProbeType probeStrategy;
|
|
} ProbingHashType;
|
|
|
|
// prototypes
|
|
|
|
/*
|
|
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 );
|
|
|
|
/*
|
|
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 );
|
|
|
|
/*
|
|
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 );
|
|
|
|
/*
|
|
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 );
|
|
|
|
/*
|
|
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 );
|
|
|
|
/*
|
|
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 );
|
|
|
|
/*
|
|
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 );
|
|
|
|
/*
|
|
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 );
|
|
|
|
/*
|
|
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 );
|
|
|
|
/*
|
|
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);
|
|
|
|
|
|
#endif // HASH_UTILITIES_H
|