home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD2.mdf / c / library / dos / diverses / leda / src / basic / _k_heap.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-11-15  |  3.1 KB  |  184 lines

  1. /*******************************************************************************
  2. +
  3. +  LEDA  2.1.1                                                 11-15-1991
  4. +
  5. +
  6. +  _k_heap.c
  7. +
  8. +
  9. +  Copyright (c) 1991  by  Max-Planck-Institut fuer Informatik
  10. +  Im Stadtwald, 6600 Saarbruecken, FRG     
  11. +  All rights reserved.
  12. *******************************************************************************/
  13.  
  14.  
  15. #include <LEDA/k_heap.h>
  16.  
  17. #define SON1(i)   (K*i+1)
  18. #define PARENT(i) ((i-1)/K)
  19.  
  20.  
  21. K_HEAP::K_HEAP(int n, int k)  
  22.   K = k;
  23.   Key = new ent[n]; 
  24.   Inf = new ent[n]; 
  25.   Item = new int[n];
  26.   Pos = new int[n+1];
  27.   for (int i=1;i<=n;i++) free_items.append(Ent(i));
  28.   max_count = n; count = 0; 
  29. }
  30.  
  31. K_HEAP::~K_HEAP()  
  32.   delete Key; 
  33.   delete Inf;
  34.   delete Item;
  35.   delete Pos;
  36.   free_items.clear();
  37. }
  38.  
  39. void K_HEAP::clear()
  40. { count = 0;
  41.   free_items.clear();
  42.   for (int i=1;i<=max_count;i++) free_items.append(Ent(i));
  43. }
  44.  
  45. void  K_HEAP::rise(int& pos, ent k, ent i)
  46. {
  47.   register int parent = PARENT(pos);
  48.  
  49.   while (pos > 0 && cmp(Inf[parent],i) > 0 )
  50.     { Inf[pos] = Inf[parent];
  51.       Key[pos] = Key[parent];
  52.       Item[pos] = Item[parent];
  53.       Pos[Item[pos]] = pos;
  54.       pos = parent;
  55.       parent = PARENT(pos);
  56.      }
  57.  
  58.   Inf[pos] = i;
  59.   Key[pos] = k;
  60.  
  61. }
  62.  
  63. void K_HEAP::sink(int& current, ent k, ent i)
  64. {
  65.   register int child   = SON1(current);
  66.  
  67.   while (child < count)
  68.   { 
  69.     int r = Min(count,child+K);
  70.     for (int j=child+1;j<r;j++)
  71.       if (cmp(Inf[j],Inf[child])<0 ) child = j;
  72.  
  73.     if (cmp(i,Inf[child])>0) 
  74.      { Inf[current] = Inf[child];
  75.        Key[current] = Key[child];
  76.        Item[current] = Item[child];
  77.        Pos[Item[current]] = current;
  78.        current = child;
  79.        child = SON1(current);
  80.       }
  81.     else  break;
  82.    }
  83.  
  84.   Inf[current] = i;
  85.   Key[current] = k;
  86. }
  87.  
  88.  
  89. void K_HEAP::decrease_inf(int it, ent i)
  90. {
  91.   int pos = Pos[it];
  92.   if (cmp(Inf[pos],i)<0) 
  93.   { cout << string ("INF = %d  i = %d\n",Inf[pos],i);
  94.     cout.flush();
  95.     error_handler(1,"k-heap: inf too large in decrease_inf");
  96.   }
  97.  
  98.   rise(pos,Key[pos],i);
  99.   Pos[it] = pos;
  100.   Item[pos] = it;
  101. }
  102.  
  103.  
  104. int K_HEAP::insert(ent k, ent i) 
  105. {
  106.   if (free_items.empty()) error_handler(1,"k_heap: overflow");
  107.  
  108.   int new_it = (int)free_items.head();
  109.   free_items.pop();
  110.  
  111.   int p = count++;
  112.  
  113.   rise(p,k,i);
  114.  
  115.   Pos[new_it] = p;
  116.   Item[p] = new_it;
  117.  
  118.   return new_it;
  119. }
  120.  
  121. int K_HEAP::insert0(ent k, ent i) 
  122. {
  123.   if (free_items.empty()) error_handler(1,"k_heap: overflow");
  124.  
  125.   int new_it = (int)free_items.head();
  126.   free_items.pop();
  127.  
  128.   Key[count] = k;
  129.   Inf[count] = i;
  130.   Item[count] = new_it;
  131.   Pos[new_it] = count;
  132.   count++;
  133.  
  134.   return new_it;
  135. }
  136.  
  137.  
  138.  
  139. ent K_HEAP::del_item(int it)
  140. {
  141.   int p = Pos[it];
  142.  
  143.   ent result = Key[p];
  144.  
  145.   count--;
  146.  
  147.   free_items.append(Ent(it));
  148.  
  149.   sink(p,Key[count],Inf[count]);
  150.  
  151.   Item[p] = Item[count];
  152.   Pos[Item[p]]= p;
  153.  
  154.   return result;
  155. }
  156.  
  157. void K_HEAP::print()
  158.   int i;
  159.  
  160.   for(i=0;i<count;i++) 
  161.     { print_key(Key[i]);
  162.       cout << "-";
  163.       print_inf(Inf[i]);
  164.       cout << "  ";
  165.      }
  166.   newline;
  167.  
  168.  
  169.   cout << "ITEM: ";
  170.   for(i=0;i<count;i++) cout << string(" %3d",Item[i]);
  171.   newline;
  172.  
  173.   cout << "POS:  ";
  174.   for(i=0;i<count;i++) cout << string(" %3d",Pos[Item[i]]);
  175.   newline;
  176.  
  177.   newline;
  178.  
  179. }
  180.