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

MultiPlatform

  1. /*******************************************************************************
  2. +
  3. +  LEDA-R  3.2.3
  4. +
  5. +  sortseq.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_SORTSEQ_H
  12. #define LEDA_SORTSEQ_H
  13. /*{Manpage {sortseq} {K,I} {Sorted Sequences}}*/ 
  14. /*
  15. #include <LEDA/impl/rs_tree.h>
  16. #define SEQ_DEF_IMPL rs_tree
  17. #define SEQ_BASE_IMPL bin_tree
  18. typedef rs_tree_item seq_item;
  19. */
  20. #include <LEDA/impl/skiplist.h>
  21. #define SEQ_DEF_IMPL skiplist
  22. #define SEQ_BASE_IMPL skiplist
  23. typedef skiplist_item seq_item;
  24. template<class K, class I>
  25. class sortseq : public virtual SEQ_DEF_IMPL {
  26. /*{Mdefinition
  27. An instance $S$ of the parameterized data type name is a sequence of
  28. items ($seq_item$). Every item contains a key from the linearly ordered data
  29. type $K$, called the key type of $S$, and an information from data type $I$,
  30. called the information type  of $S$. The number of items in $S$ is called the
  31. size of $S$. A sorted sequence of size zero is called empty. We use $<k,i>$ to
  32. denote a $seq_item$ with key $k$ and information $i$ (called the information
  33. associated with key $k$). For each $k in K$ there is at most one item
  34. $<k,i> in S$.
  35. The linear order on $K$ may be time-dependent, e.g., in an algorithm that
  36. sweeps an arrangement of lines by a vertical sweep line we may want to order
  37. the lines by the y-coordinates of their intersections with the sweep line.
  38. However, whenever an operation (except for reverse_items) is applied to a
  39. sorted sequence $S$, the keys of $S$ must form an increasing sequence according
  40. to the currently valid linear order on $K$. For operation reverse_items this
  41. must hold after the execution of the operation.}*/
  42. int  int_type()              const { return LEDA_INT_TYPE(K); }
  43. int  cmp(GenPtr x, GenPtr y) const { return LEDA_COMPARE(K,x,y); }
  44. void clear_key(GenPtr& x)    const { LEDA_CLEAR(K,x); }
  45. void clear_inf(GenPtr& x)    const { LEDA_CLEAR(I,x); }
  46. void copy_key(GenPtr& x)     const { LEDA_COPY(K,x); }
  47. void copy_inf(GenPtr& x)     const { LEDA_COPY(I,x); }
  48. void print_key(GenPtr x)     const { LEDA_PRINT(K,x,cout); }
  49. void print_inf(GenPtr x)     const { LEDA_PRINT(I,x,cout); }
  50. public:
  51. /*{Mcreation S }*/
  52. sortseq() {}
  53. /*{Mcreate creates an instance var of type name and initializes it to 
  54.             the empty sorted sequence.}*/
  55. sortseq(const sortseq<K,I>& w) : SEQ_BASE_IMPL(w) {}
  56. sortseq<K,I>& operator=(const sortseq<K,I>& w)
  57. { SEQ_DEF_IMPL::operator=(w); return *this; }
  58. virtual ~sortseq()   { clear(); }
  59. /*{Moperations 2.8 3.5}*/
  60. virtual K  key(seq_item it) const 
  61. { return LEDA_ACCESS(K,SEQ_DEF_IMPL::key(it)); }
  62. /*{Mop         returns the key of item $it$.\
  63.         precond $it$ is an item in var.}*/
  64. virtual I  inf(seq_item it) const 
  65. { return LEDA_ACCESS(I,SEQ_DEF_IMPL::inf(it)); } 
  66. /*{Mop         returns the information of item $it.$\
  67.         precond $it$ is an item in var.}*/
  68. virtual seq_item lookup(K k) const 
  69. { return SEQ_DEF_IMPL::lookup(Convert(k)); }
  70. /*{Mop         returns the item with key $k$
  71.         (nil if no such item exists in var).}*/
  72. virtual seq_item insert(K k, I i)
  73. { return SEQ_DEF_IMPL::insert(Convert(k),Convert(i)); }
  74. /*{Mop         associates information $i$ with key $k$: If
  75.         there is an item $<k,j>$ in var then $j$ is
  76. replaced by $i$, else a new item $<k,i>$ is
  77. added to var. In both cases the item is 
  78. returned.}*/
  79. virtual seq_item insert_at(seq_item it, K k, I i)
  80. { return SEQ_DEF_IMPL::insert_at_item(it,Convert(k),Convert(i)); }
  81. /*{Mopl        Like insert($k,i$), the item $it$ gives the 
  82.         position of the item $<k,i>$ in the sequence.\
  83. precond $it$ is an item in $S$ with either
  84. key($it$) is maximal with key($it) le k$ or
  85. key($it$) is minimal with key$(it) ge k$.}*/
  86. virtual seq_item locate_succ(K k) const
  87. { return SEQ_DEF_IMPL::locate_succ(Convert(k)); }
  88. /*{Mop         returns the item $<k',i>$ in var such that
  89.         $k'$ is minimal with $k' ge k$ (nil if no
  90. such item exists).}*/
  91. virtual seq_item locate_pred(K k) const
  92. { return SEQ_DEF_IMPL::locate_pred(Convert(k)); }
  93. /*{Mop         returns the item $<k',i>$ in var such that
  94.         $k'$ is maximal with $k' le k$ (nil if no
  95. such item exists).}*/
  96. virtual seq_item locate(K k) const
  97. { return SEQ_DEF_IMPL::locate_succ(Convert(k)); }
  98. /*{Mop         returns var.locate_succ$(K k)$. }*/
  99. virtual seq_item succ(seq_item it) const { return SEQ_DEF_IMPL::succ(it); }
  100. /*{Mop        returns the successor item of $it$, i.e., the
  101.        item $<k,i>$ in var such that $k$ is minimal
  102.        with $k > key(it)$ (nil if no such item exists).\
  103.        precond $it$ is an item in var.}*/
  104. virtual seq_item pred(seq_item it) const { return SEQ_DEF_IMPL::pred(it); }
  105. /*{Mop        returns the predecessor item of $it$, i.e., the
  106.        item $<k,i>$ in var such that $k$ is maximal
  107.        with $k < key(it)$ (nil if no such item exists).\
  108.        precond $it$ is an item in var.}*/
  109. virtual seq_item min() const { return SEQ_DEF_IMPL::min(); }
  110. /*{Mop         returns the item with minimal key
  111.         (nil if var is empty).}*/
  112. virtual seq_item max() const { return SEQ_DEF_IMPL::max(); }
  113. /*{Mop         returns the item with maximal key
  114.         (nil if var is empty).}*/
  115. virtual void flip_items(seq_item a, seq_item b)    { reverse_items(a,b); }
  116. virtual void del(K k)         { SEQ_DEF_IMPL::del(Convert(k)); } 
  117. /*{Mop         removes the item with key $k$ from var
  118.         (null operation if no such item exists).}*/
  119. virtual void del_item(seq_item it)  { SEQ_DEF_IMPL::del_item(it); } 
  120. /*{Mopl        removes the item $it$ from var.\
  121.         precond $it$ is an item in var.}*/
  122. virtual void change_inf(seq_item it, I i) { SEQ_DEF_IMPL::change_inf(it,Convert(i));}
  123. /*{Mopl         makes $i$ the information of item $it$.\
  124.         precond $it$ is an item in var.}*/
  125. virtual void reverse_items(seq_item a, seq_item b) { SEQ_DEF_IMPL::reverse_items(a,b); }
  126. /*{Mopl       the subsequence of $S$ from $a$ to $b$ is reversed.\
  127.        precond $a$ appears before $b$ in $S$.}*/
  128. virtual void split(seq_item it,sortseq<K,I>& S1, sortseq<K,I>& S2)
  129. { SEQ_DEF_IMPL::split_at_item(it,(SEQ_DEF_IMPL&)S1,(SEQ_DEF_IMPL&)S2); }
  130. /*{Mopl      splits $S$ at item $it$ into sequences $S_1$ and $S_2$
  131.       and makes $S$ empty. More precisely, if
  132.       $S = x_1,dots,x_{k-1},it,x_{k+1},dots,x_n$ then
  133.       $S_1 = x_1,dots,x_{k-1},it$ and $S_2 = x_{k+1},dots,x_n$.\
  134.       precond $it$ is an item in $S$. }*/
  135. virtual sortseq<K,I>& conc(sortseq<K,I>& S1) 
  136. { SEQ_DEF_IMPL::conc((SEQ_DEF_IMPL&)S1); return *this; }
  137. /*{Mopl      appends $S_1$ to $S$, makes $S_1$ empty and
  138.      returns $S$.precond
  139.      $S$.key($S$.max()) $<$ $S_1$.key($S_1$.min()).}*/
  140. virtual void     clear() { SEQ_DEF_IMPL::clear(); }
  141. /*{Mop      makes var the empty sorted sequence.}*/
  142. virtual int      size()  const { return SEQ_DEF_IMPL::size(); }
  143. /*{Mop      returns the size of var.}*/
  144. virtual bool     empty() const { return (size()==0) ? true : false; }
  145. /*{Mop      returns true if var is empty, false otherwise.}*/
  146. virtual seq_item first_item() const 
  147. { return SEQ_DEF_IMPL::first_item(); }
  148. virtual seq_item next_item(seq_item it) const 
  149. { return SEQ_DEF_IMPL::next_item(it);}
  150. };
  151. /*{Mimplementation
  152. Sorted sequences are implemented by (2,4)-trees. Operations lookup, locate, 
  153. insert, del, split, conc take time $O(log n)$, operations succ, pred, max, 
  154. min, key, inf, insert_at and del_item take time $O(1)$. Clear takes 
  155. time $O(n)$ and reverse_items $O(ell)$, where $ell$ is the length of the 
  156. reversed subsequence. The space requirement is $O(n)$. Here $n$ is the current 
  157. size of the sequence.}*/
  158. /*{Mexample
  159. We use a sorted sequence to list all elements in a sequence of strings lying
  160. lexicographically between two given search strings. We first read a sequence of strings terminated by "stop" and then a pair of search strings. We output all
  161. strings that lie lexicographically between the two search strings (inclusive).
  162. begingroup
  163. ttbig
  164. {obeyspacesgdef { }}
  165. ttverbatim
  166. #include <LEDA/sortseq.h>
  167. main()
  168.  sortseq<string,int> S;
  169.  string s1,s2;
  170.  while ( cin >> s1 &&  s1 != "stop" )  S.insert(s1,0);
  171.  while ( cin >> s1 >> s2 )
  172.  { seq_item start = S.locate_succ(s1);
  173.    seq_item stop  = S.locate_pred(s2);
  174.    if (S.key(start) <= S.key(stop))
  175.    { for (seq_item it = start; it != stop; it = S.succ(it))
  176.       cout << S.key(it) << endl; 
  177.    }
  178.   }
  179. }
  180. endgroup
  181. }*/
  182. #endif