CS249-Projects/PA06-1018/IteratorUtility.c
cdez b7cb3dc9e0 Got close to finishing
++ IteratorUtility.c
++ LLDataEngine.c
++ QueueUtility.c
++ StackUtility.c
+- PA06_Main.c
+- Makefile for PA06
2023-10-17 20:48:30 -07:00

213 lines
No EOL
7.4 KiB
C

// header files
#include "IteratorUtility.h"
/* Data Structures
typedef struct IteratorStruct
{
StateArrayType *iterData;
int currentIndex;
} IteratorType;
Data Structures */
// function implementation
/*
Name: clearIterator
Process: clears the StateDataType sub-structure, and then the IteratorType
Function input/parameters: pointer to IteratorType (IteratorType *)
Function output/parameters: none
Function output/returned: NULL
Device input/---: none
Device output/---: none
Dependencies: clearDataStructure, free
*/
IteratorType *clearIterator( IteratorType *iterator )
{
// clear the encapsulated data structure
clearDataStructure(iterator->iterData);
// clear the rest of the data structure
free(iterator);
// return NULL
return NULL;
}
/*
Name: createIterator
Process: initializes the IteratorType,
and then the StateDataType sub-structure
Function input/parameters: initial capacity (int)
Function output/parameters: none
Function output/returned: pointer to initialized IteratorType (IteratorType *)
Device input/---: none
Device output/---: none
Dependencies: malloc, createDataStructure
*/
IteratorType *createIterator( int initialCapacity )
{
IteratorType *iteratorDataStructure;
// create the entire data structure
iteratorDataStructure = malloc(sizeof(IteratorType));
iteratorDataStructure->currentIndex = 0;
// create the encapsulated data structure
iteratorDataStructure->iterData = createDataStructure(initialCapacity);
// return IteratorType
return iteratorDataStructure;
}
/*
Name: getFirst
Process: gets first value in iterator, returns true,
returns false otherwise
Function input/parameters: pointer to IteratorType (IteratorType *)
Function output/parameters: return value (StateDataType *)
Function output/returned: Boolean result of operation
Device input/---: none
Device output/---: none
Dependencies: getItemAtIndex
*/
bool getFirst( IteratorType *iterator, StateDataType *returned )
{
// set currentIndex to 0
iterator->currentIndex = 0;
// return function true/false if value possible
return getItemAtIndex(returned, *iterator->iterData, iterator->currentIndex);
}
/*
Name: getLast
Process: gets last value in iterator, returns true,
returns false otherwise
Function input/parameters: pointer to IteratorType (IteratorType *)
Function output/parameters: return value (StateDataType *)
Function output/returned: Boolean result of operation
Device input/---: none
Device output/---: none
Dependencies: getItemAtIndex, getListSize
*/
bool getLast( IteratorType *iterator, StateDataType *returned )
{
// set currentIndex to getListSize
iterator->currentIndex = getListSize(*iterator->iterData);
// return function to see if possible
return getItemAtIndex(returned, *iterator->iterData, iterator->currentIndex);
}
/*
Name: getNext
Process: gets next value in iterator if available, returns true,
otherwise, sets return value to empty, returns false
Function input/parameters: pointer to IteratorType (IteratorType *)
Function output/parameters: return value (StateDataType *)
Function output/returned: Boolean result of operation
Device input/---: none
Device output/---: none
Dependencies: getItemAtIndex, getListSize
*/
bool getNext( IteratorType *iterator, StateDataType *returned )
{
// check to see if the currentIndex value is not size.
if(iterator->currentIndex != getListSize(*iterator->iterData))
{
iterator->currentIndex = iterator->currentIndex + 1;
return getItemAtIndex(returned, *iterator->iterData,
iterator->currentIndex);
}
// return false
return false;
}
/*
Name: getPrevious
Process: gets previous value in iterator if available, returns true,
otherwise, sets return value to empty, returns false
Function input/parameters: pointer to IteratorType (IteratorType *)
Function output/parameters: return value (StateDataType *)
Function output/returned: Boolean result of operation
Device input/---: none
Device output/---: none
Dependencies: getItemAtIndex
*/
bool getPrevious( IteratorType *iterator, StateDataType *returned )
{
// check to see if the currentIndex value is not zero.
if(iterator->currentIndex != 0)
{
iterator->currentIndex = iterator->currentIndex - 1;
return getItemAtIndex(returned, *iterator->iterData,
iterator->currentIndex);
}
// return false
return false;
}
/*
Name: insertAfterCurrentIndex
Process: inserts value immediately after currentIndex, if possible, returns true,
otherwise, returns false
Function input/parameters: pointer to IteratorType (IteratorType *),
value to be inserted (const StateDataType)
Function output/parameters: none
Function output/returned: Boolean result of operation
Device input/---: none
Device output/---: none
Dependencies: insertItemAtIndex, getListSize
*/
bool insertAfterCurrentIndex( IteratorType *iterator,
const StateDataType toBeInserted )
{
// return function true/false based on possibility
return insertItemAtIndex(iterator->iterData, toBeInserted,
iterator->currentIndex + 1);
}
/*
Name: insertAtCurrentIndex
Process: inserts value at currentIndex, if possible, returns true
otherwise, returns false
Function input/parameters: pointer to IteratorType (IteratorType *),
value to be inserted (const StateDataType)
Function output/parameters: none
Function output/returned: Boolean result of operation
Device input/---: none
Device output/---: none
Dependencies: insertItemAtIndex
*/
bool insertAtCurrentIndex( IteratorType *iterator,
const StateDataType toBeInserted )
{
// return true/false based on if the function deems worthy
return insertItemAtIndex(iterator->iterData, toBeInserted,
iterator->currentIndex);
}
/*
Name: removeAtCurrentIndex
Process: removes value at current index if possible, returns true,
otherwise, sets return value to empty, returns false
Function input/parameters: pointer to IteratorType (IteratorType *),
value to be remove (StateDataType *)
Function output/parameters: pointer to updated IteratorType (IteratorType *),
pointer to item removed (StateDataType *)
Function output/returned: Boolean result of operation
Device input/---: none
Device output/---: none
Dependencies: removeItemAtIndex, getListSize
*/
bool removeAtCurrentIndex( IteratorType *iterator, StateDataType *removedItem )
{
// need to check if the last element is being removed
if(iterator->currentIndex == getListSize(*iterator->iterData))
{
// move the currentIndex back one,
// return removeItemAtIndex with currentIndex + 1
iterator->currentIndex = iterator->currentIndex - 1;
return removeItemAtIndex(iterator->iterData, removedItem,
iterator->currentIndex + 1);
// gives the effect like it removed,
// but doesn't screw up the rest of the program.
}
// otherwise, remove the item
return removeItemAtIndex(iterator->iterData, removedItem,
iterator->currentIndex);
}