3 * =============================================================================
5 * Copyright (C) 2004-2007 AlliedModders LLC. All rights reserved.
6 * =============================================================================
8 * This program is free software; you can redistribute it and/or modify it under
9 * the terms of the GNU General Public License, version 3.0, as published by the
10 * Free Software Foundation.
12 * This program is distributed in the hope that it will be useful, but WITHOUT
13 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
14 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
17 * You should have received a copy of the GNU General Public License along with
18 * this program. If not, see <http://www.gnu.org/licenses/>.
20 * As a special exception, AlliedModders LLC gives you permission to link the
21 * code of this program (as well as its derivative works) to "Half-Life 2," the
22 * "Source Engine," the "SourcePawn JIT," and any Game MODs that run on software
23 * by the Valve Corporation. You must obey the GNU General Public License in
24 * all respects for all other code used. Additionally, AlliedModders LLC grants
25 * this exception to all derivative works. AlliedModders LLC defines further
26 * exceptions, found in LICENSE.txt (as of this writing, version JULY-31-2007),
27 * or <http://www.sourcemod.net/license.php>.
35 extern HandleType_t htCellArray;
40 CellArray(size_t blocksize) : m_Data(NULL), m_BlockSize(blocksize), m_AllocSize(0), m_Size(0)
57 cell_t *arr = &m_Data[m_Size * m_BlockSize];
61 cell_t *at(size_t b) const
63 return &m_Data[b * m_BlockSize];
65 size_t blocksize() const
73 bool swap(size_t item1, size_t item2)
75 /* Make sure there is extra space available */
81 cell_t *pri = at(item1);
82 cell_t *alt = at(item2);
84 /* Get our temporary array 1 after the limit */
85 cell_t *temp = &m_Data[m_Size * m_BlockSize];
87 memcpy(temp, pri, sizeof(cell_t) * m_BlockSize);
88 memcpy(pri, alt, sizeof(cell_t) * m_BlockSize);
89 memcpy(alt, temp, sizeof(cell_t) * m_BlockSize);
93 void remove(size_t index)
95 /* If we're at the end, take the easy way out */
96 if (index == m_Size - 1)
102 /* Otherwise, it's time to move stuff! */
103 size_t remaining_indexes = (m_Size - 1) - index;
104 cell_t *src = at(index + 1);
105 cell_t *dest = at(index);
106 memmove(dest, src, sizeof(cell_t) * m_BlockSize * remaining_indexes);
110 cell_t *insert_at(size_t index)
112 /* Make sure it'll fit */
113 if (!GrowIfNeeded(1))
118 /* move everything up */
119 cell_t *src = at(index);
120 cell_t *dst = at(index + 1);
121 memmove(dst, src, sizeof(cell_t) * m_BlockSize * (m_Size-index));
127 bool resize(size_t count)
135 if(!GrowIfNeeded(count - m_Size))
146 CellArray *array = new CellArray(m_BlockSize);
147 array->m_AllocSize = m_AllocSize;
148 array->m_Size = m_Size;
149 array->m_Data = (cell_t *)malloc(sizeof(cell_t) * m_BlockSize * m_AllocSize);
150 memcpy(array->m_Data, m_Data, sizeof(cell_t) * m_BlockSize * m_Size);
161 return m_AllocSize * m_BlockSize * sizeof(cell_t);
165 bool GrowIfNeeded(size_t count)
167 /* Shortcut out if we can store this */
168 if (m_Size + count <= m_AllocSize)
172 /* Set a base allocation size of 8 items */
177 /* If it's not enough, keep doubling */
178 while (m_Size + count > m_AllocSize)
182 /* finally, allocate the new block */
185 m_Data = (cell_t *)realloc(m_Data, sizeof(cell_t) * m_BlockSize * m_AllocSize);
187 m_Data = (cell_t *)malloc(sizeof(cell_t) * m_BlockSize * m_AllocSize);
189 return (m_Data != NULL);