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

MultiPlatform

  1. /*******************************************************************************
  2. +
  3. +  LEDA-R  3.2.3
  4. +
  5. +  p_dictionary.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_P_DICTIONARY_H
  12. #define LEDA_P_DICTIONARY_H
  13. #include <LEDA/impl/pers_tree.h>
  14. typedef pers_tree_node* p_dic_item;
  15. template <class K,class I>
  16. class PERS_DIC: public pers_rb_tree, public handle_rep {
  17. void copy_key(GenPtr& x)   { LEDA_COPY(K,x); }
  18. void copy_inf(GenPtr& x)   { LEDA_COPY(I,x); }
  19. void clear_key(GenPtr& x)  { LEDA_CLEAR(K,x); }
  20. void clear_inf(GenPtr& x)  { LEDA_CLEAR(I,x); }
  21. void print_key(GenPtr x)   { LEDA_PRINT(K,x,cout); }
  22. void print_inf(GenPtr x)   { LEDA_PRINT(I,x,cout); }
  23. int  cmp_keys(GenPtr x, GenPtr y) { return LEDA_COMPARE(K,x,y); }
  24. Version V;
  25. public:
  26.  PERS_DIC() { init_tree(); V = v_list->vl.first(); }
  27.  PERS_DIC(V_LIST* vl,Version v) { v_list=vl; V=v;  }
  28.  void CLEAR() { if (--v_list->count==0) del_tree(); }
  29. ~PERS_DIC() { CLEAR(); }
  30. PERS_DIC(const PERS_DIC<K,I>& D)
  31. { v_list = D.v_list; v_list->count++; V = D.V; count = D.count; }
  32. PERS_DIC<K,I>& operator=(PERS_DIC<K,I>& D)
  33. { CLEAR(); v_list = D.v_list; v_list->count++; V = D.V; count = D.count;
  34.   return *this; }
  35. K  key(p_dic_item p) { return LEDA_ACCESS(K,pers_rb_tree::key(p)); }
  36. I inf(p_dic_item p)  { return LEDA_ACCESS(I,pers_rb_tree::inf(p)); }
  37. p_dic_item locate(K k) { return pers_rb_tree::locate(Convert(k),V); }
  38. p_dic_item locate_pred(K k) { return pers_rb_tree::locate_pred(Convert(k),V); }
  39. p_dic_item lookup(K k) { return pers_rb_tree::lookup(Convert(k),V); }
  40. PERS_DIC<K,I>  insert(K k, I i)
  41. { return PERS_DIC<K,I>(v_list,pers_rb_tree::insert(Convert(k),Convert(i),V)); }
  42. PERS_DIC<K,I>  del(K k)
  43. { return PERS_DIC<K,I>(v_list,pers_rb_tree::del(Convert(k),V)); }
  44. PERS_DIC<K,I>  change_inf(p_dic_item p, I i)
  45. { return PERS_DIC<K,I>(v_list,pers_rb_tree::change_inf(p,Convert(i),V)); }
  46. p_dic_item min()          { return pers_rb_tree::min(V); }
  47. p_dic_item max()          { return pers_rb_tree::max(V); }
  48. p_dic_item succ(p_dic_item p)  { return pers_rb_tree::succ(p,V); }
  49. p_dic_item pred(p_dic_item p)  { return pers_rb_tree::pred(p,V); }
  50. int   size()         { return pers_rb_tree::size(V); }
  51. void  print()        { pers_rb_tree::print(V); }
  52. void  draw(DRAW_NODE_FCT f, DRAW_EDGE_FCT g, double x0, double x1, double y, double dy)  { pers_rb_tree::draw(f,g,V,x0,x1,y,dy); }
  53. double get_version() { return ver_num(V); }
  54. };
  55. /*{Manpage {p_dictionary} {K,I} {Persistent Dictionaries}}*/
  56. template <class K, class I>
  57. class p_dictionary : public handle_base {
  58. /*{Mdefinition
  59. An instance $D$ of the parameterized data type name is a set
  60. of items (type $p_dic_item$). Every item in $D$ contains a key from the
  61. linearly ordered data type $K$, called the key type of $D$, and an information
  62. from data type $I$, called the information type  of $D$. The number of items in
  63. $D$ is called the size of $D$. A dictionary of size zero is called empty.
  64. We use $<k,i>$ to denote an item with key $k$ and information
  65. $i$ ($i$ is said to be the information associated with key $k$).  For each
  66. $k in K$ there is at most one item $<k,i> in D$.
  67. The difference between dictionaries (cf. section ref{Dictionaries}) and 
  68. persistent dictionaries lies in the fact that update operations performed
  69. on a persistent dictionary $D$ do not change $D$ but create and return a
  70. new dictionary $D'$. For example, $D$.del($k$) returns the dictionary $D'$
  71. containing all items $it$ of $D$ with key($it$) $ne$ $k$. Also, an assignment
  72. $ D1 = D2 $ does not assign a copy of $D2$ (with new items) to $D1$ but the
  73. value of $D2$ itself.
  74. }*/
  75. PERS_DIC<K,I>* ptr() const { return (PERS_DIC<K,I>*) PTR; }
  76. public:
  77. /*{Mcreation D }*/
  78.  p_dictionary()      { PTR = new PERS_DIC<K,I>; }
  79. /*{Mcreate creates an instance var of type name and initializes var to an
  80.     empty persistent dictionary.}*/
  81.  p_dictionary(PERS_DIC<K,I>* p) { PTR = (PERS_DIC<K,I>*)p; }
  82.  p_dictionary(const p_dictionary<K,I>& p) : handle_base(p) {}
  83. ~p_dictionary()     {}
  84.  p_dictionary<K,I>& operator=(const p_dictionary<K,I>& p)
  85.  { handle_base::operator=(p); return *this; }
  86. /*{Moperations 3.8 4}*/
  87. K key(p_dic_item it)     { return ptr()->key(it); }
  88. /*{Mop        returns the key of item $it$.\
  89.                precond $it$ $in$ var.}*/
  90. I inf(p_dic_item it)     { return ptr()->inf(it); }
  91. /*{Mop        returns the information of item $it$.\
  92.        precond $it$ $in$ var.}*/
  93. p_dic_item locate(K k)      { return ptr()->locate(k); }
  94. p_dic_item locate_pred(K k) { return ptr()->locate_pred(k); }
  95. p_dic_item lookup(K k)      { return ptr()->lookup(k); }
  96. /*{Mop        returns the item with key $k$ (nil if no such
  97.        item exists in var).}*/
  98. p_dictionary<K,I> del(K k)
  99. { return new PERS_DIC<K,I>
  100.                                        (ptr()->del(k)); }
  101. /*{Mop        returns ${x in var | key(x) ne k}$.}*/
  102. p_dictionary<K,I> del_item(p_dic_item it);
  103. /*{Mopl       returns ${x in var | x ne it}$.}*/
  104. p_dictionary<K,I> insert(K k, I i)
  105. { return new PERS_DIC<K,I>
  106.                                        (ptr()->insert(k,i)); }
  107. /*{Mop        returns var.del($k$) $cup$ ${<k,i>}$.}*/
  108. p_dictionary<K,I> change_inf(p_dic_item it, I i)
  109. { return new PERS_DIC<K,I>
  110.                                        (ptr()->change_inf(it,i)); }
  111. /*{Mopl       returns var.del_item($it$) $cup$
  112.                ${<k,i>}$, where $k = key(it)$.\
  113.        precond $it$ $in$ var.}*/
  114. p_dic_item min()         { return ptr()->min();     }
  115. p_dic_item max()         { return ptr()->max();     }
  116. p_dic_item succ(p_dic_item p) { return ptr()->succ(p);   }
  117. p_dic_item succ(K k)    { return ptr()->locate(k); }
  118. p_dic_item pred(p_dic_item p) { return ptr()->pred(p);   }
  119. p_dic_item pred(K k)    { return ptr()->locate_pred(k); }
  120. p_dic_item first_item()       { return ptr()->min();     }
  121. p_dic_item next_item(p_dic_item p) { return ptr()->succ(p);   }
  122. int   size()        { return ptr()->size();    }
  123. /*{Mop        returns the size of var.}*/
  124. bool   empty()       { return ptr()->size()==0; }
  125. /*{Mop        returns true if var is empty, false otherwise.}*/
  126. void print()       { ptr()->print(); }
  127. void draw(DRAW_NODE_FCT f,DRAW_EDGE_FCT g,double x0,double x1,double y,double dy)  { ptr()->draw(f,g,x0,x1,y,dy); }
  128. }; 
  129. /*{Mimplementation
  130. Persistent dictionaries are implemented by leaf oriented 
  131. persistent red black trees.
  132. Operations insert, lookup, del_item, del take time $O(log^2 n)$, key, inf, 
  133. empty, size and change_inf take time $O(1)$. The space requirement is
  134. $O(1)$ for each update operation.}*/
  135.  
  136. #endif