瀏覽代碼

core: implement Array::Resize() to forcibly set the size of an array.

legacy
Sam Hocevar sam 12 年之前
父節點
當前提交
518942d4ca
共有 1 個文件被更改,包括 17 次插入0 次删除
  1. +17
    -0
      src/array.h

+ 17
- 0
src/array.h 查看文件

@@ -157,6 +157,8 @@ public:

void Remove(int pos, int todelete = 1)
{
/* FIXME: we need to call dtors for the first
* todelete elements here */
for (int i = pos; i + todelete < m_count; i++)
m_data[i] = m_data[i + todelete];
for (int i = m_count - todelete; i < m_count; i++)
@@ -164,6 +166,21 @@ public:
m_count -= todelete;
}

void Resize(int count, Element e = Element())
{
Reserve(count);

/* Too many elements? Remove them. */
for (int i = count; i < m_count; ++i)
m_data[i].~Element();

/* Not enough elements? Add some. */
for (int i = m_count; i < count; ++i)
m_data[i] = e;

m_count = count;
}

inline void Empty()
{
Remove(0, m_count);


Loading…
取消
儲存