Add RD for vector class using templates

This commit is contained in:
Shaun Reed 2020-07-24 16:55:36 -04:00
parent 2c6400cc87
commit c31a7a6571
3 changed files with 36 additions and 31 deletions

View File

@ -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 <iostream>
// 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<std::string> testlist;
Vector<TYPE> testList;
bool exit = false;
int choice = -1;
<typename T> 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<std::string> constrTest;
Vector<TYPE> 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<std::string> copyTest(testList);
Vector<TYPE> 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<std::string> assignTest;
Vector<TYPE> assignTest;
assignTest = testList;
std::cout << "##### Assignment Test #####\n";
assignTest.print();
@ -93,7 +96,7 @@ int main()
case DESTRUCT:
{
Vector<std::string> destrTest(testList);
Vector<TYPE> destrTest(testList);
std::cout << "Current destrTest Vector contents...\n";
destrTest.print();
std::cout << "Deleting local destrTest Vector...\n";

View File

@ -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<T> object
*/
template<typename T>
Vector<T>::Vector(const Vector<T>& rhs)
@ -28,8 +28,8 @@ Vector<T>::Vector(const Vector<T>& 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<T>::Vector(const Vector<T>& 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<T> A deep copy of the rhs Vector object
*/
template<typename T>
Vector<T> Vector<T>::operator=(Vector<T> rhs)
@ -95,7 +95,7 @@ bool Vector<T>::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<typename T>
T Vector<T>::pop()
@ -126,12 +126,12 @@ void Vector<T>::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<typename T>
T Vector<T>::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<T>::print() const
* @return int at this->maxSize
*/
template<typename T>
T Vector<T>::getMax() const
int Vector<T>::getMax() const
{
return maxSize;
}
@ -194,7 +194,7 @@ T Vector<T>::getMax() const
* @return int at this->curIndex + 1
*/
template<typename T>
T Vector<T>::getSize() const
int Vector<T>::getSize() const
{
return curIndex + 1;
}
@ -206,7 +206,7 @@ T Vector<T>::getSize() const
* @return int at this->curIndex
*/
template<typename T>
T Vector<T>::getIndex() const
int Vector<T>::getIndex() const
{
return curIndex;
}
@ -217,10 +217,10 @@ T Vector<T>::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<typename T>
T Vector<T>::getValue(T index) const
T Vector<T>::getValue(int index) const
{
return data[index];
}
@ -262,7 +262,7 @@ bool Vector<T>::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<typename T>
T Vector<T>::pop(T *&data)
@ -291,7 +291,7 @@ void Vector<T>::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<typename T>
T Vector<T>::peek(T *data) const
@ -315,5 +315,7 @@ void Vector<T>::print(T *data) const
std::cout << std::endl;
}
// Instantiate relevant type templates for this class
template class Vector<int>;
template class Vector<float>;
template class Vector<std::string>;

View File

@ -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