You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

56 lines
1.1 KiB
Objective-C

#import "YSArray.h"
#include <string.h>
#include <stdio.h>
@implementation YSArray
-(id) init {
self = [super init];
array = NULL;
return self;
}
-(void) addObject: (id) theObject {
printf("%s\n", "YSArray: addObject");
if (array == NULL) {
printf("%s\n", "before malloc");
array = malloc(sizeof(id));
if (array == NULL) {
printf("%s\n", "addObject: malloc failed.");
return;
}
printf("%s\n", "after malloc");
} else {
printf("%s\n", "before realloc");
array = realloc(array, count * sizeof(id));
if (array == NULL) {
printf("%s\n", "addObject: realloc failed.");
return;
}
}
array[count++] = theObject;
printf("%s\n", "YSArray: object added.");
}
-(void) removeObject: (unsigned int) index {
if (count == 0 || count == 1) {
array[0] = NULL;
return;
}
if (index >= count) {
return;
}
if (index == count - 1) {
array[count - 1] = NULL;
}
//hope count > index;
memcpy(&array[index], &array[index + 1], (count - index) * sizeof(id));
}
@end