home *** CD-ROM | disk | FTP | other *** search
/ NeXTSTEP 3.2 (Developer) / NS_dev_3.2.iso / NextDeveloper / Headers / g++ / gen / XPPQ.ccP < prev    next >
Text File  |  1993-06-29  |  3KB  |  144 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. #ifdef __GNUG__
  20. #pragma implementation
  21. #endif
  22. #include "<T>.XPPQ.h"
  23.  
  24. int <T>XPPQ::OK()
  25. {
  26.   int v = p.OK();
  27.   v &= p.low() == 1;
  28.   v &= count == p.length();
  29.   if (!v) error("invariant failure");
  30.   return v;
  31. }
  32.  
  33. Pix <T>XPPQ::seek(<T&> item)
  34. {
  35.   for (int i = p.low(); i < p.fence(); p.next(i))
  36.     if (<T>EQ(p[i],item)) return p.index_to_Pix(i);
  37.   return 0;
  38. }
  39.  
  40. // standard 2-ary heap ops
  41. // pointers are used a lot to avoid thrashing across chunks with plexes
  42.  
  43. Pix <T>XPPQ::enq(<T&> item)
  44. {
  45.   p.add_high(item);
  46.   <T>* pk = &(p.high_element());
  47.   int par = ++count >> 1;
  48.   while (par != 0)
  49.   {
  50.     <T>* ppar = &(p[par]);
  51.     if (!(<T>LE(*ppar, item)))
  52.     {
  53.       *pk = *ppar;
  54.       pk = ppar;
  55.       par >>= 1;
  56.     }
  57.     else
  58.       break;
  59.   }
  60.   *pk = item;
  61.   return Pix(pk);
  62. }
  63.  
  64. void <T>XPPQ::del_front()
  65. {
  66.   if (count == 0) error("empty PQ");
  67.   --count;
  68.   <T>* pk = &(p.low_element());
  69.   <T>* ph = &(p.high_element());
  70.   int child = 2;
  71.   while (child <= count)
  72.   {
  73.     <T>* pchild = &(p[child]);
  74.     if (child < count)
  75.     {
  76.       <T>* prchild = &(p[child+1]);
  77.       if (!(<T>LE(*pchild, *prchild)))
  78.       {
  79.         pchild = prchild;
  80.         ++child;
  81.       }
  82.     }
  83.     if (!(<T>LE(*ph, *pchild)))
  84.     {
  85.       *pk = *pchild;
  86.       pk = pchild;
  87.       child <<= 1;
  88.     }
  89.     else
  90.       break;
  91.   }
  92.   *pk = *ph;
  93.   p.del_high();
  94. }
  95.  
  96.  
  97. void <T>XPPQ::del(Pix i)
  98. {
  99.   if (i == 0) error("null Pix");
  100.   --count;
  101.   int k = p.Pix_to_index(i);
  102.   <T>* pk = &(p[k]);
  103.   <T>* ph = &(p.high_element());
  104.   int child = k << 1;
  105.   while (child <= count)
  106.   {
  107.     <T>* pchild = &(p[child]);
  108.     if (child < count)
  109.     {
  110.       <T>* prchild = &(p[child+1]);
  111.       if (!(<T>LE(*pchild, *prchild)))
  112.       {
  113.         pchild = prchild;
  114.         ++child;
  115.       }
  116.     }
  117.     if (!(<T>LE(*ph, *pchild)))
  118.     {
  119.       *pk = *pchild;
  120.       pk = pchild;
  121.       child <<= 1;
  122.     }
  123.     else
  124.       break;
  125.   }
  126.   int par = child >> 2;
  127.   while (par != 0)
  128.   {
  129.     <T>* ppar = &(p[par]);
  130.     if (!(<T>LE(*ppar, *ph)))
  131.     {
  132.       *pk = *ppar;
  133.       pk = ppar;
  134.       par >>= 1;
  135.     }
  136.     else
  137.       break;
  138.   }
  139.   *pk = *ph;
  140.   p.del_high();
  141. }
  142.  
  143.  
  144.