core/CellArray.h
author David Anderson <dvander@alliedmods.net>
Sun Aug 26 17:41:02 2007 +0000 (2007-08-26)
changeset 1369 bd9ccced1b1a
parent 1330 ab94316bb52b
child 1664 e054be79e28a
permissions -rw-r--r--
fixed amb834 - command callbacks could potentially occur on invalid clients in very rare cases
     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 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 class CellArray
    36 {
    37 public:
    38 	CellArray(size_t blocksize) : m_Data(NULL), m_BlockSize(blocksize), m_AllocSize(0), m_Size(0)
    39 	{
    40 	}
    41 	~CellArray()
    42 	{
    43 		free(m_Data);
    44 	}
    45 	size_t size() const
    46 	{
    47 		return m_Size;
    48 	}
    49 	cell_t *push()
    50 	{
    51 		if (!GrowIfNeeded(1))
    52 		{
    53 			return NULL;
    54 		}
    55 		cell_t *arr = &m_Data[m_Size * m_BlockSize];
    56 		m_Size++;
    57 		return arr;
    58 	}
    59 	cell_t *at(size_t b) const
    60 	{
    61 		return &m_Data[b * m_BlockSize];
    62 	}
    63 	size_t blocksize() const
    64 	{
    65 		return m_BlockSize;
    66 	}
    67 	void clear()
    68 	{
    69 		m_Size = 0;
    70 	}
    71 	bool swap(size_t item1, size_t item2)
    72 	{
    73 		/* Make sure there is extra space available */
    74 		if (!GrowIfNeeded(1))
    75 		{
    76 			return false;
    77 		}
    78 
    79 		cell_t *pri = at(item1);
    80 		cell_t *alt = at(item2);
    81 
    82 		/* Get our temporary array 1 after the limit */
    83 		cell_t *temp = &m_Data[m_Size * m_BlockSize];
    84 
    85 		memcpy(temp, pri, sizeof(cell_t) * m_BlockSize);
    86 		memcpy(pri, alt, sizeof(cell_t) * m_BlockSize);
    87 		memcpy(alt, temp, sizeof(cell_t) * m_BlockSize);
    88 
    89 		return true;
    90 	}
    91 	void remove(size_t index)
    92 	{
    93 		/* If we're at the end, take the easy way out */
    94 		if (index == m_Size - 1)
    95 		{
    96 			m_Size--;
    97 			return;
    98 		}
    99 
   100 		/* Otherwise, it's time to move stuff! */
   101 		size_t remaining_indexes = (m_Size - 1) - index;
   102 		cell_t *src = at(index + 1);
   103 		cell_t *dest = at(index);
   104 		memmove(dest, src, sizeof(cell_t) * m_BlockSize * remaining_indexes);
   105 
   106 		m_Size--;
   107 	}
   108 	cell_t *insert_at(size_t index)
   109 	{
   110 		/* Make sure it'll fit */
   111 		if (!GrowIfNeeded(1))
   112 		{
   113 			return NULL;
   114 		}
   115 
   116 		/* move everything up */
   117 		cell_t *src = at(index);
   118 		cell_t *dst = at(index + 1);
   119 		memmove(dst, src, sizeof(cell_t) * m_BlockSize * m_Size);
   120 
   121 		m_Size++;
   122 
   123 		return src;
   124 	}
   125 	bool resize(size_t count)
   126 	{
   127 		if (count <= m_Size)
   128 		{
   129 			m_Size = count;
   130 			return true;
   131 		}
   132 
   133 		if(!GrowIfNeeded(count - m_Size))
   134 		{
   135 			return false;
   136 		}
   137 
   138 		m_Size = count;
   139 		return true;
   140 	}
   141 private:
   142 	bool GrowIfNeeded(size_t count)
   143 	{
   144 		/* Shortcut out if we can store this */
   145 		if (m_Size + count <= m_AllocSize)
   146 		{
   147 			return true;
   148 		}
   149 		/* Set a base allocation size of 8 items */
   150 		if (!m_AllocSize)
   151 		{
   152 			m_AllocSize = 8;
   153 		}
   154 		/* If it's not enough, keep doubling */
   155 		while (m_Size + count > m_AllocSize)
   156 		{
   157 			m_AllocSize *= 2;
   158 		}
   159 		/* finally, allocate the new block */
   160 		if (m_Data)
   161 		{
   162 			m_Data = (cell_t *)realloc(m_Data, sizeof(cell_t) * m_BlockSize * m_AllocSize);
   163 		} else {
   164 			m_Data = (cell_t *)malloc(sizeof(cell_t) * m_BlockSize * m_AllocSize);
   165 		}
   166 		return (m_Data != NULL);
   167 	}
   168 private:
   169 	cell_t *m_Data;
   170 	size_t m_BlockSize;
   171 	size_t m_AllocSize;
   172 	size_t m_Size;
   173 };