2017-07-06 10:48:46 +00:00
|
|
|
#pragma once
|
|
|
|
#include <fnd/types.h>
|
2018-06-24 04:45:45 +00:00
|
|
|
#include <fnd/Vec.h>
|
2017-07-06 10:48:46 +00:00
|
|
|
|
|
|
|
namespace fnd
|
|
|
|
{
|
|
|
|
template <class T>
|
|
|
|
class List
|
|
|
|
{
|
|
|
|
public:
|
2018-06-24 04:45:45 +00:00
|
|
|
// constructors
|
|
|
|
List();
|
|
|
|
List(const List<T>& other);
|
|
|
|
|
|
|
|
// copy operator
|
|
|
|
const List<T>& operator=(const List<T>& other);
|
|
|
|
|
|
|
|
// equivalence operators
|
|
|
|
bool operator==(const List<T>& other) const;
|
|
|
|
bool operator!=(const List<T>& 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 <class K>
|
|
|
|
bool hasElement(const K& key) const;
|
|
|
|
template <class K>
|
|
|
|
const T& getElement(const K& key) const;
|
|
|
|
template <class K>
|
|
|
|
T& getElement(const K& key);
|
|
|
|
|
|
|
|
private:
|
|
|
|
static const size_t kDefaultSize = 20;
|
2017-07-06 10:48:46 +00:00
|
|
|
|
2018-06-24 04:45:45 +00:00
|
|
|
fnd::Vec<T> m_Vec;
|
|
|
|
size_t m_Num;
|
|
|
|
};
|
2017-07-06 10:48:46 +00:00
|
|
|
|
2018-06-24 04:45:45 +00:00
|
|
|
template<class T>
|
|
|
|
inline List<T>::List() :
|
|
|
|
m_Vec(),
|
|
|
|
m_Num(0)
|
|
|
|
{
|
|
|
|
m_Vec.alloc(kDefaultSize);
|
|
|
|
}
|
2017-07-06 10:48:46 +00:00
|
|
|
|
2018-06-24 04:45:45 +00:00
|
|
|
template<class T>
|
|
|
|
inline List<T>::List(const List<T>& other) :
|
|
|
|
m_Vec(other.m_Vec),
|
|
|
|
m_Size(other.m_Size)
|
|
|
|
{}
|
2017-07-06 10:48:46 +00:00
|
|
|
|
2018-06-24 04:45:45 +00:00
|
|
|
template<class T>
|
|
|
|
inline const List<T>& List<T>::operator=(const List<T>& other)
|
|
|
|
{
|
|
|
|
m_Vec = other.m_Vec;
|
|
|
|
m_Size = other.m_Size;
|
|
|
|
}
|
2017-07-06 10:48:46 +00:00
|
|
|
|
2018-06-24 04:45:45 +00:00
|
|
|
template<class T>
|
|
|
|
inline bool List<T>::operator==(const List<T>& other) const
|
|
|
|
{
|
|
|
|
bool isEqual = true;
|
|
|
|
|
|
|
|
if (m_Num == other.m_Num)
|
|
|
|
{
|
|
|
|
for (size_t i = 0; i < m_Num && isEqual == true; i++)
|
2017-07-06 10:48:46 +00:00
|
|
|
{
|
2018-06-24 04:45:45 +00:00
|
|
|
if ((*this)[i] != other[i])
|
|
|
|
isEqual = false;
|
2017-07-06 10:48:46 +00:00
|
|
|
}
|
|
|
|
}
|
2018-06-24 04:45:45 +00:00
|
|
|
else
|
2017-07-06 10:48:46 +00:00
|
|
|
{
|
2018-06-24 04:45:45 +00:00
|
|
|
isEqual = false;
|
2017-07-06 10:48:46 +00:00
|
|
|
}
|
|
|
|
|
2018-06-24 04:45:45 +00:00
|
|
|
return isEqual;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<class T>
|
|
|
|
inline bool List<T>::operator!=(const List<T>& other) const
|
|
|
|
{
|
|
|
|
return !(*this == other);
|
|
|
|
}
|
|
|
|
|
|
|
|
template<class T>
|
|
|
|
inline void List<T>::addElement(const T & element)
|
|
|
|
{
|
|
|
|
(*this)[m_Num] = element;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<class T>
|
|
|
|
inline const T & List<T>::operator[](size_t index) const
|
|
|
|
{
|
|
|
|
if (index >= m_Num)
|
2017-07-06 10:48:46 +00:00
|
|
|
{
|
2018-06-24 04:45:45 +00:00
|
|
|
throw fnd::Exception("List", "Out of bound read");
|
2017-07-06 10:48:46 +00:00
|
|
|
}
|
|
|
|
|
2018-06-24 04:45:45 +00:00
|
|
|
return m_Vec[index];
|
|
|
|
}
|
2017-07-06 10:48:46 +00:00
|
|
|
|
2018-06-24 04:45:45 +00:00
|
|
|
template<class T>
|
|
|
|
inline T & List<T>::operator[](size_t index)
|
|
|
|
{
|
|
|
|
if (index > m_Num)
|
2017-07-12 14:03:56 +00:00
|
|
|
{
|
2018-06-24 04:45:45 +00:00
|
|
|
throw fnd::Exception("List", "Out of bound read");
|
2017-07-12 14:03:56 +00:00
|
|
|
}
|
2018-06-24 04:45:45 +00:00
|
|
|
else if (index == m_Num)
|
2017-07-12 14:03:56 +00:00
|
|
|
{
|
2018-06-24 04:45:45 +00:00
|
|
|
if ((m_Num * 2) >= m_Vec.size())
|
2017-07-12 14:03:56 +00:00
|
|
|
{
|
2018-06-24 04:45:45 +00:00
|
|
|
m_Vec.alloc((m_Num + 1) * 2);
|
2017-07-18 14:17:32 +00:00
|
|
|
}
|
|
|
|
|
2018-06-24 04:45:45 +00:00
|
|
|
m_Num++;
|
2017-07-18 14:17:32 +00:00
|
|
|
}
|
|
|
|
|
2018-06-24 04:45:45 +00:00
|
|
|
return m_Vec[index];
|
|
|
|
}
|
|
|
|
|
|
|
|
template<class T>
|
|
|
|
inline const T & List<T>::atBack() const
|
|
|
|
{
|
|
|
|
return m_Vec[m_Num - 1];
|
|
|
|
}
|
|
|
|
|
|
|
|
template<class T>
|
|
|
|
inline T & List<T>::atBack()
|
|
|
|
{
|
|
|
|
return m_Vec[m_Num - 1];
|
|
|
|
}
|
|
|
|
|
|
|
|
template<class T>
|
|
|
|
inline size_t List<T>::size() const
|
|
|
|
{
|
|
|
|
return m_Num;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<class T>
|
|
|
|
inline void List<T>::clear()
|
|
|
|
{
|
|
|
|
m_Num = 0;
|
|
|
|
m_Vec.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
template<class T>
|
|
|
|
template<class K>
|
|
|
|
inline bool List<T>::hasElement(const K & key) const
|
|
|
|
{
|
|
|
|
for (size_t i = 0; i < m_Num; i++)
|
2017-07-18 14:17:32 +00:00
|
|
|
{
|
2018-06-24 04:45:45 +00:00
|
|
|
if (m_List[i] == key)
|
2017-07-18 14:17:32 +00:00
|
|
|
{
|
2018-06-24 04:45:45 +00:00
|
|
|
return true;
|
2017-07-18 14:17:32 +00:00
|
|
|
}
|
|
|
|
}
|
2018-06-24 04:45:45 +00:00
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<class T>
|
|
|
|
template<class K>
|
|
|
|
inline const T & List<T>::getElement(const K & key) const
|
|
|
|
{
|
|
|
|
for (size_t i = 0; i < m_Num; i++)
|
2017-07-18 14:17:32 +00:00
|
|
|
{
|
2018-06-24 04:45:45 +00:00
|
|
|
if (m_List[i] == key)
|
2017-07-12 14:03:56 +00:00
|
|
|
{
|
2018-06-24 04:45:45 +00:00
|
|
|
return m_List[i];
|
2017-07-12 14:03:56 +00:00
|
|
|
}
|
|
|
|
}
|
2017-07-06 10:48:46 +00:00
|
|
|
|
2018-06-24 04:45:45 +00:00
|
|
|
throw fnd::Exception("getElement(): element does not exist");
|
|
|
|
}
|
|
|
|
|
|
|
|
template<class T>
|
|
|
|
template<class K>
|
|
|
|
inline T & List<T>::getElement(const K & key)
|
|
|
|
{
|
|
|
|
for (size_t i = 0; i < m_Num; i++)
|
2017-07-06 10:48:46 +00:00
|
|
|
{
|
2018-06-24 04:45:45 +00:00
|
|
|
if (m_List[i] == key)
|
2017-07-06 10:48:46 +00:00
|
|
|
{
|
2018-06-24 04:45:45 +00:00
|
|
|
return m_List[i];
|
2017-07-06 10:48:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-24 04:45:45 +00:00
|
|
|
throw fnd::Exception("getElement(): element does not exist");
|
|
|
|
}
|
|
|
|
}
|