1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/core/CellArray.h Tue Jul 24 01:18:28 2007 +0000
1.3 @@ -0,0 +1,149 @@
1.4 +/**
1.5 + * vim: set ts=4 :
1.6 + * ===============================================================
1.7 + * SourceMod (C)2004-2007 AlliedModders LLC. All rights reserved.
1.8 + * ===============================================================
1.9 + *
1.10 + * This file is not open source and may not be copied without explicit
1.11 + * written permission of AlliedModders LLC. This file may not be redistributed
1.12 + * in whole or significant part.
1.13 + * For information, see LICENSE.txt or http://www.sourcemod.net/license.php
1.14 + *
1.15 + * Version: $Id$
1.16 + */
1.17 +
1.18 +#include <malloc.h>
1.19 +#include <string.h>
1.20 +
1.21 +class CellArray
1.22 +{
1.23 +public:
1.24 + CellArray(size_t blocksize) : m_Data(NULL), m_BlockSize(blocksize), m_AllocSize(0), m_Size(0)
1.25 + {
1.26 + }
1.27 + ~CellArray()
1.28 + {
1.29 + free(m_Data);
1.30 + }
1.31 + size_t size() const
1.32 + {
1.33 + return m_Size;
1.34 + }
1.35 + cell_t *push()
1.36 + {
1.37 + if (!GrowIfNeeded(1))
1.38 + {
1.39 + return NULL;
1.40 + }
1.41 + cell_t *arr = &m_Data[m_Size * m_BlockSize];
1.42 + m_Size++;
1.43 + return arr;
1.44 + }
1.45 + cell_t *at(size_t b) const
1.46 + {
1.47 + return &m_Data[b * m_BlockSize];
1.48 + }
1.49 + size_t blocksize() const
1.50 + {
1.51 + return m_BlockSize;
1.52 + }
1.53 + void clear()
1.54 + {
1.55 + m_Size = 0;
1.56 + }
1.57 + bool swap(size_t item1, size_t item2)
1.58 + {
1.59 + /* Make sure there is extra space available */
1.60 + if (!GrowIfNeeded(1))
1.61 + {
1.62 + return false;
1.63 + }
1.64 +
1.65 + cell_t *pri = at(item1);
1.66 + cell_t *alt = at(item2);
1.67 +
1.68 + /* Get our temporary array 1 after the limit */
1.69 + cell_t *temp = &m_Data[m_Size * m_BlockSize];
1.70 +
1.71 + memcpy(temp, pri, sizeof(cell_t) * m_BlockSize);
1.72 + memcpy(pri, alt, sizeof(cell_t) * m_BlockSize);
1.73 + memcpy(alt, temp, sizeof(cell_t) * m_BlockSize);
1.74 +
1.75 + return true;
1.76 + }
1.77 + void remove(size_t index)
1.78 + {
1.79 + /* If we're at the end, take the easy way out */
1.80 + if (index == m_Size - 1)
1.81 + {
1.82 + m_Size--;
1.83 + return;
1.84 + }
1.85 +
1.86 + /* Otherwise, it's time to move stuff! */
1.87 + size_t remaining_indexes = (m_Size - 1) - index;
1.88 + cell_t *src = at(index + 1);
1.89 + cell_t *dest = at(index);
1.90 + memmove(dest, src, sizeof(cell_t) * m_BlockSize * remaining_indexes);
1.91 +
1.92 + m_Size--;
1.93 + }
1.94 + cell_t *insert_at(size_t index)
1.95 + {
1.96 + /* Make sure it'll fit */
1.97 + if (!GrowIfNeeded(1))
1.98 + {
1.99 + return NULL;
1.100 + }
1.101 +
1.102 + /* move everything up */
1.103 + cell_t *src = at(index);
1.104 + cell_t *dst = at(index + 1);
1.105 + memmove(dst, src, sizeof(cell_t) * m_BlockSize * m_Size);
1.106 +
1.107 + m_Size++;
1.108 +
1.109 + return src;
1.110 + }
1.111 + bool resize(size_t count)
1.112 + {
1.113 + if (count <= m_Size)
1.114 + {
1.115 + m_Size = count;
1.116 + return true;
1.117 + }
1.118 + return GrowIfNeeded(m_Size - count);
1.119 + }
1.120 +private:
1.121 + bool GrowIfNeeded(size_t count)
1.122 + {
1.123 + /* Shortcut out if we can store this */
1.124 + if (m_Size + count <= m_AllocSize)
1.125 + {
1.126 + return true;
1.127 + }
1.128 + /* Set a base allocation size of 8 items */
1.129 + if (!m_AllocSize)
1.130 + {
1.131 + m_AllocSize = 8;
1.132 + }
1.133 + /* If it's not enough, keep doubling */
1.134 + while (m_Size + count > m_AllocSize)
1.135 + {
1.136 + m_AllocSize *= 2;
1.137 + }
1.138 + /* finally, allocate the new block */
1.139 + if (m_Data)
1.140 + {
1.141 + m_Data = (cell_t *)realloc(m_Data, sizeof(cell_t) * m_BlockSize * m_AllocSize);
1.142 + } else {
1.143 + m_Data = (cell_t *)malloc(sizeof(cell_t) * m_BlockSize * m_AllocSize);
1.144 + }
1.145 + return (m_Data != NULL);
1.146 + }
1.147 +private:
1.148 + cell_t *m_Data;
1.149 + size_t m_BlockSize;
1.150 + size_t m_AllocSize;
1.151 + size_t m_Size;
1.152 +};