223 lines
7.5 KiB
C
223 lines
7.5 KiB
C
#ifndef ARRAY_DATA_ENGINE_H
|
|
#define ARRAY_DATA_ENGINE_H
|
|
|
|
// header files
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <stdbool.h>
|
|
#include <string.h>
|
|
#include "StandardConstants.h"
|
|
|
|
// data structures
|
|
typedef struct StateDataStruct
|
|
{
|
|
char name[ STD_STR_LEN ];
|
|
|
|
int population;
|
|
|
|
double growth;
|
|
|
|
char majority;
|
|
} StateDataType;
|
|
|
|
typedef struct StateArrayStruct
|
|
{
|
|
StateDataType *array;
|
|
|
|
int size, capacity;
|
|
} StateArrayType;
|
|
|
|
typedef enum { NOT_FOUND = -1, FOUND } FoundType;
|
|
|
|
// Function prototypes
|
|
|
|
/*
|
|
Name: clearDataStructure
|
|
Process: deallocates memory for the array data structure
|
|
and encapsulated state data,
|
|
returns NULL to calling function
|
|
Function input/parameters: pointer to state data array (StateDataArray *)
|
|
Function output/parameters: none
|
|
Function output/returned: NULL
|
|
Device input/---: none
|
|
Device output/---: none
|
|
Dependencies: free
|
|
*/
|
|
StateArrayType *clearDataStructure( StateArrayType *arrayPtr );
|
|
|
|
/*
|
|
Name: copyDataList
|
|
Process: copies elements from one state data array to another
|
|
resets array size as needed,
|
|
also resets array capacity as needed if destination array type
|
|
capacity is less than capacity of source array type
|
|
Function input/parameters: source state data array type (StateArrayType *)
|
|
Function output/parameters: pointer to destination state data array type
|
|
(StateArrayType *)
|
|
Function output/returned: none
|
|
Device input/---: none
|
|
Device output/---: none
|
|
Dependencies: privateCheckForResize, deepCopyData
|
|
*/
|
|
void copyDataList( StateArrayType *dest, const StateArrayType src );
|
|
|
|
/*
|
|
Name: createDataStructure
|
|
Process: dynamically creates instance of state array type
|
|
along with dynamically allocating the state data array within it
|
|
Function input/parameters: given initial capacity (int)
|
|
Function output/parameters: none
|
|
Function output/returned: pointer to state array type (StateArrayType *)
|
|
Device input/---: none
|
|
Device output/---: none
|
|
Dependencies: malloc, sizeof (C keyword)
|
|
*/
|
|
StateArrayType *createDataStructure( int initialCapacity );
|
|
|
|
/*
|
|
Name: deepCopyData
|
|
Process: copies all members of a StateDataType item into another one
|
|
Function input/parameters: state data type item (const StateDataType )
|
|
Function output/parameters: pointer to state data item (StateDataType *)
|
|
Function output/returned: none
|
|
Device input/---: none
|
|
Device output/---: none
|
|
Dependencies: privateCopyString
|
|
*/
|
|
void deepCopyData( StateDataType *dest, const StateDataType src );
|
|
|
|
/*
|
|
Name: displayData
|
|
Process: displays comma-delimited data items in array if array is not empty,
|
|
otherwise displays "Data not found - Display aborted",
|
|
allows for subtitle to be added to "Display Data - "
|
|
Function input/parameters: state array type (const StateArrayType),
|
|
subtitle (const char *)
|
|
Function output/parameters: none
|
|
Function output/returned: none
|
|
Device input/---: none
|
|
Device output/monitor: data displayed as specified
|
|
Dependencies: printf, listIsEmpty
|
|
*/
|
|
void displayData( const StateArrayType stateData, const char *subTitle );
|
|
|
|
/*
|
|
Name: getItemAtIndex
|
|
Process: checks for index within bounds,
|
|
acquires and returns the value as a parameter,
|
|
and returns true,
|
|
otherwise sets data in parameter to empty and returns false
|
|
Function input/parameters: state array type (const StateArrayType),
|
|
list index (int)
|
|
Function output/parameters: value if found,
|
|
otherwise, value set to empty (StateDataType *)
|
|
Function output/returned: Boolean result of operation (bool)
|
|
Device input/---: none
|
|
Device output/---: none
|
|
Dependencies: deepCopyData, setItemToEmpty
|
|
*/
|
|
bool getItemAtIndex( StateDataType *returnVal,
|
|
const StateArrayType stateData, int listIndex );
|
|
|
|
/*
|
|
Name: getListSize
|
|
Process: provides the size of the list
|
|
Function input/parameters: state array type (const StateArrayType)
|
|
Function output/parameters: none
|
|
Function output/returned: size of the list (int)
|
|
Device input/---: none
|
|
Device output/---: none
|
|
Dependencies: none
|
|
*/
|
|
int getListSize( const StateArrayType stateData );
|
|
|
|
/*
|
|
Name: insertItemAtIndex
|
|
Process: checks for index in bounds, inserts item at index, returns true,
|
|
resizes array as needed,
|
|
if index not in bounds, returns false
|
|
Function input/parameters: pointer to state array type (StateArrayType *),
|
|
StateDataType item to insert (const StateDataType),
|
|
index for insertion (int)
|
|
Function output/parameters: updated pointer to state array type
|
|
(StateArrayType *)
|
|
Function output/returned: Boolean result of specified process (bool)
|
|
Device input/---: none
|
|
Device output/---: none
|
|
Dependencies: checkForResize
|
|
*/
|
|
bool insertItemAtIndex( StateArrayType *stateData,
|
|
StateDataType toBeInserted, int insertIndex );
|
|
|
|
/*
|
|
Name: listIsEmpty
|
|
Process: returns true if list is empty, false otherwise
|
|
Function input/parameters: state array type (const StateArrayType)
|
|
Function output/parameters: none
|
|
Function output/returned: Boolean result of test as specified (bool)
|
|
Device input/---: none
|
|
Device output/---: none
|
|
Dependencies: none
|
|
*/
|
|
bool listIsEmpty( const StateArrayType array );
|
|
|
|
/*
|
|
Name: privateCheckForResize
|
|
Process: checks if array needs to be resized,
|
|
resizes (increases capacity of) array to double current capacity
|
|
Function input/parameters: pointer to state array type (StateArrayType *)
|
|
Function output/parameters: updated pointer
|
|
to state array type (StateArrayType *)
|
|
if resize has taken place,
|
|
otherwise array type is unchanged
|
|
Function output/returned: none
|
|
Device input/---: none
|
|
Device output/---: none
|
|
Dependencies: malloc, sizeof (C keyword), deepCopyData, free
|
|
*/
|
|
void privateCheckForResize( StateArrayType *stateData );
|
|
|
|
/*
|
|
Name: privateCopyString
|
|
Process: copies string character for character up to NULL_CHAR of source string
|
|
Function input/parameters: source string to be copied (const char *)
|
|
Function output/parameters: destination string (char *)
|
|
Function output/returned: none
|
|
Device input/---: none
|
|
Device output/---: none
|
|
Dependencies: none
|
|
*/
|
|
void privateCopyString( char *dest, const char *src );
|
|
|
|
/*
|
|
Name: removeItemAtIndex
|
|
Process: removes from array at index, shifts data down as needed,
|
|
returns true if found, false otherwise
|
|
Function input/parameters: pointer to state array type (StateArrayType *),
|
|
index to remove (int)
|
|
Function output/parameters: pointer to updated state array type
|
|
(StateArrayType *),
|
|
pointer to removed item (StateDataType *)
|
|
Function output/returned: Boolean result of removal operation (bool)
|
|
Device input/---: none
|
|
Device output/---: none
|
|
Dependencies: none
|
|
*/
|
|
bool removeItemAtIndex( StateArrayType *stateData,
|
|
StateDataType *removedItem, int removeIndex );
|
|
|
|
/*
|
|
Name: setItemToEmpty
|
|
Process: sets state data type struct to empty or default values,
|
|
including empty string and majority 'x'
|
|
Function input/parameters: pointer to state data value (StateDataType *)
|
|
Function output/parameters: pointer to updated state data value
|
|
(StateDataType *)
|
|
Function output/returned: none
|
|
Device input/---: none
|
|
Device output/---: none
|
|
Dependencies: none
|
|
*/
|
|
void setItemToEmpty( StateDataType *data );
|
|
|
|
#endif // ARRAY_DATA_ENGINE_H
|