fix YSArray

main
sandyx86 1 year ago
parent d2cfb8c4c4
commit 3a6f54cb9d

Binary file not shown.

@ -35,7 +35,7 @@ $(BUILD)/%.o: $(SRC)/%.c
$(CC) -c $< -o $@ $(CC) -c $< -o $@
$(BUILD)/%.o: $(SRC)/%.m $(BUILD)/%.o: $(SRC)/%.m
$(CC) -c $< -o $@ $(CC) -c $< -o $@
clean: clean:
rm $(BUILD)/*.o rm $(BUILD)/*.o

@ -6,11 +6,14 @@
@interface YSArray : YSObject { @interface YSArray : YSObject {
@public //public so you can index it directly @public //public so you can index it directly
id *array; id *array;
int count; unsigned int count;
unsigned int cap;
} }
-(void) addObject: (id) theObject; -(void) addObject: (id) theObject;
-(void) removeObject: (unsigned int) index; -(void) removeObject: (unsigned int) index;
-(unsigned int) capacity;
-(void) setCapacity: (unsigned int) c;
@end @end

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

@ -65,6 +65,10 @@ inline id YSDeallocateObject(id theObject) {
return self; return self;
} }
-(void) dealloc {
YSDeallocateObject(self);
}
#if !defined(__MINGW32__) && !defined(__MINGW64__) #if !defined(__MINGW32__) && !defined(__MINGW64__)
-(id) self { -(id) self {
@ -96,9 +100,6 @@ inline id YSDeallocateObject(id theObject) {
return object_getClass(self); return object_getClass(self);
} }
-(void) dealloc {
YSDeallocateObject(self);
}
-(id) copy { -(id) copy {
return [(id)self copyWithZone: YSDefaultMallocZone()]; return [(id)self copyWithZone: YSDefaultMallocZone()];

Loading…
Cancel
Save