#pragma once #include #include namespace fnd { template class List { public: // constructors List(); List(const List& other); // copy operator const List& operator=(const List& other); // equivalence operators bool operator==(const List& other) const; bool operator!=(const List& other) const; // back relative insertion void addElement(const T& element); // element access const T& operator[](size_t index) const; T& operator[](size_t index); const T& atBack() const; T& atBack(); // element num size_t size() const; // clear List void clear(); // element access by key template bool hasElement(const K& key) const; template const T& getElement(const K& key) const; template T& getElement(const K& key); private: static const size_t kDefaultSize = 20; fnd::Vec m_Vec; size_t m_Num; }; template inline List::List() : m_Vec(), m_Num(0) { m_Vec.alloc(kDefaultSize); } template inline List::List(const List& other) : m_Vec(other.m_Vec), m_Size(other.m_Size) {} template inline const List& List::operator=(const List& other) { m_Vec = other.m_Vec; m_Size = other.m_Size; } template inline bool List::operator==(const List& other) const { bool isEqual = true; if (m_Num == other.m_Num) { for (size_t i = 0; i < m_Num && isEqual == true; i++) { if ((*this)[i] != other[i]) isEqual = false; } } else { isEqual = false; } return isEqual; } template inline bool List::operator!=(const List& other) const { return !(*this == other); } template inline void List::addElement(const T & element) { (*this)[m_Num] = element; } template inline const T & List::operator[](size_t index) const { if (index >= m_Num) { throw fnd::Exception("List", "Out of bound read"); } return m_Vec[index]; } template inline T & List::operator[](size_t index) { if (index > m_Num) { throw fnd::Exception("List", "Out of bound read"); } else if (index == m_Num) { if ((m_Num * 2) >= m_Vec.size()) { m_Vec.alloc((m_Num + 1) * 2); } m_Num++; } return m_Vec[index]; } template inline const T & List::atBack() const { return m_Vec[m_Num - 1]; } template inline T & List::atBack() { return m_Vec[m_Num - 1]; } template inline size_t List::size() const { return m_Num; } template inline void List::clear() { m_Num = 0; m_Vec.clear(); } template template inline bool List::hasElement(const K & key) const { for (size_t i = 0; i < m_Num; i++) { if (m_List[i] == key) { return true; } } return false; } template template inline const T & List::getElement(const K & key) const { for (size_t i = 0; i < m_Num; i++) { if (m_List[i] == key) { return m_List[i]; } } throw fnd::Exception("getElement(): element does not exist"); } template template inline T & List::getElement(const K & key) { for (size_t i = 0; i < m_Num; i++) { if (m_List[i] == key) { return m_List[i]; } } throw fnd::Exception("getElement(): element does not exist"); } }