2002-10-31 23:00:40 +00:00
|
|
|
#include "Array.h"
|
|
|
|
|
|
|
|
@implementation Array
|
|
|
|
|
|
|
|
- (id) init
|
|
|
|
{
|
2002-11-01 22:53:02 +00:00
|
|
|
count = size = 0;
|
|
|
|
incr = 16;
|
|
|
|
array = NIL;
|
2002-10-31 23:00:40 +00:00
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (id) initWithIncrement: (integer) inc
|
|
|
|
{
|
2002-11-01 22:53:02 +00:00
|
|
|
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;
|
2002-11-01 22:53:02 +00:00
|
|
|
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)
|
2002-11-01 22:53:02 +00:00
|
|
|
index = count - 1;
|
|
|
|
if (index < 0 || index >= count)
|
2002-10-31 23:00:40 +00:00
|
|
|
return NIL;
|
2002-11-01 22:53:02 +00:00
|
|
|
return array[index];
|
2002-10-31 23:00:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void) setItemAt: (integer) index item: (void []) item
|
|
|
|
{
|
|
|
|
if (index == -1)
|
2002-11-01 22:53:02 +00:00
|
|
|
index = count - 1;
|
|
|
|
if (index < 0 || index >= count)
|
2002-10-31 23:00:40 +00:00
|
|
|
return;
|
2002-11-01 22:53:02 +00:00
|
|
|
array[index] = item;
|
2002-10-31 23:00:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void) addItem: (void []) item
|
|
|
|
{
|
2002-11-01 22:53:02 +00:00
|
|
|
if (count == size) {
|
|
|
|
size += incr;
|
|
|
|
array = (void[][])obj_realloc (array, size * @sizeof (void []));
|
2002-10-31 23:00:40 +00:00
|
|
|
}
|
2002-11-01 22:53:02 +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)
|
2002-11-01 22:53:02 +00:00
|
|
|
index = count -1;
|
|
|
|
if (index < 0 || index >= count)
|
2002-10-31 23:00:40 +00:00
|
|
|
return NIL;
|
2002-11-01 22:53:02 +00:00
|
|
|
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)
|
2002-11-01 22:53:02 +00:00
|
|
|
index = count -1;
|
|
|
|
if (index < 0 || index >= count)
|
2002-10-31 23:00:40 +00:00
|
|
|
return NIL;
|
2002-11-01 22:53:02 +00:00
|
|
|
if (count == size) {
|
|
|
|
size += incr;
|
|
|
|
array = (void[][])obj_realloc (array, size * @sizeof (void []));
|
2002-10-31 23:00:40 +00:00
|
|
|
}
|
2002-11-01 22:53:02 +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
|