home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD2.mdf / c / library / dos / diverses / leda / incl / prio_m.h < prev    next >
Encoding:
C/C++ Source or Header  |  1991-11-15  |  5.9 KB  |  178 lines

  1. /*******************************************************************************
  2. +
  3. +  LEDA  2.1.1                                                 11-15-1991
  4. +
  5. +
  6. +  PRIO_M.h
  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.  
  16.  
  17. #ifndef PRIO_H
  18. #define PRIO_H
  19.  
  20.  
  21. #include <LEDA/f_heap.h>
  22.  
  23.  
  24. typedef f_heap_item pq_item;
  25.  
  26. #define prio(ktype,itype) name3(itype,ktype,prio)
  27.  
  28.  
  29. #define priodeclare2(ktype,itype)\
  30. \
  31. class prio(ktype,itype)  : public f_heap\
  32. {\
  33.   int  cmp(ent& x, ent& y) const { return compare(*(itype*)&x,*(itype*)&y); }\
  34.   void print_key(ent& x)   const { Print(*(itype*)&x); }\
  35.   void print_inf(ent& x)   const { Print(*(ktype*)&x); }\
  36.   void clear_key(ent& x)   const { Clear(*(itype*)&x); }\
  37.   void clear_inf(ent& x)   const { Clear(*(ktype*)&x); }\
  38.   void copy_key(ent& x)    const { Copy(*(itype*)&x); }\
  39.   void copy_inf(ent& x)    const { Copy(*(ktype*)&x); }\
  40. \
  41. public:\
  42. \
  43. virtual pq_item insert(ktype k,itype i) { return f_heap::insert(Ent(i),Ent(k));}\
  44. virtual pq_item find_min()        const { return f_heap::find_min();}\
  45. virtual ktype   del_min()   { ktype x = key(find_min()); f_heap::del_min(); return x; }\
  46. virtual ktype key(pq_item x)      const { return ktype(f_heap::inf(x)); }\
  47. virtual itype inf(pq_item x)      const { return itype(f_heap::key(x)); }\
  48. virtual void change_key(pq_item x, ktype k) { f_heap::change_inf(x,Ent(k)); }\
  49. virtual void decrease_inf(pq_item x,itype i){ f_heap::decrease_key(x,Ent(i)); }\
  50. virtual void del_item(pq_item x)            { f_heap::del_item(x); }\
  51. \
  52. virtual bool empty() const { return f_heap::empty(); }\
  53. virtual int  size()  const { return f_heap::size(); }\
  54. \
  55. virtual pq_item first_item()          const { return f_heap::first_item(); }\
  56. virtual pq_item next_item(pq_item it) const { return f_heap::next_item(it); }\
  57. \
  58. virtual prio(ktype,itype)& operator=(const prio(ktype,itype)& Q) { return (prio(ktype,itype)&)f_heap::operator=((f_heap&)Q); }\
  59. \
  60.  prio(ktype,itype)()  {}\
  61.  prio(ktype,itype)(const prio(ktype,itype)& Q) : f_heap((f_heap&)Q)  {}\
  62. ~prio(ktype,itype)()  { f_heap::clear(); }\
  63. };
  64.  
  65. #define forall_pq_items(i,Q) for(i = (Q).first_item(); i; i=(Q).next_item(i))
  66.  
  67.  
  68.  
  69. #define PRIO_ITEM(T) name2(T,_item)
  70.  
  71. #define PRIO(ktype,itype,impl) name4(itype,ktype,impl,PRIO)
  72.  
  73.  
  74. #define PRIOdeclare3(ktype,itype,impl)\
  75. \
  76. class PRIO(ktype,itype,impl) : public prio(ktype,itype), public impl\
  77. {\
  78. int  cmp(ent& x, ent& y) const { int b =  compare(*(itype*)x,*(itype*)y);\
  79.                                  return (b==0) ? int(x)-int(y) : b;}\
  80. \
  81. void print_key(ent& x)   const { Print(*(itype*)x);  }\
  82. void print_inf(ent& x)   const { Print(*(ktype*)&x); }\
  83. void clear_key(ent&)     const {}\
  84. void clear_inf(ent& x)   const { Clear(*(ktype*)&x); }\
  85. void copy_key(ent&)      const {}\
  86. void copy_inf(ent& x)    const { Copy(*(ktype*)&x); }\
  87. \
  88. public:\
  89. \
  90. pq_item insert(ktype k,itype i) { itype* p=new itype; *p=i; Copy(*p);\
  91.                                   return pq_item(impl::insert(Ent(p),Ent(k)));}\
  92. \
  93. pq_item find_min()   const { return pq_item(impl::find_min());}\
  94. \
  95. ktype del_min()            { pq_item it = find_min(); ktype x = key(it);\
  96.                              del_item(it); return x; }\
  97. \
  98. ktype key(pq_item x) const { return ktype(impl::inf(PRIO_ITEM(impl)(x))); }\
  99. itype inf(pq_item x) const { return *(itype*)impl::key(PRIO_ITEM(impl)(x));}\
  100. \
  101. void  change_key(pq_item x, ktype k)\
  102.                            { impl::change_inf(PRIO_ITEM(impl)(x),Ent(k)); }\
  103. \
  104. void  decrease_inf(pq_item x,itype i)\
  105.                            { itype* p=new itype; *p=i; Copy(*p);\
  106.                              impl::decrease_key(PRIO_ITEM(impl)(x),Ent(p));}\
  107. \
  108. void  del_item(pq_item x)  { delete (itype*)impl::key(PRIO_ITEM(impl)(x));\
  109.                              impl::del_item(PRIO_ITEM(impl)(x)); }\
  110. \
  111. bool  empty() const        { return impl::empty(); }\
  112. int   size()  const        { return impl::size(); }\
  113. \
  114. pq_item first_item()          const { return pq_item(impl::first_item()); }\
  115. pq_item next_item(pq_item it) const { return pq_item(impl::next_item(PRIO_ITEM(impl)(it))); }\
  116. \
  117. prio(ktype,itype)& operator=(const prio(ktype,itype)& Q)\
  118. {  return (PRIO(ktype,itype,impl)&)impl::operator=(*(impl*)&Q); }\
  119. \
  120. PRIO(ktype,itype,impl)(const PRIO(ktype,itype,impl)& Q):impl((impl&)Q)  {}\
  121. \
  122. PRIO(ktype,itype,impl)()  {}\
  123. \
  124. ~PRIO(ktype,itype,impl)()  { impl::clear(); }\
  125. };
  126.  
  127.  
  128.  
  129. //------------------------------------------------------------------------------
  130. // Bounded Priority Queues
  131. //------------------------------------------------------------------------------
  132.  
  133.  
  134. #define B_PRIO_ITEM(T) name2(T,_item)
  135.  
  136. #define B_PRIO(ktype,impl) name3(ktype,impl,B_PRIO)
  137.  
  138.  
  139. #define B_PRIOdeclare2(ktype,impl)\
  140. \
  141. class B_PRIO(ktype,impl) : public prio(ktype,int), public impl\
  142. {\
  143. int  cmp(ent& x, ent& y) const { return int(x) - int(y); }\
  144. void print_inf(ent& x)   const { Print(int(x)); }\
  145. void print_key(ent& x)   const { Print(*(ktype*)&x);}\
  146. void clear_key(ent& x)   const { Clear(*(ktype*)&x);}\
  147. void copy_key(ent& x)    const { Copy(*(ktype*)&x); }\
  148. \
  149. public:\
  150. \
  151. pq_item insert(ktype k,int i) { return pq_item(impl::insert(Ent(k),i));}\
  152. \
  153. pq_item find_min()   const { return pq_item(impl::find_min());}\
  154. \
  155. ktype del_min()            { return ktype(impl::del_min()); }\
  156. \
  157. ktype key(pq_item x) const { return ktype(impl::key(B_PRIO_ITEM(impl)(x))); }\
  158. int   inf(pq_item x) const { return impl::inf(B_PRIO_ITEM(impl)(x));}\
  159. \
  160. void  change_key(pq_item x, ktype k)\
  161.                            { impl::change_key(B_PRIO_ITEM(impl)(x),Ent(k)); }\
  162. \
  163. void  decrease_inf(pq_item x, int i)\
  164.                            { impl::decrease_inf(B_PRIO_ITEM(impl)(x),i);}\
  165. \
  166. void  del_item(pq_item x)  { impl::del_item(B_PRIO_ITEM(impl)(x)); }\
  167. \
  168. bool  empty() const        { return impl::empty(); }\
  169. int   size()  const        { return impl::size(); }\
  170. \
  171.  B_PRIO(ktype,impl)(int N) : impl(N) {}\
  172. ~B_PRIO(ktype,impl)()                { impl::clear(); }\
  173. };
  174.  
  175.  
  176. #endif
  177.