From c31a7a6571001774996af8699a7a4bbdabcf9bd7 Mon Sep 17 00:00:00 2001 From: Shaun Reed Date: Fri, 24 Jul 2020 16:55:36 -0400 Subject: [PATCH] Add RD for vector class using templates --- cpp/datastructs/templates/vector/driver.cpp | 19 ++++++------ cpp/datastructs/templates/vector/vector.cpp | 32 +++++++++++---------- cpp/datastructs/templates/vector/vector.h | 16 +++++------ 3 files changed, 36 insertions(+), 31 deletions(-) diff --git a/cpp/datastructs/templates/vector/driver.cpp b/cpp/datastructs/templates/vector/driver.cpp index 332ae84..66dbc69 100644 --- a/cpp/datastructs/templates/vector/driver.cpp +++ b/cpp/datastructs/templates/vector/driver.cpp @@ -1,7 +1,7 @@ /*############################################################################# ## Author: Shaun Reed ## ## Legal: All Content (c) 2020 Shaun Reed, all rights reserved ## -## About: A driver program to test a vector implementation ## +## About: A driver program to test a vector implementation using templates ## ## ## ## Contact: shaunrd0@gmail.com | URL: www.shaunreed.com | GitHub: shaunrd0 ## ############################################################################## @@ -11,6 +11,9 @@ #include "vector.h" #include +// Input the type we want to use within our vector here +#define TYPE std::string + enum OPS { EXIT, PUSH, POP, TOP, PRINT, EMPTY, CONSTRUCT, COPY, ASSIGN, DESTRUCT }; @@ -18,10 +21,10 @@ enum OPS { int main() { std::cout << "Driver: \n"; - Vector testlist; + Vector testList; bool exit = false; int choice = -1; - T val; + TYPE val; while (!exit) { @@ -39,7 +42,7 @@ int main() std::cout << "enter a value to push to our vector: "; std::cin >> val; std::cin.clear(); - testlist.push(val); + testList.push(val); break; case POP: @@ -62,7 +65,7 @@ int main() // Will 'have nothing to print' because a default Vector contains no values case CONSTRUCT: { - Vector constrTest; + Vector constrTest; std::cout << "##### Constructor Test #####\n"; constrTest.print(); std::cout << "Deleting local constrTest Vector...\n"; @@ -73,7 +76,7 @@ int main() // The new Vector output here should be identical to this session's Vector case COPY: { - Vector copyTest(testList); + Vector copyTest(testList); std::cout << "##### Copy Constructor Test #####\n"; copyTest.print(); std::cout << "Deleting local copyTest Vector...\n"; @@ -83,7 +86,7 @@ int main() // Test assignment operator, setting new Vector object equal to the existing case ASSIGN: { - Vector assignTest; + Vector assignTest; assignTest = testList; std::cout << "##### Assignment Test #####\n"; assignTest.print(); @@ -93,7 +96,7 @@ int main() case DESTRUCT: { - Vector destrTest(testList); + Vector destrTest(testList); std::cout << "Current destrTest Vector contents...\n"; destrTest.print(); std::cout << "Deleting local destrTest Vector...\n"; diff --git a/cpp/datastructs/templates/vector/vector.cpp b/cpp/datastructs/templates/vector/vector.cpp index f2fa8ad..4024506 100644 --- a/cpp/datastructs/templates/vector/vector.cpp +++ b/cpp/datastructs/templates/vector/vector.cpp @@ -1,7 +1,7 @@ /*############################################################################# ## Author: Shaun Reed ## ## Legal: All Content (c) 2020 Shaun Reed, all rights reserved ## -## About: An example of a vector implementation ## +## About: An example of a vector implementation using templates ## ## ## ## Contact: shaunrd0@gmail.com | URL: www.shaunreed.com | GitHub: shaunrd0 ## ############################################################################## @@ -19,7 +19,7 @@ * @brief Construct a new Vector::Vector object from an existing one * Creates a new deep copy of a given Vector * - * @param rhs Vector object + * @param rhs Vector object */ template Vector::Vector(const Vector& rhs) @@ -28,8 +28,8 @@ Vector::Vector(const Vector& rhs) curIndex = rhs.getIndex(); // Avoid copying over unused indices from parent Vector maxSize = rhs.getSize(); - data = new T[curIndex]; - for (int i = 0; i <= rhs.getSize(); i++) { + data = new T[curIndex+1]; + for (int i = 0; i <= rhs.getIndex(); i++) { data[i] = rhs.getValue(i); } } @@ -46,7 +46,7 @@ Vector::Vector(const Vector& rhs) * Destructor called on previous Vector data at the end of this scope * * @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 */ template Vector Vector::operator=(Vector rhs) @@ -95,7 +95,7 @@ bool Vector::push(T val) * Once returned, the curIndex is decremented via data[curIndex--] * If the vector is empty, returns INT32_MIN * - * @return int The value held at the Node pointed to by Vector::data[index] + * @return T The value held at the Node pointed to by Vector::data[index] */ template T Vector::pop() @@ -126,12 +126,12 @@ void Vector::makeEmpty() * @brief returns the value at the end of the vector * If the vector is empty, returns INT32_MIN * - * @return int The value held at the current data[index] of the vector + * @return T The value held at the current data[index] of the vector */ template T Vector::peek() const { - T val = NULL; + T val; if (!isEmpty()) { val = peek(data); std::cout << "[" << peek(data) << "] is at the end of our vector\n"; @@ -182,7 +182,7 @@ void Vector::print() const * @return int at this->maxSize */ template -T Vector::getMax() const +int Vector::getMax() const { return maxSize; } @@ -194,7 +194,7 @@ T Vector::getMax() const * @return int at this->curIndex + 1 */ template -T Vector::getSize() const +int Vector::getSize() const { return curIndex + 1; } @@ -206,7 +206,7 @@ T Vector::getSize() const * @return int at this->curIndex */ template -T Vector::getIndex() const +int Vector::getIndex() const { return curIndex; } @@ -217,10 +217,10 @@ T Vector::getIndex() const * @brief Get the value stored at a given index within the vector * * @param index The index containing the value to be returned - * @return int The value held at the index given + * @return T The value held at the index given */ template -T Vector::getValue(T index) const +T Vector::getValue(int index) const { return data[index]; } @@ -262,7 +262,7 @@ bool Vector::push(T val, T *&data) * Decrements the curIndex after storing the value to be returned * * @param data The Vector data to modify - * @return int The value stored at the index removed from the end of the Vector + * @return T The value stored at the index removed from the end of the Vector */ template T Vector::pop(T *&data) @@ -291,7 +291,7 @@ void Vector::makeEmpty(T *&data) * @brief Private member to display the value at the end of our Vector * * @param data The Vector data to peek - * @return int The value stored at the end of the Vector + * @return T The value stored at the end of the Vector */ template T Vector::peek(T *data) const @@ -315,5 +315,7 @@ void Vector::print(T *data) const std::cout << std::endl; } +// Instantiate relevant type templates for this class template class Vector; +template class Vector; template class Vector; diff --git a/cpp/datastructs/templates/vector/vector.h b/cpp/datastructs/templates/vector/vector.h index ac9bf0b..9380b1c 100644 --- a/cpp/datastructs/templates/vector/vector.h +++ b/cpp/datastructs/templates/vector/vector.h @@ -1,7 +1,7 @@ /*############################################################################# ## Author: Shaun Reed ## ## Legal: All Content (c) 2020 Shaun Reed, all rights reserved ## -## About: An example of a vector implementation ## +## About: An example of a vector implementation using templates ## ## ## ## Contact: shaunrd0@gmail.com | URL: www.shaunreed.com | GitHub: shaunrd0 ## ############################################################################## @@ -28,21 +28,21 @@ class Vector { bool isEmpty() const; bool isFull() const; void print() const; - T getMax() const; - T getSize() const; - T getIndex() const; - T getValue(T index) const; + int getMax() const; + int getSize() const; + int getIndex() const; + T getValue(int index) const; private: - T maxSize; - T curIndex; + int maxSize; + int curIndex; T *data; bool push(T val, T *&data); T pop(T *&data); void makeEmpty(T *&data); T peek(T *data) const; void print(T *data) const; - }; + #endif