From ef1b68e69719e767f067e85db3ce5b8c9f3b3cf2 Mon Sep 17 00:00:00 2001 From: Bill Currie Date: Tue, 29 Jul 2003 16:01:35 +0000 Subject: [PATCH] add a List class --- ruamoko/include/List.h | 31 +++++++++++ ruamoko/include/Makefile.am | 2 +- ruamoko/lib/List.r | 104 ++++++++++++++++++++++++++++++++++++ ruamoko/lib/Makefile.am | 2 +- 4 files changed, 137 insertions(+), 2 deletions(-) create mode 100644 ruamoko/include/List.h create mode 100644 ruamoko/lib/List.r diff --git a/ruamoko/include/List.h b/ruamoko/include/List.h new file mode 100644 index 000000000..feac1bfc2 --- /dev/null +++ b/ruamoko/include/List.h @@ -0,0 +1,31 @@ +#ifndef __ruamoko_List_h +#define __ruamoko_List_h + +#include "Object.h" + +struct list_bucket_t = { + list_bucket_t [] next; + list_bucket_t [][] prev; + id obj; +}; + +@interface List: Object +{ + integer count; + list_bucket_t [] head; + list_bucket_t [][] tail; +} +- (id) init; +- (void) free; +- (id) getItemAt: (integer) index; +- (id) head; +- (id) tail; +- (void) addItemAtHead: (id) item; +- (void) addItemAtTail: (id) item; +- (void) removeItem: (id) item; +- (integer) count; +-(void)makeObjectsPerformSelector:(SEL)selector; +-(void)makeObjectsPerformSelector:(SEL)selector withObject:(id)arg; +@end + +#endif//__ruamoko_List_h diff --git a/ruamoko/include/Makefile.am b/ruamoko/include/Makefile.am index e419e7857..b1cb24215 100644 --- a/ruamoko/include/Makefile.am +++ b/ruamoko/include/Makefile.am @@ -8,4 +8,4 @@ include_HEADERS= \ draw.h key.h \ \ cbuf.h cmd.h cvar.h file.h gib.h hash.h plist.h \ - Object.h Array.h Entity.h InputLine.h Point.h Rect.h Size.h + Object.h Array.h Entity.h InputLine.h List.h Point.h Rect.h Size.h diff --git a/ruamoko/lib/List.r b/ruamoko/lib/List.r new file mode 100644 index 000000000..1589825c7 --- /dev/null +++ b/ruamoko/lib/List.r @@ -0,0 +1,104 @@ +#include "List.h" + +@implementation List + +- (id) init +{ + count = 0; + head = NIL; + tail = &head; + return self; +} + +- (void) free +{ + local list_bucket_t [] e, t = NIL; //FIXME t uninitialized + + for (e = head; e; e = t) { + t = e.next; + [e.obj free]; + obj_free (e); + } + [super free]; +} + +- (id) getItemAt: (integer) index +{ + local list_bucket_t [] e; + if (index < 0 || index >= count) + return NIL; + for (e = head; e && index; index--) + e = e.next; + return e.obj; +} + +-(id) head +{ + if (!head) + return NIL; + return head.obj; +} + +-(id) tail +{ + local list_bucket_t [] e = (list_bucket_t []) tail; + if (!e) + return NIL; + return e.obj; +} + +-(void) addItemAtHead: (id) item +{ + local list_bucket_t [] e = obj_malloc (@sizeof (list_bucket_t)); + e.obj = item; + e.next = head; + e.prev = &head; + if (head) + head.prev = &e.next; + head = e; +} + +-(void) addItemAtTail: (id) item +{ + local list_bucket_t [] e = obj_malloc (@sizeof (list_bucket_t)); + e.obj = item; + e.next = NIL; + e.prev = tail; + tail[0] = e; + tail = &e.next; +} + +- (void) removeItem: (id) item +{ + local list_bucket_t [] e; + + for (e = head; e; e = e.next) { + if (e.obj == item) { + e.prev[0] = e.next; + if (e.next) + e.next.prev = e.prev; + return; + } + } +} + +- (integer) count +{ + return count; +} + +-(void)makeObjectsPerformSelector:(SEL)selector +{ + local list_bucket_t [] e; + for (e = head; e; e = e.next) + [e.obj perform:selector]; +} + +-(void)makeObjectsPerformSelector:(SEL)selector withObject:(id)arg +{ + local list_bucket_t [] e; + for (e = head; e; e = e.next) + [e.obj perform:selector withObject:arg]; +} + +@end diff --git a/ruamoko/lib/Makefile.am b/ruamoko/lib/Makefile.am index e8a54dbf6..f35d41e90 100644 --- a/ruamoko/lib/Makefile.am +++ b/ruamoko/lib/Makefile.am @@ -30,7 +30,7 @@ libr_a_SOURCES=\ crudefile.r debug.r hash.r entities.r infokey.r math.r message.r \ nq_message.r physics.r plist.r qfile.r qw_message.r qw_physics.r qw_sys.r \ sound.r \ - string.r system.r Object.r Array.r Entity.r Point.r Size.r Rect.r + string.r system.r Object.r Array.r Entity.r List.r Point.r Size.r Rect.r libr_a_AR=$(PAK) -cf libgui_a_SOURCES=\