#ifndef PARGEN_LIST_H #define PARGEN_LIST_H class emptyNode { public: emptyNode *next; emptyNode () { next = 0; } emptyNode (emptyNode *p) { next = p; } }; class emptyList { protected: emptyNode *head, *tail; int len; public: int empty() { return(head == 0); } int length() { return len; } void insert(emptyNode *); void append(emptyNode *); emptyNode *get(); emptyList() { head = tail = 0; len = 0; } ~emptyList(); friend class emptyListIter; }; template class node : public emptyNode { public: T item; node (const T &i) : item(i) { } }; template class list : public emptyList { public: void insert(const T &i) { emptyList::insert(new node(i)); } void append(const T &i) { emptyList::append(new node(i)); } T get() { node *p = (node *) emptyList::get(); T i = p->item; delete p; return i; } void remove(const T &target) { node *p = (node *) head; node *previous = 0; while (p) { if (target == p->item) { if (tail == p) tail = previous; if (previous == 0) head = p->next; else previous->next = p->next; delete p; --len; return; } previous = p; p = (node *) p->next; } } inline T *find(const T &target); inline int contains(list &a); inline void setUnion(list &a); }; class emptyListIter { private: emptyNode *current; public: emptyListIter(emptyList *l) { current = l->head; } void init(emptyList *l) { current = l->head; } emptyNode *operator() () { if (!current) return 0; emptyNode *p = current; current = current->next; return p; } }; template class listIter : emptyListIter { public: listIter(list *s) : emptyListIter(s) { } void init(list *s) { emptyListIter::init(s); } T* operator() () { node *l = (node *) emptyListIter::operator() (); return(l ? &l->item : 0); } }; /* inlines */ template inline Type *list::find(const Type &target) { listIter forEach(this); while (Type *p = forEach()) { if (*p == target) return(p); } return 0; } template inline int list::contains(list &a) { listIter forEach(&a); T *p; while (p = forEach()) if (!find(*p)) return FALSE; return TRUE; } template inline void list::setUnion(list &a) { listIter forEach(&a); T *p; while (p = forEach()) if (!find(*p)) append(*p); } #endif