/* clistf.h v1.0 (C) 2000 adolfo@di-mare.com */ #ifndef CLISTF_H #define CLISTF_H #include "ptrbit.h" #include #include class clistf { // circular list public: class list_link; class itr : public ptrbit { protected: // itr(ptrbit &o) : ptrbit ( o ) {} public: friend class clistf; friend class list_link; itr() {} itr(itr &o) : ptrbit ( o ) {} itr(list_link *o) : ptrbit ( (void*) o ) {} itr& operator++(); // ++i itr operator++(int) { // i++ itr tmp = *this; this->operator++(); // ++*this; return tmp; } itr prev(); protected: public: // debug // operator ptrbit() { return (ptrbit)ai(); } // list_link& operator->() { return *(list_link*)(vp()); } list_link * ai() { return (list_link*) (ptrbit::ai()); } list_link * um() { return (list_link*) (ptrbit::vp()); } void operator=(list_link *x) { set( (void*) x); } void operator=(itr &x) { set ( x.ai() ); } // define these in derived itr classes // list_link& operator*() { return * (list_link*)(ai()); } // list_link& operator->() { return this->operator*(); } }; // itr class list_link { public: // debug itr next; public: list_link() {} void* address() { return &next; } // debug friend class clistf; friend class itr; }; unsigned count(); int empty() { return _last == end(); } int valid(itr); void link_after( itr where, list_link &what); itr unlink_after(itr where); void push_front(list_link &what) { link_after(0, what); } void push_back( list_link &what) { link_after(_last, what); } itr last() { return _last; } itr begin(); itr end() { return 0; } itr nth(unsigned n); unsigned pos(itr i); public: clistf() : _last(0) {}; private: clistf(const clistf&) : _last(0) {} // forbid copy void operator=(const clistf&) {} private: // member fields itr _last; // points to last, last points to first }; // clistf inline clistf::itr clistf::begin() { return (_last.ai() == 0 ? 0 : _last.um()->next.um()); } inline clistf::itr& clistf::itr::operator++() { // ++i set (ai()->next.ai()); if (this->marked()) { set(0); } return *this; } #include // offsetof() template class clistfT : public clistf { private: friend void print_list_ai(clistfT&); // debug struct node { T e; // element list_link lk; // link field for the list node(T& n) : e(n), lk() {} }; // node public: class iterator : public itr { friend class clistfT; public: iterator() : itr ( ) {} iterator(itr &o) : itr (o) {} iterator(list_link *o) : itr (o) {} void operator=(itr &x) { itr::operator=(x); } private: public: // debug node* np() { // Node Pointer return ((node*)((char*)vp() - offsetof(node, lk))); } public: T& operator*() { return np()->e; } // T& operator->() { return this->operator*(); } }; // iterator public: ~clistfT() { while (!empty()) { iterator i = unlink_after(0); delete i.um(); } } static iterator create(T& n) { node* p = new node(n); return &p->lk; } static void destroy(iterator &p) { delete p.np(); } }; // clistfT #endif /* CLISTF_H */ /* EOF: clistf.h */