quakeforge/ruamoko/lib/Array.r

91 lines
1.5 KiB
R
Raw Normal View History

2002-10-31 23:00:40 +00:00
#include "Array.h"
@implementation Array
- (id) 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 = (void[][]) obj_malloc (inc * @sizeof (void []));
2002-10-31 23:00:40 +00:00
return self;
}
- (void) free
{
local integer i;
for (i = 0; i < count; i++)
obj_free (array[i]);
obj_free (array);
2002-10-31 23:00:40 +00:00
}
- (void []) getItemAt: (integer) index
{
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: (void []) item
{
if (index == -1)
index = count - 1;
if (index < 0 || index >= count)
2002-10-31 23:00:40 +00:00
return;
array[index] = item;
2002-10-31 23:00:40 +00:00
}
- (void) addItem: (void []) item
{
if (count == size) {
size += incr;
array = (void[][])obj_realloc (array, size * @sizeof (void []));
2002-10-31 23:00:40 +00:00
}
array[count++] = item;
2002-10-31 23:00:40 +00:00
}
- (void []) removeItemAt: (integer) index
{
local integer i;
local void [] item;
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];
2002-10-31 23:00:40 +00:00
return item;
}
- (void []) insertItemAt: (integer) index item:(void []) item
{
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 = (void[][])obj_realloc (array, size * @sizeof (void []));
2002-10-31 23:00:40 +00:00
}
for (i = count; i > index; i--)
array[i] = array[i - 1];
array[index] = item;
count++;
2002-10-31 23:00:40 +00:00
return item;
}
@end