r125: This commit was manufactured by cvs2svn to create tag 'r1_1_7-last'.
[cinelerra_cv/mob.git] / hvirtual / guicast / arraylist.h
blob55e01a8dadbc7e21a97db3342774d5024151dcd2
1 #ifndef ARRAYLIST_H
2 #define ARRAYLIST_H
4 // designed for lists of track numbers
6 template<class TYPE>
7 class ArrayList
9 public:
10 ArrayList();
11 virtual ~ArrayList();
13 TYPE append(TYPE value);
14 TYPE append();
15 TYPE insert(TYPE value, int number);
17 // allocate
18 void allocate(int total);
19 // remove last pointer from end
20 void remove();
21 // remove last pointer and object from end
22 void remove_object();
23 // remove pointer to object from list
24 void remove(TYPE value);
25 // remove object and pointer to it from list
26 void remove_object(TYPE value);
27 // remove object and pointer to it from list
28 void remove_object_number(int number);
29 // remove pointer to item numbered
30 void remove_number(int number);
31 // Return number of first object matching argument
32 int number_of(TYPE object);
33 void remove_all();
34 // Remove pointer and objects for each array entry
35 void remove_all_objects();
36 TYPE last();
37 void set_array_delete();
39 void sort();
41 TYPE* values;
42 int total;
44 private:
45 int available;
46 int array_delete;
49 template<class TYPE>
50 ArrayList<TYPE>::ArrayList()
52 total = 0;
53 available = 1;
54 array_delete = 0;
55 values = new TYPE[available];
59 template<class TYPE>
60 ArrayList<TYPE>::~ArrayList()
62 // Just remove the pointer
63 delete [] values;
66 template<class TYPE>
67 void ArrayList<TYPE>::set_array_delete()
69 array_delete = 1;
73 template<class TYPE>
74 void ArrayList<TYPE>::allocate(int total)
76 if(total > available)
78 available = total;
79 TYPE* newvalues = new TYPE[available];
80 for(int i = 0; i < total; i++) newvalues[i] = values[i];
81 delete [] values;
82 values = newvalues;
86 template<class TYPE>
87 TYPE ArrayList<TYPE>::append(TYPE value) // add to end of list
89 if(total + 1 > available)
91 available *= 2;
92 TYPE* newvalues = new TYPE[available];
93 for(int i = 0; i < total; i++) newvalues[i] = values[i];
94 delete [] values;
95 values = newvalues;
98 values[total++] = value;
99 return value;
102 template<class TYPE>
103 TYPE ArrayList<TYPE>::append() // add to end of list
105 if(total + 1 > available)
107 available *= 2;
108 TYPE* newvalues = new TYPE[available];
109 for(int i = 0; i < total; i++) newvalues[i] = values[i];
110 delete [] values;
111 values = newvalues;
113 total++;
115 return values[total - 1];
118 template<class TYPE>
119 TYPE ArrayList<TYPE>::insert(TYPE value, int number)
121 append(0);
122 for(int i = total - 1; i > number; i--)
124 values[i] = values[i - 1];
126 values[number] = value;
129 template<class TYPE>
130 void ArrayList<TYPE>::remove(TYPE value) // remove value from anywhere in list
132 int in, out;
134 for(in = 0, out = 0; in < total;)
136 if(values[in] != value) values[out++] = values[in++];
137 else
139 in++;
142 total = out;
145 template<class TYPE>
146 TYPE ArrayList<TYPE>::last() // last element in list
148 return values[total - 1];
153 template<class TYPE>
154 void ArrayList<TYPE>::remove_object(TYPE value) // remove value from anywhere in list
156 remove(value);
157 if (array_delete)
158 delete [] value;
159 else
160 delete value;
163 template<class TYPE>
164 void ArrayList<TYPE>::remove_object_number(int number)
166 if(number < total)
168 if (array_delete)
169 delete [] values[number];
170 else
171 delete values[number];
172 remove_number(number);
174 else
175 fprintf(stderr, "ArrayList<TYPE>::remove_object_number: number %d out of range %s.\n", number, total);
179 template<class TYPE>
180 void ArrayList<TYPE>::remove_object() // remove value from anywhere in list
182 if(total)
184 if (array_delete)
185 delete [] values[total - 1];
186 else
187 delete values[total - 1];
188 remove();
190 else
191 fprintf(stderr, "ArrayList<TYPE>::remove_object: array is 0 length.\n");
196 template<class TYPE>
197 void ArrayList<TYPE>::remove()
199 total--;
202 // remove pointer from anywhere in list
203 template<class TYPE>
204 void ArrayList<TYPE>::remove_number(int number)
206 int in, out;
207 for(in = 0, out = 0; in < total;)
209 if(in != number)
210 values[out++] = values[in++];
211 else
212 // need to delete it here
213 in++;
215 total = out;
218 template<class TYPE>
219 void ArrayList<TYPE>::remove_all_objects()
221 //printf("ArrayList<TYPE>::remove_all_objects 1 %d\n", total);
222 for(int i = 0; i < total; i++)
224 if(array_delete)
225 delete [] values[i];
226 else
227 delete values[i];
229 total = 0;
232 template<class TYPE>
233 void ArrayList<TYPE>::remove_all()
235 total = 0;
238 // sort from least to greatest value
239 template<class TYPE>
240 void ArrayList<TYPE>::sort()
242 int result = 1;
243 TYPE temp;
245 while(result)
247 result = 0;
248 for(int i = 0, j = 1; j < total; i++, j++)
250 if(values[j] < values[i])
252 temp = values[i];
253 values[i] = values[j];
254 values[j] = temp;
255 result = 1;
261 template<class TYPE>
262 int ArrayList<TYPE>::number_of(TYPE object)
264 for(int i = 0; i < total; i++)
266 if(values[i] == object) return i;
268 return 0;
272 #endif