home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c017 / 7.ddi / GPPLIB.ZIP / OSET.CC < prev    next >
Encoding:
C/C++ Source or Header  |  1988-08-17  |  13.9 KB  |  830 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 "<T>OSet.h"
  25.  
  26.  
  27. // error handling
  28.  
  29.  
  30. void default_<T>OSet_error_handler(char* msg)
  31. {
  32.   cerr << "Fatal <T>OSet error. " << msg << "\n";
  33.   exit(1);
  34. }
  35.  
  36. one_arg_error_handler_t <T>OSet_error_handler = default_<T>OSet_error_handler;
  37.  
  38. one_arg_error_handler_t set_<T>OSet_error_handler(one_arg_error_handler_t f)
  39. {
  40.   one_arg_error_handler_t old = <T>OSet_error_handler;
  41.   <T>OSet_error_handler = f;
  42.   return old;
  43. }
  44.  
  45. void <T>OSet::error(const char* msg)
  46. {
  47.   (*<T>OSet_error_handler)(msg);
  48. }
  49.  
  50. <T>SLListNode* copy<T>SLListNodes(<T>SLListNode* a)
  51. {
  52.   if (a == 0)
  53.     return a;
  54.   else
  55.   {
  56.     <T>SLListNode* h = new <T>SLListNode(a->hd);
  57.     <T>SLListNode* trail = h;
  58.     for(a = a->tl; a != 0; a = a->tl)
  59.     {
  60.       <T>SLListNode* n = new <T>SLListNode(a->hd);
  61.       trail->tl = n;
  62.       trail = n;
  63.     }
  64.     return h;
  65.   }
  66. }
  67.  
  68.  
  69. void <T>OSet::clear()
  70. {
  71.   <T>SLListNode* p = P;
  72.   P = 0;
  73.   while (p != 0)
  74.   {
  75.     <T>SLListNode* nxt = p->tl;
  76.     delete(p);
  77.     p = nxt;
  78.   }
  79. }
  80.  
  81.  
  82. void <T>OSet::apply(<T>Procedure f)
  83. {
  84.   for(<T>SLListNode* p = P; p != 0; p = p->tl)
  85.     (*f)((p->hd));
  86. }
  87.  
  88.  
  89. <T> <T>OSet::reduce(<T>Combiner f, <T&> base)
  90. {
  91.   <T> r = base;
  92.   for(<T>SLListNode* p = P; p != 0; p = p->tl)
  93.     r = (*f)(r, (p->hd));
  94.   return r;
  95. }
  96.  
  97. int <T>OSet::contains(<T&> targ)
  98. {
  99.   <T>SLListNode* p = P;
  100.   for (;;)
  101.   {
  102.     if (p == 0)
  103.       return 0;
  104.     int cmp = COMPARISON_FUNCTION(p->hd, targ);
  105.     if (cmp == 0)
  106.       return 1;
  107.     else if (cmp > 0)
  108.       return 0;
  109.     else
  110.       p = p->tl;
  111.   }
  112. }
  113.  
  114.  
  115. void <T>OSetTrav::advance_to(<T&> targ)
  116. {
  117.   <T>SLListNode* p = P;
  118.   for (;;)
  119.   {
  120.     if (p == 0)
  121.     {
  122.       P = 0;
  123.       return;
  124.     }
  125.     int cmp = COMPARISON_FUNCTION(p->hd, targ);
  126.     if (cmp == 0)
  127.     {
  128.       P = p;
  129.       return;
  130.     }
  131.     else if (cmp > 0)
  132.     {
  133.       P = 0;
  134.       return;
  135.     }
  136.     else
  137.       p = p->tl;
  138.   }
  139. }
  140.  
  141.  
  142.  
  143. void <T>OSet::del(<T&> targ)
  144. {
  145.   <T>SLListNode* h = P;
  146.   if (h == 0)
  147.     return;
  148.   int cmp = COMPARISON_FUNCTION(h->hd, targ);
  149.   if (cmp == 0)
  150.   {
  151.     <T>SLListNode* nxt = h->tl;
  152.     delete(h);
  153.     P = nxt;
  154.     return;
  155.   }
  156.  
  157.   <T>SLListNode* trail = h;
  158.   <T>SLListNode* p = h->tl;
  159.   for (;;)
  160.   {
  161.     if (p == 0)
  162.       return;
  163.     cmp = COMPARISON_FUNCTION(p->hd, targ);
  164.     if (cmp == 0)
  165.     {
  166.       <T>SLListNode* nxt = p->tl;
  167.       delete(p);
  168.       trail->tl = nxt;
  169.       return;
  170.     }
  171.     else if (cmp > 0)
  172.       return;
  173.     else
  174.     {
  175.       trail = p;
  176.       p = p->tl;
  177.     }
  178.   }
  179. }
  180.  
  181.  
  182. void <T>OSet::add(<T&> item)
  183. {
  184.   <T>SLListNode* a = P;
  185.   if (a == 0)
  186.   {
  187.     P = new <T>SLListNode(item);
  188.     return;
  189.   }
  190.   int cmp = COMPARISON_FUNCTION(a->hd, item);
  191.   if (cmp == 0)
  192.     return;
  193.   else if (cmp > 0)
  194.   {
  195.     P = new <T>SLListNode(item);
  196.     P->tl = a;
  197.     return;
  198.   }
  199.   else
  200.   {
  201.     <T>SLListNode* h = a;
  202.     <T>SLListNode* trail = h;
  203.     a = a->tl;
  204.     for (;;)
  205.     {
  206.       if (a == 0)
  207.       {
  208.         <T>SLListNode* n = new <T>SLListNode(item);
  209.         trail->tl = n;
  210.         return;
  211.       }
  212.       cmp = COMPARISON_FUNCTION(a->hd, item);
  213.       if (cmp == 0)
  214.         return;
  215.       else if (cmp < 0)
  216.       {
  217.         trail = a;
  218.         a = a->tl;
  219.       }
  220.       else
  221.       {
  222.         <T>SLListNode* n = new <T>SLListNode(item);
  223.         n->tl = a;
  224.         trail->tl = n;
  225.         return;
  226.       }
  227.     }
  228.   }
  229. }
  230.  
  231. int operator == (<T>OSet& x, <T>OSet& y)
  232. {
  233.   <T>SLListNode* a = x.P;
  234.   <T>SLListNode* b = y.P;
  235.   for (;;)
  236.   {
  237.     if (a == 0)
  238.       return b == 0;
  239.     else if (b == 0 || COMPARISON_FUNCTION(a->hd, b->hd) != 0)
  240.       return 0;
  241.     else
  242.     {
  243.       a = a->tl;
  244.       b = b->tl;
  245.     }
  246.   }
  247. }
  248.  
  249. int operator <= (<T>OSet& x, <T>OSet& y)
  250. {
  251.   <T>SLListNode* a = x.P;
  252.   <T>SLListNode* b = y.P;
  253.   for (;;)
  254.   {
  255.     if (a == 0)
  256.       return 1;
  257.     else if (b == 0)
  258.       return 0;
  259.     int cmp = COMPARISON_FUNCTION(a->hd, b->hd);
  260.     if (cmp == 0)
  261.     {
  262.       a = a->tl;
  263.       b = b->tl;
  264.     }
  265.     else if (cmp < 0)
  266.       return 0;
  267.     else
  268.       b = b->tl;
  269.   }
  270. }
  271.  
  272. int operator < (<T>OSet& x, <T>OSet& y)
  273. {
  274.   <T>SLListNode* a = x.P;
  275.   <T>SLListNode* b = y.P;
  276.   int one_diff = 0;
  277.   for (;;)
  278.   {
  279.     if (a == 0)
  280.       return one_diff;
  281.     else if (b == 0)
  282.       return 0;
  283.     int cmp = COMPARISON_FUNCTION(a->hd, b->hd);
  284.     if (cmp == 0)
  285.     {
  286.       a = a->tl;
  287.       b = b->tl;
  288.     }
  289.     else if (cmp < 0)
  290.       return 0;
  291.     else
  292.     {
  293.       one_diff = 1;
  294.       b = b->tl;
  295.     }
  296.   }
  297. }
  298.  
  299.  
  300.  
  301. <T>OSet operator | (<T>OSet& x, <T>OSet& y)
  302. {
  303.   <T>SLListNode* a = x.P;
  304.   <T>SLListNode* b = y.P;
  305.   if (a == 0)
  306.     return <T>OSet(y);
  307.   else if (b == 0)
  308.     return <T>OSet(x);
  309.  
  310.   int cmp = COMPARISON_FUNCTION(a->hd, b->hd);
  311.   <T>SLListNode* h = new <T>SLListNode;
  312.   if (cmp <= 0)
  313.   {
  314.     h->hd = a->hd;
  315.     a = a->tl;
  316.   }
  317.   else
  318.   {
  319.     h->hd = b->hd;
  320.     b = b->tl;
  321.   }
  322.  
  323.   <T>SLListNode* r = h;
  324.  
  325.   for(;;)
  326.   {
  327.     if (a == 0)
  328.     {
  329.       r->tl = copy<T>SLListNodes(b);
  330.       return <T>OSet(h);
  331.     }
  332.     else if (b == 0)
  333.     {
  334.       r->tl = copy<T>SLListNodes(a);
  335.       return <T>OSet(h);
  336.     }
  337.     cmp = COMPARISON_FUNCTION(a->hd, b->hd);
  338.     if (cmp == 0)
  339.       a = a->tl;
  340.     else if (cmp < 0)
  341.     {
  342.       <T>SLListNode* n = new <T>SLListNode(a->hd);
  343.       r->tl = n;
  344.       r = n;
  345.       a = a->tl;
  346.     }
  347.     else
  348.     {
  349.       <T>SLListNode* n = new <T>SLListNode(b->hd);
  350.       r->tl = n;
  351.       r = n;
  352.       b = b->tl;
  353.     }
  354.   }
  355. }
  356.  
  357. void <T>OSet::destructive_union(<T>OSet& y)
  358. {
  359.   <T>SLListNode* a = P;
  360.   <T>SLListNode* b = y.P;
  361.   y.P = 0;
  362.   if (b == 0)
  363.     return;
  364.   if (a == 0)
  365.   {
  366.     P = b;
  367.     return;
  368.   }
  369.  
  370.   <T>SLListNode* r;
  371.   int cmp = COMPARISON_FUNCTION(a->hd, b->hd);
  372.   if (cmp == 0)
  373.   {
  374.     <T>SLListNode* t = a->tl;
  375.     delete(a);
  376.     a = t;
  377.     r = b;
  378.     b = b->tl;
  379.   }
  380.   else if (cmp < 0)
  381.   {
  382.     r = a;
  383.     a = a->tl;
  384.   }
  385.   else
  386.   {
  387.     r = b;
  388.     b = b->tl;
  389.   }
  390.   P = r;
  391.   for(;;)
  392.   {
  393.     if (a == 0)
  394.     {
  395.       r->tl = b;
  396.       return;
  397.     }
  398.     else if (b == 0)
  399.     {
  400.       r->tl = a;
  401.       return;
  402.     }
  403.     cmp = COMPARISON_FUNCTION(a->hd, b->hd);
  404.     if (cmp == 0)
  405.     {
  406.       <T>SLListNode* t = a->tl;
  407.       delete(a);
  408.       a = t;
  409.       r->tl = b;
  410.       r = b;
  411.       b = b->tl;
  412.     }
  413.     else if (cmp < 0)
  414.     {
  415.       r->tl = a;
  416.       r = a;
  417.       a = a->tl;
  418.     }
  419.     else
  420.     {
  421.       r->tl = b;
  422.       r = b;
  423.       b = b->tl;
  424.     }
  425.   }
  426. }
  427.  
  428. <T>OSet& <T>OSet::operator |=(<T>OSet& y)
  429. {
  430.   <T>SLListNode* a = P;
  431.   <T>SLListNode* b = y.P;
  432.   if (b == 0)
  433.     return *this;
  434.   else if (a == 0)
  435.   {
  436.     P = copy<T>SLListNodes(b);
  437.     return *this;
  438.   }
  439.   <T>SLListNode* r;
  440.   int cmp = COMPARISON_FUNCTION(a->hd, b->hd);
  441.   if (cmp <= 0)
  442.   {
  443.     r = a;
  444.     a = a->tl;
  445.   }
  446.   else
  447.   {
  448.     r = new <T>SLListNode(b->hd);
  449.     b = b->tl;
  450.   }
  451.   P = r;
  452.  
  453.   for(;;)
  454.   {
  455.     if (a == 0)
  456.     {
  457.       r->tl = copy<T>SLListNodes(b);
  458.       return *this;
  459.     }
  460.     else if (b == 0)
  461.     {
  462.       r->tl = a;
  463.       return *this;
  464.     }
  465.     cmp = COMPARISON_FUNCTION(a->hd, b->hd);
  466.     if (cmp == 0)
  467.       b = b->tl;
  468.     else if (cmp < 0)
  469.     {
  470.       r->tl = a;
  471.       r = a;
  472.       a = a->tl;
  473.     }
  474.     else
  475.     {
  476.       <T>SLListNode* n = new <T>SLListNode(b->hd);
  477.       r->tl = n;
  478.       r = n;
  479.       b = b->tl;
  480.     }
  481.   }
  482. }
  483.  
  484.  
  485. <T>OSet operator & (<T>OSet& x, <T>OSet& y)
  486. {
  487.   <T>SLListNode* a = x.P;
  488.   <T>SLListNode* b = y.P;
  489.   for (;;)
  490.   {
  491.     if (a == 0 || b == 0)
  492.       return <T>OSet();
  493.     int cmp = COMPARISON_FUNCTION(a->hd, b->hd);
  494.     if (cmp == 0)
  495.       break;
  496.     else if (cmp < 0)
  497.       a = a->tl;
  498.     else
  499.       b = b->tl;
  500.   }
  501.  
  502.   <T>SLListNode* h = new <T>SLListNode(a->hd);
  503.   <T>SLListNode* r = h;
  504.   a = a->tl;
  505.   b = b->tl;
  506.  
  507.   for (;;)
  508.   {
  509.     if (a == 0 || b == 0)
  510.     {
  511.       return <T>OSet(h);
  512.     }
  513.     int cmp = COMPARISON_FUNCTION(a->hd, b->hd);
  514.     if (cmp == 0)
  515.     {
  516.       <T>SLListNode* n = new <T>SLListNode(a->hd);
  517.       r->tl = n;
  518.       r = n;
  519.       a = a->tl;
  520.       b = b->tl;
  521.     }
  522.     else if (cmp < 0)
  523.       a = a->tl;
  524.     else
  525.       b = b->tl;
  526.   }
  527. }
  528.  
  529. <T>OSet& <T>OSet::operator &= (<T>OSet& y)
  530. {
  531.   <T>SLListNode* a = P;
  532.   <T>SLListNode* b = y.P;
  533.   for (;;)
  534.   {
  535.     if (a == 0 || b == 0)
  536.       return *this;
  537.     int cmp = COMPARISON_FUNCTION(a->hd, b->hd);
  538.     if (cmp == 0)
  539.       break;
  540.     else if (cmp < 0)
  541.     {
  542.       <T>SLListNode* t = a->tl;
  543.       delete(a);
  544.       a = t;
  545.     }
  546.     else
  547.       b = b->tl;
  548.   }
  549.   P = a;
  550.   <T>SLListNode* r = a;
  551.   a = a->tl;
  552.   b = b->tl;
  553.  
  554.   for (;;)
  555.   {
  556.     if (a == 0 || b == 0)
  557.     {
  558.       return *this;
  559.     }
  560.     int cmp = COMPARISON_FUNCTION(a->hd, b->hd);
  561.     if (cmp == 0)
  562.     {
  563.       r->tl = a;
  564.       r = a;
  565.       a = a->tl;
  566.       b = b->tl;
  567.     }
  568.     else if (cmp < 0)
  569.     {
  570.       <T>SLListNode* t = a->tl;
  571.       delete(a);
  572.       a = t;
  573.     }
  574.     else
  575.       b = b->tl;
  576.   }
  577. }
  578.  
  579. <T>OSet operator - (<T>OSet& x, <T>OSet& y)
  580. {
  581.   <T>SLListNode* a = x.P;
  582.   <T>SLListNode* b = y.P;
  583.   for (;;)
  584.   {
  585.     if (a == 0 || b == 0)
  586.       return <T>OSet();
  587.     int cmp = COMPARISON_FUNCTION(a->hd, b->hd);
  588.     if (cmp == 0)
  589.     {
  590.       a = a->tl;
  591.       b = b->tl;
  592.     }
  593.     else if (cmp < 0)
  594.       break;
  595.     else
  596.       b = b->tl;
  597.   }
  598.  
  599.   <T>SLListNode* h = new <T>SLListNode(a->hd);
  600.   <T>SLListNode* r = h;
  601.   a = a->tl;
  602.  
  603.   for (;;)
  604.   {
  605.     if (a == 0)
  606.     {
  607.       return <T>OSet(h);
  608.     }
  609.     else if (b == 0)
  610.     {
  611.       r->tl = copy<T>SLListNodes(a);
  612.       return <T>OSet(h);
  613.     }
  614.     int cmp = COMPARISON_FUNCTION(a->hd, b->hd);
  615.     if (cmp == 0)
  616.     {
  617.       a = a->tl;
  618.       b = b->tl;
  619.     }
  620.     else if (cmp < 0)
  621.     {
  622.       <T>SLListNode* n = new <T>SLListNode(a->hd);
  623.       r->tl = n;
  624.       r = n;
  625.       a = a->tl;
  626.     }
  627.     else
  628.       b = b->tl;
  629.   }
  630. }
  631.  
  632. <T>OSet operator ^ (<T>OSet& x, <T>OSet& y)
  633. {
  634.   <T>SLListNode* a = x.P;
  635.   <T>SLListNode* b = y.P;
  636.   <T>SLListNode* h;
  637.   for (;;)
  638.   {
  639.     if (a == 0)
  640.       return <T>OSet(y);
  641.     else if (b == 0)
  642.       return <T>OSet(a);
  643.     int cmp = COMPARISON_FUNCTION(a->hd, b->hd);
  644.     if (cmp == 0)
  645.     {
  646.       a = a->tl;
  647.       b = b->tl;
  648.     }
  649.     else if (cmp < 0)
  650.     {
  651.       h = new <T>SLListNode(a->hd);
  652.       a = a->tl;
  653.       break;
  654.     }
  655.     else 
  656.     {
  657.       h = new <T>SLListNode(b->hd);
  658.       b = b->tl;
  659.       break;
  660.     }
  661.   }
  662.  
  663.   <T>SLListNode* r = h;
  664.  
  665.   for (;;)
  666.   {
  667.     if (a == 0)
  668.     {
  669.       r->tl = copy<T>SLListNodes(b);
  670.       return <T>OSet(h);
  671.     }
  672.     else if (b == 0)
  673.     {
  674.       r->tl = copy<T>SLListNodes(a);
  675.       return <T>OSet(h);
  676.     }
  677.     int cmp = COMPARISON_FUNCTION(a->hd, b->hd);
  678.     if (cmp == 0)
  679.     {
  680.       a = a->tl;
  681.       b = b->tl;
  682.     }
  683.     else if (cmp < 0)
  684.     {
  685.       <T>SLListNode* n = new <T>SLListNode(a->hd);
  686.       r->tl = n;
  687.       r = n;
  688.       a = a->tl;
  689.     }
  690.     else
  691.     {
  692.       <T>SLListNode* n = new <T>SLListNode(b->hd);
  693.       r->tl = n;
  694.       r = n;
  695.       b = b->tl;
  696.     }
  697.   }
  698. }
  699.  
  700. <T>OSet& <T>OSet::operator -=(<T>OSet& y)
  701. {
  702.   <T>SLListNode* a = P;
  703.   <T>SLListNode* b = y.P;
  704.   <T>SLListNode* r;
  705.   for (;;)
  706.   {
  707.     if (a == 0 || b == 0)
  708.     {
  709.       P = a;
  710.       return *this;
  711.     }
  712.     int cmp = COMPARISON_FUNCTION(a->hd, b->hd);
  713.     if (cmp == 0)
  714.     {
  715.       <T>SLListNode* t = a->tl;
  716.       delete(a);
  717.       a = t;
  718.       b = b->tl;
  719.     }
  720.     else if (cmp < 0)
  721.     {
  722.       r = a;
  723.       a = a->tl;
  724.       break;
  725.     }
  726.     else
  727.       b = b->tl;
  728.   }
  729.   P = r;
  730.  
  731.   for (;;)
  732.   {
  733.     if (a == 0 || b == 0)
  734.     {
  735.       r->tl = a;
  736.       return *this;
  737.     }
  738.     int cmp = COMPARISON_FUNCTION(a->hd, b->hd);
  739.     if (cmp == 0)
  740.     {
  741.       <T>SLListNode* t = a->tl;
  742.       delete(a);
  743.       a = t;
  744.       b = b->tl;
  745.     }
  746.     else if (cmp < 0)
  747.     {
  748.       r->tl = a;
  749.       r = a;
  750.       a = a->tl;
  751.     }
  752.     else
  753.       b = b->tl;
  754.   }
  755. }
  756.  
  757. <T>OSet& <T>OSet::operator ^=(<T>OSet& y)
  758. {
  759.   <T>SLListNode* a = P;
  760.   <T>SLListNode* b = y.P;
  761.   <T>SLListNode* r;
  762.   for (;;)
  763.   {
  764.     if (a == 0)
  765.     {
  766.       P = copy<T>SLListNodes(b);
  767.       return *this;
  768.     }
  769.     else if (b == 0)
  770.     {
  771.       P = a;
  772.       return *this;
  773.     }
  774.     int cmp = COMPARISON_FUNCTION(a->hd, b->hd);
  775.     if (cmp == 0)
  776.     {
  777.       <T>SLListNode* t = a->tl;
  778.       delete(a);
  779.       a = t;
  780.       b = b->tl;
  781.     }
  782.     else if (cmp < 0)
  783.     {
  784.       r = a;
  785.       a = a->tl;
  786.       break;
  787.     }
  788.     else 
  789.     {
  790.       r = b;
  791.       b = b->tl;
  792.       break;
  793.     }
  794.   }
  795.   P = r;
  796.  
  797.   for (;;)
  798.   {
  799.     if (a == 0)
  800.     {
  801.       r->tl = copy<T>SLListNodes(b);
  802.       return *this;
  803.     }
  804.     else if (b == 0)
  805.     {
  806.       r->tl = a;
  807.       return *this;
  808.     }
  809.     int cmp = COMPARISON_FUNCTION(a->hd, b->hd);
  810.     if (cmp == 0)
  811.     {
  812.       a = a->tl;
  813.       b = b->tl;
  814.     }
  815.     else if (cmp < 0)
  816.     {
  817.       r->tl = a;
  818.       r = a;
  819.       a = a->tl;
  820.     }
  821.     else
  822.     {
  823.       <T>SLListNode* n = new <T>SLListNode(b->hd);
  824.       r->tl = n;
  825.       r = n;
  826.       b = b->tl;
  827.     }
  828.   }
  829. }
  830.