nstool/lib/libfnd/include/fnd/List.h

168 lines
2.7 KiB
C
Raw Permalink Normal View History

2017-07-06 10:48:46 +00:00
#pragma once
#include <fnd/types.h>
#include <vector>
//#include <fnd/Vec.h>
2017-07-06 10:48:46 +00:00
namespace fnd
{
template <class T>
class List
{
public:
// constructors
List();
List(const List<T>& other);
// copy operator
void 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:
std::vector<T> m_Vec;
};
2017-07-06 10:48:46 +00:00
template<class T>
inline List<T>::List() :
m_Vec()
{
}
2017-07-06 10:48:46 +00:00
template<class T>
inline List<T>::List(const List<T>& other) :
List()
{
*this = other;
}
2017-07-06 10:48:46 +00:00
template<class T>
inline void List<T>::operator=(const List<T>& other)
{
m_Vec = other.m_Vec;
}
2017-07-06 10:48:46 +00:00
template<class T>
inline bool List<T>::operator==(const List<T>& other) const
{
return m_Vec == other.m_Vec;
}
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)
{
m_Vec.push_back(element);
}
template<class T>
inline const T & List<T>::operator[](size_t index) const
{
return m_Vec[index];
}
2017-07-06 10:48:46 +00:00
template<class T>
inline T & List<T>::operator[](size_t index)
{
return m_Vec[index];
}
template<class T>
inline const T & List<T>::atBack() const
{
return m_Vec.back();
}
template<class T>
inline T & List<T>::atBack()
{
return m_Vec.back();
}
template<class T>
inline size_t List<T>::size() const
{
return m_Vec.size();
}
template<class T>
inline void List<T>::clear()
{
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_Vec.size(); i++)
2017-07-18 14:17:32 +00:00
{
if (m_Vec[i] == key)
2017-07-18 14:17:32 +00:00
{
return true;
2017-07-18 14:17:32 +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_Vec.size(); i++)
2017-07-18 14:17:32 +00:00
{
if (m_Vec[i] == key)
2017-07-12 14:03:56 +00:00
{
return m_Vec[i];
2017-07-12 14:03:56 +00:00
}
}
2017-07-06 10:48:46 +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_Vec.size(); i++)
2017-07-06 10:48:46 +00:00
{
if (m_Vec[i] == key)
2017-07-06 10:48:46 +00:00
{
return m_Vec[i];
2017-07-06 10:48:46 +00:00
}
}
throw fnd::Exception("getElement(): element does not exist");
}
}