core/CellArray.h
author Phong Dang <teame06@gmail.com>
Wed Aug 01 07:29:40 2007 +0000 (2007-08-01)
changeset 1241 2e8046793bc9
parent 1231 17badaf66ce4
child 1330 ab94316bb52b
permissions -rw-r--r--
Added check to make sure player are alive to perform sm_slap, sm_burn, sm_slay.
     1 /**
     2  * vim: set ts=4 :
     3  * ================================================================
     4  * SourceMod
     5  * Copyright (C) 2004-2007 AlliedModders LLC.  All rights reserved.
     6  * ================================================================
     7  *
     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.
    11  * 
    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.
    16  *
    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/>.
    19  *
    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>.
    29  *
    30  * Version: $Id$
    31  */
    32 
    33 #include <malloc.h>
    34 #include <string.h>
    35 
    36 class CellArray
    37 {
    38 public:
    39 	CellArray(size_t blocksize) : m_Data(NULL), m_BlockSize(blocksize), m_AllocSize(0), m_Size(0)
    40 	{
    41 	}
    42 	~CellArray()
    43 	{
    44 		free(m_Data);
    45 	}
    46 	size_t size() const
    47 	{
    48 		return m_Size;
    49 	}
    50 	cell_t *push()
    51 	{
    52 		if (!GrowIfNeeded(1))
    53 		{
    54 			return NULL;
    55 		}
    56 		cell_t *arr = &m_Data[m_Size * m_BlockSize];
    57 		m_Size++;
    58 		return arr;
    59 	}
    60 	cell_t *at(size_t b) const
    61 	{
    62 		return &m_Data[b * m_BlockSize];
    63 	}
    64 	size_t blocksize() const
    65 	{
    66 		return m_BlockSize;
    67 	}
    68 	void clear()
    69 	{
    70 		m_Size = 0;
    71 	}
    72 	bool swap(size_t item1, size_t item2)
    73 	{
    74 		/* Make sure there is extra space available */
    75 		if (!GrowIfNeeded(1))
    76 		{
    77 			return false;
    78 		}
    79 
    80 		cell_t *pri = at(item1);
    81 		cell_t *alt = at(item2);
    82 
    83 		/* Get our temporary array 1 after the limit */
    84 		cell_t *temp = &m_Data[m_Size * m_BlockSize];
    85 
    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);
    89 
    90 		return true;
    91 	}
    92 	void remove(size_t index)
    93 	{
    94 		/* If we're at the end, take the easy way out */
    95 		if (index == m_Size - 1)
    96 		{
    97 			m_Size--;
    98 			return;
    99 		}
   100 
   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);
   106 
   107 		m_Size--;
   108 	}
   109 	cell_t *insert_at(size_t index)
   110 	{
   111 		/* Make sure it'll fit */
   112 		if (!GrowIfNeeded(1))
   113 		{
   114 			return NULL;
   115 		}
   116 
   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);
   121 
   122 		m_Size++;
   123 
   124 		return src;
   125 	}
   126 	bool resize(size_t count)
   127 	{
   128 		if (count <= m_Size)
   129 		{
   130 			m_Size = count;
   131 			return true;
   132 		}
   133 
   134 		if(!GrowIfNeeded(count - m_Size))
   135 		{
   136 			return false;
   137 		}
   138 
   139 		m_Size = count;
   140 		return true;
   141 	}
   142 private:
   143 	bool GrowIfNeeded(size_t count)
   144 	{
   145 		/* Shortcut out if we can store this */
   146 		if (m_Size + count <= m_AllocSize)
   147 		{
   148 			return true;
   149 		}
   150 		/* Set a base allocation size of 8 items */
   151 		if (!m_AllocSize)
   152 		{
   153 			m_AllocSize = 8;
   154 		}
   155 		/* If it's not enough, keep doubling */
   156 		while (m_Size + count > m_AllocSize)
   157 		{
   158 			m_AllocSize *= 2;
   159 		}
   160 		/* finally, allocate the new block */
   161 		if (m_Data)
   162 		{
   163 			m_Data = (cell_t *)realloc(m_Data, sizeof(cell_t) * m_BlockSize * m_AllocSize);
   164 		} else {
   165 			m_Data = (cell_t *)malloc(sizeof(cell_t) * m_BlockSize * m_AllocSize);
   166 		}
   167 		return (m_Data != NULL);
   168 	}
   169 private:
   170 	cell_t *m_Data;
   171 	size_t m_BlockSize;
   172 	size_t m_AllocSize;
   173 	size_t m_Size;
   174 };