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;
|
2003-05-23 04:27:30 +00:00
|
|
|
array = (id []) obj_malloc (inc * @sizeof (id));
|
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++)
|
2003-05-23 04:27:30 +00:00
|
|
|
[array[i] free];
|
2002-11-01 22:53:02 +00:00
|
|
|
obj_free (array);
|
2002-10-31 23:00:40 +00:00
|
|
|
}
|
|
|
|
|
2003-05-23 04:38:08 +00:00
|
|
|
- (id) getItemAt: (integer) index
|
2002-10-31 23:00:40 +00:00
|
|
|
{
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2003-05-23 04:38:08 +00:00
|
|
|
- (void) setItemAt: (integer) index item: (id) item
|
2002-10-31 23:00:40 +00:00
|
|
|
{
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2003-05-23 04:38:08 +00:00
|
|
|
- (void) addItem: (id) item
|
2002-10-31 23:00:40 +00:00
|
|
|
{
|
2002-11-01 22:53:02 +00:00
|
|
|
if (count == size) {
|
|
|
|
size += incr;
|
2003-05-23 04:27:30 +00:00
|
|
|
array = (id [])obj_realloc (array, size * @sizeof (id));
|
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
|
|
|
}
|
|
|
|
|
2003-05-23 04:38:08 +00:00
|
|
|
- (void) removeItem: (id) item
|
2003-05-14 21:11:23 +00:00
|
|
|
{
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2003-05-23 04:38:08 +00:00
|
|
|
- (id) removeItemAt: (integer) index
|
2002-10-31 23:00:40 +00:00
|
|
|
{
|
|
|
|
local integer i;
|
2003-05-23 04:38:08 +00:00
|
|
|
local id item;
|
2002-10-31 23:00:40 +00:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2003-05-23 04:38:08 +00:00
|
|
|
- (id) insertItemAt: (integer) index item:(id) item
|
2002-10-31 23:00:40 +00:00
|
|
|
{
|
|
|
|
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;
|
2003-05-23 04:27:30 +00:00
|
|
|
array = (id [])obj_realloc (array, size * @sizeof (id));
|
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;
|
|
|
|
}
|
|
|
|
|
2003-04-07 22:34:39 +00:00
|
|
|
- (integer) count
|
|
|
|
{
|
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
2002-10-31 23:00:40 +00:00
|
|
|
@end
|