ds@1952: /**
ds@1952: * vim: set ts=4 :
ds@1952: * =============================================================================
ds@1952: * SourceMod
ds@1952: * Copyright (C) 2004-2008 AlliedModders LLC. All rights reserved.
ds@1952: * =============================================================================
ds@1952: *
ds@1952: * This program is free software; you can redistribute it and/or modify it under
ds@1952: * the terms of the GNU General Public License, version 3.0, as published by the
ds@1952: * Free Software Foundation.
ds@1952: *
ds@1952: * This program is distributed in the hope that it will be useful, but WITHOUT
ds@1952: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
ds@1952: * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
ds@1952: * details.
ds@1952: *
ds@1952: * You should have received a copy of the GNU General Public License along with
ds@1952: * this program. If not, see .
ds@1952: *
ds@1952: * As a special exception, AlliedModders LLC gives you permission to link the
ds@1952: * code of this program (as well as its derivative works) to "Half-Life 2," the
ds@1952: * "Source Engine," the "SourcePawn JIT," and any Game MODs that run on software
ds@1952: * by the Valve Corporation. You must obey the GNU General Public License in
ds@1952: * all respects for all other code used. Additionally, AlliedModders LLC grants
ds@1952: * this exception to all derivative works. AlliedModders LLC defines further
ds@1952: * exceptions, found in LICENSE.txt (as of this writing, version JULY-31-2007),
ds@1952: * or .
ds@1952: *
ds@1952: * Version: $Id$
ds@1952: */
ds@1952:
ds@1952: #include
ds@1952: #include
ds@1952:
ds@1952: extern HandleType_t htCellArray;
ds@1952:
ds@1952: class CellArray
ds@1952: {
ds@1952: public:
ds@1952: CellArray(size_t blocksize) : m_Data(NULL), m_BlockSize(blocksize), m_AllocSize(0), m_Size(0)
ds@1952: {
ds@1952: }
dvander@1999:
ds@1952: ~CellArray()
ds@1952: {
ds@1952: free(m_Data);
ds@1952: }
dvander@1999:
ds@1952: size_t size() const
ds@1952: {
ds@1952: return m_Size;
ds@1952: }
dvander@1999:
ds@1952: cell_t *push()
ds@1952: {
ds@1952: if (!GrowIfNeeded(1))
ds@1952: {
ds@1952: return NULL;
ds@1952: }
ds@1952: cell_t *arr = &m_Data[m_Size * m_BlockSize];
ds@1952: m_Size++;
ds@1952: return arr;
ds@1952: }
dvander@1999:
ds@1952: cell_t *at(size_t b) const
ds@1952: {
ds@1952: return &m_Data[b * m_BlockSize];
ds@1952: }
dvander@1999:
ds@1952: size_t blocksize() const
ds@1952: {
ds@1952: return m_BlockSize;
ds@1952: }
dvander@1999:
ds@1952: void clear()
ds@1952: {
ds@1952: m_Size = 0;
ds@1952: }
dvander@1999:
ds@1952: bool swap(size_t item1, size_t item2)
ds@1952: {
ds@1952: /* Make sure there is extra space available */
ds@1952: if (!GrowIfNeeded(1))
ds@1952: {
ds@1952: return false;
ds@1952: }
ds@1952:
ds@1952: cell_t *pri = at(item1);
ds@1952: cell_t *alt = at(item2);
ds@1952:
ds@1952: /* Get our temporary array 1 after the limit */
ds@1952: cell_t *temp = &m_Data[m_Size * m_BlockSize];
ds@1952:
ds@1952: memcpy(temp, pri, sizeof(cell_t) * m_BlockSize);
ds@1952: memcpy(pri, alt, sizeof(cell_t) * m_BlockSize);
ds@1952: memcpy(alt, temp, sizeof(cell_t) * m_BlockSize);
ds@1952:
ds@1952: return true;
ds@1952: }
dvander@1999:
ds@1952: void remove(size_t index)
ds@1952: {
ds@1952: /* If we're at the end, take the easy way out */
ds@1952: if (index == m_Size - 1)
ds@1952: {
ds@1952: m_Size--;
ds@1952: return;
ds@1952: }
ds@1952:
ds@1952: /* Otherwise, it's time to move stuff! */
ds@1952: size_t remaining_indexes = (m_Size - 1) - index;
ds@1952: cell_t *src = at(index + 1);
ds@1952: cell_t *dest = at(index);
ds@1952: memmove(dest, src, sizeof(cell_t) * m_BlockSize * remaining_indexes);
ds@1952:
ds@1952: m_Size--;
ds@1952: }
dvander@1999:
ds@1952: cell_t *insert_at(size_t index)
ds@1952: {
ds@1952: /* Make sure it'll fit */
ds@1952: if (!GrowIfNeeded(1))
ds@1952: {
ds@1952: return NULL;
ds@1952: }
ds@1952:
ds@1952: /* move everything up */
ds@1952: cell_t *src = at(index);
ds@1952: cell_t *dst = at(index + 1);
ds@1952: memmove(dst, src, sizeof(cell_t) * m_BlockSize * (m_Size-index));
ds@1952:
ds@1952: m_Size++;
ds@1952:
ds@1952: return src;
ds@1952: }
dvander@1999:
ds@1952: bool resize(size_t count)
ds@1952: {
ds@1952: if (count <= m_Size)
ds@1952: {
ds@1952: m_Size = count;
ds@1952: return true;
ds@1952: }
ds@1952:
dvander@1999: if (!GrowIfNeeded(count - m_Size))
ds@1952: {
ds@1952: return false;
ds@1952: }
ds@1952:
ds@1952: m_Size = count;
ds@1952: return true;
ds@1952: }
ds@1952:
ds@1952: CellArray *clone()
ds@1952: {
ds@1952: CellArray *array = new CellArray(m_BlockSize);
ds@1952: array->m_AllocSize = m_AllocSize;
ds@1952: array->m_Size = m_Size;
ds@1952: array->m_Data = (cell_t *)malloc(sizeof(cell_t) * m_BlockSize * m_AllocSize);
ds@1952: memcpy(array->m_Data, m_Data, sizeof(cell_t) * m_BlockSize * m_Size);
ds@1952: return array;
ds@1952: }
ds@1952:
ds@1952: cell_t *base()
ds@1952: {
ds@1952: return m_Data;
ds@1952: }
ds@1952:
ds@1952: size_t mem_usage()
ds@1952: {
ds@1952: return m_AllocSize * m_BlockSize * sizeof(cell_t);
ds@1952: }
ds@1952:
ds@1952: private:
ds@1952: bool GrowIfNeeded(size_t count)
ds@1952: {
ds@1952: /* Shortcut out if we can store this */
ds@1952: if (m_Size + count <= m_AllocSize)
ds@1952: {
ds@1952: return true;
ds@1952: }
ds@1952: /* Set a base allocation size of 8 items */
ds@1952: if (!m_AllocSize)
ds@1952: {
ds@1952: m_AllocSize = 8;
ds@1952: }
ds@1952: /* If it's not enough, keep doubling */
ds@1952: while (m_Size + count > m_AllocSize)
ds@1952: {
ds@1952: m_AllocSize *= 2;
ds@1952: }
ds@1952: /* finally, allocate the new block */
ds@1952: if (m_Data)
ds@1952: {
ds@1952: m_Data = (cell_t *)realloc(m_Data, sizeof(cell_t) * m_BlockSize * m_AllocSize);
ds@1952: } else {
ds@1952: m_Data = (cell_t *)malloc(sizeof(cell_t) * m_BlockSize * m_AllocSize);
ds@1952: }
ds@1952: return (m_Data != NULL);
ds@1952: }
ds@1952: private:
ds@1952: cell_t *m_Data;
ds@1952: size_t m_BlockSize;
ds@1952: size_t m_AllocSize;
ds@1952: size_t m_Size;
ds@1952: };