home *** CD-ROM | disk | FTP | other *** search
/ NeXTSTEP 3.2 (Developer) / NS_dev_3.2.iso / NextDeveloper / Headers / g++ / DLList.h < prev    next >
C/C++ Source or Header  |  1993-06-29  |  4KB  |  125 lines

  1. // This may look like C code, but it is really -*- C++ -*-
  2. /* 
  3. Copyright (C) 1988 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.  
  20. #ifndef _DLList_h
  21. #ifdef __GNUG__
  22. //#pragma interface
  23. #endif
  24. #define _DLList_h 1
  25.  
  26. #include <Pix.h>
  27.  
  28. struct BaseDLNode {
  29.     BaseDLNode *bk;
  30.     BaseDLNode *fd;
  31.     void *item() {return (void*)(this+1);} //Return ((DLNode<T>*)this)->hd
  32. };
  33.  
  34. template<class T>
  35. class DLNode : public BaseDLNode
  36. {
  37.   public:
  38.     T hd;
  39.     DLNode() { }
  40.     DLNode(const T& h, DLNode* p = 0, DLNode* n = 0)
  41.         :hd(h), bk(p), fd(n) {}
  42.     ~DLNode() { }
  43. };
  44.  
  45. class BaseDLList {
  46.   protected:
  47.     BaseDLNode *h;
  48.  
  49.     BaseDLList() { h = 0; }
  50.     void copy(const BaseDLList&);
  51.     BaseDLList& operator= (const BaseDLList& a);
  52.     virtual void delete_node(BaseDLNode*node) = 0;
  53.     virtual BaseDLNode* copy_node(void* datum) = 0;
  54.     virtual void copy_item(void *dst, void *src) = 0;
  55.     virtual ~BaseDLList() { }
  56.  
  57.     Pix                   prepend(void*);
  58.     Pix                   append(void*);
  59.     Pix ins_after(Pix p, void *datum);
  60.     Pix ins_before(Pix p, void *datum);
  61.     void remove_front(void *dst);
  62.     void remove_rear(void *dst);
  63.     void join(BaseDLList&);
  64.  
  65.   public:
  66.     int                   empty() { return h == 0; }
  67.     int                   length();
  68.     void                  clear();
  69.     void                  error(const char* msg);
  70.     int                   owns(Pix p);
  71.     int                   OK();
  72.     void                  del(Pix& p, int dir = 1);
  73.     void                  del_after(Pix& p);
  74.     void                  del_front();
  75.     void                  del_rear();
  76. };
  77.  
  78. template <class T>
  79. class DLList : public BaseDLList {
  80.     //friend class          <T>DLListTrav;
  81.  
  82.     virtual void delete_node(BaseDLNode *node) { delete (DLNode<T>*)node; }
  83.     virtual BaseDLNode* copy_node(void *datum)
  84.     { return new DLNode<T>(*(T*)datum); }
  85.     virtual void copy_item(void *dst, void *src) { *(T*)dst = *(T*)src; }
  86.  
  87.   public:
  88.     DLList() : BaseDLList() { }
  89.     DLList(const DLList<T>& a) : BaseDLList() { copy(a); }
  90.  
  91.     DLList<T>&            operator = (const DLList<T>& a)
  92.     { BaseDLList::operator=((const BaseDLList&) a); return *this; }
  93.     virtual ~DLList() { clear(); }
  94.  
  95.     Pix prepend(T& item) {return BaseDLList::prepend(&item);}
  96.     Pix append(T& item) {return BaseDLList::append(&item);}
  97.  
  98.     void join(DLList<T>& a) { BaseDLList::join(a); }
  99.  
  100.     T& front() {
  101.     if (h == 0) error("front: empty list");
  102.     return ((DLNode<T>*)h)->hd; }
  103.     T& rear() {
  104.     if (h == 0) error("rear: empty list");
  105.     return ((DLNode<T>*)h->bk)->hd;
  106.     }
  107.     T remove_front() { T dst; remove_front(&dst); return dst; }
  108.     T remove_rear() { T dst; remove_rear(&dst); return dst; }
  109.  
  110.     T&                  operator () (Pix p) {
  111.     if (p == 0) error("null Pix");
  112.     return ((DLNode<T>*)p)->hd;
  113.     }
  114.     Pix                   first() {  return Pix(h); }
  115.     Pix                   last() { return (h == 0)? 0 : Pix(h->bk); }
  116.     void                  next(Pix& p)
  117.     { p = (p == 0 || p == h->bk)? 0 : Pix(((DLNode<T>*)p)->fd); }
  118.     void                  prev(Pix& p)
  119.     { p = (p == 0 || p == h)? 0 : Pix(((DLNode<T>*)p)->bk); }
  120.     Pix ins_after(Pix p, T& item) {return BaseDLList::ins_after(p, &item); }
  121.     Pix ins_before(Pix p, T& item) {return BaseDLList::ins_before(p, &item);}
  122. };
  123.  
  124. #endif
  125.