core/logic/CellArray.h
author Peace-Maker <januk@freenet.de>
Sat May 10 05:42:33 2014 -0700 (2014-05-10)
changeset 4354 7011587e0021
parent 2726 c96f3fab8211
permissions -rw-r--r--
Remove LogMessageEx definition from logging.inc (bug 5897, r=kyles).
     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 #ifndef _INCLUDE_SOURCEMOD_CELLARRAY_H_
    33 #define _INCLUDE_SOURCEMOD_CELLARRAY_H_
    34 
    35 #include <stdlib.h>
    36 #include <string.h>
    37 
    38 extern HandleType_t htCellArray;
    39 
    40 class CellArray
    41 {
    42 public:
    43 	CellArray(size_t blocksize) : m_Data(NULL), m_BlockSize(blocksize), m_AllocSize(0), m_Size(0)
    44 	{
    45 	}
    46 
    47 	~CellArray()
    48 	{
    49 		free(m_Data);
    50 	}
    51 
    52 	size_t size() const
    53 	{
    54 		return m_Size;
    55 	}
    56 
    57 	cell_t *push()
    58 	{
    59 		if (!GrowIfNeeded(1))
    60 		{
    61 			return NULL;
    62 		}
    63 		cell_t *arr = &m_Data[m_Size * m_BlockSize];
    64 		m_Size++;
    65 		return arr;
    66 	}
    67 
    68 	cell_t *at(size_t b) const
    69 	{
    70 		return &m_Data[b * m_BlockSize];
    71 	}
    72 
    73 	size_t blocksize() const
    74 	{
    75 		return m_BlockSize;
    76 	}
    77 
    78 	void clear()
    79 	{
    80 		m_Size = 0;
    81 	}
    82 
    83 	bool swap(size_t item1, size_t item2)
    84 	{
    85 		/* Make sure there is extra space available */
    86 		if (!GrowIfNeeded(1))
    87 		{
    88 			return false;
    89 		}
    90 
    91 		cell_t *pri = at(item1);
    92 		cell_t *alt = at(item2);
    93 
    94 		/* Get our temporary array 1 after the limit */
    95 		cell_t *temp = &m_Data[m_Size * m_BlockSize];
    96 
    97 		memcpy(temp, pri, sizeof(cell_t) * m_BlockSize);
    98 		memcpy(pri, alt, sizeof(cell_t) * m_BlockSize);
    99 		memcpy(alt, temp, sizeof(cell_t) * m_BlockSize);
   100 
   101 		return true;
   102 	}
   103 
   104 	void remove(size_t index)
   105 	{
   106 		/* If we're at the end, take the easy way out */
   107 		if (index == m_Size - 1)
   108 		{
   109 			m_Size--;
   110 			return;
   111 		}
   112 
   113 		/* Otherwise, it's time to move stuff! */
   114 		size_t remaining_indexes = (m_Size - 1) - index;
   115 		cell_t *src = at(index + 1);
   116 		cell_t *dest = at(index);
   117 		memmove(dest, src, sizeof(cell_t) * m_BlockSize * remaining_indexes);
   118 
   119 		m_Size--;
   120 	}
   121 
   122 	cell_t *insert_at(size_t index)
   123 	{
   124 		/* Make sure it'll fit */
   125 		if (!GrowIfNeeded(1))
   126 		{
   127 			return NULL;
   128 		}
   129 
   130 		/* move everything up */
   131 		cell_t *src = at(index);
   132 		cell_t *dst = at(index + 1);
   133 		memmove(dst, src, sizeof(cell_t) * m_BlockSize * (m_Size-index));
   134 
   135 		m_Size++;
   136 
   137 		return src;
   138 	}
   139 
   140 	bool resize(size_t count)
   141 	{
   142 		if (count <= m_Size)
   143 		{
   144 			m_Size = count;
   145 			return true;
   146 		}
   147 
   148 		if (!GrowIfNeeded(count - m_Size))
   149 		{
   150 			return false;
   151 		}
   152 
   153 		m_Size = count;
   154 		return true;
   155 	}
   156 
   157 	CellArray *clone()
   158 	{
   159 		CellArray *array = new CellArray(m_BlockSize);
   160 		array->m_AllocSize = m_AllocSize;
   161 		array->m_Size = m_Size;
   162 		array->m_Data = (cell_t *)malloc(sizeof(cell_t) * m_BlockSize * m_AllocSize);
   163 		memcpy(array->m_Data, m_Data, sizeof(cell_t) * m_BlockSize * m_Size);
   164 		return array;
   165 	}
   166 
   167 	cell_t *base()
   168 	{
   169 		return m_Data;
   170 	}
   171 
   172 	size_t mem_usage()
   173 	{
   174 		return m_AllocSize * m_BlockSize * sizeof(cell_t);
   175 	}
   176 
   177 private:
   178 	bool GrowIfNeeded(size_t count)
   179 	{
   180 		/* Shortcut out if we can store this */
   181 		if (m_Size + count <= m_AllocSize)
   182 		{
   183 			return true;
   184 		}
   185 		/* Set a base allocation size of 8 items */
   186 		if (!m_AllocSize)
   187 		{
   188 			m_AllocSize = 8;
   189 		}
   190 		/* If it's not enough, keep doubling */
   191 		while (m_Size + count > m_AllocSize)
   192 		{
   193 			m_AllocSize *= 2;
   194 		}
   195 		/* finally, allocate the new block */
   196 		if (m_Data)
   197 		{
   198 			m_Data = (cell_t *)realloc(m_Data, sizeof(cell_t) * m_BlockSize * m_AllocSize);
   199 		} else {
   200 			m_Data = (cell_t *)malloc(sizeof(cell_t) * m_BlockSize * m_AllocSize);
   201 		}
   202 		return (m_Data != NULL);
   203 	}
   204 private:
   205 	cell_t *m_Data;
   206 	size_t m_BlockSize;
   207 	size_t m_AllocSize;
   208 	size_t m_Size;
   209 };
   210 
   211 #endif /* _INCLUDE_SOURCEMOD_CELLARRAY_H_ */
   212