home *** CD-ROM | disk | FTP | other *** search
/ NeXTSTEP 3.2 (Developer) / NS_dev_3.2.iso / NextDeveloper / Headers / g++ / SLList.h < prev    next >
C/C++ Source or Header  |  1993-06-29  |  4KB  |  116 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, 675 Mass Ave, Cambridge, MA 02139, USA.
  17. */
  18.  
  19. #ifndef _SLList_h
  20. #ifdef __GNUG__
  21. //#pragma interface
  22. #endif
  23. #define _LList_h 1
  24.  
  25. #include <Pix.h>
  26.  
  27. struct BaseSLNode
  28. {
  29.    BaseSLNode *tl;
  30.    void *item() {return (void*)(this+1);} // Return ((SLNode<T>*)this)->hd
  31. };
  32.  
  33. template<class T>
  34. class SLNode : public BaseSLNode
  35. {
  36.   public:
  37.     T                    hd; // Data part of node
  38.                          SLNode() { }
  39.                          SLNode(const T& h, SLNode* t = 0)
  40.                  : hd(h), tl(t) { }
  41.                          ~SLNode() { }
  42. };
  43.  
  44. extern int __SLListLength(BaseSLNode *ptr);
  45.  
  46. class BaseSLList {
  47.   protected:
  48.     BaseSLNode *last;
  49.     virtual void delete_node(BaseSLNode*node) = 0;
  50.     virtual BaseSLNode* copy_node(void* datum) = 0;
  51.     virtual void copy_item(void *dst, void *src) = 0;
  52.     virtual ~BaseSLList() { }
  53.     BaseSLList() { last = 0; }
  54.     void copy(const BaseSLList&);
  55.     BaseSLList& operator = (const BaseSLList& a);
  56.     Pix ins_after(Pix p, void *datum);
  57.     Pix prepend(void *datum);
  58.     Pix append(void *datum);
  59.     int remove_front(void *dst, int signal_error = 0);
  60.     void join(BaseSLList&);
  61.   public:
  62.     int length();
  63.     void clear();
  64.     Pix                   prepend(BaseSLNode*);
  65.     Pix                   append(BaseSLNode*);
  66.     int                   OK();
  67.     void                  error(const char* msg);
  68.     void                  del_after(Pix p);
  69.     int                   owns(Pix p);
  70.     void                  del_front();
  71. };
  72.  
  73. template <class T>
  74. class SLList : public BaseSLList
  75. {
  76.   private:
  77.     virtual void delete_node(BaseSLNode *node) { delete (SLNode<T>*)node; }
  78.     virtual BaseSLNode* copy_node(void *datum)
  79.     { return new SLNode<T>(*(T*)datum); }
  80.     virtual void copy_item(void *dst, void *src) { *(T*)dst = *(T*)src; }
  81.  
  82. public:
  83.     SLList() : BaseSLList() { }
  84.     SLList(const SLList<T>& a) : BaseSLList() { copy(a); }
  85.     SLList<T>&            operator = (const SLList<T>& a)
  86.     { BaseSLList::operator=((const BaseSLList&) a); return *this; }
  87.     virtual ~SLList() { clear(); }
  88.  
  89.     int                   empty() { return last == 0; }
  90.  
  91.     Pix prepend(T& item) {return BaseSLList::prepend(&item);}
  92.     Pix append(T& item) {return BaseSLList::append(&item);}
  93.     Pix prepend(SLNode<T>* node) {return BaseSLList::prepend(node);}
  94.     Pix append(SLNode<T>* node) {return BaseSLList::append(node);}
  95.  
  96.     T& operator () (Pix p) {
  97.     if (p == 0) error("null Pix");
  98.     return ((SLNode<T>*)(p))->hd; }
  99.     inline Pix first() { return (last == 0)? 0 : Pix(last->tl); }
  100.     void next(Pix& p)
  101.     { p = (p == 0 || p == last)? 0 : Pix(((SLNode<T>*)(p))->tl); }
  102.     Pix ins_after(Pix p, T& item) { return BaseSLList::ins_after(p, &item); }
  103.     void join(SLList<T>& a) { BaseSLList::join(a); }
  104.     
  105.     T& front() {
  106.     if (last == 0) error("front: empty list");
  107.     return ((SLNode<T>*)last->tl)->hd; }
  108.     T& rear() {
  109.     if (last == 0) error("rear: empty list");
  110.     return ((SLNode<T>*)last)->hd; }
  111.     int remove_front(T& x) { return BaseSLList::remove_front(&x); }
  112.     T remove_front() { T dst; remove_front(&dst, 1); return dst; }
  113. };
  114.  
  115. #endif
  116.