home *** CD-ROM | disk | FTP | other *** search
/ GEMini Atari / GEMini_Atari_CD-ROM_Walnut_Creek_December_1993.iso / files / gnu / g__lib / avlmap.ccp < prev    next >
Encoding:
Text File  |  1993-07-23  |  12.7 KB  |  608 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 <assert.h>
  26. #include "<T>.<C>.AVLMap.h"
  27.  
  28.  
  29. /*
  30.  constants & inlines for maintaining balance & thread status in tree nodes
  31. */
  32.  
  33. #define AVLBALANCEMASK    3
  34. #define AVLBALANCED       0
  35. #define AVLLEFTHEAVY      1
  36. #define AVLRIGHTHEAVY     2
  37.  
  38. #define LTHREADBIT        4
  39. #define RTHREADBIT        8
  40.  
  41.  
  42. static inline int bf(<T><C>AVLNode* t)
  43. {
  44.   return t->stat & AVLBALANCEMASK;
  45. }
  46.  
  47. static inline void set_bf(<T><C>AVLNode* t, int b)
  48. {
  49.   t->stat = (t->stat & ~AVLBALANCEMASK) | (b & AVLBALANCEMASK);
  50. }
  51.  
  52.  
  53. static inline int rthread(<T><C>AVLNode* t)
  54. {
  55.   return t->stat & RTHREADBIT;
  56. }
  57.  
  58. static inline void set_rthread(<T><C>AVLNode* t, int b)
  59. {
  60.   if (b)
  61.     t->stat |= RTHREADBIT;
  62.   else
  63.     t->stat &= ~RTHREADBIT;
  64. }
  65.  
  66. static inline int lthread(<T><C>AVLNode* t)
  67. {
  68.   return t->stat & LTHREADBIT;
  69. }
  70.  
  71. static inline void set_lthread(<T><C>AVLNode* t, int b)
  72. {
  73.   if (b)
  74.     t->stat |= LTHREADBIT;
  75.   else
  76.     t->stat &= ~LTHREADBIT;
  77. }
  78.  
  79. /*
  80.  traversal primitives
  81. */
  82.  
  83.  
  84. <T><C>AVLNode* <T><C>AVLMap::leftmost()
  85. {
  86.   <T><C>AVLNode* t = root;
  87.   if (t != 0) while (t->lt != 0) t = t->lt;
  88.   return t;
  89. }
  90.  
  91. <T><C>AVLNode* <T><C>AVLMap::rightmost()
  92. {
  93.   <T><C>AVLNode* t = root;
  94.   if (t != 0) while (t->rt != 0) t = t->rt;
  95.   return t;
  96. }
  97.  
  98. <T><C>AVLNode* <T><C>AVLMap::succ(<T><C>AVLNode* t)
  99. {
  100.   <T><C>AVLNode* r = t->rt;
  101.   if (!rthread(t)) while (!lthread(r)) r = r->lt;
  102.   return r;
  103. }
  104.  
  105. <T><C>AVLNode* <T><C>AVLMap::pred(<T><C>AVLNode* t)
  106. {
  107.   <T><C>AVLNode* l = t->lt;
  108.   if (!lthread(t)) while (!rthread(l)) l = l->rt;
  109.   return l;
  110. }
  111.  
  112.  
  113. Pix <T><C>AVLMap::seek(<T&> key)
  114. {
  115.   <T><C>AVLNode* t = root;
  116.   if (t == 0)
  117.     return 0;
  118.   for (;;)
  119.   {
  120.     int cmp = <T>CMP(key, t->item);
  121.     if (cmp == 0)
  122.       return Pix(t);
  123.     else if (cmp < 0)
  124.     {
  125.       if (lthread(t))
  126.         return 0;
  127.       else
  128.         t = t->lt;
  129.     }
  130.     else if (rthread(t))
  131.       return 0;
  132.     else
  133.       t = t->rt;
  134.   }
  135. }
  136.  
  137.  
  138. /*
  139.  The combination of threads and AVL bits make adding & deleting
  140.  interesting, but very awkward.
  141.  
  142.  We use the following statics to avoid passing them around recursively
  143. */
  144.  
  145. static int _need_rebalancing;   // to send back balance info from rec. calls
  146. static <T>*   _target_item;     // add/del_item target
  147. static <T><C>AVLNode* _found_node; // returned added/deleted node
  148. static int    _already_found;   // for deletion subcases
  149.  
  150. static <T><C>AVLNode** _hold_nodes;       // used for rebuilding trees
  151. static int  _max_hold_index;              // # elements-1 in _hold_nodes
  152.  
  153.  
  154. void <T><C>AVLMap:: _add(<T><C>AVLNode*& t)
  155. {
  156.   int cmp = <T>CMP(*_target_item, t->item);
  157.   if (cmp == 0)
  158.   {
  159.     _found_node = t;
  160.     return;
  161.   }
  162.   else if (cmp < 0)
  163.   {
  164.     if (lthread(t))
  165.     {
  166.       ++count;
  167.       _found_node = new <T><C>AVLNode(*_target_item, def);
  168.       set_lthread(_found_node, 1);
  169.       set_rthread(_found_node, 1);
  170.       _found_node->lt = t->lt;
  171.       _found_node->rt = t;
  172.       t->lt = _found_node;
  173.       set_lthread(t, 0);
  174.       _need_rebalancing = 1;
  175.     }
  176.     else
  177.       _add(t->lt);
  178.     if (_need_rebalancing)
  179.     {
  180.       switch(bf(t))
  181.       {
  182.       case AVLRIGHTHEAVY:
  183.         set_bf(t, AVLBALANCED);
  184.         _need_rebalancing = 0;
  185.         return;
  186.       case AVLBALANCED:
  187.         set_bf(t, AVLLEFTHEAVY);
  188.         return;
  189.       case AVLLEFTHEAVY:
  190.         <T><C>AVLNode* l = t->lt;
  191.         if (bf(l) == AVLLEFTHEAVY)
  192.         {
  193.           if (rthread(l))
  194.             t->lt = l;
  195.           else
  196.             t->lt = l->rt;
  197.           set_lthread(t, rthread(l));
  198.           l->rt = t;
  199.           set_rthread(l, 0);
  200.           set_bf(t, AVLBALANCED);
  201.           set_bf(l, AVLBALANCED);
  202.           t = l;
  203.           _need_rebalancing = 0;
  204.         }
  205.         else
  206.         {
  207.           <T><C>AVLNode* r = l->rt;
  208.           set_rthread(l, lthread(r));
  209.           if (lthread(r))
  210.             l->rt = r;
  211.           else
  212.             l->rt = r->lt;
  213.           r->lt = l;
  214.           set_lthread(r, 0);
  215.           set_lthread(t, rthread(r));
  216.           if (rthread(r))
  217.             t->lt = r;
  218.           else
  219.             t->lt = r->rt;
  220.           r->rt = t;
  221.           set_rthread(r, 0);
  222.           if (bf(r) == AVLLEFTHEAVY)
  223.             set_bf(t, AVLRIGHTHEAVY);
  224.           else
  225.             set_bf(t, AVLBALANCED);
  226.           if (bf(r) == AVLRIGHTHEAVY)
  227.             set_bf(l, AVLLEFTHEAVY);
  228.           else
  229.             set_bf(l, AVLBALANCED);
  230.           set_bf(r, AVLBALANCED);
  231.           t = r;
  232.           _need_rebalancing = 0;
  233.           return;
  234.         }
  235.       }
  236.     }
  237.   }
  238.   else
  239.   {
  240.     if (rthread(t))
  241.     {
  242.       ++count;
  243.       _found_node = new <T><C>AVLNode(*_target_item, def);
  244.       set_rthread(t, 0);
  245.       set_lthread(_found_node, 1);
  246.       set_rthread(_found_node, 1);
  247.       _found_node->lt = t;
  248.       _found_node->rt = t->rt;
  249.       t->rt = _found_node;
  250.       _need_rebalancing = 1;
  251.     }
  252.     else
  253.       _add(t->rt);
  254.     if (_need_rebalancing)
  255.     {
  256.       switch(bf(t))
  257.       {
  258.       case AVLLEFTHEAVY:
  259.         set_bf(t, AVLBALANCED);
  260.         _need_rebalancing = 0;
  261.         return;
  262.       case AVLBALANCED:
  263.         set_bf(t, AVLRIGHTHEAVY);
  264.         return;
  265.       case AVLRIGHTHEAVY:
  266.         <T><C>AVLNode* r = t->rt;
  267.         if (bf(r) == AVLRIGHTHEAVY)
  268.         {
  269.           if (lthread(r))
  270.             t->rt = r;
  271.           else
  272.             t->rt = r->lt;
  273.           set_rthread(t, lthread(r));
  274.           r->lt = t;
  275.           set_lthread(r, 0);
  276.           set_bf(t, AVLBALANCED);
  277.           set_bf(r, AVLBALANCED);
  278.           t = r;
  279.           _need_rebalancing = 0;
  280.         }
  281.         else
  282.         {
  283.           <T><C>AVLNode* l = r->lt;
  284.           set_lthread(r, rthread(l));
  285.           if (rthread(l))
  286.             r->lt = l;
  287.           else
  288.             r->lt = l->rt;
  289.           l->rt = r;
  290.           set_rthread(l, 0);
  291.           set_rthread(t, lthread(l));
  292.           if (lthread(l))
  293.             t->rt = l;
  294.           else
  295.             t->rt = l->lt;
  296.           l->lt = t;
  297.           set_lthread(l, 0);
  298.           if (bf(l) == AVLRIGHTHEAVY)
  299.             set_bf(t, AVLLEFTHEAVY);
  300.           else
  301.             set_bf(t, AVLBALANCED);
  302.           if (bf(l) == AVLLEFTHEAVY)
  303.             set_bf(r, AVLRIGHTHEAVY);
  304.           else
  305.             set_bf(r, AVLBALANCED);
  306.           set_bf(l, AVLBALANCED);
  307.           t = l;
  308.           _need_rebalancing = 0;
  309.           return;
  310.         }
  311.       }
  312.     }
  313.   }
  314. }
  315.  
  316.     
  317. <C>& <T><C>AVLMap::operator [] (<T&> item)
  318. {
  319.   if (root == 0)
  320.   {
  321.     ++count;
  322.     root = new <T><C>AVLNode(item, def);
  323.     set_rthread(root, 1);
  324.     set_lthread(root, 1);
  325.     return root->cont;
  326.   }
  327.   else
  328.   {
  329.     _target_item = &item;
  330.     _need_rebalancing = 0;
  331.     _add(root);
  332.     return _found_node->cont;
  333.   }
  334. }
  335.  
  336.  
  337. void <T><C>AVLMap::_del(<T><C>AVLNode* par, <T><C>AVLNode*& t)
  338. {
  339.   int comp;
  340.   if (_already_found)
  341.   {
  342.     if (rthread(t))
  343.       comp = 0;
  344.     else
  345.       comp = 1;
  346.   }
  347.   else 
  348.     comp = <T>CMP(*_target_item, t->item);
  349.   if (comp == 0)
  350.   {
  351.     if (lthread(t) && rthread(t))
  352.     {
  353.       _found_node = t;
  354.       if (t == par->lt)
  355.       {
  356.         set_lthread(par, 1);
  357.         par->lt = t->lt;
  358.       }
  359.       else
  360.       {
  361.         set_rthread(par, 1);
  362.         par->rt = t->rt;
  363.       }
  364.       _need_rebalancing = 1;
  365.       return;
  366.     }
  367.     else if (lthread(t))
  368.     {
  369.       _found_node = t;
  370.       <T><C>AVLNode* s = succ(t);
  371.       if (s != 0 && lthread(s))
  372.         s->lt = t->lt;
  373.       t = t->rt;
  374.       _need_rebalancing = 1;
  375.       return;
  376.     }
  377.     else if (rthread(t))
  378.     {
  379.       _found_node = t;
  380.       <T><C>AVLNode* p = pred(t);
  381.       if (p != 0 && rthread(p))
  382.         p->rt = t->rt;
  383.       t = t->lt;
  384.       _need_rebalancing = 1;
  385.       return;
  386.     }
  387.     else                        // replace item & find someone deletable
  388.     {
  389.       <T><C>AVLNode* p = pred(t);
  390.       t->item = p->item;
  391.       _already_found = 1;
  392.       comp = -1;                // fall through below to left
  393.     }
  394.   }
  395.  
  396.   if (comp < 0)
  397.   {
  398.     if (lthread(t))
  399.       return;
  400.     _del(t, t->lt);
  401.     if (!_need_rebalancing)
  402.       return;
  403.     switch (bf(t))
  404.     {
  405.     case AVLLEFTHEAVY:
  406.       set_bf(t, AVLBALANCED);
  407.       return;
  408.     case AVLBALANCED:
  409.       set_bf(t, AVLRIGHTHEAVY);
  410.       _need_rebalancing = 0;
  411.       return;
  412.     case AVLRIGHTHEAVY:
  413.       <T><C>AVLNode* r = t->rt;
  414.       switch (bf(r))
  415.       {
  416.       case AVLBALANCED:
  417.         if (lthread(r))
  418.           t->rt = r;
  419.         else
  420.           t->rt = r->lt;
  421.         set_rthread(t, lthread(r));
  422.         r->lt = t;
  423.         set_lthread(r, 0);
  424.         set_bf(t, AVLRIGHTHEAVY);
  425.         set_bf(r, AVLLEFTHEAVY);
  426.         _need_rebalancing = 0;
  427.         t = r;
  428.         return;
  429.       case AVLRIGHTHEAVY:
  430.         if (lthread(r))
  431.           t->rt = r;
  432.         else
  433.           t->rt = r->lt;
  434.         set_rthread(t, lthread(r));
  435.         r->lt = t;
  436.         set_lthread(r, 0);
  437.         set_bf(t, AVLBALANCED);
  438.         set_bf(r, AVLBALANCED);
  439.         t = r;
  440.         return;
  441.       case AVLLEFTHEAVY:
  442.         <T><C>AVLNode* l = r->lt;
  443.         set_lthread(r, rthread(l));
  444.         if (rthread(l))
  445.           r->lt = l;
  446.         else
  447.           r->lt = l->rt;
  448.         l->rt = r;
  449.         set_rthread(l, 0);
  450.         set_rthread(t, lthread(l));
  451.         if (lthread(l))
  452.           t->rt = l;
  453.         else
  454.           t->rt = l->lt;
  455.         l->lt = t;
  456.         set_lthread(l, 0);
  457.         if (bf(l) == AVLRIGHTHEAVY)
  458.           set_bf(t, AVLLEFTHEAVY);
  459.         else
  460.           set_bf(t, AVLBALANCED);
  461.         if (bf(l) == AVLLEFTHEAVY)
  462.           set_bf(r, AVLRIGHTHEAVY);
  463.         else
  464.           set_bf(r, AVLBALANCED);
  465.         set_bf(l, AVLBALANCED);
  466.         t = l;
  467.         return;
  468.       }
  469.     }
  470.   }
  471.   else
  472.   {
  473.     if (rthread(t))
  474.       return;
  475.     _del(t, t->rt);
  476.     if (!_need_rebalancing)
  477.       return;
  478.     switch (bf(t))
  479.     {
  480.     case AVLRIGHTHEAVY:
  481.       set_bf(t, AVLBALANCED);
  482.       return;
  483.     case AVLBALANCED:
  484.       set_bf(t, AVLLEFTHEAVY);
  485.       _need_rebalancing = 0;
  486.       return;
  487.     case AVLLEFTHEAVY:
  488.       <T><C>AVLNode* l = t->lt;
  489.       switch (bf(l))
  490.       {
  491.       case AVLBALANCED:
  492.         if (rthread(l))
  493.           t->lt = l;
  494.         else
  495.           t->lt = l->rt;
  496.         set_lthread(t, rthread(l));
  497.         l->rt = t;
  498.         set_rthread(l, 0);
  499.         set_bf(t, AVLLEFTHEAVY);
  500.         set_bf(l, AVLRIGHTHEAVY);
  501.         _need_rebalancing = 0;
  502.         t = l;
  503.         return;
  504.       case AVLLEFTHEAVY:
  505.         if (rthread(l))
  506.           t->lt = l;
  507.         else
  508.           t->lt = l->rt;
  509.         set_lthread(t, rthread(l));
  510.         l->rt = t;
  511.         set_rthread(l, 0);
  512.         set_bf(t, AVLBALANCED);
  513.         set_bf(l, AVLBALANCED);
  514.         t = l;
  515.         return;
  516.       case AVLRIGHTHEAVY:
  517.         <T><C>AVLNode* r = l->rt;
  518.         set_rthread(l, lthread(r));
  519.         if (lthread(r))
  520.           l->rt = r;
  521.         else
  522.           l->rt = r->lt;
  523.         r->lt = l;
  524.         set_lthread(r, 0);
  525.         set_lthread(t, rthread(r));
  526.         if (rthread(r))
  527.           t->lt = r;
  528.         else
  529.           t->lt = r->rt;
  530.         r->rt = t;
  531.         set_rthread(r, 0);
  532.         if (bf(r) == AVLLEFTHEAVY)
  533.           set_bf(t, AVLRIGHTHEAVY);
  534.         else
  535.           set_bf(t, AVLBALANCED);
  536.         if (bf(r) == AVLRIGHTHEAVY)
  537.           set_bf(l, AVLLEFTHEAVY);
  538.         else
  539.           set_bf(l, AVLBALANCED);
  540.         set_bf(r, AVLBALANCED);
  541.         t = r;
  542.         return;
  543.       }
  544.     }
  545.   }
  546. }
  547.  
  548.         
  549.  
  550. void <T><C>AVLMap::del(<T&> item)
  551. {
  552.   if (root == 0) return;
  553.   _need_rebalancing = 0;
  554.   _already_found = 0;
  555.   _found_node = 0;
  556.   _target_item = &item;
  557.   _del(root, root);
  558.   if (_found_node)
  559.   {
  560.     delete(_found_node);
  561.     if (--count == 0)
  562.       root = 0;
  563.   }
  564. }
  565.  
  566. void <T><C>AVLMap::_kill(<T><C>AVLNode* t)
  567. {
  568.   if (t != 0)
  569.   {
  570.     if (!lthread(t)) _kill(t->lt);
  571.     if (!rthread(t)) _kill(t->rt);
  572.     delete t;
  573.   }
  574. }
  575.  
  576.  
  577. <T><C>AVLMap::<T><C>AVLMap(<T><C>AVLMap& b) :(b.def)
  578. {
  579.   root = 0;
  580.   count = 0;
  581.   for (Pix i = b.first(); i != 0; b.next(i)) 
  582.     (*this)[b.key(i)] = b.contents(i);
  583. }
  584.  
  585.  
  586. int <T><C>AVLMap::OK()
  587. {
  588.   int v = 1;
  589.   if (root == 0) 
  590.     v = count == 0;
  591.   else
  592.   {
  593.     int n = 1;
  594.     <T><C>AVLNode* trail = leftmost();
  595.     <T><C>AVLNode* t = succ(trail);
  596.     while (t != 0)
  597.     {
  598.       ++n;
  599.       v &= <T>CMP(trail->item, t->item) < 0;
  600.       trail = t;
  601.       t = succ(t);
  602.     }
  603.     v &= n == count;
  604.   }
  605.   if (!v) error("invariant failure");
  606.   return v;
  607. }
  608.