quakeforge/ruamoko/lib/Array.r
2003-04-07 22:34:39 +00:00

95 lines
1.5 KiB
R

#include "Array.h"
@implementation Array
- (id) init
{
count = size = 0;
incr = 16;
array = NIL;
return self;
}
- (id) initWithIncrement: (integer) inc
{
count = 0;
size = incr = inc;
array = (void[][]) obj_malloc (inc * @sizeof (void []));
return self;
}
- (void) free
{
local integer i;
for (i = 0; i < count; i++)
obj_free (array[i]);
obj_free (array);
}
- (void []) getItemAt: (integer) index
{
if (index == -1)
index = count - 1;
if (index < 0 || index >= count)
return NIL;
return array[index];
}
- (void) setItemAt: (integer) index item: (void []) item
{
if (index == -1)
index = count - 1;
if (index < 0 || index >= count)
return;
array[index] = item;
}
- (void) addItem: (void []) item
{
if (count == size) {
size += incr;
array = (void[][])obj_realloc (array, size * @sizeof (void []));
}
array[count++] = item;
}
- (void []) removeItemAt: (integer) index
{
local integer i;
local void [] item;
if (index == -1)
index = count -1;
if (index < 0 || index >= count)
return NIL;
item = array[index];
count--;
for (i = index; i < count; i++)
array[i] = array[i + 1];
return item;
}
- (void []) insertItemAt: (integer) index item:(void []) item
{
local integer i;
if (index == -1)
index = count -1;
if (index < 0 || index >= count)
return NIL;
if (count == size) {
size += incr;
array = (void[][])obj_realloc (array, size * @sizeof (void []));
}
for (i = count; i > index; i--)
array[i] = array[i - 1];
array[index] = item;
count++;
return item;
}
- (integer) count
{
return count;
}
@end