Work on Vector RD

+ Finish const, destr, and op=
+ Improve logic to match changes to data members
+ Update doxygen to reflect changes made to Vector class
This commit is contained in:
Shaun Reed 2020-07-03 21:30:44 -04:00
parent d2b0281fa2
commit b258c14022
3 changed files with 57 additions and 30 deletions

View File

@ -1,5 +1,17 @@
###############################################################################
## Author: Shaun Reed ##
## Legal: All Content (c) 2020 Shaun Reed, all rights reserved ##
## About: A basic CMakeLists configuration to test Vector implementation ##
## ##
## Contact: shaunrd0@gmail.com | URL: www.shaunreed.com | GitHub: shaunrd0 ##
##############################################################################
## vector.cpp
#
cmake_minimum_required(VERSION 3.2) cmake_minimum_required(VERSION 3.2)
project(VectorDriver)
# Define the project name
project(VectorDriver)
# Define source files
set(SRC driver.cpp vector.cpp) set(SRC driver.cpp vector.cpp)
# Build an executable
add_executable(VectorDriver ${SRC}) add_executable(VectorDriver ${SRC})

View File

@ -58,6 +58,8 @@ int main()
testList.makeEmpty(); testList.makeEmpty();
break; break;
// If this test is successful, we build a default empty Vector object
// Will 'have nothing to print' because a default Vector contains no values
case CONSTRUCT: case CONSTRUCT:
{ {
Vector constrTest; Vector constrTest;
@ -67,6 +69,8 @@ int main()
break; break;
} }
// If this is successful, we build a new Vector based on the existing Vector
// The new Vector output here should be identical to this session's Vector
case COPY: case COPY:
{ {
Vector copyTest(testList); Vector copyTest(testList);
@ -76,6 +80,7 @@ int main()
break; break;
} }
// Test assignment operator, setting new Vector object equal to the existing
case ASSIGN: case ASSIGN:
{ {
Vector assignTest; Vector assignTest;
@ -86,12 +91,14 @@ int main()
break; break;
} }
// Arguably redundant, since this is only another way to call to makeEmpty()
case DESTRUCT: case DESTRUCT:
{ {
Vector destrTest(testList); Vector destrTest(testList);
std::cout << "Current destrTest Vector contents...\n";
destrTest.print();
std::cout << "Deleting local destrTest Vector...\n"; std::cout << "Deleting local destrTest Vector...\n";
// destrTest.~Vector(); is called at the end of this case {} scope destrTest.~Vector(); // Implicitly called at the end of this scope {}
destrTest.print();
break; break;
} }

View File

@ -24,9 +24,10 @@
*/ */
Vector::Vector(const Vector& rhs) Vector::Vector(const Vector& rhs)
{ {
if (rhs.getSize() >= 0) { if (rhs.getIndex() >= 0) {
curIndex = rhs.getSize(); curIndex = rhs.getIndex();
maxSize = rhs.getMax(); // Avoid copying over unused indices from parent Vector
maxSize = rhs.getSize();
data = new int[curIndex]; data = new int[curIndex];
for (int i = 0; i <= rhs.getSize(); i++) { for (int i = 0; i <= rhs.getSize(); i++) {
data[i] = rhs.getValue(i); data[i] = rhs.getValue(i);
@ -44,7 +45,7 @@ Vector::Vector(const Vector& rhs)
* Pass the rhs by value to create local copy, swap its contents * Pass the rhs by value to create local copy, swap its contents
* Destructor called on previous Vector data at the end of this scope * Destructor called on previous Vector data at the end of this scope
* *
* @param rhs Vector object passed by valuIt would be easier to emulate the android app on a PCe * @param rhs Vector object passed by value, creating a local variable
* @return Vector A deep copy of the rhs Vector object * @return Vector A deep copy of the rhs Vector object
*/ */
Vector Vector::operator=(Vector rhs) Vector Vector::operator=(Vector rhs)
@ -52,6 +53,10 @@ Vector Vector::operator=(Vector rhs)
if (this == &rhs) return *this; if (this == &rhs) return *this;
// Swap the pointers, moving the previous head data to the local variable rhs // Swap the pointers, moving the previous head data to the local variable rhs
std::swap(data, rhs.data); std::swap(data, rhs.data);
curIndex = rhs.getIndex();
// Use the current size of the vector we are equal to
// Avoids copying over unused indices
maxSize = rhs.getSize();
return *this; return *this;
} }
@ -60,7 +65,7 @@ Vector Vector::operator=(Vector rhs)
*/ */
Vector::~Vector() Vector::~Vector()
{ {
if (getSize() + 1 >= 1) makeEmpty(); if (!isEmpty()) makeEmpty();
} }
@ -71,7 +76,7 @@ Vector::~Vector()
/** push /** push
* @brief Push a value to the end of our Vector * @brief Push a value to the end of our Vector
* *
* @param x The value to be inserted * @param val The value to be inserted
*/ */
bool Vector::push(int val) bool Vector::push(int val)
{ {
@ -83,7 +88,8 @@ bool Vector::push(int val)
} }
/** pop /** pop
* @brief returns the value at the Vector::data[index -1] if it exists * @brief returns the value at the Vector::data[curIndex] if it exists
* Once returned, the curIndex is decremented via data[curIndex--]
* If the vector is empty, returns INT32_MIN * If the vector is empty, returns INT32_MIN
* *
* @return int The value held at the Node pointed to by Vector::data[index -1] * @return int The value held at the Node pointed to by Vector::data[index -1]
@ -92,7 +98,6 @@ int Vector::pop()
{ {
int val = INT32_MIN; int val = INT32_MIN;
if (!isEmpty()) { if (!isEmpty()) {
// (!isEmpty()) ? data[index - 1] : INT32_MIN;
val = pop(data); val = pop(data);
std::cout << "[" << val << "] has been popped from our Vector\n"; std::cout << "[" << val << "] has been popped from our Vector\n";
} }
@ -106,7 +111,7 @@ int Vector::pop()
void Vector::makeEmpty() void Vector::makeEmpty()
{ {
if (isEmpty()) { if (isEmpty()) {
std::cout << "Our vector is empty...\n"; std::cout << "Cannot makeEmpty, our Vector is already empty...\n";
return; return;
} }
else makeEmpty(data); else makeEmpty(data);
@ -122,7 +127,6 @@ int Vector::peek() const
{ {
int val = INT32_MIN; int val = INT32_MIN;
if (!isEmpty()) { if (!isEmpty()) {
// (!isEmpty()) ? data[index - 1] : INT32_MIN;
val = peek(data); val = peek(data);
std::cout << "[" << peek(data) << "] is at the end of our vector\n"; std::cout << "[" << peek(data) << "] is at the end of our vector\n";
} }
@ -130,6 +134,8 @@ int Vector::peek() const
return val; return val;
} }
// TODO: Verify that isEmpty works by actually checking data == NULL
/** isEmpty /** isEmpty
* @brief Determine if the Vector is empty * @brief Determine if the Vector is empty
* *
@ -163,7 +169,8 @@ void Vector::print() const
} }
/** getMax /** getMax
* @brief Returns the maximum size of the vector * @brief Returns the literal maximum size of the vector
* Not offset to match any index - Vector with max size 3 has indices 0-2
* *
* @return int at this->maxSize * @return int at this->maxSize
*/ */
@ -174,6 +181,7 @@ int Vector::getMax() const
/** getSize /** getSize
* @brief Returns the current size of the vector * @brief Returns the current size of the vector
* AKA the current number of indices being used, NOT the max indices
* *
* @return int at this->curIndex + 1 * @return int at this->curIndex + 1
*/ */
@ -184,6 +192,7 @@ int Vector::getSize() const
/** getIndex /** getIndex
* @brief Returns the current index of the vector * @brief Returns the current index of the vector
* AKA the last index the vector wrote to
* *
* @return int at this->curIndex * @return int at this->curIndex
*/ */
@ -197,8 +206,8 @@ int Vector::getIndex() const
/** getValue /** getValue
* @brief Get the value stored at a given index within the vector * @brief Get the value stored at a given index within the vector
* *
* @param index * @param index The index containing the value to be returned
* @return int * @return int The value held at the index given
*/ */
int Vector::getValue(int index) const int Vector::getValue(int index) const
{ {
@ -213,7 +222,8 @@ int Vector::getValue(int index) const
* @brief Private member to handle inserting value into the vector * @brief Private member to handle inserting value into the vector
* *
* @param val Value to be inserted * @param val Value to be inserted
* @param head The head of the vector to push the value into * @param data The data of the vector to push the value into
*
* @return true If the value was inserted * @return true If the value was inserted
* @return false If the value could not be inserted * @return false If the value could not be inserted
*/ */
@ -227,7 +237,6 @@ bool Vector::push(int val, int *&data)
for (int i = 0; i <= curIndex; i++) { for (int i = 0; i <= curIndex; i++) {
temp[i] = data[i]; temp[i] = data[i];
} }
// data = temp;
std::swap(data, temp); std::swap(data, temp);
} }
curIndex += 1; curIndex += 1;
@ -236,10 +245,11 @@ bool Vector::push(int val, int *&data)
} }
/** pop /** pop
* @brief * @brief Returns the value held at the last index within the Vector
* Decrements the curIndex after storing the value to be returned
* *
* @param data * @param data The Vector data to modify
* @return int * @return int The value stored at the index removed from the end of the Vector
*/ */
int Vector::pop(int *&data) int Vector::pop(int *&data)
{ {
@ -249,8 +259,7 @@ int Vector::pop(int *&data)
} }
/** makeEmpty /** makeEmpty
* @brief Private member to empty this Vector object, deleting all associated Nodes * @brief Private member to empty Vector object, deleting all associated data
* Does not print any output. Avoids destructors printing to cout
* *
* @param head The head of the stack to be deleted * @param head The head of the stack to be deleted
* *
@ -264,10 +273,10 @@ void Vector::makeEmpty(int *&data)
} }
/** peek /** peek
* @brief * @brief Private member to display the value at the end of our Vector
* *
* @param data * @param data The Vector data peek
* @return int * @return int The value stored at the end of the Vector
*/ */
int Vector::peek(int *data) const int Vector::peek(int *data) const
{ {
@ -280,7 +289,6 @@ int Vector::peek(int *data) const
*/ */
void Vector::print(int *data) const void Vector::print(int *data) const
{ {
// int *temp = data;
std::cout << "Vector Contents: "; std::cout << "Vector Contents: ";
for (int i = 0; i <= curIndex; i++) { for (int i = 0; i <= curIndex; i++) {
std::cout << data[i] << " | "; std::cout << data[i] << " | ";