implement a dynamic array class

This commit is contained in:
Bill Currie 2002-10-31 23:00:40 +00:00
parent 1a6a9f177d
commit 9dc9ee537d
4 changed files with 116 additions and 2 deletions

22
ruamoko/include/Array.h Normal file
View file

@ -0,0 +1,22 @@
#ifndef __ruamoko_Array_h
#define __ruamoko_Array_h
#include "Object.h"
@interface Array: Object
{
integer count, size;
integer incr;
(void [])[]array;
}
- (id) init;
- (id) initWithIncrement: (integer) inc;
- (void) free;
- (void []) getItemAt: (integer) index;
- (void) setItemAt: (integer) index item:(void []) item;
- (void) addItem: (void []) item;
- (void []) removeItemAt: (integer) index;
- (void []) insertItemAt: (integer) index item:(void []) item;
@end
#endif//__ruamoko_Array_h

View file

@ -7,4 +7,4 @@ include_HEADERS= \
draw.h key.h \
\
cbuf.h cmd.h cvar.h file.h \
Object.h Entity.h InputLine.h Point.h Rect.h Size.h
Object.h Array.h Entity.h InputLine.h Point.h Rect.h Size.h

92
ruamoko/lib/Array.r Normal file
View file

@ -0,0 +1,92 @@
#include "Array.h"
@implementation Array
- (id) init
{
self.count = self.size = 0;
self.incr = 16;
self.array = NIL;
return self;
}
- (id) initWithIncrement: (integer) inc
{
self.count = 0;
self.size = self.incr = inc;
self.array = (void[][]) obj_malloc (inc * @sizeof (void []));
return self;
}
- (void) free
{
local integer i;
for (i = 0; i < self.count; i++)
obj_free (self.array[i]);
obj_free (self.array);
}
- (void []) getItemAt: (integer) index
{
if (index == -1)
index = self.count - 1;
if (index < 0 || index >= self.count)
return NIL;
return self.array[index];
}
- (void) setItemAt: (integer) index item: (void []) item
{
if (index == -1)
index = self.count - 1;
if (index < 0 || index >= self.count)
return;
self.array[index] = item;
}
- (void) addItem: (void []) item
{
if (self.count == self.size) {
self.size += self.incr;
self.array = (void[][])obj_realloc (self.array,
self.size * @sizeof (void []));
}
self.array[self.count++] = item;
}
- (void []) removeItemAt: (integer) index
{
local integer i;
local void [] item;
if (index == -1)
index = self.count -1;
if (index < 0 || index >= self.count)
return NIL;
item = self.array[index];
self.count--;
for (i = index; i < self.count; i++)
self.array[i] = self.array[i + 1];
return item;
}
- (void []) insertItemAt: (integer) index item:(void []) item
{
local integer i;
if (index == -1)
index = self.count -1;
if (index < 0 || index >= self.count)
return NIL;
if (self.count == self.size) {
self.size += self.incr;
self.array = (void[][])obj_realloc (self.array,
self.size * @sizeof (void []));
}
for (i = self.count; i > index; i--)
self.array[i] = self.array[i - 1];
self.array[index] = item;
self.count++;
return item;
}
@end

View file

@ -27,7 +27,7 @@ EXTRA_LIBRARIES= $(ruamoko_libs)
libr_a_SOURCES=\
crudefile.r debug.r entities.r infokey.r math.r message.r nq_message.r \
physics.r qw_message.r qw_physics.r qw_sys.r sound.r string.r system.r \
Object.r Entity.r Point.r Size.r Rect.r
Object.r Array.r Entity.r Point.r Size.r Rect.r
libr_a_AR=$(PAK) -cf
libgui_a_SOURCES=\