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

MultiPlatform

  1. /*******************************************************************************
  2. +
  3. +  LEDA-R  3.2.3
  4. +
  5. +  set.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_SET_H
  12. #define LEDA_SET_H
  13. //------------------------------------------------------------------------------
  14. // set             
  15. //------------------------------------------------------------------------------
  16. #include <LEDA/basic.h>
  17. #include <LEDA/impl/rs_tree.h>
  18. /*{Manpage {set} {E} {Sets}}*/
  19. template<class E>
  20. class set : private rs_tree {
  21. /*{Mdefinition
  22. An instance $S$ of the parameterized data type name is a collection of
  23. elements of the linearly ordered type $E$, called the element type of $S$. The
  24. size of $S$ is the number of elements in $S$, a set of size zero is called the
  25. empty set.}*/
  26. int  int_type()              const { return LEDA_INT_TYPE(E); }
  27. int  cmp(GenPtr x, GenPtr y) const { return LEDA_COMPARE(E,x,y); }
  28. void clear_key(GenPtr& x)    const { LEDA_CLEAR(E,x); }
  29. void copy_key(GenPtr& x)     const { LEDA_COPY(E,x); }
  30. public:
  31. /*{Mcreation S }*/
  32.  set() {}
  33. /*{Mcreate creates an instance var of type name and initializes it to 
  34.             the empty set.}*/
  35.  set(const set<E>& S) : rs_tree(S) {}
  36. ~set() { clear(); }
  37.  set<E>& operator=(const set<E>& S) 
  38.  { rs_tree::operator=(S); return *this;}
  39. /*{Moperations 1.8 5}*/
  40. virtual void insert(E x) { rs_tree::insert(Convert(x),0); }
  41. /*{Mop        adds $x$ to var.}*/
  42. virtual void del(E x) { rs_tree::del(Convert(x)); }
  43. /*{Mop        deletes $x$ from var.}*/
  44. virtual bool member(E x) const { return (rs_tree::lookup(Convert(x))!=nil); }
  45. /*{Mop        returns true if $x$ in var, false otherwise.}*/
  46. virtual E choose() const 
  47. { return LEDA_ACCESS(E,rs_tree::key(rs_tree::min())); }
  48. /*{Mop        returns an element of var.\
  49.                precond var is not empty.}*/
  50. virtual bool empty() const {return rs_tree::empty();}
  51. /*{Mop        returns true if var is empty, false otherwise.}*/
  52. virtual int size() const {return rs_tree::size();}
  53. /*{Mop        returns the size of var.}*/
  54. virtual void clear() { rs_tree::clear(); }
  55. /*{Mop        makes var the empty set.}*/
  56. // iteration
  57. virtual GenPtr first_item()  const { return rs_tree::first_item(); }
  58. virtual void loop_to_succ(GenPtr& x) const 
  59. { x=rs_tree::next_item(rs_tree_item(x)); }
  60. virtual GenPtr forall_loop_test(GenPtr it, E& y) const
  61. { if (it) y = LEDA_ACCESS(E,rs_tree::key(rs_tree_item(it)));
  62.   return it;
  63.  }
  64. #if defined(__ELSE_SCOPE_BUG__)
  65.   GenPtr forall_loop_item;
  66.   GenPtr& Forall_Loop_Item() const { return (GenPtr&)forall_loop_item; }
  67. #endif
  68. /*{Mtext
  69. bigskip
  70. {bf Iteration}
  71. medskip
  72. {bf forall}($x,S$) 
  73. ${$  ``the elements of $S$ are successively assigned to $x$''  $}$ }*/
  74. };
  75. /*{Mimplementation
  76. Sets are implemented by randomized search trees cite{AS89}. Operations insert,
  77. del, member take time $O(log n)$, empty, size take time $O(1)$, and clear 
  78. takes time $O(n)$, where $n$ is the current size of the set.}*/
  79. #endif