mirror of
https://github.com/alliedmodders/metamod-source.git
synced 2025-02-21 14:54:14 +01:00
Updated stack + added simple stack test
--HG-- extra : convert_revision : svn%3Ac2935e3e-5518-0410-8daf-afa5dab7d4e3/trunk%40130
This commit is contained in:
parent
17aa1b4c3f
commit
19000cb24b
@ -70,7 +70,7 @@ namespace SourceHook
|
|||||||
iterator operator++(int) // postincrement
|
iterator operator++(int) // postincrement
|
||||||
{
|
{
|
||||||
iterator tmp = *this;
|
iterator tmp = *this;
|
||||||
++m_Ptr;
|
++m_Index;
|
||||||
return tmp;
|
return tmp;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -83,7 +83,7 @@ namespace SourceHook
|
|||||||
iterator operator--(int) // postdecrememnt
|
iterator operator--(int) // postdecrememnt
|
||||||
{
|
{
|
||||||
iterator tmp = *this;
|
iterator tmp = *this;
|
||||||
--m_Ptr;
|
--m_Index;
|
||||||
return tmp;
|
return tmp;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -110,12 +110,37 @@ namespace SourceHook
|
|||||||
assert(m_Elements);
|
assert(m_Elements);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
CStack(const CStack &other) : m_Elements(NULL),
|
||||||
|
m_AllocatedSize(0),
|
||||||
|
m_UsedSize(0)
|
||||||
|
{
|
||||||
|
assert(reserve(other.m_AllocatedSize) && m_Elements);
|
||||||
|
m_UsedSize = other.m_UsedSize;
|
||||||
|
for (size_t i = 0; i < m_UsedSize; ++i)
|
||||||
|
m_Elements[i] = other.m_Elements[i];
|
||||||
|
}
|
||||||
|
|
||||||
~CStack()
|
~CStack()
|
||||||
{
|
{
|
||||||
if (m_Elements)
|
if (m_Elements)
|
||||||
delete [] m_Elements;
|
delete [] m_Elements;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void operator=(const CStack &other)
|
||||||
|
{
|
||||||
|
if (m_AllocatedSize < other.m_AllocatedSize)
|
||||||
|
{
|
||||||
|
if (m_Elements)
|
||||||
|
delete [] m_Elements;
|
||||||
|
m_Elements = new T[other.m_AllocatedSize];
|
||||||
|
assert(m_Elements);
|
||||||
|
m_AllocatedSize = other.m_AllocatedSize;
|
||||||
|
}
|
||||||
|
m_UsedSize = other.m_UsedSize;
|
||||||
|
for (size_t i = 0; i < m_UsedSize; ++i)
|
||||||
|
m_Elements[i] = other.m_Elements[i];
|
||||||
|
}
|
||||||
|
|
||||||
bool push(const T &val)
|
bool push(const T &val)
|
||||||
{
|
{
|
||||||
if (m_UsedSize + 1 == m_AllocatedSize)
|
if (m_UsedSize + 1 == m_AllocatedSize)
|
||||||
@ -128,9 +153,12 @@ namespace SourceHook
|
|||||||
m_AllocatedSize /= 2;
|
m_AllocatedSize /= 2;
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
for (size_t i = 0; i < m_UsedSize; ++i)
|
if (m_Elements)
|
||||||
newElements[i] = m_Elements[i];
|
{
|
||||||
delete [] m_Elements;
|
for (size_t i = 0; i < m_UsedSize; ++i)
|
||||||
|
newElements[i] = m_Elements[i];
|
||||||
|
delete [] m_Elements;
|
||||||
|
}
|
||||||
m_Elements = newElements;
|
m_Elements = newElements;
|
||||||
}
|
}
|
||||||
m_Elements[m_UsedSize++] = val;
|
m_Elements[m_UsedSize++] = val;
|
||||||
@ -159,6 +187,37 @@ namespace SourceHook
|
|||||||
{
|
{
|
||||||
return iterator(this, m_UsedSize);
|
return iterator(this, m_UsedSize);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
size_t size()
|
||||||
|
{
|
||||||
|
return m_UsedSize;
|
||||||
|
}
|
||||||
|
size_t capacity()
|
||||||
|
{
|
||||||
|
return m_AllocatedSize;
|
||||||
|
}
|
||||||
|
bool empty()
|
||||||
|
{
|
||||||
|
return m_UsedSize == 0 ? true : false;
|
||||||
|
}
|
||||||
|
bool reserve(size_t size)
|
||||||
|
{
|
||||||
|
if (size > m_AllocatedSize)
|
||||||
|
{
|
||||||
|
T *newElements = new T[size];
|
||||||
|
if (!newElements)
|
||||||
|
return false;
|
||||||
|
if (m_Elements)
|
||||||
|
{
|
||||||
|
for (size_t i = 0; i < m_UsedSize; ++i)
|
||||||
|
newElements[i] = m_Elements[i];
|
||||||
|
delete [] m_Elements;
|
||||||
|
}
|
||||||
|
m_Elements = newElements;
|
||||||
|
m_AllocatedSize = size;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
};
|
};
|
||||||
}; //namespace SourceHook
|
}; //namespace SourceHook
|
||||||
|
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
#include <string>
|
#include <string>
|
||||||
#include "sh_list.h"
|
#include "sh_list.h"
|
||||||
|
#include "sh_stack.h"
|
||||||
#include "sh_tinyhash.h"
|
#include "sh_tinyhash.h"
|
||||||
#include "testevents.h"
|
#include "testevents.h"
|
||||||
|
|
||||||
@ -163,6 +164,53 @@ namespace
|
|||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool DoTestStack(std::string &error)
|
||||||
|
{
|
||||||
|
typedef SourceHook::CStack<int> IntStack;
|
||||||
|
IntStack stk;
|
||||||
|
int i;
|
||||||
|
|
||||||
|
CHECK_COND(stk.size() == 0 && stk.empty(), "A0");
|
||||||
|
|
||||||
|
for (i = 0; i < 5000; ++i)
|
||||||
|
stk.push(i);
|
||||||
|
|
||||||
|
CHECK_COND(stk.front() == 4999, "1");
|
||||||
|
CHECK_COND(stk.size() == 5000 && !stk.empty(), "A1");
|
||||||
|
|
||||||
|
IntStack::iterator iter;
|
||||||
|
i = 0;
|
||||||
|
for (iter = stk.begin(); iter != stk.end(); ++iter, ++i)
|
||||||
|
CHECK_COND(*iter == i, "2");
|
||||||
|
|
||||||
|
i = 0;
|
||||||
|
for (iter = stk.begin(); iter != stk.end(); iter++, ++i)
|
||||||
|
CHECK_COND(*iter == i, "3");
|
||||||
|
|
||||||
|
--iter;
|
||||||
|
iter--;
|
||||||
|
*iter = 'g'+'a'+'b'+'e'+'n';
|
||||||
|
stk.pop();
|
||||||
|
|
||||||
|
CHECK_COND(stk.size() == 4999 && !stk.empty(), "A2");
|
||||||
|
CHECK_COND(stk.front() == 'g'+'a'+'b'+'e'+'n', "4");
|
||||||
|
|
||||||
|
IntStack stk2(stk);
|
||||||
|
CHECK_COND(stk2.size() == 4999 && !stk2.empty(), "A3");
|
||||||
|
|
||||||
|
IntStack::iterator iter2 = stk2.begin();
|
||||||
|
for (iter = stk.begin(); iter != stk.end(); ++iter, iter2++)
|
||||||
|
CHECK_COND(*iter == *iter2, "5");
|
||||||
|
|
||||||
|
while (!stk2.empty())
|
||||||
|
stk2.pop();
|
||||||
|
CHECK_COND(stk2.size() == 0 && stk2.empty(), "A4");
|
||||||
|
stk = stk2;
|
||||||
|
CHECK_COND(stk.size() == 0 && stk.empty(), "A5");
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
bool TestList(std::string &error)
|
bool TestList(std::string &error)
|
||||||
@ -173,6 +221,8 @@ bool TestList(std::string &error)
|
|||||||
if (!DoTestTinyHash(error))
|
if (!DoTestTinyHash(error))
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
|
if (!DoTestStack(error))
|
||||||
|
return false;
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user