home *** CD-ROM | disk | FTP | other *** search
- /*******************************************************************************
- +
- + LEDA 2.1.1 11-15-1991
- +
- +
- + list.h
- +
- +
- + Copyright (c) 1991 by Max-Planck-Institut fuer Informatik
- + Im Stadtwald, 6600 Saarbruecken, FRG
- + All rights reserved.
- +
- *******************************************************************************/
-
-
-
- #ifndef LISTH
- #define LISTH
-
- /*------------------------------------------------------------------------------
- list (doubled linked lists)
-
- Stefan Naeher
- FB10 Informatik
- Universitaet des Saarlandes
- 6600 Saarbruecken
-
- September 1988
-
- ------------------------------------------------------------------------------*/
-
- #include <LEDA/basic.h>
-
- //------------------------------------------------------------------------------
- // some declarations
- //------------------------------------------------------------------------------
-
- class dlist;
- class dlink;
-
- typedef dlink* list_item;
-
-
- //------------------------------------------------------------------------------
- // class dlink (list items)
- //------------------------------------------------------------------------------
-
- class dlink {
-
- friend class dlist;
-
- dlink* succ;
- dlink* pred;
- ent e;
-
- dlink(ent a, dlink* pre, dlink* suc)
- {
- e = a;
- succ = suc;
- pred = pre;
- }
-
- OPERATOR_NEW(3)
- OPERATOR_DEL(3)
-
- //space: 3 words = 12 bytes
- };
-
-
- //------------------------------------------------------------------------------
- // dlist: base class for all doubly linked lists
- //------------------------------------------------------------------------------
-
- class dlist {
-
- dlink* h; //head
- dlink* t; //tail
- dlink* iterator; //iterator
- int count; //length of list
-
- //space: 4 words + virtual = 5 words = 20 bytes
-
- // virtual int cmp(ent& x, ent& y) { return x-y; }
-
- virtual void print_el(ent& x,ostream& out) const { out << int(x); }
- virtual void read_el(ent& x,istream& in) const { in >> *(int*)&x; }
- virtual void clear_el(ent& x) const { x=0; }
- virtual void copy_el(ent& x) const { x=x; }
- virtual void init_el(ent& x) const { x=0; }
-
- // void quick_sort(list_item* A,int l, int r);
-
- void quick_sort(list_item* A,int l, int r, CMP_ENT cmp_fct);
-
-
- public:
-
- // access operations
-
- int length() const { return count; }
- int size() const { return count; }
- bool empty() const { return (count==0) ? true : false;}
-
- dlink* first() const { return h; }
- dlink* first_item() const { return h; }
- dlink* last() const { return t; }
- dlink* last_item() const { return t; }
- dlink* next_item(dlink* p) const { return p->succ; }
- dlink* succ(dlink* l) const { return l->succ; }
- dlink* pred(dlink* l) const { return l->pred; }
- dlink* cyclic_succ(dlink*) const;
- dlink* cyclic_pred(dlink*) const;
- dlink* succ(dlink* l, int i) const;
- dlink* pred(dlink* l, int i) const;
- dlink* get_item(int = 0) const;
-
- dlink* max(CMP_ENT) const;
- dlink* min(CMP_ENT) const;
- dlink* search(ent) const;
-
- int rank(ent) const;
-
- ent contents(dlink* l) const { return l->e; }
- ent head() const { return h ? h->e : 0;}
- ent tail() const { return t ? t->e : 0;}
-
-
- // update operations
-
- dlink* insert(ent a, dlink* l, int dir=0);
- dlink* push(ent a);
- dlink* append(ent a);
-
- void assign(dlink* l, ent a) { copy_el(a); l->e = a; }
- void conc(dlist&);
- void split(list_item,dlist&,dlist&);
-
- void del(dlink* loc);
- void pop();
- void Pop();
-
- void apply(ENT_TO_VOID);
- void sort(CMP_ENT);
- void bucket_sort(int i, int j, ENT_TO_INT f);
- void permute();
- void clear();
-
- // iteration
-
- void set_iterator(dlink* loc) const
- { const list_item *const p = &iterator; // hack to stay const
- (*(dlink**)p) = loc;
- }
-
- void init_iterator() const { set_iterator(nil); }
- void reset() const { set_iterator(nil); }
- dlink* get_iterator() const { return iterator; }
-
- dlink* move_iterator(int=0) const;
- bool current_element(ent&) const;
- bool next_element(ent&) const;
- bool prev_element(ent&) const;
-
-
- // operators
-
- ent& entry(dlink* l) { return l->e; }
- ent inf(dlink* l) const { return l->e; }
- ent& operator[](dlink* l) { return l->e; }
- ent operator[](dlink* l) const { return l->e; }
-
- dlist& operator=(const dlist&);
- dlist operator+(const dlist&);
-
-
- void print(ostream&,string, char) const;
- void print(ostream& out,char space=' ') const { print(out,"",space); }
- void print(string s, char space=' ') const { print(cout,s,space); }
- void print(char space=' ') const { print(cout,"",space); }
-
-
- void read(istream&,string, char);
- void read(istream& in,char delim=EOF) { read(in,"",delim); }
- void read(string s, char delim='\n') { read(cin,s,delim); }
- void read(char delim='\n') { read(cin,"",delim);}
-
-
- // constructors & destructors
-
- dlist();
- dlist(ent a);
- dlist(const dlist&);
-
- ~dlist() { clear(); }
-
- int space() const { return sizeof(dlist) + count * sizeof(dlink); }
- };
-
-
- //------------------------------------------------------------------------------
- // list: generic dlists
- //-----------------------------------------------------------------------------
-
- #define list(type) name2(type,list)
-
- #define listdeclare(type)\
- \
- typedef int (*COMPARE(type,list)) (type&, type&);\
- typedef void (*APPLY(type,list)) (type&);\
- typedef int (*ORD(type,list)) (type&);\
- \
- struct list(type) : public dlist {\
- \
- void print_el(ent& x,ostream& out) const { Print(*(type*)&x,out); }\
- void read_el(ent& x,istream& in) const { type y=0; Read(y,in); x = Copy(y); }\
- void clear_el(ent& x) const { Clear(*(type*)&x); }\
- void copy_el(ent& x) const { Copy(*(type*)&x); }\
- void init_el(ent& x) const { type y=0; x = Copy(y); }\
- \
- void conc(list(type)& l) { dlist::conc((dlist&)l); }\
- void split(list_item p, list(type)& l1, list(type)& l2)\
- { dlist::split(p,(dlist&)l1,(dlist&)l2);}\
- list_item search(type a) const{ return dlist::search(Ent(a)); }\
- int rank(type a) const{ return dlist::rank(Ent(a)); }\
- \
- list_item push(type a) { return dlist::push(Copy(a)); }\
- list_item append(type a) { return dlist::append(Copy(a)); }\
- list_item insert(type a, list_item l, int dir=0)\
- { return dlist::insert(Copy(a),l,dir); }\
- \
- type contents(list_item l) const { ent x = dlist::contents(l); return *(type*)&x; }\
- type inf(list_item l) const{ return contents(l); }\
- \
- type head() const{ ent x = dlist::head(); return *(type*)&x; }\
- type tail() const{ ent x = dlist::tail(); return *(type*)&x; }\
- \
- type pop() { type x=head(); dlist::pop(); return x; }\
- type Pop() { type x=tail(); dlist::Pop(); return x; }\
- \
- type del(list_item a) { type x=contents(a); dlist::del(a); return x; }\
- type del_item(list_item a) { type x=contents(a); dlist::del(a); return x; }\
- \
- void assign(list_item p,type a) { dlist::assign(p,Ent(a)); }\
- \
- list_item max(COMPARE(type,list) f=0) const { return dlist::max(CMP_ENT(f)); }\
- list_item min(COMPARE(type,list) f=0) const { return dlist::max(CMP_ENT(f)); }\
- void sort(COMPARE(type,list) f=0) { dlist::sort(CMP_ENT(f)); }\
- void apply(APPLY(type,list) f) { dlist::apply(ENT_TO_VOID(f)); }\
- void bucket_sort(int i, int j, ORD(type,list) f)\
- { dlist::bucket_sort(i,j,ENT_TO_INT(f)); }\
- \
- bool current_element(type& x) const { ent y; bool b = dlist::current_element(y);\
- if (b) x = *(type*)&y; return b; }\
- bool next_element(type& x) const { ent y; bool b = dlist::next_element(y);\
- if (b) x = *(type*)&y; return b; }\
- bool prev_element(type& x) const { ent y; bool b = dlist::prev_element(y);\
- if (b) x = *(type*)&y; return b; }\
- \
- list(type)() {}\
- list(type)(const list(type)& a) : dlist((dlist&)a) {}\
- list(type)(type a) : dlist(Ent(a)) {}\
- \
- ~list(type)() { clear(); }\
- \
- list(type)& operator=(const list(type)& a)\
- { return *(list(type)*)&dlist::operator=(a);}\
- \
- list(type) operator+(const list(type)& a)\
- { dlist L = *(dlist*)this + *(dlist*)&a;\
- /* dlist L = dlist::operator+(a); */ \
- return *(list(type)*)&L; }\
- list_item operator+=(type x)\
- { return append(x); }\
- \
- list_item operator[](int i) { return get_item(i); }\
- \
- type& operator[](list_item p)\
- { return *(type*)&dlist::entry(p); }\
- type operator[](list_item p) const\
- { return type(dlist::inf(p)); }\
- };
-
-
-
- //------------------------------------------------------------------------------
- // Iteration Macros
- //------------------------------------------------------------------------------
-
- #define forall_list_items(a,b) for( a=(b).first(); a ; a=(b).succ(a) )
- #define Forall_list_items(a,b) for( a=(b).last(); a ; a=(b).pred(a) )
-
- /* forall(a,b) defined in basic.h */
-
- #define Forall(a,b) for( (b).init_iterator(); (b).prev_element(a); )
-
-
- #endif
-