d_array.h
Upload User: gzelex
Upload Date: 2007-01-07
Package Size: 707k
Code Size: 4k
Development Platform:

MultiPlatform

  1. /*******************************************************************************
  2. +
  3. +  LEDA-R  3.2.3
  4. +
  5. +  d_array.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_D_ARRAY_H
  12. #define LEDA_D_ARRAY_H
  13. //------------------------------------------------------------------------------
  14. // d_array 
  15. //------------------------------------------------------------------------------
  16. #include <LEDA/impl/skiplist.h> 
  17. /*{Manpage {d_array} {I,E} {Dictionary Arrays}}*/
  18. #define DA_DEF_IMPL skiplist
  19. typedef skiplist_item d_array_item;
  20. template<class I, class E> 
  21. class d_array : public virtual DA_DEF_IMPL 
  22. {
  23. /*{Mdefinition
  24. An instance $A$ of the parameterized data type name (dictionary
  25. array) is an injective mapping from the linearly ordered data type $I$, called
  26. the index type of $A$, to the set of variables of data type $E$, called the
  27. element type of $A$. We use $A(i)$ to denote the variable with index $i$.}*/
  28. E init;
  29. int  cmp(GenPtr x, GenPtr y) const { return LEDA_COMPARE(I,x,y); }
  30. /*
  31. int  hash_fct(GenPtr x)      const { return LEDA_HASH(I,x); }
  32. */
  33. void clear_key(GenPtr& x)    const { LEDA_CLEAR(I,x); }
  34. void clear_inf(GenPtr& x)    const { LEDA_CLEAR(E,x); }
  35. void copy_key(GenPtr& x)     const { LEDA_COPY(I,x);  }
  36. void copy_inf(GenPtr& x)     const { LEDA_COPY(E,x);  }
  37. int  int_type()              const { return LEDA_INT_TYPE(I); }
  38. public:
  39. /*{Mcreation A }*/
  40. d_array()        { }
  41. d_array(E x) { init=x; }
  42. /*{Mcreate 
  43. creates an injective function $a$ from $I$ to the set of unused variables of
  44. type $E$, assigns $x$ to all variables in the range of $a$ and initializes $A$
  45. with $a$.}*/
  46. d_array<I,E>& operator=(const d_array<I,E>& A)
  47. { DA_DEF_IMPL::operator=(A); init=A.init; return *this; }
  48. d_array(const d_array<I,E>& A) : DA_DEF_IMPL(A) {init=A.init;}
  49. virtual ~d_array() { clear(); }
  50. /*{Moperations 2 4 }*/
  51. virtual E&  operator[](const I& i) 
  52. { d_array_item it=DA_DEF_IMPL::lookup(Convert(i));
  53.   if (it==nil) 
  54.   { GenPtr p = Convert(init);
  55.     it=DA_DEF_IMPL::insert(Convert(i),p);
  56.    }
  57.   return LEDA_ACCESS(E,info(it)); 
  58. }
  59. /*{Marrop    returns the variable $A(i)$.}*/
  60. virtual bool defined(I i)  const 
  61. { return (DA_DEF_IMPL::lookup(Convert(i))!=nil); }
  62. /*{Mop      returns true if $i in dom(A)$, false otherwise; here
  63.              $dom(A)$ is the set of all $iin I$ for which $A[i]$ has
  64.              already been executed.}*/
  65. virtual void undefine(I i)
  66. { DA_DEF_IMPL::del(Convert(i)); }
  67. /*{Mop      removes $i$ from $dom(A)$. } */
  68. // iteration
  69. virtual void loop_to_succ(GenPtr& x) const 
  70.    { x = DA_DEF_IMPL::next_item(d_array_item(x)); }
  71. virtual GenPtr forall_defined_test(GenPtr it, I& x) const
  72. { if (it) x = LEDA_ACCESS(I,key(d_array_item(it)));
  73.   return it;
  74. }
  75. virtual GenPtr forall_loop_test(GenPtr it, E& x) const
  76. { if (it) x = LEDA_ACCESS(E,inf(d_array_item(it)));
  77.   return it;
  78. }
  79. #if defined(__ELSE_SCOPE_BUG__)
  80. GenPtr forall_loop_item;
  81. GenPtr& Forall_Loop_Item() const { return (GenPtr&)forall_loop_item; }
  82. #endif
  83. };
  84. /*{Mtext     
  85. bigskip
  86. {bf Iteration} }*/
  87. /*{Mtext
  88. {bf forall_defined}($i,A$) 
  89. ${$ ``the elements from $dom(A)$ are successively assigned to $i$'' $}$ }*/
  90. /*{Mtext
  91. {bf forall}($x,A$) 
  92. ${$ ``for all $i in dom(A)$ the entries $A[i]$ are successively assigned to $x$'' $}$ }*/
  93. /*{Mimplementation
  94. Dictionary arrays are implemented by randomized search trees cite{AS89}. 
  95. Access operations $A[i]$ take time $O(log dom(A))$.
  96. The space requirement is $O(dom(A))$.}*/
  97.   
  98. /*{Mexample
  99. {bf Program 1}:
  100. We use a dictionary array to count the number of occurrences of the elements in a 
  101. sequence of strings.
  102. begingroup
  103. ttbig
  104. {obeyspacesgdef { }}
  105. ttverbatim
  106. #include <LEDA/d_array.h>
  107. main()
  108.   d_array<string,int> N(0);
  109.   string s;
  110.   while (cin >> s) N[s]++;
  111.   forall_defined(s,N) cout << s << "  " << N[s] << endl;
  112. }
  113. endgroup
  114. bigskip
  115. {bf Program 2}:
  116. We use a $d_array<string,string>$ to realize an english/german dictionary.
  117. begingroup
  118. ttbig
  119. {obeyspacesgdef { }}
  120. ttverbatim
  121. #include <LEDA/d_array.h>
  122. main()
  123.   d_array<string,string> dic;
  124.   dic["hello"] = "hallo";
  125.   dic["world"] = "Welt";
  126.   dic["book"]  = "Buch";
  127.   dic["key"]   = "Schluessel";
  128.   
  129.   string s;
  130.   forall_defined(s,dic) cout << s << "  " << dic[s] << endl;
  131. }
  132. endgroup
  133. }*/
  134. #endif