00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028 #ifndef _util_container_avlset_h
00029 #define _util_container_avlset_h
00030
00031 #include <util/container/avlmap.h>
00032
00033
00034 template <class K>
00035 class AVLSet {
00036 private:
00037 AVLMap<K,int> map_;
00038 public:
00039 class iterator {
00040 private:
00041 const EAVLMMap<K, AVLMapNode<K,int> > *map_;
00042 const AVLMapNode<K, int> *node;
00043 public:
00044 iterator(): map_(0), node(0) {}
00045 iterator(const EAVLMMap<K,AVLMapNode<K,int> > *m,
00046 const AVLMapNode<K,int> *n)
00047 :map_(m), node(n) {}
00048 iterator(const eavl_typename AVLSet<K>::iterator &i):map_(i.map_),node(i.node) {}
00049 void operator++() { map_->next(node); }
00050 void operator++(int) { operator++(); }
00051 int operator == (const eavl_typename AVLSet<K>::iterator &i) const
00052 { return map_ == i.map_ && node == i.node; }
00053 int operator != (const eavl_typename AVLSet<K>::iterator &i) const
00054 { return !(map_ == i.map_ && node == i.node); }
00055 void operator = (const eavl_typename AVLSet<K>::iterator &i)
00056 { map_ = i.map_; node = i.node; }
00057 const K &key() const { return node->node.key; }
00058 const K &operator *() const { return node->node.key; }
00059
00060 };
00061 public:
00062 AVLSet() {};
00063 void clear() { map_.clear(); }
00064 void insert(const K& key) { map_.insert(key,0); }
00065 void remove(const K& key) { map_.remove(key); }
00066 int contains(const K& key) const { return map_.contains(key); }
00067 iterator find(const K& k) const;
00068
00069 int height() { return map_.height(); }
00070 void check() { map_.check(); }
00071
00072 int length() const { return map_.length(); }
00073
00074 iterator begin() const { return iterator(&map_.map_,map_.map_.start()); }
00075 iterator end() const { return iterator(&map_.map_,0); }
00076
00077 void operator -= (const AVLSet<K> &s);
00078 void operator |= (const AVLSet<K> &s);
00079
00080 void print() { map_.print(); }
00081 };
00082
00083 template <class K>
00084 void
00085 AVLSet<K>::operator -= (const AVLSet<K> &s)
00086 {
00087 for (AVLSet<K>::iterator i=s.begin(); i!=s.end(); i++) {
00088 remove(*i);
00089 }
00090 }
00091
00092 template <class K>
00093 void
00094 AVLSet<K>::operator |= (const AVLSet<K> &s)
00095 {
00096 for (AVLSet<K>::iterator i=s.begin(); i!=s.end(); i++) {
00097 insert(*i);
00098 }
00099 }
00100
00101 template <class K>
00102 inline typename AVLSet<K>::iterator
00103 AVLSet<K>::find(const K& k) const
00104 {
00105 return iterator(&map_.map_,map_.map_.find(k));
00106 }
00107
00108 #endif
00109
00110
00111
00112
00113
00114
00115