mirror of
https://github.com/jakcron/nstool
synced 2024-11-22 21:49:30 +00:00
[fnd] Add List<T> template class.
This commit is contained in:
parent
f59068088d
commit
1515c56d2f
3 changed files with 95 additions and 0 deletions
91
lib/fnd/List.h
Normal file
91
lib/fnd/List.h
Normal file
|
@ -0,0 +1,91 @@
|
|||
#pragma once
|
||||
#include <fnd/types.h>
|
||||
#include <vector>
|
||||
|
||||
namespace fnd
|
||||
{
|
||||
template <class T>
|
||||
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<T> mElements;
|
||||
|
||||
void initList(T* elements, size_t num)
|
||||
{
|
||||
mElements.clear();
|
||||
for (size_t i = 0; i < num; i++)
|
||||
{
|
||||
mElements.push_back(elements[i]);
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
|
@ -118,6 +118,7 @@
|
|||
<ClInclude Include="elf.h" />
|
||||
<ClInclude Include="exception.h" />
|
||||
<ClInclude Include="io.h" />
|
||||
<ClInclude Include="List.h" />
|
||||
<ClInclude Include="memory_blob.h" />
|
||||
<ClInclude Include="string_conv.h" />
|
||||
<ClInclude Include="types.h" />
|
||||
|
|
|
@ -33,6 +33,9 @@
|
|||
<ClInclude Include="io.h">
|
||||
<Filter>Header Files</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="List.h">
|
||||
<Filter>Header Files</Filter>
|
||||
</ClInclude>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ClCompile Include="memory_blob.cpp">
|
||||
|
|
Loading…
Reference in a new issue