00001
00002
00003
00004
00005 #ifndef _GARRAY_H_
00006 #define _GARRAY_H_
00007
00008 #include <stdlib.h>
00009 #include "LgiDefs.h"
00010
00011 #define GARRAY_MIN_SIZE 16
00012
00029 template <class Type>
00030 class GArray
00031 {
00032 Type *p;
00033 uint32 len;
00034 uint32 alloc;
00035
00036 public:
00038 GArray(int PreAlloc = 0)
00039 {
00040 p = 0;
00041 len = 0;
00042 alloc = PreAlloc;
00043 if (alloc)
00044 {
00045 int Bytes = sizeof(Type) * alloc;
00046 p = (Type*) malloc(Bytes);
00047 if (p)
00048 {
00049 memset(p, 0, Bytes);
00050 }
00051 else
00052 {
00053 alloc = 0;
00054 }
00055 }
00056 }
00057
00059 ~GArray()
00060 {
00061 Length(0);
00062 }
00063
00065 int Length()
00066 {
00067 return len;
00068 }
00069
00071 bool Length(uint32 i)
00072 {
00073 if (i > 0)
00074 {
00075 uint nalloc = i + GARRAY_MIN_SIZE;
00076 if (nalloc != alloc)
00077 {
00078 Type *np = (Type*)malloc(sizeof(Type) * nalloc);
00079 if (NOT np)
00080 {
00081 return false;
00082 }
00083
00084 if (p)
00085 {
00086
00087 memcpy(np, p, min(len, i) * sizeof(Type));
00088 free(p);
00089 }
00090 p = np;
00091 alloc = nalloc;
00092 }
00093
00094 if (i > len)
00095 {
00096
00097 memset(p + len, 0, sizeof(Type) * (i - len));
00098 }
00099
00100 len = i;
00101 }
00102 else
00103 {
00104 if (p)
00105 {
00106 for (uint i=0; i<len; i++)
00107 {
00108 p->~Type();
00109 }
00110 free(p);
00111 p = 0;
00112 }
00113 len = alloc = 0;
00114 }
00115
00116 return true;
00117 }
00118
00119 GArray<Type> &operator =(GArray<Type> &a)
00120 {
00121 Length(a.Length());
00122 if (p AND a.p)
00123 {
00124 memcpy(p, a.p, sizeof(Type) * len);
00125 }
00126 return *this;
00127 }
00128
00132 Type &operator [](uint32 i)
00133 {
00134 static Type *t = 0;
00135 if (i < 0)
00136 {
00137 return *t;
00138 }
00139
00140 if (i > 5000000)
00141 {
00142 #ifdef _DEBUG
00143 #ifdef WIN32
00144 _asm int 3
00145 #else
00146 LgiAssert(0);
00147 #endif
00148 #endif
00149
00150 return *t;
00151 }
00152
00153 if (i >= alloc)
00154 {
00155
00156 uint nalloc = max(alloc, GARRAY_MIN_SIZE);
00157 while (nalloc <= i)
00158 {
00159 nalloc <<= 1;
00160 }
00161
00162
00163 Type *np = (Type*) malloc(sizeof(Type) * nalloc);
00164 if (np)
00165 {
00166
00167 memset(np + len, 0, (nalloc - len) * sizeof(Type));
00168 if (p)
00169 {
00170
00171 memcpy(np, p, len * sizeof(Type));
00172
00173
00174 free(p);
00175 }
00176
00177
00178 p = np;
00179 alloc = nalloc;
00180 }
00181 else
00182 {
00183 static Type *t = 0;
00184 return *t;
00185 }
00186 }
00187
00188
00189 if (i + 1 > len)
00190 {
00191 len = i + 1;
00192 }
00193
00194 return p[i];
00195 }
00196
00198 void DeleteObjects()
00199 {
00200 for (uint i=0; i<len; i++)
00201 {
00202 DeleteObj(p[i]);
00203 }
00204 Length(0);
00205 }
00206
00208 void DeleteArrays()
00209 {
00210 for (int i=0; i<len; i++)
00211 {
00212 DeleteArray(p[i]);
00213 }
00214 Length(0);
00215 }
00216
00218 int IndexOf(Type n)
00219 {
00220 for (uint i=0; i<len; i++)
00221 {
00222 if (p[i] == n) return i;
00223 }
00224
00225 return -1;
00226 }
00227
00229 bool HasItem(Type n)
00230 {
00231 return IndexOf(n) >= 0;
00232 }
00233
00235 bool DeleteAt
00236 (
00238 uint Index,
00240 bool Ordered = false
00241 )
00242 {
00243 if (p AND Index >= 0)
00244 {
00245 if (Index < len - 1)
00246 {
00247 if (Ordered)
00248 {
00249 memmove(p + Index, p + Index + 1, (len - Index - 1) * sizeof(Type) );
00250 }
00251 else
00252 {
00253 p[Index] = p[len-1];
00254 }
00255 }
00256 len--;
00257 return true;
00258 }
00259
00260 return false;
00261 }
00262
00264 bool Delete
00265 (
00267 Type n,
00269 bool Ordered = false
00270 )
00271 {
00272 int i = IndexOf(n);
00273 if (p AND i >= 0)
00274 {
00275 return DeleteAt(i, Ordered);
00276 }
00277
00278 return false;
00279 }
00280
00282 void Add
00283 (
00285 Type n
00286 )
00287 {
00288 (*this)[len] = n;
00289 }
00290
00292 bool AddAt
00293 (
00295 int Index,
00297 Type n
00298 )
00299 {
00300
00301 if (Length(len + 1))
00302 {
00303
00304 memmove(p + Index + 1, p + Index, (len - Index - 1) * sizeof(Type) );
00305
00306
00307 p[Index] = n;
00308
00309 return true;
00310 }
00311
00312 return false;
00313 }
00314
00316 void Sort(int (*Compare)(Type*, Type*))
00317 {
00318 typedef int (*qsort_compare)(const void *, const void *);
00319 qsort(p, len, sizeof(Type), (qsort_compare)Compare);
00320 }
00321 };
00322
00323 #endif