core/CellArray.h
author David Anderson <dvander@alliedmods.net>
Tue Dec 04 01:08:03 2007 +0000 (2007-12-04)
changeset 1756 33f9fa5adfe3
parent 1701 9cf7f48da853
child 1766 d30c252089be
permissions -rw-r--r--
added @t, @ts, @ct, @cts target patterns
     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 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 	~CellArray()
    44 	{
    45 		free(m_Data);
    46 	}
    47 	size_t size() const
    48 	{
    49 		return m_Size;
    50 	}
    51 	cell_t *push()
    52 	{
    53 		if (!GrowIfNeeded(1))
    54 		{
    55 			return NULL;
    56 		}
    57 		cell_t *arr = &m_Data[m_Size * m_BlockSize];
    58 		m_Size++;
    59 		return arr;
    60 	}
    61 	cell_t *at(size_t b) const
    62 	{
    63 		return &m_Data[b * m_BlockSize];
    64 	}
    65 	size_t blocksize() const
    66 	{
    67 		return m_BlockSize;
    68 	}
    69 	void clear()
    70 	{
    71 		m_Size = 0;
    72 	}
    73 	bool swap(size_t item1, size_t item2)
    74 	{
    75 		/* Make sure there is extra space available */
    76 		if (!GrowIfNeeded(1))
    77 		{
    78 			return false;
    79 		}
    80 
    81 		cell_t *pri = at(item1);
    82 		cell_t *alt = at(item2);
    83 
    84 		/* Get our temporary array 1 after the limit */
    85 		cell_t *temp = &m_Data[m_Size * m_BlockSize];
    86 
    87 		memcpy(temp, pri, sizeof(cell_t) * m_BlockSize);
    88 		memcpy(pri, alt, sizeof(cell_t) * m_BlockSize);
    89 		memcpy(alt, temp, sizeof(cell_t) * m_BlockSize);
    90 
    91 		return true;
    92 	}
    93 	void remove(size_t index)
    94 	{
    95 		/* If we're at the end, take the easy way out */
    96 		if (index == m_Size - 1)
    97 		{
    98 			m_Size--;
    99 			return;
   100 		}
   101 
   102 		/* Otherwise, it's time to move stuff! */
   103 		size_t remaining_indexes = (m_Size - 1) - index;
   104 		cell_t *src = at(index + 1);
   105 		cell_t *dest = at(index);
   106 		memmove(dest, src, sizeof(cell_t) * m_BlockSize * remaining_indexes);
   107 
   108 		m_Size--;
   109 	}
   110 	cell_t *insert_at(size_t index)
   111 	{
   112 		/* Make sure it'll fit */
   113 		if (!GrowIfNeeded(1))
   114 		{
   115 			return NULL;
   116 		}
   117 
   118 		/* move everything up */
   119 		cell_t *src = at(index);
   120 		cell_t *dst = at(index + 1);
   121 		memmove(dst, src, sizeof(cell_t) * m_BlockSize * m_Size);
   122 
   123 		m_Size++;
   124 
   125 		return src;
   126 	}
   127 	bool resize(size_t count)
   128 	{
   129 		if (count <= m_Size)
   130 		{
   131 			m_Size = count;
   132 			return true;
   133 		}
   134 
   135 		if(!GrowIfNeeded(count - m_Size))
   136 		{
   137 			return false;
   138 		}
   139 
   140 		m_Size = count;
   141 		return true;
   142 	}
   143 
   144 	CellArray *clone()
   145 	{
   146 		CellArray *array = new CellArray(m_BlockSize);
   147 		array->m_AllocSize = m_AllocSize;
   148 		array->m_Size = m_Size;
   149 		array->m_Data = (cell_t *)malloc(sizeof(cell_t) * m_BlockSize * m_AllocSize);
   150 		memcpy(array->m_Data, m_Data, sizeof(cell_t) * m_BlockSize * m_Size);
   151 		return array;
   152 	}
   153 
   154 	cell_t *base()
   155 	{
   156 		return m_Data;
   157 	}
   158 
   159 private:
   160 	bool GrowIfNeeded(size_t count)
   161 	{
   162 		/* Shortcut out if we can store this */
   163 		if (m_Size + count <= m_AllocSize)
   164 		{
   165 			return true;
   166 		}
   167 		/* Set a base allocation size of 8 items */
   168 		if (!m_AllocSize)
   169 		{
   170 			m_AllocSize = 8;
   171 		}
   172 		/* If it's not enough, keep doubling */
   173 		while (m_Size + count > m_AllocSize)
   174 		{
   175 			m_AllocSize *= 2;
   176 		}
   177 		/* finally, allocate the new block */
   178 		if (m_Data)
   179 		{
   180 			m_Data = (cell_t *)realloc(m_Data, sizeof(cell_t) * m_BlockSize * m_AllocSize);
   181 		} else {
   182 			m_Data = (cell_t *)malloc(sizeof(cell_t) * m_BlockSize * m_AllocSize);
   183 		}
   184 		return (m_Data != NULL);
   185 	}
   186 private:
   187 	cell_t *m_Data;
   188 	size_t m_BlockSize;
   189 	size_t m_AllocSize;
   190 	size_t m_Size;
   191 };