diff --git a/sourcehook/sh_vector.h b/sourcehook/sh_vector.h index 45f4ed6..26b16b9 100755 --- a/sourcehook/sh_vector.h +++ b/sourcehook/sh_vector.h @@ -19,39 +19,58 @@ namespace SourceHook // Vector template class CVector { - bool Grow() + bool Grow(size_t amount) { // automatic grow size_t newSize = m_Size * 2; + if (newSize == 0) - newSize = 8; // a good init value + { + newSize = 8; + } + + while (m_CurrentUsedSize + amount > newSize) + { + newSize *= 2; + } + T *newData = new T[newSize]; - if (!newData) - return false; + if (m_Data) { for (size_t i=0; i= m_Size) - return Grow(); + if (m_CurrentUsedSize + amount >= m_Size) + { + return Grow(amount); + } else + { return true; + } } bool ChangeSize(size_t size) { // change size if (size == m_Size) + { return true; + } if (!size) { @@ -65,19 +84,24 @@ template class CVector } T *newData = new T[size]; - if (!newData) - return false; + if (m_Data) { size_t end = (m_CurrentUsedSize < size) ? (m_CurrentUsedSize) : size; for (size_t i=0; i m_Size) + { m_CurrentUsedSize = m_Size; + } return true; } @@ -85,7 +109,9 @@ template class CVector void FreeMemIfPossible() { if (!m_Data) + { return; + } if (!m_CurrentUsedSize) { @@ -95,10 +121,14 @@ template class CVector size_t newSize = m_Size; while (m_CurrentUsedSize <= newSize / 2) + { newSize /= 2; + } if (newSize != m_Size) + { ChangeSize(newSize); + } } protected: T *m_Data; @@ -321,14 +351,13 @@ public: bool push_back(const T & elem) { - ++m_CurrentUsedSize; - if (!GrowIfNeeded()) + if (!GrowIfNeeded(1)) { - --m_CurrentUsedSize; return false; } - m_Data[m_CurrentUsedSize - 1] = elem; + m_Data[m_CurrentUsedSize++] = elem; + return true; } @@ -433,13 +462,13 @@ public: size_t ofs = where - begin(); - ++m_CurrentUsedSize; - if (!GrowIfNeeded()) + if (!GrowIfNeeded(1)) { - --m_CurrentUsedSize; return false; } + ++m_CurrentUsedSize; + where = begin() + ofs; // Move subsequent entries