quakeforge/ruamoko/lib/Array.r
Brian Koropoff f9ff908157 Changed the Array class to use object ids instead of null pointers for easy
and safe freeing of elements when an Array is disposed.
2003-05-23 04:27:30 +00:00

108 lines
1.7 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 = (id []) obj_malloc (inc * @sizeof (id));
return self;
}
- (void) free
{
local integer i;
for (i = 0; i < count; i++)
[array[i] free];
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 = (id [])obj_realloc (array, size * @sizeof (id));
}
array[count++] = item;
}
- (void) removeItem: (void []) item
{
local integer i, n;
for (i = 0; i < count; i++)
if (array[i] == item) {
count--;
for (n = i; n < count; n++)
array[n] = array[n + 1];
}
return;
}
- (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 = (id [])obj_realloc (array, size * @sizeof (id));
}
for (i = count; i > index; i--)
array[i] = array[i - 1];
array[index] = item;
count++;
return item;
}
- (integer) count
{
return count;
}
@end