home *** CD-ROM | disk | FTP | other *** search
/ GEMini Atari / GEMini_Atari_CD-ROM_Walnut_Creek_December_1993.iso / files / gnu / g__lib / phpq.ccp < prev    next >
Text File  |  1993-07-23  |  7KB  |  340 lines

  1. // This may look like C code, but it is really -*- C++ -*-
  2. /* 
  3. Copyright (C) 1988 Free Software Foundation
  4.     written by Dirk Grunwald (grunwald@cs.uiuc.edu)
  5.     adapted for libg++ by Doug Lea (dl@rocky.oswego.edu)
  6.  
  7. This file is part of GNU CC.
  8.  
  9. GNU CC is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY.  No author or distributor
  11. accepts responsibility to anyone for the consequences of using it
  12. or for whether it serves any particular purpose or works at all,
  13. unless he says so in writing.  Refer to the GNU CC General Public
  14. License for full details.
  15.  
  16. Everyone is granted permission to copy, modify and redistribute
  17. GNU CC, but only under the conditions described in the
  18. GNU CC General Public License.   A copy of this license is
  19. supposed to have been given to you along with GNU CC so you
  20. can know your rights and responsibilities.  It should be in a
  21. file named COPYING.  Among other things, the copyright notice
  22. and this notice must be preserved on all copies.  
  23. */
  24.  
  25.  
  26. #include <values.h>
  27. #include "<T>.PHPQ.h"
  28.  
  29. //
  30. //    This defines a Pairing Heap structure
  31. //
  32. //    See ``The Pairing Heap: A New Form of Self-Adjusting Heap''
  33. //    Fredman, Segdewick et al,
  34. //    Algorithmica (1986) 1:111-129
  35. //
  36. //    In particular, this implements the pairing heap using the circular
  37. //    list.
  38. //
  39. //
  40.  
  41. <T>PHPQ::<T>PHPQ(int sz = DEFAULT_INITIAL_CAPACITY)
  42. {
  43.   storage = 0;
  44.   root = 0;
  45.   count = 0;
  46.   size = 0;
  47.   prealloc(sz);
  48. }
  49.  
  50. <T>PHPQ::<T>PHPQ(const <T>PHPQ& a)
  51. {
  52.   storage = 0;
  53.   root  = 0;
  54.   count = 0;
  55.   size = 0;
  56.   prealloc(a.size);
  57.   for (Pix i = a.first(); i != 0; a.next(i)) enq(a(i));
  58. }
  59.  
  60.  
  61. void <T>PHPQ::prealloc(int newsize)
  62. {
  63.   ++newsize; // leave a spot for freelist
  64.   if (size != 0)
  65.   {
  66.     int news = size;
  67.     while (news <= newsize) news = (news * 3) / 2;
  68.     newsize = news;
  69.   }
  70.   // see if indices are OK
  71.   <T>PHPQNode test;
  72.   test.sibling = 0;
  73.   test.sibling = ~test.sibling;
  74.   if ((unsigned long)newsize > (unsigned long)(test.sibling))
  75.     error("storage size exceeds index range");
  76.  
  77.   if (storage == 0)
  78.   {
  79.     storage = new <T>PHPQNode[size = newsize];
  80.     for (int i = 0; i < size; ++i) 
  81.     {
  82.       storage[i].sibling = i + 1;
  83.       storage[i].valid = 0;
  84.     }
  85.     storage[size-1].sibling = 0;
  86.   }
  87.   else
  88.   {
  89.     <T>PHPQNode* newstor = new <T>PHPQNode[newsize];
  90.     for (int i = 1; i < size; ++i)
  91.       newstor[i] = storage[i];
  92.     delete storage;
  93.     storage = newstor;
  94.     for (i = size; i < newsize; ++i) 
  95.     {
  96.       storage[i].sibling = i + 1;
  97.       storage[i].valid = 0;
  98.     }
  99.     storage[newsize-1].sibling = 0;
  100.     storage[0].sibling = size;
  101.     size = newsize;
  102.   }
  103. }
  104.  
  105.  
  106. void <T>PHPQ::clear()
  107. {
  108.   for (int i = 0; i < size; ++i) 
  109.   {
  110.     storage[i].sibling = i + 1;
  111.     storage[i].valid = 0;
  112.   }
  113.   storage[size-1].sibling = 0;
  114.   root = 0;
  115.   count = 0;
  116. }
  117.  
  118. Pix <T>PHPQ::enq(<T&> item)
  119. {
  120.   ++count;
  121.   if (storage[0].sibling == 0)
  122.     prealloc(count);
  123.  
  124.   int cell = storage[0].sibling;
  125.   storage[0].sibling = storage[cell].sibling;
  126.   storage[cell].sibling = 0;
  127.   storage[cell].children = 0;
  128.   storage[cell].item = item;
  129.   storage[cell].valid = 1;
  130.   
  131.   if (root == 0) 
  132.   {
  133.     root = cell;
  134.     return Pix(root);
  135.   }
  136.   else 
  137.   {
  138.     int parent;
  139.     int child;
  140.     
  141.     if (<T>LE(storage[root].item, storage[cell].item))
  142.     {
  143.       parent = root; child = cell;
  144.     } 
  145.     else 
  146.     {
  147.       parent = cell; child = root;
  148.     }
  149.     int popsKid = storage[parent].children;
  150.     
  151.     if (popsKid == 0) 
  152.     {
  153.       storage[parent].children = child;
  154.       storage[child].sibling = child;
  155.     } 
  156.     else 
  157.     {
  158.       int temp = storage[popsKid].sibling;
  159.       storage[popsKid].sibling = child;
  160.       storage[child].sibling = temp;
  161.       storage[parent].children = child;
  162.     }
  163.     root = parent;
  164.     return Pix(cell);
  165.   }
  166. }
  167.  
  168. //
  169. //    Item removal is the most complicated routine.
  170. //
  171. //    We remove the root (should there be one) and then select a new
  172. //    root. The siblings of the root are in a circular list. We continue
  173. //    to pair elements in this list until there is a single element.
  174. //    This element will be the new root.
  175.  
  176. void <T>PHPQ::del_front()
  177. {
  178.   int valid = 0;
  179.   do 
  180.   {
  181.     if (root == 0) return;
  182.     if (valid = storage[root].valid)
  183.       --count;
  184.     storage[root].valid = 0;
  185.     int child = storage[root].children;
  186.     storage[root].sibling = storage[0].sibling;
  187.     storage[0].sibling = root;
  188.  
  189.     if (child == 0)
  190.     {
  191.       root = 0;
  192.       return;
  193.     }
  194.     else 
  195.     {
  196.       while(storage[child].sibling != child) 
  197.       {
  198.         // We have at least two kids, but we may only have
  199.         // two kids. So, oneChild != child, but it is possible
  200.         // that twoChild == child.
  201.         
  202.         int oneChild = storage[child].sibling;
  203.         int twoChild = storage[oneChild].sibling;
  204.  
  205.         // Remove the two from the sibling list
  206.  
  207.         storage[child].sibling = storage[twoChild].sibling;
  208.         storage[oneChild].sibling = 0;
  209.         storage[twoChild].sibling = 0;
  210.         
  211.         int bestChild;
  212.         int worstChild;
  213.     
  214.         if (<T>LE(storage[oneChild].item, storage[twoChild].item))
  215.         {
  216.           bestChild = oneChild; worstChild = twoChild;
  217.         } 
  218.         else 
  219.         {
  220.           bestChild = twoChild; worstChild = oneChild;
  221.         }
  222.         int popsKid = storage[bestChild].children;
  223.         
  224.         if (popsKid == 0) 
  225.         {
  226.           storage[bestChild].children = worstChild;
  227.           storage[worstChild].sibling = worstChild;
  228.         } 
  229.         else 
  230.         {
  231.           int temp = storage[popsKid].sibling;
  232.           storage[popsKid].sibling = worstChild;
  233.           storage[worstChild].sibling = temp;
  234.           storage[bestChild].children = worstChild;
  235.         }
  236.         if (twoChild == child) 
  237.         {
  238.           // We have reduced the two to one, so we'll be exiting.
  239.           child = bestChild;
  240.           storage[child].sibling = child;
  241.         } 
  242.         else 
  243.         {
  244.           // We've removed two siblings, now we need to insert
  245.           // the better of the two
  246.           storage[bestChild].sibling = storage[child].sibling;
  247.           storage[child].sibling = bestChild;
  248.           child = storage[bestChild].sibling;
  249.         }
  250.       }
  251.       root = child;
  252.     }
  253.   } while ( !valid );
  254. }
  255.  
  256. void <T>PHPQ::del(Pix p) 
  257. {
  258.   if (p == 0) error("null Pix");
  259.   int i = int(p);
  260.   if (storage[i].valid)
  261.   {
  262.     if (i == root)
  263.       del_front();
  264.     else
  265.     {
  266.       storage[i].valid = 0;
  267.       --count;
  268.     }
  269.   }
  270. }
  271.  
  272.  
  273. Pix <T>PHPQ::seek(<T&> key)
  274. {
  275.   for (int i = 1; i < size; ++i)
  276.     if (storage[i].valid && <T>EQ(storage[i].item, key))
  277.       return Pix(i);
  278.   return 0;
  279. }
  280.  
  281. Pix <T>PHPQ::first()
  282. {
  283.   for (int i = 1; i < size; ++i)
  284.     if (storage[i].valid)
  285.       return Pix(i);
  286.   return 0;
  287. }
  288.  
  289.  
  290. void <T>PHPQ::next(Pix& p)
  291. {
  292.   if (p == 0) return;
  293.   for (int i = int(p)+1; i < size; ++i)
  294.     if (storage[i].valid)
  295.     {
  296.       p = Pix(i); 
  297.       return;
  298.     }
  299.   p = 0;
  300. }
  301.  
  302. int <T>PHPQ::OK()
  303. {
  304.   int v = storage != 0;
  305.   int n = check_sibling_list(root, 0);
  306.   v &= n  == count;
  307.   int ct = MAXLONG;
  308.   n = 0;
  309.   int f = storage[0].sibling;
  310.   while (f != 0 && ct-- > 0)
  311.   {
  312.     f = storage[f].sibling;
  313.     ++n;
  314.   }
  315.   v &= ct > 0;
  316.   v &= n <= size - count;
  317.   if (!v) error("invariant failure");
  318.   return v;
  319. }
  320.  
  321.  
  322. int <T>PHPQ::check_sibling_list(int t, int cnt)
  323. {
  324.   if (t != 0)
  325.   {
  326.     int s = t;
  327.     long ct = MAXLONG;      // Lots of chances to find self!
  328.     do 
  329.     {
  330.       if (storage[s].valid) cnt++;
  331.       cnt += check_sibling_list(storage[s].children, cnt);
  332.       s = storage[s].sibling;
  333.     } while (ct-- > 0 && s != t && s != 0);
  334.     if (ct <= 0) return -1;
  335.   }
  336.   return cnt;
  337. }
  338.  
  339.  
  340.