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