From 1515c56d2f5ae9b00f4726518bda9facc6836858 Mon Sep 17 00:00:00 2001 From: jakcron Date: Thu, 6 Jul 2017 20:48:46 +1000 Subject: [PATCH] [fnd] Add List template class. --- lib/fnd/List.h | 91 +++++++++++++++++++++++++++++++++++++ lib/fnd/fnd.vcxproj | 1 + lib/fnd/fnd.vcxproj.filters | 3 ++ 3 files changed, 95 insertions(+) create mode 100644 lib/fnd/List.h diff --git a/lib/fnd/List.h b/lib/fnd/List.h new file mode 100644 index 0000000..9a23ed3 --- /dev/null +++ b/lib/fnd/List.h @@ -0,0 +1,91 @@ +#pragma once +#include +#include + +namespace fnd +{ + template + class List + { + public: + List() : + mElements() + { + } + + List(const T* elements, size_t num) : + mElements(num) + { + initList(elements, num); + } + + // assignment operator + const List& operator=(const List& other) + { + mElements.clear(); + for (size_t i = 0; i < other.getSize(); i++) + { + mElements.push_back(other[i]); + } + + return *this; + } + + // comparision operator + bool operator==(const List& other) const + { + if (other.getSize() != this->getSize()) + { + return false; + } + + for (size_t i = 0; i < this->getSize(); i++) + { + if (getElement(i) != other[i]) + { + return false; + } + } + + return true; + } + + bool operator!=(const List& other) const + { + return !operator==(other); + } + + // access operators + const T& getElement(size_t index) const + { + return mElements[index]; + } + T& getElement(size_t index) + { + if (index == mElements.size()) mElements.push_back(T()); + return mElements[index]; + } + + const T& operator[](size_t index) const { return getElement(index); } + T& operator[](size_t index) { return getElement(index); } + const T& atBack() const { return getElement(getSize() - 1); } + T& atBack() { return getElement(getSize() - 1); } + + // functions + void addElement(const T& element) { mElements.push_back(element); } + size_t getSize() const { return mElements.size(); } + void clear() { mElements.clear(); } + private: + std::vector mElements; + + void initList(T* elements, size_t num) + { + mElements.clear(); + for (size_t i = 0; i < num; i++) + { + mElements.push_back(elements[i]); + } + } + }; +} + diff --git a/lib/fnd/fnd.vcxproj b/lib/fnd/fnd.vcxproj index 26ed2dd..3cae9da 100644 --- a/lib/fnd/fnd.vcxproj +++ b/lib/fnd/fnd.vcxproj @@ -118,6 +118,7 @@ + diff --git a/lib/fnd/fnd.vcxproj.filters b/lib/fnd/fnd.vcxproj.filters index 66964fe..4f99952 100644 --- a/lib/fnd/fnd.vcxproj.filters +++ b/lib/fnd/fnd.vcxproj.filters @@ -33,6 +33,9 @@ Header Files + + Header Files +