fix YSArray

main
sandyx86 1 year ago
parent d2cfb8c4c4
commit 3a6f54cb9d

Binary file not shown.

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

@ -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;
}
[self setCapacity: cap + 1];
} else {
array = realloc(array, (count + 1) * sizeof(id));
if (array == NULL) {
return;
}
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

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

Loading…
Cancel
Save