Initial commit for starting work on templates
This commit is contained in:
18
cpp/datastructs/templates/vector/CMakeLists.txt
Normal file
18
cpp/datastructs/templates/vector/CMakeLists.txt
Normal file
@@ -0,0 +1,18 @@
|
||||
###############################################################################
|
||||
## 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)
|
||||
|
||||
# Define the project name
|
||||
project(VectorDriver)
|
||||
# Define source files
|
||||
set(SRC driver.cpp vector.cpp)
|
||||
# Build an executable
|
||||
add_executable(VectorDriver ${SRC})
|
||||
23
cpp/datastructs/templates/vector/Makefile
Normal file
23
cpp/datastructs/templates/vector/Makefile
Normal file
@@ -0,0 +1,23 @@
|
||||
CXX=g++
|
||||
CXXFLAGS=-g -Wall
|
||||
|
||||
###############################################################################
|
||||
# Driver
|
||||
###############################################################################
|
||||
|
||||
driver: driver.cpp vector.o
|
||||
${CXX} ${CXXFLAGS} driver.cpp vector.o -o driver
|
||||
|
||||
###############################################################################
|
||||
# Vector
|
||||
###############################################################################
|
||||
|
||||
vector.o: vector.cpp vector.h
|
||||
${CXX} ${CXXFLAGS} -c vector.cpp -o vector.o
|
||||
|
||||
###############################################################################
|
||||
# Clean
|
||||
###############################################################################
|
||||
|
||||
clean:
|
||||
rm -f *.o driver
|
||||
111
cpp/datastructs/templates/vector/driver.cpp
Normal file
111
cpp/datastructs/templates/vector/driver.cpp
Normal file
@@ -0,0 +1,111 @@
|
||||
/*#############################################################################
|
||||
## Author: Shaun Reed ##
|
||||
## Legal: All Content (c) 2020 Shaun Reed, all rights reserved ##
|
||||
## About: A driver program to test a vector implementation ##
|
||||
## ##
|
||||
## Contact: shaunrd0@gmail.com | URL: www.shaunreed.com | GitHub: shaunrd0 ##
|
||||
##############################################################################
|
||||
## driver.cpp
|
||||
*/
|
||||
|
||||
#include "vector.h"
|
||||
#include <iostream>
|
||||
|
||||
enum OPS {
|
||||
EXIT, PUSH, POP, TOP, PRINT, EMPTY, CONSTRUCT, COPY, ASSIGN, DESTRUCT
|
||||
};
|
||||
|
||||
int main()
|
||||
{
|
||||
std::cout << "Driver: \n";
|
||||
Vector<std::string> testlist;
|
||||
bool exit = false;
|
||||
int choice = -1;
|
||||
<typename T> T val;
|
||||
|
||||
while (!exit)
|
||||
{
|
||||
std::cout << "##### vectors menu #####\n\t0. exit"
|
||||
<< "\n\t1. push\n\t2. pop\n\t3. peek\n\t4. print\n\t5. empty"
|
||||
<< "\n\t6. construct\n\t7. copy\n\t8. assign\n\t9. destruct\n\t";
|
||||
std::cin >> choice;
|
||||
std::cin.clear();
|
||||
switch (choice) {
|
||||
case EXIT:
|
||||
exit = true;
|
||||
break;
|
||||
|
||||
case PUSH:
|
||||
std::cout << "enter a value to push to our vector: ";
|
||||
std::cin >> val;
|
||||
std::cin.clear();
|
||||
testlist.push(val);
|
||||
break;
|
||||
|
||||
case POP:
|
||||
testList.pop();
|
||||
break;
|
||||
|
||||
case TOP:
|
||||
testList.peek();
|
||||
break;
|
||||
|
||||
case PRINT:
|
||||
testList.print();
|
||||
break;
|
||||
|
||||
case EMPTY:
|
||||
testList.makeEmpty();
|
||||
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:
|
||||
{
|
||||
Vector<std::string> constrTest;
|
||||
std::cout << "##### Constructor Test #####\n";
|
||||
constrTest.print();
|
||||
std::cout << "Deleting local constrTest Vector...\n";
|
||||
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:
|
||||
{
|
||||
Vector<std::string> copyTest(testList);
|
||||
std::cout << "##### Copy Constructor Test #####\n";
|
||||
copyTest.print();
|
||||
std::cout << "Deleting local copyTest Vector...\n";
|
||||
break;
|
||||
}
|
||||
|
||||
// Test assignment operator, setting new Vector object equal to the existing
|
||||
case ASSIGN:
|
||||
{
|
||||
Vector<std::string> assignTest;
|
||||
assignTest = testList;
|
||||
std::cout << "##### Assignment Test #####\n";
|
||||
assignTest.print();
|
||||
std::cout << "Deleting local assignTest Vector...\n";
|
||||
break;
|
||||
}
|
||||
|
||||
case DESTRUCT:
|
||||
{
|
||||
Vector<std::string> destrTest(testList);
|
||||
std::cout << "Current destrTest Vector contents...\n";
|
||||
destrTest.print();
|
||||
std::cout << "Deleting local destrTest Vector...\n";
|
||||
destrTest.~Vector(); // Implicitly called at the end of this scope {}
|
||||
destrTest.print();
|
||||
break;
|
||||
}
|
||||
|
||||
default:
|
||||
std::cout << "Invalid entry...\n";
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
319
cpp/datastructs/templates/vector/vector.cpp
Normal file
319
cpp/datastructs/templates/vector/vector.cpp
Normal file
@@ -0,0 +1,319 @@
|
||||
/*#############################################################################
|
||||
## Author: Shaun Reed ##
|
||||
## Legal: All Content (c) 2020 Shaun Reed, all rights reserved ##
|
||||
## About: An example of a vector implementation ##
|
||||
## ##
|
||||
## Contact: shaunrd0@gmail.com | URL: www.shaunreed.com | GitHub: shaunrd0 ##
|
||||
##############################################################################
|
||||
## vector.cpp
|
||||
*/
|
||||
|
||||
#include "vector.h"
|
||||
|
||||
|
||||
/******************************************************************************
|
||||
* Constructors, Destructors, Operators
|
||||
*****************************************************************************/
|
||||
|
||||
/** copy constructor
|
||||
* @brief Construct a new Vector::Vector object from an existing one
|
||||
* Creates a new deep copy of a given Vector
|
||||
*
|
||||
* @param rhs Vector object
|
||||
*/
|
||||
template<typename T>
|
||||
Vector<T>::Vector(const Vector<T>& rhs)
|
||||
{
|
||||
if (rhs.getIndex() >= 0) {
|
||||
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[i] = rhs.getValue(i);
|
||||
}
|
||||
}
|
||||
else {
|
||||
curIndex = -1;
|
||||
maxSize = 0;
|
||||
data = NULL;
|
||||
}
|
||||
}
|
||||
|
||||
/** operator=
|
||||
* @brief Assign two Vector objects equal using copy constr and class destr
|
||||
* Pass the rhs by value to create local copy, swap its contents
|
||||
* 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
|
||||
*/
|
||||
template<typename T>
|
||||
Vector<T> Vector<T>::operator=(Vector<T> rhs)
|
||||
{
|
||||
if (this == &rhs) return *this;
|
||||
// Swap the pointers, moving the previous head data to the local variable rhs
|
||||
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;
|
||||
}
|
||||
|
||||
/** destructor
|
||||
* @brief Destroy the Vector::Vector object
|
||||
*/
|
||||
template<typename T>
|
||||
Vector<T>::~Vector()
|
||||
{
|
||||
if (!isEmpty()) makeEmpty();
|
||||
}
|
||||
|
||||
|
||||
/******************************************************************************
|
||||
* Public Member Functions
|
||||
*****************************************************************************/
|
||||
|
||||
/** push
|
||||
* @brief Push a value to the end of our Vector
|
||||
*
|
||||
* @param val The value to be inserted
|
||||
*/
|
||||
template<typename T>
|
||||
bool Vector<T>::push(T val)
|
||||
{
|
||||
bool inserted = push(val, data);
|
||||
if (inserted)
|
||||
std::cout << "[" << val << "] was inserted into the vector\n";
|
||||
else std::cout << "[" << val << "] could not be inserted into the vector\n";
|
||||
return inserted;
|
||||
}
|
||||
|
||||
/** pop
|
||||
* @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
|
||||
*
|
||||
* @return int The value held at the Node pointed to by Vector::data[index]
|
||||
*/
|
||||
template<typename T>
|
||||
T Vector<T>::pop()
|
||||
{
|
||||
T val;
|
||||
if (!isEmpty()) {
|
||||
val = pop(data);
|
||||
std::cout << "[" << val << "] has been popped from our Vector\n";
|
||||
}
|
||||
else std::cout << "Nothing to pop, our Vector is empty...\n";
|
||||
return val;
|
||||
}
|
||||
|
||||
/** makeEmpty
|
||||
* @brief Empty this Vector object, deleting all associated data
|
||||
*/
|
||||
template<typename T>
|
||||
void Vector<T>::makeEmpty()
|
||||
{
|
||||
if (isEmpty()) {
|
||||
std::cout << "Cannot makeEmpty, our Vector is already empty...\n";
|
||||
return;
|
||||
}
|
||||
else makeEmpty(data);
|
||||
}
|
||||
|
||||
/** peek
|
||||
* @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
|
||||
*/
|
||||
template<typename T>
|
||||
T Vector<T>::peek() const
|
||||
{
|
||||
T val = NULL;
|
||||
if (!isEmpty()) {
|
||||
val = peek(data);
|
||||
std::cout << "[" << peek(data) << "] is at the end of our vector\n";
|
||||
}
|
||||
else std::cout << "Nothing to peek, our vector is empty...\n";
|
||||
return val;
|
||||
}
|
||||
|
||||
/** isEmpty
|
||||
* @brief Determine if the Vector is empty
|
||||
*
|
||||
* @return true If the Vector::data is NULL
|
||||
* @return false If the Vector::data contains any values
|
||||
*/
|
||||
template<typename T>
|
||||
bool Vector<T>::isEmpty() const
|
||||
{
|
||||
return data == NULL;
|
||||
}
|
||||
|
||||
/** isFull
|
||||
* @brief Determine if the Vector is full
|
||||
*
|
||||
* @return true If the Vector::size is equal to the current index
|
||||
* @return false If the Vector::size is greater than the current index
|
||||
*/
|
||||
template<typename T>
|
||||
bool Vector<T>::isFull() const
|
||||
{
|
||||
return getSize() == getMax() || data == NULL;
|
||||
}
|
||||
|
||||
/** print
|
||||
* @brief Output the data held by the Vector object
|
||||
* Calls to the private print()
|
||||
*/
|
||||
template<typename T>
|
||||
void Vector<T>::print() const
|
||||
{
|
||||
if(!isEmpty()) print(data);
|
||||
else std::cout << "Nothing to print, our vector is empty...\n";
|
||||
}
|
||||
|
||||
/** getMax
|
||||
* @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
|
||||
*/
|
||||
template<typename T>
|
||||
T Vector<T>::getMax() const
|
||||
{
|
||||
return maxSize;
|
||||
}
|
||||
|
||||
/** getSize
|
||||
* @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
|
||||
*/
|
||||
template<typename T>
|
||||
T Vector<T>::getSize() const
|
||||
{
|
||||
return curIndex + 1;
|
||||
}
|
||||
|
||||
/** getIndex
|
||||
* @brief Returns the current index of the vector
|
||||
* AKA the last index the vector wrote to
|
||||
*
|
||||
* @return int at this->curIndex
|
||||
*/
|
||||
template<typename T>
|
||||
T Vector<T>::getIndex() const
|
||||
{
|
||||
return curIndex;
|
||||
}
|
||||
|
||||
// TODO: use operator[](){...} instead
|
||||
|
||||
/** getValue
|
||||
* @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
|
||||
*/
|
||||
template<typename T>
|
||||
T Vector<T>::getValue(T index) const
|
||||
{
|
||||
return data[index];
|
||||
}
|
||||
|
||||
|
||||
/******************************************************************************
|
||||
* Private Member Functions
|
||||
*****************************************************************************/
|
||||
|
||||
/** push
|
||||
* @brief Private member to handle inserting value into the vector
|
||||
*
|
||||
* @param val Value to be inserted
|
||||
* @param data The data of the vector to push the value into
|
||||
*
|
||||
* @return true If the value was inserted
|
||||
* @return false If the value could not be inserted
|
||||
*/
|
||||
template<typename T>
|
||||
bool Vector<T>::push(T val, T *&data)
|
||||
{
|
||||
T *temp;
|
||||
if (isFull()) {
|
||||
if (maxSize <= 0) maxSize = 1;
|
||||
else maxSize *= 2;
|
||||
temp = new T[maxSize];
|
||||
for (int i = 0; i <= curIndex; i++) {
|
||||
temp[i] = data[i];
|
||||
}
|
||||
std::swap(data, temp);
|
||||
}
|
||||
curIndex += 1;
|
||||
data[curIndex] = val;
|
||||
return data[curIndex] == val;
|
||||
}
|
||||
|
||||
/** pop
|
||||
* @brief Returns the value held at the last index within the Vector
|
||||
* 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
|
||||
*/
|
||||
template<typename T>
|
||||
T Vector<T>::pop(T *&data)
|
||||
{
|
||||
// We already know the vector is not empty from public pop()
|
||||
T val = data[curIndex--];
|
||||
if (curIndex < 0) makeEmpty(data);
|
||||
return val;
|
||||
}
|
||||
|
||||
/** makeEmpty
|
||||
* @brief Private member to empty Vector object, deleting all associated data
|
||||
*
|
||||
* @param data The data of the Vector to be deleted
|
||||
*/
|
||||
template<typename T>
|
||||
void Vector<T>::makeEmpty(T *&data)
|
||||
{
|
||||
delete[] data;
|
||||
maxSize = 0;
|
||||
curIndex = -1;
|
||||
data = NULL;
|
||||
}
|
||||
|
||||
/** peek
|
||||
* @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
|
||||
*/
|
||||
template<typename T>
|
||||
T Vector<T>::peek(T *data) const
|
||||
{
|
||||
// We already know the vector is not empty from public peek()
|
||||
return data[curIndex];
|
||||
}
|
||||
|
||||
/** print
|
||||
* @brief Output the contents of a Vector from the beginning to the end
|
||||
*
|
||||
* @param data The data within the Vector to output
|
||||
*/
|
||||
template<typename T>
|
||||
void Vector<T>::print(T *data) const
|
||||
{
|
||||
std::cout << "Vector Contents: ";
|
||||
for (int i = 0; i <= curIndex; i++) {
|
||||
std::cout << data[i] << " | ";
|
||||
}
|
||||
std::cout << std::endl;
|
||||
}
|
||||
|
||||
template class Vector<int>;
|
||||
template class Vector<std::string>;
|
||||
48
cpp/datastructs/templates/vector/vector.h
Normal file
48
cpp/datastructs/templates/vector/vector.h
Normal file
@@ -0,0 +1,48 @@
|
||||
/*#############################################################################
|
||||
## Author: Shaun Reed ##
|
||||
## Legal: All Content (c) 2020 Shaun Reed, all rights reserved ##
|
||||
## About: An example of a vector implementation ##
|
||||
## ##
|
||||
## Contact: shaunrd0@gmail.com | URL: www.shaunreed.com | GitHub: shaunrd0 ##
|
||||
##############################################################################
|
||||
## vector.h
|
||||
*/
|
||||
|
||||
#ifndef VECTOR_H
|
||||
#define VECTOR_H
|
||||
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
|
||||
template <typename T>
|
||||
class Vector {
|
||||
public:
|
||||
Vector() : maxSize(0), curIndex(-1), data(NULL) {};
|
||||
Vector(const Vector& rhs);
|
||||
Vector<T> operator=(Vector<T> rhs);
|
||||
~Vector();
|
||||
bool push(T val);
|
||||
T pop();
|
||||
void makeEmpty();
|
||||
T peek() const;
|
||||
bool isEmpty() const;
|
||||
bool isFull() const;
|
||||
void print() const;
|
||||
T getMax() const;
|
||||
T getSize() const;
|
||||
T getIndex() const;
|
||||
T getValue(T index) const;
|
||||
|
||||
private:
|
||||
T maxSize;
|
||||
T 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
|
||||
Reference in New Issue
Block a user