1.1 --- a/core/CellArray.h Fri Jul 11 06:51:21 2008 +0000
1.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000
1.3 @@ -1,206 +0,0 @@
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 -
1.47 - ~CellArray()
1.48 - {
1.49 - free(m_Data);
1.50 - }
1.51 -
1.52 - size_t size() const
1.53 - {
1.54 - return m_Size;
1.55 - }
1.56 -
1.57 - cell_t *push()
1.58 - {
1.59 - if (!GrowIfNeeded(1))
1.60 - {
1.61 - return NULL;
1.62 - }
1.63 - cell_t *arr = &m_Data[m_Size * m_BlockSize];
1.64 - m_Size++;
1.65 - return arr;
1.66 - }
1.67 -
1.68 - cell_t *at(size_t b) const
1.69 - {
1.70 - return &m_Data[b * m_BlockSize];
1.71 - }
1.72 -
1.73 - size_t blocksize() const
1.74 - {
1.75 - return m_BlockSize;
1.76 - }
1.77 -
1.78 - void clear()
1.79 - {
1.80 - m_Size = 0;
1.81 - }
1.82 -
1.83 - bool swap(size_t item1, size_t item2)
1.84 - {
1.85 - /* Make sure there is extra space available */
1.86 - if (!GrowIfNeeded(1))
1.87 - {
1.88 - return false;
1.89 - }
1.90 -
1.91 - cell_t *pri = at(item1);
1.92 - cell_t *alt = at(item2);
1.93 -
1.94 - /* Get our temporary array 1 after the limit */
1.95 - cell_t *temp = &m_Data[m_Size * m_BlockSize];
1.96 -
1.97 - memcpy(temp, pri, sizeof(cell_t) * m_BlockSize);
1.98 - memcpy(pri, alt, sizeof(cell_t) * m_BlockSize);
1.99 - memcpy(alt, temp, sizeof(cell_t) * m_BlockSize);
1.100 -
1.101 - return true;
1.102 - }
1.103 -
1.104 - void remove(size_t index)
1.105 - {
1.106 - /* If we're at the end, take the easy way out */
1.107 - if (index == m_Size - 1)
1.108 - {
1.109 - m_Size--;
1.110 - return;
1.111 - }
1.112 -
1.113 - /* Otherwise, it's time to move stuff! */
1.114 - size_t remaining_indexes = (m_Size - 1) - index;
1.115 - cell_t *src = at(index + 1);
1.116 - cell_t *dest = at(index);
1.117 - memmove(dest, src, sizeof(cell_t) * m_BlockSize * remaining_indexes);
1.118 -
1.119 - m_Size--;
1.120 - }
1.121 -
1.122 - cell_t *insert_at(size_t index)
1.123 - {
1.124 - /* Make sure it'll fit */
1.125 - if (!GrowIfNeeded(1))
1.126 - {
1.127 - return NULL;
1.128 - }
1.129 -
1.130 - /* move everything up */
1.131 - cell_t *src = at(index);
1.132 - cell_t *dst = at(index + 1);
1.133 - memmove(dst, src, sizeof(cell_t) * m_BlockSize * (m_Size-index));
1.134 -
1.135 - m_Size++;
1.136 -
1.137 - return src;
1.138 - }
1.139 -
1.140 - bool resize(size_t count)
1.141 - {
1.142 - if (count <= m_Size)
1.143 - {
1.144 - m_Size = count;
1.145 - return true;
1.146 - }
1.147 -
1.148 - if (!GrowIfNeeded(count - m_Size))
1.149 - {
1.150 - return false;
1.151 - }
1.152 -
1.153 - m_Size = count;
1.154 - return true;
1.155 - }
1.156 -
1.157 - CellArray *clone()
1.158 - {
1.159 - CellArray *array = new CellArray(m_BlockSize);
1.160 - array->m_AllocSize = m_AllocSize;
1.161 - array->m_Size = m_Size;
1.162 - array->m_Data = (cell_t *)malloc(sizeof(cell_t) * m_BlockSize * m_AllocSize);
1.163 - memcpy(array->m_Data, m_Data, sizeof(cell_t) * m_BlockSize * m_Size);
1.164 - return array;
1.165 - }
1.166 -
1.167 - cell_t *base()
1.168 - {
1.169 - return m_Data;
1.170 - }
1.171 -
1.172 - size_t mem_usage()
1.173 - {
1.174 - return m_AllocSize * m_BlockSize * sizeof(cell_t);
1.175 - }
1.176 -
1.177 -private:
1.178 - bool GrowIfNeeded(size_t count)
1.179 - {
1.180 - /* Shortcut out if we can store this */
1.181 - if (m_Size + count <= m_AllocSize)
1.182 - {
1.183 - return true;
1.184 - }
1.185 - /* Set a base allocation size of 8 items */
1.186 - if (!m_AllocSize)
1.187 - {
1.188 - m_AllocSize = 8;
1.189 - }
1.190 - /* If it's not enough, keep doubling */
1.191 - while (m_Size + count > m_AllocSize)
1.192 - {
1.193 - m_AllocSize *= 2;
1.194 - }
1.195 - /* finally, allocate the new block */
1.196 - if (m_Data)
1.197 - {
1.198 - m_Data = (cell_t *)realloc(m_Data, sizeof(cell_t) * m_BlockSize * m_AllocSize);
1.199 - } else {
1.200 - m_Data = (cell_t *)malloc(sizeof(cell_t) * m_BlockSize * m_AllocSize);
1.201 - }
1.202 - return (m_Data != NULL);
1.203 - }
1.204 -private:
1.205 - cell_t *m_Data;
1.206 - size_t m_BlockSize;
1.207 - size_t m_AllocSize;
1.208 - size_t m_Size;
1.209 -};