2003-07-29 16:01:35 +00:00
|
|
|
#include "List.h"
|
|
|
|
|
2010-01-13 06:34:32 +00:00
|
|
|
struct list_bucket_s {
|
2011-02-13 12:25:36 +00:00
|
|
|
struct list_bucket_s *next;
|
|
|
|
struct list_bucket_s **prev;
|
2003-07-30 04:11:45 +00:00
|
|
|
id obj;
|
|
|
|
};
|
|
|
|
typedef struct list_bucket_s list_bucket_t;
|
|
|
|
|
2003-07-29 16:01:35 +00:00
|
|
|
@implementation List
|
|
|
|
|
|
|
|
- (id) init
|
|
|
|
{
|
|
|
|
count = 0;
|
2011-01-14 03:07:40 +00:00
|
|
|
head = nil;
|
2003-07-29 16:01:35 +00:00
|
|
|
tail = &head;
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
2004-11-11 04:06:00 +00:00
|
|
|
- (void) dealloc
|
2003-07-29 16:01:35 +00:00
|
|
|
{
|
2011-02-13 12:25:36 +00:00
|
|
|
local list_bucket_t *e, *t = nil; //FIXME t uninitialized
|
2003-07-29 16:01:35 +00:00
|
|
|
|
|
|
|
for (e = head; e; e = t) {
|
|
|
|
t = e.next;
|
2004-11-11 04:06:00 +00:00
|
|
|
[e.obj release];
|
2003-07-29 16:01:35 +00:00
|
|
|
obj_free (e);
|
|
|
|
}
|
2004-11-11 04:06:00 +00:00
|
|
|
[super dealloc];
|
2003-07-29 16:01:35 +00:00
|
|
|
}
|
|
|
|
|
2011-03-25 07:46:32 +00:00
|
|
|
- (id) getItemAt: (int) index
|
2003-07-29 16:01:35 +00:00
|
|
|
{
|
2011-02-13 12:25:36 +00:00
|
|
|
local list_bucket_t *e;
|
2003-07-29 16:01:35 +00:00
|
|
|
if (index < 0 || index >= count)
|
2011-01-14 03:07:40 +00:00
|
|
|
return nil;
|
2003-07-29 16:01:35 +00:00
|
|
|
for (e = head; e && index; index--)
|
|
|
|
e = e.next;
|
|
|
|
return e.obj;
|
|
|
|
}
|
|
|
|
|
|
|
|
-(id) head
|
|
|
|
{
|
|
|
|
if (!head)
|
2011-01-14 03:07:40 +00:00
|
|
|
return nil;
|
2003-07-29 16:01:35 +00:00
|
|
|
return head.obj;
|
|
|
|
}
|
|
|
|
|
|
|
|
-(id) tail
|
|
|
|
{
|
2011-02-13 12:25:36 +00:00
|
|
|
local list_bucket_t *e = (list_bucket_t *) tail;
|
2003-07-29 16:01:35 +00:00
|
|
|
if (!e)
|
2011-01-14 03:07:40 +00:00
|
|
|
return nil;
|
2003-07-29 16:01:35 +00:00
|
|
|
return e.obj;
|
|
|
|
}
|
|
|
|
|
|
|
|
-(void) addItemAtHead: (id) item
|
|
|
|
{
|
2011-02-13 12:25:36 +00:00
|
|
|
local list_bucket_t *e = obj_malloc (@sizeof (list_bucket_t));
|
2003-07-29 16:01:35 +00:00
|
|
|
e.obj = item;
|
|
|
|
e.next = head;
|
|
|
|
e.prev = &head;
|
|
|
|
if (head)
|
|
|
|
head.prev = &e.next;
|
|
|
|
head = e;
|
2003-09-03 01:27:57 +00:00
|
|
|
count++;
|
2003-07-29 16:01:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
-(void) addItemAtTail: (id) item
|
|
|
|
{
|
2011-02-13 12:25:36 +00:00
|
|
|
local list_bucket_t *e = obj_malloc (@sizeof (list_bucket_t));
|
2003-07-29 16:01:35 +00:00
|
|
|
e.obj = item;
|
2011-01-14 03:07:40 +00:00
|
|
|
e.next = nil;
|
2003-07-29 16:01:35 +00:00
|
|
|
e.prev = tail;
|
|
|
|
tail[0] = e;
|
|
|
|
tail = &e.next;
|
2003-09-03 01:27:57 +00:00
|
|
|
count++;
|
2003-07-29 16:01:35 +00:00
|
|
|
}
|
|
|
|
|
2003-07-29 17:43:42 +00:00
|
|
|
- (id) removeItem: (id) item
|
2003-07-29 16:01:35 +00:00
|
|
|
{
|
2011-02-13 12:25:36 +00:00
|
|
|
local list_bucket_t *e;
|
2003-07-29 16:01:35 +00:00
|
|
|
|
|
|
|
for (e = head; e; e = e.next) {
|
|
|
|
if (e.obj == item) {
|
|
|
|
e.prev[0] = e.next;
|
|
|
|
if (e.next)
|
|
|
|
e.next.prev = e.prev;
|
2003-07-29 17:43:42 +00:00
|
|
|
obj_free (e);
|
2003-09-03 01:27:57 +00:00
|
|
|
count--;
|
2003-07-29 17:43:42 +00:00
|
|
|
return item;
|
2003-07-29 16:01:35 +00:00
|
|
|
}
|
|
|
|
}
|
2011-01-14 03:07:40 +00:00
|
|
|
return nil;
|
2003-07-29 17:43:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (id) removeItemAtHead
|
|
|
|
{
|
2011-02-13 12:25:36 +00:00
|
|
|
local list_bucket_t *e;
|
2003-07-29 17:43:42 +00:00
|
|
|
local id obj;
|
|
|
|
|
|
|
|
if (!count)
|
2011-01-14 03:07:40 +00:00
|
|
|
return nil;
|
2003-07-29 17:43:42 +00:00
|
|
|
e = head;
|
|
|
|
obj = e.obj;
|
|
|
|
e.prev[0] = e.next;
|
|
|
|
if (e.next)
|
|
|
|
e.next.prev = e.prev;
|
2003-09-03 01:27:57 +00:00
|
|
|
if (tail == &e.next)
|
|
|
|
tail = e.prev;
|
2003-07-29 17:43:42 +00:00
|
|
|
obj_free (e);
|
2003-09-03 01:27:57 +00:00
|
|
|
count--;
|
2003-07-29 17:43:42 +00:00
|
|
|
return obj;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (id) removeItemAtTail
|
|
|
|
{
|
2011-02-13 12:25:36 +00:00
|
|
|
local list_bucket_t *e;
|
2003-07-29 17:43:42 +00:00
|
|
|
local id obj;
|
|
|
|
|
|
|
|
if (!count)
|
2011-01-14 03:07:40 +00:00
|
|
|
return nil;
|
2011-02-13 12:25:36 +00:00
|
|
|
e = (list_bucket_t *) tail;
|
2003-07-29 17:43:42 +00:00
|
|
|
obj = e.obj;
|
|
|
|
e.prev[0] = e.next;
|
|
|
|
if (e.next)
|
|
|
|
e.next.prev = e.prev;
|
|
|
|
obj_free (e);
|
2003-09-03 01:27:57 +00:00
|
|
|
count--;
|
2003-07-29 17:43:42 +00:00
|
|
|
return obj;
|
2003-07-29 16:01:35 +00:00
|
|
|
}
|
|
|
|
|
2011-03-25 07:46:32 +00:00
|
|
|
- (int) count
|
2003-07-29 16:01:35 +00:00
|
|
|
{
|
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
|
|
|
-(void)makeObjectsPerformSelector:(SEL)selector
|
|
|
|
{
|
2011-02-13 12:25:36 +00:00
|
|
|
local list_bucket_t *e;
|
2003-07-29 16:01:35 +00:00
|
|
|
for (e = head; e; e = e.next)
|
2003-07-29 19:55:41 +00:00
|
|
|
[e.obj performSelector:selector];
|
2003-07-29 16:01:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
-(void)makeObjectsPerformSelector:(SEL)selector withObject:(id)arg
|
|
|
|
{
|
2011-02-13 12:25:36 +00:00
|
|
|
local list_bucket_t *e;
|
2003-07-29 16:01:35 +00:00
|
|
|
for (e = head; e; e = e.next)
|
2003-07-29 19:55:41 +00:00
|
|
|
[e.obj performSelector:selector withObject:arg];
|
2003-07-29 16:01:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@end
|