core/CellArray.h
author Matt Woodrow <pred@alliedmods.net>
Fri Aug 08 00:17:37 2008 +0000 (2008-08-08)
changeset 2224 676c3b826f13
parent 1999 63aafc6ff212
child 2289 44045ead61cd
permissions -rw-r--r--
Initial Import of beta structs extension.
ds@1952
     1
/**
ds@1952
     2
 * vim: set ts=4 :
ds@1952
     3
 * =============================================================================
ds@1952
     4
 * SourceMod
ds@1952
     5
 * Copyright (C) 2004-2008 AlliedModders LLC.  All rights reserved.
ds@1952
     6
 * =============================================================================
ds@1952
     7
 *
ds@1952
     8
 * This program is free software; you can redistribute it and/or modify it under
ds@1952
     9
 * the terms of the GNU General Public License, version 3.0, as published by the
ds@1952
    10
 * Free Software Foundation.
ds@1952
    11
 * 
ds@1952
    12
 * This program is distributed in the hope that it will be useful, but WITHOUT
ds@1952
    13
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
ds@1952
    14
 * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
ds@1952
    15
 * details.
ds@1952
    16
 *
ds@1952
    17
 * You should have received a copy of the GNU General Public License along with
ds@1952
    18
 * this program.  If not, see <http://www.gnu.org/licenses/>.
ds@1952
    19
 *
ds@1952
    20
 * As a special exception, AlliedModders LLC gives you permission to link the
ds@1952
    21
 * code of this program (as well as its derivative works) to "Half-Life 2," the
ds@1952
    22
 * "Source Engine," the "SourcePawn JIT," and any Game MODs that run on software
ds@1952
    23
 * by the Valve Corporation.  You must obey the GNU General Public License in
ds@1952
    24
 * all respects for all other code used.  Additionally, AlliedModders LLC grants
ds@1952
    25
 * this exception to all derivative works.  AlliedModders LLC defines further
ds@1952
    26
 * exceptions, found in LICENSE.txt (as of this writing, version JULY-31-2007),
ds@1952
    27
 * or <http://www.sourcemod.net/license.php>.
ds@1952
    28
 *
ds@1952
    29
 * Version: $Id$
ds@1952
    30
 */
ds@1952
    31
ds@1952
    32
#include <malloc.h>
ds@1952
    33
#include <string.h>
ds@1952
    34
ds@1952
    35
extern HandleType_t htCellArray;
ds@1952
    36
ds@1952
    37
class CellArray
ds@1952
    38
{
ds@1952
    39
public:
ds@1952
    40
	CellArray(size_t blocksize) : m_Data(NULL), m_BlockSize(blocksize), m_AllocSize(0), m_Size(0)
ds@1952
    41
	{
ds@1952
    42
	}
dvander@1999
    43
ds@1952
    44
	~CellArray()
ds@1952
    45
	{
ds@1952
    46
		free(m_Data);
ds@1952
    47
	}
dvander@1999
    48
ds@1952
    49
	size_t size() const
ds@1952
    50
	{
ds@1952
    51
		return m_Size;
ds@1952
    52
	}
dvander@1999
    53
ds@1952
    54
	cell_t *push()
ds@1952
    55
	{
ds@1952
    56
		if (!GrowIfNeeded(1))
ds@1952
    57
		{
ds@1952
    58
			return NULL;
ds@1952
    59
		}
ds@1952
    60
		cell_t *arr = &m_Data[m_Size * m_BlockSize];
ds@1952
    61
		m_Size++;
ds@1952
    62
		return arr;
ds@1952
    63
	}
dvander@1999
    64
ds@1952
    65
	cell_t *at(size_t b) const
ds@1952
    66
	{
ds@1952
    67
		return &m_Data[b * m_BlockSize];
ds@1952
    68
	}
dvander@1999
    69
ds@1952
    70
	size_t blocksize() const
ds@1952
    71
	{
ds@1952
    72
		return m_BlockSize;
ds@1952
    73
	}
dvander@1999
    74
ds@1952
    75
	void clear()
ds@1952
    76
	{
ds@1952
    77
		m_Size = 0;
ds@1952
    78
	}
dvander@1999
    79
ds@1952
    80
	bool swap(size_t item1, size_t item2)
ds@1952
    81
	{
ds@1952
    82
		/* Make sure there is extra space available */
ds@1952
    83
		if (!GrowIfNeeded(1))
ds@1952
    84
		{
ds@1952
    85
			return false;
ds@1952
    86
		}
ds@1952
    87
ds@1952
    88
		cell_t *pri = at(item1);
ds@1952
    89
		cell_t *alt = at(item2);
ds@1952
    90
ds@1952
    91
		/* Get our temporary array 1 after the limit */
ds@1952
    92
		cell_t *temp = &m_Data[m_Size * m_BlockSize];
ds@1952
    93
ds@1952
    94
		memcpy(temp, pri, sizeof(cell_t) * m_BlockSize);
ds@1952
    95
		memcpy(pri, alt, sizeof(cell_t) * m_BlockSize);
ds@1952
    96
		memcpy(alt, temp, sizeof(cell_t) * m_BlockSize);
ds@1952
    97
ds@1952
    98
		return true;
ds@1952
    99
	}
dvander@1999
   100
ds@1952
   101
	void remove(size_t index)
ds@1952
   102
	{
ds@1952
   103
		/* If we're at the end, take the easy way out */
ds@1952
   104
		if (index == m_Size - 1)
ds@1952
   105
		{
ds@1952
   106
			m_Size--;
ds@1952
   107
			return;
ds@1952
   108
		}
ds@1952
   109
ds@1952
   110
		/* Otherwise, it's time to move stuff! */
ds@1952
   111
		size_t remaining_indexes = (m_Size - 1) - index;
ds@1952
   112
		cell_t *src = at(index + 1);
ds@1952
   113
		cell_t *dest = at(index);
ds@1952
   114
		memmove(dest, src, sizeof(cell_t) * m_BlockSize * remaining_indexes);
ds@1952
   115
ds@1952
   116
		m_Size--;
ds@1952
   117
	}
dvander@1999
   118
ds@1952
   119
	cell_t *insert_at(size_t index)
ds@1952
   120
	{
ds@1952
   121
		/* Make sure it'll fit */
ds@1952
   122
		if (!GrowIfNeeded(1))
ds@1952
   123
		{
ds@1952
   124
			return NULL;
ds@1952
   125
		}
ds@1952
   126
ds@1952
   127
		/* move everything up */
ds@1952
   128
		cell_t *src = at(index);
ds@1952
   129
		cell_t *dst = at(index + 1);
ds@1952
   130
		memmove(dst, src, sizeof(cell_t) * m_BlockSize * (m_Size-index));
ds@1952
   131
ds@1952
   132
		m_Size++;
ds@1952
   133
ds@1952
   134
		return src;
ds@1952
   135
	}
dvander@1999
   136
ds@1952
   137
	bool resize(size_t count)
ds@1952
   138
	{
ds@1952
   139
		if (count <= m_Size)
ds@1952
   140
		{
ds@1952
   141
			m_Size = count;
ds@1952
   142
			return true;
ds@1952
   143
		}
ds@1952
   144
dvander@1999
   145
		if (!GrowIfNeeded(count - m_Size))
ds@1952
   146
		{
ds@1952
   147
			return false;
ds@1952
   148
		}
ds@1952
   149
ds@1952
   150
		m_Size = count;
ds@1952
   151
		return true;
ds@1952
   152
	}
ds@1952
   153
ds@1952
   154
	CellArray *clone()
ds@1952
   155
	{
ds@1952
   156
		CellArray *array = new CellArray(m_BlockSize);
ds@1952
   157
		array->m_AllocSize = m_AllocSize;
ds@1952
   158
		array->m_Size = m_Size;
ds@1952
   159
		array->m_Data = (cell_t *)malloc(sizeof(cell_t) * m_BlockSize * m_AllocSize);
ds@1952
   160
		memcpy(array->m_Data, m_Data, sizeof(cell_t) * m_BlockSize * m_Size);
ds@1952
   161
		return array;
ds@1952
   162
	}
ds@1952
   163
ds@1952
   164
	cell_t *base()
ds@1952
   165
	{
ds@1952
   166
		return m_Data;
ds@1952
   167
	}
ds@1952
   168
ds@1952
   169
	size_t mem_usage()
ds@1952
   170
	{
ds@1952
   171
		return m_AllocSize * m_BlockSize * sizeof(cell_t);
ds@1952
   172
	}
ds@1952
   173
ds@1952
   174
private:
ds@1952
   175
	bool GrowIfNeeded(size_t count)
ds@1952
   176
	{
ds@1952
   177
		/* Shortcut out if we can store this */
ds@1952
   178
		if (m_Size + count <= m_AllocSize)
ds@1952
   179
		{
ds@1952
   180
			return true;
ds@1952
   181
		}
ds@1952
   182
		/* Set a base allocation size of 8 items */
ds@1952
   183
		if (!m_AllocSize)
ds@1952
   184
		{
ds@1952
   185
			m_AllocSize = 8;
ds@1952
   186
		}
ds@1952
   187
		/* If it's not enough, keep doubling */
ds@1952
   188
		while (m_Size + count > m_AllocSize)
ds@1952
   189
		{
ds@1952
   190
			m_AllocSize *= 2;
ds@1952
   191
		}
ds@1952
   192
		/* finally, allocate the new block */
ds@1952
   193
		if (m_Data)
ds@1952
   194
		{
ds@1952
   195
			m_Data = (cell_t *)realloc(m_Data, sizeof(cell_t) * m_BlockSize * m_AllocSize);
ds@1952
   196
		} else {
ds@1952
   197
			m_Data = (cell_t *)malloc(sizeof(cell_t) * m_BlockSize * m_AllocSize);
ds@1952
   198
		}
ds@1952
   199
		return (m_Data != NULL);
ds@1952
   200
	}
ds@1952
   201
private:
ds@1952
   202
	cell_t *m_Data;
ds@1952
   203
	size_t m_BlockSize;
ds@1952
   204
	size_t m_AllocSize;
ds@1952
   205
	size_t m_Size;
ds@1952
   206
};