home *** CD-ROM | disk | FTP | other *** search
/ Geek Gadgets 1 / ADE-1.bin / ade-dist / libg++-2.7.1-bin.lha / lib / g++-include / SLList.h < prev    next >
C/C++ Source or Header  |  1996-10-12  |  4KB  |  130 lines

  1. // This may look like C code, but it is really -*- C++ -*-
  2. /* 
  3. Copyright (C) 1988, 1992 Free Software Foundation
  4.     written by Doug Lea (dl@rocky.oswego.edu)
  5.  
  6. This file is part of the GNU C++ Library.  This library is free
  7. software; you can redistribute it and/or modify it under the terms of
  8. the GNU Library General Public License as published by the Free
  9. Software Foundation; either version 2 of the License, or (at your
  10. option) any later version.  This library is distributed in the hope
  11. that it will be useful, but WITHOUT ANY WARRANTY; without even the
  12. implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
  13. PURPOSE.  See the GNU Library General Public License for more details.
  14. You should have received a copy of the GNU Library General Public
  15. License along with this library; if not, write to the Free Software
  16. Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  17. */
  18.  
  19. #ifndef _SLList_h
  20. #ifdef __GNUG__
  21. //#pragma interface
  22. #endif
  23. #define _SLList_h 1
  24.  
  25. #undef OK
  26.  
  27. #include <Pix.h>
  28.  
  29. struct BaseSLNode
  30. {
  31.    union {
  32.      struct BaseSLNode *tl;
  33.      double dummy;  /* To force correct alignment */
  34.    };
  35.    void *item() {return (void*)(this+1);} // Return ((SLNode<T>*)this)->hd
  36. };
  37.  
  38. template<class T>
  39. class SLNode : public BaseSLNode
  40. {
  41.   public:
  42.     T                    hd; // Data part of node
  43.                          SLNode() { }
  44.                          SLNode(const T& h, SLNode* t = 0)
  45.                  : hd(h) { tl = t; }
  46.                          ~SLNode() { }
  47. };
  48.  
  49. extern int __SLListLength(BaseSLNode *ptr);
  50.  
  51. class BaseSLList {
  52.   protected:
  53.     BaseSLNode *last;
  54.     virtual void delete_node(BaseSLNode*node) = 0;
  55.     virtual BaseSLNode* copy_node(const void* datum) = 0;
  56.     virtual void copy_item(void *dst, void *src) = 0;
  57.     virtual ~BaseSLList() { }
  58.     BaseSLList() { last = 0; }
  59.     void copy(const BaseSLList&);
  60.     BaseSLList& operator = (const BaseSLList& a);
  61.     Pix ins_after(Pix p, const void *datum);
  62.     Pix prepend(const void *datum);
  63.     Pix append(const void *datum);
  64.     int remove_front(void *dst, int signal_error = 0);
  65.     void join(BaseSLList&);
  66.   public:
  67.     int length() const;
  68.     int empty() const { return last == 0; }
  69.     void clear();
  70.     Pix                   prepend(BaseSLNode*);
  71.     Pix                   append(BaseSLNode*);
  72.     int                   OK() const;
  73.     void                  error(const char* msg) const;
  74.     void                  del_after(Pix p);
  75.     int                   owns(Pix p) const;
  76.     void                  del_front();
  77. };
  78.  
  79. template <class T>
  80. class SLList : public BaseSLList
  81. {
  82.   private:
  83.     virtual void delete_node(BaseSLNode *node) { delete (SLNode<T>*)node; }
  84.     virtual BaseSLNode* copy_node(const void *datum)
  85.     { return new SLNode<T>(*(const T*)datum); }
  86.     virtual void copy_item(void *dst, void *src) { *(T*)dst = *(T*)src; }
  87.  
  88. public:
  89.     SLList() : BaseSLList() { }
  90.     SLList(const SLList<T>& a) : BaseSLList() { copy(a); }
  91.     SLList<T>&            operator = (const SLList<T>& a)
  92.     { BaseSLList::operator=((const BaseSLList&) a); return *this; }
  93.     virtual ~SLList() { clear(); }
  94.  
  95.     Pix prepend(const T& item) {return BaseSLList::prepend(&item);}
  96.     Pix append(const T& item) {return BaseSLList::append(&item);}
  97.     Pix prepend(SLNode<T>* node) {return BaseSLList::prepend(node);}
  98.     Pix append(SLNode<T>* node) {return BaseSLList::append(node);}
  99.  
  100.     T& operator () (Pix p) {
  101.     if (p == 0) error("null Pix");
  102.     return ((SLNode<T>*)(p))->hd; }
  103.     const T& operator () (Pix p) const {
  104.     if (p == 0) error("null Pix");
  105.     return ((SLNode<T>*)(p))->hd; }
  106.     inline Pix first() const { return (last == 0) ? 0 : Pix(last->tl); }
  107.     void next(Pix& p) const
  108.     { p = (p == 0 || p == last) ? 0 : Pix(((SLNode<T>*)(p))->tl); }
  109.     Pix ins_after(Pix p, const T& item)
  110.       { return BaseSLList::ins_after(p, &item); }
  111.     void join(SLList<T>& a) { BaseSLList::join(a); }
  112.     
  113.     T& front() {
  114.     if (last == 0) error("front: empty list");
  115.     return ((SLNode<T>*)last->tl)->hd; }
  116.     T& rear() {
  117.     if (last == 0) error("rear: empty list");
  118.     return ((SLNode<T>*)last)->hd; }
  119.     const T& front() const {
  120.     if (last == 0) error("front: empty list");
  121.     return ((SLNode<T>*)last->tl)->hd; }
  122.     const T& rear() const {
  123.     if (last == 0) error("rear: empty list");
  124.     return ((SLNode<T>*)last)->hd; }
  125.     int remove_front(T& x) { return BaseSLList::remove_front(&x); }
  126.     T remove_front() { T dst; BaseSLList::remove_front(&dst, 1); return dst; }
  127. };
  128.  
  129. #endif
  130.