quakeforge/ruamoko/lib/Array.r

135 lines
2.2 KiB
R
Raw Normal View History

2002-10-31 23:00:40 +00:00
#include "Array.h"
@implementation Array
- (id) init
{
self = [super init];
count = size = 0;
incr = 16;
array = NIL;
2002-10-31 23:00:40 +00:00
return self;
}
- (id) initWithIncrement: (integer) inc
{
count = 0;
size = incr = inc;
array = (id []) obj_malloc (inc * @sizeof (id));
2002-10-31 23:00:40 +00:00
return self;
}
- (void) dealloc
2002-10-31 23:00:40 +00:00
{
local integer i;
for (i = 0; i < count; i++) {
if (array[i])
[array[i] release];
}
if (array) {
obj_free (array);
}
[super dealloc];
2002-10-31 23:00:40 +00:00
}
- (id) getItemAt: (integer) index
2002-10-31 23:00:40 +00:00
{
if (index == -1)
index = count - 1;
if (index < 0 || index >= count)
2002-10-31 23:00:40 +00:00
return NIL;
return array[index];
2002-10-31 23:00:40 +00:00
}
- (void) setItemAt: (integer) index item: (id) item
2002-10-31 23:00:40 +00:00
{
if (index == -1)
index = count - 1;
if (index < 0 || index >= count)
2002-10-31 23:00:40 +00:00
return;
[array[index] release];
array[index] = item;
[item retain];
2002-10-31 23:00:40 +00:00
}
- (void) addItem: (id) item
2002-10-31 23:00:40 +00:00
{
if (count == size) {
size += incr;
array = (id [])obj_realloc (array, size * @sizeof (id));
2002-10-31 23:00:40 +00:00
}
array[count++] = item;
[item retain];
2002-10-31 23:00:40 +00:00
}
- (void) removeItem: (id) item
{
local integer i, n;
for (i = 0; i < count; i++)
if (array[i] == item) {
[item release];
count--;
for (n = i; n < count; n++)
array[n] = array[n + 1];
}
return;
}
- (id) removeItemAt: (integer) index
2002-10-31 23:00:40 +00:00
{
local integer i;
local id item;
2002-10-31 23:00:40 +00:00
if (index == -1)
index = count -1;
if (index < 0 || index >= count)
2002-10-31 23:00:40 +00:00
return NIL;
item = array[index];
count--;
for (i = index; i < count; i++)
array[i] = array[i + 1];
[item release];
2002-10-31 23:00:40 +00:00
return item;
}
- (id) insertItemAt: (integer) index item:(id) item
2002-10-31 23:00:40 +00:00
{
local integer i;
if (index == -1)
index = count -1;
if (index < 0 || index >= count)
2002-10-31 23:00:40 +00:00
return NIL;
if (count == size) {
size += incr;
array = (id [])obj_realloc (array, size * @sizeof (id));
2002-10-31 23:00:40 +00:00
}
for (i = count; i > index; i--)
array[i] = array[i - 1];
array[index] = item;
count++;
[item retain];
2002-10-31 23:00:40 +00:00
return item;
}
- (integer) count
{
return count;
}
-(void)makeObjectsPerformSelector:(SEL)selector
{
local integer i;
for (i = 0; i < count; i++)
2003-07-29 19:55:41 +00:00
[array[i] performSelector:selector];
}
-(void)makeObjectsPerformSelector:(SEL)selector withObject:(id)arg
{
local integer i;
for (i = 0; i < count; i++)
2003-07-29 19:55:41 +00:00
[array[i] performSelector:selector withObject:arg];
}
2002-10-31 23:00:40 +00:00
@end