2002-10-31 23:00:40 +00:00
|
|
|
#include "Array.h"
|
|
|
|
|
|
|
|
@implementation Array
|
|
|
|
|
|
|
|
- (id) init
|
|
|
|
{
|
2005-05-06 23:06:50 +00:00
|
|
|
self = [super 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;
|
|
|
|
}
|
|
|
|
|
2004-02-04 00:56:33 +00:00
|
|
|
- (void) dealloc
|
2002-10-31 23:00:40 +00:00
|
|
|
{
|
|
|
|
local integer i;
|
2005-05-06 23:06:50 +00:00
|
|
|
for (i = 0; i < count; i++) {
|
|
|
|
if (array[i])
|
|
|
|
[array[i] release];
|
|
|
|
}
|
|
|
|
if (array) {
|
|
|
|
obj_free (array);
|
|
|
|
}
|
2004-02-04 00:56:33 +00:00
|
|
|
[super dealloc];
|
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;
|
2005-05-06 23:06:50 +00:00
|
|
|
[array[index] release];
|
2002-11-01 22:53:02 +00:00
|
|
|
array[index] = item;
|
2005-05-02 02:32:03 +00:00
|
|
|
[item retain];
|
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;
|
2005-05-02 02:32:03 +00:00
|
|
|
[item retain];
|
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--;
|
2006-12-20 11:33:40 +00:00
|
|
|
for (n = i--; n < count; n++)
|
2003-05-14 21:11:23 +00:00
|
|
|
array[n] = array[n + 1];
|
2006-12-20 11:33:40 +00:00
|
|
|
[item release];
|
2003-05-14 21:11:23 +00:00
|
|
|
}
|
|
|
|
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];
|
2005-05-02 02:32:03 +00:00
|
|
|
[item release];
|
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++;
|
2005-05-02 02:32:03 +00:00
|
|
|
[item retain];
|
2002-10-31 23:00:40 +00:00
|
|
|
return item;
|
|
|
|
}
|
|
|
|
|
2003-04-07 22:34:39 +00:00
|
|
|
- (integer) count
|
|
|
|
{
|
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
2006-12-16 13:59:29 +00:00
|
|
|
- (integer) findItem: (id) item
|
|
|
|
{
|
|
|
|
local integer i;
|
|
|
|
for (i = 0; i < count; i++)
|
|
|
|
if (array[i] == item)
|
|
|
|
return i;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2003-07-24 20:50:40 +00:00
|
|
|
-(void)makeObjectsPerformSelector:(SEL)selector
|
|
|
|
{
|
|
|
|
local integer i;
|
|
|
|
for (i = 0; i < count; i++)
|
2003-07-29 19:55:41 +00:00
|
|
|
[array[i] performSelector:selector];
|
2003-07-24 20:50:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
-(void)makeObjectsPerformSelector:(SEL)selector withObject:(id)arg
|
|
|
|
{
|
|
|
|
local integer i;
|
|
|
|
for (i = 0; i < count; i++)
|
2003-07-29 19:55:41 +00:00
|
|
|
[array[i] performSelector:selector withObject:arg];
|
2003-07-24 20:50:40 +00:00
|
|
|
}
|
|
|
|
|
2002-10-31 23:00:40 +00:00
|
|
|
@end
|