00001 #ifndef ARRAYLIST_H
00002 #define ARRAYLIST_H
00003
00004
00005
00006 #include <stdio.h>
00007 #include <stdlib.h>
00008
00009 #define ARRAYLIST_REMOVEOBJECT_DELETE 0
00010 #define ARRAYLIST_REMOVEOBJECT_DELETEARRAY 1
00011 #define ARRAYLIST_REMOVEOBJECT_FREE 2
00012
00013 template<class TYPE>
00014 class ArrayList
00015 {
00016 public:
00017 ArrayList();
00018 virtual ~ArrayList();
00019
00020 TYPE append(TYPE value);
00021 TYPE append();
00022 TYPE insert(TYPE value, int number);
00023
00024
00025 void allocate(int total);
00026
00027 void remove();
00028
00029 void remove_object();
00030
00031 void remove(TYPE value);
00032
00033 void remove_object(TYPE value);
00034
00035 void remove_object_number(int number);
00036
00037 void remove_number(int number);
00038
00039 int number_of(TYPE object);
00040 void remove_all();
00041
00042 void remove_all_objects();
00043
00044 TYPE last();
00045
00046
00047 void set_array_delete();
00048 void set_free();
00049
00050 void sort();
00051
00052 TYPE* values;
00053 int total;
00054
00055 private:
00056 int available;
00057 int removeobject_type;
00058 };
00059
00060 template<class TYPE>
00061 ArrayList<TYPE>::ArrayList()
00062 {
00063 total = 0;
00064 available = 16;
00065 removeobject_type = ARRAYLIST_REMOVEOBJECT_DELETE;
00066 values = new TYPE[available];
00067 }
00068
00069
00070 template<class TYPE>
00071 ArrayList<TYPE>::~ArrayList()
00072 {
00073
00074 delete [] values;
00075 values = 0;
00076 }
00077
00078 template<class TYPE>
00079 void ArrayList<TYPE>::set_array_delete()
00080 {
00081 removeobject_type = ARRAYLIST_REMOVEOBJECT_DELETEARRAY;
00082
00083 }
00084
00085 template<class TYPE>
00086 void ArrayList<TYPE>::set_free()
00087 {
00088 removeobject_type = ARRAYLIST_REMOVEOBJECT_FREE;
00089
00090 }
00091
00092
00093 template<class TYPE>
00094 void ArrayList<TYPE>::allocate(int total)
00095 {
00096 if(total > available)
00097 {
00098 available = total;
00099 TYPE* newvalues = new TYPE[available];
00100 for(int i = 0; i < total; i++) newvalues[i] = values[i];
00101 delete [] values;
00102 values = newvalues;
00103 }
00104 }
00105
00106 template<class TYPE>
00107 TYPE ArrayList<TYPE>::append(TYPE value)
00108 {
00109 if(total + 1 > available)
00110 {
00111 available *= 2;
00112 TYPE* newvalues = new TYPE[available];
00113 for(int i = 0; i < total; i++) newvalues[i] = values[i];
00114 delete [] values;
00115 values = newvalues;
00116 }
00117
00118 values[total++] = value;
00119 return value;
00120 }
00121
00122 template<class TYPE>
00123 TYPE ArrayList<TYPE>::append()
00124 {
00125 if(total + 1 > available)
00126 {
00127 available *= 2;
00128 TYPE* newvalues = new TYPE[available];
00129 for(int i = 0; i < total; i++) newvalues[i] = values[i];
00130 delete [] values;
00131 values = newvalues;
00132 }
00133 total++;
00134
00135 return values[total - 1];
00136 }
00137
00138 template<class TYPE>
00139 TYPE ArrayList<TYPE>::insert(TYPE value, int number)
00140 {
00141 append(0);
00142 for(int i = total - 1; i > number; i--)
00143 {
00144 values[i] = values[i - 1];
00145 }
00146 values[number] = value;
00147 }
00148
00149 template<class TYPE>
00150 void ArrayList<TYPE>::remove(TYPE value)
00151 {
00152 int in, out;
00153
00154 for(in = 0, out = 0; in < total;)
00155 {
00156 if(values[in] != value) values[out++] = values[in++];
00157 else
00158 {
00159 in++;
00160 }
00161 }
00162 total = out;
00163 }
00164
00165 template<class TYPE>
00166 TYPE ArrayList<TYPE>::last()
00167 {
00168 return values[total - 1];
00169 }
00170
00171
00172
00173 template<class TYPE>
00174 void ArrayList<TYPE>::remove_object(TYPE value)
00175 {
00176 remove(value);
00177 switch (removeobject_type)
00178 {
00179 case ARRAYLIST_REMOVEOBJECT_DELETE:
00180 delete value;
00181 break;
00182 case ARRAYLIST_REMOVEOBJECT_DELETEARRAY:
00183 delete [] value;
00184 break;
00185 case ARRAYLIST_REMOVEOBJECT_FREE:
00186 free(value);
00187 break;
00188 default:
00189 printf("Unknown function to use to free array\n");
00190 break;
00191 }
00192 }
00193
00194 template<class TYPE>
00195 void ArrayList<TYPE>::remove_object_number(int number)
00196 {
00197 if(number < total)
00198 {
00199 switch (removeobject_type)
00200 {
00201 case ARRAYLIST_REMOVEOBJECT_DELETE:
00202 delete values[number];
00203 break;
00204 case ARRAYLIST_REMOVEOBJECT_DELETEARRAY:
00205 delete [] values[number];
00206 break;
00207 case ARRAYLIST_REMOVEOBJECT_FREE:
00208 free(values[number]);
00209 break;
00210 default:
00211 printf("Unknown function to use to free array\n");
00212 break;
00213 }
00214
00215 remove_number(number);
00216 }
00217 else
00218 fprintf(stderr, "ArrayList<TYPE>::remove_object_number: number %d out of range %s.\n", number, total);
00219 }
00220
00221
00222 template<class TYPE>
00223 void ArrayList<TYPE>::remove_object()
00224 {
00225 if(total)
00226 {
00227 switch (removeobject_type)
00228 {
00229 case ARRAYLIST_REMOVEOBJECT_DELETE:
00230 delete values[total - 1];
00231 break;
00232 case ARRAYLIST_REMOVEOBJECT_DELETEARRAY:
00233 delete [] values[total -1];
00234 break;
00235 case ARRAYLIST_REMOVEOBJECT_FREE:
00236 free(values[total - 1]);
00237 break;
00238 default:
00239 printf("Unknown function to use to free array\n");
00240 break;
00241 }
00242
00243 remove();
00244 }
00245 else
00246 fprintf(stderr, "ArrayList<TYPE>::remove_object: array is 0 length.\n");
00247 }
00248
00249
00250
00251 template<class TYPE>
00252 void ArrayList<TYPE>::remove()
00253 {
00254 total--;
00255 }
00256
00257
00258 template<class TYPE>
00259 void ArrayList<TYPE>::remove_number(int number)
00260 {
00261 int in, out;
00262 for(in = 0, out = 0; in < total;)
00263 {
00264 if(in != number)
00265 values[out++] = values[in++];
00266 else
00267
00268 in++;
00269 }
00270 total = out;
00271 }
00272
00273 template<class TYPE>
00274 void ArrayList<TYPE>::remove_all_objects()
00275 {
00276
00277 for(int i = 0; i < total; i++)
00278 {
00279 switch (removeobject_type)
00280 {
00281 case ARRAYLIST_REMOVEOBJECT_DELETE:
00282 delete values[i];
00283 break;
00284 case ARRAYLIST_REMOVEOBJECT_DELETEARRAY:
00285 delete [] values[i];
00286 break;
00287 case ARRAYLIST_REMOVEOBJECT_FREE:
00288 free(values[i]);
00289 break;
00290 default:
00291 printf("Unknown function to use to free array\n");
00292 break;
00293 }
00294 }
00295 total = 0;
00296 }
00297
00298 template<class TYPE>
00299 void ArrayList<TYPE>::remove_all()
00300 {
00301 total = 0;
00302 }
00303
00304
00305 template<class TYPE>
00306 void ArrayList<TYPE>::sort()
00307 {
00308 int result = 1;
00309 TYPE temp;
00310
00311 while(result)
00312 {
00313 result = 0;
00314 for(int i = 0, j = 1; j < total; i++, j++)
00315 {
00316 if(values[j] < values[i])
00317 {
00318 temp = values[i];
00319 values[i] = values[j];
00320 values[j] = temp;
00321 result = 1;
00322 }
00323 }
00324 }
00325 }
00326
00327 template<class TYPE>
00328 int ArrayList<TYPE>::number_of(TYPE object)
00329 {
00330 for(int i = 0; i < total; i++)
00331 {
00332 if(values[i] == object) return i;
00333 }
00334 return 0;
00335 }
00336
00337
00338 #endif