home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c017 / 7.ddi / GPPLIB.ZIP / BSTASSOC.CC < prev    next >
Encoding:
C/C++ Source or Header  |  1988-08-17  |  5.0 KB  |  255 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><U>BSTAssoc.h"
  26.  
  27. // error handling
  28.  
  29.  
  30. void default_<T><U>BSTAssoc_error_handler(char* msg)
  31. {
  32.   cerr << "Fatal <T><U>BSTAssoc error. " << msg << "\n";
  33.   exit(1);
  34. }
  35.  
  36. one_arg_error_handler_t <T><U>BSTAssoc_error_handler = default_<T><U>BSTAssoc_error_handler;
  37.  
  38. one_arg_error_handler_t set_<T><U>BSTAssoc_error_handler(one_arg_error_handler_t f)
  39. {
  40.   one_arg_error_handler_t old = <T><U>BSTAssoc_error_handler;
  41.   <T><U>BSTAssoc_error_handler = f;
  42.   return old;
  43. }
  44.  
  45. void <T><U>BSTAssoc::error(const char* msg)
  46. {
  47.   (*<T><U>BSTAssoc_error_handler)(msg);
  48. }
  49.  
  50.  
  51. int <T><U>BSTAssoc::contains(<T&> key)
  52. {
  53.   <T><U>BSTAssocNode* t = root;
  54.   for (;;)
  55.   {
  56.     if (t == 0)
  57.       return 0;
  58.     int comp = key_key_cmp(t->key, key);
  59.     if (comp == 0)
  60.       return 1;
  61.     else if (comp > 0)
  62.       t = t->lt;
  63.     else
  64.       t = t->rt;
  65.   }
  66. }
  67.  
  68. <U>& <T><U>BSTAssoc::operator[](<T&> key)
  69. {
  70.   if (root == 0)
  71.   {
  72.     ++cnt;
  73.     root = new <T><U>BSTAssocNode(key);
  74.     return root->cont;
  75.   }
  76.  
  77.   <T><U>BSTAssocNode* t = root;
  78.   <T><U>BSTAssocNode* p = root;
  79.   int comp;
  80.   for (;;)
  81.   {
  82.     if (t == 0)
  83.     {
  84.       ++cnt;
  85.       t = new <T><U>BSTAssocNode(key);
  86.       if (comp > 0)
  87.         p->lt = t;
  88.       else
  89.         p->rt = t;
  90.       return t->cont;
  91.     }
  92.     p = t;
  93.     comp = key_key_cmp(t->key, key);
  94.     if (comp == 0)
  95.       return t->cont;
  96.     else if (comp > 0)
  97.       t = t->lt;
  98.     else
  99.       t = t->rt;
  100.   }
  101. }
  102.  
  103.  
  104. int <T><U>BSTAssoc::del(<T&> key)
  105. {
  106.   <T><U>BSTAssocNode* t = root;
  107.   <T><U>BSTAssocNode* p = root;
  108.   for (;;)
  109.   {
  110.     if (t == 0)
  111.       return 0;
  112.     int comp = key_key_cmp(t->key, key);
  113.     if (comp == 0)
  114.     {
  115.       --cnt;
  116.       <T><U>BSTAssocNode* repl;
  117.       if (t->lt == 0)
  118.         repl = t->rt;
  119.       else if (t->rt == 0)
  120.         repl = t->lt;
  121.       else
  122.       {
  123.         <T><U>BSTAssocNode* prepl = t;
  124.         repl = t->lt;
  125.         while (repl->rt != 0)
  126.         {
  127.           prepl = repl;
  128.           repl = repl->rt;
  129.         }
  130.         if (prepl != t)
  131.         {
  132.           prepl->rt = repl->lt;
  133.           repl->lt = t->lt;
  134.         }
  135.         repl->rt = t->rt;
  136.       }
  137.       if (t == root)
  138.         root = repl;
  139.       else if (t == p->lt)
  140.         p->lt = repl;
  141.       else
  142.         p->rt = repl;
  143.       return 1;
  144.     }
  145.     p = t;
  146.     if (comp > 0)
  147.       t = t->lt;
  148.     else
  149.       t = t->rt;
  150.   }
  151. }
  152.  
  153.  
  154. void <T><U>BSTAssoc::_kill(<T><U>BSTAssocNode* t)
  155. {
  156.   if (t != 0)
  157.   {
  158.     _kill(t->lt);
  159.     _kill(t->rt);
  160.     delete t;
  161.   }
  162. }
  163.  
  164. void <T><U>BSTAssoc::_apply(<U>Procedure f, <T><U>BSTAssocNode* t)
  165. {
  166.   if (t != 0)
  167.   {
  168.     _apply(f, t->lt);
  169.     (*f)(t->cont);
  170.     _apply(f, t->rt);
  171.   }
  172. }
  173.  
  174. <T><U>BSTAssocNode* <T><U>BSTAssoc::_copy(<T><U>BSTAssocNode* t)
  175. {
  176.   if (t == 0)
  177.     return 0;
  178.   else
  179.     return new <T><U>BSTAssocNode(t->key, t->cont, _copy(t->lt), _copy(t->rt));
  180. }
  181.  
  182. static <T><U>BSTAssocNode* _BSTbal(<T><U>BSTAssocNode** a, int lo, int hi)
  183. {
  184.   if (lo > hi)
  185.     return 0;
  186.   else if (lo == hi)
  187.   {
  188.     a[lo]->lt = a[lo]->rt = 0;
  189.     return a[lo];
  190.   }
  191.   else
  192.   {
  193.     int mid = (lo + hi) / 2;
  194.     a[mid]->lt = _BSTbal(a, lo, mid-1);
  195.     a[mid]->rt = _BSTbal(a, mid+1, hi);
  196.     return a[mid];
  197.   }
  198. }
  199.  
  200. static int _BSTfill(<T><U>BSTAssocNode** a, int pos, <T><U>BSTAssocNode* t)
  201. {
  202.   if (t == 0)
  203.     return pos;
  204.   else
  205.   {
  206.     int p = _BSTfill(a, pos, t->lt);
  207.     a[p++] = t;
  208.     return _BSTfill(a, p, t->rt);
  209.   }
  210. }
  211.  
  212. void <T><U>BSTAssoc::balance()
  213. {
  214.   if (root != 0)
  215.   {
  216.     <T><U>BSTAssocNode** a = new <T><U>BSTAssocNodePtr [cnt];
  217.     _BSTfill(a, 0, root);
  218.     root = _BSTbal(a, 0, cnt-1);
  219.     delete a;
  220.   }
  221. }
  222.  
  223.  
  224.  
  225. void <T><U>BSTAssocTrav::reset()
  226. {
  227.   if (stk != 0)
  228.     delete stk;
  229.   stk = new <T><U>BSTAssocNodePtr[h->cnt];
  230.   sp = -1;
  231.   <T><U>BSTAssocNode* p = h->root;
  232.   while (p != 0)
  233.   {
  234.     stk[++sp] = p;
  235.     p = p->lt;
  236.   }
  237. }
  238.  
  239.  
  240. void <T><U>BSTAssocTrav::advance()
  241. {
  242.   if (sp >= 0)
  243.   {
  244.     <T><U>BSTAssocNode* p = stk[sp--]->rt;
  245.     while (p != 0)
  246.     {
  247.       stk[++sp] = p;
  248.       p = p->lt;
  249.     }
  250.   }
  251. }
  252.     
  253.  
  254.   
  255.