00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011 #ifndef INCLUDE_RSRC_ARRAY
00012 #define INCLUDE_RSRC_ARRAY
00013
00014 #include <malloc.h>
00015
00016 #define ARRAY_DEF_ALLOC 1000
00017 #define ARRAY_DEF_DEALLOC 500
00018 #define ARRAY_DEF_MAX 100000000
00019
00020
00021
00022
00023 template <class T>
00024 class Array
00025 {
00026 private :
00027 unsigned int allocBlockSize;
00028 unsigned int deallocBlockSize;
00029 unsigned int maxSize;
00030 T *tab;
00031 unsigned int allocatedSize;
00032 unsigned int nbDatas;
00033 unsigned int begin;
00034 long int end;
00035
00036 public :
00037
00038
00039
00040
00041
00042 Array(unsigned int iMaxSize = ARRAY_DEF_MAX, unsigned int iAllocSize = ARRAY_DEF_ALLOC, unsigned int iDeallocSize = ARRAY_DEF_DEALLOC)
00043 {
00044 allocBlockSize = iAllocSize;
00045 deallocBlockSize = iDeallocSize;
00046 maxSize = iMaxSize;
00047 tab = NULL;
00048 allocatedSize = 0;
00049 nbDatas = 0;
00050 begin = 0;
00051 end = -1;
00052 };
00053
00054
00055
00056 ~Array()
00057 {
00058 if(tab)
00059 free(tab);
00060 };
00061
00062
00063
00064
00065
00066
00067 unsigned int push_back(T pData)
00068 {
00069 if((unsigned int)(end + 1) >= allocatedSize)
00070 {
00071 if(allocatedSize > maxSize)
00072 return 1;
00073
00074 allocatedSize += allocBlockSize;
00075 tab = (T *)realloc(tab, sizeof(T) * allocatedSize);
00076 }
00077
00078 end++;
00079 tab[end] = pData;
00080 nbDatas++;
00081
00082 return 0;
00083 };
00084
00085
00086
00087
00088
00089
00090
00091 unsigned int push(T pData, long int iPos)
00092 {
00093 unsigned int i;
00094
00095 if(iPos > (long int)nbDatas)
00096 return 1;
00097
00098 if((unsigned int)(end + 1) >= allocatedSize)
00099 {
00100 if(allocatedSize > maxSize)
00101 return 1;
00102
00103 allocatedSize += allocBlockSize;
00104 tab = (T *)realloc(tab, sizeof(T) * allocatedSize);
00105 }
00106
00107 end++;
00108
00109 for(i = iPos; i <= end; i++)
00110 tab[i + 1] = tab[i];
00111 tab[iPos] = pData;
00112
00113 nbDatas++;
00114
00115 return 0;
00116 };
00117
00118
00119
00120
00121
00122
00123 unsigned int erase(long int iPos)
00124 {
00125 long int i, j;
00126
00127 if(iPos > (long int)nbDatas)
00128 return 1;
00129
00130 for(i = iPos; i < end; i++)
00131 tab[i] = tab[i + 1];
00132 tab[i] = NULL;
00133 end--;
00134 nbDatas--;
00135
00136 if(begin > deallocBlockSize)
00137 {
00138 for(i = begin, j = 0; i <= end; i++, j++)
00139 tab[j] = tab[i];
00140
00141 begin = 0;
00142 end = nbDatas - 1;
00143 }
00144
00145 return 0;
00146 };
00147
00148
00149
00150
00151 void clear()
00152 {
00153 begin = 0;
00154 end = -1;
00155 nbDatas = 0;
00156 };
00157
00158
00159
00160
00161
00162 inline T front()
00163 {
00164 if(nbDatas)
00165 return tab[begin];
00166
00167 return NULL;
00168 };
00169
00170
00171
00172
00173
00174 inline T back()
00175 {
00176 if(nbDatas)
00177 return tab[end];
00178
00179 return NULL;
00180 };
00181
00182
00183
00184
00185
00186
00187 inline T operator[](unsigned int iPos)
00188 {
00189 if(iPos < nbDatas)
00190 return tab[begin + iPos];
00191
00192 return NULL;
00193 };
00194
00195
00196
00197
00198
00199 inline unsigned int size()
00200 {
00201 return nbDatas;
00202 };
00203
00204
00205
00206
00207
00208 inline unsigned int capacity()
00209 {
00210 return allocatedSize;
00211 };
00212
00213
00214
00215
00216
00217 inline unsigned int allocSize()
00218 {
00219 return allocBlockSize;
00220 };
00221
00222
00223
00224
00225
00226 inline unsigned int deallocSize()
00227 {
00228 return deallocBlockSize;
00229 };
00230
00231
00232
00233
00234
00235 inline unsigned int max_size()
00236 {
00237 return maxSize;
00238 };
00239 };
00240
00241 #endif
00242