|
|
|
|
@ -6,45 +6,38 @@
|
|
|
|
|
-(id) init {
|
|
|
|
|
self = [super init];
|
|
|
|
|
array = malloc(sizeof(id)); //allocate for one id
|
|
|
|
|
cap = 1;
|
|
|
|
|
count = 0;
|
|
|
|
|
return self;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//add an object to the array, capacity increases with every object
|
|
|
|
|
-(void) addObject: (id) theObject {
|
|
|
|
|
|
|
|
|
|
if (array == NULL) {
|
|
|
|
|
//it needs malloced
|
|
|
|
|
array = malloc(sizeof(id));
|
|
|
|
|
|
|
|
|
|
if (array == NULL) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
array = realloc(array, (count + 1) * sizeof(id));
|
|
|
|
|
[self setCapacity: cap + 1];
|
|
|
|
|
|
|
|
|
|
if (array == NULL) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
array[count++] = theObject;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//remove an object from the array, capacity decreases with every object
|
|
|
|
|
-(void) removeObject: (unsigned int) index {
|
|
|
|
|
if (count == 0 || count == 1) {
|
|
|
|
|
array[0] = NULL;
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (index >= count) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
//i think this will work
|
|
|
|
|
memcpy(&array[index], &array[index + 1], (cap - index) * sizeof(id));
|
|
|
|
|
count--;
|
|
|
|
|
[self setCapacity: cap - 1];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (index == count - 1) {
|
|
|
|
|
array[count - 1] = NULL;
|
|
|
|
|
}
|
|
|
|
|
-(unsigned int) capacity {
|
|
|
|
|
return cap;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//hope count > index;
|
|
|
|
|
memcpy(&array[index], &array[index + 1], (count - index) * sizeof(id));
|
|
|
|
|
//setCapacity reallocs the array to the specified capacity
|
|
|
|
|
-(void) setCapacity: (unsigned int) c {
|
|
|
|
|
cap = c;
|
|
|
|
|
array = realloc(array, cap * sizeof(id));
|
|
|
|
|
}
|
|
|
|
|
@end
|