Ejemplo n.º 1
0
void VArray<TYPE, ARG_TYPE>::InsertAt(int nStartIndex, VArray* pNewArray)
{
  VASSERT(V_IS_VALID_PTR(this));
  VASSERT(pNewArray != NULL);
  VASSERT(V_IS_VALID_PTR(this));
  VASSERT(nStartIndex >= 0);

  if (pNewArray->GetSize() > 0)
  {
    InsertAt(nStartIndex, pNewArray->GetAt(0), pNewArray->GetSize());
    for (int i = 0; i < pNewArray->GetSize(); i++)
      SetAt(nStartIndex + i, pNewArray->GetAt(i));
  }
}
Ejemplo n.º 2
0
void VArray<TYPE, ARG_TYPE>::InsertAt(int nIndex, ARG_TYPE newElement, int nCount /*=1*/)
{
  VASSERT(V_IS_VALID_PTR(this));
  VASSERT(nIndex >= 0);    ///< will expand to meet need
  VASSERT(nCount > 0);     ///< zero or negative size not allowed

  if (nIndex >= m_nSize)
  {
    // adding after the end of the array
    SetSize(nIndex + nCount, -1);   ///< grow so nIndex is valid
  }
  else
  {
    // inserting in the middle of the array
    int nOldSize = m_nSize;
    SetSize(m_nSize + nCount, -1);  ///< grow it to new size
    // destroy intial data before copying over it
    VDestructElements<TYPE>(&m_pData[nOldSize], nCount);
    // shift old data up to fill gap
    memmove(&m_pData[nIndex+nCount], &m_pData[nIndex],
      (nOldSize-nIndex) * sizeof(TYPE));

    // re-init slots we copied from
    VConstructElementsZeroInit<TYPE>(&m_pData[nIndex], nCount);
  }

  // insert new value in the gap
  VASSERT(nIndex + nCount <= m_nSize);
  while (nCount--)
    m_pData[nIndex++] = newElement;
}
Ejemplo n.º 3
0
void VArray<TYPE, ARG_TYPE>::Copy(const VArray& src)
{
  VASSERT(V_IS_VALID_PTR(this));
  VASSERT(this != &src);   ///< cannot append to itself

  SetSize(src.m_nSize);
  VCopyElements<TYPE>(m_pData, src.m_pData, src.m_nSize);
}
Ejemplo n.º 4
0
void VArray<TYPE, ARG_TYPE>::SetAtGrow(int nIndex, ARG_TYPE newElement)
{
  VASSERT(V_IS_VALID_PTR(this));
  VASSERT(nIndex >= 0);

  if (nIndex >= m_nSize)
    SetSize(nIndex+1, -1);
  m_pData[nIndex] = newElement;
}
Ejemplo n.º 5
0
int VArray<TYPE, ARG_TYPE>::Append(const VArray& src)
{
  VASSERT(V_IS_VALID_PTR(this));
  VASSERT(this != &src);   ///< cannot append to itself

  int nOldSize = m_nSize;
  SetSize(m_nSize + src.m_nSize);
  VCopyElements<TYPE>(m_pData + nOldSize, src.m_pData, src.m_nSize);
  return nOldSize;
}
Ejemplo n.º 6
0
VArray<TYPE, ARG_TYPE>::~VArray()
{
  VASSERT(V_IS_VALID_PTR(this));

  if (m_pData != NULL)
  {
    VDestructElements<TYPE>(m_pData, m_nSize);
    delete[] (BYTE*)m_pData;
  }
}
Ejemplo n.º 7
0
void VArray<TYPE, ARG_TYPE>::RemoveAtSwapWithLast(int nIndex)
{
  VASSERT(V_IS_VALID_PTR(this));
  VASSERT(nIndex >= 0);
  VASSERT(nIndex < m_nSize);

  VDestructElements<TYPE>(&m_pData[nIndex], 1);
  memcpy(&m_pData[nIndex], &m_pData[m_nSize-1], sizeof(TYPE));
  --m_nSize;
}
Ejemplo n.º 8
0
void VArray<TYPE, ARG_TYPE>::RemoveAt(int nIndex, int nCount)
{
  VASSERT(V_IS_VALID_PTR(this));
  VASSERT(nIndex >= 0);
  VASSERT(nCount >= 0);
  VASSERT(nIndex + nCount <= m_nSize);

  // just remove a range
  int nMoveCount = m_nSize - (nIndex + nCount);
  VDestructElements<TYPE>(&m_pData[nIndex], nCount);
  if (nMoveCount)
    memmove(&m_pData[nIndex], &m_pData[nIndex + nCount],
      nMoveCount * sizeof(TYPE));
  m_nSize -= nCount;
}
Ejemplo n.º 9
0
void VArray<TYPE, ARG_TYPE>::Swap(VArray& other)
{
  VASSERT(V_IS_VALID_PTR(this));

  TYPE* pData    = m_pData;
  int   nSize    = m_nSize;
  int   nMaxSize = m_nMaxSize;
  int   nGrowBy  = m_nGrowBy;

  m_pData    = other.m_pData;
  m_nSize    = other.m_nSize;
  m_nMaxSize = other.m_nMaxSize;
  m_nGrowBy  = other.m_nGrowBy;

  other.m_pData    = pData;
  other.m_nSize    = nSize;
  other.m_nMaxSize = nMaxSize;
  other.m_nGrowBy  = nGrowBy;
}
Ejemplo n.º 10
0
void VArray<TYPE, ARG_TYPE>::FreeExtra()
{
  VASSERT(V_IS_VALID_PTR(this));

  if (m_nSize != m_nMaxSize)
  {
    // shrink to desired size
    TYPE* pNewData = NULL;
    if (m_nSize != 0)
    {
      pNewData = (TYPE*) new BYTE[m_nSize * sizeof(TYPE)];
      // copy new data from old
      memcpy(pNewData, m_pData, m_nSize * sizeof(TYPE));
    }

    // get rid of old stuff (note: no destructors called)
    delete[] (BYTE*)m_pData;
    m_pData = pNewData;
    m_nMaxSize = m_nSize;
  }
}
Ejemplo n.º 11
0
void VArray<TYPE, ARG_TYPE>::SetSize(int nNewSize, int nGrowBy, bool bDeallocateIfEmpty)
{
  VASSERT(V_IS_VALID_PTR(this));
  VASSERT(nNewSize >= 0);

  if (nGrowBy != -1)
    m_nGrowBy = nGrowBy;  ///< set new size

  if (nNewSize == 0 && bDeallocateIfEmpty)
  {
    // shrink to nothing
    if (m_pData != NULL)
    {
      VDestructElements<TYPE>(m_pData, m_nSize);
      delete[] (BYTE*)m_pData;
      m_pData = NULL;
    }
    m_nSize = m_nMaxSize = 0;
  }
  else if (m_pData == NULL && nNewSize > 0)
  {
    // Construct first array. 
    m_nMaxSize = hkvMath::Max(nGrowBy, nNewSize);
    m_pData = (TYPE*) new BYTE[m_nMaxSize * sizeof(TYPE)];
    VConstructElementsZeroInit<TYPE>(m_pData, nNewSize);
    m_nSize = nNewSize;
  }
  else if (nNewSize <= m_nMaxSize)
  {
    // it fits
    if (nNewSize > m_nSize)
    {
      // initialize the new elements
      VConstructElementsZeroInit<TYPE>(&m_pData[m_nSize], nNewSize-m_nSize);
    }
    else if (m_nSize > nNewSize)
    {
      // destroy the old elements
      VDestructElements<TYPE>(&m_pData[nNewSize], m_nSize-nNewSize);
    }
    m_nSize = nNewSize;
  }
  else
  {
    nGrowBy = m_nGrowBy;
    if (nGrowBy == 0)
    {
      // otherwise, grow array by 1.5 of previous size (minimal growth = 4)
      nGrowBy = m_nMaxSize / 2 < 4 ? 4 : m_nMaxSize / 2;
    }
    int nNewMax = m_nMaxSize + nGrowBy;
    if (nNewSize > nNewMax)
    {
      nNewMax = nNewSize;
    }

    VASSERT(nNewMax >= m_nMaxSize);  ///< no wrap around
    TYPE* pNewData = (TYPE*) new BYTE[nNewMax * sizeof(TYPE)];

    // copy new data from old
    memcpy(pNewData, m_pData, m_nSize * sizeof(TYPE));

    // construct remaining elements
    VASSERT(nNewSize > m_nSize);
    VConstructElementsZeroInit<TYPE>(&pNewData[m_nSize], nNewSize-m_nSize);

    // get rid of old stuff (note: no destructors called)
    delete[] (BYTE*)m_pData;
    m_pData = pNewData;
    m_nSize = nNewSize;
    m_nMaxSize = nNewMax;
  }
}