eb_tree.h
上传用户:gzelex
上传日期:2007-01-07
资源大小:707k
文件大小:5k
开发平台:

MultiPlatform

  1. /*******************************************************************************
  2. +
  3. +  LEDA-R  3.2.3
  4. +
  5. +  eb_tree.h
  6. +
  7. +  Copyright (c) 1995  by  Max-Planck-Institut fuer Informatik
  8. +  Im Stadtwald, 66123 Saarbruecken, Germany     
  9. +  All rights reserved.
  10. *******************************************************************************/
  11. #ifndef LEDA_STRATIFIED_H
  12. #define LEDA_STRATIFIED_H
  13. //------------------------------------------------------------------------------
  14. //  stratified tree  (van emde boas tree)
  15. // 
  16. //  Stefan Naeher (1989)
  17. //
  18. //  Modification History:
  19. //
  20. //  - Use of bounded dictionaries instead of arrays
  21. //    ( bounded dictionaries are implemented by
  22. //      dynamic perfect hashing )
  23. //
  24. //      Michael Wenzel (1990)
  25. //
  26. //
  27. //  - Dynamization
  28. //
  29. //      Michael Wenzel (1990)
  30. //
  31. //
  32. //  - Function pred
  33. //
  34. //      Michael Wenzel (1990)
  35. //
  36. //
  37. //  - Minimum and maximum not recursively stored
  38. //
  39. //      Michael Wenzel (1991)
  40. //
  41. //
  42. //  - Class l_stratified for bot-structures
  43. //      with <= 2 Elements
  44. //
  45. //      Michael Wenzel (1991)
  46. //
  47. //
  48. //  - Union b_dict for dictionary
  49. //    ( Union of bounded dictionary and array )
  50. //
  51. //      Michael Wenzel (1991)
  52. //
  53. //
  54. //------------------------------------------------------------------------------
  55. //-----------------------------------------------------------------------------
  56. // Include & Makros
  57. //-----------------------------------------------------------------------------
  58. #include <LEDA/impl/dp_hash.h>   
  59. #define pot_2(x)          ( 1 << x )
  60. #define mal_pot_2(x,y)    ( (x) << (y) )
  61. #define down(k)           ( (k) >> 1 )
  62. #define up(k)             ( down(k) + ((k) & 1) )
  63. #define high_bits(x)      ( x >> down(k) ) 
  64. #define low_bits(x)       ( x & (pot_2(down(k))-1) ) 
  65. //-----------------------------------------------------------------------------
  66. // definitions
  67. //-----------------------------------------------------------------------------
  68. class l_stratified;
  69. class stratified;
  70. typedef l_stratified* l_stratified_ptr;
  71. typedef stratified* stratified_ptr;
  72. //-----------------------------------------------------------------------------
  73. // class definition 
  74. //-----------------------------------------------------------------------------
  75.                                // union for dictionary
  76. class b_dict {
  77.   union {
  78.            GenPtr*         l;       // array
  79.            dp_hash*      d;       // bounded dictionary;
  80.         };
  81. void insert(int x, GenPtr y, int k)
  82.    {
  83.      if ( k <= 8 )  l[x] = y;        
  84.      else           d->insert(Convert(x),y);  
  85.                            }
  86. void del(int x, int k)     {
  87.                              if ( k <= 8 )  l[x] = 0; 
  88.      else           d->del(Convert(x));
  89.                            }
  90. GenPtr& lookup(int x, int k)  {
  91.      if ( k <= 8 )  
  92.        return l[x];
  93.      else     
  94.      { stp p = d->lookup(Convert(x));
  95.        return d->info(p);
  96.                              }
  97.                            }
  98.  b_dict(int);
  99. ~b_dict() {}
  100. void clear(int);
  101. LEDA_MEMORY(b_dict)
  102. friend class stratified;
  103. };
  104. typedef b_dict* b_dict_ptr;
  105.                                // class for <= 2 elements
  106. class l_stratified {
  107. int          mi;      // minimum
  108. int          ma;      // maximum
  109. int size()                {
  110.     if ( ma == -1 ) return 0 ;
  111.     else if ( ma == mi ) return 1 ;
  112.             else if ( ma <  mi ) return 2 ;
  113.             else return 3;   // illegal, pointer was a stratified_ptr
  114.           }
  115. int insert(int x)         { if ( size() == 0 )   { mi = ma = x; return 1; }
  116.     else if ( x < ma )   { ma = x; return 1; }
  117.     else if ( x > mi )   { mi = x; return 1; }
  118.     else return 0;
  119.   } 
  120. int del(int x)            { if (size() == 1 && x == mi) { mi=ma=-1; return 1; }
  121.     else if ( x == mi )   { mi = ma; return 1; }
  122.     else if ( x == ma )   { ma = mi; return 1; }
  123.     else return 0;
  124.   } 
  125. int member(int x)         {
  126.     return ( x == mi ) || ( x == ma );
  127.                           }
  128. int max()                 { if ( size() <= 2 )
  129.       return mi;
  130.     else
  131.       return ma;
  132.   }
  133. int min()                 { if ( size() <= 2 )
  134.       return ma; 
  135.     else
  136.       return mi;
  137.   }
  138. int succ(int x)           { if ( x < ma ) return ma;
  139.     else if ( x < mi ) return mi;
  140.     else return -1;
  141.                           }
  142. int pred(int x)           { if ( x > mi ) return mi;
  143.     else if ( x > ma ) return ma;
  144.     else return -1;
  145.                           }
  146. l_stratified(int x=-1)    { mi = ma = x; }
  147. l_stratified(stratified*, int);
  148. LEDA_MEMORY(l_stratified)
  149. friend class stratified;
  150. friend class b_dict;
  151. };
  152.                                // recursive structure    
  153. class stratified : public l_stratified { 
  154. int            k;       // k-structure
  155. int            sz;      // size 
  156. l_stratified*  top;     // up(k)-structure
  157. b_dict_ptr     bot;     // pointer to bounded Dictionary of (k/2)-structures
  158. public:
  159.  stratified(l_stratified*, int);
  160.  stratified(int);
  161. ~stratified();
  162. int  min()        { return mi; }
  163. int  max()        { return ma; }
  164. int  size()       { return sz; }
  165. int  min2();
  166. int  max2();
  167. int  member(int);
  168. int  succ(int);
  169. int  pred(int);
  170. int insert(int);
  171. int del(int);
  172. void print();
  173. LEDA_MEMORY(stratified)
  174. };
  175. typedef stratified eb_tree;
  176. #endif