Add RD for vector class using templates
This commit is contained in:
parent
2c6400cc87
commit
c31a7a6571
|
@ -1,7 +1,7 @@
|
||||||
/*#############################################################################
|
/*#############################################################################
|
||||||
## Author: Shaun Reed ##
|
## Author: Shaun Reed ##
|
||||||
## Legal: All Content (c) 2020 Shaun Reed, all rights reserved ##
|
## 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 ##
|
## Contact: shaunrd0@gmail.com | URL: www.shaunreed.com | GitHub: shaunrd0 ##
|
||||||
##############################################################################
|
##############################################################################
|
||||||
|
@ -11,6 +11,9 @@
|
||||||
#include "vector.h"
|
#include "vector.h"
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
|
||||||
|
// Input the type we want to use within our vector here
|
||||||
|
#define TYPE std::string
|
||||||
|
|
||||||
enum OPS {
|
enum OPS {
|
||||||
EXIT, PUSH, POP, TOP, PRINT, EMPTY, CONSTRUCT, COPY, ASSIGN, DESTRUCT
|
EXIT, PUSH, POP, TOP, PRINT, EMPTY, CONSTRUCT, COPY, ASSIGN, DESTRUCT
|
||||||
};
|
};
|
||||||
|
@ -18,10 +21,10 @@ enum OPS {
|
||||||
int main()
|
int main()
|
||||||
{
|
{
|
||||||
std::cout << "Driver: \n";
|
std::cout << "Driver: \n";
|
||||||
Vector<std::string> testlist;
|
Vector<TYPE> testList;
|
||||||
bool exit = false;
|
bool exit = false;
|
||||||
int choice = -1;
|
int choice = -1;
|
||||||
<typename T> T val;
|
TYPE val;
|
||||||
|
|
||||||
while (!exit)
|
while (!exit)
|
||||||
{
|
{
|
||||||
|
@ -39,7 +42,7 @@ int main()
|
||||||
std::cout << "enter a value to push to our vector: ";
|
std::cout << "enter a value to push to our vector: ";
|
||||||
std::cin >> val;
|
std::cin >> val;
|
||||||
std::cin.clear();
|
std::cin.clear();
|
||||||
testlist.push(val);
|
testList.push(val);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case POP:
|
case POP:
|
||||||
|
@ -62,7 +65,7 @@ int main()
|
||||||
// Will 'have nothing to print' because a default Vector contains no values
|
// Will 'have nothing to print' because a default Vector contains no values
|
||||||
case CONSTRUCT:
|
case CONSTRUCT:
|
||||||
{
|
{
|
||||||
Vector<std::string> constrTest;
|
Vector<TYPE> constrTest;
|
||||||
std::cout << "##### Constructor Test #####\n";
|
std::cout << "##### Constructor Test #####\n";
|
||||||
constrTest.print();
|
constrTest.print();
|
||||||
std::cout << "Deleting local constrTest Vector...\n";
|
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
|
// The new Vector output here should be identical to this session's Vector
|
||||||
case COPY:
|
case COPY:
|
||||||
{
|
{
|
||||||
Vector<std::string> copyTest(testList);
|
Vector<TYPE> copyTest(testList);
|
||||||
std::cout << "##### Copy Constructor Test #####\n";
|
std::cout << "##### Copy Constructor Test #####\n";
|
||||||
copyTest.print();
|
copyTest.print();
|
||||||
std::cout << "Deleting local copyTest Vector...\n";
|
std::cout << "Deleting local copyTest Vector...\n";
|
||||||
|
@ -83,7 +86,7 @@ int main()
|
||||||
// Test assignment operator, setting new Vector object equal to the existing
|
// Test assignment operator, setting new Vector object equal to the existing
|
||||||
case ASSIGN:
|
case ASSIGN:
|
||||||
{
|
{
|
||||||
Vector<std::string> assignTest;
|
Vector<TYPE> assignTest;
|
||||||
assignTest = testList;
|
assignTest = testList;
|
||||||
std::cout << "##### Assignment Test #####\n";
|
std::cout << "##### Assignment Test #####\n";
|
||||||
assignTest.print();
|
assignTest.print();
|
||||||
|
@ -93,7 +96,7 @@ int main()
|
||||||
|
|
||||||
case DESTRUCT:
|
case DESTRUCT:
|
||||||
{
|
{
|
||||||
Vector<std::string> destrTest(testList);
|
Vector<TYPE> destrTest(testList);
|
||||||
std::cout << "Current destrTest Vector contents...\n";
|
std::cout << "Current destrTest Vector contents...\n";
|
||||||
destrTest.print();
|
destrTest.print();
|
||||||
std::cout << "Deleting local destrTest Vector...\n";
|
std::cout << "Deleting local destrTest Vector...\n";
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
/*#############################################################################
|
/*#############################################################################
|
||||||
## Author: Shaun Reed ##
|
## Author: Shaun Reed ##
|
||||||
## Legal: All Content (c) 2020 Shaun Reed, all rights reserved ##
|
## 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 ##
|
## 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
|
* @brief Construct a new Vector::Vector object from an existing one
|
||||||
* Creates a new deep copy of a given Vector
|
* Creates a new deep copy of a given Vector
|
||||||
*
|
*
|
||||||
* @param rhs Vector object
|
* @param rhs Vector<T> object
|
||||||
*/
|
*/
|
||||||
template<typename T>
|
template<typename T>
|
||||||
Vector<T>::Vector(const Vector<T>& rhs)
|
Vector<T>::Vector(const Vector<T>& rhs)
|
||||||
|
@ -28,8 +28,8 @@ Vector<T>::Vector(const Vector<T>& rhs)
|
||||||
curIndex = rhs.getIndex();
|
curIndex = rhs.getIndex();
|
||||||
// Avoid copying over unused indices from parent Vector
|
// Avoid copying over unused indices from parent Vector
|
||||||
maxSize = rhs.getSize();
|
maxSize = rhs.getSize();
|
||||||
data = new T[curIndex];
|
data = new T[curIndex+1];
|
||||||
for (int i = 0; i <= rhs.getSize(); i++) {
|
for (int i = 0; i <= rhs.getIndex(); i++) {
|
||||||
data[i] = rhs.getValue(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
|
* Destructor called on previous Vector data at the end of this scope
|
||||||
*
|
*
|
||||||
* @param rhs Vector object passed by value, creating a local variable
|
* @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>
|
template<typename T>
|
||||||
Vector<T> Vector<T>::operator=(Vector<T> rhs)
|
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--]
|
* 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]
|
* @return T The value held at the Node pointed to by Vector::data[index]
|
||||||
*/
|
*/
|
||||||
template<typename T>
|
template<typename T>
|
||||||
T Vector<T>::pop()
|
T Vector<T>::pop()
|
||||||
|
@ -126,12 +126,12 @@ void Vector<T>::makeEmpty()
|
||||||
* @brief returns the value at the end of the vector
|
* @brief returns the value at the end of the vector
|
||||||
* If the vector is empty, returns INT32_MIN
|
* 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>
|
template<typename T>
|
||||||
T Vector<T>::peek() const
|
T Vector<T>::peek() const
|
||||||
{
|
{
|
||||||
T val = NULL;
|
T val;
|
||||||
if (!isEmpty()) {
|
if (!isEmpty()) {
|
||||||
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";
|
||||||
|
@ -182,7 +182,7 @@ void Vector<T>::print() const
|
||||||
* @return int at this->maxSize
|
* @return int at this->maxSize
|
||||||
*/
|
*/
|
||||||
template<typename T>
|
template<typename T>
|
||||||
T Vector<T>::getMax() const
|
int Vector<T>::getMax() const
|
||||||
{
|
{
|
||||||
return maxSize;
|
return maxSize;
|
||||||
}
|
}
|
||||||
|
@ -194,7 +194,7 @@ T Vector<T>::getMax() const
|
||||||
* @return int at this->curIndex + 1
|
* @return int at this->curIndex + 1
|
||||||
*/
|
*/
|
||||||
template<typename T>
|
template<typename T>
|
||||||
T Vector<T>::getSize() const
|
int Vector<T>::getSize() const
|
||||||
{
|
{
|
||||||
return curIndex + 1;
|
return curIndex + 1;
|
||||||
}
|
}
|
||||||
|
@ -206,7 +206,7 @@ T Vector<T>::getSize() const
|
||||||
* @return int at this->curIndex
|
* @return int at this->curIndex
|
||||||
*/
|
*/
|
||||||
template<typename T>
|
template<typename T>
|
||||||
T Vector<T>::getIndex() const
|
int Vector<T>::getIndex() const
|
||||||
{
|
{
|
||||||
return curIndex;
|
return curIndex;
|
||||||
}
|
}
|
||||||
|
@ -217,10 +217,10 @@ T Vector<T>::getIndex() const
|
||||||
* @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 The index containing the value to be returned
|
* @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>
|
template<typename T>
|
||||||
T Vector<T>::getValue(T index) const
|
T Vector<T>::getValue(int index) const
|
||||||
{
|
{
|
||||||
return data[index];
|
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
|
* Decrements the curIndex after storing the value to be returned
|
||||||
*
|
*
|
||||||
* @param data The Vector data to modify
|
* @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>
|
template<typename T>
|
||||||
T Vector<T>::pop(T *&data)
|
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
|
* @brief Private member to display the value at the end of our Vector
|
||||||
*
|
*
|
||||||
* @param data The Vector data to peek
|
* @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>
|
template<typename T>
|
||||||
T Vector<T>::peek(T *data) const
|
T Vector<T>::peek(T *data) const
|
||||||
|
@ -315,5 +315,7 @@ void Vector<T>::print(T *data) const
|
||||||
std::cout << std::endl;
|
std::cout << std::endl;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Instantiate relevant type templates for this class
|
||||||
template class Vector<int>;
|
template class Vector<int>;
|
||||||
|
template class Vector<float>;
|
||||||
template class Vector<std::string>;
|
template class Vector<std::string>;
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
/*#############################################################################
|
/*#############################################################################
|
||||||
## Author: Shaun Reed ##
|
## Author: Shaun Reed ##
|
||||||
## Legal: All Content (c) 2020 Shaun Reed, all rights reserved ##
|
## 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 ##
|
## Contact: shaunrd0@gmail.com | URL: www.shaunreed.com | GitHub: shaunrd0 ##
|
||||||
##############################################################################
|
##############################################################################
|
||||||
|
@ -28,21 +28,21 @@ class Vector {
|
||||||
bool isEmpty() const;
|
bool isEmpty() const;
|
||||||
bool isFull() const;
|
bool isFull() const;
|
||||||
void print() const;
|
void print() const;
|
||||||
T getMax() const;
|
int getMax() const;
|
||||||
T getSize() const;
|
int getSize() const;
|
||||||
T getIndex() const;
|
int getIndex() const;
|
||||||
T getValue(T index) const;
|
T getValue(int index) const;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
T maxSize;
|
int maxSize;
|
||||||
T curIndex;
|
int curIndex;
|
||||||
T *data;
|
T *data;
|
||||||
bool push(T val, T *&data);
|
bool push(T val, T *&data);
|
||||||
T pop(T *&data);
|
T pop(T *&data);
|
||||||
void makeEmpty(T *&data);
|
void makeEmpty(T *&data);
|
||||||
T peek(T *data) const;
|
T peek(T *data) const;
|
||||||
void print(T *data) const;
|
void print(T *data) const;
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
Loading…
Reference in New Issue