r878: This is Cinelerra 2.1.
[cinelerra_cv/ct.git] / guicast / arraylist.h
blob6549fafc5479b195ebdeb34ee0e526b78699f98f
1 #ifndef ARRAYLIST_H
2 #define ARRAYLIST_H
4 // designed for lists of track numbers
6 #include <stdio.h>
7 #include <stdlib.h>
9 #define ARRAYLIST_REMOVEOBJECT_DELETE 0
10 #define ARRAYLIST_REMOVEOBJECT_DELETEARRAY 1
11 #define ARRAYLIST_REMOVEOBJECT_FREE 2
13 template<class TYPE>
14 class ArrayList
16 public:
17 ArrayList();
18 virtual ~ArrayList();
20 TYPE append(TYPE value);
21 TYPE append();
22 TYPE insert(TYPE value, int number);
24 // allocate
25 void allocate(int total);
26 // remove last pointer from end
27 void remove();
28 // remove last pointer and object from end
29 void remove_object();
30 // remove pointer to object from list
31 void remove(TYPE value);
32 // remove object and pointer to it from list
33 void remove_object(TYPE value);
34 // remove object and pointer to it from list
35 void remove_object_number(int number);
36 // remove pointer to item numbered
37 void remove_number(int number);
38 // Return number of first object matching argument
39 int number_of(TYPE object);
40 void remove_all();
41 // Remove pointer and objects for each array entry
42 void remove_all_objects();
43 // Get last element in list
44 TYPE last();
45 // Call this if the TYPE is a pointer to an array which must be
46 // deleted by delete [].
47 void set_array_delete();
48 void set_free();
50 void sort();
52 TYPE* values;
53 int total;
55 private:
56 int available;
57 int removeobject_type;
60 template<class TYPE>
61 ArrayList<TYPE>::ArrayList()
63 total = 0;
64 available = 16;
65 removeobject_type = ARRAYLIST_REMOVEOBJECT_DELETE;
66 values = new TYPE[available];
70 template<class TYPE>
71 ArrayList<TYPE>::~ArrayList()
73 // Just remove the pointer
74 delete [] values;
75 values = 0;
78 template<class TYPE>
79 void ArrayList<TYPE>::set_array_delete()
81 removeobject_type = ARRAYLIST_REMOVEOBJECT_DELETEARRAY;
85 template<class TYPE>
86 void ArrayList<TYPE>::set_free()
88 removeobject_type = ARRAYLIST_REMOVEOBJECT_FREE;
93 template<class TYPE>
94 void ArrayList<TYPE>::allocate(int total)
96 if(total > available)
98 available = total;
99 TYPE* newvalues = new TYPE[available];
100 for(int i = 0; i < total; i++) newvalues[i] = values[i];
101 delete [] values;
102 values = newvalues;
106 template<class TYPE>
107 TYPE ArrayList<TYPE>::append(TYPE value) // add to end of list
109 if(total + 1 > available)
111 available *= 2;
112 TYPE* newvalues = new TYPE[available];
113 for(int i = 0; i < total; i++) newvalues[i] = values[i];
114 delete [] values;
115 values = newvalues;
118 values[total++] = value;
119 return value;
122 template<class TYPE>
123 TYPE ArrayList<TYPE>::append() // add to end of list
125 if(total + 1 > available)
127 available *= 2;
128 TYPE* newvalues = new TYPE[available];
129 for(int i = 0; i < total; i++) newvalues[i] = values[i];
130 delete [] values;
131 values = newvalues;
133 total++;
135 return values[total - 1];
138 template<class TYPE>
139 TYPE ArrayList<TYPE>::insert(TYPE value, int number)
141 append(0);
142 for(int i = total - 1; i > number; i--)
144 values[i] = values[i - 1];
146 values[number] = value;
149 template<class TYPE>
150 void ArrayList<TYPE>::remove(TYPE value) // remove value from anywhere in list
152 int in, out;
154 for(in = 0, out = 0; in < total;)
156 if(values[in] != value) values[out++] = values[in++];
157 else
159 in++;
162 total = out;
165 template<class TYPE>
166 TYPE ArrayList<TYPE>::last() // last element in list
168 return values[total - 1];
173 template<class TYPE>
174 void ArrayList<TYPE>::remove_object(TYPE value) // remove value from anywhere in list
176 remove(value);
177 switch (removeobject_type)
179 case ARRAYLIST_REMOVEOBJECT_DELETE:
180 delete value;
181 break;
182 case ARRAYLIST_REMOVEOBJECT_DELETEARRAY:
183 delete [] value;
184 break;
185 case ARRAYLIST_REMOVEOBJECT_FREE:
186 free(value);
187 break;
188 default:
189 printf("Unknown function to use to free array\n");
190 break;
194 template<class TYPE>
195 void ArrayList<TYPE>::remove_object_number(int number)
197 if(number < total)
199 switch (removeobject_type)
201 case ARRAYLIST_REMOVEOBJECT_DELETE:
202 delete values[number];
203 break;
204 case ARRAYLIST_REMOVEOBJECT_DELETEARRAY:
205 delete [] values[number];
206 break;
207 case ARRAYLIST_REMOVEOBJECT_FREE:
208 free(values[number]);
209 break;
210 default:
211 printf("Unknown function to use to free array\n");
212 break;
215 remove_number(number);
217 else
218 fprintf(stderr, "ArrayList<TYPE>::remove_object_number: number %d out of range %s.\n", number, total);
222 template<class TYPE>
223 void ArrayList<TYPE>::remove_object() // remove last object from array
225 if(total)
227 switch (removeobject_type)
229 case ARRAYLIST_REMOVEOBJECT_DELETE:
230 delete values[total - 1];
231 break;
232 case ARRAYLIST_REMOVEOBJECT_DELETEARRAY:
233 delete [] values[total -1];
234 break;
235 case ARRAYLIST_REMOVEOBJECT_FREE:
236 free(values[total - 1]);
237 break;
238 default:
239 printf("Unknown function to use to free array\n");
240 break;
243 remove();
245 else
246 fprintf(stderr, "ArrayList<TYPE>::remove_object: array is 0 length.\n");
251 template<class TYPE>
252 void ArrayList<TYPE>::remove()
254 total--;
257 // remove pointer from anywhere in list
258 template<class TYPE>
259 void ArrayList<TYPE>::remove_number(int number)
261 int in, out;
262 for(in = 0, out = 0; in < total;)
264 if(in != number)
265 values[out++] = values[in++];
266 else
267 // need to delete it here
268 in++;
270 total = out;
273 template<class TYPE>
274 void ArrayList<TYPE>::remove_all_objects()
276 //printf("ArrayList<TYPE>::remove_all_objects 1 %d\n", total);
277 for(int i = 0; i < total; i++)
279 switch (removeobject_type)
281 case ARRAYLIST_REMOVEOBJECT_DELETE:
282 delete values[i];
283 break;
284 case ARRAYLIST_REMOVEOBJECT_DELETEARRAY:
285 delete [] values[i];
286 break;
287 case ARRAYLIST_REMOVEOBJECT_FREE:
288 free(values[i]);
289 break;
290 default:
291 printf("Unknown function to use to free array\n");
292 break;
295 total = 0;
298 template<class TYPE>
299 void ArrayList<TYPE>::remove_all()
301 total = 0;
304 // sort from least to greatest value
305 template<class TYPE>
306 void ArrayList<TYPE>::sort()
308 int result = 1;
309 TYPE temp;
311 while(result)
313 result = 0;
314 for(int i = 0, j = 1; j < total; i++, j++)
316 if(values[j] < values[i])
318 temp = values[i];
319 values[i] = values[j];
320 values[j] = temp;
321 result = 1;
327 template<class TYPE>
328 int ArrayList<TYPE>::number_of(TYPE object)
330 for(int i = 0; i < total; i++)
332 if(values[i] == object) return i;
334 return 0;
338 #endif