1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/core/CellArray.h Sun Mar 30 07:00:22 2008 +0000
1.3 @@ -0,0 +1,196 @@
1.4 +/**
1.5 + * vim: set ts=4 :
1.6 + * =============================================================================
1.7 + * SourceMod
1.8 + * Copyright (C) 2004-2008 AlliedModders LLC. All rights reserved.
1.9 + * =============================================================================
1.10 + *
1.11 + * This program is free software; you can redistribute it and/or modify it under
1.12 + * the terms of the GNU General Public License, version 3.0, as published by the
1.13 + * Free Software Foundation.
1.14 + *
1.15 + * This program is distributed in the hope that it will be useful, but WITHOUT
1.16 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
1.17 + * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
1.18 + * details.
1.19 + *
1.20 + * You should have received a copy of the GNU General Public License along with
1.21 + * this program. If not, see <http://www.gnu.org/licenses/>.
1.22 + *
1.23 + * As a special exception, AlliedModders LLC gives you permission to link the
1.24 + * code of this program (as well as its derivative works) to "Half-Life 2," the
1.25 + * "Source Engine," the "SourcePawn JIT," and any Game MODs that run on software
1.26 + * by the Valve Corporation. You must obey the GNU General Public License in
1.27 + * all respects for all other code used. Additionally, AlliedModders LLC grants
1.28 + * this exception to all derivative works. AlliedModders LLC defines further
1.29 + * exceptions, found in LICENSE.txt (as of this writing, version JULY-31-2007),
1.30 + * or <http://www.sourcemod.net/license.php>.
1.31 + *
1.32 + * Version: $Id$
1.33 + */
1.34 +
1.35 +#include <malloc.h>
1.36 +#include <string.h>
1.37 +
1.38 +extern HandleType_t htCellArray;
1.39 +
1.40 +class CellArray
1.41 +{
1.42 +public:
1.43 + CellArray(size_t blocksize) : m_Data(NULL), m_BlockSize(blocksize), m_AllocSize(0), m_Size(0)
1.44 + {
1.45 + }
1.46 + ~CellArray()
1.47 + {
1.48 + free(m_Data);
1.49 + }
1.50 + size_t size() const
1.51 + {
1.52 + return m_Size;
1.53 + }
1.54 + cell_t *push()
1.55 + {
1.56 + if (!GrowIfNeeded(1))
1.57 + {
1.58 + return NULL;
1.59 + }
1.60 + cell_t *arr = &m_Data[m_Size * m_BlockSize];
1.61 + m_Size++;
1.62 + return arr;
1.63 + }
1.64 + cell_t *at(size_t b) const
1.65 + {
1.66 + return &m_Data[b * m_BlockSize];
1.67 + }
1.68 + size_t blocksize() const
1.69 + {
1.70 + return m_BlockSize;
1.71 + }
1.72 + void clear()
1.73 + {
1.74 + m_Size = 0;
1.75 + }
1.76 + bool swap(size_t item1, size_t item2)
1.77 + {
1.78 + /* Make sure there is extra space available */
1.79 + if (!GrowIfNeeded(1))
1.80 + {
1.81 + return false;
1.82 + }
1.83 +
1.84 + cell_t *pri = at(item1);
1.85 + cell_t *alt = at(item2);
1.86 +
1.87 + /* Get our temporary array 1 after the limit */
1.88 + cell_t *temp = &m_Data[m_Size * m_BlockSize];
1.89 +
1.90 + memcpy(temp, pri, sizeof(cell_t) * m_BlockSize);
1.91 + memcpy(pri, alt, sizeof(cell_t) * m_BlockSize);
1.92 + memcpy(alt, temp, sizeof(cell_t) * m_BlockSize);
1.93 +
1.94 + return true;
1.95 + }
1.96 + void remove(size_t index)
1.97 + {
1.98 + /* If we're at the end, take the easy way out */
1.99 + if (index == m_Size - 1)
1.100 + {
1.101 + m_Size--;
1.102 + return;
1.103 + }
1.104 +
1.105 + /* Otherwise, it's time to move stuff! */
1.106 + size_t remaining_indexes = (m_Size - 1) - index;
1.107 + cell_t *src = at(index + 1);
1.108 + cell_t *dest = at(index);
1.109 + memmove(dest, src, sizeof(cell_t) * m_BlockSize * remaining_indexes);
1.110 +
1.111 + m_Size--;
1.112 + }
1.113 + cell_t *insert_at(size_t index)
1.114 + {
1.115 + /* Make sure it'll fit */
1.116 + if (!GrowIfNeeded(1))
1.117 + {
1.118 + return NULL;
1.119 + }
1.120 +
1.121 + /* move everything up */
1.122 + cell_t *src = at(index);
1.123 + cell_t *dst = at(index + 1);
1.124 + memmove(dst, src, sizeof(cell_t) * m_BlockSize * (m_Size-index));
1.125 +
1.126 + m_Size++;
1.127 +
1.128 + return src;
1.129 + }
1.130 + bool resize(size_t count)
1.131 + {
1.132 + if (count <= m_Size)
1.133 + {
1.134 + m_Size = count;
1.135 + return true;
1.136 + }
1.137 +
1.138 + if(!GrowIfNeeded(count - m_Size))
1.139 + {
1.140 + return false;
1.141 + }
1.142 +
1.143 + m_Size = count;
1.144 + return true;
1.145 + }
1.146 +
1.147 + CellArray *clone()
1.148 + {
1.149 + CellArray *array = new CellArray(m_BlockSize);
1.150 + array->m_AllocSize = m_AllocSize;
1.151 + array->m_Size = m_Size;
1.152 + array->m_Data = (cell_t *)malloc(sizeof(cell_t) * m_BlockSize * m_AllocSize);
1.153 + memcpy(array->m_Data, m_Data, sizeof(cell_t) * m_BlockSize * m_Size);
1.154 + return array;
1.155 + }
1.156 +
1.157 + cell_t *base()
1.158 + {
1.159 + return m_Data;
1.160 + }
1.161 +
1.162 + size_t mem_usage()
1.163 + {
1.164 + return m_AllocSize * m_BlockSize * sizeof(cell_t);
1.165 + }
1.166 +
1.167 +private:
1.168 + bool GrowIfNeeded(size_t count)
1.169 + {
1.170 + /* Shortcut out if we can store this */
1.171 + if (m_Size + count <= m_AllocSize)
1.172 + {
1.173 + return true;
1.174 + }
1.175 + /* Set a base allocation size of 8 items */
1.176 + if (!m_AllocSize)
1.177 + {
1.178 + m_AllocSize = 8;
1.179 + }
1.180 + /* If it's not enough, keep doubling */
1.181 + while (m_Size + count > m_AllocSize)
1.182 + {
1.183 + m_AllocSize *= 2;
1.184 + }
1.185 + /* finally, allocate the new block */
1.186 + if (m_Data)
1.187 + {
1.188 + m_Data = (cell_t *)realloc(m_Data, sizeof(cell_t) * m_BlockSize * m_AllocSize);
1.189 + } else {
1.190 + m_Data = (cell_t *)malloc(sizeof(cell_t) * m_BlockSize * m_AllocSize);
1.191 + }
1.192 + return (m_Data != NULL);
1.193 + }
1.194 +private:
1.195 + cell_t *m_Data;
1.196 + size_t m_BlockSize;
1.197 + size_t m_AllocSize;
1.198 + size_t m_Size;
1.199 +};