diff --git a/libyeslib.a b/libyeslib.a new file mode 100644 index 0000000..0dc0db0 Binary files /dev/null and b/libyeslib.a differ diff --git a/makefile b/makefile index f70f4bd..33b3ebb 100644 --- a/makefile +++ b/makefile @@ -35,7 +35,7 @@ $(BUILD)/%.o: $(SRC)/%.c $(CC) -c $< -o $@ $(BUILD)/%.o: $(SRC)/%.m - $(CC) -c $< -o $@ + $(CC) -c $< -o $@ clean: rm $(BUILD)/*.o \ No newline at end of file diff --git a/src/YSArray.h b/src/YSArray.h index 4b13992..0b2294b 100644 --- a/src/YSArray.h +++ b/src/YSArray.h @@ -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 diff --git a/src/YSArray.m b/src/YSArray.m index c36aee7..9ffc632 100644 --- a/src/YSArray.m +++ b/src/YSArray.m @@ -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 \ No newline at end of file diff --git a/src/YSObject.m b/src/YSObject.m index 7ae2a5b..f241cbf 100644 --- a/src/YSObject.m +++ b/src/YSObject.m @@ -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()];