home *** CD-ROM | disk | FTP | other *** search
/ Power Programmierung 2 / Power-Programmierung CD 2 (Tewi)(1994).iso / c / library / dos / diverses / leda / incl / slist.h < prev    next >
Encoding:
C/C++ Source or Header  |  1991-11-15  |  4.8 KB  |  180 lines

  1. /*******************************************************************************
  2. +
  3. +  LEDA  2.1.1                                                 11-15-1991
  4. +
  5. +
  6. +  slist.h
  7. +
  8. +
  9. +  Copyright (c) 1991  by  Max-Planck-Institut fuer Informatik
  10. +  Im Stadtwald, 6600 Saarbruecken, FRG     
  11. +  All rights reserved.
  12. *******************************************************************************/
  13.  
  14.  
  15.  
  16.  
  17. #ifndef SLISTH
  18. #define SLISTH
  19.  
  20. /*------------------------------------------------------------------------------ 
  21.     SLIST   (simply linked lists)
  22.  
  23.     Stefan Naeher
  24.     FB10 Informatik 
  25.     Universitaet des Saarlandes 
  26.     6600 Saarbruecken
  27.  
  28.     September 1988
  29.     
  30. ------------------------------------------------------------------------------*/
  31.  
  32.  
  33. #include <LEDA/basic.h>
  34.  
  35.  
  36. class SLIST; 
  37. class slink;
  38.  
  39. typedef slink* slist_item;
  40.  
  41. //------------------------------------------------------------------------------
  42. // class slink 
  43. //------------------------------------------------------------------------------
  44.  
  45. class slink {
  46.  
  47.   friend class SLIST;
  48.  
  49.   slink* succ;
  50.   ent e;
  51.  
  52.   slink(ent a, slink* suc)
  53.   { 
  54.     e = a;
  55.     succ = suc;
  56.   }
  57.  
  58.   OPERATOR_NEW(2)
  59.   OPERATOR_DEL(2)
  60.  
  61. };
  62.  
  63.  
  64. //------------------------------------------------------------------------------
  65. // SLIST: base class for all simply linked Lists
  66. //------------------------------------------------------------------------------
  67.  
  68. class SLIST {
  69.  
  70.    slink* h;                     //head
  71.    slink* t;                     //tail
  72.    slink* iterator;              //iterator;
  73.    int    count;                 //length of List
  74.  
  75. virtual void copy_el(ent& x)  const { x=x; }
  76. virtual void clear_el(ent& x) const { x=x; }
  77.  
  78. public:
  79.  
  80.    int space()  const { return sizeof(SLIST) + count * sizeof(slink); }
  81.    int length() const { return count; }
  82.    int empty()  const { return (count==0);}
  83.  
  84.    slink* push(ent a);
  85.    slink* push(SLIST& l);
  86.    slink* append(ent a);
  87.    slink* append(SLIST& l);
  88.    slink* insert(ent a, slink* l);
  89.    slink* first() const { return h; }
  90.    slink* last()  const { return t; }
  91.  
  92.    slink* get_iterator()  const { return iterator; }
  93.  
  94.    void   set_iterator(slink* loc) const 
  95.    { const slist_item *const p = &iterator;  
  96.      (*(slink**)p) = loc;
  97.     }
  98.  
  99.    void init_iterator()  const { set_iterator(0); }
  100.  
  101.    slink* move_iterator() const  
  102.    { set_iterator( iterator ? iterator->succ : h);
  103.      return iterator;
  104.    } 
  105.  
  106.    slink* cyclic_succ(slink*) const;
  107.    slink* succ(slink* l)      const { return l->succ; }
  108.  
  109.    void pop();
  110.  
  111.    ent  head()   const { return h ? h->e : 0;}
  112.    ent  tail()   const { return t ? t->e : 0;}
  113.  
  114.    bool current_element(ent& x)  const
  115.    { if (!iterator) return false;
  116.      else { x = iterator->e;
  117.             return true; }
  118.     }
  119.  
  120.    bool next_element(ent& x) const 
  121.   { move_iterator(); 
  122.     return current_element(x); 
  123.    }
  124.  
  125.    ent  contents(slink* l)  const { return l->e; }
  126.    ent& operator[](slink* l)      { return l->e; }
  127.  
  128.    void clear();
  129.  
  130.    SLIST();    
  131.    SLIST(ent a);
  132.    SLIST& operator=(const SLIST&); 
  133.    SLIST  operator+(SLIST); 
  134.    SLIST(const SLIST&);
  135.    ~SLIST()     { clear(); }
  136. };
  137.  
  138.  
  139. //------------------------------------------------------------------------------
  140. // slist: generic SLISTs
  141. //-----------------------------------------------------------------------------
  142.  
  143. #define slist(type) name2(type,slist)
  144.  
  145. #define slistdeclare(type)\
  146. \
  147. struct slist(type) : SLIST {\
  148. slink* push(type a)            { return SLIST::push( Ent(a) ); }\
  149. slink* push(slist(type)& l)    { return SLIST::push((SLIST&)l); }\
  150. slink* append(type a)          { return SLIST::append( Ent(a) ); }\
  151. slink* append(slist(type)& l)  { return SLIST::append((SLIST&)l); }\
  152. slink* insert(type a, slink* l){ return SLIST::insert(Ent(a),l); }\
  153. bool current_element(type& x) const { ent y; bool b = SLIST::current_element(y);\
  154.                                       if (b) x = type(y); return b; }\
  155. bool next_element(type& x)    const { ent y; bool b = SLIST::next_element(y);\
  156.                                       if (b) x = type(y); return b; }\
  157. type pop()                    { return type(SLIST::pop() ); }\
  158. type head()             const { return type(SLIST::head() ); }\
  159. type tail()             const { return type(SLIST::tail() ); }\
  160. type contents(slink* l) const { return type(SLIST::contents(l)); }\
  161. type inf(slink* l)      const { return type(SLIST::contents(l)); }\
  162. \
  163.    slist(type)() { }\
  164.    slist(type)(const slist(type)& a) : SLIST((SLIST&)a) { }\
  165.    slist(type)(type a) : SLIST(Ent(a)) { }\
  166.    ~slist(type)() {}\
  167. \
  168.    slist(type)& operator=(const slist(type)& a)\
  169.            { return (slist(type)&)SLIST::operator=((SLIST&)a); }\
  170.    slist(type) operator+(slist(type) a)\
  171.            { return (slist(type)&)(SLIST(*this) + SLIST(a)); }\
  172.    slink* operator+=(type x)  { return append(x); }\
  173.    slink* operator&=(type x)  { return push(x); }\
  174.    type& operator[](slink* l) { return *(type*)&SLIST::operator[](l); }\
  175. };
  176.  
  177.  
  178. #endif
  179.