core/CellArray.h
author David Anderson <dvander@alliedmods.net>
Mon Sep 29 20:02:19 2008 -0700 (2008-09-29)
changeset 2335 cf312a67fb8f
parent 2289 44045ead61cd
child 2471 194be299b700
permissions -rw-r--r--
Fixed OnConfigsExecuted() not working on listen servers (bug 3287, r=me).
     1 /**
     2  * vim: set ts=4 :
     3  * =============================================================================
     4  * SourceMod
     5  * Copyright (C) 2004-2008 AlliedModders LLC.  All rights reserved.
     6  * =============================================================================
     7  *
     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.
    11  * 
    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
    15  * details.
    16  *
    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/>.
    19  *
    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>.
    28  *
    29  * Version: $Id$
    30  */
    31 
    32 #include <malloc.h>
    33 #include <string.h>
    34 
    35 extern HandleType_t htCellArray;
    36 
    37 class CellArray
    38 {
    39 public:
    40 	CellArray(size_t blocksize) : m_Data(NULL), m_BlockSize(blocksize), m_AllocSize(0), m_Size(0)
    41 	{
    42 	}
    43 
    44 	~CellArray()
    45 	{
    46 		free(m_Data);
    47 	}
    48 
    49 	size_t size() const
    50 	{
    51 		return m_Size;
    52 	}
    53 
    54 	cell_t *push()
    55 	{
    56 		if (!GrowIfNeeded(1))
    57 		{
    58 			return NULL;
    59 		}
    60 		cell_t *arr = &m_Data[m_Size * m_BlockSize];
    61 		m_Size++;
    62 		return arr;
    63 	}
    64 
    65 	cell_t *at(size_t b) const
    66 	{
    67 		return &m_Data[b * m_BlockSize];
    68 	}
    69 
    70 	size_t blocksize() const
    71 	{
    72 		return m_BlockSize;
    73 	}
    74 
    75 	void clear()
    76 	{
    77 		m_Size = 0;
    78 	}
    79 
    80 	bool swap(size_t item1, size_t item2)
    81 	{
    82 		/* Make sure there is extra space available */
    83 		if (!GrowIfNeeded(1))
    84 		{
    85 			return false;
    86 		}
    87 
    88 		cell_t *pri = at(item1);
    89 		cell_t *alt = at(item2);
    90 
    91 		/* Get our temporary array 1 after the limit */
    92 		cell_t *temp = &m_Data[m_Size * m_BlockSize];
    93 
    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);
    97 
    98 		return true;
    99 	}
   100 
   101 	void remove(size_t index)
   102 	{
   103 		/* If we're at the end, take the easy way out */
   104 		if (index == m_Size - 1)
   105 		{
   106 			m_Size--;
   107 			return;
   108 		}
   109 
   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);
   115 
   116 		m_Size--;
   117 	}
   118 
   119 	cell_t *insert_at(size_t index)
   120 	{
   121 		/* Make sure it'll fit */
   122 		if (!GrowIfNeeded(1))
   123 		{
   124 			return NULL;
   125 		}
   126 
   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));
   131 
   132 		m_Size++;
   133 
   134 		return src;
   135 	}
   136 
   137 	bool resize(size_t count)
   138 	{
   139 		if (count <= m_Size)
   140 		{
   141 			m_Size = count;
   142 			return true;
   143 		}
   144 
   145 		if (!GrowIfNeeded(count - m_Size))
   146 		{
   147 			return false;
   148 		}
   149 
   150 		m_Size = count;
   151 		return true;
   152 	}
   153 
   154 	CellArray *clone()
   155 	{
   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);
   161 		return array;
   162 	}
   163 
   164 	cell_t *base()
   165 	{
   166 		return m_Data;
   167 	}
   168 
   169 	size_t mem_usage()
   170 	{
   171 		return m_AllocSize * m_BlockSize * sizeof(cell_t);
   172 	}
   173 
   174 private:
   175 	bool GrowIfNeeded(size_t count)
   176 	{
   177 		/* Shortcut out if we can store this */
   178 		if (m_Size + count <= m_AllocSize)
   179 		{
   180 			return true;
   181 		}
   182 		/* Set a base allocation size of 8 items */
   183 		if (!m_AllocSize)
   184 		{
   185 			m_AllocSize = 8;
   186 		}
   187 		/* If it's not enough, keep doubling */
   188 		while (m_Size + count > m_AllocSize)
   189 		{
   190 			m_AllocSize *= 2;
   191 		}
   192 		/* finally, allocate the new block */
   193 		if (m_Data)
   194 		{
   195 			m_Data = (cell_t *)realloc(m_Data, sizeof(cell_t) * m_BlockSize * m_AllocSize);
   196 		} else {
   197 			m_Data = (cell_t *)malloc(sizeof(cell_t) * m_BlockSize * m_AllocSize);
   198 		}
   199 		return (m_Data != NULL);
   200 	}
   201 private:
   202 	cell_t *m_Data;
   203 	size_t m_BlockSize;
   204 	size_t m_AllocSize;
   205 	size_t m_Size;
   206 };