home *** CD-ROM | disk | FTP | other *** search
- /*******************************************************************************
- +
- + LEDA 2.1.1 11-15-1991
- +
- +
- + slist.h
- +
- +
- + Copyright (c) 1991 by Max-Planck-Institut fuer Informatik
- + Im Stadtwald, 6600 Saarbruecken, FRG
- + All rights reserved.
- +
- *******************************************************************************/
-
-
-
-
- #ifndef SLISTH
- #define SLISTH
-
- /*------------------------------------------------------------------------------
- SLIST (simply linked lists)
-
- Stefan Naeher
- FB10 Informatik
- Universitaet des Saarlandes
- 6600 Saarbruecken
-
- September 1988
-
- ------------------------------------------------------------------------------*/
-
-
- #include <LEDA/basic.h>
-
-
- class SLIST;
- class slink;
-
- typedef slink* slist_item;
-
- //------------------------------------------------------------------------------
- // class slink
- //------------------------------------------------------------------------------
-
- class slink {
-
- friend class SLIST;
-
- slink* succ;
- ent e;
-
- slink(ent a, slink* suc)
- {
- e = a;
- succ = suc;
- }
-
- OPERATOR_NEW(2)
- OPERATOR_DEL(2)
-
- };
-
-
- //------------------------------------------------------------------------------
- // SLIST: base class for all simply linked Lists
- //------------------------------------------------------------------------------
-
- class SLIST {
-
- slink* h; //head
- slink* t; //tail
- slink* iterator; //iterator;
- int count; //length of List
-
- virtual void copy_el(ent& x) const { x=x; }
- virtual void clear_el(ent& x) const { x=x; }
-
- public:
-
- int space() const { return sizeof(SLIST) + count * sizeof(slink); }
- int length() const { return count; }
- int empty() const { return (count==0);}
-
- slink* push(ent a);
- slink* push(SLIST& l);
- slink* append(ent a);
- slink* append(SLIST& l);
- slink* insert(ent a, slink* l);
- slink* first() const { return h; }
- slink* last() const { return t; }
-
- slink* get_iterator() const { return iterator; }
-
- void set_iterator(slink* loc) const
- { const slist_item *const p = &iterator;
- (*(slink**)p) = loc;
- }
-
- void init_iterator() const { set_iterator(0); }
-
- slink* move_iterator() const
- { set_iterator( iterator ? iterator->succ : h);
- return iterator;
- }
-
- slink* cyclic_succ(slink*) const;
- slink* succ(slink* l) const { return l->succ; }
-
- void pop();
-
- ent head() const { return h ? h->e : 0;}
- ent tail() const { return t ? t->e : 0;}
-
- bool current_element(ent& x) const
- { if (!iterator) return false;
- else { x = iterator->e;
- return true; }
- }
-
- bool next_element(ent& x) const
- { move_iterator();
- return current_element(x);
- }
-
- ent contents(slink* l) const { return l->e; }
- ent& operator[](slink* l) { return l->e; }
-
- void clear();
-
- SLIST();
- SLIST(ent a);
- SLIST& operator=(const SLIST&);
- SLIST operator+(SLIST);
- SLIST(const SLIST&);
- ~SLIST() { clear(); }
- };
-
-
- //------------------------------------------------------------------------------
- // slist: generic SLISTs
- //-----------------------------------------------------------------------------
-
- #define slist(type) name2(type,slist)
-
- #define slistdeclare(type)\
- \
- struct slist(type) : SLIST {\
- slink* push(type a) { return SLIST::push( Ent(a) ); }\
- slink* push(slist(type)& l) { return SLIST::push((SLIST&)l); }\
- slink* append(type a) { return SLIST::append( Ent(a) ); }\
- slink* append(slist(type)& l) { return SLIST::append((SLIST&)l); }\
- slink* insert(type a, slink* l){ return SLIST::insert(Ent(a),l); }\
- bool current_element(type& x) const { ent y; bool b = SLIST::current_element(y);\
- if (b) x = type(y); return b; }\
- bool next_element(type& x) const { ent y; bool b = SLIST::next_element(y);\
- if (b) x = type(y); return b; }\
- type pop() { return type(SLIST::pop() ); }\
- type head() const { return type(SLIST::head() ); }\
- type tail() const { return type(SLIST::tail() ); }\
- type contents(slink* l) const { return type(SLIST::contents(l)); }\
- type inf(slink* l) const { return type(SLIST::contents(l)); }\
- \
- slist(type)() { }\
- slist(type)(const slist(type)& a) : SLIST((SLIST&)a) { }\
- slist(type)(type a) : SLIST(Ent(a)) { }\
- ~slist(type)() {}\
- \
- slist(type)& operator=(const slist(type)& a)\
- { return (slist(type)&)SLIST::operator=((SLIST&)a); }\
- slist(type) operator+(slist(type) a)\
- { return (slist(type)&)(SLIST(*this) + SLIST(a)); }\
- slink* operator+=(type x) { return append(x); }\
- slink* operator&=(type x) { return push(x); }\
- type& operator[](slink* l) { return *(type*)&SLIST::operator[](l); }\
- };
-
-
- #endif
-