Merge.
3 * =============================================================================
5 * Copyright (C) 2004-2008 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)
60 cell_t *arr = &m_Data[m_Size * m_BlockSize];
65 cell_t *at(size_t b) const
67 return &m_Data[b * m_BlockSize];
70 size_t blocksize() const
80 bool swap(size_t item1, size_t item2)
82 /* Make sure there is extra space available */
88 cell_t *pri = at(item1);
89 cell_t *alt = at(item2);
91 /* Get our temporary array 1 after the limit */
92 cell_t *temp = &m_Data[m_Size * m_BlockSize];
94 memcpy(temp, pri, sizeof(cell_t) * m_BlockSize);
95 memcpy(pri, alt, sizeof(cell_t) * m_BlockSize);
96 memcpy(alt, temp, sizeof(cell_t) * m_BlockSize);
101 void remove(size_t index)
103 /* If we're at the end, take the easy way out */
104 if (index == m_Size - 1)
110 /* Otherwise, it's time to move stuff! */
111 size_t remaining_indexes = (m_Size - 1) - index;
112 cell_t *src = at(index + 1);
113 cell_t *dest = at(index);
114 memmove(dest, src, sizeof(cell_t) * m_BlockSize * remaining_indexes);
119 cell_t *insert_at(size_t index)
121 /* Make sure it'll fit */
122 if (!GrowIfNeeded(1))
127 /* move everything up */
128 cell_t *src = at(index);
129 cell_t *dst = at(index + 1);
130 memmove(dst, src, sizeof(cell_t) * m_BlockSize * (m_Size-index));
137 bool resize(size_t count)
145 if (!GrowIfNeeded(count - m_Size))
156 CellArray *array = new CellArray(m_BlockSize);
157 array->m_AllocSize = m_AllocSize;
158 array->m_Size = m_Size;
159 array->m_Data = (cell_t *)malloc(sizeof(cell_t) * m_BlockSize * m_AllocSize);
160 memcpy(array->m_Data, m_Data, sizeof(cell_t) * m_BlockSize * m_Size);
171 return m_AllocSize * m_BlockSize * sizeof(cell_t);
175 bool GrowIfNeeded(size_t count)
177 /* Shortcut out if we can store this */
178 if (m_Size + count <= m_AllocSize)
182 /* Set a base allocation size of 8 items */
187 /* If it's not enough, keep doubling */
188 while (m_Size + count > m_AllocSize)
192 /* finally, allocate the new block */
195 m_Data = (cell_t *)realloc(m_Data, sizeof(cell_t) * m_BlockSize * m_AllocSize);
197 m_Data = (cell_t *)malloc(sizeof(cell_t) * m_BlockSize * m_AllocSize);
199 return (m_Data != NULL);