4 // designed for lists of track numbers
9 #define ARRAYLIST_REMOVEOBJECT_DELETE 0
10 #define ARRAYLIST_REMOVEOBJECT_DELETEARRAY 1
11 #define ARRAYLIST_REMOVEOBJECT_FREE 2
20 TYPE
append(TYPE value
);
22 TYPE
insert(TYPE value
, int number
);
25 void allocate(int total
);
26 // remove last pointer from end
28 // remove last pointer and object from end
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
);
41 // Remove pointer and objects for each array entry
42 void remove_all_objects();
43 // Get last element in list
45 // Call this if the TYPE is a pointer to an array which must be
46 // deleted by delete [].
47 void set_array_delete();
57 int removeobject_type
;
61 ArrayList
<TYPE
>::ArrayList()
65 removeobject_type
= ARRAYLIST_REMOVEOBJECT_DELETE
;
66 values
= new TYPE
[available
];
71 ArrayList
<TYPE
>::~ArrayList()
73 // Just remove the pointer
79 void ArrayList
<TYPE
>::set_array_delete()
81 removeobject_type
= ARRAYLIST_REMOVEOBJECT_DELETEARRAY
;
86 void ArrayList
<TYPE
>::set_free()
88 removeobject_type
= ARRAYLIST_REMOVEOBJECT_FREE
;
94 void ArrayList
<TYPE
>::allocate(int total
)
99 TYPE
* newvalues
= new TYPE
[available
];
100 for(int i
= 0; i
< total
; i
++) newvalues
[i
] = values
[i
];
107 TYPE ArrayList
<TYPE
>::append(TYPE value
) // add to end of list
109 if(total
+ 1 > available
)
112 TYPE
* newvalues
= new TYPE
[available
];
113 for(int i
= 0; i
< total
; i
++) newvalues
[i
] = values
[i
];
118 values
[total
++] = value
;
123 TYPE ArrayList
<TYPE
>::append() // add to end of list
125 if(total
+ 1 > available
)
128 TYPE
* newvalues
= new TYPE
[available
];
129 for(int i
= 0; i
< total
; i
++) newvalues
[i
] = values
[i
];
135 return values
[total
- 1];
139 TYPE ArrayList
<TYPE
>::insert(TYPE value
, int number
)
142 for(int i
= total
- 1; i
> number
; i
--)
144 values
[i
] = values
[i
- 1];
146 values
[number
] = value
;
150 void ArrayList
<TYPE
>::remove(TYPE value
) // remove value from anywhere in list
154 for(in
= 0, out
= 0; in
< total
;)
156 if(values
[in
] != value
) values
[out
++] = values
[in
++];
166 TYPE ArrayList
<TYPE
>::last() // last element in list
168 return values
[total
- 1];
174 void ArrayList
<TYPE
>::remove_object(TYPE value
) // remove value from anywhere in list
177 switch (removeobject_type
)
179 case ARRAYLIST_REMOVEOBJECT_DELETE
:
182 case ARRAYLIST_REMOVEOBJECT_DELETEARRAY
:
185 case ARRAYLIST_REMOVEOBJECT_FREE
:
189 printf("Unknown function to use to free array\n");
195 void ArrayList
<TYPE
>::remove_object_number(int number
)
199 switch (removeobject_type
)
201 case ARRAYLIST_REMOVEOBJECT_DELETE
:
202 delete values
[number
];
204 case ARRAYLIST_REMOVEOBJECT_DELETEARRAY
:
205 delete [] values
[number
];
207 case ARRAYLIST_REMOVEOBJECT_FREE
:
208 free(values
[number
]);
211 printf("Unknown function to use to free array\n");
215 remove_number(number
);
218 fprintf(stderr
, "ArrayList<TYPE>::remove_object_number: number %d out of range %s.\n", number
, total
);
223 void ArrayList
<TYPE
>::remove_object() // remove last object from array
227 switch (removeobject_type
)
229 case ARRAYLIST_REMOVEOBJECT_DELETE
:
230 delete values
[total
- 1];
232 case ARRAYLIST_REMOVEOBJECT_DELETEARRAY
:
233 delete [] values
[total
-1];
235 case ARRAYLIST_REMOVEOBJECT_FREE
:
236 free(values
[total
- 1]);
239 printf("Unknown function to use to free array\n");
246 fprintf(stderr
, "ArrayList<TYPE>::remove_object: array is 0 length.\n");
252 void ArrayList
<TYPE
>::remove()
257 // remove pointer from anywhere in list
259 void ArrayList
<TYPE
>::remove_number(int number
)
262 for(in
= 0, out
= 0; in
< total
;)
265 values
[out
++] = values
[in
++];
267 // need to delete it here
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
:
284 case ARRAYLIST_REMOVEOBJECT_DELETEARRAY
:
287 case ARRAYLIST_REMOVEOBJECT_FREE
:
291 printf("Unknown function to use to free array\n");
299 void ArrayList
<TYPE
>::remove_all()
304 // sort from least to greatest value
306 void ArrayList
<TYPE
>::sort()
314 for(int i
= 0, j
= 1; j
< total
; i
++, j
++)
316 if(values
[j
] < values
[i
])
319 values
[i
] = values
[j
];
328 int ArrayList
<TYPE
>::number_of(TYPE object
)
330 for(int i
= 0; i
< total
; i
++)
332 if(values
[i
] == object
) return i
;