Main Page | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Class Members | File Members

arraylist.h

Go to the documentation of this file.
00001 #ifndef ARRAYLIST_H
00002 #define ARRAYLIST_H
00003 
00004 #include <stdio.h>
00005 
00006 // designed for lists of track numbers
00007 
00008 #include <stdio.h>
00009 
00010 
00011 template<class TYPE>
00012 class ArrayList
00013 {
00014 public:
00015         ArrayList();
00016         virtual ~ArrayList();
00017 
00018         TYPE append(TYPE value);
00019         TYPE append();
00020         TYPE insert(TYPE value, int number);
00021 
00022 // allocate
00023         void allocate(int total);
00024 // remove last pointer from end
00025         void remove();          
00026 // remove last pointer and object from end
00027         void remove_object();          
00028 // remove pointer to object from list
00029         void remove(TYPE value);       
00030 // remove object and pointer to it from list
00031         void remove_object(TYPE value);     
00032 // remove object and pointer to it from list
00033         void remove_object_number(int number);     
00034 // remove pointer to item numbered
00035         void remove_number(int number);
00036 // Return number of first object matching argument
00037         int number_of(TYPE object);
00038         void remove_all();
00039 // Remove pointer and objects for each array entry
00040         void remove_all_objects();
00041         TYPE last();
00042         void set_array_delete();
00043 
00044         void sort();
00045 
00046         TYPE* values;
00047         int total;
00048 
00049 private:
00050         int available;
00051         int array_delete;
00052 };
00053 
00054 template<class TYPE>
00055 ArrayList<TYPE>::ArrayList()
00056 {
00057         total = 0;
00058         available = 16;
00059         array_delete = 0;
00060         values = new TYPE[available];
00061 }
00062 
00063 
00064 template<class TYPE>
00065 ArrayList<TYPE>::~ArrayList()
00066 {
00067 // Just remove the pointer
00068         delete [] values;
00069         values = 0;
00070 }
00071 
00072 template<class TYPE>
00073 void ArrayList<TYPE>::set_array_delete()
00074 {
00075     array_delete = 1;
00076 }
00077 
00078 
00079 template<class TYPE>
00080 void ArrayList<TYPE>::allocate(int total)
00081 {
00082         if(total > available)
00083         {
00084                 available = total;
00085                 TYPE* newvalues = new TYPE[available];
00086                 for(int i = 0; i < total; i++) newvalues[i] = values[i];
00087                 delete [] values;
00088                 values = newvalues;
00089         }
00090 }
00091 
00092 template<class TYPE>
00093 TYPE ArrayList<TYPE>::append(TYPE value)            // add to end of list
00094 {
00095         if(total + 1 > available) 
00096         {
00097                 available *= 2;
00098                 TYPE* newvalues = new TYPE[available];
00099                 for(int i = 0; i < total; i++) newvalues[i] = values[i];
00100                 delete [] values;
00101                 values = newvalues;
00102         }
00103         
00104         values[total++] = value;
00105         return value;
00106 }
00107 
00108 template<class TYPE>
00109 TYPE ArrayList<TYPE>::append()            // add to end of list
00110 {
00111         if(total + 1 > available)
00112         {
00113                 available *= 2;
00114                 TYPE* newvalues = new TYPE[available];
00115                 for(int i = 0; i < total; i++) newvalues[i] = values[i];
00116                 delete [] values;
00117                 values = newvalues;
00118         }
00119         total++;
00120 
00121         return values[total - 1];
00122 }
00123 
00124 template<class TYPE>
00125 TYPE ArrayList<TYPE>::insert(TYPE value, int number)
00126 {
00127         append(0);
00128         for(int i = total - 1; i > number; i--)
00129         {
00130                 values[i] = values[i - 1];
00131         }
00132         values[number] = value;
00133 }
00134 
00135 template<class TYPE>
00136 void ArrayList<TYPE>::remove(TYPE value)                   // remove value from anywhere in list
00137 {
00138         int in, out;
00139 
00140         for(in = 0, out = 0; in < total;)
00141         {
00142                 if(values[in] != value) values[out++] = values[in++];
00143                 else 
00144                 {
00145                         in++; 
00146                 }
00147         }
00148         total = out;
00149 }
00150 
00151 template<class TYPE>
00152 TYPE ArrayList<TYPE>::last()                   // last element in list
00153 {
00154         return values[total - 1];
00155 }
00156 
00157 
00158 
00159 template<class TYPE>
00160 void ArrayList<TYPE>::remove_object(TYPE value)                   // remove value from anywhere in list
00161 {
00162         remove(value);
00163         if (array_delete) 
00164                 delete [] value;
00165         else 
00166                 delete value;
00167 }
00168 
00169 template<class TYPE>
00170 void ArrayList<TYPE>::remove_object_number(int number)
00171 {
00172         if(number < total)
00173         {
00174                 if (array_delete) 
00175                         delete [] values[number];
00176                 else
00177                         delete values[number];
00178                 remove_number(number);
00179         }
00180         else
00181                 fprintf(stderr, "ArrayList<TYPE>::remove_object_number: number %d out of range %s.\n", number, total);
00182 }
00183 
00184 
00185 template<class TYPE>
00186 void ArrayList<TYPE>::remove_object()                   // remove value from anywhere in list
00187 {
00188         if(total)
00189         {
00190                 if (array_delete) 
00191                         delete [] values[total - 1];
00192                 else 
00193                         delete values[total - 1];
00194                 remove();
00195         }
00196         else
00197                 fprintf(stderr, "ArrayList<TYPE>::remove_object: array is 0 length.\n");
00198 }
00199 
00200 
00201 
00202 template<class TYPE>
00203 void ArrayList<TYPE>::remove()
00204 {
00205         total--;
00206 }
00207 
00208 // remove pointer from anywhere in list
00209 template<class TYPE>
00210 void ArrayList<TYPE>::remove_number(int number)                   
00211 {
00212         int in, out;
00213         for(in = 0, out = 0; in < total;)
00214         {
00215                 if(in != number)
00216                         values[out++] = values[in++];
00217                 else
00218 // need to delete it here
00219                         in++;       
00220         }
00221         total = out;
00222 }
00223 
00224 template<class TYPE>
00225 void ArrayList<TYPE>::remove_all_objects()
00226 {
00227 //printf("ArrayList<TYPE>::remove_all_objects 1 %d\n", total);
00228         for(int i = 0; i < total; i++)
00229         {
00230                 if(array_delete)
00231                         delete [] values[i];
00232                 else
00233                         delete values[i];
00234         }
00235         total = 0;
00236 }
00237 
00238 template<class TYPE>
00239 void ArrayList<TYPE>::remove_all()
00240 {
00241         total = 0;
00242 }
00243 
00244 // sort from least to greatest value
00245 template<class TYPE>
00246 void ArrayList<TYPE>::sort()
00247 {
00248         int result = 1;
00249         TYPE temp;
00250 
00251         while(result)
00252         {
00253                 result = 0;
00254                 for(int i = 0, j = 1; j < total; i++, j++)
00255                 {
00256                         if(values[j] < values[i])
00257                         {
00258                                 temp = values[i];
00259                                 values[i] = values[j];
00260                                 values[j] = temp;
00261                                 result = 1;
00262                         }
00263                 }
00264         }
00265 }
00266 
00267 template<class TYPE>
00268 int ArrayList<TYPE>::number_of(TYPE object)
00269 {
00270         for(int i = 0; i < total; i++)
00271         {
00272                 if(values[i] == object) return i;
00273         }
00274         return 0;
00275 }
00276 
00277 
00278 #endif

Generated on Sun Jan 8 13:26:31 2006 for Guicast-svn by  doxygen 1.4.4