home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c017 / 7.ddi / GPPLIB.ZIP / DLLIST.CC < prev    next >
Encoding:
C/C++ Source or Header  |  1988-08-17  |  5.0 KB  |  273 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 GNU CC.
  7.  
  8. GNU CC is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY.  No author or distributor
  10. accepts responsibility to anyone for the consequences of using it
  11. or for whether it serves any particular purpose or works at all,
  12. unless he says so in writing.  Refer to the GNU CC General Public
  13. License for full details.
  14.  
  15. Everyone is granted permission to copy, modify and redistribute
  16. GNU CC, but only under the conditions described in the
  17. GNU CC General Public License.   A copy of this license is
  18. supposed to have been given to you along with GNU CC so you
  19. can know your rights and responsibilities.  It should be in a
  20. file named COPYING.  Among other things, the copyright notice
  21. and this notice must be preserved on all copies.  
  22. */
  23.  
  24. #include <stream.h>
  25. #include "<T>DLList.h"
  26.  
  27. // error handling
  28.  
  29.  
  30. void default_<T>DLList_error_handler(char* msg)
  31. {
  32.   cerr << "Fatal <T>DLList error. " << msg << "\n";
  33.   exit(1);
  34. }
  35.  
  36. one_arg_error_handler_t <T>DLList_error_handler = default_<T>DLList_error_handler;
  37.  
  38. one_arg_error_handler_t set_<T>DLList_error_handler(one_arg_error_handler_t f)
  39. {
  40.   one_arg_error_handler_t old = <T>DLList_error_handler;
  41.   <T>DLList_error_handler = f;
  42.   return old;
  43. }
  44.  
  45. void <T>DLList::error(const char* msg)
  46. {
  47.   (*<T>DLList_error_handler)(msg);
  48. }
  49.  
  50. <T>DLList::<T>DLList(<T>DLList& a)
  51. {
  52.   if (a.h == 0)
  53.     h = 0;
  54.   else
  55.   {
  56.     <T>DLListNode* p = a.h;
  57.     <T>DLListNode* t = new <T>DLListNode(p->hd);
  58.     h = t;
  59.     p = p->fd;
  60.     while (p != a.h)
  61.     {
  62.       <T>DLListNode* n = new <T>DLListNode(p->hd);
  63.       t->fd = n;
  64.       n->bk = t;
  65.       t = n;
  66.         p = p->fd;
  67.     }
  68.     t->fd = h;
  69.     h->bk = t;
  70.     return;
  71.   }
  72. }
  73.  
  74. inline <T>DLList& <T>DLList::operator = (<T>DLList& a)
  75. {
  76.   if (h != a.h)
  77.   {
  78.     clear();
  79.     if (a.h != 0)
  80.     {
  81.       <T>DLListNode* p = a.h;
  82.       <T>DLListNode* t = new <T>DLListNode(p->hd);
  83.       h = t;
  84.       p = p->fd;
  85.       while (p != a.h)
  86.       {
  87.         <T>DLListNode* n = new <T>DLListNode(p->hd);
  88.         t->fd = n;
  89.         n->bk = t;
  90.         t = n;
  91.         p = p->fd;
  92.       }
  93.       t->fd = h;
  94.       h->bk = t;
  95.     }
  96.   }
  97.   return *this;
  98. }
  99. void <T>DLList::clear()
  100. {
  101.   if (h == 0)
  102.     return;
  103.  
  104.   <T>DLListNode* p = h->fd;
  105.   h->fd = 0;
  106.   h = 0;
  107.  
  108.   while (p != 0)
  109.   {
  110.     <T>DLListNode* nxt = p->fd;
  111.     delete(p);
  112.     p = nxt;
  113.   }
  114. }
  115.  
  116.  
  117. void <T>DLList::prepend(<T&> item)
  118. {
  119.   <T>DLListNode* t = new <T>DLListNode(item);
  120.   if (h == 0)
  121.     t->fd = t->bk = h = t;
  122.   else
  123.   {
  124.     t->fd = h->fd;
  125.     t->bk = h;
  126.     h->fd = t;
  127.     h = t;
  128.   }
  129. }
  130.  
  131. void <T>DLList::append(<T&> item)
  132. {
  133.   <T>DLListNode* t = new <T>DLListNode(item);
  134.   if (h == 0)
  135.     t->fd = t->bk = h = t;
  136.   else
  137.   {
  138.     t->fd = h;
  139.     t->bk = h->bk;
  140.     h->bk->fd = t;
  141.     h->bk = t;
  142.   }
  143. }
  144.  
  145.  
  146. void <T>DLListTrav::insert_after(<T&> item)
  147. {
  148.   if (current == 0)
  149.   {
  150.     L->error("insert_after: null traverser");
  151.     return;
  152.   }
  153.   <T>DLListNode* t = new <T>DLListNode(item, current, current->fd);
  154.   current->fd->bk = t;
  155.   current->fd = t;
  156. }
  157.  
  158. void <T>DLListTrav::insert_before(<T&> item)
  159. {
  160.   if (current == 0)
  161.   {
  162.     L->error("insert_before: null traverser");
  163.     return;
  164.   }
  165.   <T>DLListNode* t = new <T>DLListNode(item, current->bk, current);
  166.   current->bk->fd = t;
  167.   current->bk = t;
  168.   if (current == L->h)
  169.     L->h = t;
  170. }
  171.  
  172. void <T>DLListTrav::del(int dir = 1)
  173. {
  174.   if (current == 0)
  175.   {
  176.     L->error("del: null traverser");
  177.     return;
  178.   }
  179.   <T>DLListNode* t = current;
  180.   if (t->fd == t)
  181.     current = L->h = 0;
  182.   else
  183.   {
  184.     if (dir < 0)
  185.     {
  186.       if (t == L->h)
  187.         current == 0;
  188.       else
  189.         current = t->bk;
  190.     }
  191.     else
  192.     {
  193.       if (t == L->h->bk)
  194.         current = 0;
  195.       else
  196.         current = t->fd;
  197.     }
  198.     t->bk->fd = t->fd;
  199.     t->fd->bk = t->bk;
  200.     if (t == L->h)
  201.       L->h = t->fd;
  202.   }
  203.   delete t;
  204. }
  205.  
  206. <T> <T>DLList::remove_front()
  207. {
  208.   if (h == 0)
  209.     error("remove_front of empty list");
  210.   <T>DLListNode* t = h;
  211.   <T> res = t->hd;
  212.   if (h->fd == h)
  213.     h = 0;
  214.   else
  215.   {
  216.     h->fd->bk = h->bk;
  217.     h->bk->fd = h->fd;
  218.     h = h->fd;
  219.   }
  220.   delete t;
  221.   return res;
  222. }
  223.  
  224.  
  225. void <T>DLList::del_front()
  226. {
  227.   if (h == 0)
  228.     error("del_front of empty list");
  229.   <T>DLListNode* t = h;
  230.   if (h->fd == h)
  231.     h = 0;
  232.   else
  233.   {
  234.     h->fd->bk = h->bk;
  235.     h->bk->fd = h->fd;
  236.     h = h->fd;
  237.   }
  238.   delete t;
  239. }
  240.  
  241. <T> <T>DLList::remove_rear()
  242. {
  243.   if (h == 0)
  244.     error("remove_rear of empty list");
  245.   <T>DLListNode* t = h->bk;
  246.   <T> res = t->hd;
  247.   if (h->fd == h)
  248.     h = 0;
  249.   else
  250.   {
  251.     t->fd->bk = t->bk;
  252.     t->bk->fd = t->fd;
  253.   }
  254.   delete t;
  255.   return res;
  256. }
  257.  
  258.  
  259. void <T>DLList::del_rear()
  260. {
  261.   if (h == 0)
  262.     error("del_rear of empty list");
  263.   <T>DLListNode* t = h->bk;
  264.   if (h->fd == h)
  265.     h = 0;
  266.   else
  267.   {
  268.     t->fd->bk = t->bk;
  269.     t->bk->fd = t->fd;
  270.   }
  271.   delete t;
  272. }
  273.